| 1 |
"use strict"; |
| 2 |
Object.defineProperty(exports, "__esModule", { value: true }); |
| 3 |
exports.verifyPublicKey = verifyPublicKey; |
| 4 |
exports.verifyCertificate = verifyCertificate; |
| 5 |
/* |
| 6 |
Copyright 2023 The Sigstore Authors. |
| 7 |
|
| 8 |
Licensed under the Apache License, Version 2.0 (the "License"); |
| 9 |
you may not use this file except in compliance with the License. |
| 10 |
You may obtain a copy of the License at |
| 11 |
|
| 12 |
http://www.apache.org/licenses/LICENSE-2.0 |
| 13 |
|
| 14 |
Unless required by applicable law or agreed to in writing, software |
| 15 |
distributed under the License is distributed on an "AS IS" BASIS, |
| 16 |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 17 |
See the License for the specific language governing permissions and |
| 18 |
limitations under the License. |
| 19 |
*/ |
| 20 |
const core_1 = require("@sigstore/core"); |
| 21 |
const error_1 = require("../error"); |
| 22 |
const certificate_1 = require("./certificate"); |
| 23 |
const sct_1 = require("./sct"); |
| 24 |
const OID_FULCIO_ISSUER_V1 = '1.3.6.1.4.1.57264.1.1'; |
| 25 |
const OID_FULCIO_ISSUER_V2 = '1.3.6.1.4.1.57264.1.8'; |
| 26 |
function verifyPublicKey(hint, timestamps, trustMaterial) { |
| 27 |
const key = trustMaterial.publicKey(hint); |
| 28 |
timestamps.forEach((timestamp) => { |
| 29 |
if (!key.validFor(timestamp)) { |
| 30 |
throw new error_1.VerificationError({ |
| 31 |
code: 'PUBLIC_KEY_ERROR', |
| 32 |
message: `Public key is not valid for timestamp: ${timestamp.toISOString()}`, |
| 33 |
}); |
| 34 |
} |
| 35 |
}); |
| 36 |
return { key: key.publicKey }; |
| 37 |
} |
| 38 |
function verifyCertificate(leaf, timestamps, trustMaterial) { |
| 39 |
// Check that leaf certificate chains to a trusted CA |
| 40 |
let path = []; |
| 41 |
timestamps.forEach((timestamp) => { |
| 42 |
path = (0, certificate_1.verifyCertificateChain)(timestamp, leaf, trustMaterial.certificateAuthorities); |
| 43 |
}); |
| 44 |
return { |
| 45 |
scts: (0, sct_1.verifySCTs)(path[0], path[1], trustMaterial.ctlogs), |
| 46 |
signer: getSigner(path[0]), |
| 47 |
}; |
| 48 |
} |
| 49 |
function getSigner(cert) { |
| 50 |
let issuer; |
| 51 |
const issuerExtension = cert.extension(OID_FULCIO_ISSUER_V2); |
| 52 |
/* istanbul ignore next */ |
| 53 |
if (issuerExtension) { |
| 54 |
issuer = issuerExtension.valueObj.subs?.[0]?.value.toString('ascii'); |
| 55 |
} |
| 56 |
else { |
| 57 |
issuer = cert.extension(OID_FULCIO_ISSUER_V1)?.value.toString('ascii'); |
| 58 |
} |
| 59 |
const oids = cert.extensions.map((ext) => { |
| 60 |
const oid = ext.subs[0].toOID(); |
| 61 |
return { |
| 62 |
oid: { id: oid.split('.').map(Number) }, |
| 63 |
value: ext.subs[ext.subs.length - 1].value, |
| 64 |
}; |
| 65 |
}); |
| 66 |
const identity = { |
| 67 |
extensions: { issuer }, |
| 68 |
subjectAlternativeName: cert.subjectAltName, |
| 69 |
oids, |
| 70 |
}; |
| 71 |
return { |
| 72 |
key: core_1.crypto.createPublicKey(cert.publicKey), |
| 73 |
identity, |
| 74 |
}; |
| 75 |
} |