sasl.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. 'use strict'
  2. const crypto = require('./utils')
  3. const { signatureAlgorithmHashFromCertificate } = require('./cert-signatures')
  4. function startSession(mechanisms, stream) {
  5. const candidates = ['SCRAM-SHA-256']
  6. if (stream) candidates.unshift('SCRAM-SHA-256-PLUS') // higher-priority, so placed first
  7. const mechanism = candidates.find((candidate) => mechanisms.includes(candidate))
  8. if (!mechanism) {
  9. throw new Error('SASL: Only mechanism(s) ' + candidates.join(' and ') + ' are supported')
  10. }
  11. if (mechanism === 'SCRAM-SHA-256-PLUS' && typeof stream.getPeerCertificate !== 'function') {
  12. // this should never happen if we are really talking to a Postgres server
  13. throw new Error('SASL: Mechanism SCRAM-SHA-256-PLUS requires a certificate')
  14. }
  15. const clientNonce = crypto.randomBytes(18).toString('base64')
  16. const gs2Header = mechanism === 'SCRAM-SHA-256-PLUS' ? 'p=tls-server-end-point' : stream ? 'y' : 'n'
  17. return {
  18. mechanism,
  19. clientNonce,
  20. response: gs2Header + ',,n=*,r=' + clientNonce,
  21. message: 'SASLInitialResponse',
  22. }
  23. }
  24. async function continueSession(session, password, serverData, stream) {
  25. if (session.message !== 'SASLInitialResponse') {
  26. throw new Error('SASL: Last message was not SASLInitialResponse')
  27. }
  28. if (typeof password !== 'string') {
  29. throw new Error('SASL: SCRAM-SERVER-FIRST-MESSAGE: client password must be a string')
  30. }
  31. if (password === '') {
  32. throw new Error('SASL: SCRAM-SERVER-FIRST-MESSAGE: client password must be a non-empty string')
  33. }
  34. if (typeof serverData !== 'string') {
  35. throw new Error('SASL: SCRAM-SERVER-FIRST-MESSAGE: serverData must be a string')
  36. }
  37. const sv = parseServerFirstMessage(serverData)
  38. if (!sv.nonce.startsWith(session.clientNonce)) {
  39. throw new Error('SASL: SCRAM-SERVER-FIRST-MESSAGE: server nonce does not start with client nonce')
  40. } else if (sv.nonce.length === session.clientNonce.length) {
  41. throw new Error('SASL: SCRAM-SERVER-FIRST-MESSAGE: server nonce is too short')
  42. }
  43. const clientFirstMessageBare = 'n=*,r=' + session.clientNonce
  44. const serverFirstMessage = 'r=' + sv.nonce + ',s=' + sv.salt + ',i=' + sv.iteration
  45. // without channel binding:
  46. let channelBinding = stream ? 'eSws' : 'biws' // 'y,,' or 'n,,', base64-encoded
  47. // override if channel binding is in use:
  48. if (session.mechanism === 'SCRAM-SHA-256-PLUS') {
  49. const peerCert = stream.getPeerCertificate().raw
  50. let hashName = signatureAlgorithmHashFromCertificate(peerCert)
  51. if (hashName === 'MD5' || hashName === 'SHA-1') hashName = 'SHA-256'
  52. const certHash = await crypto.hashByName(hashName, peerCert)
  53. const bindingData = Buffer.concat([Buffer.from('p=tls-server-end-point,,'), Buffer.from(certHash)])
  54. channelBinding = bindingData.toString('base64')
  55. }
  56. const clientFinalMessageWithoutProof = 'c=' + channelBinding + ',r=' + sv.nonce
  57. const authMessage = clientFirstMessageBare + ',' + serverFirstMessage + ',' + clientFinalMessageWithoutProof
  58. const saltBytes = Buffer.from(sv.salt, 'base64')
  59. const saltedPassword = await crypto.deriveKey(password, saltBytes, sv.iteration)
  60. const clientKey = await crypto.hmacSha256(saltedPassword, 'Client Key')
  61. const storedKey = await crypto.sha256(clientKey)
  62. const clientSignature = await crypto.hmacSha256(storedKey, authMessage)
  63. const clientProof = xorBuffers(Buffer.from(clientKey), Buffer.from(clientSignature)).toString('base64')
  64. const serverKey = await crypto.hmacSha256(saltedPassword, 'Server Key')
  65. const serverSignatureBytes = await crypto.hmacSha256(serverKey, authMessage)
  66. session.message = 'SASLResponse'
  67. session.serverSignature = Buffer.from(serverSignatureBytes).toString('base64')
  68. session.response = clientFinalMessageWithoutProof + ',p=' + clientProof
  69. }
  70. function finalizeSession(session, serverData) {
  71. if (session.message !== 'SASLResponse') {
  72. throw new Error('SASL: Last message was not SASLResponse')
  73. }
  74. if (typeof serverData !== 'string') {
  75. throw new Error('SASL: SCRAM-SERVER-FINAL-MESSAGE: serverData must be a string')
  76. }
  77. const { serverSignature } = parseServerFinalMessage(serverData)
  78. if (serverSignature !== session.serverSignature) {
  79. throw new Error('SASL: SCRAM-SERVER-FINAL-MESSAGE: server signature does not match')
  80. }
  81. }
  82. /**
  83. * printable = %x21-2B / %x2D-7E
  84. * ;; Printable ASCII except ",".
  85. * ;; Note that any "printable" is also
  86. * ;; a valid "value".
  87. */
  88. function isPrintableChars(text) {
  89. if (typeof text !== 'string') {
  90. throw new TypeError('SASL: text must be a string')
  91. }
  92. return text
  93. .split('')
  94. .map((_, i) => text.charCodeAt(i))
  95. .every((c) => (c >= 0x21 && c <= 0x2b) || (c >= 0x2d && c <= 0x7e))
  96. }
  97. /**
  98. * base64-char = ALPHA / DIGIT / "/" / "+"
  99. *
  100. * base64-4 = 4base64-char
  101. *
  102. * base64-3 = 3base64-char "="
  103. *
  104. * base64-2 = 2base64-char "=="
  105. *
  106. * base64 = *base64-4 [base64-3 / base64-2]
  107. */
  108. function isBase64(text) {
  109. return /^(?:[a-zA-Z0-9+/]{4})*(?:[a-zA-Z0-9+/]{2}==|[a-zA-Z0-9+/]{3}=)?$/.test(text)
  110. }
  111. function parseAttributePairs(text) {
  112. if (typeof text !== 'string') {
  113. throw new TypeError('SASL: attribute pairs text must be a string')
  114. }
  115. return new Map(
  116. text.split(',').map((attrValue) => {
  117. if (!/^.=/.test(attrValue)) {
  118. throw new Error('SASL: Invalid attribute pair entry')
  119. }
  120. const name = attrValue[0]
  121. const value = attrValue.substring(2)
  122. return [name, value]
  123. })
  124. )
  125. }
  126. function parseServerFirstMessage(data) {
  127. const attrPairs = parseAttributePairs(data)
  128. const nonce = attrPairs.get('r')
  129. if (!nonce) {
  130. throw new Error('SASL: SCRAM-SERVER-FIRST-MESSAGE: nonce missing')
  131. } else if (!isPrintableChars(nonce)) {
  132. throw new Error('SASL: SCRAM-SERVER-FIRST-MESSAGE: nonce must only contain printable characters')
  133. }
  134. const salt = attrPairs.get('s')
  135. if (!salt) {
  136. throw new Error('SASL: SCRAM-SERVER-FIRST-MESSAGE: salt missing')
  137. } else if (!isBase64(salt)) {
  138. throw new Error('SASL: SCRAM-SERVER-FIRST-MESSAGE: salt must be base64')
  139. }
  140. const iterationText = attrPairs.get('i')
  141. if (!iterationText) {
  142. throw new Error('SASL: SCRAM-SERVER-FIRST-MESSAGE: iteration missing')
  143. } else if (!/^[1-9][0-9]*$/.test(iterationText)) {
  144. throw new Error('SASL: SCRAM-SERVER-FIRST-MESSAGE: invalid iteration count')
  145. }
  146. const iteration = parseInt(iterationText, 10)
  147. return {
  148. nonce,
  149. salt,
  150. iteration,
  151. }
  152. }
  153. function parseServerFinalMessage(serverData) {
  154. const attrPairs = parseAttributePairs(serverData)
  155. const serverSignature = attrPairs.get('v')
  156. if (!serverSignature) {
  157. throw new Error('SASL: SCRAM-SERVER-FINAL-MESSAGE: server signature is missing')
  158. } else if (!isBase64(serverSignature)) {
  159. throw new Error('SASL: SCRAM-SERVER-FINAL-MESSAGE: server signature must be base64')
  160. }
  161. return {
  162. serverSignature,
  163. }
  164. }
  165. function xorBuffers(a, b) {
  166. if (!Buffer.isBuffer(a)) {
  167. throw new TypeError('first argument must be a Buffer')
  168. }
  169. if (!Buffer.isBuffer(b)) {
  170. throw new TypeError('second argument must be a Buffer')
  171. }
  172. if (a.length !== b.length) {
  173. throw new Error('Buffer lengths must match')
  174. }
  175. if (a.length === 0) {
  176. throw new Error('Buffers cannot be empty')
  177. }
  178. return Buffer.from(a.map((_, i) => a[i] ^ b[i]))
  179. }
  180. module.exports = {
  181. startSession,
  182. continueSession,
  183. finalizeSession,
  184. }