|
1 |
| -import { |
2 |
| - ASN1Construction, |
3 |
| - ASN1TagClass, |
4 |
| - ASN1UniversalType, |
5 |
| - DERElement, |
6 |
| - ObjectIdentifier, |
7 |
| -} from 'asn1-ts' |
8 |
| - |
9 |
| -/** |
10 |
| - * Represents an ASN.1 AlgorithmIdentifier structure commonly used in cryptographic protocols. |
11 |
| - * This class handles the construction and DER encoding of an algorithm identifier, which typically |
12 |
| - * consists of an algorithm OID and optional parameters. |
13 |
| - */ |
14 |
| -class AlgorithmIdentifier { |
15 |
| - public algorithm: ObjectIdentifier |
16 |
| - public parameters: null |
17 |
| - |
18 |
| - /** |
19 |
| - * Creates an instance of AlgorithmIdentifier. |
20 |
| - * |
21 |
| - * @param algorithm The ObjectIdentifier of the algorithm. |
22 |
| - * @param parameters The parameters of the algorithm, generally null in many cryptographic uses. |
23 |
| - */ |
24 |
| - constructor(algorithm: ObjectIdentifier, parameters: null = null) { |
25 |
| - this.algorithm = algorithm |
26 |
| - this.parameters = parameters |
27 |
| - } |
28 |
| - |
29 |
| - /** |
30 |
| - * Encodes this AlgorithmIdentifier into its DER (Distinguished Encoding Rules) format. |
31 |
| - * |
32 |
| - * @returns Uint8Array containing the DER encoded bytes of the AlgorithmIdentifier. |
33 |
| - */ |
34 |
| - public toDER(): Uint8Array { |
35 |
| - const sequenceElement = new DERElement( |
36 |
| - ASN1TagClass.universal, |
37 |
| - ASN1Construction.constructed, |
38 |
| - ASN1UniversalType.sequence, |
39 |
| - ) |
40 |
| - |
41 |
| - const oidElement = new DERElement( |
42 |
| - ASN1TagClass.universal, |
43 |
| - ASN1Construction.primitive, |
44 |
| - ASN1UniversalType.objectIdentifier, |
45 |
| - ) |
46 |
| - oidElement.objectIdentifier = this.algorithm |
47 |
| - |
48 |
| - const nullElement = new DERElement( |
49 |
| - ASN1TagClass.universal, |
50 |
| - ASN1Construction.primitive, |
51 |
| - ASN1UniversalType.nill, |
52 |
| - ) |
53 |
| - |
54 |
| - sequenceElement.sequence = [oidElement, nullElement] |
55 |
| - |
56 |
| - return sequenceElement.toBytes() |
57 |
| - } |
58 |
| -} |
| 1 | +import { ObjectIdentifier } from 'asn1js' |
59 | 2 |
|
60 | 3 | /**
|
61 | 4 | * Encodes a given string representation of an OID into its DER format.
|
62 | 5 | * This function is specifically used to encode signature algorithm OIDs.
|
63 | 6 | *
|
64 | 7 | * @param oid The string representation of the ObjectIdentifier to be encoded.
|
65 |
| - * @returns Uint8Array containing the DER encoded OID. |
66 |
| - * @example |
67 |
| - * ```ts |
68 |
| - * const oid = '1.2.840.113549.1.1.11' // Example OID for SHA-256 with RSA Encryption |
69 |
| - * const derEncodedOID = derEncodeSignatureAlgorithmOID(oid) |
70 |
| - * console.log(new Uint8Array(derEncodedOID)) // Logs the DER encoded bytes |
71 |
| - * ``` |
| 8 | + * @returns Uint8Array containing the DER encoded OID along with NULL params of X.509 signature algorithm. |
72 | 9 | */
|
73 | 10 | export function derEncodeSignatureAlgorithmOID(oid: string): Uint8Array {
|
74 |
| - const numbers = oid.split('.').map((n) => parseInt(n, 10)) // Convert the string parts to numbers |
75 |
| - const algorithmIdentifier = new AlgorithmIdentifier(new ObjectIdentifier(numbers)) |
76 |
| - return algorithmIdentifier.toDER() |
| 11 | + const objectIdentifier = new ObjectIdentifier({ value: oid }) |
| 12 | + const berArrayBuffer = objectIdentifier.toBER(false) |
| 13 | + |
| 14 | + // Typically, in X.509, the algorithm identifier is followed by parameters; for many algorithms, this is just NULL. |
| 15 | + const nullParameter = [0x05, 0x00] // DER encoding for NULL |
| 16 | + |
| 17 | + // Calculate the total length including OID and NULL parameter |
| 18 | + const totalLength = berArrayBuffer.byteLength + nullParameter.length |
| 19 | + |
| 20 | + const sequenceHeader = [0x30, totalLength] // 0x30 is the DER tag for SEQUENCE |
| 21 | + |
| 22 | + return new Uint8Array([...sequenceHeader, ...new Uint8Array(berArrayBuffer), ...nullParameter]) |
77 | 23 | }
|
0 commit comments