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