Botan  2.1.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::vector<uint8_t> Public_Key::subject_public_key() const
18  {
19  return DER_Encoder()
23  .end_cons()
25  }
26 
27 /*
28 * Default OID access
29 */
31  {
32  try {
33  return OIDS::lookup(algo_name());
34  }
35  catch(Lookup_Error&)
36  {
37  throw Lookup_Error("PK algo " + algo_name() + " has no defined OIDs");
38  }
39  }
40 
42  {
43  const size_t PKCS8_VERSION = 0;
44 
45  return DER_Encoder()
47  .encode(PKCS8_VERSION)
50  .end_cons()
51  .get_contents();
52  }
53 
54 /*
55 * Hash of the PKCS #8 encoding for this key object
56 */
57 std::string Private_Key::fingerprint(const std::string& alg) const
58  {
60  std::unique_ptr<HashFunction> hash(HashFunction::create(alg));
61  hash->update(buf);
62  const auto hex_print = hex_encode(hash->final());
63 
64  std::string formatted_print;
65 
66  for(size_t i = 0; i != hex_print.size(); i += 2)
67  {
68  formatted_print.push_back(hex_print[i]);
69  formatted_print.push_back(hex_print[i+1]);
70 
71  if(i != hex_print.size() - 2)
72  formatted_print.push_back(':');
73  }
74 
75  return formatted_print;
76  }
77 
78 std::unique_ptr<PK_Ops::Encryption>
80  const std::string& /*params*/,
81  const std::string& /*provider*/) const
82  {
83  throw Lookup_Error(algo_name() + " does not support encryption");
84  }
85 
86 std::unique_ptr<PK_Ops::KEM_Encryption>
88  const std::string& /*params*/,
89  const std::string& /*provider*/) const
90  {
91  throw Lookup_Error(algo_name() + " does not support KEM encryption");
92  }
93 
94 std::unique_ptr<PK_Ops::Verification>
95 Public_Key::create_verification_op(const std::string& /*params*/,
96  const std::string& /*provider*/) const
97  {
98  throw Lookup_Error(algo_name() + " does not support verification");
99  }
100 
101 std::unique_ptr<PK_Ops::Decryption>
103  const std::string& /*params*/,
104  const std::string& /*provider*/) const
105  {
106  throw Lookup_Error(algo_name() + " does not support decryption");
107  }
108 
109 std::unique_ptr<PK_Ops::KEM_Decryption>
111  const std::string& /*params*/,
112  const std::string& /*provider*/) const
113  {
114  throw Lookup_Error(algo_name() + " does not support KEM decryption");
115  }
116 
117 std::unique_ptr<PK_Ops::Signature>
119  const std::string& /*params*/,
120  const std::string& /*provider*/) const
121  {
122  throw Lookup_Error(algo_name() + " does not support signatures");
123  }
124 
125 std::unique_ptr<PK_Ops::Key_Agreement>
127  const std::string& /*params*/,
128  const std::string& /*provider*/) const
129  {
130  throw Lookup_Error(algo_name() + " does not support key agreement");
131  }
132 
133 }
std::vector< uint8_t > get_contents_unlocked()
Definition: der_enc.h:27
virtual std::vector< uint8_t > public_key_bits() 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:118
std::string fingerprint(const std::string &alg="SHA") const
Definition: pk_keys.cpp:57
virtual AlgorithmIdentifier algorithm_identifier() const =0
virtual secure_vector< uint8_t > private_key_bits() const =0
secure_vector< uint8_t > get_contents()
Definition: der_enc.cpp:124
virtual std::string algo_name() const =0
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:79
DER_Encoder & end_cons()
Definition: der_enc.cpp:147
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:102
virtual OID get_oid() const
Definition: pk_keys.cpp:30
DER_Encoder & encode(bool b)
Definition: der_enc.cpp:216
std::vector< T, secure_allocator< T >> secure_vector
Definition: secmem.h:121
std::string lookup(const OID &oid)
Definition: oids.cpp:18
static std::unique_ptr< HashFunction > create(const std::string &algo_spec, const std::string &provider="")
Definition: hash.cpp:93
virtual AlgorithmIdentifier pkcs8_algorithm_identifier() const
Definition: pk_keys.h:188
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:110
Definition: alg_id.cpp:13
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:126
secure_vector< uint8_t > private_key_info() const
Definition: pk_keys.cpp:41
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:87
DER_Encoder & start_cons(ASN1_Tag type_tag, ASN1_Tag class_tag=UNIVERSAL)
Definition: der_enc.cpp:137
void hex_encode(char output[], const uint8_t input[], size_t input_length, bool uppercase)
Definition: hex.cpp:14
virtual std::unique_ptr< PK_Ops::Verification > create_verification_op(const std::string &params, const std::string &provider) const
Definition: pk_keys.cpp:95
std::vector< uint8_t > subject_public_key() const
Definition: pk_keys.cpp:17
MechanismType hash