tor-browser

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

pkixtestalg.cpp (7895B)


      1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
      2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
      3 /* This code is made available to you under your choice of the following sets
      4 * of licensing terms:
      5 */
      6 /* This Source Code Form is subject to the terms of the Mozilla Public
      7 * License, v. 2.0. If a copy of the MPL was not distributed with this
      8 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
      9 */
     10 /* Copyright 2015 Mozilla Contributors
     11 *
     12 * Licensed under the Apache License, Version 2.0 (the "License");
     13 * you may not use this file except in compliance with the License.
     14 * You may obtain a copy of the License at
     15 *
     16 *     http://www.apache.org/licenses/LICENSE-2.0
     17 *
     18 * Unless required by applicable law or agreed to in writing, software
     19 * distributed under the License is distributed on an "AS IS" BASIS,
     20 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     21 * See the License for the specific language governing permissions and
     22 * limitations under the License.
     23 */
     24 
     25 #include "mozpkix/test/pkixtestutil.h"
     26 
     27 #include "mozpkix/pkixder.h"
     28 #include "mozpkix/nss_scoped_ptrs.h"
     29 
     30 // python DottedOIDToCode.py --prefixdefine PREFIX_1_2_840_10040 1.2.840.10040
     31 #define PREFIX_1_2_840_10040 0x2a, 0x86, 0x48, 0xce, 0x38
     32 
     33 // python DottedOIDToCode.py --prefixdefine PREFIX_1_2_840_10045 1.2.840.10045
     34 #define PREFIX_1_2_840_10045 0x2a, 0x86, 0x48, 0xce, 0x3d
     35 
     36 // python DottedOIDToCode.py --prefixdefine PREFIX_1_2_840_113549 1.2.840.113549
     37 #define PREFIX_1_2_840_113549 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d
     38 
     39 namespace mozilla { namespace pkix { namespace test {
     40 
     41 namespace {
     42 
     43 enum class NULLParam { NO, YES };
     44 
     45 template <size_t SIZE>
     46 ByteString
     47 OID(const uint8_t (&rawValue)[SIZE])
     48 {
     49  return TLV(der::OIDTag, ByteString(rawValue, SIZE));
     50 }
     51 
     52 template <size_t SIZE>
     53 ByteString
     54 SimpleAlgID(const uint8_t (&rawValue)[SIZE],
     55            NULLParam nullParam = NULLParam::NO)
     56 {
     57  ByteString sequenceValue(OID(rawValue));
     58  if (nullParam == NULLParam::YES) {
     59    sequenceValue.append(TLV(der::NULLTag, ByteString()));
     60  }
     61  return TLV(der::SEQUENCE, sequenceValue);
     62 }
     63 
     64 template <size_t SIZE>
     65 ByteString
     66 DERInteger(const uint8_t (&rawValue)[SIZE])
     67 {
     68  ByteString value(rawValue, SIZE);
     69  if (value[0] & 0x80u) {
     70    // Prefix with a leading zero to disambiguate this from a negative value.
     71    value.insert(value.begin(), 0x00);
     72  }
     73  return TLV(der::INTEGER, value);
     74 }
     75 
     76 // Generated with "openssl dsaparam -C -noout 2048" and reformatted.
     77 // openssl 1.0 or later must be used so that a 256-bit Q value is
     78 // generated.
     79 static const uint8_t DSS_P_RAW[] =
     80 {
     81  0xB3,0xCD,0x29,0x44,0xF0,0x25,0xA7,0x73,0xFC,0x86,0x70,0xA2,
     82  0x69,0x5A,0x97,0x3F,0xBD,0x1C,0x6F,0xAA,0x4A,0x40,0x42,0x8E,
     83  0xCF,0xAE,0x62,0x12,0xED,0xB4,0xFD,0x05,0xC2,0xAE,0xB1,0x8C,
     84  0xFC,0xBE,0x38,0x90,0xBB,0x7C,0xFF,0x16,0xF4,0xED,0xCE,0x72,
     85  0x12,0x93,0x83,0xF0,0xA4,0xA1,0x71,0xDC,0x4B,0xF0,0x4E,0x3A,
     86  0x2B,0xFA,0x17,0xB7,0xB3,0x2A,0xCC,0x2C,0xD3,0xC8,0x21,0x49,
     87  0x7A,0x83,0x71,0x8B,0x3D,0x62,0x96,0xDC,0xAD,0xA8,0x03,0xBE,
     88  0x1D,0x33,0x11,0xF3,0xEB,0xD8,0x1B,0x8D,0xDB,0x62,0x79,0x83,
     89  0xF8,0x67,0x4E,0x62,0x21,0x2C,0x81,0x59,0xE8,0x73,0xD7,0xAF,
     90  0xB9,0x63,0x60,0xEA,0xAE,0xEC,0x68,0x6A,0xB4,0xB0,0x65,0xBA,
     91  0xA3,0x4C,0x09,0x99,0x29,0x6A,0x2E,0x2B,0xFC,0x6D,0x51,0xCA,
     92  0x30,0xA2,0x2F,0x7A,0x65,0x76,0xA7,0x55,0x13,0x11,0xA0,0x02,
     93  0xA2,0x59,0x4B,0xCE,0xA7,0x05,0xF6,0x07,0x35,0x9B,0x41,0xD7,
     94  0x11,0x5A,0x18,0x57,0xA7,0x78,0x88,0xC3,0xA8,0xE3,0x39,0xF5,
     95  0x47,0x3D,0x2E,0x18,0x54,0xB0,0xF0,0xBF,0x65,0x3F,0x77,0xC7,
     96  0x11,0xB8,0x0D,0x52,0xAD,0xC8,0xE8,0x6D,0xF6,0x7E,0x88,0x65,
     97  0x84,0x2B,0xF7,0xEF,0x8E,0xB5,0x7C,0xBD,0x2E,0x0D,0xF3,0xC6,
     98  0xDD,0x0B,0xB4,0xF2,0x23,0x1F,0xDA,0x55,0x05,0xF5,0xDC,0x53,
     99  0xA6,0x83,0xDA,0x5C,0xEF,0x29,0x02,0x78,0x68,0xD0,0xA4,0x39,
    100  0x09,0x7F,0xFA,0x49,0x18,0xD0,0xB5,0x19,0x35,0x31,0x8E,0xDE,
    101  0x43,0x35,0xA3,0xB9,0x6D,0xC1,0x70,0xC6,0x0D,0x18,0x24,0xEB,
    102  0x1E,0x4D,0x52,0xB7,
    103 };
    104 
    105 static const uint8_t DSS_Q_RAW[] =
    106 {
    107  0x8D,0x6B,0x86,0x89,0x9C,0x8D,0x30,0x91,0xCC,0x6E,0x34,0xF1,
    108  0xE8,0x9C,0x8A,0x5C,0xD6,0xAB,0x01,0x1E,0xC4,0xDB,0xFD,0x07,
    109  0xEB,0x5F,0x4E,0xE8,0xFA,0xFC,0x98,0x2D,
    110 };
    111 
    112 static const uint8_t DSS_G_RAW[] =
    113 {
    114  0x0E,0x2C,0x34,0xB2,0xE1,0x66,0x49,0xB6,0x9A,0x7D,0x67,0x3E,
    115  0xEE,0x98,0x35,0x18,0x28,0x35,0xFC,0x05,0x36,0x3B,0x94,0xE6,
    116  0x1E,0x1C,0x5B,0x05,0x3E,0x86,0x1B,0xE3,0xED,0xD2,0xE1,0xF3,
    117  0xF7,0xF7,0x60,0x6D,0x7D,0xA1,0xAF,0x9A,0xD1,0xDF,0xA2,0x9C,
    118  0xFC,0xA2,0xEB,0x90,0x8B,0x1C,0x82,0x92,0x45,0x7B,0x30,0x2A,
    119  0xFD,0x7A,0xE6,0x68,0x8F,0xEC,0x89,0x3A,0x9A,0xAD,0xFE,0x25,
    120  0x5E,0x51,0xC5,0x29,0x45,0x7F,0xAC,0xDE,0xFC,0xB4,0x1B,0x3A,
    121  0xDA,0xC7,0x21,0x68,0x87,0x27,0x8D,0x7B,0xB2,0xBB,0x41,0x60,
    122  0x46,0x42,0x5B,0x6B,0xE8,0x80,0xD2,0xE4,0xA3,0x30,0x8F,0xD5,
    123  0x71,0x07,0x8A,0x7B,0x32,0x56,0x84,0x41,0x1C,0xDF,0x69,0xE9,
    124  0xFD,0xBA,0x48,0xE0,0x43,0xA0,0x38,0x92,0x12,0xF3,0x52,0xA5,
    125  0x40,0x87,0xCB,0x34,0xBB,0x3E,0x25,0x29,0x3C,0xC6,0xA5,0x17,
    126  0xFD,0x58,0x47,0x89,0xDB,0x9B,0xB9,0xCF,0xE9,0xA8,0xF2,0xEC,
    127  0x55,0x76,0xF5,0xF1,0x9C,0x6E,0x0A,0x3F,0x16,0x5F,0x49,0x31,
    128  0x31,0x1C,0x43,0xA2,0x83,0xDA,0xDD,0x7F,0x1C,0xEA,0x05,0x36,
    129  0x7B,0xED,0x09,0xFB,0x6F,0x8A,0x2B,0x55,0xB9,0xBC,0x4A,0x8C,
    130  0x28,0xC1,0x4D,0x13,0x6E,0x47,0xF4,0xAD,0x79,0x00,0xE9,0x5A,
    131  0xB6,0xC7,0x73,0x28,0xA9,0x89,0xAD,0xE8,0x6E,0xC6,0x54,0xA5,
    132  0x56,0x2D,0xAA,0x81,0x83,0x9E,0xC1,0x13,0x79,0xA4,0x12,0xE0,
    133  0x76,0x1F,0x25,0x43,0xB6,0xDE,0x56,0xF7,0x52,0xCC,0x07,0xB8,
    134  0x37,0xE2,0x8C,0xC5,0x56,0x8C,0xDD,0x63,0xF5,0xB6,0xA3,0x46,
    135  0x62,0xF6,0x35,0x76,
    136 };
    137 
    138 } // namespace
    139 
    140 TestSignatureAlgorithm::TestSignatureAlgorithm(
    141  const TestPublicKeyAlgorithm& aPublicKeyAlg,
    142  TestDigestAlgorithmID aDigestAlg,
    143  const ByteString& aAlgorithmIdentifier,
    144  bool aAccepted)
    145  : publicKeyAlg(aPublicKeyAlg)
    146  , digestAlg(aDigestAlg)
    147  , algorithmIdentifier(aAlgorithmIdentifier)
    148  , accepted(aAccepted)
    149 {
    150 }
    151 
    152 ByteString DSS_P() { return ByteString(DSS_P_RAW, sizeof(DSS_P_RAW)); }
    153 ByteString DSS_Q() { return ByteString(DSS_Q_RAW, sizeof(DSS_Q_RAW)); }
    154 ByteString DSS_G() { return ByteString(DSS_G_RAW, sizeof(DSS_G_RAW)); }
    155 
    156 TestPublicKeyAlgorithm
    157 DSS()
    158 {
    159  static const uint8_t oidValue[] = { PREFIX_1_2_840_10040, 4, 1 };
    160 
    161  // RFC 3279 Section-2.3.2
    162  return TestPublicKeyAlgorithm(
    163           TLV(der::SEQUENCE,
    164               OID(oidValue) +
    165               TLV(der::SEQUENCE,
    166                   DERInteger(DSS_P_RAW) +
    167                   DERInteger(DSS_Q_RAW) +
    168                   DERInteger(DSS_G_RAW))));
    169 }
    170 
    171 // RFC 3279 Section 2.3.1
    172 TestPublicKeyAlgorithm
    173 RSA_PKCS1()
    174 {
    175  static const uint8_t rsaEncryption[] = { PREFIX_1_2_840_113549, 1, 1, 1 };
    176  return TestPublicKeyAlgorithm(SimpleAlgID(rsaEncryption, NULLParam::YES));
    177 }
    178 
    179 // RFC 3279 Section 2.2.1
    180 TestSignatureAlgorithm md2WithRSAEncryption()
    181 {
    182  static const uint8_t oidValue[] = { PREFIX_1_2_840_113549, 1, 1, 2 };
    183  return TestSignatureAlgorithm(RSA_PKCS1(), TestDigestAlgorithmID::MD2,
    184                                SimpleAlgID(oidValue), false);
    185 }
    186 
    187 // RFC 3279 Section 2.2.1
    188 TestSignatureAlgorithm md5WithRSAEncryption()
    189 {
    190  static const uint8_t oidValue[] = { PREFIX_1_2_840_113549, 1, 1, 4 };
    191  return TestSignatureAlgorithm(RSA_PKCS1(), TestDigestAlgorithmID::MD5,
    192                                SimpleAlgID(oidValue), false);
    193 }
    194 
    195 // RFC 3279 Section 2.2.1
    196 TestSignatureAlgorithm sha1WithRSAEncryption()
    197 {
    198  static const uint8_t oidValue[] = { PREFIX_1_2_840_113549, 1, 1, 5 };
    199  return TestSignatureAlgorithm(RSA_PKCS1(), TestDigestAlgorithmID::SHA1,
    200                                SimpleAlgID(oidValue), true);
    201 }
    202 
    203 // RFC 4055 Section 5
    204 TestSignatureAlgorithm sha256WithRSAEncryption()
    205 {
    206  static const uint8_t oidValue[] = { PREFIX_1_2_840_113549, 1, 1, 11 };
    207  return TestSignatureAlgorithm(RSA_PKCS1(), TestDigestAlgorithmID::SHA256,
    208                                SimpleAlgID(oidValue), true);
    209 }
    210 
    211 } } } // namespace mozilla::pkix