-
Notifications
You must be signed in to change notification settings - Fork 0
/
certificate.ts
36 lines (33 loc) · 1.97 KB
/
certificate.ts
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
import * as asn1 from 'asn1js'
import { Certificate } from 'pkijs'
export function getSignatureAlgorithmDetails(oid: string): { signatureAlgorithm: string; hashFunction: string } {
const details = {
'1.2.840.113549.1.1.5': { signatureAlgorithm: 'rsa', hashFunction: 'sha1' },
'1.2.840.113549.1.1.11': { signatureAlgorithm: 'rsa', hashFunction: 'sha256' },
'1.2.840.113549.1.1.12': { signatureAlgorithm: 'rsa', hashFunction: 'sha384' },
'1.2.840.113549.1.1.13': { signatureAlgorithm: 'rsa', hashFunction: 'sha512' },
// rsapss
'1.2.840.113549.1.1.10': { signatureAlgorithm: 'rsapss', hashFunction: 'sha256' }, // TODO: detect which hash function is used (not always sha256)
// ecdsa
'1.2.840.10045.4.1': { signatureAlgorithm: 'ecdsa', hashFunction: 'sha1' },
'1.2.840.10045.4.3.1': { signatureAlgorithm: 'ecdsa', hashFunction: 'sha224' },
'1.2.840.10045.4.3.2': { signatureAlgorithm: 'ecdsa', hashFunction: 'sha256' },
'1.2.840.10045.4.3.3': { signatureAlgorithm: 'ecdsa', hashFunction: 'sha384' },
'1.2.840.10045.4.3.4': { signatureAlgorithm: 'ecdsa', hashFunction: 'sha512' },
}
return details[oid] || { signatureAlgorithm: `Unknown (${oid})`, hashFunction: 'Unknown' }
}
export function getSignatureAlgorithm(pemContent: string) {
const certBuffer = Buffer.from(pemContent.replace(/(-----(BEGIN|END) CERTIFICATE-----|\n)/g, ''), 'base64')
const asn1Data = asn1.fromBER(certBuffer)
const cert = new Certificate({ schema: asn1Data.result })
const signatureAlgorithmOid = cert.signatureAlgorithm.algorithmId
const { signatureAlgorithm, hashFunction } = getSignatureAlgorithmDetails(signatureAlgorithmOid)
return { signatureAlgorithm, hashFunction }
}
export const getCircuitName = (circuitType: string, signatureAlgorithm: string, hashFunction: string) => {
if (signatureAlgorithm === 'ecdsa')
return circuitType + '_' + signatureAlgorithm + '_' + hashFunction
else
return circuitType + '_' + signatureAlgorithm + '_65537_' + hashFunction
}