]>
Commit | Line | Data |
---|---|---|
1 | import { Request } from 'express' | |
2 | import { BCRYPT_SALT_SIZE, HTTP_SIGNATURE, PRIVATE_RSA_KEY_SIZE } from '../initializers/constants' | |
3 | import { createPrivateKey, getPublicKey, promisify1, promisify2, sha256 } from './core-utils' | |
4 | import { jsonld } from './custom-jsonld-signature' | |
5 | import { logger } from './logger' | |
6 | import { cloneDeep } from 'lodash' | |
7 | import { createSign, createVerify } from 'crypto' | |
8 | import { buildDigest } from '../lib/job-queue/handlers/utils/activitypub-http-utils' | |
9 | import * as bcrypt from 'bcrypt' | |
10 | import { MActor } from '../typings/models' | |
11 | ||
12 | const bcryptComparePromise = promisify2<any, string, boolean>(bcrypt.compare) | |
13 | const bcryptGenSaltPromise = promisify1<number, string>(bcrypt.genSalt) | |
14 | const bcryptHashPromise = promisify2<any, string | number, string>(bcrypt.hash) | |
15 | ||
16 | const httpSignature = require('http-signature') | |
17 | ||
18 | async function createPrivateAndPublicKeys () { | |
19 | logger.info('Generating a RSA key...') | |
20 | ||
21 | const { key } = await createPrivateKey(PRIVATE_RSA_KEY_SIZE) | |
22 | const { publicKey } = await getPublicKey(key) | |
23 | ||
24 | return { privateKey: key, publicKey } | |
25 | } | |
26 | ||
27 | // User password checks | |
28 | ||
29 | function comparePassword (plainPassword: string, hashPassword: string) { | |
30 | return bcryptComparePromise(plainPassword, hashPassword) | |
31 | } | |
32 | ||
33 | async function cryptPassword (password: string) { | |
34 | const salt = await bcryptGenSaltPromise(BCRYPT_SALT_SIZE) | |
35 | ||
36 | return bcryptHashPromise(password, salt) | |
37 | } | |
38 | ||
39 | // HTTP Signature | |
40 | ||
41 | function isHTTPSignatureDigestValid (rawBody: Buffer, req: Request): boolean { | |
42 | if (req.headers[HTTP_SIGNATURE.HEADER_NAME] && req.headers['digest']) { | |
43 | return buildDigest(rawBody.toString()) === req.headers['digest'] | |
44 | } | |
45 | ||
46 | return true | |
47 | } | |
48 | ||
49 | function isHTTPSignatureVerified (httpSignatureParsed: any, actor: MActor): boolean { | |
50 | return httpSignature.verifySignature(httpSignatureParsed, actor.publicKey) === true | |
51 | } | |
52 | ||
53 | function parseHTTPSignature (req: Request, clockSkew?: number) { | |
54 | return httpSignature.parse(req, { authorizationHeaderName: HTTP_SIGNATURE.HEADER_NAME, clockSkew }) | |
55 | } | |
56 | ||
57 | // JSONLD | |
58 | ||
59 | function isJsonLDSignatureVerified (fromActor: MActor, signedDocument: any): Promise<boolean> { | |
60 | if (signedDocument.signature.type === 'RsaSignature2017') { | |
61 | return isJsonLDRSA2017Verified(fromActor, signedDocument) | |
62 | } | |
63 | ||
64 | logger.warn('Unknown JSON LD signature %s.', signedDocument.signature.type, signedDocument) | |
65 | ||
66 | return Promise.resolve(false) | |
67 | } | |
68 | ||
69 | // Backward compatibility with "other" implementations | |
70 | async function isJsonLDRSA2017Verified (fromActor: MActor, signedDocument: any) { | |
71 | const [ documentHash, optionsHash ] = await Promise.all([ | |
72 | createDocWithoutSignatureHash(signedDocument), | |
73 | createSignatureHash(signedDocument.signature) | |
74 | ]) | |
75 | ||
76 | const toVerify = optionsHash + documentHash | |
77 | ||
78 | const verify = createVerify('RSA-SHA256') | |
79 | verify.update(toVerify, 'utf8') | |
80 | ||
81 | return verify.verify(fromActor.publicKey, signedDocument.signature.signatureValue, 'base64') | |
82 | } | |
83 | ||
84 | async function signJsonLDObject (byActor: MActor, data: any) { | |
85 | const signature = { | |
86 | type: 'RsaSignature2017', | |
87 | creator: byActor.url, | |
88 | created: new Date().toISOString() | |
89 | } | |
90 | ||
91 | const [ documentHash, optionsHash ] = await Promise.all([ | |
92 | createDocWithoutSignatureHash(data), | |
93 | createSignatureHash(signature) | |
94 | ]) | |
95 | ||
96 | const toSign = optionsHash + documentHash | |
97 | ||
98 | const sign = createSign('RSA-SHA256') | |
99 | sign.update(toSign, 'utf8') | |
100 | ||
101 | const signatureValue = sign.sign(byActor.privateKey, 'base64') | |
102 | Object.assign(signature, { signatureValue }) | |
103 | ||
104 | return Object.assign(data, { signature }) | |
105 | } | |
106 | ||
107 | // --------------------------------------------------------------------------- | |
108 | ||
109 | export { | |
110 | isHTTPSignatureDigestValid, | |
111 | parseHTTPSignature, | |
112 | isHTTPSignatureVerified, | |
113 | isJsonLDSignatureVerified, | |
114 | comparePassword, | |
115 | createPrivateAndPublicKeys, | |
116 | cryptPassword, | |
117 | signJsonLDObject | |
118 | } | |
119 | ||
120 | // --------------------------------------------------------------------------- | |
121 | ||
122 | function hash (obj: any): Promise<any> { | |
123 | return jsonld.promises | |
124 | .normalize(obj, { | |
125 | algorithm: 'URDNA2015', | |
126 | format: 'application/n-quads' | |
127 | }) | |
128 | .then(res => sha256(res)) | |
129 | } | |
130 | ||
131 | function createSignatureHash (signature: any) { | |
132 | const signatureCopy = cloneDeep(signature) | |
133 | Object.assign(signatureCopy, { | |
134 | '@context': [ | |
135 | 'https://w3id.org/security/v1', | |
136 | { RsaSignature2017: 'https://w3id.org/security#RsaSignature2017' } | |
137 | ] | |
138 | }) | |
139 | ||
140 | delete signatureCopy.type | |
141 | delete signatureCopy.id | |
142 | delete signatureCopy.signatureValue | |
143 | ||
144 | return hash(signatureCopy) | |
145 | } | |
146 | ||
147 | function createDocWithoutSignatureHash (doc: any) { | |
148 | const docWithoutSignature = cloneDeep(doc) | |
149 | delete docWithoutSignature.signature | |
150 | ||
151 | return hash(docWithoutSignature) | |
152 | } |