Skip to content
PKIjsPKIjsPKIjsv4.0.1

Certificate

Represents an X.509 certificate described in RFC5280 Section 4.

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 certificate
const 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" extension
const 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" extension
const bitArray = new ArrayBuffer(1);
const bitView = new Uint8Array(bitArray);
bitView[0] |= 0x02; // Key usage "cRLSign" flag
bitView[0] |= 0x04; // Key usage "keyCertSign" flag
const 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 certificate
await certificate.subjectPublicKeyInfo.importKey(keys.publicKey);
// Signing final certificate
await certificate.sign(keys.privateKey, "SHA-256");
const raw = certificate.toSchema().toBER();

new Certificate(parameters?): Certificate

Initializes a new instance of the Certificate class

CertificateParameters = {}

Initialization parameters

Certificate

PkiObject.constructor

optional extensions?: Extension[]

If present, this field is a SEQUENCE of one or more certificate extensions

ICertificate.extensions


issuer: RelativeDistinguishedNames

The issuer field identifies the entity that has signed and issued the certificate

ICertificate.issuer


optional issuerUniqueID?: 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

ICertificate.issuerUniqueID


notAfter: Time

The date on which the certificate validity period ends

ICertificate.notAfter


notBefore: Time

The date on which the certificate validity period begins

ICertificate.notBefore


serialNumber: Integer

Serial number of the certificate

ICertificate.serialNumber


signature: AlgorithmIdentifier

This field contains the algorithm identifier for the algorithm used by the CA to sign the certificate

ICertificate.signature


signatureAlgorithm: AlgorithmIdentifier

The signatureAlgorithm field contains the identifier for the cryptographic algorithm used by the CA to sign this certificate

ICertificate.signatureAlgorithm


signatureValue: BitString

The signatureValue field contains a digital signature computed upon the ASN.1 DER encoded tbsCertificate

ICertificate.signatureValue


subject: RelativeDistinguishedNames

The subject field identifies the entity associated with the public key stored in the subject public key field

ICertificate.subject


subjectPublicKeyInfo: PublicKeyInfo

This field is used to carry the public key and identify the algorithm with which the key is used

ICertificate.subjectPublicKeyInfo


optional subjectUniqueID?: 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

ICertificate.subjectUniqueID


tbsView: Uint8Array


version: number

Version number

ICertificate.version


static CLASS_NAME: string = "Certificate"

Name of the class

PkiObject.CLASS_NAME

get className(): string

string

PkiObject.className


get tbs(): ArrayBuffer

ArrayBuffer

set tbs(value): void

ArrayBuffer

void

ToBeSigned (TBS) part of the certificate

ICertificate.tbs

encodeTBS(): Sequence

Creates ASN.1 schema for existing values of TBS part for the certificate

Sequence

ASN.1 SEQUENCE


fromSchema(schema): void

Converts parsed ASN.1 object into current class

any

ASN.1 schema

void

PkiObject.fromSchema


getKeyHash(hashAlgorithm?, crypto?): Promise<ArrayBuffer>

Get hash value for subject public key (default SHA-1)

string = "SHA-1"

Hashing algorithm name

ICryptoEngine = ...

Crypto engine

Promise<ArrayBuffer>

Computed hash value from Certificate.tbsCertificate.subjectPublicKeyInfo.subjectPublicKey


getPublicKey(parameters?, crypto?): Promise<CryptoKey>

Importing public key for current certificate

CryptoEnginePublicKeyParams

Public key export parameters

ICryptoEngine = ...

Crypto engine

Promise<CryptoKey>

WebCrypto public key


sign(privateKey, hashAlgorithm?, crypto?): Promise<void>

Make a signature for current value from TBS section

CryptoKey

Private key for SUBJECT_PUBLIC_KEY_INFO structure

string = "SHA-1"

Hashing algorithm

ICryptoEngine = ...

Crypto engine

Promise<void>


toJSON(): CertificateJson

Converts the class to JSON object

CertificateJson

JSON object

PkiObject.toJSON


toSchema(encodeFlag?): Sequence

Converts current object to ASN.1 object and sets correct values

boolean = false

If param equal to false then creates schema via decoding stored value. In other case creates schema via assembling from cached parts

Sequence

ASN.1 object

PkiObject.toSchema


toString(encoding?): string

"hex" | "base64" | "base64url" = "hex"

string

PkiObject.toString


verify(issuerCertificate?, crypto?): Promise<boolean>

Verifies the certificate signature

Certificate

ICryptoEngine = ...

Crypto engine

Promise<boolean>


static blockName(): string

Returns block name

string

Returns string block name

PkiObject.blockName


static defaultValues(memberName): ArrayBuffer

Return default values for all class members

"tbs"

String name for a class member

ArrayBuffer

Predefined default value

PkiObject.defaultValues

static defaultValues(memberName): number

Return default values for all class members

"version"

String name for a class member

number

Predefined default value

PkiObject.defaultValues

static defaultValues(memberName): Integer

Return default values for all class members

"serialNumber"

String name for a class member

Integer

Predefined default value

PkiObject.defaultValues

static defaultValues(memberName): AlgorithmIdentifier

Return default values for all class members

"signature"

String name for a class member

AlgorithmIdentifier

Predefined default value

PkiObject.defaultValues

static defaultValues(memberName): RelativeDistinguishedNames

Return default values for all class members

"issuer"

String name for a class member

RelativeDistinguishedNames

Predefined default value

PkiObject.defaultValues

static defaultValues(memberName): Time

Return default values for all class members

"notBefore"

String name for a class member

Time

Predefined default value

PkiObject.defaultValues

static defaultValues(memberName): Time

Return default values for all class members

"notAfter"

String name for a class member

Time

Predefined default value

PkiObject.defaultValues

static defaultValues(memberName): RelativeDistinguishedNames

Return default values for all class members

"subject"

String name for a class member

RelativeDistinguishedNames

Predefined default value

PkiObject.defaultValues

static defaultValues(memberName): PublicKeyInfo

Return default values for all class members

"subjectPublicKeyInfo"

String name for a class member

PublicKeyInfo

Predefined default value

PkiObject.defaultValues

static defaultValues(memberName): ArrayBuffer

Return default values for all class members

"issuerUniqueID"

String name for a class member

ArrayBuffer

Predefined default value

PkiObject.defaultValues

static defaultValues(memberName): ArrayBuffer

Return default values for all class members

"subjectUniqueID"

String name for a class member

ArrayBuffer

Predefined default value

PkiObject.defaultValues

static defaultValues(memberName): Extension[]

Return default values for all class members

"extensions"

String name for a class member

Extension[]

Predefined default value

PkiObject.defaultValues

static defaultValues(memberName): AlgorithmIdentifier

Return default values for all class members

"signatureAlgorithm"

String name for a class member

AlgorithmIdentifier

Predefined default value

PkiObject.defaultValues

static defaultValues(memberName): BitString

Return default values for all class members

"signatureValue"

String name for a class member

BitString

Predefined default value

PkiObject.defaultValues


static fromBER<T>(this, raw): T

Creates PKI object from the raw data

T extends PkiObject

PkiObjectConstructor<T>

BufferSource

ASN.1 encoded raw data

T

Initialized and filled current class object

PkiObject.fromBER


static schema(parameters?): any

Returns value of pre-defined ASN.1 schema for current class

CertificateSchema = {}

Input parameters for the schema

any

ASN.1 schema object

PkiObject.schema