verify.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. "use strict";
  2. var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
  3. if (k2 === undefined) k2 = k;
  4. var desc = Object.getOwnPropertyDescriptor(m, k);
  5. if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
  6. desc = { enumerable: true, get: function() { return m[k]; } };
  7. }
  8. Object.defineProperty(o, k2, desc);
  9. }) : (function(o, m, k, k2) {
  10. if (k2 === undefined) k2 = k;
  11. o[k2] = m[k];
  12. }));
  13. var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
  14. Object.defineProperty(o, "default", { enumerable: true, value: v });
  15. }) : function(o, v) {
  16. o["default"] = v;
  17. });
  18. var __importStar = (this && this.__importStar) || function (mod) {
  19. if (mod && mod.__esModule) return mod;
  20. var result = {};
  21. if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
  22. __setModuleDefault(result, mod);
  23. return result;
  24. };
  25. Object.defineProperty(exports, "__esModule", { value: true });
  26. exports.Verifier = void 0;
  27. const ca = __importStar(require("./ca/verify"));
  28. const error_1 = require("./error");
  29. const tlog = __importStar(require("./tlog/verify"));
  30. const sigstore = __importStar(require("./types/sigstore"));
  31. const util_1 = require("./util");
  32. class Verifier {
  33. constructor(trustedRoot, keySelector) {
  34. this.trustedRoot = trustedRoot;
  35. this.keySelector = keySelector || (() => undefined);
  36. }
  37. // Verifies the bundle signature, the bundle's certificate chain (if present)
  38. // and the bundle's transparency log entries.
  39. verify(bundle, options, data) {
  40. this.verifyArtifactSignature(bundle, data);
  41. if (sigstore.isBundleWithCertificateChain(bundle)) {
  42. this.verifySigningCertificate(bundle, options);
  43. }
  44. if (options.tlogOptions.disable === false) {
  45. this.verifyTLogEntries(bundle, options);
  46. }
  47. }
  48. // Performs bundle signature verification. Determines the type of the bundle
  49. // content and delegates to the appropriate signature verification function.
  50. verifyArtifactSignature(bundle, data) {
  51. const publicKey = this.getPublicKey(bundle);
  52. switch (bundle.content?.$case) {
  53. case 'messageSignature':
  54. if (!data) {
  55. throw new error_1.VerificationError('no data provided for message signature verification');
  56. }
  57. verifyMessageSignature(data, bundle.content.messageSignature, publicKey);
  58. break;
  59. case 'dsseEnvelope':
  60. verifyDSSESignature(bundle.content.dsseEnvelope, publicKey);
  61. break;
  62. }
  63. }
  64. // Performs verification of the bundle's certificate chain. The bundle must
  65. // contain a certificate chain and the options must contain the required
  66. // options for CA verification.
  67. // TODO: We've temporarily removed the requirement that the options contain
  68. // the list of trusted signer identities. This will be added back in a future
  69. // release.
  70. verifySigningCertificate(bundle, options) {
  71. if (!sigstore.isCAVerificationOptions(options)) {
  72. throw new error_1.VerificationError('no trusted certificates provided for verification');
  73. }
  74. ca.verifySigningCertificate(bundle, this.trustedRoot, options);
  75. }
  76. // Performs verification of the bundle's transparency log entries. The bundle
  77. // must contain a list of transparency log entries.
  78. verifyTLogEntries(bundle, options) {
  79. tlog.verifyTLogEntries(bundle, this.trustedRoot, options.tlogOptions);
  80. }
  81. // Returns the public key which will be used to verify the bundle signature.
  82. // The public key is selected based on the verification material in the bundle
  83. // and the options provided.
  84. getPublicKey(bundle) {
  85. // Select the key which will be used to verify the signature
  86. switch (bundle.verificationMaterial?.content?.$case) {
  87. // If the bundle contains a certificate chain, the public key is the
  88. // first certificate in the chain (the signing certificate)
  89. case 'x509CertificateChain':
  90. return getPublicKeyFromCertificateChain(bundle.verificationMaterial.content.x509CertificateChain);
  91. // If the bundle contains a public key hint, the public key is selected
  92. // from the list of trusted keys in the options
  93. case 'publicKey':
  94. return getPublicKeyFromHint(bundle.verificationMaterial.content.publicKey, this.keySelector);
  95. }
  96. }
  97. }
  98. exports.Verifier = Verifier;
  99. // Retrieves the public key from the first certificate in the certificate chain
  100. function getPublicKeyFromCertificateChain(certificateChain) {
  101. const cert = util_1.pem.fromDER(certificateChain.certificates[0].rawBytes);
  102. return util_1.crypto.createPublicKey(cert);
  103. }
  104. // Retrieves the public key through the key selector callback, passing the
  105. // public key hint from the bundle
  106. function getPublicKeyFromHint(publicKeyID, keySelector) {
  107. const key = keySelector(publicKeyID.hint);
  108. if (!key) {
  109. throw new error_1.VerificationError('no public key found for signature verification');
  110. }
  111. try {
  112. return util_1.crypto.createPublicKey(key);
  113. }
  114. catch (e) {
  115. throw new error_1.VerificationError('invalid public key');
  116. }
  117. }
  118. // Performs signature verification for bundle containing a message signature.
  119. // Verifies that the digest and signature found in the bundle match the
  120. // provided data.
  121. function verifyMessageSignature(data, messageSignature, publicKey) {
  122. // Extract signature for message
  123. const { signature, messageDigest } = messageSignature;
  124. const calculatedDigest = util_1.crypto.hash(data);
  125. if (!calculatedDigest.equals(messageDigest.digest)) {
  126. throw new error_1.VerificationError('message digest verification failed');
  127. }
  128. if (!util_1.crypto.verifyBlob(data, publicKey, signature)) {
  129. throw new error_1.VerificationError('artifact signature verification failed');
  130. }
  131. }
  132. // Performs signature verification for bundle containing a DSSE envelope.
  133. // Calculates the PAE for the DSSE envelope and verifies it against the
  134. // signature in the envelope.
  135. function verifyDSSESignature(envelope, publicKey) {
  136. // Construct payload over which the signature was originally created
  137. const { payloadType, payload } = envelope;
  138. const data = util_1.dsse.preAuthEncoding(payloadType, payload);
  139. // Only support a single signature in DSSE
  140. const signature = envelope.signatures[0].sig;
  141. if (!util_1.crypto.verifyBlob(data, publicKey, signature)) {
  142. throw new error_1.VerificationError('artifact signature verification failed');
  143. }
  144. }