Coverage Report

Created: 2026-02-09 06:47

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