Botan  2.4.0
Crypto and TLS for C++11
pk_keys.cpp
Go to the documentation of this file.
1 /*
2 * PK Key Types
3 * (C) 1999-2007 Jack Lloyd
4 *
5 * Botan is released under the Simplified BSD License (see license.txt)
6 */
7 
8 #include <botan/pk_keys.h>
9 #include <botan/pk_ops.h>
10 #include <botan/der_enc.h>
11 #include <botan/oids.h>
12 #include <botan/hash.h>
13 #include <botan/hex.h>
14 
15 namespace Botan {
16 
17 std::string create_hex_fingerprint(const uint8_t bits[],
18  size_t bits_len,
19  const std::string& hash_name)
20  {
21  std::unique_ptr<HashFunction> hash_fn(HashFunction::create_or_throw(hash_name));
22  const std::string hex_hash = hex_encode(hash_fn->process(bits, bits_len));
23 
24  std::string fprint;
25 
26  for(size_t i = 0; i != hex_hash.size(); i += 2)
27  {
28  if(i != 0)
29  fprint.push_back(':');
30 
31  fprint.push_back(hex_hash[i]);
32  fprint.push_back(hex_hash[i+1]);
33  }
34 
35  return fprint;
36  }
37 
38 std::vector<uint8_t> Public_Key::subject_public_key() const
39  {
40  return DER_Encoder()
44  .end_cons()
46  }
47 
48 /*
49 * Default OID access
50 */
52  {
53  try {
54  return OIDS::lookup(algo_name());
55  }
56  catch(Lookup_Error&)
57  {
58  throw Lookup_Error("PK algo " + algo_name() + " has no defined OIDs");
59  }
60  }
61 
63  {
64  const size_t PKCS8_VERSION = 0;
65 
66  return DER_Encoder()
68  .encode(PKCS8_VERSION)
69  .encode(pkcs8_algorithm_identifier())
70  .encode(private_key_bits(), OCTET_STRING)
71  .end_cons()
72  .get_contents();
73  }
74 
75 /*
76 * Hash of the X.509 subjectPublicKey encoding
77 */
78 std::string Public_Key::fingerprint_public(const std::string& hash_algo) const
79  {
80  return create_hex_fingerprint(subject_public_key(), hash_algo);
81  }
82 
83 /*
84 * Hash of the PKCS #8 encoding for this key object
85 */
86 std::string Private_Key::fingerprint_private(const std::string& hash_algo) const
87  {
88  return create_hex_fingerprint(private_key_bits(), hash_algo);
89  }
90 
91 std::unique_ptr<PK_Ops::Encryption>
93  const std::string& /*params*/,
94  const std::string& /*provider*/) const
95  {
96  throw Lookup_Error(algo_name() + " does not support encryption");
97  }
98 
99 std::unique_ptr<PK_Ops::KEM_Encryption>
101  const std::string& /*params*/,
102  const std::string& /*provider*/) const
103  {
104  throw Lookup_Error(algo_name() + " does not support KEM encryption");
105  }
106 
107 std::unique_ptr<PK_Ops::Verification>
108 Public_Key::create_verification_op(const std::string& /*params*/,
109  const std::string& /*provider*/) const
110  {
111  throw Lookup_Error(algo_name() + " does not support verification");
112  }
113 
114 std::unique_ptr<PK_Ops::Decryption>
116  const std::string& /*params*/,
117  const std::string& /*provider*/) const
118  {
119  throw Lookup_Error(algo_name() + " does not support decryption");
120  }
121 
122 std::unique_ptr<PK_Ops::KEM_Decryption>
124  const std::string& /*params*/,
125  const std::string& /*provider*/) const
126  {
127  throw Lookup_Error(algo_name() + " does not support KEM decryption");
128  }
129 
130 std::unique_ptr<PK_Ops::Signature>
132  const std::string& /*params*/,
133  const std::string& /*provider*/) const
134  {
135  throw Lookup_Error(algo_name() + " does not support signatures");
136  }
137 
138 std::unique_ptr<PK_Ops::Key_Agreement>
140  const std::string& /*params*/,
141  const std::string& /*provider*/) const
142  {
143  throw Lookup_Error(algo_name() + " does not support key agreement");
144  }
145 
146 }
void hex_encode(char output[], const uint8_t input[], size_t input_length, bool uppercase)
Definition: hex.cpp:14
static std::unique_ptr< HashFunction > create_or_throw(const std::string &algo_spec, const std::string &provider="")
Definition: hash.cpp:345
std::vector< uint8_t > get_contents_unlocked()
Definition: der_enc.h:27
virtual std::vector< uint8_t > public_key_bits() const =0
virtual AlgorithmIdentifier algorithm_identifier() const =0
virtual std::unique_ptr< PK_Ops::Signature > create_signature_op(RandomNumberGenerator &rng, const std::string &params, const std::string &provider) const
Definition: pk_keys.cpp:131
secure_vector< uint8_t > get_contents()
Definition: der_enc.cpp:123
virtual std::string algo_name() const =0
std::string fingerprint_public(const std::string &alg="SHA-256") const
Definition: pk_keys.cpp:78
virtual std::unique_ptr< PK_Ops::KEM_Decryption > create_kem_decryption_op(RandomNumberGenerator &rng, const std::string &params, const std::string &provider) const
Definition: pk_keys.cpp:123
DER_Encoder & end_cons()
Definition: der_enc.cpp:146
virtual std::unique_ptr< PK_Ops::KEM_Encryption > create_kem_encryption_op(RandomNumberGenerator &rng, const std::string &params, const std::string &provider) const
Definition: pk_keys.cpp:100
virtual std::unique_ptr< PK_Ops::Decryption > create_decryption_op(RandomNumberGenerator &rng, const std::string &params, const std::string &provider) const
Definition: pk_keys.cpp:115
std::string fingerprint_private(const std::string &alg) const
Definition: pk_keys.cpp:86
DER_Encoder & encode(bool b)
Definition: der_enc.cpp:202
secure_vector< uint8_t > private_key_info() const
Definition: pk_keys.cpp:62
virtual OID get_oid() const
Definition: pk_keys.cpp:51
Definition: alg_id.cpp:13
AlgorithmIdentifier hash_algo
Definition: x509_obj.cpp:22
std::string create_hex_fingerprint(const uint8_t bits[], size_t bits_len, const std::string &hash_name)
Definition: pk_keys.cpp:17
DER_Encoder & start_cons(ASN1_Tag type_tag, ASN1_Tag class_tag=UNIVERSAL)
Definition: der_enc.cpp:136
virtual std::unique_ptr< PK_Ops::Verification > create_verification_op(const std::string &params, const std::string &provider) const
Definition: pk_keys.cpp:108
std::vector< T, secure_allocator< T > > secure_vector
Definition: secmem.h:88
virtual std::unique_ptr< PK_Ops::Encryption > create_encryption_op(RandomNumberGenerator &rng, const std::string &params, const std::string &provider) const
Definition: pk_keys.cpp:92
std::string lookup(const OID &oid)
Definition: oids.cpp:18
std::vector< uint8_t > subject_public_key() const
Definition: pk_keys.cpp:38
virtual std::unique_ptr< PK_Ops::Key_Agreement > create_key_agreement_op(RandomNumberGenerator &rng, const std::string &params, const std::string &provider) const
Definition: pk_keys.cpp:139