Coverage Report

Created: 2023-02-22 06:14

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