Botan 2.1.0 Crypto and TLS for C++11

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

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()
20 .start_cons(SEQUENCE)
21 .encode(algorithm_identifier())
22 .encode(public_key_bits(), BIT_STRING)
23 .end_cons()
24 .get_contents_unlocked();
25 }
26
27 /*
28 * Default OID access
29 */
30 OID Public_Key::get_oid() const
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
41 secure_vector<uint8_t> Private_Key::private_key_info() const
42 {
43 const size_t PKCS8_VERSION = 0;
44
45 return DER_Encoder()
46 .start_cons(SEQUENCE)
47 .encode(PKCS8_VERSION)
48 .encode(pkcs8_algorithm_identifier())
49 .encode(private_key_bits(), OCTET_STRING)
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 {
59 secure_vector<uint8_t> buf = private_key_bits();
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>
79 Public_Key::create_encryption_op(RandomNumberGenerator& /*rng*/,
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>
87 Public_Key::create_kem_encryption_op(RandomNumberGenerator& /*rng*/,
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>
102 Private_Key::create_decryption_op(RandomNumberGenerator& /*rng*/,
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>
110 Private_Key::create_kem_decryption_op(RandomNumberGenerator& /*rng*/,
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>
118 Private_Key::create_signature_op(RandomNumberGenerator& /*rng*/,
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>
126 Private_Key::create_key_agreement_op(RandomNumberGenerator& /*rng*/,
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 }
Botan::DER_Encoder::get_contents_unlocked
std::vector< uint8_t > get_contents_unlocked()
Definition: der_enc.h:27
Botan::Public_Key::public_key_bits
virtual std::vector< uint8_t > public_key_bits() const =0
Botan::RandomNumberGenerator
Definition: rng.h:25
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:118
Botan::Private_Key::fingerprint
std::string fingerprint(const std::string &alg="SHA") const
Definition: pk_keys.cpp:57
Botan::Public_Key::algorithm_identifier
virtual AlgorithmIdentifier algorithm_identifier() const =0
Botan::Private_Key::private_key_bits
virtual secure_vector< uint8_t > private_key_bits() const =0
Botan::DER_Encoder::get_contents
secure_vector< uint8_t > get_contents()
Definition: der_enc.cpp:124
Botan::Public_Key::algo_name
virtual std::string algo_name() const =0
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:79
Botan::DER_Encoder::end_cons
DER_Encoder & end_cons()
Definition: der_enc.cpp:147
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:102
Botan::Public_Key::get_oid
virtual OID get_oid() const
Definition: pk_keys.cpp:30
Botan::DER_Encoder::encode
DER_Encoder & encode(bool b)
Definition: der_enc.cpp:216
Botan::OCTET_STRING
Definition: asn1_obj.h:35
Botan::DER_Encoder
Definition: der_enc.h:22
Botan::secure_vector
std::vector< T, secure_allocator< T >> secure_vector
Definition: secmem.h:121
Botan::OIDS::lookup
std::string lookup(const OID &oid)
Definition: oids.cpp:18
Botan::HashFunction::create
static std::unique_ptr< HashFunction > create(const std::string &algo_spec, const std::string &provider="")
Definition: hash.cpp:93
Botan::Private_Key::pkcs8_algorithm_identifier
virtual AlgorithmIdentifier pkcs8_algorithm_identifier() const
Definition: pk_keys.h:188
Botan::Lookup_Error
Definition: exceptn.h:71
Botan::SEQUENCE
Definition: asn1_obj.h:39
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:110
Botan
Definition: alg_id.cpp:13
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:126
Botan::Private_Key::private_key_info
secure_vector< uint8_t > private_key_info() const
Definition: pk_keys.cpp:41
Botan::BIT_STRING
Definition: asn1_obj.h:34
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:87
Botan::DER_Encoder::start_cons
DER_Encoder & start_cons(ASN1_Tag type_tag, ASN1_Tag class_tag=UNIVERSAL)
Definition: der_enc.cpp:137
Botan::hex_encode
void hex_encode(char output[], const uint8_t input[], size_t input_length, bool uppercase)
Definition: hex.cpp:14
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:95
Botan::OID
Definition: asn1_oid.h:20
Botan::Public_Key::subject_public_key
std::vector< uint8_t > subject_public_key() const
Definition: pk_keys.cpp:17
hash
MechanismType hash
Definition: p11_mechanism.cpp:63