Skip to content
PKIjsPKIjsPKIjsv4.0.1

EnvelopedData

Represents the EnvelopedData structure described in RFC5652

The following example demonstrates how to create and encrypt CMS Enveloped Data

const cmsEnveloped = new pkijs.EnvelopedData();
// Add recipient
cmsEnveloped.addRecipientByCertificate(cert, { oaepHashAlgorithm: "SHA-256" });
// Secret key algorithm
const alg = {
name: "AES-GCM",
length: 256,
}
await cmsEnveloped.encrypt(alg, dataToEncrypt);
// Add Enveloped Data into CMS Content Info
const cmsContent = new pkijs.ContentInfo();
cmsContent.contentType = pkijs.ContentInfo.ENVELOPED_DATA;
cmsContent.content = cmsEnveloped.toSchema();
const cmsContentRaw = cmsContent.toSchema().toBER();

The following example demonstrates how to decrypt CMS Enveloped Data

// Get a "crypto" extension
const crypto = pkijs.getCrypto();
// Parse CMS Content Info
const cmsContent = pkijs.ContentInfo.fromBER(cmsContentRaw);
if (cmsContent.contentType !== pkijs.ContentInfo.ENVELOPED_DATA) {
throw new Error("CMS is not Enveloped Data");
}
// Parse CMS Enveloped Data
const cmsEnveloped = new pkijs.EnvelopedData({ schema: cmsContent.content });
// Export private key to PKCS#8
const pkcs8 = await crypto.exportKey("pkcs8", keys.privateKey);
// Decrypt data
const decryptedData = await cmsEnveloped.decrypt(0, {
recipientCertificate: cert,
recipientPrivateKey: pkcs8,
});

new EnvelopedData(parameters?): EnvelopedData

Initializes a new instance of the EnvelopedData class

EnvelopedDataParameters = {}

Initialization parameters

EnvelopedData

PkiObject.constructor

encryptedContentInfo: EncryptedContentInfo

Encrypted content information

IEnvelopedData.encryptedContentInfo


optional originatorInfo?: OriginatorInfo

Optionally provides information about the originator. It is present only if required by the key management algorithm. It may contain certificates and CRLs.

IEnvelopedData.originatorInfo


policy: Required<EncryptedContentInfoSplit>


recipientInfos: RecipientInfo[]

Collection of per-recipient information. There MUST be at least one element in the collection.

IEnvelopedData.recipientInfos


optional unprotectedAttrs?: Attribute[]

Collection of attributes that are not encrypted

IEnvelopedData.unprotectedAttrs


version: number

Version number.

The appropriate value depends on originatorInfo, RecipientInfo, and unprotectedAttrs.

The version MUST be assigned as follows:

IF (originatorInfo is present) AND
((any certificates with a type of other are present) OR
(any crls with a type of other are present))
THEN version is 4
ELSE
IF ((originatorInfo is present) AND
(any version 2 attribute certificates are present)) OR
(any RecipientInfo structures include pwri) OR
(any RecipientInfo structures include ori)
THEN version is 3
ELSE
IF (originatorInfo is absent) AND
(unprotectedAttrs is absent) AND
(all RecipientInfo structures are version 0)
THEN version is 0
ELSE version is 2

IEnvelopedData.version


static CLASS_NAME: string = "EnvelopedData"

Name of the class

PkiObject.CLASS_NAME

get className(): string

string

PkiObject.className

addRecipientByCertificate(certificate, parameters?, variant?, crypto?): boolean

Helpers function for filling “RecipientInfo” based on recipient’s certificate. Problem with WebCrypto is that for RSA certificates we have only one option - “key transport” and for ECC certificates we also have one option - “key agreement”. As soon as Google will implement DH algorithm it would be possible to use “key agreement” also for RSA certificates.

Certificate

Recipient’s certificate

object

Additional parameters necessary for “fine tunning” of encryption process

number

Variant = 1 is for “key transport”, variant = 2 is for “key agreement”. In fact the “variant” is unnecessary now because Google has no DH algorithm implementation. Thus key encryption scheme would be choosen by certificate type only: “key transport” for RSA and “key agreement” for ECC certificates.

ICryptoEngine = ...

Crypto engine

boolean


addRecipientByKeyIdentifier(key?, keyId?, parameters?, crypto?): void

Add a “RecipientInfo” using a KeyAgreeRecipientInfo of type RecipientKeyIdentifier.

CryptoKey

Recipient’s public key

ArrayBuffer

The id for the recipient’s public key

any

Additional parameters for “fine tuning” the encryption process

ICryptoEngine = ...

Crypto engine

void


addRecipientByPreDefinedData(preDefinedData, parameters?, variant, crypto?): void

Add recipient based on pre-defined data like password or KEK

ArrayBuffer

ArrayBuffer with pre-defined data

Additional parameters necessary for “fine tunning” of encryption process

string

number

AesKeyGenParams

any

ArrayBuffer

number

Variant = 1 for pre-defined “key encryption key” (KEK). Variant = 2 for password-based encryption.

ICryptoEngine = ...

Crypto engine

void


decrypt(recipientIndex, parameters, crypto?): Promise<ArrayBuffer>

Decrypts existing CMS Enveloped Data content

number

Index of recipient

EnvelopedDataDecryptParams

Additional parameters

ICryptoEngine = ...

Crypto engine

Promise<ArrayBuffer>


encrypt(contentEncryptionAlgorithm, contentToEncrypt, crypto?): Promise<(void | { ecdhPrivateKey: CryptoKey; })[]>

Creates a new CMS Enveloped Data content with encrypted data

Algorithm

WebCrypto algorithm. For the moment here could be only “AES-CBC” or “AES-GCM” algorithms.

ArrayBuffer

Content to encrypt

ICryptoEngine = ...

Crypto engine

Promise<(void | { ecdhPrivateKey: CryptoKey; })[]>


fromSchema(schema): void

Converts parsed ASN.1 object into current class

any

ASN.1 schema

void

PkiObject.fromSchema


toJSON(): EnvelopedDataJson

Converts the class to JSON object

EnvelopedDataJson

JSON object

PkiObject.toJSON


toSchema(): Sequence

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

Sequence

ASN.1 object

PkiObject.toSchema


toString(encoding?): string

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

string

PkiObject.toString


static blockName(): string

Returns block name

string

Returns string block name

PkiObject.blockName


static compareWithDefault(memberName, memberValue): boolean

Compare values with default values for all class members

string

String name for a class member

any

Value to compare with default value

boolean


static defaultValues(memberName): number

Returns default values for all class members

"version"

String name for a class member

number

Default value

PkiObject.defaultValues

static defaultValues(memberName): OriginatorInfo

Returns default values for all class members

"originatorInfo"

String name for a class member

OriginatorInfo

Default value

PkiObject.defaultValues

static defaultValues(memberName): RecipientInfo[]

Returns default values for all class members

"recipientInfos"

String name for a class member

RecipientInfo[]

Default value

PkiObject.defaultValues

static defaultValues(memberName): EncryptedContentInfo

Returns default values for all class members

"encryptedContentInfo"

String name for a class member

EncryptedContentInfo

Default value

PkiObject.defaultValues

static defaultValues(memberName): Attribute[]

Returns default values for all class members

"unprotectedAttrs"

String name for a class member

Attribute[]

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

SchemaParameters<{ encryptedContentInfo?: EncryptedContentInfoSchema; originatorInfo?: string; recipientInfos?: string; unprotectedAttrs?: string; version?: string; }> = {}

Input parameters for the schema

any

ASN.1 schema object

PkiObject.schema