@@ -1032,60 +1032,192 @@ const sec1EncExp = (cipher) => getRegExpForPEM('EC PRIVATE KEY', cipher);
1032
1032
}
1033
1033
}
1034
1034
1035
+ function addNumericalSeparator ( val ) {
1036
+ val = String ( val ) ;
1037
+ let res = '' ;
1038
+ let i = val . length ;
1039
+ const start = val [ 0 ] === '-' ? 1 : 0 ;
1040
+ for ( ; i >= start + 4 ; i -= 3 ) {
1041
+ res = `_${ val . slice ( i - 3 , i ) } ${ res } ` ;
1042
+ }
1043
+ return `${ val . slice ( 0 , i ) } ${ res } ` ;
1044
+ }
1045
+
1046
+
1035
1047
// Test RSA parameters.
1036
1048
{
1037
- // Test invalid modulus lengths.
1038
- for ( const modulusLength of [ undefined , null , 'a' , true , { } , [ ] , 512.1 , - 1 ] ) {
1049
+ // Test invalid modulus lengths. (non-number)
1050
+ for ( const modulusLength of [ undefined , null , 'a' , true , { } , [ ] ] ) {
1039
1051
assert . throws ( ( ) => generateKeyPair ( 'rsa' , {
1040
1052
modulusLength
1041
1053
} , common . mustNotCall ( ) ) , {
1042
1054
name : 'TypeError' ,
1043
- code : 'ERR_INVALID_ARG_VALUE' ,
1044
- message : "The property 'options.modulusLength' is invalid. " +
1055
+ code : 'ERR_INVALID_ARG_TYPE' ,
1056
+ message :
1057
+ 'The "options.modulusLength" property must be of type number.' +
1058
+ common . invalidArgTypeHelper ( modulusLength )
1059
+ } ) ;
1060
+ }
1061
+
1062
+ // Test invalid modulus lengths. (non-integer)
1063
+ for ( const modulusLength of [ 512.1 , 1.3 , 1.1 , 5000.9 , 100.5 ] ) {
1064
+ assert . throws ( ( ) => generateKeyPair ( 'rsa' , {
1065
+ modulusLength
1066
+ } , common . mustNotCall ( ) ) , {
1067
+ name : 'RangeError' ,
1068
+ code : 'ERR_OUT_OF_RANGE' ,
1069
+ message :
1070
+ 'The value of "options.modulusLength" is out of range. ' +
1071
+ 'It must be an integer. ' +
1045
1072
`Received ${ inspect ( modulusLength ) } `
1046
1073
} ) ;
1047
1074
}
1048
1075
1049
- // Test invalid exponents.
1050
- for ( const publicExponent of [ 'a' , true , { } , [ ] , 3.5 , - 1 ] ) {
1076
+ // Test invalid modulus lengths. (out of range)
1077
+ for ( const modulusLength of [ - 1 , - 9 , 4294967297 ] ) {
1078
+ assert . throws ( ( ) => generateKeyPair ( 'rsa' , {
1079
+ modulusLength
1080
+ } , common . mustNotCall ( ) ) , {
1081
+ name : 'RangeError' ,
1082
+ code : 'ERR_OUT_OF_RANGE' ,
1083
+ message :
1084
+ 'The value of "options.modulusLength" is out of range. ' +
1085
+ 'It must be >= 0 && < 4294967296. ' +
1086
+ `Received ${ addNumericalSeparator ( modulusLength ) } `
1087
+ } ) ;
1088
+ }
1089
+
1090
+ // Test invalid exponents. (non-number)
1091
+ for ( const publicExponent of [ 'a' , true , { } , [ ] ] ) {
1051
1092
assert . throws ( ( ) => generateKeyPair ( 'rsa' , {
1052
1093
modulusLength : 4096 ,
1053
1094
publicExponent
1054
1095
} , common . mustNotCall ( ) ) , {
1055
1096
name : 'TypeError' ,
1056
- code : 'ERR_INVALID_ARG_VALUE' ,
1057
- message : "The property 'options.publicExponent' is invalid. " +
1097
+ code : 'ERR_INVALID_ARG_TYPE' ,
1098
+ message :
1099
+ 'The "options.publicExponent" property must be of type number.' +
1100
+ common . invalidArgTypeHelper ( publicExponent )
1101
+ } ) ;
1102
+ }
1103
+
1104
+ // Test invalid exponents. (non-integer)
1105
+ for ( const publicExponent of [ 3.5 , 1.1 , 50.5 , 510.5 ] ) {
1106
+ assert . throws ( ( ) => generateKeyPair ( 'rsa' , {
1107
+ modulusLength : 4096 ,
1108
+ publicExponent
1109
+ } , common . mustNotCall ( ) ) , {
1110
+ name : 'RangeError' ,
1111
+ code : 'ERR_OUT_OF_RANGE' ,
1112
+ message :
1113
+ 'The value of "options.publicExponent" is out of range. ' +
1114
+ 'It must be an integer. ' +
1058
1115
`Received ${ inspect ( publicExponent ) } `
1059
1116
} ) ;
1060
1117
}
1118
+
1119
+ // Test invalid exponents. (out of range)
1120
+ for ( const publicExponent of [ - 5 , - 3 , 4294967297 ] ) {
1121
+ assert . throws ( ( ) => generateKeyPair ( 'rsa' , {
1122
+ modulusLength : 4096 ,
1123
+ publicExponent
1124
+ } , common . mustNotCall ( ) ) , {
1125
+ name : 'RangeError' ,
1126
+ code : 'ERR_OUT_OF_RANGE' ,
1127
+ message :
1128
+ 'The value of "options.publicExponent" is out of range. ' +
1129
+ 'It must be >= 0 && < 4294967296. ' +
1130
+ `Received ${ addNumericalSeparator ( publicExponent ) } `
1131
+ } ) ;
1132
+ }
1061
1133
}
1062
1134
1063
1135
// Test DSA parameters.
1064
1136
{
1065
- // Test invalid modulus lengths.
1066
- for ( const modulusLength of [ undefined , null , 'a' , true , { } , [ ] , 4096.1 ] ) {
1137
+ // Test invalid modulus lengths. (non-number)
1138
+ for ( const modulusLength of [ undefined , null , 'a' , true , { } , [ ] ] ) {
1067
1139
assert . throws ( ( ) => generateKeyPair ( 'dsa' , {
1068
1140
modulusLength
1069
1141
} , common . mustNotCall ( ) ) , {
1070
1142
name : 'TypeError' ,
1071
- code : 'ERR_INVALID_ARG_VALUE' ,
1072
- message : "The property 'options.modulusLength' is invalid. " +
1143
+ code : 'ERR_INVALID_ARG_TYPE' ,
1144
+ message :
1145
+ 'The "options.modulusLength" property must be of type number.' +
1146
+ common . invalidArgTypeHelper ( modulusLength )
1147
+ } ) ;
1148
+ }
1149
+
1150
+ // Test invalid modulus lengths. (non-integer)
1151
+ for ( const modulusLength of [ 512.1 , 1.3 , 1.1 , 5000.9 , 100.5 ] ) {
1152
+ assert . throws ( ( ) => generateKeyPair ( 'dsa' , {
1153
+ modulusLength
1154
+ } , common . mustNotCall ( ) ) , {
1155
+ name : 'RangeError' ,
1156
+ code : 'ERR_OUT_OF_RANGE' ,
1157
+ message :
1158
+ 'The value of "options.modulusLength" is out of range. ' +
1159
+ 'It must be an integer. ' +
1073
1160
`Received ${ inspect ( modulusLength ) } `
1074
1161
} ) ;
1075
1162
}
1076
1163
1077
- // Test invalid divisor lengths.
1078
- for ( const divisorLength of [ 'a' , true , { } , [ ] , 4096.1 , 2147483648 , - 1 ] ) {
1164
+ // Test invalid modulus lengths. (out of range)
1165
+ for ( const modulusLength of [ - 1 , - 9 , 4294967297 ] ) {
1166
+ assert . throws ( ( ) => generateKeyPair ( 'dsa' , {
1167
+ modulusLength
1168
+ } , common . mustNotCall ( ) ) , {
1169
+ name : 'RangeError' ,
1170
+ code : 'ERR_OUT_OF_RANGE' ,
1171
+ message :
1172
+ 'The value of "options.modulusLength" is out of range. ' +
1173
+ 'It must be >= 0 && < 4294967296. ' +
1174
+ `Received ${ addNumericalSeparator ( modulusLength ) } `
1175
+ } ) ;
1176
+ }
1177
+
1178
+ // Test invalid divisor lengths. (non-number)
1179
+ for ( const divisorLength of [ 'a' , true , { } , [ ] ] ) {
1079
1180
assert . throws ( ( ) => generateKeyPair ( 'dsa' , {
1080
1181
modulusLength : 2048 ,
1081
1182
divisorLength
1082
1183
} , common . mustNotCall ( ) ) , {
1083
1184
name : 'TypeError' ,
1084
- code : 'ERR_INVALID_ARG_VALUE' ,
1085
- message : "The property 'options.divisorLength' is invalid. " +
1185
+ code : 'ERR_INVALID_ARG_TYPE' ,
1186
+ message :
1187
+ 'The "options.divisorLength" property must be of type number.' +
1188
+ common . invalidArgTypeHelper ( divisorLength )
1189
+ } ) ;
1190
+ }
1191
+
1192
+ // Test invalid divisor lengths. (non-integer)
1193
+ for ( const divisorLength of [ 4096.1 , 5.1 , 6.9 , 9.5 ] ) {
1194
+ assert . throws ( ( ) => generateKeyPair ( 'dsa' , {
1195
+ modulusLength : 2048 ,
1196
+ divisorLength
1197
+ } , common . mustNotCall ( ) ) , {
1198
+ name : 'RangeError' ,
1199
+ code : 'ERR_OUT_OF_RANGE' ,
1200
+ message :
1201
+ 'The value of "options.divisorLength" is out of range. ' +
1202
+ 'It must be an integer. ' +
1086
1203
`Received ${ inspect ( divisorLength ) } `
1087
1204
} ) ;
1088
1205
}
1206
+
1207
+ // Test invalid divisor lengths. (out of range)
1208
+ for ( const divisorLength of [ - 6 , - 9 , 2147483648 ] ) {
1209
+ assert . throws ( ( ) => generateKeyPair ( 'dsa' , {
1210
+ modulusLength : 2048 ,
1211
+ divisorLength
1212
+ } , common . mustNotCall ( ) ) , {
1213
+ name : 'RangeError' ,
1214
+ code : 'ERR_OUT_OF_RANGE' ,
1215
+ message :
1216
+ 'The value of "options.divisorLength" is out of range. ' +
1217
+ 'It must be >= 0 && <= 2147483647. ' +
1218
+ `Received ${ addNumericalSeparator ( divisorLength ) } `
1219
+ } ) ;
1220
+ }
1089
1221
}
1090
1222
1091
1223
// Test EC parameters.
@@ -1112,9 +1244,10 @@ const sec1EncExp = (cipher) => getRegExpForPEM('EC PRIVATE KEY', cipher);
1112
1244
} ) ;
1113
1245
} , {
1114
1246
name : 'TypeError' ,
1115
- code : 'ERR_INVALID_ARG_VALUE' ,
1116
- message : "The property 'options.namedCurve' is invalid. " +
1117
- `Received ${ inspect ( namedCurve ) } `
1247
+ code : 'ERR_INVALID_ARG_TYPE' ,
1248
+ message :
1249
+ 'The "options.namedCurve" property must be of type string.' +
1250
+ common . invalidArgTypeHelper ( namedCurve )
1118
1251
} ) ;
1119
1252
}
1120
1253
@@ -1203,20 +1336,22 @@ const sec1EncExp = (cipher) => getRegExpForPEM('EC PRIVATE KEY', cipher);
1203
1336
primeLength : 2147483648
1204
1337
} , common . mustNotCall ( ) ) ;
1205
1338
} , {
1206
- name : 'TypeError' ,
1207
- code : 'ERR_INVALID_ARG_VALUE' ,
1208
- message : "The property 'options.primeLength' is invalid. " +
1209
- 'Received 2147483648' ,
1339
+ name : 'RangeError' ,
1340
+ code : 'ERR_OUT_OF_RANGE' ,
1341
+ message : 'The value of "options.primeLength" is out of range. ' +
1342
+ 'It must be >= 0 && <= 2147483647. ' +
1343
+ 'Received 2_147_483_648' ,
1210
1344
} ) ;
1211
1345
1212
1346
assert . throws ( ( ) => {
1213
1347
generateKeyPair ( 'dh' , {
1214
1348
primeLength : - 1
1215
1349
} , common . mustNotCall ( ) ) ;
1216
1350
} , {
1217
- name : 'TypeError' ,
1218
- code : 'ERR_INVALID_ARG_VALUE' ,
1219
- message : "The property 'options.primeLength' is invalid. " +
1351
+ name : 'RangeError' ,
1352
+ code : 'ERR_OUT_OF_RANGE' ,
1353
+ message : 'The value of "options.primeLength" is out of range. ' +
1354
+ 'It must be >= 0 && <= 2147483647. ' +
1220
1355
'Received -1' ,
1221
1356
} ) ;
1222
1357
@@ -1226,10 +1361,11 @@ const sec1EncExp = (cipher) => getRegExpForPEM('EC PRIVATE KEY', cipher);
1226
1361
generator : 2147483648 ,
1227
1362
} , common . mustNotCall ( ) ) ;
1228
1363
} , {
1229
- name : 'TypeError' ,
1230
- code : 'ERR_INVALID_ARG_VALUE' ,
1231
- message : "The property 'options.generator' is invalid. " +
1232
- 'Received 2147483648' ,
1364
+ name : 'RangeError' ,
1365
+ code : 'ERR_OUT_OF_RANGE' ,
1366
+ message : 'The value of "options.generator" is out of range. ' +
1367
+ 'It must be >= 0 && <= 2147483647. ' +
1368
+ 'Received 2_147_483_648' ,
1233
1369
} ) ;
1234
1370
1235
1371
assert . throws ( ( ) => {
@@ -1238,9 +1374,10 @@ const sec1EncExp = (cipher) => getRegExpForPEM('EC PRIVATE KEY', cipher);
1238
1374
generator : - 1 ,
1239
1375
} , common . mustNotCall ( ) ) ;
1240
1376
} , {
1241
- name : 'TypeError' ,
1242
- code : 'ERR_INVALID_ARG_VALUE' ,
1243
- message : "The property 'options.generator' is invalid. " +
1377
+ name : 'RangeError' ,
1378
+ code : 'ERR_OUT_OF_RANGE' ,
1379
+ message : 'The value of "options.generator" is out of range. ' +
1380
+ 'It must be >= 0 && <= 2147483647. ' +
1244
1381
'Received -1' ,
1245
1382
} ) ;
1246
1383
@@ -1299,9 +1436,9 @@ const sec1EncExp = (cipher) => getRegExpForPEM('EC PRIVATE KEY', cipher);
1299
1436
} ) ;
1300
1437
} , {
1301
1438
name : 'TypeError' ,
1302
- code : 'ERR_INVALID_ARG_VALUE ' ,
1303
- message : " The property ' options.hash' is invalid. " +
1304
- `Received ${ inspect ( hashValue ) } `
1439
+ code : 'ERR_INVALID_ARG_TYPE ' ,
1440
+ message : ' The " options.hash" property must be of type string.' +
1441
+ common . invalidArgTypeHelper ( hashValue )
1305
1442
} ) ;
1306
1443
}
1307
1444
@@ -1314,10 +1451,11 @@ const sec1EncExp = (cipher) => getRegExpForPEM('EC PRIVATE KEY', cipher);
1314
1451
mgf1Hash : 'sha256'
1315
1452
} , common . mustNotCall ( ) ) ;
1316
1453
} , {
1317
- name : 'TypeError' ,
1318
- code : 'ERR_INVALID_ARG_VALUE' ,
1319
- message : "The property 'options.saltLength' is invalid. " +
1320
- 'Received 2147483648'
1454
+ name : 'RangeError' ,
1455
+ code : 'ERR_OUT_OF_RANGE' ,
1456
+ message : 'The value of "options.saltLength" is out of range. ' +
1457
+ 'It must be >= 0 && <= 2147483647. ' +
1458
+ 'Received 2_147_483_648'
1321
1459
} ) ;
1322
1460
1323
1461
assert . throws ( ( ) => {
@@ -1328,9 +1466,10 @@ const sec1EncExp = (cipher) => getRegExpForPEM('EC PRIVATE KEY', cipher);
1328
1466
mgf1Hash : 'sha256'
1329
1467
} , common . mustNotCall ( ) ) ;
1330
1468
} , {
1331
- name : 'TypeError' ,
1332
- code : 'ERR_INVALID_ARG_VALUE' ,
1333
- message : "The property 'options.saltLength' is invalid. " +
1469
+ name : 'RangeError' ,
1470
+ code : 'ERR_OUT_OF_RANGE' ,
1471
+ message : 'The value of "options.saltLength" is out of range. ' +
1472
+ 'It must be >= 0 && <= 2147483647. ' +
1334
1473
'Received -1'
1335
1474
} ) ;
1336
1475
@@ -1445,9 +1584,10 @@ const sec1EncExp = (cipher) => getRegExpForPEM('EC PRIVATE KEY', cipher);
1445
1584
} ,
1446
1585
{
1447
1586
name : 'TypeError' ,
1448
- code : 'ERR_INVALID_ARG_VALUE' ,
1449
- message : "The property 'options.mgf1Hash' is invalid. " +
1450
- `Received ${ inspect ( mgf1Hash ) } `
1587
+ code : 'ERR_INVALID_ARG_TYPE' ,
1588
+ message :
1589
+ 'The "options.mgf1Hash" property must be of type string.' +
1590
+ common . invalidArgTypeHelper ( mgf1Hash )
1451
1591
1452
1592
}
1453
1593
) ;
0 commit comments