Index: net/cert/x509_util_nss_unittest.cc |
diff --git a/net/cert/x509_util_nss_unittest.cc b/net/cert/x509_util_nss_unittest.cc |
index 968cc147ec505ecd3d7ef408bb7854144a93c044..85452ad565b30f10e33686c3264cbf4efc598ba3 100644 |
--- a/net/cert/x509_util_nss_unittest.cc |
+++ b/net/cert/x509_util_nss_unittest.cc |
@@ -8,8 +8,10 @@ |
#include <cert.h> |
#include <secoid.h> |
+#include "base/base64.h" |
#include "base/memory/ref_counted.h" |
#include "base/memory/scoped_ptr.h" |
+#include "base/values.h" |
#include "crypto/ec_private_key.h" |
#include "crypto/scoped_nss_types.h" |
#include "crypto/signature_verifier.h" |
@@ -168,4 +170,107 @@ TEST(X509UtilNSSTest, CreateDomainBoundCertEC) { |
#endif |
} |
+static const unsigned char kP256SpkiPrefix[] = { |
+ 0x30, 0x59, 0x30, 0x13, 0x06, 0x07, 0x2a, 0x86, |
+ 0x48, 0xce, 0x3d, 0x02, 0x01, 0x06, 0x08, 0x2a, |
+ 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07, 0x03, |
+ 0x42, 0x00, 0x04 |
+}; |
+static const unsigned int kEcPointSize = 32U; |
+ |
+static const unsigned char spki_ec[] = { |
Ryan Sleevi
2013/08/02 23:08:43
kSpkiEc, as per http://google-styleguide.googlecod
|
+ 0x30, 0x59, 0x30, 0x13, 0x06, 0x07, 0x2a, 0x86, |
+ 0x48, 0xce, 0x3d, 0x02, 0x01, 0x06, 0x08, 0x2a, |
+ 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07, 0x03, |
+ 0x42, 0x00, 0x04, |
+ 0x29, 0x5d, 0x6e, 0xfe, 0x33, 0x77, 0x26, 0xea, |
+ 0x5b, 0xa4, 0xe6, 0x1b, 0x34, 0x6e, 0x7b, 0xa0, |
+ 0xa3, 0x8f, 0x33, 0x49, 0xa0, 0x9c, 0xae, 0x98, |
+ 0xbd, 0x46, 0x0d, 0xf6, 0xd4, 0x5a, 0xdc, 0x8a, |
+ 0x1f, 0x8a, 0xb2, 0x20, 0x51, 0xb7, 0xd2, 0x87, |
+ 0x0d, 0x53, 0x7e, 0x5d, 0x94, 0xa3, 0xe0, 0x34, |
+ 0x16, 0xa1, 0xcc, 0x10, 0x48, 0xcd, 0x70, 0x9c, |
+ 0x05, 0xd3, 0xd2, 0xca, 0xdf, 0x44, 0x2f, 0xf4 |
+}; |
+ |
+static const unsigned char spki_ec_with_zero_x_y[] = { |
+ 0x30, 0x59, 0x30, 0x13, 0x06, 0x07, 0x2a, 0x86, |
+ 0x48, 0xce, 0x3d, 0x02, 0x01, 0x06, 0x08, 0x2a, |
+ 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07, 0x03, |
+ 0x42, 0x00, 0x04, |
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 |
+}; |
+ |
+TEST(X509UtilNSSTest, ConvertSPKIFromDERToJwkEC) { |
+ base::StringPiece spki; |
+ base::DictionaryValue public_key_jwk; |
+ |
+ EXPECT_FALSE(x509_util::ConvertSPKIFromDERToJWK( |
+ spki, |
+ &public_key_jwk)); |
+ EXPECT_TRUE(public_key_jwk.empty()); |
+ |
+ // Test the result of a "normal" point on this curve. |
+ spki.set(reinterpret_cast<const char*>(spki_ec), sizeof(spki_ec)); |
+ EXPECT_TRUE(x509_util::ConvertSPKIFromDERToJWK( |
+ spki, |
+ &public_key_jwk)); |
+ |
+ std::string string_value; |
+ EXPECT_TRUE(public_key_jwk.GetString("alg", &string_value)); |
+ EXPECT_EQ(std::string("EC"), string_value); |
+ EXPECT_TRUE(public_key_jwk.GetString("crv", &string_value)); |
+ EXPECT_EQ(std::string("P-256"), string_value); |
Ryan Sleevi
2013/08/02 23:08:43
eliminate all the extra std::string() stuff here.
|
+ |
+ EXPECT_TRUE(public_key_jwk.GetString("x", &string_value)); |
+ std::string decoded_coordinate; |
+ EXPECT_TRUE(base::Base64Decode(string_value, &decoded_coordinate)); |
+ EXPECT_EQ(kEcPointSize, decoded_coordinate.size()); |
+ EXPECT_EQ(0, memcmp(decoded_coordinate.data(), |
+ spki_ec + sizeof(kP256SpkiPrefix), |
+ kEcPointSize)); |
+ |
+ EXPECT_TRUE(public_key_jwk.GetString("y", &string_value)); |
+ EXPECT_TRUE(base::Base64Decode(string_value, &decoded_coordinate)); |
+ EXPECT_EQ(kEcPointSize, decoded_coordinate.size()); |
+ EXPECT_EQ(0, memcmp(decoded_coordinate.data(), |
+ spki_ec + sizeof(kP256SpkiPrefix) + kEcPointSize, |
+ kEcPointSize)); |
+ |
+ // Test the result of a corner case: leading 0s in the x, y coordinates are |
+ // not trimmed, but the point is fixed-length encoded. |
+ spki.set(reinterpret_cast<const char*>(spki_ec_with_zero_x_y), |
+ sizeof(spki_ec_with_zero_x_y)); |
+ EXPECT_TRUE(x509_util::ConvertSPKIFromDERToJWK( |
+ spki, |
+ &public_key_jwk)); |
+ |
+ EXPECT_TRUE(public_key_jwk.GetString("alg", &string_value)); |
+ EXPECT_EQ(std::string("EC"), string_value); |
+ EXPECT_TRUE(public_key_jwk.GetString("crv", &string_value)); |
+ EXPECT_EQ(std::string("P-256"), string_value); |
+ |
+ EXPECT_TRUE(public_key_jwk.GetString("x", &string_value)); |
+ EXPECT_TRUE(base::Base64Decode(string_value, &decoded_coordinate)); |
+ EXPECT_EQ(kEcPointSize, decoded_coordinate.size()); |
+ EXPECT_EQ(0, memcmp(decoded_coordinate.data(), |
+ spki_ec_with_zero_x_y + sizeof(kP256SpkiPrefix), |
+ kEcPointSize)); |
+ |
+ EXPECT_TRUE(public_key_jwk.GetString("y", &string_value)); |
+ EXPECT_TRUE(base::Base64Decode(string_value, &decoded_coordinate)); |
+ EXPECT_EQ(kEcPointSize, decoded_coordinate.size()); |
+ EXPECT_EQ(0, |
+ memcmp(decoded_coordinate.data(), |
+ spki_ec_with_zero_x_y + sizeof(kP256SpkiPrefix) + kEcPointSize, |
+ kEcPointSize)); |
Ryan Sleevi
2013/08/02 23:08:43
Please consider running clang-format on this test.
|
+} |
+ |
} // namespace net |