1cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)// Copyright 2014 The Chromium Authors. All rights reserved.
2cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
3cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)// found in the LICENSE file.
4cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
5cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#ifndef CHROME_BROWSER_CHROMEOS_PLATFORM_KEYS_PLATFORM_KEYS_H_
6cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#define CHROME_BROWSER_CHROMEOS_PLATFORM_KEYS_PLATFORM_KEYS_H_
7cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
8cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include <string>
9cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include <vector>
10cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
11cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "base/callback_forward.h"
12cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "base/macros.h"
13cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "base/memory/ref_counted.h"
14cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "base/memory/scoped_ptr.h"
15cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
16f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)namespace content {
17f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)class BrowserContext;
18f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)}
19cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
20cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)namespace net {
21cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)class X509Certificate;
22cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)typedef std::vector<scoped_refptr<X509Certificate> > CertificateList;
23cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)}
24cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
25cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)namespace chromeos {
26cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
27cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)namespace platform_keys {
28cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
295f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)// A token is a store for keys or certs and can provide cryptographic
305f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)// operations.
315f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)// ChromeOS provides itself a user token and conditionally a system wide token,
325f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)// thus these tokens use static identifiers. The platform keys API is designed
335f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)// to support arbitrary other tokens in the future, which could then use
345f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)// run-time generated IDs.
355f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)extern const char kTokenIdUser[];
365f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)extern const char kTokenIdSystem[];
375f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
386d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)// Supported hash algorithms.
396d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)enum HashAlgorithm {
406d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  HASH_ALGORITHM_SHA1,
416d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  HASH_ALGORITHM_SHA256,
426d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  HASH_ALGORITHM_SHA384,
436d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  HASH_ALGORITHM_SHA512
446d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)};
456d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)
46f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)namespace subtle {
47f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)// Functions of this namespace shouldn't be called directly from the context of
48f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)// an extension. Instead use PlatformKeysService which enforces restrictions
49f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)// upon extensions.
50f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
51cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)typedef base::Callback<void(const std::string& public_key_spki_der,
52cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                            const std::string& error_message)>
53cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    GenerateKeyCallback;
54cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
55f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)// Generates a RSA key pair with |modulus_length_bits|. |token_id| is currently
56f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)// ignored, instead the user token associated with |browser_context| is always
57f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)// used. |callback| will be invoked with the resulting public key or an error.
58cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)void GenerateRSAKey(const std::string& token_id,
59f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)                    unsigned int modulus_length_bits,
60cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                    const GenerateKeyCallback& callback,
61f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)                    content::BrowserContext* browser_context);
62cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
63cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)typedef base::Callback<void(const std::string& signature,
64cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                            const std::string& error_message)> SignCallback;
65cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
666d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)// Digests |data| with |hash_algorithm| and afterwards signs the digest with the
676d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)// private key matching |public_key|, if that key is stored in the given token.
686d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)// |token_id| is currently ignored, instead the user token associated with
696d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)// |browser_context| is always used. |public_key| must be the DER encoding of a
706d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)// SubjectPublicKeyInfo. |callback| will be invoked with the signature or an
716d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)// error message.
72cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)// Currently supports RSA keys only.
73cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)void Sign(const std::string& token_id,
74cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)          const std::string& public_key,
756d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)          HashAlgorithm hash_algorithm,
76cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)          const std::string& data,
77cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)          const SignCallback& callback,
78f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)          content::BrowserContext* browser_context);
79f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
80f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)}  // namespace subtle
81cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
82cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)// If the list of certificates could be successfully retrieved, |certs| will
83cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)// contain the list of available certificates (maybe empty) and |error_message|
84cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)// will be empty. If an error occurred, |certs| will be empty and
85cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)// |error_message| contain an error message.
86cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)typedef base::Callback<void(scoped_ptr<net::CertificateList> certs,
87cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                            const std::string& error_message)>
88cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    GetCertificatesCallback;
89cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
90cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)// Returns the list of all certificates with stored private key available from
91cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)// the given token. |token_id| is currently ignored, instead the user token
92f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)// associated with |browser_context| is always used. |callback| will be invoked
93f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)// with the list of available certificates or an error message.
94cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)void GetCertificates(const std::string& token_id,
95cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                     const GetCertificatesCallback& callback,
96f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)                     content::BrowserContext* browser_context);
97cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
98cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)// If an error occurred during import, |error_message| will be set to an error
99cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)// message.
100cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)typedef base::Callback<void(const std::string& error_message)>
101cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    ImportCertificateCallback;
102cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
103cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)// Imports |certificate| to the given token if the certified key is already
104cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)// stored in this token. Any intermediate of |certificate| will be ignored.
105cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)// |token_id| is currently ignored, instead the user token associated with
106f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)// |browser_context| is always used. |callback| will be invoked when the import
107f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)// is finished, possibly with an error message.
108cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)void ImportCertificate(const std::string& token_id,
109cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                       scoped_refptr<net::X509Certificate> certificate,
110cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                       const ImportCertificateCallback& callback,
111f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)                       content::BrowserContext* browser_context);
112cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
113cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)// If an error occurred during removal, |error_message| will be set to an error
114cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)// message.
115cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)typedef base::Callback<void(const std::string& error_message)>
116cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    RemoveCertificateCallback;
117cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
118cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)// Removes |certificate| from the given token if present. Any intermediate of
119cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)// |certificate| will be ignored. |token_id| is currently ignored, instead the
120f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)// user token associated with |browser_context| is always used. |callback| will
121f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)// be invoked when the removal is finished, possibly with an error message.
122cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)void RemoveCertificate(const std::string& token_id,
123cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                       scoped_refptr<net::X509Certificate> certificate,
124cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                       const RemoveCertificateCallback& callback,
125f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)                       content::BrowserContext* browser_context);
126cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
1275f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)// If the list of available tokens could be successfully retrieved, |token_ids|
1285f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)// will contain the token ids. If an error occurs, |token_ids| will be NULL and
1295f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)// |error_message| will be set to an error message.
1305f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)typedef base::Callback<void(scoped_ptr<std::vector<std::string> > token_ids,
1315f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)                            const std::string& error_message)>
1325f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    GetTokensCallback;
1335f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
1345f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)// Gets the list of available tokens. |callback| will be invoked when the list
1355f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)// of available tokens is determined, possibly with an error message.
1365f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)// Must be called and calls |callback| on the UI thread.
1375f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)void GetTokens(const GetTokensCallback& callback,
1385f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)               content::BrowserContext* browser_context);
1395f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
140cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)}  // namespace platform_keys
141cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
142cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)}  // namespace chromeos
143cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
144cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#endif  // CHROME_BROWSER_CHROMEOS_PLATFORM_KEYS_PLATFORM_KEYS_H_
145