@@ -205,89 +205,90 @@ export class SignedDataVerifier {
205205 const request = new KJUR . asn1 . ocsp . OCSPRequest ( { reqList : [ { issuerCert : issuer . toString ( ) , subjectCert : cert . toString ( ) , alg : "sha256" } ] } )
206206 const headers = new Headers ( )
207207 headers . append ( 'Content-Type' , 'application/ocsp-request' )
208- return fetch ( matchResult [ 1 ] , {
208+
209+ const response = await fetch ( matchResult [ 1 ] , {
209210 headers : headers ,
210211 method : 'POST' ,
211212 body : Buffer . from ( request . getEncodedHex ( ) , 'hex' )
212- } ) . then ( r => r . buffer ( ) )
213- . then ( resp => {
214- const parsedResponse = new ( KJUR . asn1 . ocsp as any ) . OCSPParser ( ) . getOCSPResponse ( resp . toString ( 'hex' ) )
215- // The issuer could also be the signer
216- const jsrassignX509Issuer = new X509 ( )
217- jsrassignX509Issuer . readCertHex ( issuer . raw . toString ( 'hex' ) )
218- const allCerts : X509 [ ] = [ jsrassignX509Issuer ]
219- for ( const certHex of parsedResponse . certs ) {
220- const cert = new X509 ( )
221- cert . readCertHex ( certHex )
222- allCerts . push ( cert )
223- }
224- let signingCert : X509Certificate | null = null
225- if ( parsedResponse . respid . key ) {
226- for ( const cert of allCerts ) {
227- const shasum = createHash ( 'sha1' )
228- shasum . update ( Buffer . from ( cert . getSPKIValue ( ) , 'hex' ) )
229- const spkiHash = shasum . digest ( 'hex' )
230- if ( spkiHash === parsedResponse . respid . key ) {
231- signingCert = new X509Certificate ( Buffer . from ( cert . hex , 'hex' ) )
232- }
233- }
234- } else if ( parsedResponse . respid . name ) {
235- for ( const cert of allCerts ) {
236- if ( cert . getSubject ( ) . str === parsedResponse . respid . name . str ) {
237- signingCert = new X509Certificate ( Buffer . from ( cert . hex , 'hex' ) )
238- }
239- }
240- }
241- if ( signingCert == null ) {
242- throw new VerificationException ( VerificationStatus . FAILURE )
243- }
244- // Verify Signing Cert is issued by issuer
245- if ( signingCert . publicKey === issuer . publicKey && signingCert . subject === issuer . subject ) {
246- // This is directly signed by the issuer
247- } else if ( signingCert . verify ( issuer . publicKey ) ) {
248- // This is issued by the issuer, let's check the dates and purpose
249- const signingCertAsign = new X509 ( )
250- signingCertAsign . readCertPEM ( signingCert . toString ( ) )
251- if ( ! signingCertAsign . getExtExtKeyUsage ( ) . array . includes ( "ocspSigning" ) ) {
252- throw new VerificationException ( VerificationStatus . INVALID_CERTIFICATE )
253- }
254- this . checkDates ( signingCert , new Date ( ) )
255- } else {
256- throw new VerificationException ( VerificationStatus . INVALID_CERTIFICATE )
213+ } )
214+
215+ const responseBuffer = await response . buffer ( )
216+ const parsedResponse = new ( KJUR . asn1 . ocsp as any ) . OCSPParser ( ) . getOCSPResponse ( responseBuffer . toString ( 'hex' ) )
217+ // The issuer could also be the signer
218+ const jsrassignX509Issuer = new X509 ( )
219+ jsrassignX509Issuer . readCertHex ( issuer . raw . toString ( 'hex' ) )
220+ const allCerts : X509 [ ] = [ jsrassignX509Issuer ]
221+ for ( const certHex of parsedResponse . certs ) {
222+ const cert = new X509 ( )
223+ cert . readCertHex ( certHex )
224+ allCerts . push ( cert )
225+ }
226+ let signingCert : X509Certificate | null = null
227+ if ( parsedResponse . respid . key ) {
228+ for ( const cert of allCerts ) {
229+ const shasum = createHash ( 'sha1' )
230+ shasum . update ( Buffer . from ( cert . getSPKIValue ( ) , 'hex' ) )
231+ const spkiHash = shasum . digest ( 'hex' )
232+ if ( spkiHash === parsedResponse . respid . key ) {
233+ signingCert = new X509Certificate ( Buffer . from ( cert . hex , 'hex' ) )
257234 }
258-
259- // Extract raw responseData
260- const responseData = ASN1HEX . getTLVbyList ( resp . toString ( 'hex' ) , 0 , [ 1 , 0 , 1 , 0 , 0 ] ) as string
261- // Verify Payload signed by cert
262- const shortAlg = parsedResponse . alg . substring ( 0 , 6 ) . toUpperCase ( )
263- if ( shortAlg !== "SHA256" && shortAlg !== "SHA384" && shortAlg !== "SHA512" ) {
264- throw new VerificationException ( VerificationStatus . FAILURE )
235+ }
236+ } else if ( parsedResponse . respid . name ) {
237+ for ( const cert of allCerts ) {
238+ if ( cert . getSubject ( ) . str === parsedResponse . respid . name . str ) {
239+ signingCert = new X509Certificate ( Buffer . from ( cert . hex , 'hex' ) )
265240 }
241+ }
242+ }
243+ if ( signingCert == null ) {
244+ throw new VerificationException ( VerificationStatus . FAILURE )
245+ }
246+ // Verify Signing Cert is issued by issuer
247+ if ( signingCert . publicKey === issuer . publicKey && signingCert . subject === issuer . subject ) {
248+ // This is directly signed by the issuer
249+ } else if ( signingCert . verify ( issuer . publicKey ) ) {
250+ // This is issued by the issuer, let's check the dates and purpose
251+ const signingCertAsign = new X509 ( )
252+ signingCertAsign . readCertPEM ( signingCert . toString ( ) )
253+ if ( ! signingCertAsign . getExtExtKeyUsage ( ) . array . includes ( "ocspSigning" ) ) {
254+ throw new VerificationException ( VerificationStatus . INVALID_CERTIFICATE )
255+ }
256+ this . checkDates ( signingCert , new Date ( ) )
257+ } else {
258+ throw new VerificationException ( VerificationStatus . INVALID_CERTIFICATE )
259+ }
260+
261+ // Extract raw responseData
262+ const responseData = ASN1HEX . getTLVbyList ( responseBuffer . toString ( 'hex' ) , 0 , [ 1 , 0 , 1 , 0 , 0 ] ) as string
263+ // Verify Payload signed by cert
264+ const shortAlg = parsedResponse . alg . substring ( 0 , 6 ) . toUpperCase ( )
265+ if ( shortAlg !== "SHA256" && shortAlg !== "SHA384" && shortAlg !== "SHA512" ) {
266+ throw new VerificationException ( VerificationStatus . FAILURE )
267+ }
266268
267- if ( ! verify ( shortAlg , Buffer . from ( responseData , 'hex' ) , signingCert . publicKey , Buffer . from ( parsedResponse . sighex , 'hex' ) ) ) {
268- throw new VerificationException ( VerificationStatus . FAILURE )
269- }
270-
271- for ( const singleResponse of parsedResponse . array ) {
272- // Confirm entry is for this cert
273- const certIdBuilder = new KJUR . asn1 . ocsp . CertID ( ) as any
274- const currentCertCertId = certIdBuilder . getParamByCerts ( issuer . toString ( ) , cert . toString ( ) , 'sha256' )
275- if ( ! ( currentCertCertId . alg === singleResponse . certid . alg && currentCertCertId . issname === singleResponse . certid . issname &&
276- currentCertCertId . isskey === singleResponse . certid . isskey && currentCertCertId . sbjsn === singleResponse . certid . sbjsn ) ) {
277- continue
278- }
279- // Validate contents
280- const issueDate = this . parseX509Date ( singleResponse . thisupdate )
281- const nextDate = this . parseX509Date ( singleResponse . nextupdate )
282-
283- if ( singleResponse . status . status !== 'good' || new Date ( ) . getTime ( ) - MAX_SKEW < issueDate . getTime ( ) || nextDate . getTime ( ) < new Date ( ) . getTime ( ) + MAX_SKEW ) {
284- throw new VerificationException ( VerificationStatus . FAILURE )
285- }
286- // Success
287- return
288- }
269+ if ( ! verify ( shortAlg , Buffer . from ( responseData , 'hex' ) , signingCert . publicKey , Buffer . from ( parsedResponse . sighex , 'hex' ) ) ) {
270+ throw new VerificationException ( VerificationStatus . FAILURE )
271+ }
272+
273+ for ( const singleResponse of parsedResponse . array ) {
274+ // Confirm entry is for this cert
275+ const certIdBuilder = new KJUR . asn1 . ocsp . CertID ( ) as any
276+ const currentCertCertId = certIdBuilder . getParamByCerts ( issuer . toString ( ) , cert . toString ( ) , 'sha256' )
277+ if ( ! ( currentCertCertId . alg === singleResponse . certid . alg && currentCertCertId . issname === singleResponse . certid . issname &&
278+ currentCertCertId . isskey === singleResponse . certid . isskey && currentCertCertId . sbjsn === singleResponse . certid . sbjsn ) ) {
279+ continue
280+ }
281+ // Validate contents
282+ const issueDate = this . parseX509Date ( singleResponse . thisupdate )
283+ const nextDate = this . parseX509Date ( singleResponse . nextupdate )
284+
285+ if ( singleResponse . status . status !== 'good' || new Date ( ) . getTime ( ) - MAX_SKEW < issueDate . getTime ( ) || nextDate . getTime ( ) < new Date ( ) . getTime ( ) + MAX_SKEW ) {
289286 throw new VerificationException ( VerificationStatus . FAILURE )
290- } ) ;
287+ }
288+ // Success
289+ return
290+ }
291+ throw new VerificationException ( VerificationStatus . FAILURE )
291292 }
292293
293294 private checkDates ( cert : X509Certificate , effectiveDate : Date ) {
0 commit comments