1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
|
/* -*- 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 <string>
#include "gtest/gtest.h"
#include "nss_scoped_ptrs.h"
#include "cryptohi.h"
#include "secitem.h"
#include "secerr.h"
namespace nss_test {
class SignParamsTestF : public ::testing::Test {
protected:
ScopedPLArenaPool arena_;
ScopedSECKEYPrivateKey privk_;
ScopedSECKEYPublicKey pubk_;
ScopedSECKEYPrivateKey ecPrivk_;
ScopedSECKEYPublicKey ecPubk_;
void SetUp() {
arena_.reset(PORT_NewArena(2048));
SECKEYPublicKey *pubk;
SECKEYPrivateKey *privk = SECKEY_CreateRSAPrivateKey(1024, &pubk, NULL);
ASSERT_NE(nullptr, pubk);
pubk_.reset(pubk);
ASSERT_NE(nullptr, privk);
privk_.reset(privk);
SECKEYECParams ecParams = {siBuffer, NULL, 0};
SECOidData *oidData;
oidData = SECOID_FindOIDByTag(SEC_OID_CURVE25519);
ASSERT_NE(nullptr, oidData);
ASSERT_NE(nullptr,
SECITEM_AllocItem(NULL, &ecParams, (2 + oidData->oid.len)))
<< "Couldn't allocate memory for OID.";
ecParams.data[0] = SEC_ASN1_OBJECT_ID; /* we have to prepend 0x06 */
ecParams.data[1] = oidData->oid.len;
memcpy(ecParams.data + 2, oidData->oid.data, oidData->oid.len);
SECKEYPublicKey *ecPubk;
SECKEYPrivateKey *ecPrivk =
SECKEY_CreateECPrivateKey(&ecParams, &ecPubk, NULL);
ASSERT_NE(nullptr, ecPubk);
ecPubk_.reset(ecPubk);
ASSERT_NE(nullptr, ecPrivk);
ecPrivk_.reset(ecPrivk);
}
void CreatePssParams(SECKEYRSAPSSParams *params, SECOidTag hashAlgTag) {
PORT_Memset(params, 0, sizeof(SECKEYRSAPSSParams));
params->hashAlg = (SECAlgorithmID *)PORT_ArenaZAlloc(
arena_.get(), sizeof(SECAlgorithmID));
ASSERT_NE(nullptr, params->hashAlg);
SECStatus rv =
SECOID_SetAlgorithmID(arena_.get(), params->hashAlg, hashAlgTag, NULL);
ASSERT_EQ(SECSuccess, rv);
}
void CreatePssParams(SECKEYRSAPSSParams *params, SECOidTag hashAlgTag,
SECOidTag maskHashAlgTag) {
CreatePssParams(params, hashAlgTag);
SECAlgorithmID maskHashAlg;
PORT_Memset(&maskHashAlg, 0, sizeof(maskHashAlg));
SECStatus rv =
SECOID_SetAlgorithmID(arena_.get(), &maskHashAlg, maskHashAlgTag, NULL);
ASSERT_EQ(SECSuccess, rv);
SECItem *maskHashAlgItem =
SEC_ASN1EncodeItem(arena_.get(), NULL, &maskHashAlg,
SEC_ASN1_GET(SECOID_AlgorithmIDTemplate));
params->maskAlg = (SECAlgorithmID *)PORT_ArenaZAlloc(
arena_.get(), sizeof(SECAlgorithmID));
ASSERT_NE(nullptr, params->maskAlg);
rv = SECOID_SetAlgorithmID(arena_.get(), params->maskAlg,
SEC_OID_PKCS1_MGF1, maskHashAlgItem);
ASSERT_EQ(SECSuccess, rv);
}
void CreatePssParams(SECKEYRSAPSSParams *params, SECOidTag hashAlgTag,
SECOidTag maskHashAlgTag, unsigned long saltLength) {
CreatePssParams(params, hashAlgTag, maskHashAlgTag);
SECItem *saltLengthItem =
SEC_ASN1EncodeInteger(arena_.get(), ¶ms->saltLength, saltLength);
ASSERT_EQ(¶ms->saltLength, saltLengthItem);
}
void CheckHashAlg(SECKEYRSAPSSParams *params, SECOidTag hashAlgTag) {
// If hash algorithm is SHA-1, it must be omitted in the parameters
if (hashAlgTag == SEC_OID_SHA1) {
EXPECT_EQ(nullptr, params->hashAlg);
} else {
EXPECT_NE(nullptr, params->hashAlg);
EXPECT_EQ(hashAlgTag, SECOID_GetAlgorithmTag(params->hashAlg));
}
}
void CheckMaskAlg(SECKEYRSAPSSParams *params, SECOidTag hashAlgTag) {
SECStatus rv;
// If hash algorithm is SHA-1, it must be omitted in the parameters
if (hashAlgTag == SEC_OID_SHA1)
EXPECT_EQ(nullptr, params->hashAlg);
else {
EXPECT_NE(nullptr, params->maskAlg);
EXPECT_EQ(SEC_OID_PKCS1_MGF1, SECOID_GetAlgorithmTag(params->maskAlg));
SECAlgorithmID hashAlg;
rv = SEC_QuickDERDecodeItem(arena_.get(), &hashAlg,
SEC_ASN1_GET(SECOID_AlgorithmIDTemplate),
¶ms->maskAlg->parameters);
ASSERT_EQ(SECSuccess, rv);
EXPECT_EQ(hashAlgTag, SECOID_GetAlgorithmTag(&hashAlg));
}
}
void CheckSaltLength(SECKEYRSAPSSParams *params, SECOidTag hashAlg) {
// If the salt length parameter is missing, that means it is 20 (default)
if (!params->saltLength.data) {
return;
}
unsigned long value;
SECStatus rv = SEC_ASN1DecodeInteger(¶ms->saltLength, &value);
ASSERT_EQ(SECSuccess, rv);
// The salt length are usually the same as the hash length,
// except for the case where the hash length exceeds the limit
// set by the key length
switch (hashAlg) {
case SEC_OID_SHA1:
EXPECT_EQ(20UL, value);
break;
case SEC_OID_SHA224:
EXPECT_EQ(28UL, value);
break;
case SEC_OID_SHA256:
EXPECT_EQ(32UL, value);
break;
case SEC_OID_SHA384:
EXPECT_EQ(48UL, value);
break;
case SEC_OID_SHA512:
// Truncated from 64, because our private key is 1024-bit
EXPECT_EQ(62UL, value);
break;
default:
FAIL();
}
}
};
class SignParamsTest
: public SignParamsTestF,
public ::testing::WithParamInterface<std::tuple<SECOidTag, SECOidTag>> {};
class SignParamsSourceTest : public SignParamsTestF,
public ::testing::WithParamInterface<SECOidTag> {};
TEST_P(SignParamsTest, CreateRsa) {
SECOidTag hashAlg = std::get<0>(GetParam());
SECOidTag srcHashAlg = std::get<1>(GetParam());
SECItem *srcParams;
if (srcHashAlg != SEC_OID_UNKNOWN) {
SECKEYRSAPSSParams pssParams;
ASSERT_NO_FATAL_FAILURE(
CreatePssParams(&pssParams, srcHashAlg, srcHashAlg));
srcParams = SEC_ASN1EncodeItem(arena_.get(), nullptr, &pssParams,
SEC_ASN1_GET(SECKEY_RSAPSSParamsTemplate));
ASSERT_NE(nullptr, srcParams);
} else {
srcParams = NULL;
}
SECItem *params = SEC_CreateSignatureAlgorithmParameters(
arena_.get(), nullptr, SEC_OID_PKCS1_RSA_ENCRYPTION, hashAlg, srcParams,
privk_.get());
// PKCS#1 RSA actually doesn't take any parameters, but if it is
// given, return a copy of it
if (srcHashAlg != SEC_OID_UNKNOWN) {
EXPECT_EQ(srcParams->len, params->len);
EXPECT_EQ(0, memcmp(params->data, srcParams->data, srcParams->len));
} else {
EXPECT_EQ(nullptr, params);
}
}
TEST_P(SignParamsTest, CreateRsaPss) {
SECOidTag hashAlg = std::get<0>(GetParam());
SECOidTag srcHashAlg = std::get<1>(GetParam());
SECItem *srcParams;
if (srcHashAlg != SEC_OID_UNKNOWN) {
SECKEYRSAPSSParams pssParams;
ASSERT_NO_FATAL_FAILURE(
CreatePssParams(&pssParams, srcHashAlg, srcHashAlg));
srcParams = SEC_ASN1EncodeItem(arena_.get(), nullptr, &pssParams,
SEC_ASN1_GET(SECKEY_RSAPSSParamsTemplate));
ASSERT_NE(nullptr, srcParams);
} else {
srcParams = NULL;
}
SECItem *params = SEC_CreateSignatureAlgorithmParameters(
arena_.get(), nullptr, SEC_OID_PKCS1_RSA_PSS_SIGNATURE, hashAlg,
srcParams, privk_.get());
if (hashAlg != SEC_OID_UNKNOWN && srcHashAlg != SEC_OID_UNKNOWN &&
hashAlg != srcHashAlg) {
EXPECT_EQ(nullptr, params);
return;
}
EXPECT_NE(nullptr, params);
SECKEYRSAPSSParams pssParams;
PORT_Memset(&pssParams, 0, sizeof(pssParams));
SECStatus rv =
SEC_QuickDERDecodeItem(arena_.get(), &pssParams,
SEC_ASN1_GET(SECKEY_RSAPSSParamsTemplate), params);
ASSERT_EQ(SECSuccess, rv);
if (hashAlg == SEC_OID_UNKNOWN) {
if (!pssParams.hashAlg) {
hashAlg = SEC_OID_SHA1;
} else {
hashAlg = SECOID_GetAlgorithmTag(pssParams.hashAlg);
}
if (srcHashAlg == SEC_OID_UNKNOWN) {
// If both hashAlg and srcHashAlg is unset, NSS will decide the hash
// algorithm based on the key length; in this case it's SHA256
EXPECT_EQ(SEC_OID_SHA256, hashAlg);
} else {
EXPECT_EQ(srcHashAlg, hashAlg);
}
}
ASSERT_NO_FATAL_FAILURE(CheckHashAlg(&pssParams, hashAlg));
ASSERT_NO_FATAL_FAILURE(CheckMaskAlg(&pssParams, hashAlg));
ASSERT_NO_FATAL_FAILURE(CheckSaltLength(&pssParams, hashAlg));
// The default trailer field (1) must be omitted
EXPECT_EQ(nullptr, pssParams.trailerField.data);
}
TEST_P(SignParamsTest, CreateRsaPssWithECPrivateKey) {
SECOidTag hashAlg = std::get<0>(GetParam());
SECOidTag srcHashAlg = std::get<1>(GetParam());
SECItem *srcParams;
if (srcHashAlg != SEC_OID_UNKNOWN) {
SECKEYRSAPSSParams pssParams;
ASSERT_NO_FATAL_FAILURE(
CreatePssParams(&pssParams, srcHashAlg, srcHashAlg));
srcParams = SEC_ASN1EncodeItem(arena_.get(), nullptr, &pssParams,
SEC_ASN1_GET(SECKEY_RSAPSSParamsTemplate));
ASSERT_NE(nullptr, srcParams);
} else {
srcParams = NULL;
}
SECItem *params = SEC_CreateSignatureAlgorithmParameters(
arena_.get(), nullptr, SEC_OID_PKCS1_RSA_PSS_SIGNATURE, hashAlg,
srcParams, ecPrivk_.get());
EXPECT_EQ(nullptr, params);
}
TEST_P(SignParamsTest, CreateRsaPssWithInvalidHashAlg) {
SECOidTag srcHashAlg = std::get<1>(GetParam());
SECItem *srcParams;
if (srcHashAlg != SEC_OID_UNKNOWN) {
SECKEYRSAPSSParams pssParams;
ASSERT_NO_FATAL_FAILURE(
CreatePssParams(&pssParams, srcHashAlg, srcHashAlg));
srcParams = SEC_ASN1EncodeItem(arena_.get(), nullptr, &pssParams,
SEC_ASN1_GET(SECKEY_RSAPSSParamsTemplate));
ASSERT_NE(nullptr, srcParams);
} else {
srcParams = NULL;
}
SECItem *params = SEC_CreateSignatureAlgorithmParameters(
arena_.get(), nullptr, SEC_OID_PKCS1_RSA_PSS_SIGNATURE, SEC_OID_MD5,
srcParams, privk_.get());
EXPECT_EQ(nullptr, params);
}
TEST_P(SignParamsSourceTest, CreateRsaPssWithInvalidHashAlg) {
SECOidTag hashAlg = GetParam();
SECItem *srcParams;
SECKEYRSAPSSParams pssParams;
ASSERT_NO_FATAL_FAILURE(
CreatePssParams(&pssParams, SEC_OID_MD5, SEC_OID_MD5));
srcParams = SEC_ASN1EncodeItem(arena_.get(), nullptr, &pssParams,
SEC_ASN1_GET(SECKEY_RSAPSSParamsTemplate));
ASSERT_NE(nullptr, srcParams);
SECItem *params = SEC_CreateSignatureAlgorithmParameters(
arena_.get(), nullptr, SEC_OID_PKCS1_RSA_PSS_SIGNATURE, hashAlg,
srcParams, privk_.get());
EXPECT_EQ(nullptr, params);
}
TEST_P(SignParamsSourceTest, CreateRsaPssWithInvalidSaltLength) {
SECOidTag hashAlg = GetParam();
SECItem *srcParams;
SECKEYRSAPSSParams pssParams;
ASSERT_NO_FATAL_FAILURE(
CreatePssParams(&pssParams, SEC_OID_SHA512, SEC_OID_SHA512, 100));
srcParams = SEC_ASN1EncodeItem(arena_.get(), nullptr, &pssParams,
SEC_ASN1_GET(SECKEY_RSAPSSParamsTemplate));
ASSERT_NE(nullptr, srcParams);
SECItem *params = SEC_CreateSignatureAlgorithmParameters(
arena_.get(), nullptr, SEC_OID_PKCS1_RSA_PSS_SIGNATURE, hashAlg,
srcParams, privk_.get());
EXPECT_EQ(nullptr, params);
}
TEST_P(SignParamsSourceTest, CreateRsaPssWithHashMismatch) {
SECOidTag hashAlg = GetParam();
SECItem *srcParams;
SECKEYRSAPSSParams pssParams;
ASSERT_NO_FATAL_FAILURE(
CreatePssParams(&pssParams, SEC_OID_SHA256, SEC_OID_SHA512));
srcParams = SEC_ASN1EncodeItem(arena_.get(), nullptr, &pssParams,
SEC_ASN1_GET(SECKEY_RSAPSSParamsTemplate));
ASSERT_NE(nullptr, srcParams);
SECItem *params = SEC_CreateSignatureAlgorithmParameters(
arena_.get(), nullptr, SEC_OID_PKCS1_RSA_PSS_SIGNATURE, hashAlg,
srcParams, privk_.get());
EXPECT_EQ(nullptr, params);
}
INSTANTIATE_TEST_CASE_P(
SignParamsTestCases, SignParamsTest,
::testing::Combine(::testing::Values(SEC_OID_UNKNOWN, SEC_OID_SHA1,
SEC_OID_SHA224, SEC_OID_SHA256,
SEC_OID_SHA384, SEC_OID_SHA512),
::testing::Values(SEC_OID_UNKNOWN, SEC_OID_SHA1,
SEC_OID_SHA224, SEC_OID_SHA256,
SEC_OID_SHA384, SEC_OID_SHA512)));
INSTANTIATE_TEST_CASE_P(SignParamsSourceTestCases, SignParamsSourceTest,
::testing::Values(SEC_OID_UNKNOWN, SEC_OID_SHA1,
SEC_OID_SHA224, SEC_OID_SHA256,
SEC_OID_SHA384, SEC_OID_SHA512));
} // namespace nss_test
|