diff options
Diffstat (limited to 'security/nss/gtests/der_gtest')
-rw-r--r-- | security/nss/gtests/der_gtest/Makefile | 43 | ||||
-rw-r--r-- | security/nss/gtests/der_gtest/der_getint_unittest.cc | 122 | ||||
-rw-r--r-- | security/nss/gtests/der_gtest/der_gtest.gyp | 33 | ||||
-rw-r--r-- | security/nss/gtests/der_gtest/der_private_key_import_unittest.cc | 110 | ||||
-rw-r--r-- | security/nss/gtests/der_gtest/manifest.mn | 22 |
5 files changed, 330 insertions, 0 deletions
diff --git a/security/nss/gtests/der_gtest/Makefile b/security/nss/gtests/der_gtest/Makefile new file mode 100644 index 000000000..0d547e080 --- /dev/null +++ b/security/nss/gtests/der_gtest/Makefile @@ -0,0 +1,43 @@ +#! gmake +# +# This Source Code Form is subject to the terms of the Mozilla Public +# License, v. 2.0. If a copy of the MPL was not distributed with this +# file, You can obtain one at http://mozilla.org/MPL/2.0/. + +####################################################################### +# (1) Include initial platform-independent assignments (MANDATORY). # +####################################################################### + +include manifest.mn + +####################################################################### +# (2) Include "global" configuration information. (OPTIONAL) # +####################################################################### + +include $(CORE_DEPTH)/coreconf/config.mk + +####################################################################### +# (3) Include "component" configuration information. (OPTIONAL) # +####################################################################### + + +####################################################################### +# (4) Include "local" platform-dependent assignments (OPTIONAL). # +####################################################################### + +include ../common/gtest.mk + +####################################################################### +# (5) Execute "global" rules. (OPTIONAL) # +####################################################################### + +include $(CORE_DEPTH)/coreconf/rules.mk + +####################################################################### +# (6) Execute "component" rules. (OPTIONAL) # +####################################################################### + + +####################################################################### +# (7) Execute "local" rules. (OPTIONAL). # +####################################################################### diff --git a/security/nss/gtests/der_gtest/der_getint_unittest.cc b/security/nss/gtests/der_gtest/der_getint_unittest.cc new file mode 100644 index 000000000..e4b225e5c --- /dev/null +++ b/security/nss/gtests/der_gtest/der_getint_unittest.cc @@ -0,0 +1,122 @@ +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim: set ts=2 et sw=2 tw=80: */ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this file, + * You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#include <climits> +#include <memory> +#include "nss.h" +#include "pk11pub.h" +#include "secutil.h" + +#include "gtest/gtest.h" +#include "scoped_ptrs.h" + +namespace nss_test { + +class DERIntegerDecodingTest : public ::testing::Test { + public: + void TestGetInteger(long number, unsigned char *der_number, + unsigned int len) { + SECItem input = {siBuffer, der_number, len}; + EXPECT_EQ(number, DER_GetInteger(&input)); + } + + void GetDerLongMax(unsigned char *der_number, unsigned int len) { + der_number[0] = 0x7F; + for (unsigned int i = 1; i < len; ++i) { + der_number[i] = 0xFF; + } + } + + void GetDerLongMin(unsigned char *der_number, unsigned int len) { + der_number[0] = 0x80; + for (unsigned int i = 1; i < len; ++i) { + der_number[i] = 0x00; + } + } +}; + +TEST_F(DERIntegerDecodingTest, DecodeLongMinus126) { + unsigned char der[] = {0x82}; + TestGetInteger(-126, der, sizeof(der)); +} + +TEST_F(DERIntegerDecodingTest, DecodeLong130) { + unsigned char der[] = {0x00, 0x82}; + TestGetInteger(130, der, sizeof(der)); +} + +TEST_F(DERIntegerDecodingTest, DecodeLong130Padded) { + unsigned char der[sizeof(long) * 2] = {0}; + der[sizeof(der) - 1] = {0x82}; + TestGetInteger(130, der, sizeof(der)); +} + +TEST_F(DERIntegerDecodingTest, DecodeLong0) { + unsigned char der[] = {0x00}; + TestGetInteger(0, der, sizeof(der)); +} + +TEST_F(DERIntegerDecodingTest, DecodeLong1) { + unsigned char der[] = {0x01}; + TestGetInteger(1, der, sizeof(der)); +} + +TEST_F(DERIntegerDecodingTest, DecodeLongMinus1) { + unsigned char der[] = {0xFF}; + TestGetInteger(-1, der, sizeof(der)); +} + +TEST_F(DERIntegerDecodingTest, DecodeLongMinus1Padded) { + unsigned char der[sizeof(long) * 2]; + memset(der, 0xFF, sizeof(der)); + TestGetInteger(-1, der, sizeof(der)); +} + +TEST_F(DERIntegerDecodingTest, DecodeLongMax) { + unsigned char der[sizeof(long)]; + GetDerLongMax(der, sizeof(long)); + TestGetInteger(LONG_MAX, der, sizeof(der)); +} + +TEST_F(DERIntegerDecodingTest, DecodeLongMin) { + unsigned char der[sizeof(long)]; + GetDerLongMin(der, sizeof(long)); + TestGetInteger(LONG_MIN, der, sizeof(der)); +} + +TEST_F(DERIntegerDecodingTest, DecodeLongMaxMinus1) { + unsigned char der[sizeof(long)]; + GetDerLongMax(der, sizeof(long)); + der[sizeof(long) - 1] = 0xFE; + TestGetInteger(LONG_MAX - 1, der, sizeof(der)); +} + +TEST_F(DERIntegerDecodingTest, DecodeLongMinPlus1) { + unsigned char der[sizeof(long)]; + GetDerLongMin(der, sizeof(long)); + der[sizeof(long) - 1] = 0x01; + TestGetInteger(LONG_MIN + 1, der, sizeof(der)); +} + +TEST_F(DERIntegerDecodingTest, DecodeLongMinMinus1) { + unsigned char der[sizeof(long) + 1]; + GetDerLongMax(der, sizeof(long) + 1); + der[0] = 0xFF; + der[1] = 0x7F; + TestGetInteger(LONG_MIN, der, sizeof(der)); + EXPECT_EQ(SEC_ERROR_BAD_DER, PORT_GetError()); +} + +TEST_F(DERIntegerDecodingTest, DecodeLongMaxPlus1) { + unsigned char der[sizeof(long) + 1]; + GetDerLongMin(der, sizeof(long) + 1); + der[0] = 0x00; + der[1] = 0x80; + TestGetInteger(LONG_MAX, der, sizeof(der)); + EXPECT_EQ(SEC_ERROR_BAD_DER, PORT_GetError()); +} + +} // namespace nss_test diff --git a/security/nss/gtests/der_gtest/der_gtest.gyp b/security/nss/gtests/der_gtest/der_gtest.gyp new file mode 100644 index 000000000..95e14e444 --- /dev/null +++ b/security/nss/gtests/der_gtest/der_gtest.gyp @@ -0,0 +1,33 @@ +# This Source Code Form is subject to the terms of the Mozilla Public +# License, v. 2.0. If a copy of the MPL was not distributed with this +# file, You can obtain one at http://mozilla.org/MPL/2.0/. +{ + 'includes': [ + '../../coreconf/config.gypi', + '../common/gtest.gypi', + ], + 'targets': [ + { + 'target_name': 'der_gtest', + 'type': 'executable', + 'sources': [ + 'der_getint_unittest.cc', + 'der_private_key_import_unittest.cc', + '<(DEPTH)/gtests/common/gtests.cc' + ], + 'dependencies': [ + '<(DEPTH)/exports.gyp:nss_exports', + '<(DEPTH)/gtests/google_test/google_test.gyp:gtest', + ] + } + ], + 'target_defaults': { + 'include_dirs': [ + '../../gtests/google_test/gtest/include', + '../../gtests/common' + ] + }, + 'variables': { + 'module': 'nss' + } +} diff --git a/security/nss/gtests/der_gtest/der_private_key_import_unittest.cc b/security/nss/gtests/der_gtest/der_private_key_import_unittest.cc new file mode 100644 index 000000000..836cc7876 --- /dev/null +++ b/security/nss/gtests/der_gtest/der_private_key_import_unittest.cc @@ -0,0 +1,110 @@ +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim: set ts=2 et sw=2 tw=80: */ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this file, + * You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#include <climits> +#include <memory> +#include "nss.h" +#include "pk11pub.h" +#include "secutil.h" + +#include "gtest/gtest.h" +#include "scoped_ptrs.h" + +namespace nss_test { + +const std::vector<uint8_t> kValidRSAKey = { + // 512-bit RSA private key (PKCS#8) + 0x30, 0x82, 0x01, 0x54, 0x02, 0x01, 0x00, 0x30, 0x0d, 0x06, 0x09, 0x2a, + 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01, 0x05, 0x00, 0x04, 0x82, + 0x01, 0x3e, 0x30, 0x82, 0x01, 0x3a, 0x02, 0x01, 0x00, 0x02, 0x41, 0x00, + 0xa2, 0x40, 0xce, 0xb5, 0x4e, 0x70, 0xdc, 0x14, 0x82, 0x5b, 0x58, 0x7d, + 0x2f, 0x5d, 0xfd, 0x46, 0x3c, 0x4b, 0x82, 0x50, 0xb6, 0x96, 0x00, 0x4a, + 0x1a, 0xca, 0xaf, 0xe4, 0x9b, 0xcf, 0x38, 0x4a, 0x46, 0xaa, 0x9f, 0xb4, + 0xd9, 0xc7, 0xee, 0x88, 0xe9, 0xef, 0x0a, 0x31, 0x5f, 0x53, 0x86, 0x8f, + 0x63, 0x68, 0x0b, 0x58, 0x34, 0x72, 0x49, 0xba, 0xed, 0xd9, 0x34, 0x15, + 0x16, 0xc4, 0xca, 0xb7, 0x02, 0x03, 0x01, 0x00, 0x01, 0x02, 0x40, 0x34, + 0xe6, 0xdc, 0x7e, 0xd0, 0xec, 0x8b, 0x55, 0x44, 0x8b, 0x73, 0xf6, 0x9d, + 0x13, 0x10, 0x19, 0x6e, 0x5f, 0x50, 0x45, 0xf0, 0xc2, 0x47, 0xa5, 0xe1, + 0xc6, 0x64, 0x43, 0x2d, 0x6a, 0x0a, 0xf7, 0xe7, 0xda, 0x40, 0xb8, 0x3a, + 0xf0, 0x47, 0xdd, 0x01, 0xf5, 0xe0, 0xa9, 0x0e, 0x47, 0xc2, 0x24, 0xd7, + 0xb5, 0x13, 0x3a, 0x35, 0x4d, 0x11, 0xaa, 0x50, 0x03, 0xb3, 0xe8, 0x54, + 0x6c, 0x99, 0x01, 0x02, 0x21, 0x00, 0xcd, 0xb2, 0xd7, 0xa7, 0x43, 0x5b, + 0xcb, 0x45, 0xe5, 0x0e, 0x86, 0xf6, 0xc1, 0x4e, 0x97, 0xed, 0x78, 0x1f, + 0x09, 0x56, 0xcd, 0x26, 0xe6, 0xf7, 0x5e, 0xd9, 0xfc, 0x88, 0x12, 0x5f, + 0x84, 0x07, 0x02, 0x21, 0x00, 0xc9, 0xee, 0x30, 0xaf, 0x6c, 0xb9, 0x5a, + 0xc9, 0xc1, 0x14, 0x9e, 0xd8, 0x4b, 0x33, 0x38, 0x48, 0x17, 0x41, 0x35, + 0x94, 0x09, 0xf3, 0x69, 0xc4, 0x97, 0xbe, 0x17, 0x7d, 0x95, 0x0f, 0xb7, + 0xd1, 0x02, 0x21, 0x00, 0x8b, 0x0e, 0xf9, 0x8d, 0x61, 0x13, 0x20, 0x63, + 0x9b, 0x0b, 0x6c, 0x20, 0x4a, 0xe4, 0xa7, 0xfe, 0xe8, 0xf3, 0x0a, 0x6c, + 0x3c, 0xfa, 0xac, 0xaf, 0xd4, 0xd6, 0xc7, 0x4a, 0xf2, 0x28, 0xd2, 0x67, + 0x02, 0x20, 0x6b, 0x0e, 0x1d, 0xbf, 0x93, 0x5b, 0xbd, 0x77, 0x43, 0x27, + 0x24, 0x83, 0xb5, 0x72, 0xa5, 0x3f, 0x0b, 0x1d, 0x26, 0x43, 0xa2, 0xf6, + 0xea, 0xb7, 0x30, 0x5f, 0xb6, 0x62, 0x7c, 0xf9, 0x85, 0x51, 0x02, 0x20, + 0x3d, 0x22, 0x63, 0x15, 0x6b, 0x32, 0x41, 0x46, 0x44, 0x78, 0xb7, 0x13, + 0xeb, 0x85, 0x4c, 0x4f, 0x6b, 0x3e, 0xf0, 0x52, 0xf0, 0x46, 0x3b, 0x65, + 0xd8, 0x21, 0x7d, 0xae, 0xc0, 0x09, 0x98, 0x34}; + +const std::vector<uint8_t> kInvalidLengthKey = { + 0x30, 0x1b, // SEQUENCE(len=27) + 0x02, 0x01, 0x00, // INT(len=1) = 0 + 0x30, 0x13, // SEQUENCE(len=19) + 0x06, 0x07, // OID(len=7) + // dhPublicKey (1.2.840.10046.2.1) + 0x2a, 0x86, 0x48, 0xce, 0x3e, 0x02, 0x01, 0x06, 0x08, // OID(len=8) + // prime256v1 (1.2.840.10045.3.1.7) */ + 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07, 0x04, + 0x00 // OCTET STRING(len=0) +}; + +const std::vector<uint8_t> kInvalidZeroLengthKey = { + 0x30, 0x1a, // SEQUENCE(len=26) + 0x02, 0x01, 0x00, // INT(len=1) = 0 + 0x30, 0x13, // SEQUENCE(len=19) + 0x06, 0x07, // OID(len=7) + // dhPublicKey (1.2.840.10046.2.1) + 0x2a, 0x86, 0x48, 0xce, 0x3e, 0x02, 0x01, 0x06, 0x08, // OID(len=8) + // prime256v1 (1.2.840.10045.3.1.7) */ + 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07, 0x04, + 0x00 // OCTET STRING(len=0) +}; + +class DERPrivateKeyImportTest : public ::testing::Test { + public: + bool ParsePrivateKey(const std::vector<uint8_t>& data) { + ScopedPK11SlotInfo slot(PK11_GetInternalSlot()); + EXPECT_TRUE(slot); + + SECKEYPrivateKey* key = nullptr; + SECItem item = {siBuffer, const_cast<unsigned char*>(data.data()), + (unsigned int)data.size()}; + + SECStatus rv = PK11_ImportDERPrivateKeyInfoAndReturnKey( + slot.get(), &item, nullptr, nullptr, false, false, KU_ALL, &key, + nullptr); + + EXPECT_EQ(rv == SECSuccess, key != nullptr); + SECKEY_DestroyPrivateKey(key); + + return rv == SECSuccess; + } +}; + +TEST_F(DERPrivateKeyImportTest, ImportPrivateRSAKey) { + EXPECT_TRUE(ParsePrivateKey(kValidRSAKey)); + EXPECT_FALSE(PORT_GetError()); +} + +TEST_F(DERPrivateKeyImportTest, ImportInvalidPrivateKey) { + EXPECT_FALSE(ParsePrivateKey(kInvalidLengthKey)); + EXPECT_EQ(PORT_GetError(), SEC_ERROR_BAD_DER); +} + +TEST_F(DERPrivateKeyImportTest, ImportZeroLengthPrivateKey) { + EXPECT_FALSE(ParsePrivateKey(kInvalidZeroLengthKey)); + EXPECT_EQ(PORT_GetError(), SEC_ERROR_BAD_KEY); +} + +} // namespace nss_test diff --git a/security/nss/gtests/der_gtest/manifest.mn b/security/nss/gtests/der_gtest/manifest.mn new file mode 100644 index 000000000..862692f56 --- /dev/null +++ b/security/nss/gtests/der_gtest/manifest.mn @@ -0,0 +1,22 @@ +# +# This Source Code Form is subject to the terms of the Mozilla Public +# License, v. 2.0. If a copy of the MPL was not distributed with this +# file, You can obtain one at http://mozilla.org/MPL/2.0/. +CORE_DEPTH = ../.. +DEPTH = ../.. +MODULE = nss + +CPPSRCS = \ + der_getint_unittest.cc \ + der_private_key_import_unittest.cc \ + $(NULL) + +INCLUDES += -I$(CORE_DEPTH)/gtests/google_test/gtest/include \ + -I$(CORE_DEPTH)/gtests/common + +REQUIRES = nspr nss libdbm gtest + +PROGRAM = der_gtest + +EXTRA_LIBS = $(DIST)/lib/$(LIB_PREFIX)gtest.$(LIB_SUFFIX) $(EXTRA_OBJS) \ + ../common/$(OBJDIR)/gtests$(OBJ_SUFFIX) |