import * as asn1js from "asn1js"; import * as pvtsutils from "pvtsutils"; import * as pvutils from "pvutils"; import * as common from "./common"; import { PublicKeyInfo, PublicKeyInfoJson } from "./PublicKeyInfo"; import { RelativeDistinguishedNames, RelativeDistinguishedNamesJson, RelativeDistinguishedNamesSchema } from "./RelativeDistinguishedNames"; import { AlgorithmIdentifier, AlgorithmIdentifierJson } from "./AlgorithmIdentifier"; import { Attribute, AttributeJson, AttributeSchema } from "./Attribute"; import * as Schema from "./Schema"; import { CryptoEnginePublicKeyParams } from "./CryptoEngine/CryptoEngineInterface"; import { AsnError } from "./errors"; import { PkiObject, PkiObjectParameters } from "./PkiObject"; import { EMPTY_BUFFER } from "./constants";
export interface ICertificationRequest { /** * Value being signed
*/
tbs: ArrayBuffer; /** * Version number. It should be 0
*/
version: number; /** * Distinguished name of the certificate subject
*/
subject: RelativeDistinguishedNames; /** * Information about the public key being certified
*/
subjectPublicKeyInfo: PublicKeyInfo; /** * Collection of attributes providing additional information about the subject of the certificate
*/
attributes?: Attribute[];
/** * signature algorithm (and any associated parameters) under which the certification-request information is signed
*/
signatureAlgorithm: AlgorithmIdentifier; /** * result of signing the certification request information with the certification request subject's private key
*/
signatureValue: asn1js.BitString;
}
public tbsView!: Uint8Array; /** * @deprecated Since version 3.0.0
*/ public get tbs(): ArrayBuffer { return pvtsutils.BufferSourceConverter.toArrayBuffer(this.tbsView);
}
/** * @deprecated Since version 3.0.0
*/ public set tbs(value: ArrayBuffer) { this.tbsView = new Uint8Array(value);
} public version!: number; public subject!: RelativeDistinguishedNames; public subjectPublicKeyInfo!: PublicKeyInfo; public attributes?: Attribute[]; public signatureAlgorithm!: AlgorithmIdentifier; public signatureValue!: asn1js.BitString;
/** * Initializes a new instance of the {@link CertificationRequest} class * @param parameters Initialization parameters
*/
constructor(parameters: CertificationRequestParameters = {}) { super();
return (new asn1js.Sequence({
value: [
CertificationRequestInfo(names.certificationRequestInfo || {}), new asn1js.Sequence({
name: (names.signatureAlgorithm || SIGNATURE_ALGORITHM),
value: [ new asn1js.ObjectIdentifier(), new asn1js.Any({ optional: true })
]
}), new asn1js.BitString({ name: (names.signatureValue || SIGNATURE_VALUE) })
]
}));
}
public fromSchema(schema: Schema.SchemaType): void { // Clear input data first
pvutils.clearProps(schema, CLEAR_PROPS);
// Check the schema is valid const asn1 = asn1js.compareSchema(schema,
schema,
CertificationRequest.schema()
);
AsnError.assertSchema(asn1, this.className);
// Get internal properties from parsed schema this.tbsView = (asn1.result.CertificationRequestInfo as asn1js.Sequence).valueBeforeDecodeView; this.version = asn1.result[CSR_INFO_VERSION].valueBlock.valueDec; this.subject = new RelativeDistinguishedNames({ schema: asn1.result[CSR_INFO_SUBJECT] }); this.subjectPublicKeyInfo = new PublicKeyInfo({ schema: asn1.result[CSR_INFO_SPKI] }); if (CSR_INFO_ATTRS in asn1.result) { this.attributes = Array.from(asn1.result[CSR_INFO_ATTRS], element => new Attribute({ schema: element }));
} this.signatureAlgorithm = new AlgorithmIdentifier({ schema: asn1.result.signatureAlgorithm }); this.signatureValue = asn1.result.signatureValue;
}
/** * Aux function making ASN1js Sequence from current TBS * @returns
*/ protected encodeTBS(): asn1js.Sequence { //#region Create array for output sequence const outputArray = [ new asn1js.Integer({ value: this.version }), this.subject.toSchema(), this.subjectPublicKeyInfo.toSchema()
];
if (ATTRIBUTES in this) {
outputArray.push(new asn1js.Constructed({
idBlock: {
tagClass: 3, // CONTEXT-SPECIFIC
tagNumber: 0// [0]
},
value: Array.from(this.attributes || [], o => o.toSchema())
}));
} //#endregion
return (new asn1js.Sequence({
value: outputArray
}));
}
public toSchema(encodeFlag = false): asn1js.Sequence {
let tbsSchema;
if (encodeFlag === false) { if (this.tbsView.byteLength === 0) { // No stored TBS part return CertificationRequest.schema();
}
//#region Construct and return new ASN.1 schema for this object return (new asn1js.Sequence({
value: [
tbsSchema, this.signatureAlgorithm.toSchema(), this.signatureValue
]
})); //#endregion
}
if (ATTRIBUTES in this) {
object.attributes = Array.from(this.attributes || [], o => o.toJSON());
}
return object;
}
/** * Makes signature for current certification request * @param privateKey WebCrypto private key * @param hashAlgorithm String representing current hashing algorithm * @param crypto Crypto engine
*/
async sign(privateKey: CryptoKey, hashAlgorithm = "SHA-1", crypto = common.getCrypto(true)): Promise<void> { // Initial checking if (!privateKey) { thrownew Error("Need to provide a private key for signing");
}
//#region Get a "default parameters" for current algorithm and set correct signature algorithm const signatureParams = await crypto.getSignatureParameters(privateKey, hashAlgorithm); const parameters = signatureParams.parameters; this.signatureAlgorithm = signatureParams.signatureAlgorithm; //#endregion
//#region Create TBS data for signing this.tbsView = new Uint8Array(this.encodeTBS().toBER()); //#endregion
//#region Signing TBS data on provided private key const signature = await crypto.signWithPrivateKey(this.tbsView, privateKey, parameters as any); this.signatureValue = new asn1js.BitString({ valueHex: signature }); //#endregion
}
/** * Verify existing certification request signature * @param crypto Crypto engine * @returns Returns `true` if signature value is valid, otherwise `false`
*/ public async verify(crypto = common.getCrypto(true)): Promise<boolean> { return crypto.verifyWithPublicKey(this.tbsView, this.signatureValue, this.subjectPublicKeyInfo, this.signatureAlgorithm);
}
/** * Importing public key for current certificate request * @param parameters * @param crypto Crypto engine * @returns WebCrypt public key
*/ public async getPublicKey(parameters?: CryptoEnginePublicKeyParams, crypto = common.getCrypto(true)): Promise<CryptoKey> { return crypto.getPublicKey(this.subjectPublicKeyInfo, this.signatureAlgorithm, parameters);
}
}
Messung V0.5 in Prozent
¤ Dauer der Verarbeitung: 0.19 Sekunden
(vorverarbeitet am 2026-06-06)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.