1 // Copyright 2016 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "net/ssl/ssl_platform_key_util.h"
6
7 #include <stddef.h>
8
9 #include "base/memory/ref_counted.h"
10 #include "net/cert/x509_certificate.h"
11 #include "net/ssl/ssl_private_key.h"
12 #include "net/test/cert_test_util.h"
13 #include "net/test/test_data_directory.h"
14 #include "testing/gtest/include/gtest/gtest.h"
15 #include "third_party/boringssl/src/include/openssl/ecdsa.h"
16 #include "third_party/boringssl/src/include/openssl/evp.h"
17
18 namespace net {
19
20 namespace {
21
GetClientCertInfoFromFile(const char * filename,int * out_type,size_t * out_max_length)22 bool GetClientCertInfoFromFile(const char* filename,
23 int* out_type,
24 size_t* out_max_length) {
25 scoped_refptr<X509Certificate> cert =
26 ImportCertFromFile(GetTestCertsDirectory(), filename);
27 if (!cert) {
28 ADD_FAILURE() << "Could not read " << filename;
29 return false;
30 }
31
32 return GetClientCertInfo(cert.get(), out_type, out_max_length);
33 }
34
BitsToBytes(size_t bits)35 size_t BitsToBytes(size_t bits) {
36 return (bits + 7) / 8;
37 }
38
39 } // namespace
40
TEST(SSLPlatformKeyUtil,GetClientCertInfo)41 TEST(SSLPlatformKeyUtil, GetClientCertInfo) {
42 int type;
43 size_t max_length;
44
45 ASSERT_TRUE(GetClientCertInfoFromFile("client_1.pem", &type, &max_length));
46 EXPECT_EQ(EVP_PKEY_RSA, type);
47 EXPECT_EQ(2048u / 8u, max_length);
48
49 ASSERT_TRUE(GetClientCertInfoFromFile("client_4.pem", &type, &max_length));
50 EXPECT_EQ(EVP_PKEY_EC, type);
51 EXPECT_EQ(ECDSA_SIG_max_len(BitsToBytes(256)), max_length);
52
53 ASSERT_TRUE(GetClientCertInfoFromFile("client_5.pem", &type, &max_length));
54 EXPECT_EQ(EVP_PKEY_EC, type);
55 EXPECT_EQ(ECDSA_SIG_max_len(BitsToBytes(384)), max_length);
56
57 ASSERT_TRUE(GetClientCertInfoFromFile("client_6.pem", &type, &max_length));
58 EXPECT_EQ(EVP_PKEY_EC, type);
59 EXPECT_EQ(ECDSA_SIG_max_len(BitsToBytes(521)), max_length);
60 }
61
62 } // namespace net
63