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