|
1 | 1 | import { serialize, type, validate } from "@js-soft/ts-serval"; |
2 | | -import { CoreBuffer, Encoding, ICoreBuffer } from "./CoreBuffer"; |
| 2 | +import { KeyPairHandle, KeyPairSpec, Provider } from "@nmshd/rs-crypto-types"; |
| 3 | +import { CryptoError } from "./CryptoError"; |
| 4 | +import { CryptoErrorCode } from "./CryptoErrorCode"; |
| 5 | +import { getProvider } from "./CryptoLayerProviders"; |
3 | 6 | import { CryptoSerializable } from "./CryptoSerializable"; |
4 | | -import { CryptoExchangeAlgorithm } from "./exchange/CryptoExchange"; |
5 | | -import { CryptoSignatureAlgorithm } from "./signature/CryptoSignatureAlgorithm"; |
6 | 7 |
|
7 | 8 | export interface ICryptoPrivateKeyHandle { |
8 | | - privateKey: ICoreBuffer; |
9 | | - algorithm: CryptoExchangeAlgorithm | CryptoSignatureAlgorithm; |
10 | | - toString(): string; |
11 | | - toPEM(): string; |
| 9 | + keyPairHandle: KeyPairHandle; |
| 10 | + spec: KeyPairSpec; |
12 | 11 | } |
13 | 12 |
|
14 | 13 | export interface ICryptoPrivateKeyHandleStatic { |
15 | 14 | new (): ICryptoPrivateKeyHandle; |
16 | | - fromPEM( |
17 | | - pem: string, |
18 | | - algorithm: CryptoExchangeAlgorithm | CryptoSignatureAlgorithm |
19 | | - ): Promise<ICryptoPrivateKeyHandle>; |
20 | | - fromString( |
21 | | - value: string, |
22 | | - algorithm: CryptoExchangeAlgorithm | CryptoSignatureAlgorithm, |
23 | | - encoding: Encoding |
24 | | - ): Promise<ICryptoPrivateKeyHandle>; |
25 | | - fromNativeKey( |
26 | | - key: any, |
27 | | - algorithm: CryptoExchangeAlgorithm | CryptoSignatureAlgorithm |
28 | | - ): Promise<ICryptoPrivateKeyHandle>; |
| 15 | + fromNativeKey(key: any, spec: KeyPairSpec): Promise<ICryptoPrivateKeyHandle>; |
29 | 16 | } |
30 | 17 |
|
31 | 18 | @type("CryptoPrivateKeyHandle") |
32 | 19 | export class CryptoPrivateKeyHandle extends CryptoSerializable implements ICryptoPrivateKeyHandle { |
33 | 20 | @validate() |
34 | 21 | @serialize() |
35 | | - public algorithm: CryptoExchangeAlgorithm | CryptoSignatureAlgorithm; |
| 22 | + public spec: KeyPairSpec; |
36 | 23 |
|
37 | 24 | @validate() |
38 | 25 | @serialize() |
39 | | - public privateKey: CoreBuffer; |
| 26 | + public id: string; |
40 | 27 |
|
41 | | - public toPEM(): string { |
42 | | - return this.privateKey.toString(Encoding.Pem, "PRIVATE KEY"); |
43 | | - } |
44 | | - |
45 | | - public override toString(): string { |
46 | | - return this.privateKey.toString(Encoding.Base64_UrlSafe_NoPadding); |
47 | | - } |
48 | | - |
49 | | - protected static stripPEM(pem: string): string { |
50 | | - pem = pem.replace(/-----BEGIN [\w ]* KEY-----/, ""); |
51 | | - pem = pem.replace(/-----END [\w ]* KEY-----/, ""); |
52 | | - pem = pem.replace(/----- BEGIN [\w ]* KEY -----/, ""); |
53 | | - pem = pem.replace(/----- END [\w ]* KEY -----/, ""); |
54 | | - pem = pem.replace(/(?:\r\n|\r|\n)/g, ""); |
55 | | - return pem; |
56 | | - } |
57 | | - |
58 | | - public static fromString( |
59 | | - value: string, |
60 | | - algorithm: CryptoExchangeAlgorithm | CryptoSignatureAlgorithm, |
61 | | - encoding: Encoding = Encoding.Base64_UrlSafe_NoPadding |
62 | | - ): CryptoPrivateKeyHandle { |
63 | | - const buffer: CoreBuffer = CoreBuffer.fromString(value, encoding); |
64 | | - return this.fromAny({ algorithm, privateKey: buffer }); |
65 | | - } |
66 | | - |
67 | | - public static fromObject( |
68 | | - value: any, |
69 | | - algorithm: CryptoExchangeAlgorithm | CryptoSignatureAlgorithm |
70 | | - ): CryptoPrivateKeyHandle { |
71 | | - const buffer: ICoreBuffer = CoreBuffer.fromObject(value); |
| 28 | + @validate() |
| 29 | + @serialize() |
| 30 | + public providerName: string; |
72 | 31 |
|
73 | | - return this.fromAny({ algorithm, privateKey: buffer }); |
74 | | - } |
| 32 | + public provider: Provider; |
75 | 33 |
|
76 | | - public static fromPEM( |
77 | | - pem: string, |
78 | | - algorithm: CryptoExchangeAlgorithm | CryptoSignatureAlgorithm |
79 | | - ): CryptoPrivateKeyHandle { |
80 | | - const value = this.stripPEM(pem); |
81 | | - return this.fromString(value, algorithm, Encoding.Base64); |
82 | | - } |
| 34 | + public keyPairHandle: KeyPairHandle; |
83 | 35 |
|
84 | 36 | public static from(value: any): CryptoPrivateKeyHandle { |
85 | 37 | return this.fromAny(value); |
86 | 38 | } |
87 | 39 |
|
88 | | - public static fromBase64(value: string): CryptoPrivateKeyHandle { |
89 | | - return this.deserialize(CoreBuffer.base64_utf8(value)); |
| 40 | + public static override async postFrom(value: CryptoPrivateKeyHandle): Promise<CryptoPrivateKeyHandle> { |
| 41 | + const provider = getProvider(value.providerName); |
| 42 | + if (!provider) { |
| 43 | + throw new CryptoError( |
| 44 | + CryptoErrorCode.CalFailedLoadingProvider, |
| 45 | + `Failed loading provider ${value.providerName}` |
| 46 | + ); |
| 47 | + } |
| 48 | + const keyHandle = await provider.loadKeyPair(value.id); |
| 49 | + |
| 50 | + value.keyPairHandle = keyHandle; |
| 51 | + value.provider = provider; |
| 52 | + return value; |
90 | 53 | } |
91 | 54 | } |
0 commit comments