OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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_CERT_X509_UTIL_H_ | 5 #ifndef NET_CERT_X509_UTIL_H_ |
6 #define NET_CERT_X509_UTIL_H_ | 6 #define NET_CERT_X509_UTIL_H_ |
7 | 7 |
8 #include <string> | 8 #include <string> |
9 | 9 |
10 #include "base/memory/ref_counted.h" | 10 #include "base/memory/ref_counted.h" |
11 #include "base/time/time.h" | 11 #include "base/time/time.h" |
| 12 #include "crypto/hmac.h" |
12 #include "net/base/net_export.h" | 13 #include "net/base/net_export.h" |
13 | 14 |
14 namespace crypto { | 15 namespace crypto { |
15 class ECPrivateKey; | 16 class ECPrivateKey; |
16 class RSAPrivateKey; | 17 class RSAPrivateKey; |
17 } | 18 } |
18 | 19 |
19 namespace net { | 20 namespace net { |
20 | 21 |
21 class X509Certificate; | 22 class X509Certificate; |
22 | 23 |
23 namespace x509_util { | 24 namespace x509_util { |
24 | 25 |
25 // Returns true if the times can be used to create an X.509 certificate. | 26 // Returns true if the times can be used to create an X.509 certificate. |
26 // Certificates can accept dates from Jan 1st, 1 to Dec 31, 9999. A bug in NSS | 27 // Certificates can accept dates from Jan 1st, 1 to Dec 31, 9999. A bug in NSS |
27 // limited the range to 1950-9999 | 28 // limited the range to 1950-9999 |
28 // (https://bugzilla.mozilla.org/show_bug.cgi?id=786531). This function will | 29 // (https://bugzilla.mozilla.org/show_bug.cgi?id=786531). This function will |
29 // return whether it is supported by the currently used crypto library. | 30 // return whether it is supported by the currently used crypto library. |
30 NET_EXPORT_PRIVATE bool IsSupportedValidityRange(base::Time not_valid_before, | 31 NET_EXPORT_PRIVATE bool IsSupportedValidityRange(base::Time not_valid_before, |
31 base::Time not_valid_after); | 32 base::Time not_valid_after); |
32 | 33 |
33 // Creates a server bound certificate containing the public key in |key|. | 34 // Creates a private keypair and server bound certificate. |
34 // Domain, serial number and validity period are given as | 35 // Domain, serial number and validity period are given as |
35 // parameters. The certificate is signed by the private key in |key|. | 36 // parameters. The certificate is signed by the private key in |key|. |
36 // The hashing algorithm for the signature is SHA-1. | 37 // The signature algorithm may be updated periodically to match best practices. |
37 // | 38 // |
38 // See Internet Draft draft-balfanz-tls-obc-00 for more details: | 39 // See Internet Draft draft-balfanz-tls-obc-00 for more details: |
39 // http://tools.ietf.org/html/draft-balfanz-tls-obc-00 | 40 // http://tools.ietf.org/html/draft-balfanz-tls-obc-00 |
40 NET_EXPORT_PRIVATE bool CreateDomainBoundCertEC( | 41 NET_EXPORT_PRIVATE bool CreateKeyAndDomainBoundCertEC( |
| 42 const std::string& domain, |
| 43 uint32 serial_number, |
| 44 base::Time not_valid_before, |
| 45 base::Time not_valid_after, |
| 46 crypto::ECPrivateKey** key, |
| 47 std::string* der_cert); |
| 48 |
| 49 // Helper function for CreateKeyAndDomainBoundCertEC |
| 50 NET_EXPORT_PRIVATE bool CreateDomainBoundCertECInternal( |
41 crypto::ECPrivateKey* key, | 51 crypto::ECPrivateKey* key, |
| 52 crypto::HMAC::HashAlgorithm alg, |
42 const std::string& domain, | 53 const std::string& domain, |
43 uint32 serial_number, | 54 uint32 serial_number, |
44 base::Time not_valid_before, | 55 base::Time not_valid_before, |
45 base::Time not_valid_after, | 56 base::Time not_valid_after, |
46 std::string* der_cert); | 57 std::string* der_cert); |
47 | 58 |
48 // Create a self-signed certificate containing the public key in |key|. | 59 // Creates a public-private keypair and a self-signed certificate. |
49 // Subject, serial number and validity period are given as parameters. | 60 // Subject, serial number and validity period are given as parameters. |
50 // The certificate is signed by the private key in |key|. The hashing | 61 // The certificate is signed by the private key in |key|. The key length and |
51 // algorithm for the signature is SHA-1. | 62 // signature algorithm may be updated periodically to match best practices. |
52 // | 63 // |
53 // |subject| is a distinguished name defined in RFC4514. | 64 // |subject| is a distinguished name defined in RFC4514. |
54 // | 65 // |
55 // An example: | 66 // An example: |
56 // CN=Michael Wong,O=FooBar Corporation,DC=foobar,DC=com | 67 // CN=Michael Wong,O=FooBar Corporation,DC=foobar,DC=com |
57 // | 68 // |
58 // SECURITY WARNING | 69 // SECURITY WARNING |
59 // | 70 // |
60 // Using self-signed certificates has the following security risks: | 71 // Using self-signed certificates has the following security risks: |
61 // 1. Encryption without authentication and thus vulnerable to | 72 // 1. Encryption without authentication and thus vulnerable to |
62 // man-in-the-middle attacks. | 73 // man-in-the-middle attacks. |
63 // 2. Self-signed certificates cannot be revoked. | 74 // 2. Self-signed certificates cannot be revoked. |
64 // | 75 // |
65 // Use this certificate only after the above risks are acknowledged. | 76 // Use this certificate only after the above risks are acknowledged. |
66 NET_EXPORT bool CreateSelfSignedCert(crypto::RSAPrivateKey* key, | 77 NET_EXPORT bool CreateKeyAndSelfSignedCert(const std::string& subject, |
67 const std::string& subject, | 78 uint32 serial_number, |
68 uint32 serial_number, | 79 base::Time not_valid_before, |
69 base::Time not_valid_before, | 80 base::Time not_valid_after, |
70 base::Time not_valid_after, | 81 crypto::RSAPrivateKey** key, |
71 std::string* der_cert); | 82 std::string* der_cert); |
| 83 |
| 84 // Helper function for CreateKeyAndSelfSignedCert. |
| 85 NET_EXPORT_PRIVATE bool CreateSelfSignedCertInternal( |
| 86 crypto::RSAPrivateKey* key, |
| 87 crypto::HMAC::HashAlgorithm alg, |
| 88 const std::string& subject, |
| 89 uint32 serial_number, |
| 90 base::Time not_valid_before, |
| 91 base::Time not_valid_after, |
| 92 std::string* der_cert); |
| 93 |
| 94 // Legacy certificate signing function. This function is preserved here because |
| 95 // best practices suggest that keys signed with one hash algorithm should not be |
| 96 // signed again later with a different algorithm. Eventually, all users of this |
| 97 // function should migrate to CreateKeyAndSelfSignedCert. |
| 98 NET_EXPORT bool CreateSha1SelfSignedCert(crypto::RSAPrivateKey* key, |
| 99 const std::string& subject, |
| 100 uint32 serial_number, |
| 101 base::Time not_valid_before, |
| 102 base::Time not_valid_after, |
| 103 std::string* der_cert); |
72 | 104 |
73 // Comparator for use in STL algorithms that will sort client certificates by | 105 // Comparator for use in STL algorithms that will sort client certificates by |
74 // order of preference. | 106 // order of preference. |
75 // Returns true if |a| is more preferable than |b|, allowing it to be used | 107 // Returns true if |a| is more preferable than |b|, allowing it to be used |
76 // with any algorithm that compares according to strict weak ordering. | 108 // with any algorithm that compares according to strict weak ordering. |
77 // | 109 // |
78 // Criteria include: | 110 // Criteria include: |
79 // - Prefer certificates that have a longer validity period (later | 111 // - Prefer certificates that have a longer validity period (later |
80 // expiration dates) | 112 // expiration dates) |
81 // - If equal, prefer certificates that were issued more recently | 113 // - If equal, prefer certificates that were issued more recently |
82 // - If equal, prefer shorter chains (if available) | 114 // - If equal, prefer shorter chains (if available) |
83 class NET_EXPORT_PRIVATE ClientCertSorter { | 115 class NET_EXPORT_PRIVATE ClientCertSorter { |
84 public: | 116 public: |
85 ClientCertSorter(); | 117 ClientCertSorter(); |
86 | 118 |
87 bool operator()( | 119 bool operator()( |
88 const scoped_refptr<X509Certificate>& a, | 120 const scoped_refptr<X509Certificate>& a, |
89 const scoped_refptr<X509Certificate>& b) const; | 121 const scoped_refptr<X509Certificate>& b) const; |
90 | 122 |
91 private: | 123 private: |
92 base::Time now_; | 124 base::Time now_; |
93 }; | 125 }; |
94 | 126 |
95 } // namespace x509_util | 127 } // namespace x509_util |
96 | 128 |
97 } // namespace net | 129 } // namespace net |
98 | 130 |
99 #endif // NET_CERT_X509_UTIL_H_ | 131 #endif // NET_CERT_X509_UTIL_H_ |
OLD | NEW |