import * as keysPBM from './keys.js' ; import 'node-forge/lib/asn1.js' ; import 'node-forge/lib/pbe.js' ; import forge from 'node-forge/lib/forge.js' ; import errcode from 'err-code' ; import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string' ; import { keyStretcher } from './key-stretcher.js' ; import generateEphemeralKeyPair from './ephemeral-keys.js' ; import { importer } from './importer.js' ; import * as RSA from './rsa-class.js' ; import * as Ed25519 from './ed25519-class.js' ; import * as Secp256k1 from './secp256k1-class.js' ; export { keyStretcher } ; export { generateEphemeralKeyPair } ; export { keysPBM } ; export const supportedKeys = { rsa : RSA , ed25519 : Ed25519 , secp256k1 : Secp256k1 } ; function unsupportedKey ( type ) { const supported = Object . keys ( supportedKeys ) . join ( ' / ' ) ; return errcode ( new Error ( `invalid or unsupported key type ${ type }. Must be ${ supported }` ) , 'ERR_UNSUPPORTED_KEY_TYPE' ) ; } function typeToKey ( type ) { type = type . toLowerCase ( ) ; if ( type === 'rsa' || type === 'ed25519' || type === 'secp256k1' ) { return supportedKeys [ type ] ; } throw unsupportedKey ( type ) ; } export async function generateKeyPair ( type , bits ? ) { return await typeToKey ( type ) . generateKeyPair ( bits ?? 2048 ) ; } export async function generateKeyPairFromSeed ( type , seed , bits ? ) { if ( type . toLowerCase ( ) !== 'ed25519' ) { throw errcode ( new Error ( 'Seed key derivation is unimplemented for RSA or secp256k1' ) , 'ERR_UNSUPPORTED_KEY_DERIVATION_TYPE' ) ; } return await Ed25519 . generateKeyPairFromSeed ( seed ) ; } export function unmarshalPublicKey ( buf ) { const decoded = keysPBM . PublicKey . decode ( buf ) ; const data = decoded . Data ; switch ( decoded . Type ) { case keysPBM . KeyType . RSA : return supportedKeys . rsa . unmarshalRsaPublicKey ( data ) ; case keysPBM . KeyType . Ed25519 : return supportedKeys . ed25519 . unmarshalEd25519PublicKey ( data ) ; case keysPBM . KeyType . Secp256k1 : return supportedKeys . secp256k1 . unmarshalSecp256k1PublicKey ( data ) ; default : throw unsupportedKey ( decoded . Type ) ; } } export function marshalPublicKey ( key , type ? ) { type = ( type ?? 'rsa' ) . toLowerCase ( ) ; typeToKey ( type ) ; return key . bytes ; } export async function unmarshalPrivateKey ( buf ) { const decoded = keysPBM . PrivateKey . decode ( buf ) ; const data = decoded . Data ; switch ( decoded . Type ) { case keysPBM . KeyType . RSA : return await supportedKeys . rsa . unmarshalRsaPrivateKey ( data ) ; case keysPBM . KeyType . Ed25519 : return supportedKeys . ed25519 . unmarshalEd25519PrivateKey ( data ) ; case keysPBM . KeyType . Secp256k1 : return supportedKeys . secp256k1 . unmarshalSecp256k1PrivateKey ( data ) ; default : throw unsupportedKey ( decoded . Type ) ; } } export function marshalPrivateKey ( key , type ? ) { type = ( type ?? 'rsa' ) . toLowerCase ( ) ; typeToKey ( type ) ; return key . bytes ; } param { string } encryptedKey param { string } password export async function importKey ( encryptedKey , password ) { try { const key = await importer ( encryptedKey , password ) ; return await unmarshalPrivateKey ( key ) ; } catch ( _ ) { } const key = forge . pki . decryptRsaPrivateKey ( encryptedKey , password ) ; if ( key === null ) { throw errcode ( new Error ( 'Cannot read the key, most likely the password is wrong or not a RSA key' ) , 'ERR_CANNOT_DECRYPT_PEM' ) ; } let der = forge . asn1 . toDer ( forge . pki . privateKeyToAsn1 ( key ) ) ; der = uint8ArrayFromString ( der . getBytes ( ) , 'ascii' ) ; return await supportedKeys . rsa . unmarshalRsaPrivateKey ( der ) ; }