Skip to content

Commit 53abfc9

Browse files
committed
chore: wip
1 parent b75c2bc commit 53abfc9

2 files changed

Lines changed: 21 additions & 18 deletions

File tree

src/pki.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { privateKeyFromAsn1, privateKeyToAsn1, rsa } from './algorithms/asymmetr
99
*/
1010
import { asn1 } from './encoding/asn1'
1111
import { pem } from './encoding/pem'
12-
import { certificateExtensionsToAsn1, certificateToAsn1, CRIAttributesAsArray, getCertificationRequestInfo } from './x509'
12+
import { certificateExtensionsToAsn1, certificateFromPem, certificateToAsn1, CRIAttributesAsArray, getCertificationRequestInfo } from './x509'
1313

1414
interface CustomError extends Error {
1515
headerType?: string
@@ -76,7 +76,8 @@ export function privateKeyInfoToPem(pki: any, maxline: number = 64): string {
7676
return pem.encode(msg, { maxline })
7777
}
7878

79-
export interface Pki {
79+
export interface PKI {
80+
certificateFromPem: typeof certificateFromPem
8081
certificateExtensionsToAsn1: typeof certificateExtensionsToAsn1
8182
certificateToAsn1: typeof certificateToAsn1
8283
CRIAttributesAsArray: typeof CRIAttributesAsArray
@@ -87,7 +88,8 @@ export interface Pki {
8788
rsa: RSA
8889
}
8990

90-
export const pki: Pki = {
91+
export const pki: PKI = {
92+
certificateFromPem,
9193
certificateExtensionsToAsn1,
9294
certificateToAsn1,
9395
getCertificationRequestInfo,

src/x509.ts

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -901,7 +901,7 @@ export function verifySignature(options: {
901901

902902
const hashFn = (md as unknown as HashFunctions)[hashOid].create()
903903
rval = options.certificate.publicKey.verify(
904-
(options.md as unknown as MD).digest().getBytes(),
904+
options.md.digest().getBytes(),
905905
options.signature,
906906
{ mgf, hash: hashFn, saltLength: params.saltLength || 20 },
907907
)
@@ -2742,11 +2742,10 @@ export function createCaStore(certs: Certificate[]): CAStore {
27422742
}
27432743
}
27442744
},
2745-
hasCertificate(cert) {
2745+
hasCertificate(cert: Certificate) {
27462746
// convert from pem if necessary
2747-
if (typeof cert === 'string') {
2747+
if (typeof cert === 'string')
27482748
cert = pki.certificateFromPem(cert)
2749-
}
27502749

27512750
let match = getBySubject(cert.subject)
27522751
if (!match)
@@ -2784,23 +2783,23 @@ export function createCaStore(certs: Certificate[]): CAStore {
27842783

27852784
return certList
27862785
},
2787-
removeCertificate(cert) {
2786+
removeCertificate(cert: Certificate) {
27882787
let result
27892788

27902789
// convert from pem if necessary
2791-
if (typeof cert === 'string') {
2790+
if (typeof cert === 'string')
27922791
cert = certificateFromPem(cert)
2793-
}
2792+
27942793
ensureSubjectHasHash(cert.subject)
2795-
if (!caStore.hasCertificate(cert)) {
2794+
if (!caStore.hasCertificate(cert))
27962795
return null
2797-
}
27982796

27992797
const match = getBySubject(cert.subject)
28002798

28012799
if (!Array.isArray(match)) {
28022800
result = caStore.certs[cert.subject.hash]
28032801
delete caStore.certs[cert.subject.hash]
2802+
28042803
return result
28052804
}
28062805

@@ -2813,16 +2812,17 @@ export function createCaStore(certs: Certificate[]): CAStore {
28132812
match.splice(i, 1)
28142813
}
28152814
}
2816-
if (match.length === 0) {
2815+
2816+
if (match.length === 0)
28172817
delete caStore.certs[cert.subject.hash]
2818-
}
28192818

28202819
return result
28212820
},
28222821
}
28232822

28242823
function getBySubject(subject: { hash: string | null }) {
28252824
ensureSubjectHasHash(subject)
2825+
28262826
return caStore.certs[subject.hash || ''] || null
28272827
}
28282828

@@ -2942,11 +2942,11 @@ export function verifyCertificateChain(
29422942
}
29432943
}
29442944
}
2945-
catch (ex) {
2945+
catch (e: any) {
29462946
error = {
29472947
message: 'Certificate signature is invalid.',
29482948
error: 'pki.BadCertificate',
2949-
details: ex.toString(),
2949+
details: e.toString(),
29502950
}
29512951
}
29522952
}
@@ -3020,15 +3020,16 @@ export function verifyCertificateChain(
30203020
function _containsAll(iattr: RDNAttribute[], sattr: RDNAttribute[]): boolean {
30213021
// ensure all parent subject attributes are present in issuer
30223022
let rval = true
3023+
30233024
for (let i = 0; rval && i < sattr.length; ++i) {
30243025
const attr = sattr[i]
30253026
rval = false
30263027
for (let j = 0; !rval && j < iattr.length; ++j) {
3027-
if (attr.type === iattr[j].type && attr.value === iattr[j].value) {
3028+
if (attr.type === iattr[j].type && attr.value === iattr[j].value)
30283029
rval = true
3029-
}
30303030
}
30313031
}
3032+
30323033
return rval
30333034
}
30343035

0 commit comments

Comments
 (0)