Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(177)

Side by Side Diff: components/cronet/android/test/mock_cert_verifier.cc

Issue 1407263010: [Cronet] Public key pinning for Java API (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Small javadoc fix Created 5 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 static bool calculatePublicKeySha256(const net::X509Certificate& cert,
25 net::HashValue* outHashValue);
mef 2015/11/02 17:56:56 no camel case for local variables. https://google.
kapishnikov 2015/11/02 22:45:49 Done.
26
23 static jlong CreateMockCertVerifier(JNIEnv* env, 27 static jlong CreateMockCertVerifier(JNIEnv* env,
24 const JavaParamRef<jclass>& jcaller, 28 const JavaParamRef<jclass>& jcaller,
25 const JavaParamRef<jobjectArray>& jcerts) { 29 const JavaParamRef<jobjectArray>& jcerts) {
26 std::vector<std::string> certs; 30 std::vector<std::string> certs;
27 base::android::AppendJavaStringArrayToStringVector(env, jcerts, &certs); 31 base::android::AppendJavaStringArrayToStringVector(env, jcerts, &certs);
28 net::MockCertVerifier* mock_cert_verifier = new net::MockCertVerifier(); 32 net::MockCertVerifier* mock_cert_verifier = new net::MockCertVerifier();
29 for (auto cert : certs) { 33 for (auto cert : certs) {
30 net::CertVerifyResult verify_result; 34 net::CertVerifyResult verify_result;
31 verify_result.verified_cert = 35 verify_result.verified_cert =
32 net::ImportCertFromFile(net::GetTestCertsDirectory(), cert); 36 net::ImportCertFromFile(net::GetTestCertsDirectory(), cert);
37
38 // Let the cert be treated as a known root cert.
39 // This will enable HPKP verification.
40 verify_result.is_issued_by_known_root = true;
41
42 // Calculate the public key hash and add it to the verify_result
43 net::HashValue hashValue;
44 if (calculatePublicKeySha256(*verify_result.verified_cert.get(),
45 &hashValue)) {
46 verify_result.public_key_hashes.push_back(hashValue);
47 }
48
33 mock_cert_verifier->AddResultForCert(verify_result.verified_cert.get(), 49 mock_cert_verifier->AddResultForCert(verify_result.verified_cert.get(),
34 verify_result, net::OK); 50 verify_result, net::OK);
35 } 51 }
36 52
37 return reinterpret_cast<jlong>(mock_cert_verifier); 53 return reinterpret_cast<jlong>(mock_cert_verifier);
38 } 54 }
39 55
56 // Populates outHashValue with the SHA256 hash of the cert public key.
57 // Returns true on success.
58 static bool calculatePublicKeySha256(const net::X509Certificate& cert,
mef 2015/11/02 17:56:56 move this to the top of the file into anonymous na
kapishnikov 2015/11/02 22:45:49 Done.
59 net::HashValue* outHashValue) {
60 // Convert the cert to DER encoded bytes
61 std::string der_cert_bytes;
62 net::X509Certificate::OSCertHandle cert_handle = cert.os_cert_handle();
63 if (!net::X509Certificate::GetDEREncoded(cert_handle, &der_cert_bytes)) {
64 LOG(INFO) << "Unable to convert the given cert to DER encoding";
65 return false;
66 }
67 // Extract the public key from the cert
68 base::StringPiece spki_bytes;
69 if (!net::asn1::ExtractSPKIFromDERCert(der_cert_bytes, &spki_bytes)) {
70 LOG(INFO) << "Unable to retrieve the public key from the DER cert";
71 return false;
72 }
73 // Convert the public key bytes to SHA256 hash
74 outHashValue->tag = net::HASH_VALUE_SHA256;
75 crypto::SHA256HashString(spki_bytes, outHashValue->data(),
76 crypto::kSHA256Length);
77 return true;
78 }
79
40 bool RegisterMockCertVerifier(JNIEnv* env) { 80 bool RegisterMockCertVerifier(JNIEnv* env) {
41 return RegisterNativesImpl(env); 81 return RegisterNativesImpl(env);
42 } 82 }
43 83
44 } // namespace cronet 84 } // namespace cronet
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698