|
| 1 | +/*! |
| 2 | + * Copyright 2024 Digital Bazaar, Inc. |
| 3 | + * SPDX-License-Identifier: BSD-3-Clause |
| 4 | + */ |
| 5 | +import crypto from 'node:crypto'; |
| 6 | + |
| 7 | +export function createProxy({original, stubs}) { |
| 8 | + if(typeof original === 'object') { |
| 9 | + throw new Error(`Expected parameter original to be an object received ` + |
| 10 | + `${typeof original}`); |
| 11 | + } |
| 12 | + return new Proxy(original, { |
| 13 | + get(target, prop) { |
| 14 | + if(stubs[prop]) { |
| 15 | + return stubs[prop]; |
| 16 | + } |
| 17 | + return Reflect.get(...arguments); |
| 18 | + } |
| 19 | + }); |
| 20 | +} |
| 21 | + |
| 22 | +/** |
| 23 | + * The major jsonld api suites use is canonize. |
| 24 | + * This function intercepts calls on canonize and |
| 25 | + * pass safe: false allowing for invalid jsonld to |
| 26 | + * be issued. |
| 27 | + * |
| 28 | + * @param {object} suite - A DataIntegrityProof. |
| 29 | + * |
| 30 | + * @returns {Proxy<object>} Returns a proxy of the proof. |
| 31 | + */ |
| 32 | +export function unsafeProxy(suite) { |
| 33 | + if(typeof suite !== 'object') { |
| 34 | + return suite; |
| 35 | + } |
| 36 | + // if the suite has a cryptosuite object proxy it |
| 37 | + if(suite._cryptosuite) { |
| 38 | + suite._cryptosuite = new Proxy(suite._cryptosuite, { |
| 39 | + get(target, prop) { |
| 40 | + if(prop === 'canonize') { |
| 41 | + return function(doc, options) { |
| 42 | + return target.canonize(doc, {...options, safe: false}); |
| 43 | + }; |
| 44 | + } |
| 45 | + return Reflect.get(...arguments); |
| 46 | + } |
| 47 | + }); |
| 48 | + } |
| 49 | + return new Proxy(suite, { |
| 50 | + get(target, prop) { |
| 51 | + if(prop === 'canonize') { |
| 52 | + return function(doc, options) { |
| 53 | + return target.canonize(doc, {...options, safe: false}); |
| 54 | + }; |
| 55 | + } |
| 56 | + return Reflect.get(...arguments); |
| 57 | + } |
| 58 | + }); |
| 59 | +} |
| 60 | + |
| 61 | +export function invalidHashProxy({ |
| 62 | + suiteName, |
| 63 | + keyType, |
| 64 | + suite, |
| 65 | +}) { |
| 66 | + if(typeof suite !== 'object') { |
| 67 | + return suite; |
| 68 | + } |
| 69 | + if(suite._cryptosuite) { |
| 70 | + if(suiteName !== 'ecdsa-rdfc-2019') { |
| 71 | + throw new Error(`Unsupported suite ${suiteName}`); |
| 72 | + } |
| 73 | + suite._cryptosuite = new Proxy(suite._cryptosuite, { |
| 74 | + get(target, prop) { |
| 75 | + if(prop === 'createVerifyData') { |
| 76 | + return async function({ |
| 77 | + cryptosuite, document, proof, |
| 78 | + documentLoader, dataIntegrityProof |
| 79 | + } = {}) { |
| 80 | + // this switch the hash to the wrong hash for that keyType |
| 81 | + const algorithm = (keyType === 'P-256') ? 'sha384' : 'sha256'; |
| 82 | + const c14nOptions = { |
| 83 | + documentLoader, |
| 84 | + safe: true, |
| 85 | + base: null, |
| 86 | + skipExpansion: false, |
| 87 | + messageDigestAlgorithm: algorithm |
| 88 | + }; |
| 89 | + |
| 90 | + // await both c14n proof hash and c14n document hash |
| 91 | + const [proofHash, docHash] = await Promise.all([ |
| 92 | + // canonize and hash proof |
| 93 | + _canonizeProof(proof, { |
| 94 | + document, cryptosuite, dataIntegrityProof, c14nOptions |
| 95 | + }).then(c14nProofOptions => sha({ |
| 96 | + algorithm, |
| 97 | + string: c14nProofOptions |
| 98 | + })), |
| 99 | + // canonize and hash document |
| 100 | + cryptosuite.canonize(document, c14nOptions).then( |
| 101 | + c14nDocument => sha({algorithm, string: c14nDocument})) |
| 102 | + ]); |
| 103 | + // concatenate hash of c14n proof options and hash of c14n document |
| 104 | + return _concat(proofHash, docHash); |
| 105 | + }; |
| 106 | + } |
| 107 | + return Reflect.get(...arguments); |
| 108 | + } |
| 109 | + }); |
| 110 | + } |
| 111 | + return suite; |
| 112 | +} |
| 113 | + |
| 114 | +function _concat(b1, b2) { |
| 115 | + const rval = new Uint8Array(b1.length + b2.length); |
| 116 | + rval.set(b1, 0); |
| 117 | + rval.set(b2, b1.length); |
| 118 | + return rval; |
| 119 | +} |
| 120 | + |
| 121 | +export async function sha({algorithm, string}) { |
| 122 | + return new Uint8Array(crypto.createHash(algorithm).update(string).digest()); |
| 123 | +} |
| 124 | + |
| 125 | +async function _canonizeProof(proof, { |
| 126 | + document, cryptosuite, dataIntegrityProof, c14nOptions |
| 127 | +}) { |
| 128 | + // `proofValue` must not be included in the proof options |
| 129 | + proof = { |
| 130 | + '@context': document['@context'], |
| 131 | + ...proof |
| 132 | + }; |
| 133 | + dataIntegrityProof.ensureSuiteContext({ |
| 134 | + document: proof, addSuiteContext: true |
| 135 | + }); |
| 136 | + delete proof.proofValue; |
| 137 | + return cryptosuite.canonize(proof, c14nOptions); |
| 138 | +} |
| 139 | + |
0 commit comments