Skip to content

Commit

Permalink
feat: added mapper function for create key in musap kms and added the…
Browse files Browse the repository at this point in the history
… option to enable certain sscd's in the constructor
  • Loading branch information
sksadjad committed Jul 26, 2024
1 parent a2bb373 commit db5c8d3
Showing 1 changed file with 23 additions and 6 deletions.
29 changes: 23 additions & 6 deletions packages/musap-rn-kms/src/agent/MusapKeyManagerSystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@ import { TextDecoder } from 'text-encoding';

export class MusapKeyManagementSystem extends KeyManagementSystem {
private musapKeyStore: MusapModuleType;
private sscdType: SscdType;

constructor(keyStore: MusapModuleType) {
constructor(keyStore: MusapModuleType, sscdType?: SscdType) {
super(keyStore as unknown as AbstractPrivateKeyStore);
this.musapKeyStore = keyStore;
this.musapKeyStore.enableSscd('TEE')
this.sscdType = sscdType ? sscdType : 'TEE';
this.musapKeyStore.enableSscd(this.sscdType)
}

async listKeys(): Promise<ManagedKeyInfo[]> {
Expand All @@ -35,9 +37,10 @@ export class MusapKeyManagementSystem extends KeyManagementSystem {
}

async createKey(args: { type: TKeyType; sscdType?: SscdType }): Promise<ManagedKeyInfo> {
const sscdType: SscdType = args.sscdType ? args.sscdType : 'TEE';
const keyAlgorithm = this.mapKeyTypeToAlgorithmType(args.type);

const keyGenReq: KeyGenReq = {
keyAlgorithm: args.type as KeyAlgorithmType,
keyAlgorithm: keyAlgorithm,
did: '',
keyUsage: 'sign',
keyAlias: uuid(),
Expand All @@ -47,11 +50,12 @@ export class MusapKeyManagementSystem extends KeyManagementSystem {
],
role: 'administrator'
};

try {
const generatedKeyUri = await this.musapKeyStore.generateKey(sscdType, keyGenReq);
const generatedKeyUri = await this.musapKeyStore.generateKey(this.sscdType, keyGenReq);
if (generatedKeyUri) {
console.log('Generated key:', generatedKeyUri);
const key = await this.musapKeyStore.getKeyByUri(generatedKeyUri)
const key = await this.musapKeyStore.getKeyByUri(generatedKeyUri);
return this.asMusapKeyInfo(key);
} else {
console.log('Failed to generate key');
Expand All @@ -63,6 +67,19 @@ export class MusapKeyManagementSystem extends KeyManagementSystem {
}
}

mapKeyTypeToAlgorithmType = (type: TKeyType): KeyAlgorithmType => {
switch (type) {
case 'Secp256k1':
return 'ECCP256K1';
case 'Secp256r1':
return 'ECCP256R1';
case 'RSA':
return 'RSA2K';
default:
throw new Error(`Key type ${type} is not supported by MUSAP`);
}
}

async deleteKey({ kid }: { kid: string }): Promise<boolean> {
try {
await this.musapKeyStore.removeKey(kid);
Expand Down

0 comments on commit db5c8d3

Please sign in to comment.