Finish refactoring of DomCodeToUsLayoutKeyboardCode().
[chromium-blink-merge.git] / extensions / browser / api / cast_channel / cast_auth_ica.cc
blob1e9f0cae3516eba42bdd76d7337474c6c072649b
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 #include "extensions/browser/api/cast_channel/cast_auth_ica.h"
7 #include "base/base64.h"
8 #include "base/lazy_instance.h"
9 #include "crypto/signature_verifier.h"
10 #include "extensions/browser/api/cast_channel/cast_auth_util.h"
11 #include "extensions/common/api/cast_channel/authority_keys.pb.h"
12 #include "net/base/hash_value.h"
13 #include "net/cert/x509_certificate.h"
15 namespace extensions {
16 namespace core_api {
17 namespace cast_channel {
18 namespace {
20 // Fingerprint of the default ICA.
21 static const net::SHA256HashValue kDefaultFingerprintICA = { {
22 0x52, 0x9D, 0x9C, 0xD6, 0x7F, 0xE5, 0xEB, 0x69, 0x8E, 0x70, 0xDD, 0x26,
23 0xD7, 0xD8, 0xF1, 0x26, 0x59, 0xF1, 0xE6, 0xE5, 0x23, 0x48, 0xBF, 0x6A,
24 0x5C, 0xF7, 0x16, 0xE1, 0x3F, 0x41, 0x0E, 0x73
25 } };
27 // Built in public key for verifying trusted authorities data.
28 const uint8 kPublicKey[] = {
29 0x30, 0x82, 0x01, 0x22, 0x30, 0x0D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86,
30 0xF7, 0x0D, 0x01, 0x01, 0x01, 0x05, 0x00, 0x03, 0x82, 0x01, 0x0F, 0x00,
31 0x30, 0x82, 0x01, 0x0A, 0x02, 0x82, 0x01, 0x01, 0x00, 0xC3, 0x8D, 0xEC,
32 0xF5, 0x94, 0x4C, 0x66, 0x90, 0xFC, 0x08, 0xED, 0x6E, 0x7D, 0xD0, 0xD6,
33 0x93, 0x62, 0x94, 0x86, 0xFB, 0xBA, 0x6F, 0x06, 0xD5, 0x26, 0xAD, 0x52,
34 0x84, 0x5A, 0x68, 0x52, 0xF1, 0xA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22, 0x36, 0x78, 0xEA, 0x98, 0x49, 0xA4, 0x0E, 0xAB,
50 0x97, 0x0A, 0x76, 0xCD, 0x52, 0x99, 0x42, 0x44, 0xC2, 0x52, 0x40, 0x62,
51 0x34, 0xC4, 0xCD, 0xE3, 0x3D, 0xCA, 0xE1, 0x33, 0x99, 0xE9, 0xF4, 0x6E,
52 0xEB, 0xB0, 0x5D, 0xA5, 0x84, 0xA2, 0x5C, 0xC7, 0xBB, 0x14, 0x67, 0x0C,
53 0x4F, 0x02, 0x03, 0x01, 0x00, 0x01};
55 // Verifies signature of the data using built-in public key.
56 bool VerifySignature(const std::string& signature, const std::string& data) {
57 if (signature.size() != 256)
58 return false;
60 crypto::SignatureVerifier verifier;
61 crypto::SignatureVerifier::HashAlgorithm hash_alg =
62 crypto::SignatureVerifier::SHA256;
63 crypto::SignatureVerifier::HashAlgorithm mask_hash_alg = hash_alg;
64 unsigned int hash_len = 32;
66 if (!verifier.VerifyInitRSAPSS(
67 hash_alg,
68 mask_hash_alg,
69 hash_len,
70 reinterpret_cast<const uint8*>(signature.data()),
71 signature.size(),
72 kPublicKey,
73 sizeof(kPublicKey))) {
74 return false;
77 verifier.VerifyUpdate(reinterpret_cast<const uint8*>(data.data()),
78 data.size());
80 return verifier.VerifyFinal();
83 } // namespace
85 AuthorityKeyStore::AuthorityKeyStore() {
88 AuthorityKeyStore::~AuthorityKeyStore() {
91 base::StringPiece AuthorityKeyStore::GetICAPublicKeyFromFingerprint(
92 const net::SHA256HashValue& fingerprint) {
93 AuthorityKeysMap::const_iterator it =
94 certificate_authorities_.find(fingerprint);
95 return (it == certificate_authorities_.end()) ? base::StringPiece()
96 : (*it).second;
99 base::StringPiece AuthorityKeyStore::GetDefaultICAPublicKey() {
100 return GetICAPublicKeyFromFingerprint(kDefaultFingerprintICA);
103 bool AuthorityKeyStore::Load(const std::string& keys) {
104 certificate_authorities_.clear();
105 authority_keys_.reset(new proto::AuthorityKeys());
107 if (!authority_keys_->ParseFromString(keys))
108 return false;
110 for (int i = 0; i < authority_keys_->keys_size(); i++) {
111 const std::string& fingerprint = authority_keys_->keys(i).fingerprint();
112 net::SHA256HashValue hash;
113 // Ensure it's the size of SHA-256 fingerprint data.
114 if (fingerprint.size() != sizeof(hash.data)) {
115 return false;
117 memcpy(hash.data, fingerprint.data(), sizeof(hash.data));
118 const std::string& public_key = authority_keys_->keys(i).public_key();
119 certificate_authorities_.insert(
120 make_pair(hash, base::StringPiece(public_key)));
123 return true;
126 // Trusted certificate authorities.
127 static base::LazyInstance<AuthorityKeyStore>::Leaky g_authority_keys_store =
128 LAZY_INSTANCE_INITIALIZER;
130 bool SetTrustedCertificateAuthorities(const std::string& keys,
131 const std::string& signature) {
132 std::string decoded_keys;
133 if (!base::Base64Decode(keys, &decoded_keys))
134 return false;
136 std::string decoded_signature;
137 if (!base::Base64Decode(signature, &decoded_signature))
138 return false;
140 if (!VerifySignature(decoded_signature, decoded_keys))
141 return false;
143 return g_authority_keys_store.Get().Load(decoded_keys);
146 base::StringPiece GetTrustedICAPublicKey(const base::StringPiece& data) {
147 AuthorityKeyStore& authority_keys_store = g_authority_keys_store.Get();
148 scoped_refptr<net::X509Certificate> ica_cert =
149 net::X509Certificate::CreateFromBytes(data.data(), data.size());
150 if (!ica_cert.get()) {
151 LOG(ERROR) << "Invalid ICA certificate.";
152 return "";
154 return authority_keys_store.GetICAPublicKeyFromFingerprint(
155 net::X509Certificate::CalculateFingerprint256(
156 ica_cert->os_cert_handle()));
159 base::StringPiece GetDefaultTrustedICAPublicKey() {
160 AuthorityKeyStore& authority_keys_store = g_authority_keys_store.Get();
161 return authority_keys_store.GetDefaultICAPublicKey();
164 } // namespace cast_channel
165 } // namespace core_api
166 } // namespace extensions