@@ -1122,57 +1122,189 @@ const sec1EncExp = (cipher) => getRegExpForPEM('EC PRIVATE KEY', cipher);
1122
1122
}
1123
1123
}
1124
1124
1125
+ function addNumericalSeparator ( val ) {
1126
+ val = String ( val ) ;
1127
+ let res = '' ;
1128
+ let i = val . length ;
1129
+ const start = val [ 0 ] === '-' ? 1 : 0 ;
1130
+ for ( ; i >= start + 4 ; i -= 3 ) {
1131
+ res = `_${ val . slice ( i - 3 , i ) } ${ res } ` ;
1132
+ }
1133
+ return `${ val . slice ( 0 , i ) } ${ res } ` ;
1134
+ }
1135
+
1136
+
1125
1137
// Test RSA parameters.
1126
1138
{
1127
- // Test invalid modulus lengths.
1128
- for ( const modulusLength of [ undefined , null , 'a' , true , { } , [ ] , 512.1 , - 1 ] ) {
1139
+ // Test invalid modulus lengths. (non-number)
1140
+ for ( const modulusLength of [ undefined , null , 'a' , true , { } , [ ] ] ) {
1129
1141
assert . throws ( ( ) => generateKeyPair ( 'rsa' , {
1130
1142
modulusLength
1131
1143
} , common . mustNotCall ( ) ) , {
1132
1144
name : 'TypeError' ,
1133
- code : 'ERR_INVALID_ARG_VALUE' ,
1134
- message : "The property 'options.modulusLength' is invalid. " +
1145
+ code : 'ERR_INVALID_ARG_TYPE' ,
1146
+ message :
1147
+ 'The "options.modulusLength" property must be of type number.' +
1148
+ common . invalidArgTypeHelper ( modulusLength )
1149
+ } ) ;
1150
+ }
1151
+
1152
+ // Test invalid modulus lengths. (non-integer)
1153
+ for ( const modulusLength of [ 512.1 , 1.3 , 1.1 , 5000.9 , 100.5 ] ) {
1154
+ assert . throws ( ( ) => generateKeyPair ( 'rsa' , {
1155
+ modulusLength
1156
+ } , common . mustNotCall ( ) ) , {
1157
+ name : 'RangeError' ,
1158
+ code : 'ERR_OUT_OF_RANGE' ,
1159
+ message :
1160
+ 'The value of "options.modulusLength" is out of range. ' +
1161
+ 'It must be an integer. ' +
1135
1162
`Received ${ inspect ( modulusLength ) } `
1136
1163
} ) ;
1137
1164
}
1138
1165
1139
- // Test invalid exponents.
1140
- for ( const publicExponent of [ 'a' , true , { } , [ ] , 3.5 , - 1 ] ) {
1166
+ // Test invalid modulus lengths. (out of range)
1167
+ for ( const modulusLength of [ - 1 , - 9 , 4294967297 ] ) {
1168
+ assert . throws ( ( ) => generateKeyPair ( 'rsa' , {
1169
+ modulusLength
1170
+ } , common . mustNotCall ( ) ) , {
1171
+ name : 'RangeError' ,
1172
+ code : 'ERR_OUT_OF_RANGE' ,
1173
+ message :
1174
+ 'The value of "options.modulusLength" is out of range. ' +
1175
+ 'It must be >= 0 && < 4294967296. ' +
1176
+ `Received ${ addNumericalSeparator ( modulusLength ) } `
1177
+ } ) ;
1178
+ }
1179
+
1180
+ // Test invalid exponents. (non-number)
1181
+ for ( const publicExponent of [ 'a' , true , { } , [ ] ] ) {
1141
1182
assert . throws ( ( ) => generateKeyPair ( 'rsa' , {
1142
1183
modulusLength : 4096 ,
1143
1184
publicExponent
1144
1185
} , common . mustNotCall ( ) ) , {
1145
1186
name : 'TypeError' ,
1146
- code : 'ERR_INVALID_ARG_VALUE' ,
1147
- message : "The property 'options.publicExponent' is invalid. " +
1187
+ code : 'ERR_INVALID_ARG_TYPE' ,
1188
+ message :
1189
+ 'The "options.publicExponent" property must be of type number.' +
1190
+ common . invalidArgTypeHelper ( publicExponent )
1191
+ } ) ;
1192
+ }
1193
+
1194
+ // Test invalid exponents. (non-integer)
1195
+ for ( const publicExponent of [ 3.5 , 1.1 , 50.5 , 510.5 ] ) {
1196
+ assert . throws ( ( ) => generateKeyPair ( 'rsa' , {
1197
+ modulusLength : 4096 ,
1198
+ publicExponent
1199
+ } , common . mustNotCall ( ) ) , {
1200
+ name : 'RangeError' ,
1201
+ code : 'ERR_OUT_OF_RANGE' ,
1202
+ message :
1203
+ 'The value of "options.publicExponent" is out of range. ' +
1204
+ 'It must be an integer. ' +
1148
1205
`Received ${ inspect ( publicExponent ) } `
1149
1206
} ) ;
1150
1207
}
1208
+
1209
+ // Test invalid exponents. (out of range)
1210
+ for ( const publicExponent of [ - 5 , - 3 , 4294967297 ] ) {
1211
+ assert . throws ( ( ) => generateKeyPair ( 'rsa' , {
1212
+ modulusLength : 4096 ,
1213
+ publicExponent
1214
+ } , common . mustNotCall ( ) ) , {
1215
+ name : 'RangeError' ,
1216
+ code : 'ERR_OUT_OF_RANGE' ,
1217
+ message :
1218
+ 'The value of "options.publicExponent" is out of range. ' +
1219
+ 'It must be >= 0 && < 4294967296. ' +
1220
+ `Received ${ addNumericalSeparator ( publicExponent ) } `
1221
+ } ) ;
1222
+ }
1151
1223
}
1152
1224
1153
1225
// Test DSA parameters.
1154
1226
{
1155
- // Test invalid modulus lengths.
1156
- for ( const modulusLength of [ undefined , null , 'a' , true , { } , [ ] , 4096.1 ] ) {
1227
+ // Test invalid modulus lengths. (non-number)
1228
+ for ( const modulusLength of [ undefined , null , 'a' , true , { } , [ ] ] ) {
1157
1229
assert . throws ( ( ) => generateKeyPair ( 'dsa' , {
1158
1230
modulusLength
1159
1231
} , common . mustNotCall ( ) ) , {
1160
1232
name : 'TypeError' ,
1161
- code : 'ERR_INVALID_ARG_VALUE' ,
1162
- message : "The property 'options.modulusLength' is invalid. " +
1233
+ code : 'ERR_INVALID_ARG_TYPE' ,
1234
+ message :
1235
+ 'The "options.modulusLength" property must be of type number.' +
1236
+ common . invalidArgTypeHelper ( modulusLength )
1237
+ } ) ;
1238
+ }
1239
+
1240
+ // Test invalid modulus lengths. (non-integer)
1241
+ for ( const modulusLength of [ 512.1 , 1.3 , 1.1 , 5000.9 , 100.5 ] ) {
1242
+ assert . throws ( ( ) => generateKeyPair ( 'dsa' , {
1243
+ modulusLength
1244
+ } , common . mustNotCall ( ) ) , {
1245
+ name : 'RangeError' ,
1246
+ code : 'ERR_OUT_OF_RANGE' ,
1247
+ message :
1248
+ 'The value of "options.modulusLength" is out of range. ' +
1249
+ 'It must be an integer. ' +
1163
1250
`Received ${ inspect ( modulusLength ) } `
1164
1251
} ) ;
1165
1252
}
1166
1253
1167
- // Test invalid divisor lengths.
1168
- for ( const divisorLength of [ 'a' , true , { } , [ ] , 4096.1 , 2147483648 , - 1 ] ) {
1254
+ // Test invalid modulus lengths. (out of range)
1255
+ for ( const modulusLength of [ - 1 , - 9 , 4294967297 ] ) {
1256
+ assert . throws ( ( ) => generateKeyPair ( 'dsa' , {
1257
+ modulusLength
1258
+ } , common . mustNotCall ( ) ) , {
1259
+ name : 'RangeError' ,
1260
+ code : 'ERR_OUT_OF_RANGE' ,
1261
+ message :
1262
+ 'The value of "options.modulusLength" is out of range. ' +
1263
+ 'It must be >= 0 && < 4294967296. ' +
1264
+ `Received ${ addNumericalSeparator ( modulusLength ) } `
1265
+ } ) ;
1266
+ }
1267
+
1268
+ // Test invalid divisor lengths. (non-number)
1269
+ for ( const divisorLength of [ 'a' , true , { } , [ ] ] ) {
1169
1270
assert . throws ( ( ) => generateKeyPair ( 'dsa' , {
1170
1271
modulusLength : 2048 ,
1171
1272
divisorLength
1172
1273
} , common . mustNotCall ( ) ) , {
1173
1274
name : 'TypeError' ,
1174
- code : 'ERR_INVALID_ARG_VALUE' ,
1175
- message : "The property 'options.divisorLength' is invalid. " +
1275
+ code : 'ERR_INVALID_ARG_TYPE' ,
1276
+ message :
1277
+ 'The "options.divisorLength" property must be of type number.' +
1278
+ common . invalidArgTypeHelper ( divisorLength )
1279
+ } ) ;
1280
+ }
1281
+
1282
+ // Test invalid divisor lengths. (non-integer)
1283
+ for ( const divisorLength of [ 4096.1 , 5.1 , 6.9 , 9.5 ] ) {
1284
+ assert . throws ( ( ) => generateKeyPair ( 'dsa' , {
1285
+ modulusLength : 2048 ,
1286
+ divisorLength
1287
+ } , common . mustNotCall ( ) ) , {
1288
+ name : 'RangeError' ,
1289
+ code : 'ERR_OUT_OF_RANGE' ,
1290
+ message :
1291
+ 'The value of "options.divisorLength" is out of range. ' +
1292
+ 'It must be an integer. ' +
1293
+ `Received ${ inspect ( divisorLength ) } `
1294
+ } ) ;
1295
+ }
1296
+
1297
+ // Test invalid divisor lengths. (out of range)
1298
+ for ( const divisorLength of [ - 6 , - 9 , 2147483648 ] ) {
1299
+ assert . throws ( ( ) => generateKeyPair ( 'dsa' , {
1300
+ modulusLength : 2048 ,
1301
+ divisorLength
1302
+ } , common . mustNotCall ( ) ) , {
1303
+ name : 'RangeError' ,
1304
+ code : 'ERR_OUT_OF_RANGE' ,
1305
+ message :
1306
+ 'The value of "options.divisorLength" is out of range. ' +
1307
+ 'It must be >= 0 && <= 2147483647. ' +
1176
1308
`Received ${ inspect ( divisorLength ) } `
1177
1309
} ) ;
1178
1310
}
@@ -1202,9 +1334,10 @@ const sec1EncExp = (cipher) => getRegExpForPEM('EC PRIVATE KEY', cipher);
1202
1334
} ) ;
1203
1335
} , {
1204
1336
name : 'TypeError' ,
1205
- code : 'ERR_INVALID_ARG_VALUE' ,
1206
- message : "The property 'options.namedCurve' is invalid. " +
1207
- `Received ${ inspect ( namedCurve ) } `
1337
+ code : 'ERR_INVALID_ARG_TYPE' ,
1338
+ message :
1339
+ 'The "options.namedCurve" property must be of type string.' +
1340
+ common . invalidArgTypeHelper ( namedCurve )
1208
1341
} ) ;
1209
1342
}
1210
1343
@@ -1293,9 +1426,10 @@ const sec1EncExp = (cipher) => getRegExpForPEM('EC PRIVATE KEY', cipher);
1293
1426
primeLength : 2147483648
1294
1427
} , common . mustNotCall ( ) ) ;
1295
1428
} , {
1296
- name : 'TypeError' ,
1297
- code : 'ERR_INVALID_ARG_VALUE' ,
1298
- message : "The property 'options.primeLength' is invalid. " +
1429
+ name : 'RangeError' ,
1430
+ code : 'ERR_OUT_OF_RANGE' ,
1431
+ message : 'The value of "options.primeLength" is out of range. ' +
1432
+ 'It must be >= 0 && <= 2147483647. ' +
1299
1433
'Received 2147483648' ,
1300
1434
} ) ;
1301
1435
@@ -1304,9 +1438,10 @@ const sec1EncExp = (cipher) => getRegExpForPEM('EC PRIVATE KEY', cipher);
1304
1438
primeLength : - 1
1305
1439
} , common . mustNotCall ( ) ) ;
1306
1440
} , {
1307
- name : 'TypeError' ,
1308
- code : 'ERR_INVALID_ARG_VALUE' ,
1309
- message : "The property 'options.primeLength' is invalid. " +
1441
+ name : 'RangeError' ,
1442
+ code : 'ERR_OUT_OF_RANGE' ,
1443
+ message : 'The value of "options.primeLength" is out of range. ' +
1444
+ 'It must be >= 0 && <= 2147483647. ' +
1310
1445
'Received -1' ,
1311
1446
} ) ;
1312
1447
@@ -1316,9 +1451,10 @@ const sec1EncExp = (cipher) => getRegExpForPEM('EC PRIVATE KEY', cipher);
1316
1451
generator : 2147483648 ,
1317
1452
} , common . mustNotCall ( ) ) ;
1318
1453
} , {
1319
- name : 'TypeError' ,
1320
- code : 'ERR_INVALID_ARG_VALUE' ,
1321
- message : "The property 'options.generator' is invalid. " +
1454
+ name : 'RangeError' ,
1455
+ code : 'ERR_OUT_OF_RANGE' ,
1456
+ message : 'The value of "options.generator" is out of range. ' +
1457
+ 'It must be >= 0 && <= 2147483647. ' +
1322
1458
'Received 2147483648' ,
1323
1459
} ) ;
1324
1460
@@ -1328,9 +1464,10 @@ const sec1EncExp = (cipher) => getRegExpForPEM('EC PRIVATE KEY', cipher);
1328
1464
generator : - 1 ,
1329
1465
} , common . mustNotCall ( ) ) ;
1330
1466
} , {
1331
- name : 'TypeError' ,
1332
- code : 'ERR_INVALID_ARG_VALUE' ,
1333
- message : "The property 'options.generator' is invalid. " +
1467
+ name : 'RangeError' ,
1468
+ code : 'ERR_OUT_OF_RANGE' ,
1469
+ message : 'The value of "options.generator" is out of range. ' +
1470
+ 'It must be >= 0 && <= 2147483647. ' +
1334
1471
'Received -1' ,
1335
1472
} ) ;
1336
1473
@@ -1389,9 +1526,10 @@ const sec1EncExp = (cipher) => getRegExpForPEM('EC PRIVATE KEY', cipher);
1389
1526
} ) ;
1390
1527
} , {
1391
1528
name : 'TypeError' ,
1392
- code : 'ERR_INVALID_ARG_VALUE' ,
1393
- message : "The property 'options.hashAlgorithm' is invalid. " +
1394
- `Received ${ inspect ( hashValue ) } `
1529
+ code : 'ERR_INVALID_ARG_TYPE' ,
1530
+ message :
1531
+ 'The "options.hashAlgorithm" property must be of type string.' +
1532
+ common . invalidArgTypeHelper ( hashValue )
1395
1533
} ) ;
1396
1534
}
1397
1535
@@ -1404,9 +1542,10 @@ const sec1EncExp = (cipher) => getRegExpForPEM('EC PRIVATE KEY', cipher);
1404
1542
mgf1HashAlgorithm : 'sha256'
1405
1543
} , common . mustNotCall ( ) ) ;
1406
1544
} , {
1407
- name : 'TypeError' ,
1408
- code : 'ERR_INVALID_ARG_VALUE' ,
1409
- message : "The property 'options.saltLength' is invalid. " +
1545
+ name : 'RangeError' ,
1546
+ code : 'ERR_OUT_OF_RANGE' ,
1547
+ message : 'The value of "options.saltLength" is out of range. ' +
1548
+ 'It must be >= 0 && <= 2147483647. ' +
1410
1549
'Received 2147483648'
1411
1550
} ) ;
1412
1551
@@ -1418,9 +1557,10 @@ const sec1EncExp = (cipher) => getRegExpForPEM('EC PRIVATE KEY', cipher);
1418
1557
mgf1HashAlgorithm : 'sha256'
1419
1558
} , common . mustNotCall ( ) ) ;
1420
1559
} , {
1421
- name : 'TypeError' ,
1422
- code : 'ERR_INVALID_ARG_VALUE' ,
1423
- message : "The property 'options.saltLength' is invalid. " +
1560
+ name : 'RangeError' ,
1561
+ code : 'ERR_OUT_OF_RANGE' ,
1562
+ message : 'The value of "options.saltLength" is out of range. ' +
1563
+ 'It must be >= 0 && <= 2147483647. ' +
1424
1564
'Received -1'
1425
1565
} ) ;
1426
1566
@@ -1534,9 +1674,10 @@ const sec1EncExp = (cipher) => getRegExpForPEM('EC PRIVATE KEY', cipher);
1534
1674
} ,
1535
1675
{
1536
1676
name : 'TypeError' ,
1537
- code : 'ERR_INVALID_ARG_VALUE' ,
1538
- message : "The property 'options.mgf1HashAlgorithm' is invalid. " +
1539
- `Received ${ inspect ( mgf1HashAlgorithm ) } `
1677
+ code : 'ERR_INVALID_ARG_TYPE' ,
1678
+ message :
1679
+ 'The "options.mgf1HashAlgorithm" property must be of type string.' +
1680
+ common . invalidArgTypeHelper ( mgf1HashAlgorithm )
1540
1681
1541
1682
}
1542
1683
) ;
0 commit comments