Botan 2.19.5 Crypto and TLS for C&

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
17std::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
38std::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 const OID o = OIDS::str2oid_or_empty(algo_name());
56 if(o.empty())
57 throw Lookup_Error("PK algo " + algo_name() + " has no defined OIDs");
58 return o;
59 }
60
61 secure_vector<uint8_t> Private_Key::private_key_info() const
62 {
63 const size_t PKCS8_VERSION = 0;
64
65 return DER_Encoder()
66 .start_cons(SEQUENCE)
67 .encode(PKCS8_VERSION)
68 .encode(pkcs8_algorithm_identifier())
69 .encode(private_key_bits(), OCTET_STRING)
70 .end_cons()
71 .get_contents();
72 }
73
74/*
75* Hash of the X.509 subjectPublicKey encoding
76*/
77std::string Public_Key::fingerprint_public(const std::string& hash_algo) const
78 {
79 return create_hex_fingerprint(subject_public_key(), hash_algo);
80 }
81
82/*
83* Hash of the PKCS #8 encoding for this key object
84*/
85std::string Private_Key::fingerprint_private(const std::string& hash_algo) const
86 {
87 return create_hex_fingerprint(private_key_bits(), hash_algo);
88 }
89
90std::unique_ptr<PK_Ops::Encryption>
91 Public_Key::create_encryption_op(RandomNumberGenerator& /*rng*/,
92 const std::string& /*params*/,
93 const std::string& /*provider*/) const
94 {
95 throw Lookup_Error(algo_name() + " does not support encryption");
96 }
97
98std::unique_ptr<PK_Ops::KEM_Encryption>
99 Public_Key::create_kem_encryption_op(RandomNumberGenerator& /*rng*/,
100 const std::string& /*params*/,
101 const std::string& /*provider*/) const
102 {
103 throw Lookup_Error(algo_name() + " does not support KEM encryption");
104 }
105
106std::unique_ptr<PK_Ops::Verification>
107 Public_Key::create_verification_op(const std::string& /*params*/,
108 const std::string& /*provider*/) const
109 {
110 throw Lookup_Error(algo_name() + " does not support verification");
111 }
112
113std::unique_ptr<PK_Ops::Decryption>
114 Private_Key::create_decryption_op(RandomNumberGenerator& /*rng*/,
115 const std::string& /*params*/,
116 const std::string& /*provider*/) const
117 {
118 throw Lookup_Error(algo_name() + " does not support decryption");
119 }
120
121std::unique_ptr<PK_Ops::KEM_Decryption>
122 Private_Key::create_kem_decryption_op(RandomNumberGenerator& /*rng*/,
123 const std::string& /*params*/,
124 const std::string& /*provider*/) const
125 {
126 throw Lookup_Error(algo_name() + " does not support KEM decryption");
127 }
128
129std::unique_ptr<PK_Ops::Signature>
130 Private_Key::create_signature_op(RandomNumberGenerator& /*rng*/,
131 const std::string& /*params*/,
132 const std::string& /*provider*/) const
133 {
134 throw Lookup_Error(algo_name() + " does not support signatures");
135 }
136
137std::unique_ptr<PK_Ops::Key_Agreement>
138 Private_Key::create_key_agreement_op(RandomNumberGenerator& /*rng*/,
139 const std::string& /*params*/,
140 const std::string& /*provider*/) const
141 {
142 throw Lookup_Error(algo_name() + " does not support key agreement");
143 }
144
145}
Botan::DER_Encoder
Definition: der_enc.h:23
Botan::DER_Encoder::get_contents
secure_vector< uint8_t > get_contents()
Definition: der_enc.cpp:152
Botan::DER_Encoder::start_cons
DER_Encoder & start_cons(ASN1_Tag type_tag, ASN1_Tag class_tag=UNIVERSAL)
Definition: der_enc.cpp:181
Botan::DER_Encoder::end_cons
DER_Encoder & end_cons()
Definition: der_enc.cpp:191
Botan::DER_Encoder::encode
DER_Encoder & encode(bool b)
Definition: der_enc.cpp:285
Botan::HashFunction::create_or_throw
static std::unique_ptr< HashFunction > create_or_throw(const std::string &algo_spec, const std::string &provider="")
Definition: hash.cpp:329
Botan::Lookup_Error
Definition: exceptn.h:248
Botan::OID
Definition: asn1_obj.h:194
Botan::OID::empty
bool empty() const
Definition: asn1_obj.h:232
Botan::Private_Key::fingerprint_private
std::string fingerprint_private(const std::string &alg) const
Definition: pk_keys.cpp:85
Botan::Private_Key::pkcs8_algorithm_identifier
virtual AlgorithmIdentifier pkcs8_algorithm_identifier() const
Definition: pk_keys.h:204
Botan::Private_Key::create_signature_op
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:130
Botan::Private_Key::create_key_agreement_op
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:138
Botan::Private_Key::private_key_info
secure_vector< uint8_t > private_key_info() const
Definition: pk_keys.cpp:61
Botan::Private_Key::create_decryption_op
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:114
Botan::Private_Key::create_kem_decryption_op
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:122
Botan::Private_Key::private_key_bits
virtual secure_vector< uint8_t > private_key_bits() const =0
Botan::Public_Key::get_oid
virtual OID get_oid() const
Definition: pk_keys.cpp:53
Botan::Public_Key::create_encryption_op
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:91
Botan::Public_Key::algorithm_identifier
virtual AlgorithmIdentifier algorithm_identifier() const =0
Botan::Public_Key::public_key_bits
virtual std::vector< uint8_t > public_key_bits() const =0
Botan::Public_Key::fingerprint_public
std::string fingerprint_public(const std::string &alg="SHA-256") const
Definition: pk_keys.cpp:77
Botan::Public_Key::create_kem_encryption_op
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:99
Botan::Public_Key::subject_public_key
std::vector< uint8_t > subject_public_key() const
Definition: pk_keys.cpp:38
Botan::Public_Key::algo_name
virtual std::string algo_name() const =0
Botan::Public_Key::create_verification_op
virtual std::unique_ptr< PK_Ops::Verification > create_verification_op(const std::string &params, const std::string &provider) const
Definition: pk_keys.cpp:107
Botan::RandomNumberGenerator
Definition: rng.h:26
Botan::OIDS::str2oid_or_empty
BOTAN_UNSTABLE_API OID str2oid_or_empty(const std::string &name)
Definition: oids.cpp:116
Botan
Definition: alg_id.cpp:13
Botan::hex_encode
void hex_encode(char output[], const uint8_t input[], size_t input_length, bool uppercase)
Definition: hex.cpp:31
Botan::create_hex_fingerprint
std::string create_hex_fingerprint(const uint8_t bits[], size_t bits_len, const std::string &hash_name)
Definition: pk_keys.cpp:17
Botan::BIT_STRING
@ BIT_STRING
Definition: asn1_obj.h:37
Botan::SEQUENCE
@ SEQUENCE
Definition: asn1_obj.h:42
Botan::OCTET_STRING
@ OCTET_STRING
Definition: asn1_obj.h:38
Botan::secure_vector
std::vector< T, secure_allocator< T > > secure_vector
Definition: secmem.h:65
hash_algo
AlgorithmIdentifier hash_algo
Definition: x509_obj.cpp:22