|
| 1 | +'use strict' |
| 2 | + |
| 3 | +const debug = require('debug') |
| 4 | +const log = debug('libp2p:envelope') |
| 5 | +log.error = debug('libp2p:envelope:error') |
| 6 | +const errCode = require('err-code') |
| 7 | + |
| 8 | +const crypto = require('libp2p-crypto') |
| 9 | +const multicodec = require('multicodec') |
| 10 | +const PeerId = require('peer-id') |
| 11 | + |
| 12 | +const Protobuf = require('./envelope.proto') |
| 13 | + |
| 14 | +/** |
| 15 | + * The Envelope is responsible for keeping arbitrary signed by a libp2p peer. |
| 16 | + */ |
| 17 | +class Envelope { |
| 18 | + /** |
| 19 | + * @constructor |
| 20 | + * @param {object} params |
| 21 | + * @param {PeerId} params.peerId |
| 22 | + * @param {Buffer} params.payloadType |
| 23 | + * @param {Buffer} params.payload marshaled record |
| 24 | + * @param {Buffer} params.signature signature of the domain string :: type hint :: payload. |
| 25 | + */ |
| 26 | + constructor ({ peerId, payloadType, payload, signature }) { |
| 27 | + this.peerId = peerId |
| 28 | + this.payloadType = payloadType |
| 29 | + this.payload = payload |
| 30 | + this.signature = signature |
| 31 | + |
| 32 | + // Cache |
| 33 | + this._marshal = undefined |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * Marshal the envelope content. |
| 38 | + * @return {Buffer} |
| 39 | + */ |
| 40 | + marshal () { |
| 41 | + if (this._marshal) { |
| 42 | + return this._marshal |
| 43 | + } |
| 44 | + // TODO: type for marshal (default: RSA) |
| 45 | + const publicKey = crypto.keys.marshalPublicKey(this.peerId.pubKey) |
| 46 | + |
| 47 | + this._marshal = Protobuf.encode({ |
| 48 | + public_key: publicKey, |
| 49 | + payload_type: this.payloadType, |
| 50 | + payload: this.payload, |
| 51 | + signature: this.signature |
| 52 | + }) |
| 53 | + |
| 54 | + return this._marshal |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * Verifies if the other Envelope is identical to this one. |
| 59 | + * @param {Envelope} other |
| 60 | + * @return {boolean} |
| 61 | + */ |
| 62 | + isEqual (other) { |
| 63 | + return this.peerId.pubKey.bytes.equals(other.peerId.pubKey.bytes) && |
| 64 | + this.payloadType.equals(other.payloadType) && |
| 65 | + this.payload.equals(other.payload) && |
| 66 | + this.signature.equals(other.signature) |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * Validate envelope data signature for the given domain. |
| 71 | + * @param {string} domain |
| 72 | + * @return {Promise} |
| 73 | + */ |
| 74 | + async validate (domain) { |
| 75 | + const signData = createSignData(domain, this.payloadType, this.payload) |
| 76 | + |
| 77 | + try { |
| 78 | + await this.peerId.pubKey.verify(signData, this.signature) |
| 79 | + } catch (_) { |
| 80 | + log.error('record signature verification failed') |
| 81 | + // TODO |
| 82 | + throw errCode(new Error('record signature verification failed'), 'ERRORS.ERR_SIGNATURE_VERIFICATION') |
| 83 | + } |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +exports = module.exports = Envelope |
| 88 | + |
| 89 | +/** |
| 90 | +* Seal marshals the given Record, places the marshaled bytes inside an Envelope |
| 91 | +* and signs with the given private key. |
| 92 | +* @async |
| 93 | +* @param {Record} record |
| 94 | +* @param {PeerId} peerId |
| 95 | +* @return {Envelope} |
| 96 | +*/ |
| 97 | +exports.seal = async (record, peerId) => { |
| 98 | + const domain = record.domain |
| 99 | + const payloadType = Buffer.from(`${multicodec.print[record.codec]}${domain}`) |
| 100 | + const payload = record.marshal() |
| 101 | + |
| 102 | + const signData = createSignData(domain, payloadType, payload) |
| 103 | + const signature = await peerId.privKey.sign(signData) |
| 104 | + |
| 105 | + return new Envelope({ |
| 106 | + peerId, |
| 107 | + payloadType, |
| 108 | + payload, |
| 109 | + signature |
| 110 | + }) |
| 111 | +} |
| 112 | + |
| 113 | +// ConsumeEnvelope unmarshals a serialized Envelope and validates its |
| 114 | +// signature using the provided 'domain' string. If validation fails, an error |
| 115 | +// is returned, along with the unmarshalled envelope so it can be inspected. |
| 116 | +// |
| 117 | +// On success, ConsumeEnvelope returns the Envelope itself, as well as the inner payload, |
| 118 | +// unmarshalled into a concrete Record type. The actual type of the returned Record depends |
| 119 | +// on what has been registered for the Envelope's PayloadType (see RegisterType for details). |
| 120 | +exports.openAndCertify = async (data, domain) => { |
| 121 | + const envelope = await unmarshalEnvelope(data) |
| 122 | + await envelope.validate(domain) |
| 123 | + |
| 124 | + return envelope |
| 125 | +} |
| 126 | + |
| 127 | +/** |
| 128 | + * Helper function that prepares a buffer to sign or verify a signature. |
| 129 | + * @param {string} domain |
| 130 | + * @param {number} payloadType |
| 131 | + * @param {Buffer} payload |
| 132 | + * @return {Buffer} |
| 133 | + */ |
| 134 | +const createSignData = (domain, payloadType, payload) => { |
| 135 | + // TODO: this should be compliant with the spec! |
| 136 | + const domainBuffer = Buffer.from(domain) |
| 137 | + const payloadTypeBuffer = Buffer.from(payloadType.toString()) |
| 138 | + |
| 139 | + return Buffer.concat([domainBuffer, payloadTypeBuffer, payload]) |
| 140 | +} |
| 141 | + |
| 142 | +/** |
| 143 | + * Unmarshal a serialized Envelope protobuf message. |
| 144 | + * @param {Buffer} data |
| 145 | + * @return {Envelope} |
| 146 | + */ |
| 147 | +const unmarshalEnvelope = async (data) => { |
| 148 | + const envelopeData = Protobuf.decode(data) |
| 149 | + const peerId = await PeerId.createFromPubKey(envelopeData.public_key) |
| 150 | + |
| 151 | + return new Envelope({ |
| 152 | + peerId, |
| 153 | + payloadType: envelopeData.payload_type, |
| 154 | + payload: envelopeData.payload, |
| 155 | + signature: envelopeData.signature |
| 156 | + }) |
| 157 | +} |
0 commit comments