import * as asn1js from "asn1js"; import * as pvtsutils from "pvtsutils"; import * as pvutils from "pvutils"; import * as common from "./common"; import { AlgorithmIdentifier, AlgorithmIdentifierJson, AlgorithmIdentifierSchema } from "./AlgorithmIdentifier"; import { RelativeDistinguishedNames, RelativeDistinguishedNamesJson, RelativeDistinguishedNamesSchema } from "./RelativeDistinguishedNames"; import { Time, TimeJson, TimeSchema } from "./Time"; import { RevokedCertificate, RevokedCertificateJson } from "./RevokedCertificate"; import { Extensions, ExtensionsJson, ExtensionsSchema } from "./Extensions"; import * as Schema from "./Schema"; import { Certificate } from "./Certificate"; import { PublicKeyInfo } from "./PublicKeyInfo"; import { id_AuthorityInfoAccess, id_AuthorityKeyIdentifier, id_BaseCRLNumber, id_CertificateIssuer, id_CRLNumber, id_CRLReason, id_FreshestCRL, id_InvalidityDate, id_IssuerAltName, id_IssuingDistributionPoint } from "./ObjectIdentifiers"; import { AsnError } from "./errors"; import { PkiObject, PkiObjectParameters } from "./PkiObject"; import { EMPTY_BUFFER } from "./constants";
function tbsCertList(parameters: TBSCertListSchema = {}): Schema.SchemaType { //TBSCertList ::= SEQUENCE { // version Version OPTIONAL, // -- if present, MUST be v2 // signature AlgorithmIdentifier, // issuer Name, // thisUpdate Time, // nextUpdate Time OPTIONAL, // revokedCertificates SEQUENCE OF SEQUENCE { // userCertificate CertificateSerialNumber, // revocationDate Time, // crlEntryExtensions Extensions OPTIONAL // -- if present, version MUST be v2 // } OPTIONAL, // crlExtensions [0] EXPLICIT Extensions OPTIONAL // -- if present, version MUST be v2 //} const names = pvutils.getParametersValue<NonNullable<typeof parameters.names>>(parameters, "names", {});
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 signature!: AlgorithmIdentifier; public issuer!: RelativeDistinguishedNames; public thisUpdate!: Time; public nextUpdate?: Time; public revokedCertificates?: RevokedCertificate[]; public crlExtensions?: Extensions; public signatureAlgorithm!: AlgorithmIdentifier; public signatureValue!: asn1js.BitString;
/** * Initializes a new instance of the {@link CertificateRevocationList} class * @param parameters Initialization parameters
*/
constructor(parameters: CertificateRevocationListParameters = {}) { super();
this.tbsView = new Uint8Array(pvutils.getParametersValue(parameters, TBS, CertificateRevocationList.defaultValues(TBS))); this.version = pvutils.getParametersValue(parameters, VERSION, CertificateRevocationList.defaultValues(VERSION)); this.signature = pvutils.getParametersValue(parameters, SIGNATURE, CertificateRevocationList.defaultValues(SIGNATURE)); this.issuer = pvutils.getParametersValue(parameters, ISSUER, CertificateRevocationList.defaultValues(ISSUER)); this.thisUpdate = pvutils.getParametersValue(parameters, THIS_UPDATE, CertificateRevocationList.defaultValues(THIS_UPDATE)); if (NEXT_UPDATE in parameters) { this.nextUpdate = pvutils.getParametersValue(parameters, NEXT_UPDATE, CertificateRevocationList.defaultValues(NEXT_UPDATE));
} if (REVOKED_CERTIFICATES in parameters) { this.revokedCertificates = pvutils.getParametersValue(parameters, REVOKED_CERTIFICATES, CertificateRevocationList.defaultValues(REVOKED_CERTIFICATES));
} if (CRL_EXTENSIONS in parameters) { this.crlExtensions = pvutils.getParametersValue(parameters, CRL_EXTENSIONS, CertificateRevocationList.defaultValues(CRL_EXTENSIONS));
} this.signatureAlgorithm = pvutils.getParametersValue(parameters, SIGNATURE_ALGORITHM, CertificateRevocationList.defaultValues(SIGNATURE_ALGORITHM)); this.signatureValue = pvutils.getParametersValue(parameters, SIGNATURE_VALUE, CertificateRevocationList.defaultValues(SIGNATURE_VALUE));
if (parameters.schema) { this.fromSchema(parameters.schema);
}
}
/** * Returns default values for all class members * @param memberName String name for a class member * @returns Default value
*/ publicstatic override defaultValues(memberName: typeof TBS): ArrayBuffer; publicstatic override defaultValues(memberName: typeof VERSION): number; publicstatic override defaultValues(memberName: typeof SIGNATURE): AlgorithmIdentifier; publicstatic override defaultValues(memberName: typeof ISSUER): RelativeDistinguishedNames; publicstatic override defaultValues(memberName: typeof THIS_UPDATE): Time; publicstatic override defaultValues(memberName: typeof NEXT_UPDATE): Time; publicstatic override defaultValues(memberName: typeof REVOKED_CERTIFICATES): RevokedCertificate[]; publicstatic override defaultValues(memberName: typeof CRL_EXTENSIONS): Extensions; publicstatic override defaultValues(memberName: typeof SIGNATURE_ALGORITHM): AlgorithmIdentifier; publicstatic override defaultValues(memberName: typeof SIGNATURE_VALUE): asn1js.BitString; publicstatic override defaultValues(memberName: string): any { switch (memberName) { case TBS: return EMPTY_BUFFER; case VERSION: return0; case SIGNATURE: returnnew AlgorithmIdentifier(); case ISSUER: returnnew RelativeDistinguishedNames(); case THIS_UPDATE: returnnew Time(); case NEXT_UPDATE: returnnew Time(); case REVOKED_CERTIFICATES: return []; case CRL_EXTENSIONS: returnnew Extensions(); case SIGNATURE_ALGORITHM: returnnew AlgorithmIdentifier(); case SIGNATURE_VALUE: returnnew asn1js.BitString(); default: returnsuper.defaultValues(memberName);
}
}
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,
CertificateRevocationList.schema()
);
AsnError.assertSchema(asn1, this.className);
//#region Get internal properties from parsed schema this.tbsView = (asn1.result.tbsCertList as asn1js.Sequence).valueBeforeDecodeView;
if (TBS_CERT_LIST_VERSION in asn1.result) { this.version = asn1.result[TBS_CERT_LIST_VERSION].valueBlock.valueDec;
} this.signature = new AlgorithmIdentifier({ schema: asn1.result[TBS_CERT_LIST_SIGNATURE] }); this.issuer = new RelativeDistinguishedNames({ schema: asn1.result[TBS_CERT_LIST_ISSUER] }); this.thisUpdate = new Time({ schema: asn1.result[TBS_CERT_LIST_THIS_UPDATE] }); if (TBS_CERT_LIST_NEXT_UPDATE in asn1.result) { this.nextUpdate = new Time({ schema: asn1.result[TBS_CERT_LIST_NEXT_UPDATE] });
} if (TBS_CERT_LIST_REVOKED_CERTIFICATES in asn1.result) { this.revokedCertificates = Array.from(asn1.result[TBS_CERT_LIST_REVOKED_CERTIFICATES], element => new RevokedCertificate({ schema: element }));
} if (TBS_CERT_LIST_EXTENSIONS in asn1.result) { this.crlExtensions = new Extensions({ schema: asn1.result[TBS_CERT_LIST_EXTENSIONS] });
}
return (new asn1js.Sequence({
value: outputArray
}));
}
/** * Convert current object to asn1js object and set correct values * @returns asn1js object
*/ public toSchema(encodeFlag = false) { //#region Decode stored TBS value
let tbsSchema;
if (!encodeFlag) { if (!this.tbsView.byteLength) { // No stored TBS part return CertificateRevocationList.schema();
}
const asn1 = asn1js.fromBER(this.tbsView);
AsnError.assert(asn1, "TBS Certificate Revocation List");
tbsSchema = asn1.result;
} //#endregion //#region Create TBS schema via assembling from TBS parts else {
tbsSchema = this.encodeTBS();
} //#endregion
//#region Construct and return new ASN.1 schema for this object return (new asn1js.Sequence({
value: [
tbsSchema, this.signatureAlgorithm.toSchema(), this.signatureValue
]
})); //#endregion
}
if (this.version !== CertificateRevocationList.defaultValues(VERSION))
res.version = this.version;
if (this.nextUpdate) {
res.nextUpdate = this.nextUpdate.toJSON();
}
if (this.revokedCertificates) {
res.revokedCertificates = Array.from(this.revokedCertificates, o => o.toJSON());
}
if (this.crlExtensions) {
res.crlExtensions = this.crlExtensions.toJSON();
}
return res;
}
/** * Returns `true` if supplied certificate is revoked, otherwise `false` * @param certificate
*/ public isCertificateRevoked(certificate: Certificate): boolean { // Check that issuer of the input certificate is the same with issuer of this CRL if (!this.issuer.isEqual(certificate.issuer)) { returnfalse;
}
// Check that there are revoked certificates in this CRL if (!this.revokedCertificates) { returnfalse;
}
// Search for input certificate in revoked certificates array for (const revokedCertificate of this.revokedCertificates) { if (revokedCertificate.userCertificate.isEqual(certificate.serialNumber)) { returntrue;
}
}
returnfalse;
}
/** * Make a signature for existing CRL data * @param privateKey Private key for "subjectPublicKeyInfo" structure * @param hashAlgorithm Hashing algorithm. Default SHA-1 * @param crypto Crypto engine
*/ public async sign(privateKey: CryptoKey, hashAlgorithm = "SHA-1", crypto = common.getCrypto(true)): Promise<void> { // Get a private key from function parameter 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 signatureParameters = await crypto.getSignatureParameters(privateKey, hashAlgorithm); const { parameters } = signatureParameters; this.signature = signatureParameters.signatureAlgorithm; this.signatureAlgorithm = signatureParameters.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
}
//#region Get information about CRL issuer certificate if (parameters.issuerCertificate) { // "issuerCertificate" must be of type "Certificate"
subjectPublicKeyInfo = parameters.issuerCertificate.subjectPublicKeyInfo;
// The CRL issuer name and "issuerCertificate" subject name are not equal if (!this.issuer.isEqual(parameters.issuerCertificate.subject)) { returnfalse;
}
}
//#region In case if there is only public key during verification if (parameters.publicKeyInfo) {
subjectPublicKeyInfo = parameters.publicKeyInfo; // Must be of type "PublicKeyInfo"
} //#endregion
if (!subjectPublicKeyInfo) { thrownew Error("Issuer's certificate must be provided as an input parameter");
} //#endregion
//#region Check the CRL for unknown critical extensions if (this.crlExtensions) { for (const extension of this.crlExtensions.extensions) { if (extension.critical) { // We can not be sure that unknown extension has no value for CRL signature if (!WELL_KNOWN_EXTENSIONS.includes(extension.extnID)) returnfalse;
}
}
} //#endregion
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.