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
12 changes: 12 additions & 0 deletions jsrsasign_loader.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Copyright (c) 2023 Apple Inc. Licensed under MIT License.

type Jsrsasign = typeof import('jsrsasign')

let jsrsasign: Jsrsasign | undefined

export function getJsrsasign(): Jsrsasign {
if (jsrsasign === undefined) {
jsrsasign = require('jsrsasign') as Jsrsasign
}
return jsrsasign
}
8 changes: 5 additions & 3 deletions jws_verification.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,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';
Expand All @@ -14,6 +13,7 @@ import { DecodedRealtimeRequestBody, DecodedRealtimeRequestBodyValidator } from
import { Validator } from './models/Validator';
import { DecodedSignedData } from './models/DecodedSignedData';
import { AppTransaction, AppTransactionValidator } from './models/AppTransaction';
import { getJsrsasign } from './jsrsasign_loader';

const MAX_SKEW = 60000

Expand Down Expand Up @@ -278,6 +278,7 @@ export class SignedDataVerifier {
}

protected async verifyCertificateChainWithoutCaching(trustedRoots: X509Certificate[], leaf: X509Certificate, intermediate: X509Certificate, effectiveDate: Date): Promise<KeyObject> {
const { X509 } = getJsrsasign()
let validity = false
let rootCert
for (const root of trustedRoots) {
Expand Down Expand Up @@ -307,6 +308,7 @@ export class SignedDataVerifier {
return leaf.publicKey
}
protected async checkOCSPStatus(cert: X509Certificate, issuer: X509Certificate): Promise<void> {
const { ASN1HEX, KJUR, X509 } = getJsrsasign()
const authorityRex = /^OCSP - URI:(.*)$/m
const matchResult = cert.infoAccess ? authorityRex.exec(cert.infoAccess) : ""
if (matchResult === null || matchResult.length !== 2) {
Expand Down Expand Up @@ -341,7 +343,7 @@ export class SignedDataVerifier {
// The issuer could also be the signer
const jsrassignX509Issuer = new X509()
jsrassignX509Issuer.readCertHex(issuer.raw.toString('hex'))
const allCerts: X509[] = [jsrassignX509Issuer]
const allCerts = [jsrassignX509Issuer]
for (const certHex of parsedResponse.certs) {
const cert = new X509()
cert.readCertHex(certHex)
Expand Down Expand Up @@ -457,4 +459,4 @@ export class VerificationException extends Error {
this.status = status
this.cause = cause
}
}
}
3 changes: 2 additions & 1 deletion receipt_utility.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Copyright (c) 2023 Apple Inc. Licensed under MIT License.

import { ASN1HEX } from 'jsrsasign';
import { getJsrsasign } from './jsrsasign_loader'

const IN_APP_TYPE_ID = 17;
const TRANSACTION_IDENTIFIER_TYPE_ID = 1703;
Expand Down Expand Up @@ -96,6 +96,7 @@ export class ReceiptUtility {
* @throws Error If the receipt is not valid base64, is not a PKCS#7 app receipt, or is otherwise not in the expected format
*/
extractTransactionIdFromAppReceipt(appReceipt: string): string | null {
const { ASN1HEX } = getJsrsasign()
const receipt = decodeBase64(appReceipt).toString('hex')
// Xcode receipts use indefinite length encoding, not supported by all parsers
// Indefinite length encoding is only entered, but never left during parsing for receipts
Expand Down
13 changes: 13 additions & 0 deletions tests/unit-tests/jsrsasign_loading.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Copyright (c) 2023 Apple Inc. Licensed under MIT License.

describe('jsrsasign loading', () => {
it('does not load jsrsasign while importing the public library', () => {
jest.isolateModules(() => {
jest.doMock('jsrsasign', () => {
throw new Error('jsrsasign was loaded during module initialization')
}, { virtual: true })

expect(() => require('../../index')).not.toThrow()
})
})
})