EnvelopedData
Represents the EnvelopedData structure described in RFC5652
Examples
Section titled “Examples”The following example demonstrates how to create and encrypt CMS Enveloped Data
const cmsEnveloped = new pkijs.EnvelopedData();
// Add recipientcmsEnveloped.addRecipientByCertificate(cert, { oaepHashAlgorithm: "SHA-256" });
// Secret key algorithmconst alg = { name: "AES-GCM", length: 256,}await cmsEnveloped.encrypt(alg, dataToEncrypt);
// Add Enveloped Data into CMS Content Infoconst 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" extensionconst crypto = pkijs.getCrypto();
// Parse CMS Content Infoconst cmsContent = pkijs.ContentInfo.fromBER(cmsContentRaw);if (cmsContent.contentType !== pkijs.ContentInfo.ENVELOPED_DATA) { throw new Error("CMS is not Enveloped Data");}// Parse CMS Enveloped Dataconst cmsEnveloped = new pkijs.EnvelopedData({ schema: cmsContent.content });
// Export private key to PKCS#8const pkcs8 = await crypto.exportKey("pkcs8", keys.privateKey);
// Decrypt dataconst decryptedData = await cmsEnveloped.decrypt(0, { recipientCertificate: cert, recipientPrivateKey: pkcs8,});Extends
Section titled “Extends”Implements
Section titled “Implements”Constructors
Section titled “Constructors”Constructor
Section titled “Constructor”new EnvelopedData(
parameters?):EnvelopedData
Initializes a new instance of the EnvelopedData class
Parameters
Section titled “Parameters”parameters?
Section titled “parameters?”Initialization parameters
Returns
Section titled “Returns”EnvelopedData
Overrides
Section titled “Overrides”Properties
Section titled “Properties”encryptedContentInfo
Section titled “encryptedContentInfo”encryptedContentInfo:
EncryptedContentInfo
Encrypted content information
Implementation of
Section titled “Implementation of”IEnvelopedData.encryptedContentInfo
originatorInfo?
Section titled “originatorInfo?”
optionaloriginatorInfo?:OriginatorInfo
Optionally provides information about the originator. It is present only if required by the key management algorithm. It may contain certificates and CRLs.
Implementation of
Section titled “Implementation of”policy
Section titled “policy”policy:
Required<EncryptedContentInfoSplit>
recipientInfos
Section titled “recipientInfos”recipientInfos:
RecipientInfo[]
Collection of per-recipient information. There MUST be at least one element in the collection.
Implementation of
Section titled “Implementation of”unprotectedAttrs?
Section titled “unprotectedAttrs?”
optionalunprotectedAttrs?:Attribute[]
Collection of attributes that are not encrypted
Implementation of
Section titled “Implementation of”IEnvelopedData.unprotectedAttrs
version
Section titled “version”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 4ELSE 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 2Implementation of
Section titled “Implementation of”CLASS_NAME
Section titled “CLASS_NAME”
staticCLASS_NAME:string="EnvelopedData"
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”Methods
Section titled “Methods”addRecipientByCertificate()
Section titled “addRecipientByCertificate()”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.
Parameters
Section titled “Parameters”certificate
Section titled “certificate”Recipient’s certificate
parameters?
Section titled “parameters?”object
Additional parameters necessary for “fine tunning” of encryption process
variant?
Section titled “variant?”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.
crypto?
Section titled “crypto?”ICryptoEngine = ...
Crypto engine
Returns
Section titled “Returns”boolean
addRecipientByKeyIdentifier()
Section titled “addRecipientByKeyIdentifier()”addRecipientByKeyIdentifier(
key?,keyId?,parameters?,crypto?):void
Add a “RecipientInfo” using a KeyAgreeRecipientInfo of type RecipientKeyIdentifier.
Parameters
Section titled “Parameters”CryptoKey
Recipient’s public key
keyId?
Section titled “keyId?”ArrayBuffer
The id for the recipient’s public key
parameters?
Section titled “parameters?”any
Additional parameters for “fine tuning” the encryption process
crypto?
Section titled “crypto?”ICryptoEngine = ...
Crypto engine
Returns
Section titled “Returns”void
addRecipientByPreDefinedData()
Section titled “addRecipientByPreDefinedData()”addRecipientByPreDefinedData(
preDefinedData,parameters?,variant,crypto?):void
Add recipient based on pre-defined data like password or KEK
Parameters
Section titled “Parameters”preDefinedData
Section titled “preDefinedData”ArrayBuffer
ArrayBuffer with pre-defined data
parameters?
Section titled “parameters?”Additional parameters necessary for “fine tunning” of encryption process
hmacHashAlgorithm?
Section titled “hmacHashAlgorithm?”string
iterationCount?
Section titled “iterationCount?”number
keyEncryptionAlgorithm?
Section titled “keyEncryptionAlgorithm?”AesKeyGenParams
keyEncryptionAlgorithmParams?
Section titled “keyEncryptionAlgorithmParams?”any
keyIdentifier?
Section titled “keyIdentifier?”ArrayBuffer
variant
Section titled “variant”number
Variant = 1 for pre-defined “key encryption key” (KEK). Variant = 2 for password-based encryption.
crypto?
Section titled “crypto?”ICryptoEngine = ...
Crypto engine
Returns
Section titled “Returns”void
decrypt()
Section titled “decrypt()”decrypt(
recipientIndex,parameters,crypto?):Promise<ArrayBuffer>
Decrypts existing CMS Enveloped Data content
Parameters
Section titled “Parameters”recipientIndex
Section titled “recipientIndex”number
Index of recipient
parameters
Section titled “parameters”Additional parameters
crypto?
Section titled “crypto?”ICryptoEngine = ...
Crypto engine
Returns
Section titled “Returns”Promise<ArrayBuffer>
encrypt()
Section titled “encrypt()”encrypt(
contentEncryptionAlgorithm,contentToEncrypt,crypto?):Promise<(void| {ecdhPrivateKey:CryptoKey; })[]>
Creates a new CMS Enveloped Data content with encrypted data
Parameters
Section titled “Parameters”contentEncryptionAlgorithm
Section titled “contentEncryptionAlgorithm”Algorithm
WebCrypto algorithm. For the moment here could be only “AES-CBC” or “AES-GCM” algorithms.
contentToEncrypt
Section titled “contentToEncrypt”ArrayBuffer
Content to encrypt
crypto?
Section titled “crypto?”ICryptoEngine = ...
Crypto engine
Returns
Section titled “Returns”Promise<(void | { ecdhPrivateKey: CryptoKey; })[]>
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”toJSON()
Section titled “toJSON()”toJSON():
EnvelopedDataJson
Converts the class to JSON object
Returns
Section titled “Returns”JSON object
Overrides
Section titled “Overrides”toSchema()
Section titled “toSchema()”toSchema():
Sequence
Converts current object to ASN.1 object and sets correct values
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”blockName()
Section titled “blockName()”
staticblockName():string
Returns block name
Returns
Section titled “Returns”string
Returns string block name
Inherited from
Section titled “Inherited from”compareWithDefault()
Section titled “compareWithDefault()”
staticcompareWithDefault(memberName,memberValue):boolean
Compare values with default values for all class members
Parameters
Section titled “Parameters”memberName
Section titled “memberName”string
String name for a class member
memberValue
Section titled “memberValue”any
Value to compare with default value
Returns
Section titled “Returns”boolean
defaultValues()
Section titled “defaultValues()”Call Signature
Section titled “Call Signature”
staticdefaultValues(memberName):number
Returns 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
Default value
Overrides
Section titled “Overrides”Call Signature
Section titled “Call Signature”
staticdefaultValues(memberName):OriginatorInfo
Returns default values for all class members
Parameters
Section titled “Parameters”memberName
Section titled “memberName”"originatorInfo"
String name for a class member
Returns
Section titled “Returns”Default value
Overrides
Section titled “Overrides”PkiObject.defaultValues
Call Signature
Section titled “Call Signature”
staticdefaultValues(memberName):RecipientInfo[]
Returns default values for all class members
Parameters
Section titled “Parameters”memberName
Section titled “memberName”"recipientInfos"
String name for a class member
Returns
Section titled “Returns”Default value
Overrides
Section titled “Overrides”PkiObject.defaultValues
Call Signature
Section titled “Call Signature”
staticdefaultValues(memberName):EncryptedContentInfo
Returns default values for all class members
Parameters
Section titled “Parameters”memberName
Section titled “memberName”"encryptedContentInfo"
String name for a class member
Returns
Section titled “Returns”Default value
Overrides
Section titled “Overrides”PkiObject.defaultValues
Call Signature
Section titled “Call Signature”
staticdefaultValues(memberName):Attribute[]
Returns default values for all class members
Parameters
Section titled “Parameters”memberName
Section titled “memberName”"unprotectedAttrs"
String name for a class member
Returns
Section titled “Returns”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?”SchemaParameters<{ encryptedContentInfo?: EncryptedContentInfoSchema; originatorInfo?: string; recipientInfos?: string; unprotectedAttrs?: string; version?: string; }> = {}
Input parameters for the schema
Returns
Section titled “Returns”any
ASN.1 schema object