tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

der_private_key_import_unittest.cc (4820B)


      1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
      2 /* vim: set ts=2 et sw=2 tw=80: */
      3 /* This Source Code Form is subject to the terms of the Mozilla Public
      4 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
      5 * You can obtain one at http://mozilla.org/MPL/2.0/. */
      6 
      7 #include <climits>
      8 #include <memory>
      9 #include "nss.h"
     10 #include "pk11pub.h"
     11 #include "secutil.h"
     12 
     13 #include "gtest/gtest.h"
     14 #include "nss_scoped_ptrs.h"
     15 
     16 namespace nss_test {
     17 
     18 const std::vector<uint8_t> kValidRSAKey = {
     19    // 512-bit RSA private key (PKCS#8)
     20    0x30, 0x82, 0x01, 0x54, 0x02, 0x01, 0x00, 0x30, 0x0d, 0x06, 0x09, 0x2a,
     21    0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01, 0x05, 0x00, 0x04, 0x82,
     22    0x01, 0x3e, 0x30, 0x82, 0x01, 0x3a, 0x02, 0x01, 0x00, 0x02, 0x41, 0x00,
     23    0xa2, 0x40, 0xce, 0xb5, 0x4e, 0x70, 0xdc, 0x14, 0x82, 0x5b, 0x58, 0x7d,
     24    0x2f, 0x5d, 0xfd, 0x46, 0x3c, 0x4b, 0x82, 0x50, 0xb6, 0x96, 0x00, 0x4a,
     25    0x1a, 0xca, 0xaf, 0xe4, 0x9b, 0xcf, 0x38, 0x4a, 0x46, 0xaa, 0x9f, 0xb4,
     26    0xd9, 0xc7, 0xee, 0x88, 0xe9, 0xef, 0x0a, 0x31, 0x5f, 0x53, 0x86, 0x8f,
     27    0x63, 0x68, 0x0b, 0x58, 0x34, 0x72, 0x49, 0xba, 0xed, 0xd9, 0x34, 0x15,
     28    0x16, 0xc4, 0xca, 0xb7, 0x02, 0x03, 0x01, 0x00, 0x01, 0x02, 0x40, 0x34,
     29    0xe6, 0xdc, 0x7e, 0xd0, 0xec, 0x8b, 0x55, 0x44, 0x8b, 0x73, 0xf6, 0x9d,
     30    0x13, 0x10, 0x19, 0x6e, 0x5f, 0x50, 0x45, 0xf0, 0xc2, 0x47, 0xa5, 0xe1,
     31    0xc6, 0x64, 0x43, 0x2d, 0x6a, 0x0a, 0xf7, 0xe7, 0xda, 0x40, 0xb8, 0x3a,
     32    0xf0, 0x47, 0xdd, 0x01, 0xf5, 0xe0, 0xa9, 0x0e, 0x47, 0xc2, 0x24, 0xd7,
     33    0xb5, 0x13, 0x3a, 0x35, 0x4d, 0x11, 0xaa, 0x50, 0x03, 0xb3, 0xe8, 0x54,
     34    0x6c, 0x99, 0x01, 0x02, 0x21, 0x00, 0xcd, 0xb2, 0xd7, 0xa7, 0x43, 0x5b,
     35    0xcb, 0x45, 0xe5, 0x0e, 0x86, 0xf6, 0xc1, 0x4e, 0x97, 0xed, 0x78, 0x1f,
     36    0x09, 0x56, 0xcd, 0x26, 0xe6, 0xf7, 0x5e, 0xd9, 0xfc, 0x88, 0x12, 0x5f,
     37    0x84, 0x07, 0x02, 0x21, 0x00, 0xc9, 0xee, 0x30, 0xaf, 0x6c, 0xb9, 0x5a,
     38    0xc9, 0xc1, 0x14, 0x9e, 0xd8, 0x4b, 0x33, 0x38, 0x48, 0x17, 0x41, 0x35,
     39    0x94, 0x09, 0xf3, 0x69, 0xc4, 0x97, 0xbe, 0x17, 0x7d, 0x95, 0x0f, 0xb7,
     40    0xd1, 0x02, 0x21, 0x00, 0x8b, 0x0e, 0xf9, 0x8d, 0x61, 0x13, 0x20, 0x63,
     41    0x9b, 0x0b, 0x6c, 0x20, 0x4a, 0xe4, 0xa7, 0xfe, 0xe8, 0xf3, 0x0a, 0x6c,
     42    0x3c, 0xfa, 0xac, 0xaf, 0xd4, 0xd6, 0xc7, 0x4a, 0xf2, 0x28, 0xd2, 0x67,
     43    0x02, 0x20, 0x6b, 0x0e, 0x1d, 0xbf, 0x93, 0x5b, 0xbd, 0x77, 0x43, 0x27,
     44    0x24, 0x83, 0xb5, 0x72, 0xa5, 0x3f, 0x0b, 0x1d, 0x26, 0x43, 0xa2, 0xf6,
     45    0xea, 0xb7, 0x30, 0x5f, 0xb6, 0x62, 0x7c, 0xf9, 0x85, 0x51, 0x02, 0x20,
     46    0x3d, 0x22, 0x63, 0x15, 0x6b, 0x32, 0x41, 0x46, 0x44, 0x78, 0xb7, 0x13,
     47    0xeb, 0x85, 0x4c, 0x4f, 0x6b, 0x3e, 0xf0, 0x52, 0xf0, 0x46, 0x3b, 0x65,
     48    0xd8, 0x21, 0x7d, 0xae, 0xc0, 0x09, 0x98, 0x34};
     49 
     50 const std::vector<uint8_t> kInvalidLengthKey = {
     51    0x30, 0x1b,        // SEQUENCE(len=27)
     52    0x02, 0x01, 0x00,  // INT(len=1) = 0
     53    0x30, 0x13,        // SEQUENCE(len=19)
     54    0x06, 0x07,        // OID(len=7)
     55    // dhPublicKey (1.2.840.10046.2.1)
     56    0x2a, 0x86, 0x48, 0xce, 0x3e, 0x02, 0x01, 0x06, 0x08,  // OID(len=8)
     57    // prime256v1 (1.2.840.10045.3.1.7) */
     58    0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07, 0x04,
     59    0x00  // OCTET STRING(len=0)
     60 };
     61 
     62 const std::vector<uint8_t> kInvalidZeroLengthKey = {
     63    0x30, 0x1a,        // SEQUENCE(len=26)
     64    0x02, 0x01, 0x00,  // INT(len=1) = 0
     65    0x30, 0x13,        // SEQUENCE(len=19)
     66    0x06, 0x07,        // OID(len=7)
     67    // dhPublicKey (1.2.840.10046.2.1)
     68    0x2a, 0x86, 0x48, 0xce, 0x3e, 0x02, 0x01, 0x06, 0x08,  // OID(len=8)
     69    // prime256v1 (1.2.840.10045.3.1.7) */
     70    0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07, 0x04,
     71    0x00  // OCTET STRING(len=0)
     72 };
     73 
     74 class DERPrivateKeyImportTest : public ::testing::Test {
     75 public:
     76  bool ParsePrivateKey(const std::vector<uint8_t>& data) {
     77    ScopedPK11SlotInfo slot(PK11_GetInternalSlot());
     78    EXPECT_TRUE(slot);
     79 
     80    SECKEYPrivateKey* key = nullptr;
     81    SECItem item = {siBuffer, const_cast<unsigned char*>(data.data()),
     82                    (unsigned int)data.size()};
     83 
     84    SECStatus rv = PK11_ImportDERPrivateKeyInfoAndReturnKey(
     85        slot.get(), &item, nullptr, nullptr, false, false, KU_ALL, &key,
     86        nullptr);
     87 
     88    EXPECT_EQ(rv == SECSuccess, key != nullptr);
     89    SECKEY_DestroyPrivateKey(key);
     90 
     91    return rv == SECSuccess;
     92  }
     93 };
     94 
     95 TEST_F(DERPrivateKeyImportTest, ImportPrivateRSAKey) {
     96  EXPECT_TRUE(ParsePrivateKey(kValidRSAKey));
     97  EXPECT_FALSE(PORT_GetError());
     98 }
     99 
    100 TEST_F(DERPrivateKeyImportTest, ImportInvalidPrivateKey) {
    101  EXPECT_FALSE(ParsePrivateKey(kInvalidLengthKey));
    102  EXPECT_EQ(PORT_GetError(), SEC_ERROR_BAD_DER);
    103 }
    104 
    105 TEST_F(DERPrivateKeyImportTest, ImportZeroLengthPrivateKey) {
    106  EXPECT_FALSE(ParsePrivateKey(kInvalidZeroLengthKey));
    107  EXPECT_EQ(PORT_GetError(), SEC_ERROR_BAD_KEY);
    108 }
    109 
    110 }  // namespace nss_test