Skip to content

Commit 7e3d023

Browse files
authored
Merge pull request #355 from PeculiarVentures:fix-minor
Fix minor type errors
2 parents 738b4c9 + 0c77b0d commit 7e3d023

File tree

5 files changed

+25
-15
lines changed

5 files changed

+25
-15
lines changed

src/CryptoEngine/CryptoEngine.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1565,6 +1565,7 @@ export class CryptoEngine extends AbstractCryptoEngine {
15651565

15661566
//#region Initial variables
15671567

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

16141615
//#region Encrypt content
1616+
// TODO encrypt doesn't use all parameters from parameters.contentEncryptionAlgorithm (eg additionalData and tagLength for AES-GCM)
16151617
const encryptedData = await this.encrypt(
16161618
{
16171619
name: parameters.contentEncryptionAlgorithm.name,

src/CryptoEngine/CryptoEngineInterface.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,14 @@ export interface CryptoEnginePublicKeyParams {
3838
algorithm: CryptoEngineAlgorithmParams;
3939
}
4040

41+
42+
export type ContentEncryptionAesCbcParams = AesCbcParams & AesDerivedKeyParams;
43+
export type ContentEncryptionAesGcmParams = AesGcmParams & AesDerivedKeyParams;
44+
export type ContentEncryptionAlgorithm = ContentEncryptionAesCbcParams | ContentEncryptionAesGcmParams;
45+
4146
export interface CryptoEngineEncryptParams {
4247
password: ArrayBuffer;
43-
contentEncryptionAlgorithm: Algorithm;
48+
contentEncryptionAlgorithm: ContentEncryptionAlgorithm;
4449
hmacHashAlgorithm: string;
4550
iterationCount: number;
4651
contentToEncrypt: ArrayBuffer;

src/EncryptedData.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ export interface EncryptedDataJson {
4343

4444
export type EncryptedDataParameters = PkiObjectParameters & Partial<IEncryptedData>;
4545

46+
export type EncryptedDataEncryptParams = Omit<CryptoEngineEncryptParams, "contentType">;
47+
4648
/**
4749
* Represents the EncryptedData structure described in [RFC5652](https://datatracker.ietf.org/doc/html/rfc5652)
4850
*
@@ -262,7 +264,7 @@ export class EncryptedData extends PkiObject implements IEncryptedData {
262264
* Creates a new CMS Encrypted Data content
263265
* @param parameters Parameters necessary for encryption
264266
*/
265-
public async encrypt(parameters: Omit<CryptoEngineEncryptParams, "contentType">): Promise<void> {
267+
public async encrypt(parameters: EncryptedDataEncryptParams): Promise<void> {
266268
//#region Check for input parameters
267269
ArgumentError.assert(parameters, "parameters", "object");
268270
//#endregion

src/PKCS8ShroudedKeyBag.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
import * as asn1js from "asn1js";
22
import * as pvutils from "pvutils";
33
import { AlgorithmIdentifier, AlgorithmIdentifierJson, AlgorithmIdentifierSchema } from "./AlgorithmIdentifier";
4-
import { EncryptedData } from "./EncryptedData";
4+
import { EncryptedData, EncryptedDataEncryptParams } from "./EncryptedData";
55
import { EncryptedContentInfo } from "./EncryptedContentInfo";
66
import { PrivateKeyInfo } from "./PrivateKeyInfo";
77
import * as Schema from "./Schema";
8-
import { CryptoEngineEncryptParams } from "./CryptoEngine/CryptoEngineInterface";
98
import { AsnError } from "./errors";
109
import { PkiObject, PkiObjectParameters } from "./PkiObject";
1110
import { EMPTY_STRING } from "./constants";
@@ -32,6 +31,8 @@ export interface PKCS8ShroudedKeyBagJson {
3231
encryptedData: asn1js.OctetStringJson;
3332
}
3433

34+
type PKCS8ShroudedKeyBagMakeInternalValuesParams = Omit<EncryptedDataEncryptParams, "contentToEncrypt">;
35+
3536
/**
3637
* Represents the PKCS8ShroudedKeyBag structure described in [RFC7292](https://datatracker.ietf.org/doc/html/rfc7292)
3738
*/
@@ -214,7 +215,7 @@ export class PKCS8ShroudedKeyBag extends PkiObject implements IPKCS8ShroudedKeyB
214215
//#endregion
215216
}
216217

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

228229
//#region Encrypt internal data
229-
const encryptParams: CryptoEngineEncryptParams = {
230+
const encryptParams: EncryptedDataEncryptParams = {
230231
...parameters,
231232
contentToEncrypt: this.parsedValue.toSchema().toBER(false),
232233
};

src/SafeBag.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@ const CLEAR_PROPS = [
1414
BAG_ATTRIBUTES
1515
];
1616

17-
export interface ISafeBag {
17+
export interface ISafeBag<T extends BagType = BagType> {
1818
bagId: string;
19-
bagValue: BagType;
19+
bagValue: T;
2020
bagAttributes?: Attribute[];
2121
}
2222

23-
export type SafeBagParameters = PkiObjectParameters & Partial<ISafeBag>;
23+
export type SafeBagParameters<T extends BagType = BagType> = PkiObjectParameters & Partial<ISafeBag<T>>;
2424

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

3636
public static override CLASS_NAME = "SafeBag";
3737

3838
public bagId!: string;
39-
public bagValue!: BagType;
39+
public bagValue!: T;
4040
public bagAttributes?: Attribute[];
4141

4242
/**
4343
* Initializes a new instance of the {@link SafeBag} class
4444
* @param parameters Initialization parameters
4545
*/
46-
constructor(parameters: SafeBagParameters = {}) {
46+
constructor(parameters: SafeBagParameters<T> = {}) {
4747
super();
4848

4949
this.bagId = pvutils.getParametersValue(parameters, BAG_ID, SafeBag.defaultValues(BAG_ID));
50-
this.bagValue = pvutils.getParametersValue(parameters, BAG_VALUE, SafeBag.defaultValues(BAG_VALUE));
50+
this.bagValue = pvutils.getParametersValue(parameters, BAG_VALUE, SafeBag.defaultValues(BAG_VALUE)) as unknown as T;
5151
if (BAG_ATTRIBUTES in parameters) {
5252
this.bagAttributes = pvutils.getParametersValue(parameters, BAG_ATTRIBUTES, SafeBag.defaultValues(BAG_ATTRIBUTES));
5353
}
@@ -162,7 +162,7 @@ export class SafeBag extends PkiObject implements ISafeBag {
162162
if (!bagType) {
163163
throw new Error(`Invalid BAG_ID for SafeBag: ${this.bagId}`);
164164
}
165-
this.bagValue = new bagType({ schema: asn1.result.bagValue });
165+
this.bagValue = new bagType({ schema: asn1.result.bagValue }) as unknown as T;
166166

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

0 commit comments

Comments
 (0)