/src/botan/src/lib/pubkey/pk_keys.cpp
Line | Count | Source (jump to first uncovered line) |
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 | | |
16 | | namespace Botan { |
17 | | |
18 | 0 | const BigInt& Asymmetric_Key::get_int_field(std::string_view field) const { |
19 | 0 | throw Unknown_PK_Field_Name(algo_name(), field); |
20 | 0 | } |
21 | | |
22 | 0 | OID Asymmetric_Key::object_identifier() const { |
23 | 0 | try { |
24 | 0 | return OID::from_string(algo_name()); |
25 | 0 | } catch(Lookup_Error&) { |
26 | 0 | throw Lookup_Error(fmt("Public key algorithm {} has no defined OIDs", algo_name())); |
27 | 0 | } |
28 | 0 | } |
29 | | |
30 | 0 | std::string create_hex_fingerprint(const uint8_t bits[], size_t bits_len, std::string_view hash_name) { |
31 | 0 | auto hash_fn = HashFunction::create_or_throw(hash_name); |
32 | 0 | const std::string hex_hash = hex_encode(hash_fn->process(bits, bits_len)); |
33 | |
|
34 | 0 | std::string fprint; |
35 | |
|
36 | 0 | for(size_t i = 0; i != hex_hash.size(); i += 2) { |
37 | 0 | if(i != 0) { |
38 | 0 | fprint.push_back(':'); |
39 | 0 | } |
40 | |
|
41 | 0 | fprint.push_back(hex_hash[i]); |
42 | 0 | fprint.push_back(hex_hash[i + 1]); |
43 | 0 | } |
44 | |
|
45 | 0 | return fprint; |
46 | 0 | } |
47 | | |
48 | 0 | std::vector<uint8_t> Public_Key::subject_public_key() const { |
49 | 0 | std::vector<uint8_t> output; |
50 | |
|
51 | 0 | DER_Encoder(output) |
52 | 0 | .start_sequence() |
53 | 0 | .encode(algorithm_identifier()) |
54 | 0 | .encode(public_key_bits(), ASN1_Type::BitString) |
55 | 0 | .end_cons(); |
56 | |
|
57 | 0 | return output; |
58 | 0 | } |
59 | | |
60 | 0 | secure_vector<uint8_t> Private_Key::private_key_info() const { |
61 | 0 | const size_t PKCS8_VERSION = 0; |
62 | |
|
63 | 0 | return DER_Encoder() |
64 | 0 | .start_sequence() |
65 | 0 | .encode(PKCS8_VERSION) |
66 | 0 | .encode(pkcs8_algorithm_identifier()) |
67 | 0 | .encode(private_key_bits(), ASN1_Type::OctetString) |
68 | 0 | .end_cons() |
69 | 0 | .get_contents(); |
70 | 0 | } |
71 | | |
72 | 0 | secure_vector<uint8_t> Private_Key::raw_private_key_bits() const { |
73 | 0 | throw Not_Implemented(algo_name() + " does not implement raw_private_key_bits"); |
74 | 0 | } |
75 | | |
76 | | /* |
77 | | * Hash of the X.509 subjectPublicKey encoding |
78 | | */ |
79 | 0 | std::string Public_Key::fingerprint_public(std::string_view hash_algo) const { |
80 | 0 | return create_hex_fingerprint(subject_public_key(), hash_algo); |
81 | 0 | } |
82 | | |
83 | | /* |
84 | | * Hash of the PKCS #8 encoding for this key object |
85 | | */ |
86 | 0 | std::string Private_Key::fingerprint_private(std::string_view hash_algo) const { |
87 | 0 | return create_hex_fingerprint(private_key_bits(), hash_algo); |
88 | 0 | } |
89 | | |
90 | | std::unique_ptr<PK_Ops::Encryption> Public_Key::create_encryption_op(RandomNumberGenerator& /*rng*/, |
91 | | std::string_view /*params*/, |
92 | 0 | std::string_view /*provider*/) const { |
93 | 0 | throw Lookup_Error(fmt("{} does not support encryption", algo_name())); |
94 | 0 | } |
95 | | |
96 | | std::unique_ptr<PK_Ops::KEM_Encryption> Public_Key::create_kem_encryption_op(std::string_view /*params*/, |
97 | 0 | std::string_view /*provider*/) const { |
98 | 0 | throw Lookup_Error(fmt("{} does not support KEM encryption", algo_name())); |
99 | 0 | } |
100 | | |
101 | | std::unique_ptr<PK_Ops::Verification> Public_Key::create_verification_op(std::string_view /*params*/, |
102 | 0 | std::string_view /*provider*/) const { |
103 | 0 | throw Lookup_Error(fmt("{} does not support verification", algo_name())); |
104 | 0 | } |
105 | | |
106 | | std::unique_ptr<PK_Ops::Verification> Public_Key::create_x509_verification_op(const AlgorithmIdentifier& /*params*/, |
107 | 0 | std::string_view /*provider*/) const { |
108 | 0 | throw Lookup_Error(fmt("{} does not support X.509 verification", algo_name())); |
109 | 0 | } |
110 | | |
111 | | std::unique_ptr<PK_Ops::Decryption> Private_Key::create_decryption_op(RandomNumberGenerator& /*rng*/, |
112 | | std::string_view /*params*/, |
113 | 0 | std::string_view /*provider*/) const { |
114 | 0 | throw Lookup_Error(fmt("{} does not support decryption", algo_name())); |
115 | 0 | } |
116 | | |
117 | | std::unique_ptr<PK_Ops::KEM_Decryption> Private_Key::create_kem_decryption_op(RandomNumberGenerator& /*rng*/, |
118 | | std::string_view /*params*/, |
119 | 0 | std::string_view /*provider*/) const { |
120 | 0 | throw Lookup_Error(fmt("{} does not support KEM decryption", algo_name())); |
121 | 0 | } |
122 | | |
123 | | std::unique_ptr<PK_Ops::Signature> Private_Key::create_signature_op(RandomNumberGenerator& /*rng*/, |
124 | | std::string_view /*params*/, |
125 | 0 | std::string_view /*provider*/) const { |
126 | 0 | throw Lookup_Error(fmt("{} does not support signatures", algo_name())); |
127 | 0 | } |
128 | | |
129 | | std::unique_ptr<PK_Ops::Key_Agreement> Private_Key::create_key_agreement_op(RandomNumberGenerator& /*rng*/, |
130 | | std::string_view /*params*/, |
131 | 0 | std::string_view /*provider*/) const { |
132 | 0 | throw Lookup_Error(fmt("{} does not support key agreement", algo_name())); |
133 | 0 | } |
134 | | |
135 | | } // namespace Botan |