nss_key_util.h 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. #ifndef CRYPTO_NSS_KEY_UTIL_H_
  5. #define CRYPTO_NSS_KEY_UTIL_H_
  6. #include <secoidt.h>
  7. #include <stdint.h>
  8. #include <vector>
  9. #include "base/containers/span.h"
  10. #include "build/build_config.h"
  11. #include "crypto/crypto_export.h"
  12. #include "crypto/scoped_nss_types.h"
  13. typedef struct PK11SlotInfoStr PK11SlotInfo;
  14. namespace crypto {
  15. // Generates a new RSA key pair of size |num_bits| in |slot|. Returns true on
  16. // success and false on failure. If |permanent| is true, the resulting key is
  17. // permanent and is not exportable in plaintext form.
  18. CRYPTO_EXPORT bool GenerateRSAKeyPairNSS(
  19. PK11SlotInfo* slot,
  20. uint16_t num_bits,
  21. bool permanent,
  22. ScopedSECKEYPublicKey* out_public_key,
  23. ScopedSECKEYPrivateKey* out_private_key);
  24. // Generates a new EC key pair with curve |named_curve| in |slot|. Returns true
  25. // on success and false on failure. If |permanent| is true, the resulting key is
  26. // permanent and is not exportable in plaintext form.
  27. CRYPTO_EXPORT bool GenerateECKeyPairNSS(
  28. PK11SlotInfo* slot,
  29. const SECOidTag named_curve,
  30. bool permanent,
  31. ScopedSECKEYPublicKey* out_public_key,
  32. ScopedSECKEYPrivateKey* out_private_key);
  33. // Imports a private key from |input| into |slot|. |input| is interpreted as a
  34. // DER-encoded PrivateKeyInfo block from PKCS #8. Returns nullptr on error. If
  35. // |permanent| is true, the resulting key is permanent and is not exportable in
  36. // plaintext form.
  37. CRYPTO_EXPORT ScopedSECKEYPrivateKey
  38. ImportNSSKeyFromPrivateKeyInfo(PK11SlotInfo* slot,
  39. const std::vector<uint8_t>& input,
  40. bool permanent);
  41. // Decodes |input| as a DER-encoded X.509 SubjectPublicKeyInfo and searches for
  42. // the private key half in the key database. Returns the private key on success
  43. // or nullptr on error.
  44. // Note: This function assumes the CKA_ID for public/private key pairs is
  45. // derived from the public key. NSS does this, but this is not guaranteed by
  46. // PKCS#11, so keys generated outside of NSS may not be found.
  47. CRYPTO_EXPORT ScopedSECKEYPrivateKey
  48. FindNSSKeyFromPublicKeyInfo(base::span<const uint8_t> input);
  49. // Decodes |input| as a DER-encoded X.509 SubjectPublicKeyInfo and searches for
  50. // the private key half in the slot specified by |slot|. Returns the private key
  51. // on success or nullptr on error.
  52. // Note: This function assumes the CKA_ID for public/private key pairs is
  53. // derived from the public key. NSS does this, but this is not guaranteed by
  54. // PKCS#11, so keys generated outside of NSS may not be found.
  55. CRYPTO_EXPORT ScopedSECKEYPrivateKey
  56. FindNSSKeyFromPublicKeyInfoInSlot(base::span<const uint8_t> input,
  57. PK11SlotInfo* slot);
  58. // Decodes |input| as a DER-encoded X.509 SubjectPublicKeyInfo and returns the
  59. // NSS representation of it.
  60. CRYPTO_EXPORT ScopedCERTSubjectPublicKeyInfo
  61. DecodeSubjectPublicKeyInfoNSS(base::span<const uint8_t> input);
  62. } // namespace crypto
  63. #endif // CRYPTO_NSS_KEY_UTIL_H_