summaryrefslogtreecommitdiffstats
path: root/base/crypto/rsa_private_key.h
blob: a6b0e20319242a1655cd6dd7bd803feab5741f1d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
// Copyright (c) 2009 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef BASE_CRYPTO_RSA_PRIVATE_KEY_H_
#define BASE_CRYPTO_RSA_PRIVATE_KEY_H_

#include "build/build_config.h"

#if defined(USE_NSS)
#include <cryptoht.h>
#include <keythi.h>
#elif defined(OS_MACOSX)
#include <Security/cssm.h>
#elif defined(OS_WIN)
#include <windows.h>
#include <wincrypt.h>
#endif

#include <vector>

#include "base/basictypes.h"

namespace base {

// Encapsulates an RSA private key. Can be used to generate new keys, export
// keys to other formats, or to extract a public key.
class RSAPrivateKey {
 public:
  // Create a new random instance. Can return NULL if initialization fails.
  static RSAPrivateKey* Create(uint16 num_bits);

  // Create a new instance by importing an existing private key. The format is
  // an ASN.1-encoded PrivateKeyInfo block from PKCS #8. This can return NULL if
  // initialization fails.
  static RSAPrivateKey* CreateFromPrivateKeyInfo(
      const std::vector<uint8>& input);

  ~RSAPrivateKey();

#if defined(USE_NSS)
  SECKEYPrivateKey* key() { return key_; }
#elif defined(OS_WIN)
  HCRYPTPROV provider() { return provider_; }
  HCRYPTKEY key() { return key_; }
#elif defined(OS_MACOSX)
  CSSM_CSP_HANDLE csp_handle() { return csp_handle_; }
  CSSM_KEY_PTR key() { return &key_; }
#endif

  // Exports the private key to a PKCS #1 PrivateKey block.
  bool ExportPrivateKey(std::vector<uint8>* output);

  // Exports the public key to an X509 SubjectPublicKeyInfo block.
  bool ExportPublicKey(std::vector<uint8>* output);

private:
  // Constructor is private. Use Create() or CreateFromPrivateKeyInfo()
  // instead.
  RSAPrivateKey();

#if defined(USE_NSS)
  SECKEYPrivateKey* key_;
  SECKEYPublicKey* public_key_;
#elif defined(OS_WIN)
  bool InitProvider();

  HCRYPTPROV provider_;
  HCRYPTKEY key_;
#elif defined(OS_MACOSX)
  CSSM_KEY key_;
  CSSM_CSP_HANDLE csp_handle_;
#endif

  DISALLOW_COPY_AND_ASSIGN(RSAPrivateKey);
};

}  // namespace base

#endif  // BASE_CRYPTO_RSA_PRIVATE_KEY_H_