| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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_BASE_ORIGIN_BOUND_CERT_SERVICE_H_ | 5 #ifndef NET_BASE_ORIGIN_BOUND_CERT_SERVICE_H_ |
| 6 #define NET_BASE_ORIGIN_BOUND_CERT_SERVICE_H_ | 6 #define NET_BASE_ORIGIN_BOUND_CERT_SERVICE_H_ |
| 7 #pragma once | 7 #pragma once |
| 8 | 8 |
| 9 #include <map> | 9 #include <map> |
| 10 #include <string> | 10 #include <string> |
| 11 #include <vector> |
| 11 | 12 |
| 12 #include "base/basictypes.h" | 13 #include "base/basictypes.h" |
| 13 #include "base/memory/scoped_ptr.h" | 14 #include "base/memory/scoped_ptr.h" |
| 14 #include "base/threading/non_thread_safe.h" | 15 #include "base/threading/non_thread_safe.h" |
| 15 #include "net/base/completion_callback.h" | 16 #include "net/base/completion_callback.h" |
| 16 #include "net/base/net_export.h" | 17 #include "net/base/net_export.h" |
| 18 #include "net/base/ssl_client_cert_type.h" |
| 17 | 19 |
| 18 namespace net { | 20 namespace net { |
| 19 | 21 |
| 20 class OriginBoundCertServiceJob; | 22 class OriginBoundCertServiceJob; |
| 21 class OriginBoundCertServiceWorker; | 23 class OriginBoundCertServiceWorker; |
| 22 class OriginBoundCertStore; | 24 class OriginBoundCertStore; |
| 23 | 25 |
| 24 // A class for creating and fetching origin bound certs. | 26 // A class for creating and fetching origin bound certs. |
| 25 // Inherits from NonThreadSafe in order to use the function | 27 // Inherits from NonThreadSafe in order to use the function |
| 26 // |CalledOnValidThread|. | 28 // |CalledOnValidThread|. |
| 27 class NET_EXPORT OriginBoundCertService | 29 class NET_EXPORT OriginBoundCertService |
| 28 : NON_EXPORTED_BASE(public base::NonThreadSafe) { | 30 : NON_EXPORTED_BASE(public base::NonThreadSafe) { |
| 29 public: | 31 public: |
| 30 // Opaque type used to cancel a request. | 32 // Opaque type used to cancel a request. |
| 31 typedef void* RequestHandle; | 33 typedef void* RequestHandle; |
| 32 | 34 |
| 35 // Password used on EncryptedPrivateKeyInfo data stored in EC private_key |
| 36 // values. (This is not used to provide any security, but to workaround NSS |
| 37 // being unable to import unencrypted PrivateKeyInfo for EC keys.) |
| 38 static const char kEPKIPassword[]; |
| 39 |
| 33 // This object owns origin_bound_cert_store. | 40 // This object owns origin_bound_cert_store. |
| 34 explicit OriginBoundCertService( | 41 explicit OriginBoundCertService( |
| 35 OriginBoundCertStore* origin_bound_cert_store); | 42 OriginBoundCertStore* origin_bound_cert_store); |
| 36 | 43 |
| 37 ~OriginBoundCertService(); | 44 ~OriginBoundCertService(); |
| 38 | 45 |
| 39 // TODO(rkn): Specify certificate type (RSA or DSA). | 46 // Fetches the origin bound cert for the specified origin of the specified |
| 47 // type if one exists and creates one otherwise. Returns OK if successful or |
| 48 // an error code upon failure. |
| 40 // | 49 // |
| 41 // Fetches the origin bound cert for the specified origin if one exists | 50 // |requested_types| is a list of the TLS ClientCertificateTypes the site will |
| 42 // and creates one otherwise. Returns OK if successful or an error code upon | 51 // accept, ordered from most preferred to least preferred. Types we don't |
| 43 // failure. | 52 // support will be ignored. See ssl_client_cert_type.h. |
| 44 // | 53 // |
| 45 // On successful completion, |private_key| stores a DER-encoded | 54 // On successful completion, |private_key| stores a DER-encoded |
| 46 // PrivateKeyInfo struct, and |cert| stores a DER-encoded certificate. | 55 // PrivateKeyInfo struct, and |cert| stores a DER-encoded certificate, and |
| 56 // |type| specifies the type of certificate that was returned. |
| 47 // | 57 // |
| 48 // |callback| must not be null. ERR_IO_PENDING is returned if the operation | 58 // |callback| must not be null. ERR_IO_PENDING is returned if the operation |
| 49 // could not be completed immediately, in which case the result code will | 59 // could not be completed immediately, in which case the result code will |
| 50 // be passed to the callback when available. | 60 // be passed to the callback when available. |
| 51 // | 61 // |
| 52 // If |out_req| is non-NULL, then |*out_req| will be filled with a handle to | 62 // If |out_req| is non-NULL, then |*out_req| will be filled with a handle to |
| 53 // the async request. This handle is not valid after the request has | 63 // the async request. This handle is not valid after the request has |
| 54 // completed. | 64 // completed. |
| 55 int GetOriginBoundCert(const std::string& origin, | 65 int GetOriginBoundCert( |
| 56 std::string* private_key, | 66 const std::string& origin, |
| 57 std::string* cert, | 67 const std::vector<uint8>& requested_types, |
| 58 const CompletionCallback& callback, | 68 SSLClientCertType* type, |
| 59 RequestHandle* out_req); | 69 std::string* private_key, |
| 70 std::string* cert, |
| 71 const CompletionCallback& callback, |
| 72 RequestHandle* out_req); |
| 60 | 73 |
| 61 // Cancels the specified request. |req| is the handle returned by | 74 // Cancels the specified request. |req| is the handle returned by |
| 62 // GetOriginBoundCert(). After a request is canceled, its completion | 75 // GetOriginBoundCert(). After a request is canceled, its completion |
| 63 // callback will not be called. | 76 // callback will not be called. |
| 64 void CancelRequest(RequestHandle req); | 77 void CancelRequest(RequestHandle req); |
| 65 | 78 |
| 66 // Public only for unit testing. | 79 // Public only for unit testing. |
| 67 int cert_count(); | 80 int cert_count(); |
| 68 uint64 requests() const { return requests_; } | 81 uint64 requests() const { return requests_; } |
| 69 uint64 cert_store_hits() const { return cert_store_hits_; } | 82 uint64 cert_store_hits() const { return cert_store_hits_; } |
| 70 uint64 inflight_joins() const { return inflight_joins_; } | 83 uint64 inflight_joins() const { return inflight_joins_; } |
| 71 | 84 |
| 72 private: | 85 private: |
| 73 friend class OriginBoundCertServiceWorker; // Calls HandleResult. | 86 friend class OriginBoundCertServiceWorker; // Calls HandleResult. |
| 74 | 87 |
| 75 // On success, |private_key| stores a DER-encoded PrivateKeyInfo | 88 // On success, |private_key| stores a DER-encoded PrivateKeyInfo |
| 76 // struct, and |cert| stores a DER-encoded certificate. Returns | 89 // struct, and |cert| stores a DER-encoded certificate. Returns |
| 77 // OK if successful and an error code otherwise. | 90 // OK if successful and an error code otherwise. |
| 78 // |serial_number| is passed in because it is created with the function | 91 // |serial_number| is passed in because it is created with the function |
| 79 // base::RandInt, which opens the file /dev/urandom. /dev/urandom is opened | 92 // base::RandInt, which opens the file /dev/urandom. /dev/urandom is opened |
| 80 // with a LazyInstance, which is not allowed on a worker thread. | 93 // with a LazyInstance, which is not allowed on a worker thread. |
| 81 static int GenerateCert(const std::string& origin, | 94 static int GenerateCert(const std::string& origin, |
| 95 SSLClientCertType type, |
| 82 uint32 serial_number, | 96 uint32 serial_number, |
| 83 std::string* private_key, | 97 std::string* private_key, |
| 84 std::string* cert); | 98 std::string* cert); |
| 85 | 99 |
| 86 void HandleResult(const std::string& origin, | 100 void HandleResult(const std::string& origin, |
| 87 int error, | 101 int error, |
| 102 SSLClientCertType type, |
| 88 const std::string& private_key, | 103 const std::string& private_key, |
| 89 const std::string& cert); | 104 const std::string& cert); |
| 90 | 105 |
| 91 scoped_ptr<OriginBoundCertStore> origin_bound_cert_store_; | 106 scoped_ptr<OriginBoundCertStore> origin_bound_cert_store_; |
| 92 | 107 |
| 93 // inflight_ maps from an origin to an active generation which is taking | 108 // inflight_ maps from an origin to an active generation which is taking |
| 94 // place. | 109 // place. |
| 95 std::map<std::string, OriginBoundCertServiceJob*> inflight_; | 110 std::map<std::string, OriginBoundCertServiceJob*> inflight_; |
| 96 | 111 |
| 97 uint64 requests_; | 112 uint64 requests_; |
| 98 uint64 cert_store_hits_; | 113 uint64 cert_store_hits_; |
| 99 uint64 inflight_joins_; | 114 uint64 inflight_joins_; |
| 100 | 115 |
| 101 DISALLOW_COPY_AND_ASSIGN(OriginBoundCertService); | 116 DISALLOW_COPY_AND_ASSIGN(OriginBoundCertService); |
| 102 }; | 117 }; |
| 103 | 118 |
| 104 } // namespace net | 119 } // namespace net |
| 105 | 120 |
| 106 #endif // NET_BASE_ORIGIN_BOUND_CERT_SERVICE_H_ | 121 #endif // NET_BASE_ORIGIN_BOUND_CERT_SERVICE_H_ |
| OLD | NEW |