Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 3 additions & 6 deletions jws_verification.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import jsonwebtoken = require('jsonwebtoken');
import base64url from 'base64url';
import { KeyObject, X509Certificate, createHash, verify } from 'crypto';
import { KJUR, X509, ASN1HEX } from 'jsrsasign';
import fetch, { Headers } from 'node-fetch';
import { Environment } from './models/Environment';
import { JWSTransactionDecodedPayload, JWSTransactionDecodedPayloadValidator } from './models/JWSTransactionDecodedPayload';
import { ResponseBodyV2DecodedPayload, ResponseBodyV2DecodedPayloadValidator } from './models/ResponseBodyV2DecodedPayload';
Expand Down Expand Up @@ -313,16 +312,14 @@ export class SignedDataVerifier {
throw new VerificationException(VerificationStatus.INVALID_CERTIFICATE)
}
const request = new KJUR.asn1.ocsp.OCSPRequest({reqList: [{issuerCert: issuer.toString(), subjectCert: cert.toString() , alg: "sha256"}]})
const headers = new Headers()
headers.append('Content-Type', 'application/ocsp-request')

let response
try {
response = await fetch(matchResult[1], {
headers: headers,
headers: { 'Content-Type': 'application/ocsp-request' },
method: 'POST',
body: Buffer.from(request.getEncodedHex(), 'hex'),
timeout: 30000
signal: AbortSignal.timeout(30000)
})

if (!response.ok) {
Expand All @@ -336,7 +333,7 @@ export class SignedDataVerifier {
throw new VerificationException(VerificationStatus.RETRYABLE_VERIFICATION_FAILURE, error instanceof Error ? error : undefined)
}

const responseBuffer = await response.buffer()
const responseBuffer = Buffer.from(await response.arrayBuffer())
const parsedResponse = new (KJUR.asn1.ocsp as any).OCSPParser().getOCSPResponse(responseBuffer.toString('hex'))
// The issuer could also be the signer
const jsrassignX509Issuer = new X509()
Expand Down
10 changes: 6 additions & 4 deletions tests/unit-tests/jws_verification.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@

import assert = require("assert");
import jsonwebtoken = require('jsonwebtoken');
import nodeFetch = require('node-fetch');
import { KeyObject, X509Certificate, createHash, createPublicKey, generateKeyPairSync } from "crypto";
import { KJUR, X509 } from "jsrsasign";
import { Response } from "node-fetch";
import { SignedDataVerifier, VerificationException, VerificationStatus } from "../../jws_verification";
import { Environment } from "../../models/Environment";
import { readFile, readBytes, getSignedPayloadVerifier, getSignedPayloadVerifierWithDefaultAppAppleId, getDefaultSignedPayloadVerifier } from "../util";
Expand Down Expand Up @@ -623,7 +621,7 @@ describe("OCSP Checks", () => {
}

function mockOCSPResponse(body: Buffer, status: number = 200) {
return jest.spyOn(nodeFetch, 'default').mockImplementation(() => Promise.resolve(new Response(body, { status: status })))
return jest.spyOn(global, 'fetch').mockImplementation(() => Promise.resolve(new Response(new Uint8Array(body), { status: status })))
}

async function expectOCSPFailure(status: VerificationStatus) {
Expand All @@ -645,6 +643,10 @@ describe("OCSP Checks", () => {
await getVerifier().testCheckOCSPStatus(leaf, intermediate)
expect(spy).toHaveBeenCalledTimes(1)
expect(spy.mock.calls[0][0]).toBe(OCSP_URL)
const init = spy.mock.calls[0][1] as RequestInit
expect(init.method).toBe('POST')
expect(init.headers).toEqual({ 'Content-Type': 'application/ocsp-request' })
expect(init.signal).toBeInstanceOf(AbortSignal)
})

it('should accept a good OCSP response signed directly by the issuer', async () => {
Expand Down Expand Up @@ -721,7 +723,7 @@ describe("OCSP Checks", () => {
})

it('should return a retryable failure when the OCSP responder cannot be reached', async () => {
jest.spyOn(nodeFetch, 'default').mockImplementation(() => Promise.reject(new Error("Network error")))
jest.spyOn(global, 'fetch').mockImplementation(() => Promise.reject(new Error("Network error")))
await expectOCSPFailure(VerificationStatus.RETRYABLE_VERIFICATION_FAILURE)
})
});