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 <string> | 10 #include <string> |
10 | 11 |
11 #include "base/memory/ref_counted.h" | 12 #include "base/basictypes.h" |
12 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
| 14 #include "base/threading/non_thread_safe.h" |
| 15 #include "net/base/completion_callback.h" |
13 #include "net/base/net_api.h" | 16 #include "net/base/net_api.h" |
14 | 17 |
15 namespace net { | 18 namespace net { |
16 | 19 |
| 20 class OriginBoundCertServiceJob; |
| 21 class OriginBoundCertServiceWorker; |
17 class OriginBoundCertStore; | 22 class OriginBoundCertStore; |
18 | 23 |
19 // A class for creating and fetching origin bound certs. | 24 // A class for creating and fetching origin bound certs. |
| 25 // Inherits from NonThreadSafe in order to use the function |
| 26 // |CalledOnValidThread|. |
20 class NET_API OriginBoundCertService | 27 class NET_API OriginBoundCertService |
21 : public base::RefCountedThreadSafe<OriginBoundCertService> { | 28 : NON_EXPORTED_BASE(public base::NonThreadSafe) { |
22 public: | 29 public: |
| 30 // Opaque type used to cancel a request. |
| 31 typedef void* RequestHandle; |
| 32 |
23 // This object owns origin_bound_cert_store. | 33 // This object owns origin_bound_cert_store. |
24 explicit OriginBoundCertService( | 34 explicit OriginBoundCertService( |
25 OriginBoundCertStore* origin_bound_cert_store); | 35 OriginBoundCertStore* origin_bound_cert_store); |
26 | 36 |
27 ~OriginBoundCertService(); | 37 ~OriginBoundCertService(); |
28 | 38 |
29 // TODO(rkn): Specify certificate type (RSA or DSA). | 39 // TODO(rkn): Specify certificate type (RSA or DSA). |
30 // TODO(rkn): Key generation can be time consuming, so this should have an | 40 // |
31 // asynchronous interface. | |
32 // Fetches the origin bound cert for the specified origin if one exists | 41 // Fetches the origin bound cert for the specified origin if one exists |
33 // and creates one otherwise. On success, |private_key_result| stores a | 42 // and creates one otherwise. Returns OK if successful or an error code upon |
34 // DER-encoded PrivateKeyInfo struct, and |cert_result| stores a DER-encoded | 43 // failure. |
35 // certificate. | 44 // |
36 bool GetOriginBoundCert(const std::string& origin, | 45 // On successful completion, |private_key| stores a DER-encoded |
37 std::string* private_key_result, | 46 // PrivateKeyInfo struct, and |cert| stores a DER-encoded certificate. |
38 std::string* cert_result); | 47 // |
| 48 // |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 |
| 50 // be passed to the callback when available. |
| 51 // |
| 52 // 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 |
| 54 // completed. |
| 55 int GetOriginBoundCert(const std::string& origin, |
| 56 std::string* private_key, |
| 57 std::string* cert, |
| 58 CompletionCallback* callback, |
| 59 RequestHandle* out_req); |
| 60 |
| 61 // Cancels the specified request. |req| is the handle returned by |
| 62 // GetOriginBoundCert(). After a request is canceled, its completion |
| 63 // callback will not be called. |
| 64 void CancelRequest(RequestHandle req); |
39 | 65 |
40 // Public only for unit testing. | 66 // Public only for unit testing. |
41 int GetCertCount(); | 67 int cert_count(); |
| 68 uint64 requests() const { return requests_; } |
| 69 uint64 cert_store_hits() const { return cert_store_hits_; } |
| 70 uint64 inflight_joins() const { return inflight_joins_; } |
42 | 71 |
43 private: | 72 private: |
| 73 friend class OriginBoundCertServiceWorker; // Calls HandleResult. |
| 74 |
| 75 // On success, |private_key| stores a DER-encoded PrivateKeyInfo |
| 76 // struct, and |cert| stores a DER-encoded certificate. Returns |
| 77 // OK if successful and an error code otherwise. |
| 78 // |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 |
| 80 // with a LazyInstance, which is not allowed on a worker thread. |
| 81 static int GenerateCert(const std::string& origin, |
| 82 uint32 serial_number, |
| 83 std::string* private_key, |
| 84 std::string* cert); |
| 85 |
| 86 void HandleResult(const std::string& origin, |
| 87 int error, |
| 88 const std::string& private_key, |
| 89 const std::string& cert); |
| 90 |
44 scoped_ptr<OriginBoundCertStore> origin_bound_cert_store_; | 91 scoped_ptr<OriginBoundCertStore> origin_bound_cert_store_; |
| 92 |
| 93 // inflight_ maps from an origin to an active generation which is taking |
| 94 // place. |
| 95 std::map<std::string, OriginBoundCertServiceJob*> inflight_; |
| 96 |
| 97 uint64 requests_; |
| 98 uint64 cert_store_hits_; |
| 99 uint64 inflight_joins_; |
| 100 |
| 101 DISALLOW_COPY_AND_ASSIGN(OriginBoundCertService); |
45 }; | 102 }; |
46 | 103 |
47 } // namespace net | 104 } // namespace net |
48 | 105 |
49 #endif // NET_BASE_ORIGIN_BOUND_CERT_SERVICE_H_ | 106 #endif // NET_BASE_ORIGIN_BOUND_CERT_SERVICE_H_ |
OLD | NEW |