blob: 112a1b9b58ed99eb4c582bfbeb69a591fd74bd17 [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.
54CRYPTO_EXPORT ScopedSECKEYPrivateKey
Omar Morsi7294a252020-03-03 10:23:3455FindNSSKeyFromPublicKeyInfo(base::span<const uint8_t> input);
davidben85bad9e2015-05-11 20:20:1056
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.
60CRYPTO_EXPORT ScopedSECKEYPrivateKey
Omar Morsi7294a252020-03-03 10:23:3461FindNSSKeyFromPublicKeyInfoInSlot(base::span<const uint8_t> input,
davidben85bad9e2015-05-11 20:20:1062 PK11SlotInfo* slot);
63
Pavol Marko701cae52020-05-14 00:17:0364// Decodes |input| as a DER-encoded X.509 SubjectPublicKeyInfo and returns the
65// NSS representation of it.
66CRYPTO_EXPORT ScopedCERTSubjectPublicKeyInfo
67DecodeSubjectPublicKeyInfoNSS(base::span<const uint8_t> input);
68
davidben85bad9e2015-05-11 20:20:1069} // namespace crypto
70
71#endif // CRYPTO_NSS_KEY_UTIL_H_