Botan  2.10.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 
42  DER_Encoder(output).start_cons(SEQUENCE)
43  .encode(algorithm_identifier())
44  .encode(public_key_bits(), BIT_STRING)
45  .end_cons();
46 
47  return output;
48  }
49 
50 /*
51 * Default OID access
52 */
53 OID Public_Key::get_oid() const
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 
64 secure_vector<uint8_t> Private_Key::private_key_info() const
65  {
66  const size_t PKCS8_VERSION = 0;
67 
68  return DER_Encoder()
69  .start_cons(SEQUENCE)
70  .encode(PKCS8_VERSION)
71  .encode(pkcs8_algorithm_identifier())
72  .encode(private_key_bits(), OCTET_STRING)
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  {
82  return create_hex_fingerprint(subject_public_key(), hash_algo);
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  {
90  return create_hex_fingerprint(private_key_bits(), hash_algo);
91  }
92 
93 std::unique_ptr<PK_Ops::Encryption>
94 Public_Key::create_encryption_op(RandomNumberGenerator& /*rng*/,
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>
102 Public_Key::create_kem_encryption_op(RandomNumberGenerator& /*rng*/,
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>
117 Private_Key::create_decryption_op(RandomNumberGenerator& /*rng*/,
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>
125 Private_Key::create_kem_decryption_op(RandomNumberGenerator& /*rng*/,
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>
133 Private_Key::create_signature_op(RandomNumberGenerator& /*rng*/,
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>
141 Private_Key::create_key_agreement_op(RandomNumberGenerator& /*rng*/,
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
RandomNumberGenerator const std::string & hash_fn
Definition: x509_ca.h:230
BigInt size_t bits
Definition: numthry.h:210
const char * algo_name
Definition: ffi.h:932
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
void BlockCipher const uint8_t size_t uint8_t output[]
Definition: package.h:29
std::string lookup(const OID &oid)
Definition: oids.cpp:113
std::string size_t const std::string & hash_name
Definition: pk_keys.h:305