Botan 3.0.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#include <botan/internal/pk_ops.h>
10#include <botan/internal/fmt.h>
11#include <botan/der_enc.h>
12#include <botan/hash.h>
13#include <botan/hex.h>
14
15namespace Botan {
16
17const BigInt& Asymmetric_Key::get_int_field(std::string_view field) const
18 {
19 throw Unknown_PK_Field_Name(algo_name(), field);
20 }
21
23 {
24 try
25 {
27 }
28 catch(Lookup_Error&)
29 {
30 throw Lookup_Error(fmt("Public key algorithm {} has no defined OIDs", algo_name()));
31 }
32 }
33
34std::string create_hex_fingerprint(const uint8_t bits[],
35 size_t bits_len,
36 std::string_view hash_name)
37 {
38 auto hash_fn = HashFunction::create_or_throw(hash_name);
39 const std::string hex_hash = hex_encode(hash_fn->process(bits, bits_len));
40
41 std::string fprint;
42
43 for(size_t i = 0; i != hex_hash.size(); i += 2)
44 {
45 if(i != 0)
46 fprint.push_back(':');
47
48 fprint.push_back(hex_hash[i]);
49 fprint.push_back(hex_hash[i+1]);
50 }
51
52 return fprint;
53 }
54
55std::vector<uint8_t> Public_Key::subject_public_key() const
56 {
57 std::vector<uint8_t> output;
58
62 .end_cons();
63
64 return output;
65 }
66
68 {
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 {
82 throw Not_Implemented(algo_name() + " does not implement raw_private_key_bits");
83 }
84
85/*
86* Hash of the X.509 subjectPublicKey encoding
87*/
88std::string Public_Key::fingerprint_public(std::string_view hash_algo) const
89 {
90 return create_hex_fingerprint(subject_public_key(), hash_algo);
91 }
92
93/*
94* Hash of the PKCS #8 encoding for this key object
95*/
96std::string Private_Key::fingerprint_private(std::string_view hash_algo) const
97 {
98 return create_hex_fingerprint(private_key_bits(), hash_algo);
99 }
100
101std::unique_ptr<PK_Ops::Encryption>
103 std::string_view /*params*/,
104 std::string_view /*provider*/) const
105 {
106 throw Lookup_Error(fmt("{} does not support encryption", algo_name()));
107 }
108
109std::unique_ptr<PK_Ops::KEM_Encryption>
110Public_Key::create_kem_encryption_op(std::string_view /*params*/,
111 std::string_view /*provider*/) const
112 {
113 throw Lookup_Error(fmt("{} does not support KEM encryption", algo_name()));
114 }
115
116std::unique_ptr<PK_Ops::Verification>
117Public_Key::create_verification_op(std::string_view /*params*/,
118 std::string_view /*provider*/) const
119 {
120 throw Lookup_Error(fmt("{} does not support verification", algo_name()));
121 }
122
123std::unique_ptr<PK_Ops::Verification>
125 std::string_view /*provider*/) const
126 {
127 throw Lookup_Error(fmt("{} does not support X.509 verification", algo_name()));
128 }
129
130std::unique_ptr<PK_Ops::Decryption>
132 std::string_view /*params*/,
133 std::string_view /*provider*/) const
134 {
135 throw Lookup_Error(fmt("{} does not support decryption", algo_name()));
136 }
137
138std::unique_ptr<PK_Ops::KEM_Decryption>
140 std::string_view /*params*/,
141 std::string_view /*provider*/) const
142 {
143 throw Lookup_Error(fmt("{} does not support KEM decryption", algo_name()));
144 }
145
146std::unique_ptr<PK_Ops::Signature>
148 std::string_view /*params*/,
149 std::string_view /*provider*/) const
150 {
151 throw Lookup_Error(fmt("{} does not support signatures", algo_name()));
152 }
153
154std::unique_ptr<PK_Ops::Key_Agreement>
156 std::string_view /*params*/,
157 std::string_view /*provider*/) const
158 {
159 throw Lookup_Error(fmt("{} does not support key agreement", algo_name()));
160 }
161
162}
virtual std::string algo_name() const =0
virtual const BigInt & get_int_field(std::string_view field) const
Definition: pk_keys.cpp:17
virtual OID object_identifier() const
Definition: pk_keys.cpp:22
secure_vector< uint8_t > get_contents()
Definition: der_enc.cpp:157
DER_Encoder & start_sequence()
Definition: der_enc.h:66
DER_Encoder & end_cons()
Definition: der_enc.cpp:196
DER_Encoder & encode(bool b)
Definition: der_enc.cpp:290
static std::unique_ptr< HashFunction > create_or_throw(std::string_view algo_spec, std::string_view provider="")
Definition: hash.cpp:320
static OID from_string(std::string_view str)
Definition: asn1_oid.cpp:78
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:147
std::string fingerprint_private(std::string_view alg) const
Definition: pk_keys.cpp:96
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:131
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:155
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:80
secure_vector< uint8_t > private_key_info() const
Definition: pk_keys.cpp:67
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:139
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:88
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:102
virtual std::unique_ptr< PK_Ops::Verification > create_verification_op(std::string_view params, std::string_view provider) const
Definition: pk_keys.cpp:117
virtual std::unique_ptr< PK_Ops::Verification > create_x509_verification_op(const AlgorithmIdentifier &signature_algorithm, std::string_view provider) const
Definition: pk_keys.cpp:124
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:110
std::vector< uint8_t > subject_public_key() const
Definition: pk_keys.cpp:55
Definition: alg_id.cpp:12
std::string fmt(std::string_view format, const T &... args)
Definition: fmt.h:60
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:34
std::vector< T, secure_allocator< T > > secure_vector
Definition: secmem.h:64