X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Fhelpers%2Fpeertube-crypto.ts;h=b8f7c782ae7b88ba88463d14f156d43d65a9dec1;hb=75b7117f078461d2507572ba9da6527894e1b734;hp=9148df2ebdc01194a3f52fc5327f47147cb3ca92;hpb=97567dd81f508dd6295ac4d73d849aa2ce0a6549;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/helpers/peertube-crypto.ts b/server/helpers/peertube-crypto.ts index 9148df2eb..b8f7c782a 100644 --- a/server/helpers/peertube-crypto.ts +++ b/server/helpers/peertube-crypto.ts @@ -1,14 +1,19 @@ +import { compare, genSalt, hash } from 'bcrypt' +import { createSign, createVerify } from 'crypto' import { Request } from 'express' +import { cloneDeep } from 'lodash' +import { sha256 } from '@shared/extra-utils' import { BCRYPT_SALT_SIZE, HTTP_SIGNATURE, PRIVATE_RSA_KEY_SIZE } from '../initializers/constants' -import { ActorModel } from '../models/activitypub/actor' -import { bcryptComparePromise, bcryptGenSaltPromise, bcryptHashPromise, createPrivateKey, getPublicKey, sha256 } from './core-utils' -import { jsig, jsonld } from './custom-jsonld-signature' +import { MActor } from '../types/models' +import { createPrivateKey, getPublicKey, promisify1, promisify2 } from './core-utils' +import { jsonld } from './custom-jsonld-signature' import { logger } from './logger' -import { cloneDeep } from 'lodash' -import { createVerify } from 'crypto' -import { buildDigest } from '../lib/job-queue/handlers/utils/activitypub-http-utils' -const httpSignature = require('http-signature') +const bcryptComparePromise = promisify2(compare) +const bcryptGenSaltPromise = promisify1(genSalt) +const bcryptHashPromise = promisify2(hash) + +const httpSignature = require('@peertube/http-signature') async function createPrivateAndPublicKeys () { logger.info('Generating a RSA key...') @@ -41,80 +46,35 @@ function isHTTPSignatureDigestValid (rawBody: Buffer, req: Request): boolean { return true } -function isHTTPSignatureVerified (httpSignatureParsed: any, actor: ActorModel): boolean { +function isHTTPSignatureVerified (httpSignatureParsed: any, actor: MActor): boolean { return httpSignature.verifySignature(httpSignatureParsed, actor.publicKey) === true } function parseHTTPSignature (req: Request, clockSkew?: number) { - return httpSignature.parse(req, { authorizationHeaderName: HTTP_SIGNATURE.HEADER_NAME, clockSkew }) + const headers = req.method === 'POST' + ? HTTP_SIGNATURE.REQUIRED_HEADERS.POST + : HTTP_SIGNATURE.REQUIRED_HEADERS.ALL + + return httpSignature.parse(req, { clockSkew, headers }) } // JSONLD -async function isJsonLDSignatureVerified (fromActor: ActorModel, signedDocument: any): Promise { +function isJsonLDSignatureVerified (fromActor: MActor, signedDocument: any): Promise { if (signedDocument.signature.type === 'RsaSignature2017') { - // Mastodon algorithm - const res = await isJsonLDRSA2017Verified(fromActor, signedDocument) - // Success? If no, try with our library - if (res === true) return true - } - - const publicKeyObject = { - '@context': jsig.SECURITY_CONTEXT_URL, - id: fromActor.url, - type: 'CryptographicKey', - owner: fromActor.url, - publicKeyPem: fromActor.publicKey - } - - const publicKeyOwnerObject = { - '@context': jsig.SECURITY_CONTEXT_URL, - id: fromActor.url, - publicKey: [ publicKeyObject ] + return isJsonLDRSA2017Verified(fromActor, signedDocument) } - const options = { - publicKey: publicKeyObject, - publicKeyOwner: publicKeyOwnerObject - } + logger.warn('Unknown JSON LD signature %s.', signedDocument.signature.type, signedDocument) - return jsig.promises - .verify(signedDocument, options) - .then((result: { verified: boolean }) => result.verified) - .catch(err => { - logger.error('Cannot check signature.', { err }) - return false - }) + return Promise.resolve(false) } // Backward compatibility with "other" implementations -async function isJsonLDRSA2017Verified (fromActor: ActorModel, signedDocument: any) { - function hash (obj: any): Promise { - return jsonld.promises - .normalize(obj, { - algorithm: 'URDNA2015', - format: 'application/n-quads' - }) - .then(res => sha256(res)) - } - - const signatureCopy = cloneDeep(signedDocument.signature) - Object.assign(signatureCopy, { - '@context': [ - 'https://w3id.org/security/v1', - { RsaSignature2017: 'https://w3id.org/security#RsaSignature2017' } - ] - }) - delete signatureCopy.type - delete signatureCopy.id - delete signatureCopy.signatureValue - - const docWithoutSignature = cloneDeep(signedDocument) - delete docWithoutSignature.signature - +async function isJsonLDRSA2017Verified (fromActor: MActor, signedDocument: any) { const [ documentHash, optionsHash ] = await Promise.all([ - hash(docWithoutSignature), - hash(signatureCopy) + createDocWithoutSignatureHash(signedDocument), + createSignatureHash(signedDocument.signature) ]) const toVerify = optionsHash + documentHash @@ -125,14 +85,33 @@ async function isJsonLDRSA2017Verified (fromActor: ActorModel, signedDocument: a return verify.verify(fromActor.publicKey, signedDocument.signature.signatureValue, 'base64') } -function signJsonLDObject (byActor: ActorModel, data: any) { - const options = { - privateKeyPem: byActor.privateKey, +async function signJsonLDObject (byActor: MActor, data: T) { + const signature = { + type: 'RsaSignature2017', creator: byActor.url, - algorithm: 'RsaSignature2017' + created: new Date().toISOString() } - return jsig.promises.sign(data, options) + const [ documentHash, optionsHash ] = await Promise.all([ + createDocWithoutSignatureHash(data), + createSignatureHash(signature) + ]) + + const toSign = optionsHash + documentHash + + const sign = createSign('RSA-SHA256') + sign.update(toSign, 'utf8') + + const signatureValue = sign.sign(byActor.privateKey, 'base64') + Object.assign(signature, { signatureValue }) + + return Object.assign(data, { signature }) +} + +function buildDigest (body: any) { + const rawBody = typeof body === 'string' ? body : JSON.stringify(body) + + return 'SHA-256=' + sha256(rawBody, 'base64') } // --------------------------------------------------------------------------- @@ -141,9 +120,44 @@ export { isHTTPSignatureDigestValid, parseHTTPSignature, isHTTPSignatureVerified, + buildDigest, isJsonLDSignatureVerified, comparePassword, createPrivateAndPublicKeys, cryptPassword, signJsonLDObject } + +// --------------------------------------------------------------------------- + +function hashObject (obj: any): Promise { + return jsonld.promises + .normalize(obj, { + algorithm: 'URDNA2015', + format: 'application/n-quads' + }) + .then(res => sha256(res)) +} + +function createSignatureHash (signature: any) { + const signatureCopy = cloneDeep(signature) + Object.assign(signatureCopy, { + '@context': [ + 'https://w3id.org/security/v1', + { RsaSignature2017: 'https://w3id.org/security#RsaSignature2017' } + ] + }) + + delete signatureCopy.type + delete signatureCopy.id + delete signatureCopy.signatureValue + + return hashObject(signatureCopy) +} + +function createDocWithoutSignatureHash (doc: any) { + const docWithoutSignature = cloneDeep(doc) + delete docWithoutSignature.signature + + return hashObject(docWithoutSignature) +}