Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
if(("hmacHashAlgorithm" in encryptionParameters) === false)
encryptionParameters.hmacHashAlgorithm = "SHA-512";
if(("iterationCount" in encryptionParameters) === false)
encryptionParameters.iterationCount = 2048;
if(("keyEncryptionAlgorithm" in encryptionParameters) === false)
{
encryptionParameters.keyEncryptionAlgorithm = {
name: "AES-KW",
length: 256
};
}
if(("keyEncryptionAlgorithmParams" in encryptionParameters) === false)
encryptionParameters.keyEncryptionAlgorithmParams = new asn1js.Null();
//endregion
//region Add new recipient based on passed variant
switch(variant)
{
case 1: // KEKRecipientInfo
{
//region keyEncryptionAlgorithm
const kekOID = getOIDByAlgorithm(encryptionParameters.keyEncryptionAlgorithm);
if(kekOID === "")
throw new Error("Incorrect value for \"keyEncryptionAlgorithm\"");
//endregion
//region KEKRecipientInfo
const keyInfo = new KEKRecipientInfo({
version: 4,
static defaultValues(memberName)
{
switch(memberName)
{
case "salt":
return {};
case "iterationCount":
return (-1);
case "keyLength":
return 0;
case "prf":
return new AlgorithmIdentifier({
algorithmId: "1.3.14.3.2.26", // SHA-1
algorithmParams: new asn1js.Null()
});
default:
throw new Error(`Invalid member name for PBKDF2Params class: ${memberName}`);
}
}
//**********************************************************************************
() => {
if(hashAlgorithm.toUpperCase() !== "SHA-1")
{
const oid = getOIDByAlgorithm({ name: hashAlgorithm });
if(oid === "")
return Promise.reject(`Incorrect hashing algorithm: ${hashAlgorithm}`);
_this._crlHashType = 1;
_this.crlHash = new OtherHashAlgAndValue({
hashAlgorithm: new AlgorithmIdentifier({
algorithmId: oid,
algorithmParams: new asn1js.Null()
})
});
}
else
_this._crlHashType = 0;
return Promise.resolve();
}
);
//region Initial variables
const ivBuffer = new ArrayBuffer(parameters.contentEncryptionAlgorithm.iv.length);
const ivView = new Uint8Array(ivBuffer);
this.getRandomValues(ivView);
const saltBuffer = new ArrayBuffer(8);
const saltView = new Uint8Array(saltBuffer);
this.getRandomValues(saltView);
const pbkdf2Params = new PBKDF2Params({
salt: new asn1js.OctetString({ valueHex: saltBuffer }),
iterationCount: parameters.iterationCount,
prf: new AlgorithmIdentifier({
algorithmId: hmacOID,
algorithmParams: new asn1js.Null()
})
});
//endregion
//region Encrypt data using PBKDF2 as a source for key
sequence = sequence.then(() =>
nodeSpecificCrypto.encryptUsingPBKDF2Password(parameters.contentEncryptionAlgorithm.name, parameters.contentEncryptionAlgorithm.length, parameters.password, saltBuffer, parameters.iterationCount, parameters.hmacHashAlgorithm, ivBuffer, parameters.contentToEncrypt)
);
//endregion
//region Store all parameters in EncryptedData object
sequence = sequence.then(result =>
{
const pbes2Parameters = new PBES2Params({
keyDerivationFunc: new AlgorithmIdentifier({
algorithmId: pbkdf2OID,
{
kwLengthView[j] = kwLength;
kwLength >>= 8;
}
//endregion
//region Create and encode "ECC-CMS-SharedInfo" structure
const eccInfo = new ECCCMSSharedInfo({
keyInfo: new AlgorithmIdentifier({
algorithmId: aesKWAlgorithm.algorithmId,
/*
Initially RFC5753 says that AES algorithms have absent parameters.
But since early implementations all put NULL here. Thus, in order to be
"backward compatible", index also put NULL here.
*/
algorithmParams: new asn1js.Null()
}),
entityUInfo: _this.recipientInfos[index].value.ukm,
suppPubInfo: new asn1js.OctetString({ valueHex: kwLengthBuffer })
});
const encodedInfo = eccInfo.toSchema().toBER(false);
//endregion
//region Get SHA algorithm used together with ECDH
const ecdhAlgorithm = getAlgorithmByOID(_this.recipientInfos[index].value.keyEncryptionAlgorithm.algorithmId);
if(("name" in ecdhAlgorithm) === false)
return Promise.reject(`Incorrect OID for key encryption algorithm: ${_this.recipientInfos[index].value.keyEncryptionAlgorithm.algorithmId}`);
//endregion
return kdf(ecdhAlgorithm.kdf, result, KWalgorithm.length, encodedInfo);
},
if(hashAlgorithmOID === "")
return Promise.reject(`Unsupported hash algorithm: ${hashAlgorithm}`);
//endregion
//region Append information about hash algorithm
if((this.digestAlgorithms.filter(algorithm => algorithm.algorithmId === hashAlgorithmOID)).length === 0)
{
this.digestAlgorithms.push(new AlgorithmIdentifier({
algorithmId: hashAlgorithmOID,
algorithmParams: new asn1js.Null()
}));
}
this.signerInfos[signerIndex].digestAlgorithm = new AlgorithmIdentifier({
algorithmId: hashAlgorithmOID,
algorithmParams: new asn1js.Null()
});
//endregion
//region Get a "default parameters" for current algorithm and set correct signature algorithm
sequence = sequence.then(() => engine.subtle.getSignatureParameters(privateKey, hashAlgorithm));
sequence = sequence.then(result =>
{
parameters = result.parameters;
this.signerInfos[signerIndex].signatureAlgorithm = result.signatureAlgorithm;
});
//endregion
//region Create TBS data for signing
sequence = sequence.then(() =>
{
get certificate()
{
return (new Certificate({
schema: new asn1js.Sequence({
value: [
this.tbsCertificate,
(new AlgorithmIdentifier({
algorithmId: "1.2.840.113549.1.1.11",
algorithmParams: new asn1js.Null()
})).toSchema(),
new asn1js.BitString({
valueHex: new ArrayBuffer(2),
unusedBits: 0
})
]
})
}));
}
//**********************************************************************************
() => {
if(hashAlgorithm.toUpperCase() !== "SHA-1")
{
const oid = getOIDByAlgorithm({ name: hashAlgorithm });
if(oid === "")
return Promise.reject(`Incorrect hashing algorithm: ${hashAlgorithm}`);
this.ocspRepHash = new OtherHashAlgAndValue({
hashAlgorithm: new AlgorithmIdentifier({
algorithmId: oid,
algorithmParams: new asn1js.Null()
})
});
}
return Promise.resolve();
}
);
})).then(result =>
{
return getTSPResponse(new TimeStampReq({
version: 1,
messageImprint: new MessageImprint({
hashAlgorithm: new AlgorithmIdentifier({
algorithmId: "1.3.14.3.2.26",
algorithmParams: new asn1js.Null()
}),
hashedMessage: new asn1js.OctetString({ valueHex: result })
})
}));
}).then(result =>
{
name: "HMAC",
hash: {
name: encryptionParameters.hmacHashAlgorithm
}
});
if(hmacOID === "")
throw new Error(`Incorrect value for "hmacHashAlgorithm": ${encryptionParameters.hmacHashAlgorithm}`);
//endregion
//region PBKDF2-params
const pbkdf2Params = new PBKDF2Params({
salt: new asn1js.OctetString({ valueHex: saltBuffer }),
iterationCount: encryptionParameters.iterationCount,
prf: new AlgorithmIdentifier({
algorithmId: hmacOID,
algorithmParams: new asn1js.Null()
})
});
//endregion
//region keyEncryptionAlgorithm
const kekOID = getOIDByAlgorithm(encryptionParameters.keyEncryptionAlgorithm);
if(kekOID === "")
throw new Error("Incorrect value for \"keyEncryptionAlgorithm\"");
//endregion
//region PasswordRecipientinfo
const keyInfo = new PasswordRecipientinfo({
version: 0,
keyDerivationAlgorithm: new AlgorithmIdentifier({
algorithmId: pbkdf2OID,
algorithmParams: pbkdf2Params.toSchema()