Botan 3.12.0
Crypto and TLS for C&
pk_keys.h
Go to the documentation of this file.
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
21namespace Botan {
22
23class BigInt;
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*/
32enum class Signature_Format : uint8_t {
35
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*/
52
53class 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*/
60class BOTAN_PUBLIC_API(3, 0) Asymmetric_Key /* NOLINT(*special-member-functions) */ {
61 public:
62 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 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*/
155class 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 BOTAN_DEPRECATED("Use object_identifier") OID get_oid() const { return this->object_identifier(); }
169
170 /**
171 * @return X.509 AlgorithmIdentifier for this key
172 */
174
175 /**
176 * @return binary public key bits, with no additional 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 BER encoded public key bits
188 */
189 virtual std::vector<uint8_t> public_key_bits() const = 0;
190
191 /**
192 * @return X.509 subject key encoding for this key object
193 */
194 std::vector<uint8_t> subject_public_key() const;
195
196 /**
197 * @return Hash of the subject public key
198 */
199 std::string fingerprint_public(std::string_view alg = "SHA-256") const;
200
201 // Declarations for internal library functions not covered by SemVer follow
202
203 /**
204 * Returns more than 1 if the output of this algorithm
205 * (ciphertext, signature) should be treated as more than one
206 * value. This is used for algorithms like DSA and ECDSA, where
207 * the (r,s) output pair can be encoded as either a plain binary
208 * list or a TLV tagged DER encoding depending on the protocol.
209 *
210 * This function is public but applications should have few
211 * reasons to ever call this.
212 *
213 * @return number of message parts
214 */
215 BOTAN_DEPRECATED("Deprecated no replacement") size_t message_parts() const {
217 }
218
219 /**
220 * Returns how large each of the message parts referred to
221 * by message_parts() is
222 *
223 * This function is public but applications should have few
224 * reasons to ever call this.
225 *
226 * @return size of the message parts in bits
227 */
228 BOTAN_DEPRECATED("Deprecated no replacement") size_t message_part_size() const {
229 return _signature_element_size_for_DER_encoding().value_or(0);
230 }
231
232 // NOLINTBEGIN(bugprone-virtual-near-miss)
233
234 /*
235 * Return the format normally used by this algorithm for X.509 signatures
236 */
239 }
240
241 // NOLINTEND(bugprone-virtual-near-miss)
242
243 /**
244 * This is an internal library function exposed on key types.
245 * In almost all cases applications should use wrappers in pubkey.h
246 *
247 * Return an encryption operation for this key/params or throw
248 *
249 * @param rng a random number generator. The PK_Op may maintain a
250 * reference to the RNG and use it many times. The rng must outlive
251 * any operations which reference it.
252 * @param params additional parameters
253 * @param provider the provider to use
254 */
255 virtual std::unique_ptr<PK_Ops::Encryption> create_encryption_op(RandomNumberGenerator& rng,
256 std::string_view params,
257 std::string_view provider) const;
258
259 /**
260 * This is an internal library function exposed on key types.
261 * In almost all cases applications should use wrappers in pubkey.h
262 *
263 * Return a KEM encryption operation for this key/params or throw
264 *
265 * @param params additional parameters
266 * @param provider the provider to use
267 */
268 virtual std::unique_ptr<PK_Ops::KEM_Encryption> create_kem_encryption_op(std::string_view params,
269 std::string_view provider) const;
270
271 /**
272 * This is an internal library function exposed on key types.
273 * In all cases applications should use wrappers in pubkey.h
274 *
275 * Return a verification operation for this key/params or throw
276 * @param params additional parameters
277 * @param provider the provider to use
278 */
279 virtual std::unique_ptr<PK_Ops::Verification> create_verification_op(std::string_view params,
280 std::string_view provider) const;
281
282 /**
283 * This is an internal library function exposed on key types.
284 * In all cases applications should use wrappers in pubkey.h
285 *
286 * Return a verification operation for this combination of key and
287 * signature algorithm or throw.
288 *
289 * @param signature_algorithm is the X.509 algorithm identifier encoding the padding
290 * scheme and hash hash function used in the signature if applicable.
291 *
292 * @param provider the provider to use
293 */
294 virtual std::unique_ptr<PK_Ops::Verification> create_x509_verification_op(
295 const AlgorithmIdentifier& signature_algorithm, std::string_view provider) const;
296};
297
298/**
299* Private Key Base Class
300*/
301class BOTAN_PUBLIC_API(2, 0) Private_Key : public virtual Public_Key {
302 public:
303 /**
304 * @return BER encoded private key bits
305 */
307
308 /**
309 * @return binary private key bits, with no additional encoding
310 *
311 * Note: some algorithms (for example RSA) do not have an obvious encoding
312 * for this value due to having many different values, and thus not implement
313 * this function. The default implementation throws Not_Implemented
314 */
316
317 /**
318 * Allocate a new object for the public key associated with this
319 * private key.
320 *
321 * @return public key
322 */
323 virtual std::unique_ptr<Public_Key> public_key() const = 0;
324
325 /**
326 * @return PKCS #8 private key encoding for this key object
327 */
329
330 /**
331 * @return PKCS #8 AlgorithmIdentifier for this key
332 * Might be different from the X.509 identifier, but normally is not
333 */
335
336 /**
337 * Indicates if this key is stateful, ie that performing a private
338 * key operation requires updating the key storage.
339 */
340 virtual bool stateful_operation() const { return false; }
341
342 /**
343 * @brief Retrieves the number of remaining operations if this is a stateful private key.
344 *
345 * @returns the number of remaining operations or std::nullopt if not applicable.
346 */
347 virtual std::optional<uint64_t> remaining_operations() const { return std::nullopt; }
348
349 // Declarations for internal library functions not covered by SemVer follow
350
351 /**
352 * @return Hash of the PKCS #8 encoding for this key object
353 */
354 std::string fingerprint_private(std::string_view alg) const;
355
356 /**
357 * This is an internal library function exposed on key types.
358 * In all cases applications should use wrappers in pubkey.h
359 *
360 * Return an decryption operation for this key/params or throw
361 *
362 * @param rng a random number generator. The PK_Op may maintain a
363 * reference to the RNG and use it many times. The rng must outlive
364 * any operations which reference it.
365 * @param params additional parameters
366 * @param provider the provider to use
367 *
368 */
369 virtual std::unique_ptr<PK_Ops::Decryption> create_decryption_op(RandomNumberGenerator& rng,
370 std::string_view params,
371 std::string_view provider) const;
372
373 /**
374 * This is an internal library function exposed on key types.
375 * In all cases applications should use wrappers in pubkey.h
376 *
377 * Return a KEM decryption operation for this key/params or throw
378 *
379 * @param rng a random number generator. The PK_Op may maintain a
380 * reference to the RNG and use it many times. The rng must outlive
381 * any operations which reference it.
382 * @param params additional parameters
383 * @param provider the provider to use
384 */
385 virtual std::unique_ptr<PK_Ops::KEM_Decryption> create_kem_decryption_op(RandomNumberGenerator& rng,
386 std::string_view params,
387 std::string_view provider) const;
388
389 /**
390 * This is an internal library function exposed on key types.
391 * In all cases applications should use wrappers in pubkey.h
392 *
393 * Return a signature operation for this key/params or throw
394 *
395 * @param rng a random number generator. The PK_Op may maintain a
396 * reference to the RNG and use it many times. The rng must outlive
397 * any operations which reference it.
398 * @param params additional parameters
399 * @param provider the provider to use
400 */
401 virtual std::unique_ptr<PK_Ops::Signature> create_signature_op(RandomNumberGenerator& rng,
402 std::string_view params,
403 std::string_view provider) const;
404
405 /**
406 * This is an internal library function exposed on key types.
407 * In all cases applications should use wrappers in pubkey.h
408 *
409 * Return a key agreement operation for this key/params or throw
410 *
411 * @param rng a random number generator. The PK_Op may maintain a
412 * reference to the RNG and use it many times. The rng must outlive
413 * any operations which reference it.
414 * @param params additional parameters
415 * @param provider the provider to use
416 */
417 virtual std::unique_ptr<PK_Ops::Key_Agreement> create_key_agreement_op(RandomNumberGenerator& rng,
418 std::string_view params,
419 std::string_view provider) const;
420};
421
422/**
423* PK Secret Value Derivation Key
424*/
426 public:
427 /*
428 * @return public component of this key
429 */
430 virtual std::vector<uint8_t> public_value() const = 0;
431};
432
433/**
434* Hex encode the data and separate them in blocks with `:` characters
435*/
436std::string BOTAN_PUBLIC_API(3, 12) format_hex_fingerprint(std::span<const uint8_t> bits);
437
438/**
439* Hash the input then format that hash using format_hex_fingerprint
440*/
441std::string BOTAN_PUBLIC_API(3, 0) create_hex_fingerprint(std::span<const uint8_t> bits, std::string_view hash_name);
442
443/**
444* Old interface for create_hex_fingerprint added in 2.4 pre-span
445*/
446inline std::string create_hex_fingerprint(const uint8_t bits[], size_t len, std::string_view hash_name) {
447 return create_hex_fingerprint({bits, len}, hash_name);
448}
449
450} // namespace Botan
451
452#endif
#define BOTAN_PUBLIC_API(maj, min)
Definition api.h:21
#define BOTAN_DEPRECATED(msg)
Definition api.h:73
virtual std::string algo_name() const =0
virtual ~Asymmetric_Key()=default
virtual bool check_key(RandomNumberGenerator &rng, bool strong) const =0
virtual bool supports_operation(PublicKeyOperation op) const =0
virtual std::unique_ptr< Private_Key > generate_another(RandomNumberGenerator &rng) const =0
virtual const BigInt & get_int_field(std::string_view field) const
Definition pk_keys.cpp:18
virtual OID object_identifier() const
Definition pk_keys.cpp:22
virtual std::optional< size_t > _signature_element_size_for_DER_encoding() const
Definition pk_keys.h:144
virtual Signature_Format _default_x509_signature_format() const
Definition pk_keys.cpp:30
virtual size_t estimated_strength() const =0
virtual std::vector< uint8_t > public_value() const =0
virtual std::unique_ptr< Public_Key > public_key() const =0
virtual AlgorithmIdentifier pkcs8_algorithm_identifier() const
Definition pk_keys.h:334
virtual secure_vector< uint8_t > raw_private_key_bits() const
Definition pk_keys.cpp:87
virtual std::optional< uint64_t > remaining_operations() const
Retrieves the number of remaining operations if this is a stateful private key.
Definition pk_keys.h:347
secure_vector< uint8_t > private_key_info() const
Definition pk_keys.cpp:75
virtual bool stateful_operation() const
Definition pk_keys.h:340
virtual secure_vector< uint8_t > private_key_bits() const =0
std::string fingerprint_public(std::string_view alg="SHA-256") const
Definition pk_keys.cpp:94
OID get_oid() const
Definition pk_keys.h:168
virtual AlgorithmIdentifier algorithm_identifier() const =0
virtual std::vector< uint8_t > public_key_bits() const =0
size_t message_part_size() const
Definition pk_keys.h:228
Signature_Format default_x509_signature_format() const
Definition pk_keys.h:237
virtual std::vector< uint8_t > raw_public_key_bits() const =0
virtual size_t key_length() const =0
size_t message_parts() const
Definition pk_keys.h:215
std::vector< uint8_t > subject_public_key() const
Definition pk_keys.cpp:63
std::string create_hex_fingerprint(std::span< const uint8_t > bits, std::string_view hash_name)
Definition pk_keys.cpp:38
PublicKeyOperation
Definition pk_keys.h:46
Signature_Format
Definition pk_keys.h:32
std::vector< T, secure_allocator< T > > secure_vector
Definition secmem.h:68
std::string format_hex_fingerprint(std::span< const uint8_t > bits)
Definition pk_keys.cpp:45