Certificate
Represents an X.509 certificate described in RFC5280 Section 4.
Examples
Section titled “Examples”The following example demonstrates how to parse X.509 Certificate
const asn1 = asn1js.fromBER(raw);if (asn1.offset === -1) { throw new Error("Incorrect encoded ASN.1 data");}
const cert = new pkijs.Certificate({ schema: asn1.result });The following example demonstrates how to create self-signed certificate
const crypto = pkijs.getCrypto(true);
// Create certificateconst certificate = new pkijs.Certificate();certificate.version = 2;certificate.serialNumber = new asn1js.Integer({ value: 1 });certificate.issuer.typesAndValues.push(new pkijs.AttributeTypeAndValue({ type: "2.5.4.3", // Common name value: new asn1js.BmpString({ value: "Test" })}));certificate.subject.typesAndValues.push(new pkijs.AttributeTypeAndValue({ type: "2.5.4.3", // Common name value: new asn1js.BmpString({ value: "Test" })}));
certificate.notBefore.value = new Date();const notAfter = new Date();notAfter.setUTCFullYear(notAfter.getUTCFullYear() + 1);certificate.notAfter.value = notAfter;
certificate.extensions = []; // Extensions are not a part of certificate by default, it's an optional array
// "BasicConstraints" extensionconst basicConstr = new pkijs.BasicConstraints({ cA: true, pathLenConstraint: 3});certificate.extensions.push(new pkijs.Extension({ extnID: "2.5.29.19", critical: false, extnValue: basicConstr.toSchema().toBER(false), parsedValue: basicConstr // Parsed value for well-known extensions}));
// "KeyUsage" extensionconst bitArray = new ArrayBuffer(1);const bitView = new Uint8Array(bitArray);bitView[0] |= 0x02; // Key usage "cRLSign" flagbitView[0] |= 0x04; // Key usage "keyCertSign" flagconst keyUsage = new asn1js.BitString({ valueHex: bitArray });certificate.extensions.push(new pkijs.Extension({ extnID: "2.5.29.15", critical: false, extnValue: keyUsage.toBER(false), parsedValue: keyUsage // Parsed value for well-known extensions}));
const algorithm = pkijs.getAlgorithmParameters("RSASSA-PKCS1-v1_5", "generateKey");if ("hash" in algorithm.algorithm) { algorithm.algorithm.hash.name = "SHA-256";}
const keys = await crypto.generateKey(algorithm.algorithm, true, algorithm.usages);
// Exporting public key into "subjectPublicKeyInfo" value of certificateawait certificate.subjectPublicKeyInfo.importKey(keys.publicKey);
// Signing final certificateawait certificate.sign(keys.privateKey, "SHA-256");
const raw = certificate.toSchema().toBER();Extends
Section titled “Extends”Implements
Section titled “Implements”Constructors
Section titled “Constructors”Constructor
Section titled “Constructor”new Certificate(
parameters?):Certificate
Initializes a new instance of the Certificate class
Parameters
Section titled “Parameters”parameters?
Section titled “parameters?”Initialization parameters
Returns
Section titled “Returns”Certificate
Overrides
Section titled “Overrides”Properties
Section titled “Properties”extensions?
Section titled “extensions?”
optionalextensions?:Extension[]
If present, this field is a SEQUENCE of one or more certificate extensions
Implementation of
Section titled “Implementation of”issuer
Section titled “issuer”issuer:
RelativeDistinguishedNames
The issuer field identifies the entity that has signed and issued the certificate
Implementation of
Section titled “Implementation of”issuerUniqueID?
Section titled “issuerUniqueID?”
optionalissuerUniqueID?:ArrayBuffer
The subject and issuer unique identifiers are present in the certificate to handle the possibility of reuse of subject and/or issuer names over time
Implementation of
Section titled “Implementation of”notAfter
Section titled “notAfter”notAfter:
Time
The date on which the certificate validity period ends
Implementation of
Section titled “Implementation of”notBefore
Section titled “notBefore”notBefore:
Time
The date on which the certificate validity period begins
Implementation of
Section titled “Implementation of”serialNumber
Section titled “serialNumber”serialNumber:
Integer
Serial number of the certificate
Implementation of
Section titled “Implementation of”signature
Section titled “signature”signature:
AlgorithmIdentifier
This field contains the algorithm identifier for the algorithm used by the CA to sign the certificate
Implementation of
Section titled “Implementation of”signatureAlgorithm
Section titled “signatureAlgorithm”signatureAlgorithm:
AlgorithmIdentifier
The signatureAlgorithm field contains the identifier for the cryptographic algorithm used by the CA to sign this certificate
Implementation of
Section titled “Implementation of”ICertificate.signatureAlgorithm
signatureValue
Section titled “signatureValue”signatureValue:
BitString
The signatureValue field contains a digital signature computed upon the ASN.1 DER encoded tbsCertificate
Implementation of
Section titled “Implementation of”subject
Section titled “subject”subject:
RelativeDistinguishedNames
The subject field identifies the entity associated with the public key stored in the subject public key field
Implementation of
Section titled “Implementation of”subjectPublicKeyInfo
Section titled “subjectPublicKeyInfo”subjectPublicKeyInfo:
PublicKeyInfo
This field is used to carry the public key and identify the algorithm with which the key is used
Implementation of
Section titled “Implementation of”ICertificate.subjectPublicKeyInfo
subjectUniqueID?
Section titled “subjectUniqueID?”
optionalsubjectUniqueID?:ArrayBuffer
The subject and issuer unique identifiers are present in the certificate to handle the possibility of reuse of subject and/or issuer names over time
Implementation of
Section titled “Implementation of”tbsView
Section titled “tbsView”tbsView:
Uint8Array
version
Section titled “version”version:
number
Version number
Implementation of
Section titled “Implementation of”CLASS_NAME
Section titled “CLASS_NAME”
staticCLASS_NAME:string="Certificate"
Name of the class
Overrides
Section titled “Overrides”Accessors
Section titled “Accessors”className
Section titled “className”Get Signature
Section titled “Get Signature”get className():
string
Returns
Section titled “Returns”string
Inherited from
Section titled “Inherited from”Get Signature
Section titled “Get Signature”get tbs():
ArrayBuffer
Returns
Section titled “Returns”ArrayBuffer
Set Signature
Section titled “Set Signature”set tbs(
value):void
Parameters
Section titled “Parameters”ArrayBuffer
Returns
Section titled “Returns”void
ToBeSigned (TBS) part of the certificate
Implementation of
Section titled “Implementation of”Methods
Section titled “Methods”encodeTBS()
Section titled “encodeTBS()”encodeTBS():
Sequence
Creates ASN.1 schema for existing values of TBS part for the certificate
Returns
Section titled “Returns”Sequence
ASN.1 SEQUENCE
fromSchema()
Section titled “fromSchema()”fromSchema(
schema):void
Converts parsed ASN.1 object into current class
Parameters
Section titled “Parameters”schema
Section titled “schema”any
ASN.1 schema
Returns
Section titled “Returns”void
Overrides
Section titled “Overrides”getKeyHash()
Section titled “getKeyHash()”getKeyHash(
hashAlgorithm?,crypto?):Promise<ArrayBuffer>
Get hash value for subject public key (default SHA-1)
Parameters
Section titled “Parameters”hashAlgorithm?
Section titled “hashAlgorithm?”string = "SHA-1"
Hashing algorithm name
crypto?
Section titled “crypto?”ICryptoEngine = ...
Crypto engine
Returns
Section titled “Returns”Promise<ArrayBuffer>
Computed hash value from Certificate.tbsCertificate.subjectPublicKeyInfo.subjectPublicKey
getPublicKey()
Section titled “getPublicKey()”getPublicKey(
parameters?,crypto?):Promise<CryptoKey>
Importing public key for current certificate
Parameters
Section titled “Parameters”parameters?
Section titled “parameters?”Public key export parameters
crypto?
Section titled “crypto?”ICryptoEngine = ...
Crypto engine
Returns
Section titled “Returns”Promise<CryptoKey>
WebCrypto public key
sign()
Section titled “sign()”sign(
privateKey,hashAlgorithm?,crypto?):Promise<void>
Make a signature for current value from TBS section
Parameters
Section titled “Parameters”privateKey
Section titled “privateKey”CryptoKey
Private key for SUBJECT_PUBLIC_KEY_INFO structure
hashAlgorithm?
Section titled “hashAlgorithm?”string = "SHA-1"
Hashing algorithm
crypto?
Section titled “crypto?”ICryptoEngine = ...
Crypto engine
Returns
Section titled “Returns”Promise<void>
toJSON()
Section titled “toJSON()”toJSON():
CertificateJson
Converts the class to JSON object
Returns
Section titled “Returns”JSON object
Overrides
Section titled “Overrides”toSchema()
Section titled “toSchema()”toSchema(
encodeFlag?):Sequence
Converts current object to ASN.1 object and sets correct values
Parameters
Section titled “Parameters”encodeFlag?
Section titled “encodeFlag?”boolean = false
If param equal to false then creates schema via decoding stored value. In other case creates schema via assembling from cached parts
Returns
Section titled “Returns”Sequence
ASN.1 object
Overrides
Section titled “Overrides”toString()
Section titled “toString()”toString(
encoding?):string
Parameters
Section titled “Parameters”encoding?
Section titled “encoding?”"hex" | "base64" | "base64url" = "hex"
Returns
Section titled “Returns”string
Inherited from
Section titled “Inherited from”verify()
Section titled “verify()”verify(
issuerCertificate?,crypto?):Promise<boolean>
Verifies the certificate signature
Parameters
Section titled “Parameters”issuerCertificate?
Section titled “issuerCertificate?”Certificate
crypto?
Section titled “crypto?”ICryptoEngine = ...
Crypto engine
Returns
Section titled “Returns”Promise<boolean>
blockName()
Section titled “blockName()”
staticblockName():string
Returns block name
Returns
Section titled “Returns”string
Returns string block name
Inherited from
Section titled “Inherited from”defaultValues()
Section titled “defaultValues()”Call Signature
Section titled “Call Signature”
staticdefaultValues(memberName):ArrayBuffer
Return default values for all class members
Parameters
Section titled “Parameters”memberName
Section titled “memberName”"tbs"
String name for a class member
Returns
Section titled “Returns”ArrayBuffer
Predefined default value
Overrides
Section titled “Overrides”Call Signature
Section titled “Call Signature”
staticdefaultValues(memberName):number
Return default values for all class members
Parameters
Section titled “Parameters”memberName
Section titled “memberName”"version"
String name for a class member
Returns
Section titled “Returns”number
Predefined default value
Overrides
Section titled “Overrides”PkiObject.defaultValues
Call Signature
Section titled “Call Signature”
staticdefaultValues(memberName):Integer
Return default values for all class members
Parameters
Section titled “Parameters”memberName
Section titled “memberName”"serialNumber"
String name for a class member
Returns
Section titled “Returns”Integer
Predefined default value
Overrides
Section titled “Overrides”PkiObject.defaultValues
Call Signature
Section titled “Call Signature”
staticdefaultValues(memberName):AlgorithmIdentifier
Return default values for all class members
Parameters
Section titled “Parameters”memberName
Section titled “memberName”"signature"
String name for a class member
Returns
Section titled “Returns”Predefined default value
Overrides
Section titled “Overrides”PkiObject.defaultValues
Call Signature
Section titled “Call Signature”
staticdefaultValues(memberName):RelativeDistinguishedNames
Return default values for all class members
Parameters
Section titled “Parameters”memberName
Section titled “memberName”"issuer"
String name for a class member
Returns
Section titled “Returns”Predefined default value
Overrides
Section titled “Overrides”PkiObject.defaultValues
Call Signature
Section titled “Call Signature”
staticdefaultValues(memberName):Time
Return default values for all class members
Parameters
Section titled “Parameters”memberName
Section titled “memberName”"notBefore"
String name for a class member
Returns
Section titled “Returns”Predefined default value
Overrides
Section titled “Overrides”PkiObject.defaultValues
Call Signature
Section titled “Call Signature”
staticdefaultValues(memberName):Time
Return default values for all class members
Parameters
Section titled “Parameters”memberName
Section titled “memberName”"notAfter"
String name for a class member
Returns
Section titled “Returns”Predefined default value
Overrides
Section titled “Overrides”PkiObject.defaultValues
Call Signature
Section titled “Call Signature”
staticdefaultValues(memberName):RelativeDistinguishedNames
Return default values for all class members
Parameters
Section titled “Parameters”memberName
Section titled “memberName”"subject"
String name for a class member
Returns
Section titled “Returns”Predefined default value
Overrides
Section titled “Overrides”PkiObject.defaultValues
Call Signature
Section titled “Call Signature”
staticdefaultValues(memberName):PublicKeyInfo
Return default values for all class members
Parameters
Section titled “Parameters”memberName
Section titled “memberName”"subjectPublicKeyInfo"
String name for a class member
Returns
Section titled “Returns”Predefined default value
Overrides
Section titled “Overrides”PkiObject.defaultValues
Call Signature
Section titled “Call Signature”
staticdefaultValues(memberName):ArrayBuffer
Return default values for all class members
Parameters
Section titled “Parameters”memberName
Section titled “memberName”"issuerUniqueID"
String name for a class member
Returns
Section titled “Returns”ArrayBuffer
Predefined default value
Overrides
Section titled “Overrides”PkiObject.defaultValues
Call Signature
Section titled “Call Signature”
staticdefaultValues(memberName):ArrayBuffer
Return default values for all class members
Parameters
Section titled “Parameters”memberName
Section titled “memberName”"subjectUniqueID"
String name for a class member
Returns
Section titled “Returns”ArrayBuffer
Predefined default value
Overrides
Section titled “Overrides”PkiObject.defaultValues
Call Signature
Section titled “Call Signature”
staticdefaultValues(memberName):Extension[]
Return default values for all class members
Parameters
Section titled “Parameters”memberName
Section titled “memberName”"extensions"
String name for a class member
Returns
Section titled “Returns”Predefined default value
Overrides
Section titled “Overrides”PkiObject.defaultValues
Call Signature
Section titled “Call Signature”
staticdefaultValues(memberName):AlgorithmIdentifier
Return default values for all class members
Parameters
Section titled “Parameters”memberName
Section titled “memberName”"signatureAlgorithm"
String name for a class member
Returns
Section titled “Returns”Predefined default value
Overrides
Section titled “Overrides”PkiObject.defaultValues
Call Signature
Section titled “Call Signature”
staticdefaultValues(memberName):BitString
Return default values for all class members
Parameters
Section titled “Parameters”memberName
Section titled “memberName”"signatureValue"
String name for a class member
Returns
Section titled “Returns”BitString
Predefined default value
Overrides
Section titled “Overrides”PkiObject.defaultValues
fromBER()
Section titled “fromBER()”
staticfromBER<T>(this,raw):T
Creates PKI object from the raw data
Type Parameters
Section titled “Type Parameters”T extends PkiObject
Parameters
Section titled “Parameters”PkiObjectConstructor<T>
BufferSource
ASN.1 encoded raw data
Returns
Section titled “Returns”T
Initialized and filled current class object
Inherited from
Section titled “Inherited from”schema()
Section titled “schema()”
staticschema(parameters?):any
Returns value of pre-defined ASN.1 schema for current class
Parameters
Section titled “Parameters”parameters?
Section titled “parameters?”CertificateSchema = {}
Input parameters for the schema
Returns
Section titled “Returns”any
ASN.1 schema object