Skip to content

Commit

Permalink
Merge pull request #355 from PeculiarVentures:fix-minor
Browse files Browse the repository at this point in the history
Fix minor type errors
  • Loading branch information
microshine authored May 31, 2022
2 parents 738b4c9 + 0c77b0d commit 7e3d023
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 15 deletions.
4 changes: 3 additions & 1 deletion src/CryptoEngine/CryptoEngine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1565,6 +1565,7 @@ export class CryptoEngine extends AbstractCryptoEngine {

//#region Initial variables

// TODO Should we reuse iv from parameters.contentEncryptionAlgorithm or use it's length for ivBuffer?
const ivBuffer = new ArrayBuffer(16); // For AES we need IV 16 bytes long
const ivView = new Uint8Array(ivBuffer);
this.getRandomValues(ivView);
Expand Down Expand Up @@ -1606,12 +1607,13 @@ export class CryptoEngine extends AbstractCryptoEngine {
iterations: parameters.iterationCount
},
pbkdfKey,
parameters.contentEncryptionAlgorithm as any,
parameters.contentEncryptionAlgorithm,
false,
["encrypt"]);
//#endregion

//#region Encrypt content
// TODO encrypt doesn't use all parameters from parameters.contentEncryptionAlgorithm (eg additionalData and tagLength for AES-GCM)
const encryptedData = await this.encrypt(
{
name: parameters.contentEncryptionAlgorithm.name,
Expand Down
7 changes: 6 additions & 1 deletion src/CryptoEngine/CryptoEngineInterface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,14 @@ export interface CryptoEnginePublicKeyParams {
algorithm: CryptoEngineAlgorithmParams;
}


export type ContentEncryptionAesCbcParams = AesCbcParams & AesDerivedKeyParams;
export type ContentEncryptionAesGcmParams = AesGcmParams & AesDerivedKeyParams;
export type ContentEncryptionAlgorithm = ContentEncryptionAesCbcParams | ContentEncryptionAesGcmParams;

export interface CryptoEngineEncryptParams {
password: ArrayBuffer;
contentEncryptionAlgorithm: Algorithm;
contentEncryptionAlgorithm: ContentEncryptionAlgorithm;
hmacHashAlgorithm: string;
iterationCount: number;
contentToEncrypt: ArrayBuffer;
Expand Down
4 changes: 3 additions & 1 deletion src/EncryptedData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ export interface EncryptedDataJson {

export type EncryptedDataParameters = PkiObjectParameters & Partial<IEncryptedData>;

export type EncryptedDataEncryptParams = Omit<CryptoEngineEncryptParams, "contentType">;

/**
* Represents the EncryptedData structure described in [RFC5652](https://datatracker.ietf.org/doc/html/rfc5652)
*
Expand Down Expand Up @@ -262,7 +264,7 @@ export class EncryptedData extends PkiObject implements IEncryptedData {
* Creates a new CMS Encrypted Data content
* @param parameters Parameters necessary for encryption
*/
public async encrypt(parameters: Omit<CryptoEngineEncryptParams, "contentType">): Promise<void> {
public async encrypt(parameters: EncryptedDataEncryptParams): Promise<void> {
//#region Check for input parameters
ArgumentError.assert(parameters, "parameters", "object");
//#endregion
Expand Down
9 changes: 5 additions & 4 deletions src/PKCS8ShroudedKeyBag.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import * as asn1js from "asn1js";
import * as pvutils from "pvutils";
import { AlgorithmIdentifier, AlgorithmIdentifierJson, AlgorithmIdentifierSchema } from "./AlgorithmIdentifier";
import { EncryptedData } from "./EncryptedData";
import { EncryptedData, EncryptedDataEncryptParams } from "./EncryptedData";
import { EncryptedContentInfo } from "./EncryptedContentInfo";
import { PrivateKeyInfo } from "./PrivateKeyInfo";
import * as Schema from "./Schema";
import { CryptoEngineEncryptParams } from "./CryptoEngine/CryptoEngineInterface";
import { AsnError } from "./errors";
import { PkiObject, PkiObjectParameters } from "./PkiObject";
import { EMPTY_STRING } from "./constants";
Expand All @@ -32,6 +31,8 @@ export interface PKCS8ShroudedKeyBagJson {
encryptedData: asn1js.OctetStringJson;
}

type PKCS8ShroudedKeyBagMakeInternalValuesParams = Omit<EncryptedDataEncryptParams, "contentToEncrypt">;

/**
* Represents the PKCS8ShroudedKeyBag structure described in [RFC7292](https://datatracker.ietf.org/doc/html/rfc7292)
*/
Expand Down Expand Up @@ -214,7 +215,7 @@ export class PKCS8ShroudedKeyBag extends PkiObject implements IPKCS8ShroudedKeyB
//#endregion
}

public async makeInternalValues(parameters: Omit<CryptoEngineEncryptParams, "contentToEncrypt">): Promise<void> {
public async makeInternalValues(parameters: PKCS8ShroudedKeyBagMakeInternalValuesParams): Promise<void> {
//#region Check that we do have PARSED_VALUE
if (!this.parsedValue) {
throw new Error("Please initialize \"parsedValue\" first");
Expand All @@ -226,7 +227,7 @@ export class PKCS8ShroudedKeyBag extends PkiObject implements IPKCS8ShroudedKeyB
//#endregion

//#region Encrypt internal data
const encryptParams: CryptoEngineEncryptParams = {
const encryptParams: EncryptedDataEncryptParams = {
...parameters,
contentToEncrypt: this.parsedValue.toSchema().toBER(false),
};
Expand Down
16 changes: 8 additions & 8 deletions src/SafeBag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ const CLEAR_PROPS = [
BAG_ATTRIBUTES
];

export interface ISafeBag {
export interface ISafeBag<T extends BagType = BagType> {
bagId: string;
bagValue: BagType;
bagValue: T;
bagAttributes?: Attribute[];
}

export type SafeBagParameters = PkiObjectParameters & Partial<ISafeBag>;
export type SafeBagParameters<T extends BagType = BagType> = PkiObjectParameters & Partial<ISafeBag<T>>;

export interface SafeBagJson {
bagId: string;
Expand All @@ -31,23 +31,23 @@ export interface SafeBagJson {
/**
* Represents the SafeBag structure described in [RFC7292](https://datatracker.ietf.org/doc/html/rfc7292)
*/
export class SafeBag extends PkiObject implements ISafeBag {
export class SafeBag<T extends BagType = BagType> extends PkiObject implements ISafeBag<T> {

public static override CLASS_NAME = "SafeBag";

public bagId!: string;
public bagValue!: BagType;
public bagValue!: T;
public bagAttributes?: Attribute[];

/**
* Initializes a new instance of the {@link SafeBag} class
* @param parameters Initialization parameters
*/
constructor(parameters: SafeBagParameters = {}) {
constructor(parameters: SafeBagParameters<T> = {}) {
super();

this.bagId = pvutils.getParametersValue(parameters, BAG_ID, SafeBag.defaultValues(BAG_ID));
this.bagValue = pvutils.getParametersValue(parameters, BAG_VALUE, SafeBag.defaultValues(BAG_VALUE));
this.bagValue = pvutils.getParametersValue(parameters, BAG_VALUE, SafeBag.defaultValues(BAG_VALUE)) as unknown as T;
if (BAG_ATTRIBUTES in parameters) {
this.bagAttributes = pvutils.getParametersValue(parameters, BAG_ATTRIBUTES, SafeBag.defaultValues(BAG_ATTRIBUTES));
}
Expand Down Expand Up @@ -162,7 +162,7 @@ export class SafeBag extends PkiObject implements ISafeBag {
if (!bagType) {
throw new Error(`Invalid BAG_ID for SafeBag: ${this.bagId}`);
}
this.bagValue = new bagType({ schema: asn1.result.bagValue });
this.bagValue = new bagType({ schema: asn1.result.bagValue }) as unknown as T;

if (BAG_ATTRIBUTES in asn1.result) {
this.bagAttributes = Array.from(asn1.result.bagAttributes, element => new Attribute({ schema: element }));
Expand Down

0 comments on commit 7e3d023

Please sign in to comment.