davidben | 85bad9e | 2015-05-11 20:20:10 | [diff] [blame] | 1 | // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef CRYPTO_NSS_KEY_UTIL_H_ |
| 6 | #define CRYPTO_NSS_KEY_UTIL_H_ |
| 7 | |
Omar Morsi | 7294a25 | 2020-03-03 10:23:34 | [diff] [blame] | 8 | #include <secoidt.h> |
davidben | 85bad9e | 2015-05-11 20:20:10 | [diff] [blame] | 9 | #include <stdint.h> |
| 10 | |
| 11 | #include <vector> |
| 12 | |
Omar Morsi | 7294a25 | 2020-03-03 10:23:34 | [diff] [blame] | 13 | #include "base/containers/span.h" |
davidben | 85bad9e | 2015-05-11 20:20:10 | [diff] [blame] | 14 | #include "build/build_config.h" |
| 15 | #include "crypto/crypto_export.h" |
| 16 | #include "crypto/scoped_nss_types.h" |
| 17 | |
| 18 | typedef struct PK11SlotInfoStr PK11SlotInfo; |
| 19 | |
| 20 | namespace crypto { |
| 21 | |
Omar Morsi | 7294a25 | 2020-03-03 10:23:34 | [diff] [blame] | 22 | // Generates a new RSA key pair of size |num_bits| in |slot|. Returns true on |
davidben | 85bad9e | 2015-05-11 20:20:10 | [diff] [blame] | 23 | // success and false on failure. If |permanent| is true, the resulting key is |
| 24 | // permanent and is not exportable in plaintext form. |
| 25 | CRYPTO_EXPORT bool GenerateRSAKeyPairNSS( |
| 26 | PK11SlotInfo* slot, |
| 27 | uint16_t num_bits, |
| 28 | bool permanent, |
| 29 | ScopedSECKEYPublicKey* out_public_key, |
| 30 | ScopedSECKEYPrivateKey* out_private_key); |
| 31 | |
Omar Morsi | 7294a25 | 2020-03-03 10:23:34 | [diff] [blame] | 32 | // Generates a new EC key pair with curve |named_curve| in |slot|. Returns true |
| 33 | // on success and false on failure. If |permanent| is true, the resulting key is |
| 34 | // permanent and is not exportable in plaintext form. |
| 35 | CRYPTO_EXPORT bool GenerateECKeyPairNSS( |
| 36 | PK11SlotInfo* slot, |
| 37 | const SECOidTag named_curve, |
| 38 | bool permanent, |
| 39 | ScopedSECKEYPublicKey* out_public_key, |
| 40 | ScopedSECKEYPrivateKey* out_private_key); |
| 41 | |
davidben | 85bad9e | 2015-05-11 20:20:10 | [diff] [blame] | 42 | // Imports a private key from |input| into |slot|. |input| is interpreted as a |
| 43 | // DER-encoded PrivateKeyInfo block from PKCS #8. Returns nullptr on error. If |
| 44 | // |permanent| is true, the resulting key is permanent and is not exportable in |
| 45 | // plaintext form. |
| 46 | CRYPTO_EXPORT ScopedSECKEYPrivateKey |
| 47 | ImportNSSKeyFromPrivateKeyInfo(PK11SlotInfo* slot, |
| 48 | const std::vector<uint8_t>& input, |
| 49 | bool permanent); |
| 50 | |
davidben | 85bad9e | 2015-05-11 20:20:10 | [diff] [blame] | 51 | // Decodes |input| as a DER-encoded X.509 SubjectPublicKeyInfo and searches for |
| 52 | // the private key half in the key database. Returns the private key on success |
| 53 | // or nullptr on error. |
| 54 | CRYPTO_EXPORT ScopedSECKEYPrivateKey |
Omar Morsi | 7294a25 | 2020-03-03 10:23:34 | [diff] [blame] | 55 | FindNSSKeyFromPublicKeyInfo(base::span<const uint8_t> input); |
davidben | 85bad9e | 2015-05-11 20:20:10 | [diff] [blame] | 56 | |
| 57 | // Decodes |input| as a DER-encoded X.509 SubjectPublicKeyInfo and searches for |
| 58 | // the private key half in the slot specified by |slot|. Returns the private key |
| 59 | // on success or nullptr on error. |
| 60 | CRYPTO_EXPORT ScopedSECKEYPrivateKey |
Omar Morsi | 7294a25 | 2020-03-03 10:23:34 | [diff] [blame] | 61 | FindNSSKeyFromPublicKeyInfoInSlot(base::span<const uint8_t> input, |
davidben | 85bad9e | 2015-05-11 20:20:10 | [diff] [blame] | 62 | PK11SlotInfo* slot); |
| 63 | |
Pavol Marko | 701cae5 | 2020-05-14 00:17:03 | [diff] [blame] | 64 | // Decodes |input| as a DER-encoded X.509 SubjectPublicKeyInfo and returns the |
| 65 | // NSS representation of it. |
| 66 | CRYPTO_EXPORT ScopedCERTSubjectPublicKeyInfo |
| 67 | DecodeSubjectPublicKeyInfoNSS(base::span<const uint8_t> input); |
| 68 | |
davidben | 85bad9e | 2015-05-11 20:20:10 | [diff] [blame] | 69 | } // namespace crypto |
| 70 | |
| 71 | #endif // CRYPTO_NSS_KEY_UTIL_H_ |