| 1 |
"use strict"; |
| 2 |
Object.defineProperty(exports, "__esModule", { value: true }); |
| 3 |
exports.filterTLogAuthorities = exports.filterCertAuthorities = void 0; |
| 4 |
exports.toTrustMaterial = toTrustMaterial; |
| 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 protobuf_specs_1 = require("@sigstore/protobuf-specs"); |
| 22 |
const error_1 = require("../error"); |
| 23 |
const BEGINNING_OF_TIME = new Date(0); |
| 24 |
const END_OF_TIME = new Date(8640000000000000); |
| 25 |
var filter_1 = require("./filter"); |
| 26 |
Object.defineProperty(exports, "filterCertAuthorities", { enumerable: true, get: function () { return filter_1.filterCertAuthorities; } }); |
| 27 |
Object.defineProperty(exports, "filterTLogAuthorities", { enumerable: true, get: function () { return filter_1.filterTLogAuthorities; } }); |
| 28 |
function toTrustMaterial(root, keys) { |
| 29 |
const keyFinder = typeof keys === 'function' ? keys : keyLocator(keys); |
| 30 |
return { |
| 31 |
certificateAuthorities: root.certificateAuthorities.map(createCertAuthority), |
| 32 |
timestampAuthorities: root.timestampAuthorities.map(createCertAuthority), |
| 33 |
tlogs: root.tlogs.map(createTLogAuthority), |
| 34 |
ctlogs: root.ctlogs.map(createTLogAuthority), |
| 35 |
publicKey: keyFinder, |
| 36 |
}; |
| 37 |
} |
| 38 |
function createTLogAuthority(tlogInstance) { |
| 39 |
const keyDetails = tlogInstance.publicKey.keyDetails; |
| 40 |
const keyType = keyDetails === protobuf_specs_1.PublicKeyDetails.PKCS1_RSA_PKCS1V5 || |
| 41 |
keyDetails === protobuf_specs_1.PublicKeyDetails.PKIX_RSA_PKCS1V5 || |
| 42 |
keyDetails === protobuf_specs_1.PublicKeyDetails.PKIX_RSA_PKCS1V15_2048_SHA256 || |
| 43 |
keyDetails === protobuf_specs_1.PublicKeyDetails.PKIX_RSA_PKCS1V15_3072_SHA256 || |
| 44 |
keyDetails === protobuf_specs_1.PublicKeyDetails.PKIX_RSA_PKCS1V15_4096_SHA256 |
| 45 |
? 'pkcs1' |
| 46 |
: 'spki'; |
| 47 |
/* istanbul ignore next */ |
| 48 |
return { |
| 49 |
baseURL: tlogInstance.baseUrl, |
| 50 |
logID: tlogInstance.checkpointKeyId |
| 51 |
? tlogInstance.checkpointKeyId.keyId |
| 52 |
: tlogInstance.logId.keyId, |
| 53 |
publicKey: core_1.crypto.createPublicKey(tlogInstance.publicKey.rawBytes, keyType), |
| 54 |
validFor: { |
| 55 |
start: tlogInstance.publicKey.validFor?.start || BEGINNING_OF_TIME, |
| 56 |
end: tlogInstance.publicKey.validFor?.end || END_OF_TIME, |
| 57 |
}, |
| 58 |
}; |
| 59 |
} |
| 60 |
function createCertAuthority(ca) { |
| 61 |
/* istanbul ignore next */ |
| 62 |
return { |
| 63 |
certChain: ca.certChain.certificates.map((cert) => { |
| 64 |
return core_1.X509Certificate.parse(Buffer.from(cert.rawBytes)); |
| 65 |
}), |
| 66 |
validFor: { |
| 67 |
start: ca.validFor?.start || BEGINNING_OF_TIME, |
| 68 |
end: ca.validFor?.end || END_OF_TIME, |
| 69 |
}, |
| 70 |
}; |
| 71 |
} |
| 72 |
function keyLocator(keys) { |
| 73 |
return (hint) => { |
| 74 |
const key = (keys || {})[hint]; |
| 75 |
if (!key) { |
| 76 |
throw new error_1.VerificationError({ |
| 77 |
code: 'PUBLIC_KEY_ERROR', |
| 78 |
message: `key not found: ${hint}`, |
| 79 |
}); |
| 80 |
} |
| 81 |
return { |
| 82 |
publicKey: core_1.crypto.createPublicKey(key.rawBytes), |
| 83 |
validFor: (date) => { |
| 84 |
/* istanbul ignore next */ |
| 85 |
return ((key.validFor?.start || BEGINNING_OF_TIME) <= date && |
| 86 |
(key.validFor?.end || END_OF_TIME) >= date); |
| 87 |
}, |
| 88 |
}; |
| 89 |
}; |
| 90 |
} |