-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixed the function considering the new library
Old ASN library is replaced by the new one because it has high no. of downloads/week although the last commit is 2 years old.
- Loading branch information
Showing
4 changed files
with
27 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,77 +1,23 @@ | ||
import { | ||
ASN1Construction, | ||
ASN1TagClass, | ||
ASN1UniversalType, | ||
DERElement, | ||
ObjectIdentifier, | ||
} from 'asn1-ts' | ||
|
||
/** | ||
* Represents an ASN.1 AlgorithmIdentifier structure commonly used in cryptographic protocols. | ||
* This class handles the construction and DER encoding of an algorithm identifier, which typically | ||
* consists of an algorithm OID and optional parameters. | ||
*/ | ||
class AlgorithmIdentifier { | ||
public algorithm: ObjectIdentifier | ||
public parameters: null | ||
|
||
/** | ||
* Creates an instance of AlgorithmIdentifier. | ||
* | ||
* @param algorithm The ObjectIdentifier of the algorithm. | ||
* @param parameters The parameters of the algorithm, generally null in many cryptographic uses. | ||
*/ | ||
constructor(algorithm: ObjectIdentifier, parameters: null = null) { | ||
this.algorithm = algorithm | ||
this.parameters = parameters | ||
} | ||
|
||
/** | ||
* Encodes this AlgorithmIdentifier into its DER (Distinguished Encoding Rules) format. | ||
* | ||
* @returns Uint8Array containing the DER encoded bytes of the AlgorithmIdentifier. | ||
*/ | ||
public toDER(): Uint8Array { | ||
const sequenceElement = new DERElement( | ||
ASN1TagClass.universal, | ||
ASN1Construction.constructed, | ||
ASN1UniversalType.sequence, | ||
) | ||
|
||
const oidElement = new DERElement( | ||
ASN1TagClass.universal, | ||
ASN1Construction.primitive, | ||
ASN1UniversalType.objectIdentifier, | ||
) | ||
oidElement.objectIdentifier = this.algorithm | ||
|
||
const nullElement = new DERElement( | ||
ASN1TagClass.universal, | ||
ASN1Construction.primitive, | ||
ASN1UniversalType.nill, | ||
) | ||
|
||
sequenceElement.sequence = [oidElement, nullElement] | ||
|
||
return sequenceElement.toBytes() | ||
} | ||
} | ||
import { ObjectIdentifier } from 'asn1js' | ||
|
||
/** | ||
* Encodes a given string representation of an OID into its DER format. | ||
* This function is specifically used to encode signature algorithm OIDs. | ||
* | ||
* @param oid The string representation of the ObjectIdentifier to be encoded. | ||
* @returns Uint8Array containing the DER encoded OID. | ||
* @example | ||
* ```ts | ||
* const oid = '1.2.840.113549.1.1.11' // Example OID for SHA-256 with RSA Encryption | ||
* const derEncodedOID = derEncodeSignatureAlgorithmOID(oid) | ||
* console.log(new Uint8Array(derEncodedOID)) // Logs the DER encoded bytes | ||
* ``` | ||
* @returns Uint8Array containing the DER encoded OID along with NULL params of X.509 signature algorithm. | ||
*/ | ||
export function derEncodeSignatureAlgorithmOID(oid: string): Uint8Array { | ||
const numbers = oid.split('.').map((n) => parseInt(n, 10)) // Convert the string parts to numbers | ||
const algorithmIdentifier = new AlgorithmIdentifier(new ObjectIdentifier(numbers)) | ||
return algorithmIdentifier.toDER() | ||
const objectIdentifier = new ObjectIdentifier({ value: oid }) | ||
const berArrayBuffer = objectIdentifier.toBER(false) | ||
|
||
// Typically, in X.509, the algorithm identifier is followed by parameters; for many algorithms, this is just NULL. | ||
const nullParameter = [0x05, 0x00] // DER encoding for NULL | ||
|
||
// Calculate the total length including OID and NULL parameter | ||
const totalLength = berArrayBuffer.byteLength + nullParameter.length | ||
|
||
const sequenceHeader = [0x30, totalLength] // 0x30 is the DER tag for SEQUENCE | ||
|
||
return new Uint8Array([...sequenceHeader, ...new Uint8Array(berArrayBuffer), ...nullParameter]) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters