|
| 1 | +const crypto = require('crypto') |
| 2 | + |
| 3 | +function startSession (mechanisms) { |
| 4 | + if (mechanisms.indexOf('SCRAM-SHA-256') === -1) { |
| 5 | + throw new Error('SASL: Only mechanism SCRAM-SHA-256 is currently supported') |
| 6 | + } |
| 7 | + |
| 8 | + const clientNonce = crypto.randomBytes(18).toString('base64') |
| 9 | + |
| 10 | + return { |
| 11 | + mechanism: 'SCRAM-SHA-256', |
| 12 | + clientNonce, |
| 13 | + response: 'n,,n=*,r=' + clientNonce, |
| 14 | + message: 'SASLInitialResponse' |
| 15 | + } |
| 16 | +} |
| 17 | + |
| 18 | +function continueSession (session, password, serverData) { |
| 19 | + if (session.message !== 'SASLInitialResponse') { |
| 20 | + throw new Error('SASL: Last message was not SASLInitialResponse') |
| 21 | + } |
| 22 | + |
| 23 | + const sv = extractVariablesFromFirstServerMessage(serverData) |
| 24 | + |
| 25 | + if (!sv.nonce.startsWith(session.clientNonce)) { |
| 26 | + throw new Error('SASL: SCRAM-SERVER-FIRST-MESSAGE: server nonce does not start with client nonce') |
| 27 | + } |
| 28 | + |
| 29 | + var saltBytes = Buffer.from(sv.salt, 'base64') |
| 30 | + |
| 31 | + var saltedPassword = Hi(password, saltBytes, sv.iteration) |
| 32 | + |
| 33 | + var clientKey = createHMAC(saltedPassword, 'Client Key') |
| 34 | + var storedKey = crypto.createHash('sha256').update(clientKey).digest() |
| 35 | + |
| 36 | + var clientFirstMessageBare = 'n=*,r=' + session.clientNonce |
| 37 | + var serverFirstMessage = 'r=' + sv.nonce + ',s=' + sv.salt + ',i=' + sv.iteration |
| 38 | + |
| 39 | + var clientFinalMessageWithoutProof = 'c=biws,r=' + sv.nonce |
| 40 | + |
| 41 | + var authMessage = clientFirstMessageBare + ',' + serverFirstMessage + ',' + clientFinalMessageWithoutProof |
| 42 | + |
| 43 | + var clientSignature = createHMAC(storedKey, authMessage) |
| 44 | + var clientProofBytes = xorBuffers(clientKey, clientSignature) |
| 45 | + var clientProof = clientProofBytes.toString('base64') |
| 46 | + |
| 47 | + var serverKey = createHMAC(saltedPassword, 'Server Key') |
| 48 | + var serverSignatureBytes = createHMAC(serverKey, authMessage) |
| 49 | + |
| 50 | + session.message = 'SASLResponse' |
| 51 | + session.serverSignature = serverSignatureBytes.toString('base64') |
| 52 | + session.response = clientFinalMessageWithoutProof + ',p=' + clientProof |
| 53 | +} |
| 54 | + |
| 55 | +function finalizeSession (session, serverData) { |
| 56 | + if (session.message !== 'SASLResponse') { |
| 57 | + throw new Error('SASL: Last message was not SASLResponse') |
| 58 | + } |
| 59 | + |
| 60 | + var serverSignature |
| 61 | + |
| 62 | + String(serverData).split(',').forEach(function (part) { |
| 63 | + switch (part[0]) { |
| 64 | + case 'v': |
| 65 | + serverSignature = part.substr(2) |
| 66 | + break |
| 67 | + } |
| 68 | + }) |
| 69 | + |
| 70 | + if (serverSignature !== session.serverSignature) { |
| 71 | + throw new Error('SASL: SCRAM-SERVER-FINAL-MESSAGE: server signature does not match') |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +function extractVariablesFromFirstServerMessage (data) { |
| 76 | + var nonce, salt, iteration |
| 77 | + |
| 78 | + String(data).split(',').forEach(function (part) { |
| 79 | + switch (part[0]) { |
| 80 | + case 'r': |
| 81 | + nonce = part.substr(2) |
| 82 | + break |
| 83 | + case 's': |
| 84 | + salt = part.substr(2) |
| 85 | + break |
| 86 | + case 'i': |
| 87 | + iteration = parseInt(part.substr(2), 10) |
| 88 | + break |
| 89 | + } |
| 90 | + }) |
| 91 | + |
| 92 | + if (!nonce) { |
| 93 | + throw new Error('SASL: SCRAM-SERVER-FIRST-MESSAGE: nonce missing') |
| 94 | + } |
| 95 | + |
| 96 | + if (!salt) { |
| 97 | + throw new Error('SASL: SCRAM-SERVER-FIRST-MESSAGE: salt missing') |
| 98 | + } |
| 99 | + |
| 100 | + if (!iteration) { |
| 101 | + throw new Error('SASL: SCRAM-SERVER-FIRST-MESSAGE: iteration missing') |
| 102 | + } |
| 103 | + |
| 104 | + return { |
| 105 | + nonce, |
| 106 | + salt, |
| 107 | + iteration |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +function xorBuffers (a, b) { |
| 112 | + if (!Buffer.isBuffer(a)) a = Buffer.from(a) |
| 113 | + if (!Buffer.isBuffer(b)) b = Buffer.from(b) |
| 114 | + var res = [] |
| 115 | + if (a.length > b.length) { |
| 116 | + for (var i = 0; i < b.length; i++) { |
| 117 | + res.push(a[i] ^ b[i]) |
| 118 | + } |
| 119 | + } else { |
| 120 | + for (var j = 0; j < a.length; j++) { |
| 121 | + res.push(a[j] ^ b[j]) |
| 122 | + } |
| 123 | + } |
| 124 | + return Buffer.from(res) |
| 125 | +} |
| 126 | + |
| 127 | +function createHMAC (key, msg) { |
| 128 | + return crypto.createHmac('sha256', key).update(msg).digest() |
| 129 | +} |
| 130 | + |
| 131 | +function Hi (password, saltBytes, iterations) { |
| 132 | + var ui1 = createHMAC(password, Buffer.concat([saltBytes, Buffer.from([0, 0, 0, 1])])) |
| 133 | + var ui = ui1 |
| 134 | + for (var i = 0; i < iterations - 1; i++) { |
| 135 | + ui1 = createHMAC(password, ui1) |
| 136 | + ui = xorBuffers(ui, ui1) |
| 137 | + } |
| 138 | + |
| 139 | + return ui |
| 140 | +} |
| 141 | + |
| 142 | +module.exports = { |
| 143 | + startSession, |
| 144 | + continueSession, |
| 145 | + finalizeSession |
| 146 | +} |
0 commit comments