ssl_platform_key_util_unittest.cc 1.8 KB

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