Coverage Report

Created: 2021-11-25 09:31

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