Botan 3.12.0
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
10#include <botan/der_enc.h>
11#include <botan/hash.h>
12#include <botan/hex.h>
13#include <botan/pk_ops.h>
14#include <botan/internal/fmt.h>
15
16namespace Botan {
17
18const BigInt& Asymmetric_Key::get_int_field(std::string_view field) const {
19 throw Unknown_PK_Field_Name(algo_name(), field);
20}
21
23 try {
25 } catch(Lookup_Error&) {
26 throw Lookup_Error(fmt("Public key algorithm {} has no defined OIDs", algo_name()));
27 }
28}
29
37
38std::string create_hex_fingerprint(std::span<const uint8_t> bits, std::string_view hash_name) {
39 auto hash_fn = HashFunction::create_or_throw(hash_name);
40 hash_fn->update(bits);
41 auto digest = hash_fn->final_stdvec();
42 return format_hex_fingerprint(digest);
43}
44
45std::string format_hex_fingerprint(std::span<const uint8_t> bits) {
46 const std::string hex = hex_encode(bits);
47
48 std::string fprint;
49 fprint.reserve(3 * bits.size());
50
51 for(size_t i = 0; i != hex.size(); i += 2) {
52 if(i != 0) {
53 fprint.push_back(':');
54 }
55
56 fprint.push_back(hex[i]);
57 fprint.push_back(hex[i + 1]);
58 }
59
60 return fprint;
61}
62
63std::vector<uint8_t> Public_Key::subject_public_key() const {
64 std::vector<uint8_t> output;
65
66 DER_Encoder(output)
70 .end_cons();
71
72 return output;
73}
74
76 const size_t PKCS8_VERSION = 0;
77
78 return DER_Encoder()
80 .encode(PKCS8_VERSION)
83 .end_cons()
84 .get_contents();
85}
86
88 throw Not_Implemented(algo_name() + " does not implement raw_private_key_bits");
89}
90
91/*
92* Hash of the X.509 subjectPublicKey encoding
93*/
94std::string Public_Key::fingerprint_public(std::string_view hash_algo) const {
95 return create_hex_fingerprint(subject_public_key(), hash_algo);
96}
97
98/*
99* Hash of the PKCS #8 encoding for this key object
100*/
101std::string Private_Key::fingerprint_private(std::string_view hash_algo) const {
102 return create_hex_fingerprint(private_key_bits(), hash_algo);
103}
104
105std::unique_ptr<PK_Ops::Encryption> Public_Key::create_encryption_op(RandomNumberGenerator& /*rng*/,
106 std::string_view /*params*/,
107 std::string_view /*provider*/) const {
108 throw Lookup_Error(fmt("{} does not support encryption", algo_name()));
109}
110
111std::unique_ptr<PK_Ops::KEM_Encryption> Public_Key::create_kem_encryption_op(std::string_view /*params*/,
112 std::string_view /*provider*/) const {
113 throw Lookup_Error(fmt("{} does not support KEM encryption", algo_name()));
114}
115
116std::unique_ptr<PK_Ops::Verification> Public_Key::create_verification_op(std::string_view /*params*/,
117 std::string_view /*provider*/) const {
118 throw Lookup_Error(fmt("{} does not support verification", algo_name()));
119}
120
121std::unique_ptr<PK_Ops::Verification> Public_Key::create_x509_verification_op(const AlgorithmIdentifier& /*params*/,
122 std::string_view /*provider*/) const {
123 throw Lookup_Error(fmt("{} does not support X.509 verification", algo_name()));
124}
125
126std::unique_ptr<PK_Ops::Decryption> Private_Key::create_decryption_op(RandomNumberGenerator& /*rng*/,
127 std::string_view /*params*/,
128 std::string_view /*provider*/) const {
129 throw Lookup_Error(fmt("{} does not support decryption", algo_name()));
130}
131
132std::unique_ptr<PK_Ops::KEM_Decryption> Private_Key::create_kem_decryption_op(RandomNumberGenerator& /*rng*/,
133 std::string_view /*params*/,
134 std::string_view /*provider*/) const {
135 throw Lookup_Error(fmt("{} does not support KEM decryption", algo_name()));
136}
137
138std::unique_ptr<PK_Ops::Signature> Private_Key::create_signature_op(RandomNumberGenerator& /*rng*/,
139 std::string_view /*params*/,
140 std::string_view /*provider*/) const {
141 throw Lookup_Error(fmt("{} does not support signatures", algo_name()));
142}
143
144std::unique_ptr<PK_Ops::Key_Agreement> Private_Key::create_key_agreement_op(RandomNumberGenerator& /*rng*/,
145 std::string_view /*params*/,
146 std::string_view /*provider*/) const {
147 throw Lookup_Error(fmt("{} does not support key agreement", algo_name()));
148}
149
150} // namespace Botan
virtual std::string algo_name() const =0
virtual const BigInt & get_int_field(std::string_view field) const
Definition pk_keys.cpp:18
virtual OID object_identifier() const
Definition pk_keys.cpp:22
virtual std::optional< size_t > _signature_element_size_for_DER_encoding() const
Definition pk_keys.h:144
virtual Signature_Format _default_x509_signature_format() const
Definition pk_keys.cpp:30
secure_vector< uint8_t > get_contents()
Definition der_enc.cpp:134
DER_Encoder & start_sequence()
Definition der_enc.h:67
DER_Encoder & end_cons()
Definition der_enc.cpp:173
DER_Encoder & encode(bool b)
Definition der_enc.cpp:245
static std::unique_ptr< HashFunction > create_or_throw(std::string_view algo_spec, std::string_view provider="")
Definition hash.cpp:308
static OID from_string(std::string_view str)
Definition asn1_oid.cpp:86
virtual std::unique_ptr< PK_Ops::Signature > create_signature_op(RandomNumberGenerator &rng, std::string_view params, std::string_view provider) const
Definition pk_keys.cpp:138
std::string fingerprint_private(std::string_view alg) const
Definition pk_keys.cpp:101
virtual std::unique_ptr< PK_Ops::Decryption > create_decryption_op(RandomNumberGenerator &rng, std::string_view params, std::string_view provider) const
Definition pk_keys.cpp:126
virtual std::unique_ptr< PK_Ops::Key_Agreement > create_key_agreement_op(RandomNumberGenerator &rng, std::string_view params, std::string_view provider) const
Definition pk_keys.cpp:144
virtual AlgorithmIdentifier pkcs8_algorithm_identifier() const
Definition pk_keys.h:334
virtual secure_vector< uint8_t > raw_private_key_bits() const
Definition pk_keys.cpp:87
secure_vector< uint8_t > private_key_info() const
Definition pk_keys.cpp:75
virtual std::unique_ptr< PK_Ops::KEM_Decryption > create_kem_decryption_op(RandomNumberGenerator &rng, std::string_view params, std::string_view provider) const
Definition pk_keys.cpp:132
virtual secure_vector< uint8_t > private_key_bits() const =0
std::string fingerprint_public(std::string_view alg="SHA-256") const
Definition pk_keys.cpp:94
virtual AlgorithmIdentifier algorithm_identifier() const =0
virtual std::vector< uint8_t > public_key_bits() const =0
virtual std::unique_ptr< PK_Ops::Encryption > create_encryption_op(RandomNumberGenerator &rng, std::string_view params, std::string_view provider) const
Definition pk_keys.cpp:105
virtual std::unique_ptr< PK_Ops::Verification > create_verification_op(std::string_view params, std::string_view provider) const
Definition pk_keys.cpp:116
virtual std::unique_ptr< PK_Ops::Verification > create_x509_verification_op(const AlgorithmIdentifier &signature_algorithm, std::string_view provider) const
Definition pk_keys.cpp:121
virtual std::unique_ptr< PK_Ops::KEM_Encryption > create_kem_encryption_op(std::string_view params, std::string_view provider) const
Definition pk_keys.cpp:111
std::vector< uint8_t > subject_public_key() const
Definition pk_keys.cpp:63
std::string fmt(std::string_view format, const T &... args)
Definition fmt.h:53
std::string create_hex_fingerprint(std::span< const uint8_t > bits, std::string_view hash_name)
Definition pk_keys.cpp:38
void hex_encode(char output[], const uint8_t input[], size_t input_length, bool uppercase)
Definition hex.cpp:34
Signature_Format
Definition pk_keys.h:32
std::vector< T, secure_allocator< T > > secure_vector
Definition secmem.h:68
std::string format_hex_fingerprint(std::span< const uint8_t > bits)
Definition pk_keys.cpp:45