Skip to content

Commit 23dab29

Browse files
committed
revert: changes made to crypto public, private, signature public and signature private key
Refs: 8bfa322
1 parent e284ab6 commit 23dab29

File tree

5 files changed

+47
-263
lines changed

5 files changed

+47
-263
lines changed

src/CryptoPrivateKey.ts

Lines changed: 14 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,11 @@
11
import { serialize, type, validate } from "@js-soft/ts-serval";
2-
import { Provider, SecurityLevel } from "@nmshd/rs-crypto-types";
32
import { CoreBuffer, Encoding, ICoreBuffer } from "./CoreBuffer";
4-
import { CryptoError } from "./CryptoError";
5-
import { CryptoErrorCode } from "./CryptoErrorCode";
6-
import { CryptoLayerKeyPair } from "./CryptoLayerKeyPair";
7-
import { getProvider } from "./CryptoLayerProviders";
83
import { CryptoSerializable } from "./CryptoSerializable";
94
import { CryptoExchangeAlgorithm } from "./exchange/CryptoExchange";
10-
import { CryptoHashAlgorithm } from "./hash/CryptoHash";
115
import { CryptoSignatureAlgorithm } from "./signature/CryptoSignatureAlgorithm";
126

137
export interface ICryptoPrivateKey {
14-
privateKey: ICoreBuffer | CryptoLayerKeyPair;
8+
privateKey: ICoreBuffer;
159
algorithm: CryptoExchangeAlgorithm | CryptoSignatureAlgorithm;
1610
toString(): string;
1711
toPEM(): string;
@@ -36,36 +30,14 @@ export class CryptoPrivateKey extends CryptoSerializable implements ICryptoPriva
3630

3731
@validate()
3832
@serialize()
39-
public privateKey: CoreBuffer | CryptoLayerKeyPair;
33+
public privateKey: CoreBuffer;
4034

4135
public toPEM(): string {
42-
if (this.privateKey instanceof CoreBuffer) {
43-
return this.privateKey.toString(Encoding.Pem, "PRIVATE KEY");
44-
}
45-
46-
if (!this.privateKey.keyPairHandle) {
47-
throw new CryptoError(
48-
CryptoErrorCode.CalUninitializedKey,
49-
"The key pair does not hold a key pair handle. It needs to be loaded from a provider via the init method."
50-
);
51-
}
52-
let privateRawKey = this.privateKey.keyPairHandle.extractKey();
53-
return new CoreBuffer(privateRawKey).toString(Encoding.Pem, "PRIVATE KEY");
36+
return this.privateKey.toString(Encoding.Pem, "PRIVATE KEY");
5437
}
5538

5639
public override toString(): string {
57-
if (this.privateKey instanceof CoreBuffer) {
58-
return this.privateKey.toString(Encoding.Base64_UrlSafe_NoPadding);
59-
}
60-
61-
if (!this.privateKey.keyPairHandle) {
62-
throw new CryptoError(
63-
CryptoErrorCode.CalUninitializedKey,
64-
"The key pair does not hold a key pair handle. It needs to be loaded from a provider via the init method."
65-
);
66-
}
67-
let privateRawKey = this.privateKey.keyPairHandle.extractKey();
68-
return new CoreBuffer(privateRawKey).toString(Encoding.Base64_UrlSafe_NoPadding);
40+
return this.privateKey.toString(Encoding.Base64_UrlSafe_NoPadding);
6941
}
7042

7143
protected static stripPEM(pem: string): string {
@@ -80,62 +52,34 @@ export class CryptoPrivateKey extends CryptoSerializable implements ICryptoPriva
8052
public static fromString(
8153
value: string,
8254
algorithm: CryptoExchangeAlgorithm | CryptoSignatureAlgorithm,
83-
encoding: Encoding = Encoding.Base64_UrlSafe_NoPadding,
84-
provider: string | SecurityLevel | undefined = "Software",
85-
hashAlgorithm?: CryptoHashAlgorithm
55+
encoding: Encoding = Encoding.Base64_UrlSafe_NoPadding
8656
): CryptoPrivateKey {
8757
const buffer: CoreBuffer = CoreBuffer.fromString(value, encoding);
88-
let providerInitalized = getProvider(provider);
89-
if (!providerInitalized) {
90-
return this.fromAny({ algorithm, privateKey: buffer });
91-
}
92-
93-
// provider load from global with config
94-
95-
return this.fromAny({
96-
algorithm,
97-
privateKey: CryptoLayerKeyPair.fromPrivateBufferWithAlgorithm(
98-
providerInitalized,
99-
buffer,
100-
algorithm,
101-
hashAlgorithm
102-
)
103-
});
58+
return this.fromAny({ algorithm, privateKey: buffer });
10459
}
10560

10661
public static fromObject(
10762
value: any,
108-
algorithm: CryptoExchangeAlgorithm | CryptoSignatureAlgorithm,
109-
provider?: Provider
63+
algorithm: CryptoExchangeAlgorithm | CryptoSignatureAlgorithm
11064
): CryptoPrivateKey {
111-
const buffer: CoreBuffer = CoreBuffer.fromObject(value);
112-
if (!provider) {
113-
return this.fromAny({ algorithm, privateKey: buffer });
114-
}
115-
return this.fromAny({
116-
algorithm,
117-
privateKey: CryptoLayerKeyPair.fromPrivateBufferWithAlgorithm(provider, buffer, algorithm)
118-
});
65+
const buffer: ICoreBuffer = CoreBuffer.fromObject(value);
66+
67+
return this.fromAny({ algorithm, privateKey: buffer });
11968
}
12069

12170
public static fromPEM(
12271
pem: string,
123-
algorithm: CryptoExchangeAlgorithm | CryptoSignatureAlgorithm,
124-
provider?: Provider
72+
algorithm: CryptoExchangeAlgorithm | CryptoSignatureAlgorithm
12573
): CryptoPrivateKey {
12674
const value = this.stripPEM(pem);
127-
return this.fromString(value, algorithm, Encoding.Base64, provider);
75+
return this.fromString(value, algorithm, Encoding.Base64);
12876
}
12977

13078
public static from(value: any): CryptoPrivateKey {
13179
return this.fromAny(value);
13280
}
13381

134-
public static fromBase64(value: string, provider?: Provider): CryptoPrivateKey {
135-
let privateKey = this.deserialize(CoreBuffer.base64_utf8(value));
136-
if (provider && privateKey.privateKey instanceof CryptoLayerKeyPair) {
137-
privateKey.privateKey.init(provider);
138-
}
139-
return privateKey;
82+
public static fromBase64(value: string): CryptoPrivateKey {
83+
return this.deserialize(CoreBuffer.base64_utf8(value));
14084
}
14185
}

src/CryptoPublicKey.ts

Lines changed: 14 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
11
import { serialize, type, validate } from "@js-soft/ts-serval";
2-
import { Provider } from "@nmshd/rs-crypto-types";
32
import { CoreBuffer, Encoding, ICoreBuffer } from "./CoreBuffer";
4-
import { CryptoError } from "./CryptoError";
5-
import { CryptoErrorCode } from "./CryptoErrorCode";
6-
import { CryptoLayerKeyPair } from "./CryptoLayerKeyPair";
73
import { CryptoSerializable } from "./CryptoSerializable";
84
import { CryptoExchangeAlgorithm } from "./exchange/CryptoExchange";
95
import { CryptoSignatureAlgorithm } from "./signature/CryptoSignatureAlgorithm";
106

117
export interface ICryptoPublicKey {
12-
publicKey: ICoreBuffer | CryptoLayerKeyPair;
8+
publicKey: ICoreBuffer;
139
algorithm: CryptoExchangeAlgorithm | CryptoSignatureAlgorithm;
1410
toString(): string;
1511
toPEM(): string;
@@ -35,36 +31,14 @@ export class CryptoPublicKey extends CryptoSerializable implements ICryptoPublic
3531

3632
@validate()
3733
@serialize()
38-
public publicKey: CoreBuffer | CryptoLayerKeyPair;
34+
public publicKey: CoreBuffer;
3935

4036
public override toString(): string {
41-
if (this.publicKey instanceof CoreBuffer) {
42-
return this.publicKey.toString(Encoding.Base64_UrlSafe_NoPadding);
43-
}
44-
45-
if (!this.publicKey.keyPairHandle) {
46-
throw new CryptoError(
47-
CryptoErrorCode.CalUninitializedKey,
48-
"The key pair does not hold a key pair handle. It needs to be loaded from a provider via the init method."
49-
);
50-
}
51-
let privateRawKey = this.publicKey.keyPairHandle.extractKey();
52-
return new CoreBuffer(privateRawKey).toString(Encoding.Base64_UrlSafe_NoPadding);
37+
return this.publicKey.toString(Encoding.Base64_UrlSafe_NoPadding);
5338
}
5439

5540
public toPEM(): string {
56-
if (this.publicKey instanceof CoreBuffer) {
57-
return this.publicKey.toString(Encoding.Pem, "PUBLIC KEY");
58-
}
59-
60-
if (!this.publicKey.keyPairHandle) {
61-
throw new CryptoError(
62-
CryptoErrorCode.CalUninitializedKey,
63-
"The key pair does not hold a key pair handle. It needs to be loaded from a provider via the init method."
64-
);
65-
}
66-
let privateRawKey = this.publicKey.keyPairHandle.extractKey();
67-
return new CoreBuffer(privateRawKey).toString(Encoding.Pem, "PUBLIC KEY");
41+
return this.publicKey.toString(Encoding.Pem, "PUBLIC KEY");
6842
}
6943

7044
protected static stripPEM(pem: string): string {
@@ -76,55 +50,34 @@ export class CryptoPublicKey extends CryptoSerializable implements ICryptoPublic
7650
return pem;
7751
}
7852

79-
public static fromPEM(
80-
pem: string,
81-
algorithm: CryptoExchangeAlgorithm | CryptoSignatureAlgorithm,
82-
provider?: Provider
83-
): CryptoPublicKey {
53+
public static fromPEM(pem: string, algorithm: CryptoExchangeAlgorithm | CryptoSignatureAlgorithm): CryptoPublicKey {
8454
const value = this.stripPEM(pem);
85-
return this.fromString(value, algorithm, Encoding.Base64, provider);
55+
return this.fromString(value, algorithm, Encoding.Base64);
8656
}
8757

8858
public static fromString(
8959
value: string,
9060
algorithm: CryptoExchangeAlgorithm | CryptoSignatureAlgorithm,
91-
encoding: Encoding = Encoding.Base64_UrlSafe_NoPadding,
92-
provider?: Provider
61+
encoding: Encoding = Encoding.Base64_UrlSafe_NoPadding
9362
): CryptoPublicKey {
94-
const buffer = CoreBuffer.fromString(value, encoding);
95-
if (!provider) {
96-
return this.fromAny({ algorithm, publicKey: buffer });
97-
}
98-
return this.fromAny({
99-
algorithm,
100-
privateKey: CryptoLayerKeyPair.fromPublicBufferWithAlgorithm(provider, buffer, algorithm)
101-
});
63+
const buffer: ICoreBuffer = CoreBuffer.fromString(value, encoding);
64+
65+
return this.fromAny({ algorithm, publicKey: buffer });
10266
}
10367

10468
public static fromObject(
10569
value: any,
106-
algorithm: CryptoExchangeAlgorithm | CryptoSignatureAlgorithm,
107-
provider?: Provider
70+
algorithm: CryptoExchangeAlgorithm | CryptoSignatureAlgorithm
10871
): CryptoPublicKey {
10972
const buffer = CoreBuffer.fromObject(value);
110-
if (!provider) {
111-
return this.fromAny({ algorithm, publicKey: buffer });
112-
}
113-
return this.fromAny({
114-
algorithm,
115-
privateKey: CryptoLayerKeyPair.fromPublicBufferWithAlgorithm(provider, buffer, algorithm)
116-
});
73+
return this.fromAny({ algorithm, publicKey: buffer });
11774
}
11875

11976
public static from(value: any): CryptoPublicKey {
12077
return this.fromAny(value);
12178
}
12279

123-
public static fromBase64(value: string, provider?: Provider): CryptoPublicKey {
124-
let publicKey = this.deserialize(CoreBuffer.base64_utf8(value));
125-
if (provider && publicKey.publicKey instanceof CryptoLayerKeyPair) {
126-
publicKey.publicKey.init(provider);
127-
}
128-
return publicKey;
80+
public static fromBase64(value: string): CryptoPublicKey {
81+
return this.deserialize(CoreBuffer.base64_utf8(value));
12982
}
13083
}

src/signature/CryptoSignaturePrivateKey.ts

Lines changed: 10 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
11
import { ISerializable, ISerialized, serialize, type, validate } from "@js-soft/ts-serval";
2-
import { Provider } from "crypto-layer-ts-types";
3-
import { CryptoError } from "src/CryptoError";
4-
import { CryptoErrorCode } from "src/CryptoErrorCode";
5-
import { CryptoLayerKeyPair } from "src/CryptoLayerKeyPair";
62
import { CoreBuffer, IClearable, ICoreBuffer } from "../CoreBuffer";
73
import { CryptoPrivateKey } from "../CryptoPrivateKey";
84
import { CryptoSignatureAlgorithm } from "./CryptoSignatureAlgorithm";
@@ -11,15 +7,13 @@ import { CryptoSignatures } from "./CryptoSignatures";
117
import { CryptoSignatureValidation } from "./CryptoSignatureValidation";
128

139
export interface ICryptoSignaturePrivateKeySerialized extends ISerialized {
14-
alg: number; // algorithm
15-
prv?: string; // privateKey
10+
alg: number;
11+
prv: string;
1612
id?: string;
17-
kid?: string; // keyId
18-
pnm?: string; // providerName
1913
}
2014
export interface ICryptoSignaturePrivateKey extends ISerializable {
2115
algorithm: CryptoSignatureAlgorithm;
22-
privateKey: ICoreBuffer | CryptoLayerKeyPair;
16+
privateKey: ICoreBuffer;
2317
id?: string;
2418
}
2519

@@ -32,65 +26,24 @@ export class CryptoSignaturePrivateKey extends CryptoPrivateKey implements ICryp
3226
public id?: string;
3327

3428
public override toJSON(verbose = true): ICryptoSignaturePrivateKeySerialized {
35-
if (this.privateKey instanceof CoreBuffer) {
36-
return {
37-
prv: this.privateKey.toBase64URL(),
38-
alg: this.algorithm,
39-
id: this.id,
40-
"@type": verbose ? "CryptoSignaturePrivateKey" : undefined
41-
};
42-
}
43-
44-
if (!this.privateKey.keyPairHandle || !this.privateKey.provider) {
45-
throw new CryptoError(
46-
CryptoErrorCode.CalUninitializedKey,
47-
"The key pair does not hold a key pair handle. It needs to be loaded from a provider via the init method."
48-
);
49-
}
5029
return {
30+
prv: this.privateKey.toBase64URL(),
5131
alg: this.algorithm,
52-
kid: this.privateKey.keyPairHandle.id(),
53-
pnm: this.privateKey.provider.providerName()
32+
id: this.id,
33+
"@type": verbose ? "CryptoSignaturePrivateKey" : undefined
5434
};
5535
}
5636

5737
public clear(): void {
58-
if (this.privateKey instanceof CoreBuffer) {
59-
this.privateKey.clear();
60-
}
38+
this.privateKey.clear();
6139
}
6240

6341
public override toBase64(verbose = true): string {
6442
return CoreBuffer.utf8_base64(this.serialize(verbose));
6543
}
6644

67-
/**
68-
* Returns the public key of this private key.
69-
*
70-
* @param provider If defined, will create the public key with this provider instead of the provider used by the private key.
71-
* @returns CryptoSignaturePublicKey
72-
*
73-
* @throws `CryptoErrorCode.CalUninitializedKey` - If `privateKey` is an uninitialized crypto layer key pair handle.
74-
*/
75-
public async toPublicKey(provider?: Provider): Promise<CryptoSignaturePublicKey> {
76-
if (this.privateKey instanceof CoreBuffer) {
77-
return await CryptoSignatures.privateKeyToPublicKey(this);
78-
}
79-
80-
provider = provider ?? this.privateKey.provider;
81-
if (!this.privateKey.keyPairHandle || !provider) {
82-
throw new CryptoError(
83-
CryptoErrorCode.CalUninitializedKey,
84-
"The key pair does not hold a key pair handle. It needs to be loaded from a provider via the init method."
85-
);
86-
}
87-
88-
let rawPublicKey = new CoreBuffer(this.privateKey.keyPairHandle.getPublicKey());
89-
let spec = this.privateKey.keyPairHandle.spec();
90-
let newKeyPair = provider.importPublicKey(spec, rawPublicKey.buffer);
91-
rawPublicKey.clear();
92-
let cryptoLayerKeyPair = new CryptoLayerKeyPair(provider, newKeyPair);
93-
return CryptoSignaturePublicKey.fromAny({ algorithm: this.algorithm, publicKey: rawPublicKey });
45+
public async toPublicKey(): Promise<CryptoSignaturePublicKey> {
46+
return await CryptoSignatures.privateKeyToPublicKey(this);
9447
}
9548

9649
public static override from(
@@ -106,20 +59,10 @@ export class CryptoSignaturePrivateKey extends CryptoPrivateKey implements ICryp
10659
privateKey: value.prv,
10760
id: value.id
10861
};
109-
} else if (value.kid && value.pnm) {
110-
let keyPairHandle = CryptoLayerKeyPair.fromAny({ id: value.kid, providerName: value.pnm });
111-
value = {
112-
algorithm: value.alg,
113-
privateKey: keyPairHandle,
114-
id: value.id
115-
};
11662
}
11763

11864
CryptoSignatureValidation.checkSignatureAlgorithm(value.algorithm);
119-
120-
if (value.privateKey instanceof CoreBuffer) {
121-
CryptoSignatureValidation.checkSignaturePrivateKey(value.privateKey, "privateKey");
122-
}
65+
CryptoSignatureValidation.checkSignaturePrivateKey(value.privateKey, "privateKey");
12366

12467
return value;
12568
}

0 commit comments

Comments
 (0)