OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef NET_SSL_SSL_PRIVATE_KEY_H_ | 5 #ifndef NET_SSL_SSL_PRIVATE_KEY_H_ |
6 #define NET_SSL_SSL_PRIVATE_KEY_H_ | 6 #define NET_SSL_SSL_PRIVATE_KEY_H_ |
7 | 7 |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 | 9 |
10 #include <vector> | 10 #include <vector> |
11 | 11 |
12 #include "base/callback_forward.h" | 12 #include "base/callback_forward.h" |
13 #include "base/macros.h" | 13 #include "base/macros.h" |
14 #include "base/memory/scoped_ptr.h" | 14 #include "base/memory/ref_counted.h" |
15 #include "base/strings/string_piece.h" | 15 #include "base/strings/string_piece.h" |
16 #include "net/base/net_errors.h" | 16 #include "net/base/net_errors.h" |
17 | 17 |
18 namespace net { | 18 namespace net { |
19 | 19 |
20 // An interface for a private key for use with SSL client authentication. | 20 // An interface for a private key for use with SSL client authentication. |
21 class SSLPrivateKey { | 21 class SSLPrivateKey : public base::RefCountedThreadSafe<SSLPrivateKey> { |
22 public: | 22 public: |
23 using SignCallback = base::Callback<void(Error, const std::vector<uint8_t>&)>; | 23 using SignCallback = base::Callback<void(Error, const std::vector<uint8_t>&)>; |
24 | 24 |
25 enum class Type { | 25 enum class Type { |
26 RSA, | 26 RSA, |
27 ECDSA, | 27 ECDSA, |
28 }; | 28 }; |
29 | 29 |
30 enum class Hash { | 30 enum class Hash { |
31 MD5_SHA1, | 31 MD5_SHA1, |
32 SHA1, | 32 SHA1, |
33 SHA256, | 33 SHA256, |
34 SHA384, | 34 SHA384, |
35 SHA512, | 35 SHA512, |
36 }; | 36 }; |
37 | 37 |
38 SSLPrivateKey() {} | 38 SSLPrivateKey() {} |
39 virtual ~SSLPrivateKey() {} | |
40 | 39 |
41 // Returns whether the key is an RSA key or an ECDSA key. Although the signing | 40 // Returns whether the key is an RSA key or an ECDSA key. Although the signing |
42 // interface is type-agnositic and type tags in interfaces are discouraged, | 41 // interface is type-agnositic and type tags in interfaces are discouraged, |
43 // TLS has key-specific logic in selecting which hashes to sign. Exposing the | 42 // TLS has key-specific logic in selecting which hashes to sign. Exposing the |
44 // key type avoids replicating BoringSSL's TLS-specific logic in SSLPrivateKey | 43 // key type avoids replicating BoringSSL's TLS-specific logic in SSLPrivateKey |
45 // implementations and complicating the interface between Chromium and | 44 // implementations and complicating the interface between Chromium and |
46 // BoringSSL. | 45 // BoringSSL. |
47 virtual Type GetType() = 0; | 46 virtual Type GetType() = 0; |
48 | 47 |
49 // Returns the digests that are supported by the key in decreasing preference. | 48 // Returns the digests that are supported by the key in decreasing preference. |
50 virtual std::vector<SSLPrivateKey::Hash> GetDigestPreferences() = 0; | 49 virtual std::vector<SSLPrivateKey::Hash> GetDigestPreferences() = 0; |
51 | 50 |
52 // Returns the maximum size of a signature, in bytes. For an RSA key, this | 51 // Returns the maximum size of a signature, in bytes. For an RSA key, this |
53 // must be the size of the modulus. | 52 // must be the size of the modulus. |
54 virtual size_t GetMaxSignatureLengthInBytes() = 0; | 53 virtual size_t GetMaxSignatureLengthInBytes() = 0; |
55 | 54 |
56 // Asynchronously signs an |input| which was computed with the hash |hash|. On | 55 // Asynchronously signs an |input| which was computed with the hash |hash|. On |
57 // completion, it calls |callback| with the signature or an error code if the | 56 // completion, it calls |callback| with the signature or an error code if the |
58 // operation failed. For an RSA key, the signature is a PKCS#1 signature. The | 57 // operation failed. For an RSA key, the signature is a PKCS#1 signature. The |
59 // SSLPrivateKey implementation is responsible for prepending the DigestInfo | 58 // SSLPrivateKey implementation is responsible for prepending the DigestInfo |
60 // prefix and adding PKCS#1 padding. | 59 // prefix and adding PKCS#1 padding. |
61 virtual void SignDigest(Hash hash, | 60 virtual void SignDigest(Hash hash, |
62 const base::StringPiece& input, | 61 const base::StringPiece& input, |
63 const SignCallback& callback) = 0; | 62 const SignCallback& callback) = 0; |
64 | 63 |
| 64 protected: |
| 65 virtual ~SSLPrivateKey() {} |
| 66 |
65 private: | 67 private: |
| 68 friend class base::RefCountedThreadSafe<SSLPrivateKey>; |
66 DISALLOW_COPY_AND_ASSIGN(SSLPrivateKey); | 69 DISALLOW_COPY_AND_ASSIGN(SSLPrivateKey); |
67 }; | 70 }; |
68 | 71 |
69 } // namespace net | 72 } // namespace net |
70 | 73 |
71 #endif // NET_SSL_SSL_PRIVATE_KEY_H_ | 74 #endif // NET_SSL_SSL_PRIVATE_KEY_H_ |
OLD | NEW |