1
- import { Certificate , CertNaming , type CryptoAlgorithm , ECDSA , KeyChain , KeyChainSerialized , KeyStore , RSA , RSAOAEP } from "@ndn/keychain" ;
1
+ import { Certificate , CertNaming , ECDSA , type KeyChain , KeyChainExternal , type KeyStore , RSA , RSAOAEP } from "@ndn/keychain" ;
2
2
import { Component , Data , Name , NameMap , ValidityPeriod } from "@ndn/packet" ;
3
3
import { type Decodable , Decoder , Encoder } from "@ndn/tlv" ;
4
+ import { assert } from "@ndn/util" ;
4
5
import { execa , execaSync } from "execa" ;
5
6
6
7
import { SafeBag } from "./safe-bag" ;
@@ -12,7 +13,7 @@ const ALGO_LIST = [ECDSA, RSA, RSAOAEP];
12
13
let ndnsecInstalled : boolean | undefined ;
13
14
14
15
/** Access ndn-cxx KeyChain. */
15
- export class NdnsecKeyChain extends KeyChainSerialized {
16
+ export class NdnsecKeyChain extends KeyChainExternal {
16
17
/**
17
18
* Whether current environment supports ndn-cxx KeyChain.
18
19
*
@@ -29,7 +30,7 @@ export class NdnsecKeyChain extends KeyChainSerialized {
29
30
home,
30
31
importOptions,
31
32
} : NdnsecKeyChain . Options = { } ) {
32
- super ( ) ;
33
+ super ( ALGO_LIST ) ;
33
34
if ( pibLocator && tpmLocator ) {
34
35
this . env . NDN_CLIENT_PIB = pibLocator ;
35
36
this . env . NDN_CLIENT_TPM = tpmLocator ;
@@ -39,11 +40,8 @@ export class NdnsecKeyChain extends KeyChainSerialized {
39
40
this . importOptions = importOptions ;
40
41
}
41
42
42
- public override readonly needJwk = true ;
43
43
private readonly env : NodeJS . ProcessEnv = { NDN_NAME_ALT_URI : "0" } ;
44
44
private readonly importOptions ?: SafeBag . ImportOptions ;
45
- private cached ?: KeyChain ;
46
- private readonly insertKeyLoader = new KeyStore . Loader ( true , ALGO_LIST ) ;
47
45
48
46
private async invokeNdnsec ( argv : readonly string [ ] , input ?: Uint8Array ) : Promise < {
49
47
readonly lines : string [ ] ;
@@ -64,7 +62,7 @@ export class NdnsecKeyChain extends KeyChainSerialized {
64
62
}
65
63
66
64
/** Copy keys and certificates to another keychain. */
67
- public async copyTo ( dest : KeyChain ) : Promise < KeyChain > {
65
+ public override async copyTo ( dest : KeyChain ) : Promise < KeyChain > {
68
66
const { lines } = await this . invokeNdnsec ( [ "list" , "-c" ] ) ;
69
67
const keyCerts = new NameMap < Name [ ] > ( ) ;
70
68
for ( const line of lines ) {
@@ -97,60 +95,30 @@ export class NdnsecKeyChain extends KeyChainSerialized {
97
95
return dest ;
98
96
}
99
97
100
- private async load ( ) {
101
- return ( this . cached ??= await this . copyTo ( KeyChain . createTemp ( ALGO_LIST ) ) ) ;
102
- }
103
-
104
- protected override async sListKeys ( prefix : Name ) : Promise < Name [ ] > {
105
- const keyChain = await this . load ( ) ;
106
- return keyChain . listKeys ( prefix ) ;
107
- }
108
-
109
- protected override async sGetKeyPair ( name : Name ) : Promise < KeyChain . KeyPair > {
110
- const keyChain = await this . load ( ) ;
111
- return keyChain . getKeyPair ( name ) ;
112
- }
113
-
114
- protected override async sInsertKey ( name : Name , stored : KeyStore . StoredKey ) : Promise < void > {
115
- const keyPair = await this . insertKeyLoader . loadKey ( name , stored ) ;
116
-
98
+ protected override async eInsertKey ( { publicKey, signer, pvt } : KeyStore . KeyPair ) : Promise < void > {
117
99
const selfSigned = await Certificate . issue ( {
118
- publicKey : keyPair . publicKey ,
100
+ publicKey,
119
101
validity : ValidityPeriod . MAX ,
120
- issuerPrivateKey : keyPair . signer ,
102
+ issuerPrivateKey : signer ,
121
103
issuerId : IMPORTING_ISSUER ,
122
104
} ) ;
123
- const pkcs8 = new Uint8Array ( await crypto . subtle . exportKey (
124
- "pkcs8" , ( keyPair . pvt as CryptoAlgorithm . PrivateKey ) . privateKey ) ) ;
105
+ assert ( "privateKey" in pvt ) ;
106
+ const pkcs8 = new Uint8Array ( await crypto . subtle . exportKey ( "pkcs8" , pvt . privateKey ) ) ;
125
107
126
108
const safeBag = await SafeBag . create ( selfSigned , pkcs8 , PASSPHRASE ) ;
127
109
await this . invokeNdnsec ( [ "import" , "-P" , PASSPHRASE , "-i-" ] , Encoder . encode ( safeBag ) ) ;
128
- delete this . cached ;
129
110
}
130
111
131
- protected override async sDeleteKey ( name : Name ) : Promise < void > {
112
+ protected override async eDeleteKey ( name : Name ) : Promise < void > {
132
113
await this . invokeNdnsec ( [ "delete" , "-k" , name . toString ( ) ] ) ;
133
- delete this . cached ;
134
- }
135
-
136
- protected override async sListCerts ( prefix : Name ) : Promise < Name [ ] > {
137
- const keyChain = await this . load ( ) ;
138
- return keyChain . listCerts ( prefix ) ;
139
- }
140
-
141
- protected override async sGetCert ( name : Name ) : Promise < Certificate > {
142
- const keyChain = await this . load ( ) ;
143
- return keyChain . getCert ( name ) ;
144
114
}
145
115
146
- protected override async sInsertCert ( cert : Certificate ) : Promise < void > {
116
+ protected override async eInsertCert ( cert : Certificate ) : Promise < void > {
147
117
await this . invokeNdnsec ( [ "cert-install" , "-K" , "-f-" ] , Encoder . encode ( cert . data ) ) ;
148
- delete this . cached ;
149
118
}
150
119
151
- protected override async sDeleteCert ( name : Name ) : Promise < void > {
120
+ protected override async eDeleteCert ( name : Name ) : Promise < void > {
152
121
await this . invokeNdnsec ( [ "delete" , "-c" , name . toString ( ) ] ) ;
153
- delete this . cached ;
154
122
}
155
123
}
156
124
@@ -162,7 +130,7 @@ export namespace NdnsecKeyChain {
162
130
*
163
131
* @remarks
164
132
* This must be specified together with `.tpmLocator`.
165
- * @see {@link https://docs.named-data.net/ndn-cxx/0.8.1 /manpages/ndn-client.conf.html#key-management }
133
+ * @see {@link https://docs.named-data.net/ndn-cxx/0.9.0 /manpages/ndn-client.conf.html#key-management }
166
134
*/
167
135
pibLocator ?: string ;
168
136
@@ -171,7 +139,7 @@ export namespace NdnsecKeyChain {
171
139
*
172
140
* @remarks
173
141
* This must be specified together with `.pibLocator`.
174
- * @see {@link https://docs.named-data.net/ndn-cxx/0.8.1 /manpages/ndn-client.conf.html#key-management }
142
+ * @see {@link https://docs.named-data.net/ndn-cxx/0.9.0 /manpages/ndn-client.conf.html#key-management }
175
143
*/
176
144
tpmLocator ?: string ;
177
145
0 commit comments