-rw-r--r-- | backend/node/src/clipperz.js | 17 |
1 files changed, 12 insertions, 5 deletions
diff --git a/backend/node/src/clipperz.js b/backend/node/src/clipperz.js index 37b9cae..72b6c9f 100644 --- a/backend/node/src/clipperz.js +++ b/backend/node/src/clipperz.js | |||
@@ -2,96 +2,98 @@ var FS = require('fs'); | |||
2 | var CRYPTO = require('crypto'); | 2 | var CRYPTO = require('crypto'); |
3 | var BIGNUM = require('bignum'); | 3 | var BIGNUM = require('bignum'); |
4 | var ASYNC = require('async'); | 4 | var ASYNC = require('async'); |
5 | 5 | ||
6 | var express_store = require('express-session').Store; | 6 | var express_store = require('express-session').Store; |
7 | 7 | ||
8 | function clipperz_hash(v) { | 8 | function clipperz_hash(v) { |
9 | return CRYPTO.createHash('sha256').update( | 9 | return CRYPTO.createHash('sha256').update( |
10 | CRYPTO.createHash('sha256').update(v).digest('binary') | 10 | CRYPTO.createHash('sha256').update(v).digest('binary') |
11 | ).digest('hex'); | 11 | ).digest('hex'); |
12 | }; | 12 | }; |
13 | function clipperz_random() { | 13 | function clipperz_random() { |
14 | for(var r = '';r.length<64;r+=''+BIGNUM(Math.floor(Math.random()*1e18)).toString(16)); | 14 | for(var r = '';r.length<64;r+=''+BIGNUM(Math.floor(Math.random()*1e18)).toString(16)); |
15 | return r.substr(0,64); | 15 | return r.substr(0,64); |
16 | }; | 16 | }; |
17 | function clipperz_store(PG) { | 17 | function clipperz_store(PG) { |
18 | var rv = function(o) { express_store.call(this,o); } | 18 | var rv = function(o) { express_store.call(this,o); } |
19 | rv.prototype.get = function(sid,cb) { PG.Q( | 19 | rv.prototype.get = function(sid,cb) { PG.Q( |
20 | "SELECT s_data FROM clipperz.thesession WHERE s_id=$1",[sid], | 20 | "SELECT s_data FROM clipperz.thesession WHERE s_id=$1",[sid], |
21 | function(e,r) { cb(e,(e||!r.rowCount)?null:JSON.parse(r.rows[0].s_data)); } | 21 | function(e,r) { cb(e,(e||!r.rowCount)?null:JSON.parse(r.rows[0].s_data)); } |
22 | ) }; | 22 | ) }; |
23 | rv.prototype.set = function(sid,data,cb) { | 23 | rv.prototype.set = function(sid,data,cb) { |
24 | var d = JSON.stringify(data); | 24 | var d = JSON.stringify(data); |
25 | PG.Q( | 25 | PG.Q( |
26 | "UPDATE clipperz.thesession SET s_data=$1, s_mtime=current_timestamp" | 26 | "UPDATE clipperz.thesession SET s_data=$1, s_mtime=current_timestamp" |
27 | +" WHERE s_id=$2",[d,sid], function(e,r) { | 27 | +" WHERE s_id=$2",[d,sid], function(e,r) { |
28 | if(e) return cb(e); | 28 | if(e) return cb(e); |
29 | if(r.rowCount) return cb(); | 29 | if(r.rowCount) return cb(); |
30 | PG.Q("INSERT INTO clipperz.thesession (s_id,s_data) VALUES ($1,$2)",[sid,d],cb); | 30 | PG.Q("INSERT INTO clipperz.thesession (s_id,s_data) VALUES ($1,$2)",[sid,d],cb); |
31 | }); | 31 | }); |
32 | }; | 32 | }; |
33 | rv.prototype.destroy = function(sid,cb) { PG.Q( | 33 | rv.prototype.destroy = function(sid,cb) { PG.Q( |
34 | "DELETE FROM clipperz.thesession WHERE s_id=$1",[sid],cb | 34 | "DELETE FROM clipperz.thesession WHERE s_id=$1",[sid],cb |
35 | ) }; | 35 | ) }; |
36 | rv.prototype.length = function(cb) { PG.Q( | 36 | rv.prototype.length = function(cb) { PG.Q( |
37 | "SELECT count(*) AS c FROM clipperz.thesession", function(e,r) { | 37 | "SELECT count(*) AS c FROM clipperz.thesession", function(e,r) { |
38 | cb(e,e?null:r.rows[0].c); | 38 | cb(e,e?null:r.rows[0].c); |
39 | } | 39 | } |
40 | ) }; | 40 | ) }; |
41 | rv.prototype.clear = function(cb) { PQ.Q( | 41 | rv.prototype.clear = function(cb) { PQ.Q( |
42 | "TRUNCATE clipperz.thesession", cb | 42 | "TRUNCATE clipperz.thesession", cb |
43 | ) }; | 43 | ) }; |
44 | rv.prototype.__proto__ = express_store.prototype; | 44 | rv.prototype.__proto__ = express_store.prototype; |
45 | return rv; | 45 | return rv; |
46 | } | 46 | } |
47 | 47 | ||
48 | var srp_g = BIGNUM(2); | 48 | var srp_g = BIGNUM(2); |
49 | var srp_n = BIGNUM("115b8b692e0e045692cf280b436735c77a5a9e8a9e7ed56c965f87db5b2a2ece3",16); | 49 | var srp_n = BIGNUM("115b8b692e0e045692cf280b436735c77a5a9e8a9e7ed56c965f87db5b2a2ece3",16); |
50 | var srp_k = BIGNUM("64398bff522814e306a97cb9bfc4364b7eed16a8c17c5208a40a2bad2933c8e",16); | ||
51 | var srp_hn = "597626870978286801440197562148588907434001483655788865609375806439877501869636875571920406529"; | ||
50 | var n123 = '112233445566778899aabbccddeeff00112233445566778899aabbccddeeff00'; | 52 | var n123 = '112233445566778899aabbccddeeff00112233445566778899aabbccddeeff00'; |
51 | 53 | ||
52 | 54 | ||
53 | var CLIPPERZ = module.exports = function(CONFIG) { | 55 | var CLIPPERZ = module.exports = function(CONFIG) { |
54 | 56 | ||
55 | var LOGGER = CONFIG.logger||{trace:function(){}}; | 57 | var LOGGER = CONFIG.logger||{trace:function(){}}; |
56 | 58 | ||
57 | var PG = { | 59 | var PG = { |
58 | url: CONFIG.psql, | 60 | url: CONFIG.psql, |
59 | PG: require('pg').native, | 61 | PG: require('pg').native, |
60 | Q: function(q,a,cb) { | 62 | Q: function(q,a,cb) { |
61 | if('function'===typeof a) cb=a,a=[]; | 63 | if('function'===typeof a) cb=a,a=[]; |
62 | LOGGER.trace({query:q,args:a},'SQL: %s',q); | 64 | LOGGER.trace({query:q,args:a},'SQL: %s',q); |
63 | PG.PG.connect(PG.url,function(e,C,D) { | 65 | PG.PG.connect(PG.url,function(e,C,D) { |
64 | if(e) return cb(e); | 66 | if(e) return cb(e); |
65 | var t0=new Date(); | 67 | var t0=new Date(); |
66 | C.query(q,a,function(e,r) { | 68 | C.query(q,a,function(e,r) { |
67 | var t1=new Date(), dt=t1-t0; | 69 | var t1=new Date(), dt=t1-t0; |
68 | D(); | 70 | D(); |
69 | LOGGER.trace({query:q,args:a,ms:dt,rows:r&&r.rowCount,err:e},"SQL query '%s' took %dms",q,dt); | 71 | LOGGER.trace({query:q,args:a,ms:dt,rows:r&&r.rowCount,err:e},"SQL query '%s' took %dms",q,dt); |
70 | cb(e,r); | 72 | cb(e,r); |
71 | }); | 73 | }); |
72 | }); | 74 | }); |
73 | }, | 75 | }, |
74 | T: function(cb) { | 76 | T: function(cb) { |
75 | PG.PG.connect(PG.url,function(e,C,D) { | 77 | PG.PG.connect(PG.url,function(e,C,D) { |
76 | if(e) return cb(e); | 78 | if(e) return cb(e); |
77 | C.query('BEGIN',function(e){ | 79 | C.query('BEGIN',function(e){ |
78 | if(e) return D(),cb(e); | 80 | if(e) return D(),cb(e); |
79 | LOGGER.trace('SQL: transaction begun'); | 81 | LOGGER.trace('SQL: transaction begun'); |
80 | cb(null,{ | 82 | cb(null,{ |
81 | Q: function(q,a,cb) { | 83 | Q: function(q,a,cb) { |
82 | LOGGER.trace({query:q,args:a},'SQL: %s',q); | 84 | LOGGER.trace({query:q,args:a},'SQL: %s',q); |
83 | if(this.over) return cb(new Error('game over')); | 85 | if(this.over) return cb(new Error('game over')); |
84 | if('function'===typeof a) cb=a,a=[]; | 86 | if('function'===typeof a) cb=a,a=[]; |
85 | var t0=new Date(); | 87 | var t0=new Date(); |
86 | C.query(q,a,function(e,r) { | 88 | C.query(q,a,function(e,r) { |
87 | var t1=new Date(), dt=t1-t0; | 89 | var t1=new Date(), dt=t1-t0; |
88 | LOGGER.trace({query:q,args:a,ms:dt,rows:r&&r.rowCount,err:e},"SQL query '%s' took %dms",q,dt); | 90 | LOGGER.trace({query:q,args:a,ms:dt,rows:r&&r.rowCount,err:e},"SQL query '%s' took %dms",q,dt); |
89 | cb(e,r); | 91 | cb(e,r); |
90 | }); | 92 | }); |
91 | }, | 93 | }, |
92 | commit: function(cb) { | 94 | commit: function(cb) { |
93 | LOGGER.trace('SQL: commit'); | 95 | LOGGER.trace('SQL: commit'); |
94 | if(this.over) return cb(new Error('game over')); | 96 | if(this.over) return cb(new Error('game over')); |
95 | return (this.over=true),C.query('COMMIT',function(e){D();cb&&cb(e)}); | 97 | return (this.over=true),C.query('COMMIT',function(e){D();cb&&cb(e)}); |
96 | }, | 98 | }, |
97 | rollback: function(cb) { | 99 | rollback: function(cb) { |
@@ -130,109 +132,114 @@ var CLIPPERZ = module.exports = function(CONFIG) { | |||
130 | pp.credentials.version,pp.user.header, pp.user.statistics, | 132 | pp.credentials.version,pp.user.header, pp.user.statistics, |
131 | pp.user.version, pp.user.lock], function(e,r) { | 133 | pp.user.version, pp.user.lock], function(e,r) { |
132 | if(e) return cb(e); | 134 | if(e) return cb(e); |
133 | res.res({lock:pp.user.lock,result:'done'}); | 135 | res.res({lock:pp.user.lock,result:'done'}); |
134 | }); | 136 | }); |
135 | } | 137 | } |
136 | break; | 138 | break; |
137 | 139 | ||
138 | case 'handshake': | 140 | case 'handshake': |
139 | switch(message) { | 141 | switch(message) { |
140 | case 'connect': return ASYNC.auto({ | 142 | case 'connect': return ASYNC.auto({ |
141 | u: function(cb) { PG.Q( | 143 | u: function(cb) { PG.Q( |
142 | "SELECT u_id, u_srp_s, u_srp_v FROM clipperz.theuser WHERE u_name=$1", | 144 | "SELECT u_id, u_srp_s, u_srp_v FROM clipperz.theuser WHERE u_name=$1", |
143 | [ppp.C], function(e,r) { | 145 | [ppp.C], function(e,r) { |
144 | if(e) return cb(e); | 146 | if(e) return cb(e); |
145 | if(!r.rowCount) return cb(null,{u_id:null,u_srp_s:n123,u_srp_v:n123}); | 147 | if(!r.rowCount) return cb(null,{u_id:null,u_srp_s:n123,u_srp_v:n123}); |
146 | cb(null,r.rows[0]); | 148 | cb(null,r.rows[0]); |
147 | }) }, | 149 | }) }, |
148 | otp: ['u',function(cb,r) { | 150 | otp: ['u',function(cb,r) { |
149 | if(!req.session.otp) return cb(); | 151 | if(!req.session.otp) return cb(); |
150 | if(req.session.u!=r.u.u_id) return cb(new Error('user/OTP mismatch')); | 152 | if(req.session.u!=r.u.u_id) return cb(new Error('user/OTP mismatch')); |
151 | PG.Q( | 153 | PG.Q( |
152 | "UPDATE clipperz.theotp AS otp" | 154 | "UPDATE clipperz.theotp AS otp" |
153 | +" SET" | 155 | +" SET" |
154 | +" otps_id=CASE WHEN s.otps_code='REQUESTED' THEN (" | 156 | +" otps_id=CASE WHEN s.otps_code='REQUESTED' THEN (" |
155 | +" SELECT ss.otps_id FROM clipperz.otpstatus AS ss WHERE ss.otps_code='USED'" | 157 | +" SELECT ss.otps_id FROM clipperz.otpstatus AS ss WHERE ss.otps_code='USED'" |
156 | +" ) ELSE otp.otps_id END," | 158 | +" ) ELSE otp.otps_id END," |
157 | +" otp_utime=current_timestamp" | 159 | +" otp_utime=current_timestamp" |
158 | +" FROM clipperz.otpstatus AS s, clipperz.theotp AS o" | 160 | +" FROM clipperz.otpstatus AS s, clipperz.theotp AS o" |
159 | +" WHERE" | 161 | +" WHERE" |
160 | +" o.otp_id=otp.otp_id AND otp.otps_id=s.otps_id" | 162 | +" o.otp_id=otp.otp_id AND otp.otps_id=s.otps_id" |
161 | +" AND otp.otp_id=$1 AND otp.u_id=$2" | 163 | +" AND otp.otp_id=$1 AND otp.u_id=$2" |
162 | +" RETURNING o.otps_id!=otp.otps_id AS yes, o.otp_ref", | 164 | +" RETURNING o.otps_id!=otp.otps_id AS yes, o.otp_ref", |
163 | [ req.session.otp, req.session.u ], | 165 | [ req.session.otp, req.session.u ], |
164 | function(e,r) { | 166 | function(e,r) { |
165 | if(e) return cb(e); | 167 | if(e) return cb(e); |
166 | if(!r.rowCount) return cb(new Error('no OTP found')); | 168 | if(!r.rowCount) return cb(new Error('no OTP found')); |
167 | r=r.rows[0]; | 169 | r=r.rows[0]; |
168 | if(!r.yes) return cb(new Error('OTP is in a sorry state')); | 170 | if(!r.yes) return cb(new Error('OTP is in a sorry state')); |
169 | cb(null,{ref:r.otp_ref}); | 171 | cb(null,{ref:r.otp_ref}); |
170 | }); | 172 | }); |
171 | }] | 173 | }] |
172 | },function(e,r) { | 174 | },function(e,r) { |
173 | if(e) return cb(e); | 175 | if(e) return cb(e); |
174 | req.session.C = ppp.C; req.session.A = ppp.A; | 176 | req.session.C = ppp.C; req.session.A = ppp.A; |
175 | req.session.s = r.u.u_srp_s; req.session.v = r.u.u_srp_v; | 177 | req.session.s = r.u.u_srp_s; req.session.v = r.u.u_srp_v; |
176 | req.session.u = r.u.u_id; | 178 | req.session.u = r.u.u_id; |
177 | req.session.b = clipperz_random(); | 179 | req.session.b = clipperz_random(); |
178 | req.session.B = BIGNUM(req.session.v,16).add(srp_g.powm(BIGNUM(req.session.b,16),srp_n)).toString(16); | 180 | req.session.B = srp_k.mul(BIGNUM(req.session.v,16)).add(srp_g.powm(BIGNUM(req.session.b,16),srp_n)).toString(16); |
179 | var rv = {s:req.session.s,B:req.session.B} | 181 | var rv = {s:req.session.s,B:req.session.B} |
180 | if(r.otp && r.otp.otp_ref) rv.oneTimePassword=r.otp.otp_ref; | 182 | if(r.otp && r.otp.otp_ref) rv.oneTimePassword=r.otp.otp_ref; |
181 | res.res(rv); | 183 | res.res(rv); |
182 | }); | 184 | }); |
183 | 185 | ||
184 | case 'credentialCheck': | 186 | case 'credentialCheck': |
185 | var u = clipperz_hash(BIGNUM(req.session.B,16).toString(10)); | 187 | var u = clipperz_hash(BIGNUM(req.session.A,16).toString(10)+BIGNUM(req.session.B,16).toString(10)); |
186 | var A = BIGNUM(req.session.A,16); | 188 | var A = BIGNUM(req.session.A,16); |
187 | var S = A.mul(BIGNUM(req.session.v,16).powm(BIGNUM(u,16),srp_n)).powm( | 189 | var S = A.mul(BIGNUM(req.session.v,16).powm(BIGNUM(u,16),srp_n)).powm(BIGNUM(req.session.b,16),srp_n); |
188 | BIGNUM(req.session.b,16), srp_n); | ||
189 | var K = clipperz_hash(S.toString(10)); | 190 | var K = clipperz_hash(S.toString(10)); |
190 | var M1 = clipperz_hash(A.toString(10)+BIGNUM(req.session.B,16).toString(10)+K.toString(16)); | 191 | var M1 = clipperz_hash( |
192 | srp_hn | ||
193 | +clipperz_hash(req.session.C) | ||
194 | +BIGNUM(req.session.s,16).toString(10) | ||
195 | +A.toString(10) | ||
196 | +BIGNUM(req.session.B,16).toString(10) | ||
197 | +K ); | ||
191 | if(M1!=ppp.M1) return res.res({error:'?'}); | 198 | if(M1!=ppp.M1) return res.res({error:'?'}); |
192 | req.session.K = K; | 199 | req.session.K = K; |
193 | var M2 = clipperz_hash(A.toString(10)+M1+K.toString(16)); | 200 | var M2 = clipperz_hash(A.toString(10)+M1+K.toString(16)); |
194 | return res.res({M2:M2,connectionId:'',loginInfo:{latest:{},current:{}},offlineCopyNeeded:false,lock:'----'}); | 201 | return res.res({M2:M2,connectionId:'',loginInfo:{latest:{},current:{}},offlineCopyNeeded:false,lock:'----'}); |
195 | 202 | ||
196 | case 'oneTimePassword': return PG.Q( | 203 | case 'oneTimePassword': return PG.Q( |
197 | "UPDATE clipperz.theotp AS otp" | 204 | "UPDATE clipperz.theotp AS otp" |
198 | +" SET" | 205 | +" SET" |
199 | +" otps_id = CASE WHEN s.otps_code!='ACTIVE' THEN s.otps_id ELSE (" | 206 | +" otps_id = CASE WHEN s.otps_code!='ACTIVE' THEN s.otps_id ELSE (" |
200 | +" SELECT ss.otps_id FROM clipperz.otpstatus AS ss WHERE ss.otps_code=CASE" | 207 | +" SELECT ss.otps_id FROM clipperz.otpstatus AS ss WHERE ss.otps_code=CASE" |
201 | +" WHEN otp.otp_key_checksum=$2 THEN 'REQUESTED'" | 208 | +" WHEN otp.otp_key_checksum=$2 THEN 'REQUESTED'" |
202 | +" ELSE 'DISABLED' END" | 209 | +" ELSE 'DISABLED' END" |
203 | +" ) END," | 210 | +" ) END," |
204 | +" otp_data = CASE WHEN s.otps_code='ACTIVE' THEN '' ELSE otp.otp_data END," | 211 | +" otp_data = CASE WHEN s.otps_code='ACTIVE' THEN '' ELSE otp.otp_data END," |
205 | +" otp_utime = current_timestamp," | 212 | +" otp_utime = current_timestamp," |
206 | +" otp_rtime = CASE WHEN otp.otp_key_checksum=$2 THEN current_timestamp ELSE otp.otp_rtime END" | 213 | +" otp_rtime = CASE WHEN otp.otp_key_checksum=$2 THEN current_timestamp ELSE otp.otp_rtime END" |
207 | +" FROM clipperz.otpstatus AS s, clipperz.theotp AS o" | 214 | +" FROM clipperz.otpstatus AS s, clipperz.theotp AS o" |
208 | +" WHERE" | 215 | +" WHERE" |
209 | +" o.otp_id=otp.otp_id AND otp.otps_id=s.otps_id AND otp.otp_key=$1" | 216 | +" o.otp_id=otp.otp_id AND otp.otps_id=s.otps_id AND otp.otp_key=$1" |
210 | +" RETURNING otp.u_id, s.otps_code, otp.otp_id, otp.otp_key_checksum, o.otp_data, otp.otp_version", | 217 | +" RETURNING otp.u_id, s.otps_code, otp.otp_id, otp.otp_key_checksum, o.otp_data, otp.otp_version", |
211 | [ ppp.oneTimePasswordKey, ppp.oneTimePasswordKeyChecksum ], | 218 | [ ppp.oneTimePasswordKey, ppp.oneTimePasswordKeyChecksum ], |
212 | function(e,r) { | 219 | function(e,r) { |
213 | if(e) return cb(e); | 220 | if(e) return cb(e); |
214 | if(!r.rowCount) return cb(new Error('OTP not found')); | 221 | if(!r.rowCount) return cb(new Error('OTP not found')); |
215 | r=r.rows[0]; | 222 | r=r.rows[0]; |
216 | if(r.otp_key_checksum!=ppp.oneTimePasswordKeyChecksum) | 223 | if(r.otp_key_checksum!=ppp.oneTimePasswordKeyChecksum) |
217 | return cb(new Error('OTP was disabled because of checksum mismatch')); | 224 | return cb(new Error('OTP was disabled because of checksum mismatch')); |
218 | if(r.otps_code!='ACTIVE') | 225 | if(r.otps_code!='ACTIVE') |
219 | return cb(new Error("OTP wasn't active, sorry")); | 226 | return cb(new Error("OTP wasn't active, sorry")); |
220 | req.session.u=r.u_id; req.session.otp=r.otp_id; | 227 | req.session.u=r.u_id; req.session.otp=r.otp_id; |
221 | res.res({data:r.otp_data,version:r.otp_version}); | 228 | res.res({data:r.otp_data,version:r.otp_version}); |
222 | }); | 229 | }); |
223 | } | 230 | } |
224 | break; | 231 | break; |
225 | 232 | ||
226 | case 'message': | 233 | case 'message': |
227 | if(!req.session.K) return res.res({result:'EXCEPTION',message:"effectively, we're missing a aconnection"}); | 234 | if(!req.session.K) return res.res({result:'EXCEPTION',message:"effectively, we're missing a aconnection"}); |
228 | if(req.session.K!=pp.srpSharedSecret) return res.res({error:'Wrong shared secret!'}); | 235 | if(req.session.K!=pp.srpSharedSecret) return res.res({error:'Wrong shared secret!'}); |
229 | switch(message) { | 236 | switch(message) { |
230 | case 'getUserDetails': return ASYNC.parallel({ | 237 | case 'getUserDetails': return ASYNC.parallel({ |
231 | u: function(cb) { | 238 | u: function(cb) { |
232 | PG.Q("SELECT u_header,u_statistics,u_version FROM clipperz.theuser WHERE u_id=$1", | 239 | PG.Q("SELECT u_header,u_statistics,u_version FROM clipperz.theuser WHERE u_id=$1", |
233 | [req.session.u],function(e,r) { | 240 | [req.session.u],function(e,r) { |
234 | if(e) return cb(e); | 241 | if(e) return cb(e); |
235 | if(!r.rowCount) return cb(new Error("user's gone AWOL")); | 242 | if(!r.rowCount) return cb(new Error("user's gone AWOL")); |
236 | cb(null,r.rows[0]); | 243 | cb(null,r.rows[0]); |
237 | }); | 244 | }); |
238 | }, | 245 | }, |