11import { concatBytes } from "@noble/curves/utils.js" ;
2- import { AbstractECDSARawSignature , C40Encoder , DateEncoder , DerTLV , parseTLVs } from "../utils.js" ;
2+ import { C40Encoder , DateEncoder , DerTLV , parseTLVs } from "../utils.js" ;
3+ import { AbstractECDSARawSignature , AbstractSeal } from "../generic.js"
34
45/**
56 * Seal header
@@ -94,11 +95,11 @@ export class VDSHeader {
9495
9596 const magicByte = data [ offset ] ;
9697 offset += 1 ;
97- if ( magicByte != VDSHeader . TAG ) throw new Error ( "Magic Constant mismatch" ) ;
98+ if ( magicByte != VDSHeader . TAG ) throw new Error ( "Magic constant mismatch" ) ;
9899
99100 const rawVersion = data [ offset ] ;
100101 offset += 1 ;
101- if ( ! ( rawVersion == 2 || rawVersion == 3 ) ) throw new Error ( "Unsupported raw version" ) ;
102+ if ( ! ( rawVersion == 2 || rawVersion == 3 ) ) throw new Error ( "Unsupported VDS version" ) ;
102103
103104 const issuingCountry = C40Encoder . decode ( data . subarray ( offset , offset + 2 ) ) ;
104105 offset += 2 ;
@@ -109,8 +110,16 @@ export class VDSHeader {
109110 offset += 4 ;
110111 signerIdentifier = signerIdentifierAndCertRefLength . substring ( 0 , 4 ) ;
111112
112- const certRefLength = parseInt ( signerIdentifierAndCertRefLength . substring ( 4 ) , 16 ) ;
113- const bytesToDecode = ( Math . floor ( ( certRefLength - 1 ) / 3 ) * 2 ) + 2 ;
113+ //const certRefLength = parseInt(signerIdentifierAndCertRefLength.substring(4), 16);
114+ //const bytesToDecode = (Math.floor((certRefLength - 1) / 3) * 2) + 2;
115+ let bytesToDecode ;
116+ if ( signerIdentifier == "DEZV" ) {
117+ const certRefLength = parseInt ( signerIdentifierAndCertRefLength . substring ( 4 ) ) ;
118+ bytesToDecode = Math . floor ( ( 2 * certRefLength + 2 ) / 3 ) ;
119+ } else {
120+ const certRefLength = parseInt ( signerIdentifierAndCertRefLength . substring ( 4 ) , 16 ) ;
121+ bytesToDecode = ( Math . floor ( ( certRefLength - 1 ) / 3 ) * 2 ) + 2 ;
122+ }
114123
115124 certificateReference = C40Encoder . decode ( data . subarray ( offset , offset + bytesToDecode ) ) ;
116125 offset += bytesToDecode ;
@@ -152,7 +161,7 @@ export class VDSSignature extends AbstractECDSARawSignature {
152161 *
153162 * Described by ICAO p.13 section 2
154163 */
155- export class Seal {
164+ export class Seal implements AbstractSeal {
156165 /**
157166 * Visible digital seal (VDS)
158167 * @param header VDS header
@@ -165,24 +174,22 @@ export class Seal {
165174 public signature : VDSSignature | null = null
166175 ) { }
167176
168- /** Signed bytes */
169177 get signedBytes ( ) : Uint8Array {
170178 return concatBytes ( this . header . encoded , ...this . messageList . map ( i => i . encoded ) ) ;
171179 }
172- /** Signature bytes */
173180 get signatureBytes ( ) : Uint8Array | null {
174181 return this . signature ? this . signature . toDER ( ) : null ;
175182 }
176183
177- /** Encoded visible digital seal */
184+ /** Encoded VDS */
178185 get encoded ( ) : Uint8Array {
179186 let encoded = this . signedBytes ;
180187 if ( this . signature ) encoded = concatBytes ( encoded , this . signature . encoded ) ;
181188
182189 return encoded ;
183190 }
184191
185- /** Decode visible digital seal from bytes */
192+ /** Decode VDS from bytes */
186193 static decode ( data : Uint8Array ) : Seal {
187194 const header = VDSHeader . decode ( data ) ;
188195 const messageList : DerTLV [ ] = [ ] ;
0 commit comments