Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 | 10x 10x 10x 10x 10x 64x 32x 64x 35x 32x 32x 3x 63x 29x 1x 28x 28x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 28x 8x 2x 6x 20x 20x 16x 4x 3x 1x 6x 6x 12x 6x 6x 44x 88x | import { decryptPrivateKey } from './runtime'
import { decodeBase64ToBytes, encodeBytesToBase64, encodeUtf8 } from './storage-encoding'
export interface ClientCertificate {
/** Base64url SHA-256 digest of the first certificate's DER bytes. */
thumbprint: string
/** Base64 DER certificates in PEM order. */
chain: string[]
privateKey: CryptoKey
}
export interface ClientAssertionOptions {
clientId: string
/** The token endpoint. */
audience: string
/** Sends the chain as `x5c` for subject name and issuer authentication. */
sendCertificateChain?: boolean
}
interface PemBlock {
text: string
label: string
body: string
}
const pemBlocks = /-----BEGIN ([A-Z0-9 ]+)-----([\s\S]*?)-----END \1-----/gu
const whitespace = /\s/gu
const base64Padding = /=+$/u
// PrivateKeyInfo version 0 and the rsaEncryption algorithm, which precede a PKCS#1 key in PKCS#8.
const rsaPrivateKeyInfoPrefix = [
0x02, 0x01, 0x00, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01,
0x05, 0x00,
]
const assertionLifetimeSeconds = 600
/** Reads the certificates and the RSA private key from the contents of a PEM file. */
export async function loadClientCertificate(
pem: string,
password?: string,
): Promise<ClientCertificate> {
const blocks = Array.from(pem.matchAll(pemBlocks), ([text, label = '', body = '']): PemBlock => ({
text,
label,
body,
}))
const chain = blocks
.filter(({ label }) => label === 'CERTIFICATE')
.map(({ body }) => body.replaceAll(whitespace, ''))
const [certificate] = chain
if (certificate === undefined) {
throw new Error('The file at the specified path does not contain a PEM-encoded certificate.')
}
const key = blocks.find(({ label }) => label.endsWith('PRIVATE KEY'))
if (key === undefined) {
throw new Error('The file at the specified path does not contain a PEM-encoded private key.')
}
const digest = await crypto.subtle.digest('SHA-256', decodeBase64ToBytes(certificate))
const keyBytes = await readPkcs8(key, password)
try {
const privateKey = await crypto.subtle.importKey(
'pkcs8',
keyBytes,
{ name: 'RSA-PSS', hash: 'SHA-256' },
false,
['sign'],
)
return { thumbprint: encodeBase64Url(digest), chain, privateKey }
} finally {
keyBytes.fill(0)
}
}
/** Signs a PS256 client assertion that is valid for 10 minutes. */
export async function createClientAssertion(
certificate: ClientCertificate,
options: ClientAssertionOptions,
): Promise<string> {
const now = Math.floor(Date.now() / 1000)
const header = {
alg: 'PS256',
typ: 'JWT',
'x5t#S256': certificate.thumbprint,
x5c: options.sendCertificateChain === true ? certificate.chain : undefined,
}
const claims = {
aud: options.audience,
iss: options.clientId,
sub: options.clientId,
jti: crypto.randomUUID(),
nbf: now,
iat: now,
exp: now + assertionLifetimeSeconds,
}
const content = `${encodeJson(header)}.${encodeJson(claims)}`
// PS256 uses a salt as long as the SHA-256 digest.
const signature = await crypto.subtle.sign(
{ name: 'RSA-PSS', saltLength: 32 },
certificate.privateKey,
encodeUtf8(content),
)
return `${content}.${encodeBase64Url(signature)}`
}
async function readPkcs8(
{ text, label, body }: PemBlock,
password: string | undefined,
): Promise<Uint8Array<ArrayBuffer>> {
// Legacy encrypted keys keep the RSA PRIVATE KEY label and announce the cipher in headers.
if (label === 'ENCRYPTED PRIVATE KEY' || body.includes('Proc-Type:')) {
if (password === undefined) {
throw new Error('The private key is encrypted and requires a password')
}
return await decryptPrivateKey(text, password)
}
const der = decodeBase64ToBytes(body.replaceAll(whitespace, ''))
if (label === 'PRIVATE KEY') {
return der
}
if (label === 'RSA PRIVATE KEY') {
return encodeDer(0x30, [...rsaPrivateKeyInfoPrefix, ...encodeDer(0x04, der)])
}
throw new Error(`Unsupported ${label}; client certificates need an RSA private key`)
}
/** Encodes a DER element; lengths from 128 take a `0x80 | n` byte and then n big-endian bytes. */
function encodeDer(tag: number, content: Uint8Array | number[]): Uint8Array<ArrayBuffer> {
const length: number[] = []
for (let rest = content.length; rest > 0; rest = Math.floor(rest / 256)) {
length.unshift(rest % 256)
}
const lengthBytes = content.length < 0x80 ? [content.length] : [0x80 | length.length, ...length]
return Uint8Array.from([tag, ...lengthBytes, ...content])
}
function encodeJson(value: unknown): string {
return encodeBase64Url(encodeUtf8(JSON.stringify(value)))
}
function encodeBase64Url(bytes: ArrayBuffer | ArrayBufferView): string {
return encodeBytesToBase64(bytes)
.replaceAll('+', '-')
.replaceAll('/', '_')
.replace(base64Padding, '')
}
|