Botan  2.7.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  std::vector<uint8_t> output;
41 
45  .end_cons();
46 
47  return output;
48  }
49 
50 /*
51 * Default OID access
52 */
54  {
55  try {
56  return OIDS::lookup(algo_name());
57  }
58  catch(Lookup_Error&)
59  {
60  throw Lookup_Error("PK algo " + algo_name() + " has no defined OIDs");
61  }
62  }
63 
65  {
66  const size_t PKCS8_VERSION = 0;
67 
68  return DER_Encoder()
70  .encode(PKCS8_VERSION)
73  .end_cons()
74  .get_contents();
75  }
76 
77 /*
78 * Hash of the X.509 subjectPublicKey encoding
79 */
80 std::string Public_Key::fingerprint_public(const std::string& hash_algo) const
81  {
83  }
84 
85 /*
86 * Hash of the PKCS #8 encoding for this key object
87 */
88 std::string Private_Key::fingerprint_private(const std::string& hash_algo) const
89  {
91  }
92 
93 std::unique_ptr<PK_Ops::Encryption>
95  const std::string& /*params*/,
96  const std::string& /*provider*/) const
97  {
98  throw Lookup_Error(algo_name() + " does not support encryption");
99  }
100 
101 std::unique_ptr<PK_Ops::KEM_Encryption>
103  const std::string& /*params*/,
104  const std::string& /*provider*/) const
105  {
106  throw Lookup_Error(algo_name() + " does not support KEM encryption");
107  }
108 
109 std::unique_ptr<PK_Ops::Verification>
110 Public_Key::create_verification_op(const std::string& /*params*/,
111  const std::string& /*provider*/) const
112  {
113  throw Lookup_Error(algo_name() + " does not support verification");
114  }
115 
116 std::unique_ptr<PK_Ops::Decryption>
118  const std::string& /*params*/,
119  const std::string& /*provider*/) const
120  {
121  throw Lookup_Error(algo_name() + " does not support decryption");
122  }
123 
124 std::unique_ptr<PK_Ops::KEM_Decryption>
126  const std::string& /*params*/,
127  const std::string& /*provider*/) const
128  {
129  throw Lookup_Error(algo_name() + " does not support KEM decryption");
130  }
131 
132 std::unique_ptr<PK_Ops::Signature>
134  const std::string& /*params*/,
135  const std::string& /*provider*/) const
136  {
137  throw Lookup_Error(algo_name() + " does not support signatures");
138  }
139 
140 std::unique_ptr<PK_Ops::Key_Agreement>
142  const std::string& /*params*/,
143  const std::string& /*provider*/) const
144  {
145  throw Lookup_Error(algo_name() + " does not support key agreement");
146  }
147 
148 }
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
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:133
virtual secure_vector< uint8_t > private_key_bits() const =0
secure_vector< uint8_t > get_contents()
Definition: der_enc.cpp:152
virtual std::string algo_name() const =0
std::string fingerprint_public(const std::string &alg="SHA-256") const
Definition: pk_keys.cpp:80
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:125
DER_Encoder & end_cons()
Definition: der_enc.cpp:191
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:102
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:117
std::string fingerprint_private(const std::string &alg) const
Definition: pk_keys.cpp:88
DER_Encoder & encode(bool b)
Definition: der_enc.cpp:285
secure_vector< uint8_t > private_key_info() const
Definition: pk_keys.cpp:64
virtual OID get_oid() const
Definition: pk_keys.cpp:53
Definition: alg_id.cpp:13
AlgorithmIdentifier hash_algo
Definition: x509_obj.cpp:23
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:181
virtual std::unique_ptr< PK_Ops::Verification > create_verification_op(const std::string &params, const std::string &provider) const
Definition: pk_keys.cpp:110
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:94
std::string lookup(const OID &oid)
Definition: oids.cpp:113
std::vector< uint8_t > subject_public_key() const
Definition: pk_keys.cpp:38
virtual AlgorithmIdentifier pkcs8_algorithm_identifier() const
Definition: pk_keys.h:192
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:141