Chromium Code Reviews| 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/evp.h> | |
| 8 #include <openssl/rsa.h> | |
| 9 #include <openssl/x509.h> | |
| 10 #include <stddef.h> | |
| 11 | |
| 7 #include "base/logging.h" | 12 #include "base/logging.h" |
| 13 #include "crypto/scoped_openssl_types.h" | |
| 14 #include "extensions/browser/api/cast_channel/cast_auth_ica.h" | |
| 15 #include "extensions/browser/api/cast_channel/cast_message_util.h" | |
| 16 #include "extensions/common/api/cast_channel/cast_channel.pb.h" | |
| 17 #include "net/cert/x509_certificate.h" | |
| 8 | 18 |
| 9 namespace extensions { | 19 namespace extensions { |
| 10 namespace core_api { | 20 namespace core_api { |
| 11 namespace cast_channel { | 21 namespace cast_channel { |
| 22 namespace { | |
| 23 | |
| 24 typedef crypto::ScopedOpenSSL<X509, X509_free>::Type ScopedX509; | |
| 25 | |
| 26 } // namespace | |
| 27 | |
| 28 // This function does the following | |
| 29 // * Verifies that |auth_message.response.client_auth_certificate| is signed | |
|
davidben
2014/10/30 18:37:23
Nit: s/auth_message.//
Kevin M
2014/10/31 21:39:37
Done.
| |
| 30 // by the trusted CA public key passed as |ca_public_key_bytes|. | |
|
davidben
2014/10/30 18:37:23
Nit: ca_public_key_bytes is no longer a parameter.
Kevin M
2014/10/31 21:39:37
Done.
| |
| 31 // * Verifies that |auth_message.response.signature| matches the signature | |
| 32 // of |peer_cert| by |auth_message.response.client_auth_certificate|'s public | |
| 33 // key. | |
| 34 AuthResult VerifyCredentials(const AuthResponse& response, | |
| 35 const std::string& peer_cert) { | |
|
davidben
2014/10/30 18:37:23
This should include crypto/openssl_util.h and add
Kevin M
2014/10/31 21:39:37
That's neat, Done.
| |
| 36 // Get the public key of the ICA that was used to sign the client's cert. | |
| 37 base::StringPiece ca_public_key_bytes; | |
| 38 if (response.intermediate_certificate().size() <= 0) { | |
| 39 ca_public_key_bytes = GetDefaultTrustedICAPublicKey(); | |
| 40 } else { | |
| 41 ca_public_key_bytes = | |
| 42 GetTrustedICAPublicKey(response.intermediate_certificate(0)); | |
| 43 if (ca_public_key_bytes.empty()) { | |
| 44 LOG(ERROR) << "Couldn't find trusted ICA."; | |
| 45 return AuthResult::Create( | |
|
mark a. foltz
2014/10/31 06:49:11
The AuthResult API has evolved with vadimgo's patc
Kevin M
2014/10/31 21:39:37
Done.
| |
| 46 "failed to verify credentials: cert not signed by trusted CA", | |
| 47 AuthResult::ERROR_NSS_CERT_NOT_SIGNED_BY_TRUSTED_CA); | |
| 48 } | |
| 49 } | |
| 50 | |
| 51 // Parse the CA public key. | |
| 52 const uint8_t* ca_ptr = | |
| 53 reinterpret_cast<const uint8_t*>(ca_public_key_bytes.data()); | |
| 54 const uint8_t* ca_public_key_end = ca_ptr + ca_public_key_bytes.size(); | |
|
mark a. foltz
2014/10/31 06:49:11
Why are you calling size() here and length() below
mark a. foltz
2014/10/31 06:49:11
What is ca_public_key_end pointing to? It seems t
Kevin M
2014/10/31 21:39:37
It's the expected value of cert_ptr after d2i_X509
Kevin M
2014/10/31 21:39:37
Good catch, done
Kevin M
2014/10/31 21:39:37
Done.
| |
| 55 crypto::ScopedRSA ca_public_key_rsa( | |
| 56 d2i_RSAPublicKey(NULL, &ca_ptr, ca_public_key_bytes.length())); | |
| 57 if (!ca_public_key_rsa || ca_ptr != ca_public_key_end) { | |
| 58 LOG(ERROR) << "Failed to import trusted public key."; | |
| 59 return AuthResult::Create( | |
| 60 "failed to import trusted public key.", | |
| 61 AuthResult::ERROR_NSS_CERT_PARSING_FAILED); | |
| 62 } | |
| 63 crypto::ScopedEVP_PKEY ca_public_key(EVP_PKEY_new()); | |
| 64 if (!ca_public_key || | |
|
mark a. foltz
2014/10/31 06:49:11
What type is ca_public_key - I assume it has a !op
Kevin M
2014/10/31 21:39:37
It's a scoped_ptr. I can't find an operator! defin
| |
| 65 !EVP_PKEY_set1_RSA(ca_public_key.get(), ca_public_key_rsa.get())) { | |
| 66 LOG(ERROR) << "Failed to initialize EVP_PKEY"; | |
| 67 return AuthResult::Create( | |
| 68 "failed to initialize EVP_PKEY.", | |
| 69 AuthResult::ERROR_NSS_CANNOT_EXTRACT_PUBLIC_KEY); | |
| 70 } | |
| 71 | |
| 72 // Parse the client auth certificate. | |
| 73 const uint8_t* client_cert_ptr = reinterpret_cast<const uint8_t*>( | |
| 74 response.client_auth_certificate().data()); | |
| 75 const uint8_t* client_cert_end = | |
|
mark a. foltz
2014/10/31 06:49:11
Similar questions about the use of client_cert_end
Kevin M
2014/10/31 21:39:37
Acknowledged.
| |
| 76 client_cert_ptr + | |
| 77 response.client_auth_certificate().size(); | |
| 78 const ScopedX509 client_cert( | |
| 79 d2i_X509(NULL, &client_cert_ptr, | |
| 80 response.client_auth_certificate().size())); | |
| 81 if (!client_cert || client_cert_ptr != client_cert_end) { | |
| 82 LOG(ERROR) << "Failed to parse certificate."; | |
| 83 return AuthResult::Create( | |
| 84 "failed to parse client_auth_certificate.", | |
| 85 AuthResult::ERROR_NSS_CERT_PARSING_FAILED); | |
| 86 } | |
| 87 | |
| 88 // Verify that the client auth certificate was signed by a trusted CA. | |
| 89 if (X509_verify(client_cert.get(), ca_public_key.get()) <= 0) { | |
|
mark a. foltz
2014/10/31 06:49:11
Does X509_verify return specific error codes? If
Kevin M
2014/10/31 21:39:37
From what I could tell, it's one or zero "except i
davidben
2014/11/01 00:19:34
For stuff in crypto/, whether it's 0 or -1 is almo
| |
| 90 LOG(ERROR) << "Certificate is not issued by a trusted CA."; | |
| 91 return AuthResult::Create( | |
| 92 "cert not signed by trusted CA", | |
| 93 AuthResult::ERROR_NSS_CERT_NOT_SIGNED_BY_TRUSTED_CA); | |
| 94 } | |
| 95 | |
| 96 // Get the client auth certificate's public key. | |
| 97 const crypto::ScopedEVP_PKEY client_public_key( | |
| 98 X509_get_pubkey(client_cert.get())); | |
| 99 const int client_public_key_type = EVP_PKEY_id(client_public_key.get()); | |
| 100 if (client_public_key_type != EVP_PKEY_RSA) { | |
| 101 LOG(ERROR) << "Expected RSA key type for client certificate, got " | |
| 102 << client_public_key_type << " instead."; | |
| 103 return AuthResult::Create( | |
| 104 "couldn't extract public_key from client cert.", | |
| 105 AuthResult::ERROR_NSS_CANNOT_EXTRACT_PUBLIC_KEY); | |
|
mark a. foltz
2014/10/31 06:49:11
Is there an OpenSSL equivalent to PORT_GetError()
| |
| 106 } | |
| 107 | |
| 108 // Check that the SSL peer certificate was signed using the client's public | |
| 109 // key. | |
| 110 const crypto::ScopedEVP_MD_CTX ctx(EVP_MD_CTX_create()); | |
| 111 if (!ctx) { | |
| 112 LOG(ERROR) << "Unable to allocate EVP_MD_CTX."; | |
| 113 return AuthResult::Create( | |
|
davidben
2014/10/30 18:37:23
You could probably just chain these few together w
Kevin M
2014/10/31 21:39:37
Having conditional blocks for each step of the way
| |
| 114 "unable to allocate EVP_MD_CTX.", | |
| 115 AuthResult::ERROR_UNEXPECTED_AUTH_LIBRARY_RESULT); | |
| 116 } | |
| 117 if (!EVP_DigestVerifyInit(ctx.get(), NULL, EVP_sha1(), NULL, | |
| 118 client_public_key.get())) { | |
| 119 LOG(ERROR) << "EVP_DigestVerifyInit failed."; | |
| 120 return AuthResult::Create( | |
| 121 "EVP_DigestVerifyInit failed.", | |
| 122 AuthResult::ERROR_UNEXPECTED_AUTH_LIBRARY_RESULT); | |
| 123 } | |
| 124 if (!EVP_DigestVerifyUpdate(ctx.get(), peer_cert.data(), peer_cert.size())) { | |
| 125 LOG(ERROR) << "EVP_DigestVerifyUpdate failed."; | |
| 126 return AuthResult::Create( | |
| 127 "EVP_DigestVerifyUpdate failed.", | |
| 128 AuthResult::ERROR_UNEXPECTED_AUTH_LIBRARY_RESULT); | |
| 129 } | |
| 130 const std::string& signature = response.signature(); | |
| 131 int result = EVP_DigestVerifyFinal( | |
|
davidben
2014/10/30 18:37:22
Actually, as of https://codereview.chromium.org/66
Kevin M
2014/10/31 21:39:38
Done.
davidben
2014/11/01 00:19:34
Oops. It occurred to me that we probably want to l
Kevin M
2014/11/03 18:31:46
Done.
| |
| 132 ctx.get(), | |
| 133 reinterpret_cast<uint8_t*>(const_cast<char*>(signature.data())), | |
| 134 signature.size()); | |
| 135 if (result <= 0) { | |
| 136 return AuthResult::Create( | |
| 137 "payload verification failed.", | |
| 138 AuthResult::ERROR_NSS_SIGNED_BLOBS_MISMATCH); | |
| 139 } | |
| 140 return AuthResult(); | |
| 141 } | |
| 12 | 142 |
| 13 AuthResult AuthenticateChallengeReply(const CastMessage& challenge_reply, | 143 AuthResult AuthenticateChallengeReply(const CastMessage& challenge_reply, |
|
mark a. foltz
2014/10/31 06:49:11
This appears to be identical to the same method in
Kevin M
2014/10/31 21:39:37
Done.
| |
| 14 const std::string& peer_cert) { | 144 const std::string& peer_cert) { |
| 15 NOTREACHED(); | 145 if (peer_cert.empty()) { |
| 146 AuthResult result = AuthResult::Create("Peer cert was empty.", | |
| 147 AuthResult::ERROR_PEER_CERT_EMPTY); | |
| 148 VLOG(1) << result.error_message; | |
| 149 return result; | |
| 150 } | |
| 151 | |
| 152 VLOG(1) << "Challenge reply: " << CastMessageToString(challenge_reply); | |
| 153 DeviceAuthMessage auth_message; | |
| 154 AuthResult result = ParseAuthMessage(challenge_reply, &auth_message); | |
| 155 if (!result.success()) { | |
| 156 VLOG(1) << result.error_message; | |
| 157 return result; | |
| 158 } | |
| 159 | |
| 160 const AuthResponse& response = auth_message.response(); | |
| 161 result = VerifyCredentials(response, peer_cert); | |
| 162 if (!result.success()) { | |
| 163 VLOG(1) << result.error_message | |
| 164 << ", error type: " << result.error_type; | |
| 165 return result; | |
| 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 |