Coverage Report

Created: 2026-12-01 06:40

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/Botan-3.13.0/build/include/public/botan/pk_keys.h
Line
Count
Source
1
/*
2
* PK Key Types
3
* (C) 1999-2007,2018 Jack Lloyd
4
*
5
* Botan is released under the Simplified BSD License (see license.txt)
6
*/
7
8
#ifndef BOTAN_PK_KEYS_H_
9
#define BOTAN_PK_KEYS_H_
10
11
#include <botan/asn1_obj.h>
12
#include <botan/pk_ops_fwd.h>
13
#include <botan/secmem.h>
14
15
#include <memory>
16
#include <optional>
17
#include <span>
18
#include <string>
19
#include <string_view>
20
21
namespace Botan {
22
23
class BigInt;
24
class RandomNumberGenerator;
25
26
/**
27
* Enumeration specifying the signature format.
28
*
29
* This is mostly used for requesting DER encoding of ECDSA signatures;
30
* most other algorithms only support "standard".
31
*/
32
enum class Signature_Format : uint8_t {
33
   Standard = 0,
34
   DerSequence = 1,
35
36
   IEEE_1363 BOTAN_DEPRECATED("Use Standard") = Standard,
37
   DER_SEQUENCE BOTAN_DEPRECATED("Use DerSequence") = DerSequence,
38
};
39
40
/**
41
* Enumeration of possible operations a public key could be used for.
42
*
43
* It is possible to query if a key supports a particular operation
44
* type using Asymmetric_Key::supports_operation()
45
*/
46
enum class PublicKeyOperation : uint8_t {
47
   Encryption,
48
   Signature,
49
   KeyEncapsulation,
50
   KeyAgreement,
51
};
52
53
class Private_Key;
54
55
/**
56
* An interface for objects that are keys in public key algorithms
57
*
58
* This is derived for both public and private keys
59
*/
60
class BOTAN_PUBLIC_API(3, 0) Asymmetric_Key /* NOLINT(*special-member-functions) */ {
61
   public:
62
323k
      virtual ~Asymmetric_Key() = default;
63
64
      /**
65
      * Get the name of the underlying public key scheme.
66
      * @return name of the public key scheme
67
      */
68
      virtual std::string algo_name() const = 0;
69
70
      /**
71
      * Return the estimated strength of the underlying key against
72
      * the best currently known attack. Note that this ignores anything
73
      * but pure attacks against the key itself and do not take into
74
      * account padding schemes, usage mistakes, etc which might reduce
75
      * the strength. However it does suffice to provide an upper bound.
76
      *
77
      * @return estimated strength in bits
78
      */
79
      virtual size_t estimated_strength() const = 0;
80
81
      /**
82
      * Get the OID of the underlying public key scheme.
83
      * @return OID of the public key scheme
84
      */
85
      virtual OID object_identifier() const;
86
87
      /**
88
      * Access an algorithm specific field
89
      *
90
      * If the field is not known for this algorithm, an Invalid_Argument is
91
      * thrown. The interpretation of the result requires knowledge of which
92
      * algorithm is involved. For instance for RSA "p" represents one of the
93
      * secret primes, while for DSA "p" is the public prime.
94
      *
95
      * Some algorithms may not implement this method at all.
96
      *
97
      * This is primarily used to implement the FFI botan_pubkey_get_field
98
      * and botan_privkey_get_field functions.
99
      *
100
      * TODO(Botan4) Change this to return by value
101
      */
102
      virtual const BigInt& get_int_field(std::string_view field) const;
103
104
      /**
105
      * Return true if this key could be used for the specified type
106
      * of operation.
107
      */
108
      virtual bool supports_operation(PublicKeyOperation op) const = 0;
109
110
      /**
111
       * Generate another (cryptographically independent) key pair using the
112
       * same algorithm parameters as this key. This is most useful for algorithms
113
       * that support PublicKeyOperation::KeyAgreement to generate a fitting ephemeral
114
       * key pair. For other key types it might throw `Not_Implemented`.
115
       */
116
      virtual std::unique_ptr<Private_Key> generate_another(RandomNumberGenerator& rng) const = 0;
117
118
      /**
119
      * Test the key values for consistency.
120
      *
121
      * Note this function is always "best effort"; for many algorithms it is
122
      * not computationally possible to ensure the key is correctly formed in
123
      * all respects. There is always the possibility a malformed key will be
124
      * accepted; this is especially the case for public keys.
125
      *
126
      * @param rng rng to use for randomized testing (may be ignored)
127
      * @param strong whether to perform strong and lengthy version of the test,
128
      *        however for many algorithms this has no effect
129
      * @return true if the tests passed
130
      */
131
      virtual bool check_key(RandomNumberGenerator& rng, bool strong) const = 0;
132
133
      // Declarations for internal library functions not covered by SemVer follow
134
135
      /**
136
      * Certain signatures schemes such as ECDSA have more than
137
      * one element, and certain unfortunate protocols decided the
138
      * thing to do was not concatenate them as normally done, but
139
      * instead DER encode each of the elements as independent values.
140
      *
141
      * If this returns a value x then the signature is checked to
142
      * be exactly 2*x bytes and split in half for DER encoding.
143
      */
144
131k
      virtual std::optional<size_t> _signature_element_size_for_DER_encoding() const { return {}; }
145
146
      /*
147
      * Return the format normally used by this algorithm for X.509 signatures
148
      */
149
      virtual Signature_Format _default_x509_signature_format() const;
150
};
151
152
/*
153
* Public Key Base Class.
154
*/
155
class BOTAN_PUBLIC_API(2, 0) Public_Key : public virtual Asymmetric_Key {
156
   public:
157
      /**
158
      * Return an integer value best approximating the length of the
159
      * primary security parameter. For example for RSA this will be
160
      * the size of the modulus, for ECDSA the size of the ECC group,
161
      * and for McEliece the size of the code will be returned.
162
      */
163
      virtual size_t key_length() const = 0;
164
165
      /**
166
      * Deprecated version of object_identifier
167
      */
168
0
      BOTAN_DEPRECATED("Use object_identifier") OID get_oid() const { return this->object_identifier(); }
169
170
      /**
171
      * Return the X.509 AlgorithmIdentifier for this key
172
      */
173
      virtual AlgorithmIdentifier algorithm_identifier() const = 0;
174
175
      /**
176
      * Return the raw public key bits (algorithm specific) with no extra encoding
177
      *
178
      * For key agreements this is an alias for PK_Key_Agreement_Key::public_value.
179
      *
180
      * @note some algorithms (for example RSA) do not have an obvious encoding
181
      * for this value due to having many different values, and thus throw
182
      * `Not_Implemented` when invoking this method.
183
      */
184
      virtual std::vector<uint8_t> raw_public_key_bits() const = 0;
185
186
      /**
187
      * Return the subject public key encoding of this public key
188
      *
189
      * @note this excludes the parameters field and may not be reliably decodable
190
      */
191
      virtual std::vector<uint8_t> public_key_bits() const = 0;
192
193
      /**
194
      * Return the SubjectPublicKeyInfo encoding of this public key
195
      *
196
      * This is the subjectPublicKey field plus the algorithm-specific parameters
197
      */
198
      std::vector<uint8_t> subject_public_key() const;
199
200
      /**
201
       * @return Hash of the subject public key
202
       */
203
      std::string fingerprint_public(std::string_view alg = "SHA-256") const;
204
205
      // Declarations for internal library functions not covered by SemVer follow
206
207
      /**
208
      * Returns more than 1 if the output of this algorithm
209
      * (ciphertext, signature) should be treated as more than one
210
      * value. This is used for algorithms like DSA and ECDSA, where
211
      * the (r,s) output pair can be encoded as either a plain binary
212
      * list or a TLV tagged DER encoding depending on the protocol.
213
      *
214
      * This function is public but applications should have few
215
      * reasons to ever call this.
216
      *
217
      * @return number of message parts
218
      */
219
0
      BOTAN_DEPRECATED("Deprecated no replacement") size_t message_parts() const {
220
0
         return _signature_element_size_for_DER_encoding() ? 2 : 1;
221
0
      }
222
223
      /**
224
      * Returns how large each of the message parts referred to
225
      * by message_parts() is
226
      *
227
      * This function is public but applications should have few
228
      * reasons to ever call this.
229
      *
230
      * @return size of the message parts in bits
231
      */
232
0
      BOTAN_DEPRECATED("Deprecated no replacement") size_t message_part_size() const {
233
0
         return _signature_element_size_for_DER_encoding().value_or(0);
234
0
      }
235
236
      // NOLINTBEGIN(bugprone-virtual-near-miss)
237
238
      /*
239
      * Return the format normally used by this algorithm for X.509 signatures
240
      */
241
0
      BOTAN_DEPRECATED("Deprecated no replacement") Signature_Format default_x509_signature_format() const {
242
0
         return _default_x509_signature_format();
243
0
      }
244
245
      // NOLINTEND(bugprone-virtual-near-miss)
246
247
      /**
248
      * This is an internal library function exposed on key types.
249
      * In almost all cases applications should use wrappers in pubkey.h
250
      *
251
      * Return an encryption operation for this key/params or throw
252
      *
253
      * @param rng a random number generator. The PK_Op may maintain a
254
      * reference to the RNG and use it many times. The rng must outlive
255
      * any operations which reference it.
256
      * @param params additional parameters
257
      * @param provider the provider to use
258
      */
259
      virtual std::unique_ptr<PK_Ops::Encryption> create_encryption_op(RandomNumberGenerator& rng,
260
                                                                       std::string_view params,
261
                                                                       std::string_view provider) const;
262
263
      /**
264
      * This is an internal library function exposed on key types.
265
      * In almost all cases applications should use wrappers in pubkey.h
266
      *
267
      * Return a KEM encryption operation for this key/params or throw
268
      *
269
      * @param params additional parameters
270
      * @param provider the provider to use
271
      */
272
      virtual std::unique_ptr<PK_Ops::KEM_Encryption> create_kem_encryption_op(std::string_view params,
273
                                                                               std::string_view provider) const;
274
275
      /**
276
      * This is an internal library function exposed on key types.
277
      * In all cases applications should use wrappers in pubkey.h
278
      *
279
      * Return a verification operation for this key/params or throw
280
      * @param params additional parameters
281
      * @param provider the provider to use
282
      */
283
      virtual std::unique_ptr<PK_Ops::Verification> create_verification_op(std::string_view params,
284
                                                                           std::string_view provider) const;
285
286
      /**
287
      * This is an internal library function exposed on key types.
288
      * In all cases applications should use wrappers in pubkey.h
289
      *
290
      * Return a verification operation for this combination of key and
291
      * signature algorithm or throw.
292
      *
293
      * @param signature_algorithm is the X.509 algorithm identifier encoding the padding
294
      * scheme and hash hash function used in the signature if applicable.
295
      *
296
      * @param provider the provider to use
297
      */
298
      virtual std::unique_ptr<PK_Ops::Verification> create_x509_verification_op(
299
         const AlgorithmIdentifier& signature_algorithm, std::string_view provider) const;
300
};
301
302
/**
303
* Private Key Base Class
304
*/
305
class BOTAN_PUBLIC_API(2, 0) Private_Key : public virtual Public_Key {
306
   public:
307
      /**
308
      * Return the PKCS8 private key encoding
309
      *
310
      * @note this encoding omits the outer PKCS8 algorithm identifiers and will
311
      * not be portably decodable on its own. Prefer `private_key_info`.
312
      */
313
      virtual secure_vector<uint8_t> private_key_bits() const = 0;
314
315
      /**
316
      * Return the binary private key bits, with no additional encoding
317
      *
318
      * @note some algorithms (for example RSA) do not have an obvious encoding
319
      * for this value due to having many different values, and thus not implement
320
      * this function. The default implementation throws `Not_Implemented`
321
      */
322
      virtual secure_vector<uint8_t> raw_private_key_bits() const;
323
324
      /**
325
      * Allocate a new object for the public key associated with this
326
      * private key.
327
      *
328
      * @return public key
329
      */
330
      virtual std::unique_ptr<Public_Key> public_key() const = 0;
331
332
      /**
333
      * Return PKCS #8 private key encoding for this key object
334
      */
335
      secure_vector<uint8_t> private_key_info() const;
336
337
      /**
338
      * Return the PKCS #8 AlgorithmIdentifier for this key
339
      *
340
      * @note normally this is the same as the public key identifier, but a few
341
      * oddball algorithms use a different value
342
      */
343
0
      virtual AlgorithmIdentifier pkcs8_algorithm_identifier() const { return algorithm_identifier(); }
344
345
      /**
346
      * Indicates if this key is stateful, ie that performing a private
347
      * key operation requires updating the key storage.
348
      */
349
0
      virtual bool stateful_operation() const { return false; }
350
351
      /**
352
       * @brief Retrieves the number of remaining operations if this is a stateful private key.
353
       *
354
       * @returns the number of remaining operations or std::nullopt if not applicable.
355
       */
356
0
      virtual std::optional<uint64_t> remaining_operations() const { return std::nullopt; }
357
358
      // Declarations for internal library functions not covered by SemVer follow
359
360
      /**
361
       * @return Hash of the PKCS #8 encoding for this key object
362
       */
363
      std::string fingerprint_private(std::string_view alg) const;
364
365
      /**
366
      * This is an internal library function exposed on key types.
367
      * In all cases applications should use wrappers in pubkey.h
368
      *
369
      * Return an decryption operation for this key/params or throw
370
      *
371
      * @param rng a random number generator. The PK_Op may maintain a
372
      * reference to the RNG and use it many times. The rng must outlive
373
      * any operations which reference it.
374
      * @param params additional parameters
375
      * @param provider the provider to use
376
      *
377
      */
378
      virtual std::unique_ptr<PK_Ops::Decryption> create_decryption_op(RandomNumberGenerator& rng,
379
                                                                       std::string_view params,
380
                                                                       std::string_view provider) const;
381
382
      /**
383
      * This is an internal library function exposed on key types.
384
      * In all cases applications should use wrappers in pubkey.h
385
      *
386
      * Return a KEM decryption operation for this key/params or throw
387
      *
388
      * @param rng a random number generator. The PK_Op may maintain a
389
      * reference to the RNG and use it many times. The rng must outlive
390
      * any operations which reference it.
391
      * @param params additional parameters
392
      * @param provider the provider to use
393
      */
394
      virtual std::unique_ptr<PK_Ops::KEM_Decryption> create_kem_decryption_op(RandomNumberGenerator& rng,
395
                                                                               std::string_view params,
396
                                                                               std::string_view provider) const;
397
398
      /**
399
      * This is an internal library function exposed on key types.
400
      * In all cases applications should use wrappers in pubkey.h
401
      *
402
      * Return a signature operation for this key/params or throw
403
      *
404
      * @param rng a random number generator. The PK_Op may maintain a
405
      * reference to the RNG and use it many times. The rng must outlive
406
      * any operations which reference it.
407
      * @param params additional parameters
408
      * @param provider the provider to use
409
      */
410
      virtual std::unique_ptr<PK_Ops::Signature> create_signature_op(RandomNumberGenerator& rng,
411
                                                                     std::string_view params,
412
                                                                     std::string_view provider) const;
413
414
      /**
415
      * This is an internal library function exposed on key types.
416
      * In all cases applications should use wrappers in pubkey.h
417
      *
418
      * Return a key agreement operation for this key/params or throw
419
      *
420
      * @param rng a random number generator. The PK_Op may maintain a
421
      * reference to the RNG and use it many times. The rng must outlive
422
      * any operations which reference it.
423
      * @param params additional parameters
424
      * @param provider the provider to use
425
      */
426
      virtual std::unique_ptr<PK_Ops::Key_Agreement> create_key_agreement_op(RandomNumberGenerator& rng,
427
                                                                             std::string_view params,
428
                                                                             std::string_view provider) const;
429
};
430
431
/**
432
* PK Secret Value Derivation Key
433
*/
434
class BOTAN_PUBLIC_API(2, 0) PK_Key_Agreement_Key : public virtual Private_Key {
435
   public:
436
      /**
437
      * Return the public value used to effect key exchange
438
      */
439
      virtual std::vector<uint8_t> public_value() const = 0;
440
};
441
442
/**
443
* Hex encode the data and separate them in blocks with `:` characters
444
*/
445
std::string BOTAN_PUBLIC_API(3, 12) format_hex_fingerprint(std::span<const uint8_t> bits);
446
447
/**
448
* Hash the input then format that hash using format_hex_fingerprint
449
*/
450
std::string BOTAN_PUBLIC_API(3, 0) create_hex_fingerprint(std::span<const uint8_t> bits, std::string_view hash_name);
451
452
/**
453
* Old interface for create_hex_fingerprint added in 2.4 pre-span
454
*/
455
0
inline std::string create_hex_fingerprint(const uint8_t bits[], size_t len, std::string_view hash_name) {
456
0
   return create_hex_fingerprint({bits, len}, hash_name);
457
0
}
458
459
}  // namespace Botan
460
461
#endif