Botan 3.7.1
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(const uint8_t bits[], size_t bits_len, std::string_view hash_name) {
39 auto hash_fn = HashFunction::create_or_throw(hash_name);
40 const std::string hex_hash = hex_encode(hash_fn->process(bits, bits_len));
41
42 std::string fprint;
43
44 for(size_t i = 0; i != hex_hash.size(); i += 2) {
45 if(i != 0) {
46 fprint.push_back(':');
47 }
48
49 fprint.push_back(hex_hash[i]);
50 fprint.push_back(hex_hash[i + 1]);
51 }
52
53 return fprint;
54}
55
56std::vector<uint8_t> Public_Key::subject_public_key() const {
57 std::vector<uint8_t> output;
58
59 DER_Encoder(output)
63 .end_cons();
64
65 return output;
66}
67
69 const size_t PKCS8_VERSION = 0;
70
71 return DER_Encoder()
73 .encode(PKCS8_VERSION)
76 .end_cons()
77 .get_contents();
78}
79
81 throw Not_Implemented(algo_name() + " does not implement raw_private_key_bits");
82}
83
84/*
85* Hash of the X.509 subjectPublicKey encoding
86*/
87std::string Public_Key::fingerprint_public(std::string_view hash_algo) const {
88 return create_hex_fingerprint(subject_public_key(), hash_algo);
89}
90
91/*
92* Hash of the PKCS #8 encoding for this key object
93*/
94std::string Private_Key::fingerprint_private(std::string_view hash_algo) const {
95 return create_hex_fingerprint(private_key_bits(), hash_algo);
96}
97
98std::unique_ptr<PK_Ops::Encryption> Public_Key::create_encryption_op(RandomNumberGenerator& /*rng*/,
99 std::string_view /*params*/,
100 std::string_view /*provider*/) const {
101 throw Lookup_Error(fmt("{} does not support encryption", algo_name()));
102}
103
104std::unique_ptr<PK_Ops::KEM_Encryption> Public_Key::create_kem_encryption_op(std::string_view /*params*/,
105 std::string_view /*provider*/) const {
106 throw Lookup_Error(fmt("{} does not support KEM encryption", algo_name()));
107}
108
109std::unique_ptr<PK_Ops::Verification> Public_Key::create_verification_op(std::string_view /*params*/,
110 std::string_view /*provider*/) const {
111 throw Lookup_Error(fmt("{} does not support verification", algo_name()));
112}
113
114std::unique_ptr<PK_Ops::Verification> Public_Key::create_x509_verification_op(const AlgorithmIdentifier& /*params*/,
115 std::string_view /*provider*/) const {
116 throw Lookup_Error(fmt("{} does not support X.509 verification", algo_name()));
117}
118
119std::unique_ptr<PK_Ops::Decryption> Private_Key::create_decryption_op(RandomNumberGenerator& /*rng*/,
120 std::string_view /*params*/,
121 std::string_view /*provider*/) const {
122 throw Lookup_Error(fmt("{} does not support decryption", algo_name()));
123}
124
125std::unique_ptr<PK_Ops::KEM_Decryption> Private_Key::create_kem_decryption_op(RandomNumberGenerator& /*rng*/,
126 std::string_view /*params*/,
127 std::string_view /*provider*/) const {
128 throw Lookup_Error(fmt("{} does not support KEM decryption", algo_name()));
129}
130
131std::unique_ptr<PK_Ops::Signature> Private_Key::create_signature_op(RandomNumberGenerator& /*rng*/,
132 std::string_view /*params*/,
133 std::string_view /*provider*/) const {
134 throw Lookup_Error(fmt("{} does not support signatures", algo_name()));
135}
136
137std::unique_ptr<PK_Ops::Key_Agreement> Private_Key::create_key_agreement_op(RandomNumberGenerator& /*rng*/,
138 std::string_view /*params*/,
139 std::string_view /*provider*/) const {
140 throw Lookup_Error(fmt("{} does not support key agreement", algo_name()));
141}
142
143} // 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:136
virtual Signature_Format _default_x509_signature_format() const
Definition pk_keys.cpp:30
secure_vector< uint8_t > get_contents()
Definition der_enc.cpp:132
DER_Encoder & start_sequence()
Definition der_enc.h:64
DER_Encoder & end_cons()
Definition der_enc.cpp:171
DER_Encoder & encode(bool b)
Definition der_enc.cpp:250
static std::unique_ptr< HashFunction > create_or_throw(std::string_view algo_spec, std::string_view provider="")
Definition hash.cpp:298
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:131
std::string fingerprint_private(std::string_view alg) const
Definition pk_keys.cpp:94
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:119
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:137
virtual AlgorithmIdentifier pkcs8_algorithm_identifier() const
Definition pk_keys.h:322
virtual secure_vector< uint8_t > raw_private_key_bits() const
Definition pk_keys.cpp:80
secure_vector< uint8_t > private_key_info() const
Definition pk_keys.cpp:68
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:125
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:87
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:98
virtual std::unique_ptr< PK_Ops::Verification > create_verification_op(std::string_view params, std::string_view provider) const
Definition pk_keys.cpp:109
virtual std::unique_ptr< PK_Ops::Verification > create_x509_verification_op(const AlgorithmIdentifier &signature_algorithm, std::string_view provider) const
Definition pk_keys.cpp:114
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:104
std::vector< uint8_t > subject_public_key() const
Definition pk_keys.cpp:56
std::string fmt(std::string_view format, const T &... args)
Definition fmt.h:53
void hex_encode(char output[], const uint8_t input[], size_t input_length, bool uppercase)
Definition hex.cpp:35
std::string create_hex_fingerprint(const uint8_t bits[], size_t bits_len, std::string_view hash_name)
Definition pk_keys.cpp:38
std::vector< T, secure_allocator< T > > secure_vector
Definition secmem.h:61
Signature_Format
Definition pk_keys.h:31