Check USB device path access when prompting users to select a device.
[chromium-blink-merge.git] / chrome / browser / chromeos / platform_keys / platform_keys.h
blob79fff7dfc64c32510d059f3acaf166ef0cf48d0e
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef CHROME_BROWSER_CHROMEOS_PLATFORM_KEYS_PLATFORM_KEYS_H_
6 #define CHROME_BROWSER_CHROMEOS_PLATFORM_KEYS_PLATFORM_KEYS_H_
8 #include <string>
9 #include <vector>
11 #include "base/callback_forward.h"
12 #include "base/macros.h"
13 #include "base/memory/ref_counted.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "net/cert/x509_certificate.h"
16 #include "net/ssl/ssl_client_cert_type.h"
18 namespace content {
19 class BrowserContext;
22 namespace chromeos {
24 namespace platform_keys {
26 // A token is a store for keys or certs and can provide cryptographic
27 // operations.
28 // ChromeOS provides itself a user token and conditionally a system wide token,
29 // thus these tokens use static identifiers. The platform keys API is designed
30 // to support arbitrary other tokens in the future, which could then use
31 // run-time generated IDs.
32 extern const char kTokenIdUser[];
33 extern const char kTokenIdSystem[];
35 // Supported hash algorithms.
36 enum HashAlgorithm {
37 HASH_ALGORITHM_NONE, // The value if no hash function is selected.
38 HASH_ALGORITHM_SHA1,
39 HASH_ALGORITHM_SHA256,
40 HASH_ALGORITHM_SHA384,
41 HASH_ALGORITHM_SHA512
44 struct ClientCertificateRequest {
45 ClientCertificateRequest();
46 ~ClientCertificateRequest();
48 // The list of the types of certificates requested, sorted in order of the
49 // server's preference.
50 std::vector<net::SSLClientCertType> certificate_key_types;
52 // List of distinguished names of certificate authorities allowed by the
53 // server. Each entry must be a DER-encoded X.509 DistinguishedName.
54 std::vector<std::string> certificate_authorities;
57 namespace subtle {
58 // Functions of this namespace shouldn't be called directly from the context of
59 // an extension. Instead use PlatformKeysService which enforces restrictions
60 // upon extensions.
62 typedef base::Callback<void(const std::string& public_key_spki_der,
63 const std::string& error_message)>
64 GenerateKeyCallback;
66 // Generates a RSA key pair with |modulus_length_bits|. |token_id| is currently
67 // ignored, instead the user token associated with |browser_context| is always
68 // used. |callback| will be invoked with the resulting public key or an error.
69 void GenerateRSAKey(const std::string& token_id,
70 unsigned int modulus_length_bits,
71 const GenerateKeyCallback& callback,
72 content::BrowserContext* browser_context);
74 typedef base::Callback<void(const std::string& signature,
75 const std::string& error_message)> SignCallback;
77 // Digests |data|, applies PKCS1 padding and afterwards signs the data with the
78 // private key matching |params.public_key|. If a non empty token id is provided
79 // and the key is not found in that token, the operation aborts. |callback| will
80 // be invoked with the signature or an error message.
81 void SignRSAPKCS1Digest(const std::string& token_id,
82 const std::string& data,
83 const std::string& public_key,
84 HashAlgorithm hash_algorithm,
85 const SignCallback& callback,
86 content::BrowserContext* browser_context);
88 // Applies PKCS1 padding and afterwards signs the data with the private key
89 // matching |params.public_key|. |data| is not digested. If a non empty token id
90 // is provided and the key is not found in that token, the operation aborts.
91 // The size of |data| (number of octets) must be smaller than k - 11, where k
92 // is the key size in octets.
93 // |callback| will be invoked with the signature or an error message.
94 void SignRSAPKCS1Raw(const std::string& token_id,
95 const std::string& data,
96 const std::string& public_key,
97 const SignCallback& callback,
98 content::BrowserContext* browser_context);
100 // If the certificate request could be processed successfully, |matches| will
101 // contain the list of matching certificates (which may be empty) and
102 // |error_message| will be empty. If an error occurred, |matches| will be null
103 // and |error_message| contain an error message.
104 typedef base::Callback<void(scoped_ptr<net::CertificateList> matches,
105 const std::string& error_message)>
106 SelectCertificatesCallback;
108 // Returns the list of all certificates that match |request|. |callback| will be
109 // invoked with these matches or an error message.
110 void SelectClientCertificates(const ClientCertificateRequest& request,
111 const SelectCertificatesCallback& callback,
112 content::BrowserContext* browser_context);
114 } // namespace subtle
116 // Returns the DER encoding of the X.509 Subject Public Key Info of the public
117 // key in |certificate|.
118 std::string GetSubjectPublicKeyInfo(
119 const scoped_refptr<net::X509Certificate>& certificate);
121 // Obtains information about the public key in |certificate|.
122 // If |certificate| contains an RSA key, sets |key_size_bits| to the modulus
123 // length, and |key_type| to type RSA and returns true.
124 // If |certificate| contains any other key type, or if the public exponent of
125 // the RSA key in |certificate| is not F4, returns false and does not update any
126 // of the output parameters.
127 // All pointer arguments must not be null.
128 bool GetPublicKey(const scoped_refptr<net::X509Certificate>& certificate,
129 net::X509Certificate::PublicKeyType* key_type,
130 size_t* key_size_bits);
132 // If the list of certificates could be successfully retrieved, |certs| will
133 // contain the list of available certificates (maybe empty) and |error_message|
134 // will be empty. If an error occurred, |certs| will be empty and
135 // |error_message| contain an error message.
136 typedef base::Callback<void(scoped_ptr<net::CertificateList> certs,
137 const std::string& error_message)>
138 GetCertificatesCallback;
140 // Returns the list of all certificates with stored private key available from
141 // the given token. |token_id| is currently ignored, instead the user token
142 // associated with |browser_context| is always used. |callback| will be invoked
143 // with the list of available certificates or an error message.
144 void GetCertificates(const std::string& token_id,
145 const GetCertificatesCallback& callback,
146 content::BrowserContext* browser_context);
148 // If an error occurred during import, |error_message| will be set to an error
149 // message.
150 typedef base::Callback<void(const std::string& error_message)>
151 ImportCertificateCallback;
153 // Imports |certificate| to the given token if the certified key is already
154 // stored in this token. Any intermediate of |certificate| will be ignored.
155 // |token_id| is currently ignored, instead the user token associated with
156 // |browser_context| is always used. |callback| will be invoked when the import
157 // is finished, possibly with an error message.
158 void ImportCertificate(const std::string& token_id,
159 const scoped_refptr<net::X509Certificate>& certificate,
160 const ImportCertificateCallback& callback,
161 content::BrowserContext* browser_context);
163 // If an error occurred during removal, |error_message| will be set to an error
164 // message.
165 typedef base::Callback<void(const std::string& error_message)>
166 RemoveCertificateCallback;
168 // Removes |certificate| from the given token if present. Any intermediate of
169 // |certificate| will be ignored. |token_id| is currently ignored, instead the
170 // user token associated with |browser_context| is always used. |callback| will
171 // be invoked when the removal is finished, possibly with an error message.
172 void RemoveCertificate(const std::string& token_id,
173 const scoped_refptr<net::X509Certificate>& certificate,
174 const RemoveCertificateCallback& callback,
175 content::BrowserContext* browser_context);
177 // If the list of available tokens could be successfully retrieved, |token_ids|
178 // will contain the token ids. If an error occurs, |token_ids| will be NULL and
179 // |error_message| will be set to an error message.
180 typedef base::Callback<void(scoped_ptr<std::vector<std::string> > token_ids,
181 const std::string& error_message)>
182 GetTokensCallback;
184 // Gets the list of available tokens. |callback| will be invoked when the list
185 // of available tokens is determined, possibly with an error message.
186 // Must be called and calls |callback| on the UI thread.
187 void GetTokens(const GetTokensCallback& callback,
188 content::BrowserContext* browser_context);
190 } // namespace platform_keys
192 } // namespace chromeos
194 #endif // CHROME_BROWSER_CHROMEOS_PLATFORM_KEYS_PLATFORM_KEYS_H_