1- import { parseKeysXml , findKeyInXml } from 'dosipas-ts' ;
1+ import { parseKeysXml , findKeyInXml , getPublicKey as derivePublicKey } from 'dosipas-ts' ;
22import type { Level1KeyProvider , UicPublicKeyEntry } from 'dosipas-ts' ;
33
44let cachedXml : string | null = null ;
55let cachedKeys : UicPublicKeyEntry [ ] | null = null ;
66
7+ /** NIST FIPS 186-4 ECDSA P-256 test vector private key (Level 1). */
8+ const FIPS_L1_PRIV_HEX = 'c9806898a0334916c860748880a541f093b579a9b1f32934d86c363c39800357' ;
9+
10+ let cachedFipsL1PubKey : Uint8Array | null = null ;
11+
12+ function getFipsL1PublicKey ( ) : Uint8Array {
13+ if ( ! cachedFipsL1PubKey ) {
14+ const privBytes = new Uint8Array ( FIPS_L1_PRIV_HEX . match ( / .{ 1 , 2 } / g) ! . map ( ( b ) => parseInt ( b , 16 ) ) ) ;
15+ cachedFipsL1PubKey = derivePublicKey ( privBytes , 'P-256' ) ;
16+ }
17+ return cachedFipsL1PubKey ;
18+ }
19+
720export async function loadKeysXml ( ) : Promise < string > {
821 if ( cachedXml ) return cachedXml ;
922 const resp = await fetch ( './uic-publickeys.xml' ) ;
@@ -19,11 +32,14 @@ export async function getKeys(): Promise<UicPublicKeyEntry[]> {
1932 return cachedKeys ;
2033}
2134
22- export async function createKeyProvider ( ) : Promise < Level1KeyProvider > {
35+ export async function createKeyProvider ( trustFipsKey = false ) : Promise < Level1KeyProvider > {
2336 const xml = await loadKeysXml ( ) ;
2437 return {
2538 async getPublicKey ( securityProvider , keyId ) {
2639 const issuerCode = securityProvider . num ?? 0 ;
40+ if ( trustFipsKey && issuerCode === 9999 && keyId === 0 ) {
41+ return getFipsL1PublicKey ( ) ;
42+ }
2743 const key = findKeyInXml ( xml , issuerCode , keyId ) ;
2844 if ( ! key ) {
2945 throw new Error ( `Key not found: issuer=${ issuerCode } , keyId=${ keyId } ` ) ;
0 commit comments