blob: 9b642a6f85b3f5d803411bac34df21b80c4998be [file] [log] [blame]
davidben85bad9e2015-05-11 20:20:101// 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 Morsi7294a252020-03-03 10:23:348#include <secoidt.h>
davidben85bad9e2015-05-11 20:20:109#include <stdint.h>
10
11#include <vector>
12
Omar Morsi7294a252020-03-03 10:23:3413#include "base/containers/span.h"
davidben85bad9e2015-05-11 20:20:1014#include "build/build_config.h"
15#include "crypto/crypto_export.h"
16#include "crypto/scoped_nss_types.h"
17
18typedef struct PK11SlotInfoStr PK11SlotInfo;
19
20namespace crypto {
21
Omar Morsi7294a252020-03-03 10:23:3422// Generates a new RSA key pair of size |num_bits| in |slot|. Returns true on
davidben85bad9e2015-05-11 20:20:1023// success and false on failure. If |permanent| is true, the resulting key is
24// permanent and is not exportable in plaintext form.
25CRYPTO_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 Morsi7294a252020-03-03 10:23:3432// 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.
35CRYPTO_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
davidben85bad9e2015-05-11 20:20:1042// 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.
46CRYPTO_EXPORT ScopedSECKEYPrivateKey
47ImportNSSKeyFromPrivateKeyInfo(PK11SlotInfo* slot,
48 const std::vector<uint8_t>& input,
49 bool permanent);
50
davidben85bad9e2015-05-11 20:20:1051// 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.
Omar Morsie7f3cef2020-10-09 09:09:4954// Note: This function assumes the CKA_ID for public/private key pairs is
55// derived from the public key. NSS does this, but this is not guaranteed by
56// PKCS#11, so keys generated outside of NSS may not be found.
davidben85bad9e2015-05-11 20:20:1057CRYPTO_EXPORT ScopedSECKEYPrivateKey
Omar Morsi7294a252020-03-03 10:23:3458FindNSSKeyFromPublicKeyInfo(base::span<const uint8_t> input);
davidben85bad9e2015-05-11 20:20:1059
60// Decodes |input| as a DER-encoded X.509 SubjectPublicKeyInfo and searches for
61// the private key half in the slot specified by |slot|. Returns the private key
62// on success or nullptr on error.
Omar Morsie7f3cef2020-10-09 09:09:4963// Note: This function assumes the CKA_ID for public/private key pairs is
64// derived from the public key. NSS does this, but this is not guaranteed by
65// PKCS#11, so keys generated outside of NSS may not be found.
davidben85bad9e2015-05-11 20:20:1066CRYPTO_EXPORT ScopedSECKEYPrivateKey
Omar Morsi7294a252020-03-03 10:23:3467FindNSSKeyFromPublicKeyInfoInSlot(base::span<const uint8_t> input,
davidben85bad9e2015-05-11 20:20:1068 PK11SlotInfo* slot);
69
Pavol Marko701cae52020-05-14 00:17:0370// Decodes |input| as a DER-encoded X.509 SubjectPublicKeyInfo and returns the
71// NSS representation of it.
72CRYPTO_EXPORT ScopedCERTSubjectPublicKeyInfo
73DecodeSubjectPublicKeyInfoNSS(base::span<const uint8_t> input);
74
davidben85bad9e2015-05-11 20:20:1075} // namespace crypto
76
77#endif // CRYPTO_NSS_KEY_UTIL_H_