/src/mozilla-central/security/pkix/test/lib/pkixtestutil.h
Line | Count | Source (jump to first uncovered line) |
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 2013 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 | | #ifndef mozilla_pkix_test_pkixtestutil_h |
26 | | #define mozilla_pkix_test_pkixtestutil_h |
27 | | |
28 | | #include <ctime> |
29 | | #include <stdint.h> // Some Mozilla-supported compilers lack <cstdint> |
30 | | #include <string> |
31 | | #include <cstring> |
32 | | |
33 | | #include "pkix/pkixtypes.h" |
34 | | #include "../../lib/ScopedPtr.h" |
35 | | |
36 | | namespace mozilla { namespace pkix { namespace test { |
37 | | |
38 | | typedef std::basic_string<uint8_t> ByteString; |
39 | | |
40 | 0 | inline bool ENCODING_FAILED(const ByteString& bs) { return bs.empty(); } |
41 | | |
42 | | template <size_t L> |
43 | | inline ByteString |
44 | | BytesToByteString(const uint8_t (&bytes)[L]) |
45 | 1.32k | { |
46 | 1.32k | return ByteString(bytes, L); |
47 | 1.32k | } Unexecuted instantiation: std::__1::basic_string<unsigned char, std::__1::char_traits<unsigned char>, std::__1::allocator<unsigned char> > mozilla::pkix::test::BytesToByteString<12ul>(unsigned char const (&) [12ul]) std::__1::basic_string<unsigned char, std::__1::char_traits<unsigned char>, std::__1::allocator<unsigned char> > mozilla::pkix::test::BytesToByteString<5ul>(unsigned char const (&) [5ul]) Line | Count | Source | 45 | 72 | { | 46 | 72 | return ByteString(bytes, L); | 47 | 72 | } |
std::__1::basic_string<unsigned char, std::__1::char_traits<unsigned char>, std::__1::allocator<unsigned char> > mozilla::pkix::test::BytesToByteString<20ul>(unsigned char const (&) [20ul]) Line | Count | Source | 45 | 150 | { | 46 | 150 | return ByteString(bytes, L); | 47 | 150 | } |
std::__1::basic_string<unsigned char, std::__1::char_traits<unsigned char>, std::__1::allocator<unsigned char> > mozilla::pkix::test::BytesToByteString<10ul>(unsigned char const (&) [10ul]) Line | Count | Source | 45 | 801 | { | 46 | 801 | return ByteString(bytes, L); | 47 | 801 | } |
std::__1::basic_string<unsigned char, std::__1::char_traits<unsigned char>, std::__1::allocator<unsigned char> > mozilla::pkix::test::BytesToByteString<11ul>(unsigned char const (&) [11ul]) Line | Count | Source | 45 | 159 | { | 46 | 159 | return ByteString(bytes, L); | 47 | 159 | } |
std::__1::basic_string<unsigned char, std::__1::char_traits<unsigned char>, std::__1::allocator<unsigned char> > mozilla::pkix::test::BytesToByteString<6ul>(unsigned char const (&) [6ul]) Line | Count | Source | 45 | 144 | { | 46 | 144 | return ByteString(bytes, L); | 47 | 144 | } |
|
48 | | |
49 | | // XXX: Ideally, we should define this instead: |
50 | | // |
51 | | // template <typename T, std::size_t N> |
52 | | // constexpr inline std::size_t |
53 | | // ArrayLength(T (&)[N]) |
54 | | // { |
55 | | // return N; |
56 | | // } |
57 | | // |
58 | | // However, we don't because not all supported compilers support constexpr, |
59 | | // and we need to calculate array lengths in static_assert sometimes. |
60 | | // |
61 | | // XXX: Evaluates its argument twice |
62 | 0 | #define MOZILLA_PKIX_ARRAY_LENGTH(x) (sizeof(x) / sizeof((x)[0])) |
63 | | |
64 | | bool InputEqualsByteString(Input input, const ByteString& bs); |
65 | | ByteString InputToByteString(Input input); |
66 | | |
67 | | // python DottedOIDToCode.py --tlv id-kp-OCSPSigning 1.3.6.1.5.5.7.3.9 |
68 | | static const uint8_t tlv_id_kp_OCSPSigning[] = { |
69 | | 0x06, 0x08, 0x2b, 0x06, 0x01, 0x05, 0x05, 0x07, 0x03, 0x09 |
70 | | }; |
71 | | |
72 | | // python DottedOIDToCode.py --tlv id-kp-serverAuth 1.3.6.1.5.5.7.3.1 |
73 | | static const uint8_t tlv_id_kp_serverAuth[] = { |
74 | | 0x06, 0x08, 0x2b, 0x06, 0x01, 0x05, 0x05, 0x07, 0x03, 0x01 |
75 | | }; |
76 | | |
77 | | enum class TestDigestAlgorithmID |
78 | | { |
79 | | MD2, |
80 | | MD5, |
81 | | SHA1, |
82 | | SHA224, |
83 | | SHA256, |
84 | | SHA384, |
85 | | SHA512, |
86 | | }; |
87 | | |
88 | | struct TestPublicKeyAlgorithm |
89 | | { |
90 | | explicit TestPublicKeyAlgorithm(const ByteString& aAlgorithmIdentifier) |
91 | 60 | : algorithmIdentifier(aAlgorithmIdentifier) { } |
92 | | bool operator==(const TestPublicKeyAlgorithm& other) const |
93 | 0 | { |
94 | 0 | return algorithmIdentifier == other.algorithmIdentifier; |
95 | 0 | } |
96 | | ByteString algorithmIdentifier; |
97 | | }; |
98 | | |
99 | | ByteString DSS_P(); |
100 | | ByteString DSS_Q(); |
101 | | ByteString DSS_G(); |
102 | | |
103 | | TestPublicKeyAlgorithm DSS(); |
104 | | TestPublicKeyAlgorithm RSA_PKCS1(); |
105 | | |
106 | | struct TestSignatureAlgorithm |
107 | | { |
108 | | TestSignatureAlgorithm(const TestPublicKeyAlgorithm& publicKeyAlg, |
109 | | TestDigestAlgorithmID digestAlg, |
110 | | const ByteString& algorithmIdentifier, |
111 | | bool accepted); |
112 | | |
113 | | TestPublicKeyAlgorithm publicKeyAlg; |
114 | | TestDigestAlgorithmID digestAlg; |
115 | | ByteString algorithmIdentifier; |
116 | | bool accepted; |
117 | | }; |
118 | | |
119 | | TestSignatureAlgorithm md2WithRSAEncryption(); |
120 | | TestSignatureAlgorithm md5WithRSAEncryption(); |
121 | | TestSignatureAlgorithm sha1WithRSAEncryption(); |
122 | | TestSignatureAlgorithm sha256WithRSAEncryption(); |
123 | | |
124 | | // e.g. YMDHMS(2016, 12, 31, 1, 23, 45) => 2016-12-31:01:23:45 (GMT) |
125 | | mozilla::pkix::Time YMDHMS(uint16_t year, uint16_t month, uint16_t day, |
126 | | uint16_t hour, uint16_t minutes, uint16_t seconds); |
127 | | |
128 | | ByteString TLV(uint8_t tag, size_t length, const ByteString& value); |
129 | | |
130 | | inline ByteString |
131 | | TLV(uint8_t tag, const ByteString& value) |
132 | 4.30k | { |
133 | 4.30k | return TLV(tag, value.length(), value); |
134 | 4.30k | } |
135 | | |
136 | | // Although we can't enforce it without relying on Cuser-defined literals, |
137 | | // which aren't supported by all of our compilers yet, you should only pass |
138 | | // string literals as the last parameter to the following two functions. |
139 | | |
140 | | template <size_t N> |
141 | | inline ByteString |
142 | | TLV(uint8_t tag, const char(&value)[N]) |
143 | 78 | { |
144 | 78 | static_assert(N > 0, "cannot have string literal of size 0"); |
145 | 78 | assert(value[N - 1] == 0); |
146 | 78 | return TLV(tag, ByteString(reinterpret_cast<const uint8_t*>(&value), N - 1)); |
147 | 78 | } std::__1::basic_string<unsigned char, std::__1::char_traits<unsigned char>, std::__1::allocator<unsigned char> > mozilla::pkix::test::TLV<2ul>(unsigned char, char const (&) [2ul]) Line | Count | Source | 143 | 27 | { | 144 | 27 | static_assert(N > 0, "cannot have string literal of size 0"); | 145 | 27 | assert(value[N - 1] == 0); | 146 | 27 | return TLV(tag, ByteString(reinterpret_cast<const uint8_t*>(&value), N - 1)); | 147 | 27 | } |
std::__1::basic_string<unsigned char, std::__1::char_traits<unsigned char>, std::__1::allocator<unsigned char> > mozilla::pkix::test::TLV<3ul>(unsigned char, char const (&) [3ul]) Line | Count | Source | 143 | 48 | { | 144 | 48 | static_assert(N > 0, "cannot have string literal of size 0"); | 145 | 48 | assert(value[N - 1] == 0); | 146 | 48 | return TLV(tag, ByteString(reinterpret_cast<const uint8_t*>(&value), N - 1)); | 147 | 48 | } |
std::__1::basic_string<unsigned char, std::__1::char_traits<unsigned char>, std::__1::allocator<unsigned char> > mozilla::pkix::test::TLV<257ul>(unsigned char, char const (&) [257ul]) Line | Count | Source | 143 | 3 | { | 144 | 3 | static_assert(N > 0, "cannot have string literal of size 0"); | 145 | 3 | assert(value[N - 1] == 0); | 146 | 3 | return TLV(tag, ByteString(reinterpret_cast<const uint8_t*>(&value), N - 1)); | 147 | 3 | } |
|
148 | | |
149 | | template <size_t N> |
150 | | inline ByteString |
151 | | TLV(uint8_t tag, size_t length, const char(&value)[N]) |
152 | 21 | { |
153 | 21 | static_assert(N > 0, "cannot have string literal of size 0"); |
154 | 21 | assert(value[N - 1] == 0); |
155 | 21 | return TLV(tag, length, |
156 | 21 | ByteString(reinterpret_cast<const uint8_t*>(&value), N - 1)); |
157 | 21 | } std::__1::basic_string<unsigned char, std::__1::char_traits<unsigned char>, std::__1::allocator<unsigned char> > mozilla::pkix::test::TLV<3ul>(unsigned char, unsigned long, char const (&) [3ul]) Line | Count | Source | 152 | 12 | { | 153 | 12 | static_assert(N > 0, "cannot have string literal of size 0"); | 154 | 12 | assert(value[N - 1] == 0); | 155 | 12 | return TLV(tag, length, | 156 | 12 | ByteString(reinterpret_cast<const uint8_t*>(&value), N - 1)); | 157 | 12 | } |
std::__1::basic_string<unsigned char, std::__1::char_traits<unsigned char>, std::__1::allocator<unsigned char> > mozilla::pkix::test::TLV<2ul>(unsigned char, unsigned long, char const (&) [2ul]) Line | Count | Source | 152 | 6 | { | 153 | 6 | static_assert(N > 0, "cannot have string literal of size 0"); | 154 | 6 | assert(value[N - 1] == 0); | 155 | 6 | return TLV(tag, length, | 156 | 6 | ByteString(reinterpret_cast<const uint8_t*>(&value), N - 1)); | 157 | 6 | } |
std::__1::basic_string<unsigned char, std::__1::char_traits<unsigned char>, std::__1::allocator<unsigned char> > mozilla::pkix::test::TLV<5ul>(unsigned char, unsigned long, char const (&) [5ul]) Line | Count | Source | 152 | 3 | { | 153 | 3 | static_assert(N > 0, "cannot have string literal of size 0"); | 154 | 3 | assert(value[N - 1] == 0); | 155 | 3 | return TLV(tag, length, | 156 | 3 | ByteString(reinterpret_cast<const uint8_t*>(&value), N - 1)); | 157 | 3 | } |
|
158 | | |
159 | | ByteString Boolean(bool value); |
160 | | ByteString Integer(long value); |
161 | | |
162 | | ByteString CN(const ByteString&, uint8_t encodingTag = 0x0c /*UTF8String*/); |
163 | | |
164 | | inline ByteString |
165 | | CN(const char* value, uint8_t encodingTag = 0x0c /*UTF8String*/) |
166 | 378 | { |
167 | 378 | return CN(ByteString(reinterpret_cast<const uint8_t*>(value), |
168 | 378 | std::strlen(value)), encodingTag); |
169 | 378 | } |
170 | | |
171 | | ByteString OU(const ByteString&, uint8_t encodingTag = 0x0c /*UTF8String*/); |
172 | | |
173 | | inline ByteString |
174 | | OU(const char* value, uint8_t encodingTag = 0x0c /*UTF8String*/) |
175 | 135 | { |
176 | 135 | return OU(ByteString(reinterpret_cast<const uint8_t*>(value), |
177 | 135 | std::strlen(value)), encodingTag); |
178 | 135 | } |
179 | | |
180 | | ByteString emailAddress(const ByteString&); |
181 | | |
182 | | inline ByteString |
183 | | emailAddress(const char* value) |
184 | 6 | { |
185 | 6 | return emailAddress(ByteString(reinterpret_cast<const uint8_t*>(value), |
186 | 6 | std::strlen(value))); |
187 | 6 | } |
188 | | |
189 | | // RelativeDistinguishedName ::= |
190 | | // SET SIZE (1..MAX) OF AttributeTypeAndValue |
191 | | // |
192 | | ByteString RDN(const ByteString& avas); |
193 | | |
194 | | // Name ::= CHOICE { -- only one possibility for now -- |
195 | | // rdnSequence RDNSequence } |
196 | | // |
197 | | // RDNSequence ::= SEQUENCE OF RelativeDistinguishedName |
198 | | // |
199 | | ByteString Name(const ByteString& rdns); |
200 | | |
201 | | inline ByteString |
202 | | CNToDERName(const ByteString& cn) |
203 | 0 | { |
204 | 0 | return Name(RDN(CN(cn))); |
205 | 0 | } |
206 | | |
207 | | inline ByteString |
208 | | CNToDERName(const char* cn) |
209 | 0 | { |
210 | 0 | return Name(RDN(CN(cn))); |
211 | 0 | } |
212 | | |
213 | | // GeneralName ::= CHOICE { |
214 | | // otherName [0] OtherName, |
215 | | // rfc822Name [1] IA5String, |
216 | | // dNSName [2] IA5String, |
217 | | // x400Address [3] ORAddress, |
218 | | // directoryName [4] Name, |
219 | | // ediPartyName [5] EDIPartyName, |
220 | | // uniformResourceIdentifier [6] IA5String, |
221 | | // iPAddress [7] OCTET STRING, |
222 | | // registeredID [8] OBJECT IDENTIFIER } |
223 | | |
224 | | inline ByteString |
225 | | RFC822Name(const ByteString& name) |
226 | 195 | { |
227 | 195 | // (2 << 6) means "context-specific", 1 is the GeneralName tag. |
228 | 195 | return TLV((2 << 6) | 1, name); |
229 | 195 | } |
230 | | |
231 | | template <size_t L> |
232 | | inline ByteString |
233 | | RFC822Name(const char (&bytes)[L]) |
234 | 195 | { |
235 | 195 | return RFC822Name(ByteString(reinterpret_cast<const uint8_t*>(&bytes), |
236 | 195 | L - 1)); |
237 | 195 | } std::__1::basic_string<unsigned char, std::__1::char_traits<unsigned char>, std::__1::allocator<unsigned char> > mozilla::pkix::test::RFC822Name<16ul>(char const (&) [16ul]) Line | Count | Source | 234 | 33 | { | 235 | 33 | return RFC822Name(ByteString(reinterpret_cast<const uint8_t*>(&bytes), | 236 | 33 | L - 1)); | 237 | 33 | } |
std::__1::basic_string<unsigned char, std::__1::char_traits<unsigned char>, std::__1::allocator<unsigned char> > mozilla::pkix::test::RFC822Name<2ul>(char const (&) [2ul]) Line | Count | Source | 234 | 12 | { | 235 | 12 | return RFC822Name(ByteString(reinterpret_cast<const uint8_t*>(&bytes), | 236 | 12 | L - 1)); | 237 | 12 | } |
std::__1::basic_string<unsigned char, std::__1::char_traits<unsigned char>, std::__1::allocator<unsigned char> > mozilla::pkix::test::RFC822Name<19ul>(char const (&) [19ul]) Line | Count | Source | 234 | 6 | { | 235 | 6 | return RFC822Name(ByteString(reinterpret_cast<const uint8_t*>(&bytes), | 236 | 6 | L - 1)); | 237 | 6 | } |
std::__1::basic_string<unsigned char, std::__1::char_traits<unsigned char>, std::__1::allocator<unsigned char> > mozilla::pkix::test::RFC822Name<17ul>(char const (&) [17ul]) Line | Count | Source | 234 | 12 | { | 235 | 12 | return RFC822Name(ByteString(reinterpret_cast<const uint8_t*>(&bytes), | 236 | 12 | L - 1)); | 237 | 12 | } |
std::__1::basic_string<unsigned char, std::__1::char_traits<unsigned char>, std::__1::allocator<unsigned char> > mozilla::pkix::test::RFC822Name<20ul>(char const (&) [20ul]) Line | Count | Source | 234 | 3 | { | 235 | 3 | return RFC822Name(ByteString(reinterpret_cast<const uint8_t*>(&bytes), | 236 | 3 | L - 1)); | 237 | 3 | } |
std::__1::basic_string<unsigned char, std::__1::char_traits<unsigned char>, std::__1::allocator<unsigned char> > mozilla::pkix::test::RFC822Name<23ul>(char const (&) [23ul]) Line | Count | Source | 234 | 3 | { | 235 | 3 | return RFC822Name(ByteString(reinterpret_cast<const uint8_t*>(&bytes), | 236 | 3 | L - 1)); | 237 | 3 | } |
std::__1::basic_string<unsigned char, std::__1::char_traits<unsigned char>, std::__1::allocator<unsigned char> > mozilla::pkix::test::RFC822Name<12ul>(char const (&) [12ul]) Line | Count | Source | 234 | 12 | { | 235 | 12 | return RFC822Name(ByteString(reinterpret_cast<const uint8_t*>(&bytes), | 236 | 12 | L - 1)); | 237 | 12 | } |
std::__1::basic_string<unsigned char, std::__1::char_traits<unsigned char>, std::__1::allocator<unsigned char> > mozilla::pkix::test::RFC822Name<18ul>(char const (&) [18ul]) Line | Count | Source | 234 | 6 | { | 235 | 6 | return RFC822Name(ByteString(reinterpret_cast<const uint8_t*>(&bytes), | 236 | 6 | L - 1)); | 237 | 6 | } |
std::__1::basic_string<unsigned char, std::__1::char_traits<unsigned char>, std::__1::allocator<unsigned char> > mozilla::pkix::test::RFC822Name<13ul>(char const (&) [13ul]) Line | Count | Source | 234 | 36 | { | 235 | 36 | return RFC822Name(ByteString(reinterpret_cast<const uint8_t*>(&bytes), | 236 | 36 | L - 1)); | 237 | 36 | } |
std::__1::basic_string<unsigned char, std::__1::char_traits<unsigned char>, std::__1::allocator<unsigned char> > mozilla::pkix::test::RFC822Name<15ul>(char const (&) [15ul]) Line | Count | Source | 234 | 18 | { | 235 | 18 | return RFC822Name(ByteString(reinterpret_cast<const uint8_t*>(&bytes), | 236 | 18 | L - 1)); | 237 | 18 | } |
std::__1::basic_string<unsigned char, std::__1::char_traits<unsigned char>, std::__1::allocator<unsigned char> > mozilla::pkix::test::RFC822Name<14ul>(char const (&) [14ul]) Line | Count | Source | 234 | 33 | { | 235 | 33 | return RFC822Name(ByteString(reinterpret_cast<const uint8_t*>(&bytes), | 236 | 33 | L - 1)); | 237 | 33 | } |
std::__1::basic_string<unsigned char, std::__1::char_traits<unsigned char>, std::__1::allocator<unsigned char> > mozilla::pkix::test::RFC822Name<1ul>(char const (&) [1ul]) Line | Count | Source | 234 | 6 | { | 235 | 6 | return RFC822Name(ByteString(reinterpret_cast<const uint8_t*>(&bytes), | 236 | 6 | L - 1)); | 237 | 6 | } |
std::__1::basic_string<unsigned char, std::__1::char_traits<unsigned char>, std::__1::allocator<unsigned char> > mozilla::pkix::test::RFC822Name<30ul>(char const (&) [30ul]) Line | Count | Source | 234 | 3 | { | 235 | 3 | return RFC822Name(ByteString(reinterpret_cast<const uint8_t*>(&bytes), | 236 | 3 | L - 1)); | 237 | 3 | } |
std::__1::basic_string<unsigned char, std::__1::char_traits<unsigned char>, std::__1::allocator<unsigned char> > mozilla::pkix::test::RFC822Name<28ul>(char const (&) [28ul]) Line | Count | Source | 234 | 6 | { | 235 | 6 | return RFC822Name(ByteString(reinterpret_cast<const uint8_t*>(&bytes), | 236 | 6 | L - 1)); | 237 | 6 | } |
std::__1::basic_string<unsigned char, std::__1::char_traits<unsigned char>, std::__1::allocator<unsigned char> > mozilla::pkix::test::RFC822Name<32ul>(char const (&) [32ul]) Line | Count | Source | 234 | 3 | { | 235 | 3 | return RFC822Name(ByteString(reinterpret_cast<const uint8_t*>(&bytes), | 236 | 3 | L - 1)); | 237 | 3 | } |
std::__1::basic_string<unsigned char, std::__1::char_traits<unsigned char>, std::__1::allocator<unsigned char> > mozilla::pkix::test::RFC822Name<29ul>(char const (&) [29ul]) Line | Count | Source | 234 | 3 | { | 235 | 3 | return RFC822Name(ByteString(reinterpret_cast<const uint8_t*>(&bytes), | 236 | 3 | L - 1)); | 237 | 3 | } |
|
238 | | |
239 | | inline ByteString |
240 | | DNSName(const ByteString& name) |
241 | 348 | { |
242 | 348 | // (2 << 6) means "context-specific", 2 is the GeneralName tag. |
243 | 348 | return TLV((2 << 6) | 2, name); |
244 | 348 | } |
245 | | |
246 | | template <size_t L> |
247 | | inline ByteString |
248 | | DNSName(const char (&bytes)[L]) |
249 | 336 | { |
250 | 336 | return DNSName(ByteString(reinterpret_cast<const uint8_t*>(&bytes), |
251 | 336 | L - 1)); |
252 | 336 | } std::__1::basic_string<unsigned char, std::__1::char_traits<unsigned char>, std::__1::allocator<unsigned char> > mozilla::pkix::test::DNSName<12ul>(char const (&) [12ul]) Line | Count | Source | 249 | 87 | { | 250 | 87 | return DNSName(ByteString(reinterpret_cast<const uint8_t*>(&bytes), | 251 | 87 | L - 1)); | 252 | 87 | } |
std::__1::basic_string<unsigned char, std::__1::char_traits<unsigned char>, std::__1::allocator<unsigned char> > mozilla::pkix::test::DNSName<2ul>(char const (&) [2ul]) Line | Count | Source | 249 | 75 | { | 250 | 75 | return DNSName(ByteString(reinterpret_cast<const uint8_t*>(&bytes), | 251 | 75 | L - 1)); | 252 | 75 | } |
std::__1::basic_string<unsigned char, std::__1::char_traits<unsigned char>, std::__1::allocator<unsigned char> > mozilla::pkix::test::DNSName<14ul>(char const (&) [14ul]) Line | Count | Source | 249 | 99 | { | 250 | 99 | return DNSName(ByteString(reinterpret_cast<const uint8_t*>(&bytes), | 251 | 99 | L - 1)); | 252 | 99 | } |
std::__1::basic_string<unsigned char, std::__1::char_traits<unsigned char>, std::__1::allocator<unsigned char> > mozilla::pkix::test::DNSName<30ul>(char const (&) [30ul]) Line | Count | Source | 249 | 6 | { | 250 | 6 | return DNSName(ByteString(reinterpret_cast<const uint8_t*>(&bytes), | 251 | 6 | L - 1)); | 252 | 6 | } |
std::__1::basic_string<unsigned char, std::__1::char_traits<unsigned char>, std::__1::allocator<unsigned char> > mozilla::pkix::test::DNSName<17ul>(char const (&) [17ul]) Line | Count | Source | 249 | 12 | { | 250 | 12 | return DNSName(ByteString(reinterpret_cast<const uint8_t*>(&bytes), | 251 | 12 | L - 1)); | 252 | 12 | } |
std::__1::basic_string<unsigned char, std::__1::char_traits<unsigned char>, std::__1::allocator<unsigned char> > mozilla::pkix::test::DNSName<18ul>(char const (&) [18ul]) Line | Count | Source | 249 | 3 | { | 250 | 3 | return DNSName(ByteString(reinterpret_cast<const uint8_t*>(&bytes), | 251 | 3 | L - 1)); | 252 | 3 | } |
std::__1::basic_string<unsigned char, std::__1::char_traits<unsigned char>, std::__1::allocator<unsigned char> > mozilla::pkix::test::DNSName<21ul>(char const (&) [21ul]) Line | Count | Source | 249 | 3 | { | 250 | 3 | return DNSName(ByteString(reinterpret_cast<const uint8_t*>(&bytes), | 251 | 3 | L - 1)); | 252 | 3 | } |
std::__1::basic_string<unsigned char, std::__1::char_traits<unsigned char>, std::__1::allocator<unsigned char> > mozilla::pkix::test::DNSName<15ul>(char const (&) [15ul]) Line | Count | Source | 249 | 3 | { | 250 | 3 | return DNSName(ByteString(reinterpret_cast<const uint8_t*>(&bytes), | 251 | 3 | L - 1)); | 252 | 3 | } |
std::__1::basic_string<unsigned char, std::__1::char_traits<unsigned char>, std::__1::allocator<unsigned char> > mozilla::pkix::test::DNSName<16ul>(char const (&) [16ul]) Line | Count | Source | 249 | 6 | { | 250 | 6 | return DNSName(ByteString(reinterpret_cast<const uint8_t*>(&bytes), | 251 | 6 | L - 1)); | 252 | 6 | } |
std::__1::basic_string<unsigned char, std::__1::char_traits<unsigned char>, std::__1::allocator<unsigned char> > mozilla::pkix::test::DNSName<13ul>(char const (&) [13ul]) Line | Count | Source | 249 | 24 | { | 250 | 24 | return DNSName(ByteString(reinterpret_cast<const uint8_t*>(&bytes), | 251 | 24 | L - 1)); | 252 | 24 | } |
std::__1::basic_string<unsigned char, std::__1::char_traits<unsigned char>, std::__1::allocator<unsigned char> > mozilla::pkix::test::DNSName<1ul>(char const (&) [1ul]) Line | Count | Source | 249 | 6 | { | 250 | 6 | return DNSName(ByteString(reinterpret_cast<const uint8_t*>(&bytes), | 251 | 6 | L - 1)); | 252 | 6 | } |
std::__1::basic_string<unsigned char, std::__1::char_traits<unsigned char>, std::__1::allocator<unsigned char> > mozilla::pkix::test::DNSName<28ul>(char const (&) [28ul]) Line | Count | Source | 249 | 12 | { | 250 | 12 | return DNSName(ByteString(reinterpret_cast<const uint8_t*>(&bytes), | 251 | 12 | L - 1)); | 252 | 12 | } |
|
253 | | |
254 | | inline ByteString |
255 | | DirectoryName(const ByteString& name) |
256 | 54 | { |
257 | 54 | // (2 << 6) means "context-specific", (1 << 5) means "constructed", and 4 is |
258 | 54 | // the DirectoryName tag. |
259 | 54 | return TLV((2 << 6) | (1 << 5) | 4, name); |
260 | 54 | } |
261 | | |
262 | | inline ByteString |
263 | | IPAddress() |
264 | 12 | { |
265 | 12 | // (2 << 6) means "context-specific", 7 is the GeneralName tag. |
266 | 12 | return TLV((2 << 6) | 7, ByteString()); |
267 | 12 | } |
268 | | |
269 | | template <size_t L> |
270 | | inline ByteString |
271 | | IPAddress(const uint8_t (&bytes)[L]) |
272 | 252 | { |
273 | 252 | // (2 << 6) means "context-specific", 7 is the GeneralName tag. |
274 | 252 | return TLV((2 << 6) | 7, ByteString(bytes, L)); |
275 | 252 | } std::__1::basic_string<unsigned char, std::__1::char_traits<unsigned char>, std::__1::allocator<unsigned char> > mozilla::pkix::test::IPAddress<4ul>(unsigned char const (&) [4ul]) Line | Count | Source | 272 | 69 | { | 273 | 69 | // (2 << 6) means "context-specific", 7 is the GeneralName tag. | 274 | 69 | return TLV((2 << 6) | 7, ByteString(bytes, L)); | 275 | 69 | } |
std::__1::basic_string<unsigned char, std::__1::char_traits<unsigned char>, std::__1::allocator<unsigned char> > mozilla::pkix::test::IPAddress<11ul>(unsigned char const (&) [11ul]) Line | Count | Source | 272 | 12 | { | 273 | 12 | // (2 << 6) means "context-specific", 7 is the GeneralName tag. | 274 | 12 | return TLV((2 << 6) | 7, ByteString(bytes, L)); | 275 | 12 | } |
std::__1::basic_string<unsigned char, std::__1::char_traits<unsigned char>, std::__1::allocator<unsigned char> > mozilla::pkix::test::IPAddress<16ul>(unsigned char const (&) [16ul]) Line | Count | Source | 272 | 45 | { | 273 | 45 | // (2 << 6) means "context-specific", 7 is the GeneralName tag. | 274 | 45 | return TLV((2 << 6) | 7, ByteString(bytes, L)); | 275 | 45 | } |
std::__1::basic_string<unsigned char, std::__1::char_traits<unsigned char>, std::__1::allocator<unsigned char> > mozilla::pkix::test::IPAddress<7ul>(unsigned char const (&) [7ul]) Line | Count | Source | 272 | 6 | { | 273 | 6 | // (2 << 6) means "context-specific", 7 is the GeneralName tag. | 274 | 6 | return TLV((2 << 6) | 7, ByteString(bytes, L)); | 275 | 6 | } |
std::__1::basic_string<unsigned char, std::__1::char_traits<unsigned char>, std::__1::allocator<unsigned char> > mozilla::pkix::test::IPAddress<9ul>(unsigned char const (&) [9ul]) Line | Count | Source | 272 | 6 | { | 273 | 6 | // (2 << 6) means "context-specific", 7 is the GeneralName tag. | 274 | 6 | return TLV((2 << 6) | 7, ByteString(bytes, L)); | 275 | 6 | } |
std::__1::basic_string<unsigned char, std::__1::char_traits<unsigned char>, std::__1::allocator<unsigned char> > mozilla::pkix::test::IPAddress<31ul>(unsigned char const (&) [31ul]) Line | Count | Source | 272 | 6 | { | 273 | 6 | // (2 << 6) means "context-specific", 7 is the GeneralName tag. | 274 | 6 | return TLV((2 << 6) | 7, ByteString(bytes, L)); | 275 | 6 | } |
std::__1::basic_string<unsigned char, std::__1::char_traits<unsigned char>, std::__1::allocator<unsigned char> > mozilla::pkix::test::IPAddress<33ul>(unsigned char const (&) [33ul]) Line | Count | Source | 272 | 6 | { | 273 | 6 | // (2 << 6) means "context-specific", 7 is the GeneralName tag. | 274 | 6 | return TLV((2 << 6) | 7, ByteString(bytes, L)); | 275 | 6 | } |
std::__1::basic_string<unsigned char, std::__1::char_traits<unsigned char>, std::__1::allocator<unsigned char> > mozilla::pkix::test::IPAddress<8ul>(unsigned char const (&) [8ul]) Line | Count | Source | 272 | 54 | { | 273 | 54 | // (2 << 6) means "context-specific", 7 is the GeneralName tag. | 274 | 54 | return TLV((2 << 6) | 7, ByteString(bytes, L)); | 275 | 54 | } |
std::__1::basic_string<unsigned char, std::__1::char_traits<unsigned char>, std::__1::allocator<unsigned char> > mozilla::pkix::test::IPAddress<32ul>(unsigned char const (&) [32ul]) Line | Count | Source | 272 | 30 | { | 273 | 30 | // (2 << 6) means "context-specific", 7 is the GeneralName tag. | 274 | 30 | return TLV((2 << 6) | 7, ByteString(bytes, L)); | 275 | 30 | } |
std::__1::basic_string<unsigned char, std::__1::char_traits<unsigned char>, std::__1::allocator<unsigned char> > mozilla::pkix::test::IPAddress<3ul>(unsigned char const (&) [3ul]) Line | Count | Source | 272 | 3 | { | 273 | 3 | // (2 << 6) means "context-specific", 7 is the GeneralName tag. | 274 | 3 | return TLV((2 << 6) | 7, ByteString(bytes, L)); | 275 | 3 | } |
std::__1::basic_string<unsigned char, std::__1::char_traits<unsigned char>, std::__1::allocator<unsigned char> > mozilla::pkix::test::IPAddress<5ul>(unsigned char const (&) [5ul]) Line | Count | Source | 272 | 6 | { | 273 | 6 | // (2 << 6) means "context-specific", 7 is the GeneralName tag. | 274 | 6 | return TLV((2 << 6) | 7, ByteString(bytes, L)); | 275 | 6 | } |
std::__1::basic_string<unsigned char, std::__1::char_traits<unsigned char>, std::__1::allocator<unsigned char> > mozilla::pkix::test::IPAddress<15ul>(unsigned char const (&) [15ul]) Line | Count | Source | 272 | 3 | { | 273 | 3 | // (2 << 6) means "context-specific", 7 is the GeneralName tag. | 274 | 3 | return TLV((2 << 6) | 7, ByteString(bytes, L)); | 275 | 3 | } |
std::__1::basic_string<unsigned char, std::__1::char_traits<unsigned char>, std::__1::allocator<unsigned char> > mozilla::pkix::test::IPAddress<17ul>(unsigned char const (&) [17ul]) Line | Count | Source | 272 | 6 | { | 273 | 6 | // (2 << 6) means "context-specific", 7 is the GeneralName tag. | 274 | 6 | return TLV((2 << 6) | 7, ByteString(bytes, L)); | 275 | 6 | } |
|
276 | | |
277 | | // Names should be zero or more GeneralNames, like DNSName and IPAddress return, |
278 | | // concatenated together. |
279 | | // |
280 | | // CreatedEncodedSubjectAltName(ByteString()) results in a SAN with an empty |
281 | | // sequence. CreateEmptyEncodedSubjectName() results in a SAN without any |
282 | | // sequence. |
283 | | ByteString CreateEncodedSubjectAltName(const ByteString& names); |
284 | | ByteString CreateEncodedEmptySubjectAltName(); |
285 | | |
286 | | class TestKeyPair |
287 | | { |
288 | | public: |
289 | 0 | virtual ~TestKeyPair() { } |
290 | | |
291 | | const TestPublicKeyAlgorithm publicKeyAlg; |
292 | | |
293 | | // The DER encoding of the entire SubjectPublicKeyInfo structure. This is |
294 | | // what is encoded in certificates. |
295 | | const ByteString subjectPublicKeyInfo; |
296 | | |
297 | | // The DER encoding of subjectPublicKeyInfo.subjectPublicKey. This is what is |
298 | | // hashed to create CertIDs for OCSP. |
299 | | const ByteString subjectPublicKey; |
300 | | |
301 | | virtual Result SignData(const ByteString& tbs, |
302 | | const TestSignatureAlgorithm& signatureAlgorithm, |
303 | | /*out*/ ByteString& signature) const = 0; |
304 | | |
305 | | virtual TestKeyPair* Clone() const = 0; |
306 | | protected: |
307 | | TestKeyPair(const TestPublicKeyAlgorithm& publicKeyAlg, const ByteString& spk); |
308 | | TestKeyPair(const TestKeyPair&) = delete; |
309 | | void operator=(const TestKeyPair&) = delete; |
310 | | }; |
311 | | |
312 | | TestKeyPair* CloneReusedKeyPair(); |
313 | | TestKeyPair* GenerateKeyPair(); |
314 | | TestKeyPair* GenerateDSSKeyPair(); |
315 | 0 | inline void DeleteTestKeyPair(TestKeyPair* keyPair) { delete keyPair; } |
316 | | typedef ScopedPtr<TestKeyPair, DeleteTestKeyPair> ScopedTestKeyPair; |
317 | | |
318 | | Result TestVerifyECDSASignedDigest(const SignedDigest& signedDigest, |
319 | | Input subjectPublicKeyInfo); |
320 | | Result TestVerifyRSAPKCS1SignedDigest(const SignedDigest& signedDigest, |
321 | | Input subjectPublicKeyInfo); |
322 | | Result TestDigestBuf(Input item, DigestAlgorithm digestAlg, |
323 | | /*out*/ uint8_t* digestBuf, size_t digestBufLen); |
324 | | |
325 | | // Replace one substring in item with another of the same length, but only if |
326 | | // the substring was found exactly once. The "same length" restriction is |
327 | | // useful for avoiding invalidating lengths encoded within the item. The |
328 | | // "only once" restriction is helpful for avoiding making accidental changes. |
329 | | // |
330 | | // The string to search for must be 8 or more bytes long so that it is |
331 | | // extremely unlikely that there will ever be any false positive matches |
332 | | // in digital signatures, keys, hashes, etc. |
333 | | Result TamperOnce(/*in/out*/ ByteString& item, const ByteString& from, |
334 | | const ByteString& to); |
335 | | |
336 | | /////////////////////////////////////////////////////////////////////////////// |
337 | | // Encode Certificates |
338 | | |
339 | | enum Version { v1 = 0, v2 = 1, v3 = 2 }; |
340 | | |
341 | | // signature is assumed to be the DER encoding of an AlgorithmIdentifer. It is |
342 | | // put into the signature field of the TBSCertificate. In most cases, it will |
343 | | // be the same as signatureAlgorithm, which is the algorithm actually used |
344 | | // to sign the certificate. |
345 | | // serialNumber is assumed to be the DER encoding of an INTEGER. |
346 | | // |
347 | | // If extensions is null, then no extensions will be encoded. Otherwise, |
348 | | // extensions must point to an array of ByteStrings, terminated with an empty |
349 | | // ByteString. (If the first item of the array is empty then an empty |
350 | | // Extensions sequence will be encoded.) |
351 | | ByteString CreateEncodedCertificate(long version, |
352 | | const TestSignatureAlgorithm& signature, |
353 | | const ByteString& serialNumber, |
354 | | const ByteString& issuerNameDER, |
355 | | time_t notBefore, time_t notAfter, |
356 | | const ByteString& subjectNameDER, |
357 | | const TestKeyPair& subjectKeyPair, |
358 | | /*optional*/ const ByteString* extensions, |
359 | | const TestKeyPair& issuerKeyPair, |
360 | | const TestSignatureAlgorithm& signatureAlgorithm); |
361 | | |
362 | | ByteString CreateEncodedSerialNumber(long value); |
363 | | |
364 | | enum class Critical { No = 0, Yes = 1 }; |
365 | | |
366 | | ByteString CreateEncodedBasicConstraints(bool isCA, |
367 | | /*optional in*/ const long* pathLenConstraint, |
368 | | Critical critical); |
369 | | |
370 | | // Creates a DER-encoded extKeyUsage extension with one EKU OID. |
371 | | ByteString CreateEncodedEKUExtension(Input eku, Critical critical); |
372 | | |
373 | | /////////////////////////////////////////////////////////////////////////////// |
374 | | // Encode OCSP responses |
375 | | |
376 | | class OCSPResponseExtension final |
377 | | { |
378 | | public: |
379 | | OCSPResponseExtension(); |
380 | | |
381 | | ByteString id; |
382 | | bool critical; |
383 | | ByteString value; |
384 | | OCSPResponseExtension* next; |
385 | | }; |
386 | | |
387 | | class OCSPResponseContext final |
388 | | { |
389 | | public: |
390 | | OCSPResponseContext(const CertID& certID, std::time_t time); |
391 | | |
392 | | const CertID& certID; |
393 | | // TODO(bug 980538): add a way to specify what certificates are included. |
394 | | |
395 | | // The fields below are in the order that they appear in an OCSP response. |
396 | | |
397 | | enum OCSPResponseStatus |
398 | | { |
399 | | successful = 0, |
400 | | malformedRequest = 1, |
401 | | internalError = 2, |
402 | | tryLater = 3, |
403 | | // 4 is not used |
404 | | sigRequired = 5, |
405 | | unauthorized = 6, |
406 | | }; |
407 | | uint8_t responseStatus; // an OCSPResponseStatus or an invalid value |
408 | | bool skipResponseBytes; // If true, don't include responseBytes |
409 | | |
410 | | // responderID |
411 | | ByteString signerNameDER; // If set, responderID will use the byName |
412 | | // form; otherwise responderID will use the |
413 | | // byKeyHash form. |
414 | | |
415 | | std::time_t producedAt; |
416 | | |
417 | | // SingleResponse extensions (for the certID given in the constructor). |
418 | | OCSPResponseExtension* singleExtensions; |
419 | | // ResponseData extensions. |
420 | | OCSPResponseExtension* responseExtensions; |
421 | | bool includeEmptyExtensions; // If true, include the extension wrapper |
422 | | // regardless of if there are any actual |
423 | | // extensions. |
424 | | ScopedTestKeyPair signerKeyPair; |
425 | | TestSignatureAlgorithm signatureAlgorithm; |
426 | | bool badSignature; // If true, alter the signature to fail verification |
427 | | const ByteString* certs; // optional; array terminated by an empty string |
428 | | |
429 | | // The following fields are on a per-SingleResponse basis. In the future we |
430 | | // may support including multiple SingleResponses per response. |
431 | | enum CertStatus |
432 | | { |
433 | | good = 0, |
434 | | revoked = 1, |
435 | | unknown = 2, |
436 | | }; |
437 | | uint8_t certStatus; // CertStatus or an invalid value |
438 | | std::time_t revocationTime; // For certStatus == revoked |
439 | | std::time_t thisUpdate; |
440 | | std::time_t nextUpdate; |
441 | | bool includeNextUpdate; |
442 | | }; |
443 | | |
444 | | ByteString CreateEncodedOCSPResponse(OCSPResponseContext& context); |
445 | | |
446 | | } } } // namespace mozilla::pkix::test |
447 | | |
448 | | #endif // mozilla_pkix_test_pkixtestutil_h |