OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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 #include "extensions/browser/api/cast_channel/cast_auth_util.h" | 5 #include "extensions/browser/api/cast_channel/cast_auth_util.h" |
6 | 6 |
7 #include <openssl/err.h> | |
8 #include <openssl/evp.h> | |
9 #include <openssl/rsa.h> | |
10 #include <openssl/x509.h> | |
11 #include <stddef.h> | |
12 | |
7 #include "base/logging.h" | 13 #include "base/logging.h" |
14 #include "base/strings/stringprintf.h" | |
15 #include "crypto/openssl_util.h" | |
16 #include "crypto/scoped_openssl_types.h" | |
17 #include "extensions/browser/api/cast_channel/cast_auth_ica.h" | |
18 #include "extensions/browser/api/cast_channel/cast_message_util.h" | |
19 #include "extensions/common/api/cast_channel/cast_channel.pb.h" | |
20 #include "net/cert/x509_certificate.h" | |
21 #include "net/cert/x509_util_openssl.h" | |
8 | 22 |
9 namespace extensions { | 23 namespace extensions { |
10 namespace core_api { | 24 namespace core_api { |
11 namespace cast_channel { | 25 namespace cast_channel { |
26 namespace { | |
12 | 27 |
13 AuthResult AuthenticateChallengeReply(const CastMessage& challenge_reply, | 28 typedef crypto::ScopedOpenSSL<X509, X509_free>::Type ScopedX509; |
14 const std::string& peer_cert) { | 29 |
15 NOTREACHED(); | 30 std::vector<std::pair<std::string, int>> GetOpenSSLErrors() { |
31 const char* filename; | |
32 int line_num; | |
33 std::vector<std::pair<std::string, int>> output; | |
34 while (ERR_get_error_line(&filename, &line_num) != 0) { | |
mark a. foltz
2014/10/31 22:29:25
Do we want to capture the return value from this t
davidben
2014/11/01 00:19:34
Please do not use ERR_error_string and use ERR_err
Kevin M
2014/11/03 18:35:57
? The code is calling ERR_get_error_line(), which
davidben
2014/11/03 19:01:50
Nah, that was fine. Sorry, I was referring to the
| |
35 output.push_back(std::make_pair(filename, line_num)); | |
36 } | |
37 return output; | |
38 } | |
39 | |
40 } // namespace | |
41 | |
42 // This function does the following | |
43 // * Verifies that the trusted CA |response.intermediate_certificate| is | |
44 // whitelisted for use. | |
45 // * Verifies that |response.client_auth_certificate| is signed | |
46 // by the trusted CA certificate. | |
47 // * Verifies that |response.signature| matches the signature | |
48 // of |peer_cert| by |response.client_auth_certificate|'s public | |
49 // key. | |
50 AuthResult VerifyCredentials(const AuthResponse& response, | |
51 const std::string& peer_cert) { | |
52 crypto::OpenSSLErrStackTracer err_tracer(FROM_HERE); | |
53 | |
54 // Get the public key of the ICA that was used to sign the client's cert. | |
55 base::StringPiece ca_public_key_bytes; | |
56 if (response.intermediate_certificate().size() <= 0) { | |
57 ca_public_key_bytes = GetDefaultTrustedICAPublicKey(); | |
58 } else { | |
59 ca_public_key_bytes = | |
60 GetTrustedICAPublicKey(response.intermediate_certificate(0)); | |
61 if (ca_public_key_bytes.empty()) { | |
62 LOG(ERROR) << "Couldn't find trusted ICA."; | |
63 return AuthResult::CreateWithOpenSSLErrors( | |
64 "failed to verify credentials: cert not signed by trusted CA", | |
65 AuthResult::ERROR_CERT_NOT_SIGNED_BY_TRUSTED_CA, | |
66 GetOpenSSLErrors()); | |
67 } | |
68 } | |
69 | |
70 // Parse the CA public key. | |
71 const uint8_t* ca_ptr = | |
72 reinterpret_cast<const uint8_t*>(ca_public_key_bytes.data()); | |
73 const uint8_t* ca_public_key_end = ca_ptr + ca_public_key_bytes.size(); | |
74 crypto::ScopedRSA ca_public_key_rsa( | |
75 d2i_RSAPublicKey(NULL, &ca_ptr, ca_public_key_bytes.size())); | |
76 if (!ca_public_key_rsa || ca_ptr != ca_public_key_end) { | |
77 LOG(ERROR) << "Failed to import trusted public key."; | |
78 return AuthResult::CreateWithOpenSSLErrors( | |
79 "failed to import trusted public key.", | |
80 AuthResult::ERROR_CERT_PARSING_FAILED, | |
81 GetOpenSSLErrors()); | |
82 } | |
83 crypto::ScopedEVP_PKEY ca_public_key(EVP_PKEY_new()); | |
84 if (!ca_public_key || | |
85 !EVP_PKEY_set1_RSA(ca_public_key.get(), ca_public_key_rsa.get())) { | |
86 LOG(ERROR) << "Failed to initialize EVP_PKEY"; | |
87 return AuthResult::CreateWithOpenSSLErrors( | |
88 "failed to initialize EVP_PKEY.", | |
89 AuthResult::ERROR_CANNOT_EXTRACT_PUBLIC_KEY, | |
90 GetOpenSSLErrors()); | |
91 } | |
92 | |
93 // Parse the client auth certificate. | |
94 const uint8_t* client_cert_ptr = reinterpret_cast<const uint8_t*>( | |
95 response.client_auth_certificate().data()); | |
96 const uint8_t* client_cert_end = | |
97 client_cert_ptr + | |
98 response.client_auth_certificate().size(); | |
99 const ScopedX509 client_cert( | |
100 d2i_X509(NULL, &client_cert_ptr, | |
101 response.client_auth_certificate().size())); | |
102 if (!client_cert || client_cert_ptr != client_cert_end) { | |
103 LOG(ERROR) << "Failed to parse certificate."; | |
104 return AuthResult::CreateWithOpenSSLErrors( | |
105 "failed to parse client_auth_certificate.", | |
106 AuthResult::ERROR_CERT_PARSING_FAILED, | |
107 GetOpenSSLErrors()); | |
108 } | |
109 | |
110 // Verify that the client auth certificate was signed by a trusted CA. | |
111 if (X509_verify(client_cert.get(), ca_public_key.get()) <= 0) { | |
112 LOG(ERROR) << "Certificate is not issued by a trusted CA."; | |
113 return AuthResult::CreateWithOpenSSLErrors( | |
114 "cert not signed by trusted CA", | |
115 AuthResult::ERROR_CERT_NOT_SIGNED_BY_TRUSTED_CA, | |
116 GetOpenSSLErrors()); | |
117 } | |
118 | |
119 // Get the client auth certificate's public key. | |
120 const crypto::ScopedEVP_PKEY client_public_key( | |
121 X509_get_pubkey(client_cert.get())); | |
122 const int client_public_key_type = EVP_PKEY_id(client_public_key.get()); | |
123 if (client_public_key_type != EVP_PKEY_RSA) { | |
124 LOG(ERROR) << "Expected RSA key type for client certificate, got " | |
125 << client_public_key_type << " instead."; | |
126 return AuthResult::CreateWithOpenSSLErrors( | |
127 "couldn't extract public_key from client cert.", | |
128 AuthResult::ERROR_CANNOT_EXTRACT_PUBLIC_KEY, | |
129 GetOpenSSLErrors()); | |
130 } | |
131 | |
132 // Check that the SSL peer certificate was signed using the client's public | |
133 // key. | |
134 const crypto::ScopedEVP_MD_CTX ctx(EVP_MD_CTX_create()); | |
135 if (!ctx) { | |
davidben
2014/11/01 00:19:35
135 through 150 can be coalesced into one block no
Kevin M
2014/11/03 19:41:55
Done.
| |
136 LOG(ERROR) << "Unable to allocate EVP_MD_CTX."; | |
137 return AuthResult::CreateWithOpenSSLErrors( | |
138 "unable to allocate EVP_MD_CTX.", | |
139 AuthResult::ERROR_UNEXPECTED_AUTH_LIBRARY_RESULT, | |
140 GetOpenSSLErrors()); | |
141 } | |
142 if (!EVP_DigestVerifyInit(ctx.get(), NULL, EVP_sha1(), NULL, | |
143 client_public_key.get())) { | |
144 LOG(ERROR) << "EVP_DigestVerifyInit failed."; | |
145 return AuthResult::CreateWithOpenSSLErrors( | |
146 "EVP_DigestVerifyInit failed.", | |
147 AuthResult::ERROR_UNEXPECTED_AUTH_LIBRARY_RESULT, | |
148 GetOpenSSLErrors()); | |
149 } | |
150 if (!EVP_DigestVerifyUpdate(ctx.get(), peer_cert.data(), peer_cert.size())) { | |
151 LOG(ERROR) << "EVP_DigestVerifyUpdate failed."; | |
152 return AuthResult::CreateWithOpenSSLErrors( | |
153 "EVP_DigestVerifyUpdate failed.", | |
154 AuthResult::ERROR_UNEXPECTED_AUTH_LIBRARY_RESULT, | |
155 GetOpenSSLErrors()); | |
156 } | |
157 const std::string& signature = response.signature(); | |
158 if (!EVP_DigestVerifyFinal( | |
159 ctx.get(), | |
160 reinterpret_cast<uint8_t*>(const_cast<char*>(signature.data())), | |
161 signature.size())) { | |
162 return AuthResult::CreateWithOpenSSLErrors( | |
163 "payload verification failed.", | |
164 AuthResult::ERROR_SIGNED_BLOBS_MISMATCH, | |
165 GetOpenSSLErrors()); | |
166 } | |
167 | |
16 return AuthResult(); | 168 return AuthResult(); |
17 } | 169 } |
18 | 170 |
19 } // namespace cast_channel | 171 } // namespace cast_channel |
20 } // namespace core_api | 172 } // namespace core_api |
21 } // namespace extensions | 173 } // namespace extensions |
OLD | NEW |