OLD | NEW |
---|---|
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "mock_cert_verifier.h" | 5 #include "mock_cert_verifier.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/android/jni_android.h" | 10 #include "base/android/jni_android.h" |
11 #include "base/android/jni_array.h" | 11 #include "base/android/jni_array.h" |
12 #include "base/android/scoped_java_ref.h" | 12 #include "crypto/sha2.h" |
13 #include "jni/MockCertVerifier_jni.h" | 13 #include "jni/MockCertVerifier_jni.h" |
14 #include "net/base/net_errors.h" | 14 #include "net/base/net_errors.h" |
15 #include "net/base/test_data_directory.h" | 15 #include "net/base/test_data_directory.h" |
16 #include "net/cert/asn1_util.h" | |
16 #include "net/cert/cert_verifier.h" | 17 #include "net/cert/cert_verifier.h" |
17 #include "net/cert/cert_verify_result.h" | 18 #include "net/cert/cert_verify_result.h" |
18 #include "net/cert/mock_cert_verifier.h" | 19 #include "net/cert/mock_cert_verifier.h" |
19 #include "net/test/cert_test_util.h" | 20 #include "net/test/cert_test_util.h" |
20 | 21 |
21 namespace cronet { | 22 namespace cronet { |
22 | 23 |
24 namespace { | |
25 | |
26 // Populates out_hash_value with the SHA256 hash of the cert public key. | |
mef
2015/11/06 18:02:17
out_hash_value -> |out_hash_value|
kapishnikov
2015/11/06 19:45:09
Done.
| |
27 // Returns true on success. | |
28 static bool calculatePublicKeySha256(const net::X509Certificate& cert, | |
29 net::HashValue* out_hash_value) { | |
30 // Convert the cert to DER encoded bytes. | |
31 std::string der_cert_bytes; | |
32 net::X509Certificate::OSCertHandle cert_handle = cert.os_cert_handle(); | |
33 if (!net::X509Certificate::GetDEREncoded(cert_handle, &der_cert_bytes)) { | |
34 LOG(INFO) << "Unable to convert the given cert to DER encoding"; | |
35 return false; | |
36 } | |
37 // Extract the public key from the cert. | |
38 base::StringPiece spki_bytes; | |
39 if (!net::asn1::ExtractSPKIFromDERCert(der_cert_bytes, &spki_bytes)) { | |
40 LOG(INFO) << "Unable to retrieve the public key from the DER cert"; | |
41 return false; | |
42 } | |
43 // Convert the public key bytes to SHA256 hash. | |
44 out_hash_value->tag = net::HASH_VALUE_SHA256; | |
45 crypto::SHA256HashString(spki_bytes, out_hash_value->data(), | |
46 crypto::kSHA256Length); | |
47 return true; | |
48 } | |
49 | |
50 } // namespace | |
51 | |
23 static jlong CreateMockCertVerifier(JNIEnv* env, | 52 static jlong CreateMockCertVerifier(JNIEnv* env, |
24 const JavaParamRef<jclass>& jcaller, | 53 const JavaParamRef<jclass>& jcaller, |
25 const JavaParamRef<jobjectArray>& jcerts) { | 54 const JavaParamRef<jobjectArray>& jcerts) { |
26 std::vector<std::string> certs; | 55 std::vector<std::string> certs; |
27 base::android::AppendJavaStringArrayToStringVector(env, jcerts, &certs); | 56 base::android::AppendJavaStringArrayToStringVector(env, jcerts, &certs); |
28 net::MockCertVerifier* mock_cert_verifier = new net::MockCertVerifier(); | 57 net::MockCertVerifier* mock_cert_verifier = new net::MockCertVerifier(); |
29 for (auto cert : certs) { | 58 for (auto cert : certs) { |
30 net::CertVerifyResult verify_result; | 59 net::CertVerifyResult verify_result; |
31 verify_result.verified_cert = | 60 verify_result.verified_cert = |
32 net::ImportCertFromFile(net::GetTestCertsDirectory(), cert); | 61 net::ImportCertFromFile(net::GetTestCertsDirectory(), cert); |
62 | |
63 // Let the cert be treated as a known root cert. | |
64 // This will enable HPKP verification. | |
65 verify_result.is_issued_by_known_root = true; | |
66 | |
67 // Calculate the public key hash and add it to the verify_result | |
mef
2015/11/06 18:02:17
given that this is test code I think we expect it
mef
2015/11/06 18:02:17
nit: period at the end.
kapishnikov
2015/11/06 19:45:09
Done.
kapishnikov
2015/11/06 19:45:09
Done.
| |
68 net::HashValue hashValue; | |
69 if (calculatePublicKeySha256(*verify_result.verified_cert.get(), | |
70 &hashValue)) { | |
71 verify_result.public_key_hashes.push_back(hashValue); | |
72 } | |
73 | |
33 mock_cert_verifier->AddResultForCert(verify_result.verified_cert.get(), | 74 mock_cert_verifier->AddResultForCert(verify_result.verified_cert.get(), |
34 verify_result, net::OK); | 75 verify_result, net::OK); |
35 } | 76 } |
36 | 77 |
37 return reinterpret_cast<jlong>(mock_cert_verifier); | 78 return reinterpret_cast<jlong>(mock_cert_verifier); |
38 } | 79 } |
39 | 80 |
40 bool RegisterMockCertVerifier(JNIEnv* env) { | 81 bool RegisterMockCertVerifier(JNIEnv* env) { |
41 return RegisterNativesImpl(env); | 82 return RegisterNativesImpl(env); |
42 } | 83 } |
43 | 84 |
44 } // namespace cronet | 85 } // namespace cronet |
OLD | NEW |