Skip to content

Commit 635d3ce

Browse files
committed
chore: wip
1 parent 422f1ea commit 635d3ce

1 file changed

Lines changed: 32 additions & 12 deletions

File tree

  • src/algorithms/asymmetric

src/algorithms/asymmetric/rsa.ts

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1199,13 +1199,13 @@ export function setRsaPublicKey(n: BigInteger, e: BigInteger): {
11991199
}
12001200
else if (scheme === 'RSA-OAEP' || scheme === 'RSAES-OAEP') {
12011201
scheme = {
1202-
encode(m, key) {
1202+
encode(m: string, key: any) {
12031203
return encode_rsa_oaep(key, m, schemeOptions)
12041204
},
12051205
}
12061206
}
12071207
else if (['RAW', 'NONE', 'NULL', null].includes(scheme)) {
1208-
scheme = { encode(e) { return e } }
1208+
scheme = { encode(e: string) { return e } }
12091209
}
12101210
else if (typeof scheme === 'string') {
12111211
throw new TypeError(`Unsupported encryption scheme: "${scheme}".`)
@@ -1345,7 +1345,7 @@ export function setPrivateKey(
13451345
}
13461346
}
13471347
else if (['RAW', 'NONE', 'NULL', null].includes(scheme)) {
1348-
scheme = { decode(d) { return d } }
1348+
scheme = { decode(d: string) { return d } }
13491349
}
13501350
else {
13511351
throw new Error(`Unsupported encryption scheme: "${scheme}".`)
@@ -1388,6 +1388,21 @@ export function wrapRsaPrivateKey(rsaKey: Asn1Object): Asn1Object {
13881388
])
13891389
}
13901390

1391+
interface PrivateKeyInfoCapture {
1392+
privateKey?: string
1393+
privateKeyVersion?: string
1394+
privateKeyModulus?: string
1395+
privateKeyPublicExponent?: string
1396+
privateKeyPrivateExponent?: string
1397+
privateKeyPrime1?: string
1398+
privateKeyPrime2?: string
1399+
privateKeyExponent1?: string
1400+
privateKeyExponent2?: string
1401+
privateKeyCoefficient?: string
1402+
algorithmIdentifier?: string
1403+
digest?: string
1404+
}
1405+
13911406
/**
13921407
* Converts a private key from an ASN.1 object.
13931408
*
@@ -1397,7 +1412,7 @@ export function wrapRsaPrivateKey(rsaKey: Asn1Object): Asn1Object {
13971412
*/
13981413
export function privateKeyFromAsn1(obj: Asn1Object): RSAKey {
13991414
// get PrivateKeyInfo
1400-
let capture = {}
1415+
let capture: PrivateKeyInfoCapture = {}
14011416
let errors: CustomError[] = []
14021417
if (asn1.validate(obj, privateKeyValidator, capture, errors)) {
14031418
obj = asn1.fromDer(createBuffer(capture.privateKey))
@@ -1480,7 +1495,8 @@ export function privateKeyToAsn1(key: {
14801495

14811496
interface PublicKeyCapture {
14821497
publicKeyModulus?: string
1483-
publicKeyExponent?: Asn1Object
1498+
publicKeyExponent?: string
1499+
publicKeyOid?: string
14841500
}
14851501

14861502
/**
@@ -1631,14 +1647,13 @@ function _encodePkcs1_v1_5(m: string, key: {
16311647
while (padNum > 0) {
16321648
let numZeros = 0
16331649
const padBytes = random.getBytes(padNum)
1650+
if (!padBytes) continue
16341651
for (let i = 0; i < padNum; ++i) {
16351652
padByte = padBytes.charCodeAt(i)
1636-
if (padByte === 0) {
1653+
if (padByte === 0)
16371654
++numZeros
1638-
}
1639-
else {
1655+
else
16401656
eb.putByte(padByte)
1641-
}
16421657
}
16431658
padNum = numZeros
16441659
}
@@ -1688,6 +1703,9 @@ function _decodePkcs1_v1_5(em: string, key: {
16881703
throw new Error('Encryption block is invalid.')
16891704
}
16901705

1706+
if (!ml)
1707+
ml = eb.length() - k
1708+
16911709
let padNum = 0
16921710
if (bt === 0x00) {
16931711
// check all padding bytes for 0x00
@@ -1747,6 +1765,8 @@ function _generateKeyPair(state: {
17471765
qBits: number
17481766
bits: number
17491767
e: BigInteger
1768+
p: BigInteger
1769+
q: BigInteger
17501770
}, options: {
17511771
algorithm?: string
17521772
workers?: number
@@ -2131,7 +2151,7 @@ export function addRSAKeyOps(key: RSAKey): RSAKeyWithOps {
21312151

21322152
if (scheme === 'RSASSA-PKCS1-V1_5') {
21332153
scheme = {
2134-
verify(digest, d) {
2154+
verify(digest: string | Uint8Array, d: string) {
21352155
// remove padding
21362156
d = _decodePkcs1_v1_5(d, key, true)
21372157
// d is ASN.1 BER-encoded DigestInfo
@@ -2140,7 +2160,7 @@ export function addRSAKeyOps(key: RSAKey): RSAKeyWithOps {
21402160
})
21412161

21422162
// validate DigestInfo
2143-
const capture = {}
2163+
const capture: PrivateKeyInfoCapture = {}
21442164
const errors: CustomError[] = []
21452165
if (!asn1.validate(obj, digestInfoValidator, capture, errors)) {
21462166
const error: CustomError = new Error('ASN.1 object does not contain a valid RSASSA-PKCS1-v1_5 DigestInfo value.')
@@ -2183,7 +2203,7 @@ export function addRSAKeyOps(key: RSAKey): RSAKeyWithOps {
21832203
}
21842204
else if (scheme === 'NONE' || scheme === 'NULL' || scheme === null) {
21852205
scheme = {
2186-
verify(digest, d) {
2206+
verify(digest: string | Uint8Array, d: string) {
21872207
// remove padding
21882208
d = _decodePkcs1_v1_5(d, key, true)
21892209
return digest === d

0 commit comments

Comments
 (0)