forked from kirm/sip.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
digest.js
260 lines (199 loc) · 6.83 KB
/
digest.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
var crypto = require('crypto');
var util = require('util');
function unq(a) {
if(a && a[0] === '"' && a[a.length-1] === '"')
return a.substr(1, a.length - 2);
return a;
}
function q(a) {
if(a && a[0] !== '"')
return ['"', a, '"'].join('');
return a;
}
function lowercase(a) {
if(typeof a === 'string')
return a.toLowerCase();
return a;
}
function kd() {
var hash = crypto.createHash('md5');
var a = Array.prototype.join.call(arguments, ':');
hash.update(a);
return hash.digest('hex');
}
exports.kd = kd;
function rbytes() {
return kd(Math.random().toString(), Math.random().toString());
}
function calculateUserRealmPasswordHash(user, realm, password) {
return kd(unq(user), unq(realm), unq(password));
}
exports.calculateUserRealmPasswordHash = calculateUserRealmPasswordHash;
function calculateHA1(ctx) {
var userhash = ctx.userhash || calculateUserRealmPasswordHash(ctx.user, ctx.realm, ctx.password);
if(ctx.algorithm === 'md5-sess') return kd(userhash, ctx.nonce, ctx.cnonce);
return userhash;
}
exports.calculateHA1 = calculateHA1;
function calculateDigest(ctx) {
switch(ctx.qop) {
case 'auth-int':
return kd(ctx.ha1, ctx.nonce, ctx.nc, ctx.cnonce, ctx.qop, kd(ctx.method, ctx.uri, kd(ctx.entity)));
case 'auth':
return kd(ctx.ha1, ctx.nonce, ctx.nc, ctx.cnonce, ctx.qop, kd(ctx.method, ctx.uri));
}
return kd(ctx.ha1, ctx.nonce, kd(ctx.method, ctx.uri));
}
exports.calculateDigest = calculateDigest;
var nonceSalt = rbytes();
function generateNonce(tag, timestamp) {
var ts = (timestamp || new Date()).toISOString();
return new Buffer([ts, kd(ts, tag, nonceSalt)].join(';'), 'ascii').toString('base64');
}
exports.generateNonce = generateNonce;
function extractNonceTimestamp(nonce, tag) {
var v = new Buffer(nonce, 'base64').toString('ascii').split(';');
if(v.length != 2)
return;
var ts = new Date(v[0]);
return generateNonce(tag, ts) === nonce && ts;
}
exports.extractNonceTimestamp = extractNonceTimestamp;
function numberTo8Hex(n) {
n = n.toString(16);
return '00000000'.substr(n.length) + n;
}
function findDigestRealm(headers, realm) {
if(!realm) return headers && headers[0];
return headers && headers.filter(function(x) { return x.scheme === 'Digest' && unq(x.realm) === realm; })[0];
}
function selectQop(challenge, preference) {
if(!challenge)
return;
challenge = unq(challenge).split(',');
if(!preference)
return challenge[0];
if(typeof(preference) === 'string')
preference = preference.split(',');
for(var i = 0; i !== preference.length; ++i)
for(var j = 0; j !== challenge.length; ++j)
if(challenge[j] === preference[i])
return challenge[j];
throw new Error('failed to negotiate protection quality');
}
exports.challenge = function(ctx, rs) {
ctx.proxy = rs.status === 407;
ctx.nonce = ctx.cnonce || rbytes();
ctx.nc = 0;
ctx.qop = ctx.qop || 'auth,auth-int';
ctx.algorithm = ctx.algorithm || 'md5';
var hname = ctx.proxy ? 'proxy-authenticate' : 'www-authenticate';
(rs.headers[hname] || (rs.headers[hname]=[])).push(
{
scheme: 'Digest',
realm: q(ctx.realm),
qop: q(ctx.qop),
algorithm: q(ctx.algoritm),
nonce: q(ctx.nonce),
opaque: q(ctx.opaque)
}
);
return rs;
}
exports.authenticateRequest = function(ctx, rq, creds) {
var response = findDigestRealm(rq.headers[ctx.proxy ? 'proxy-authorization': 'authorization'], ctx.realm);
if(!response) return false;
var cnonce = unq(response.cnonce);
var uri = unq(response.uri);
var qop = unq(lowercase(response.qop));
ctx.nc = (ctx.nc || 0) +1;
if(!ctx.ha1) {
ctx.userhash = creds.hash || calculateUserRealmPasswordHash(creds.user, ctx.realm, creds.password);
ctx.ha1 = ctx.userhash;
if(ctx.algoritm === 'md5-sess')
ctx.ha1 = kd(ctx.userhash, ctx.nonce, cnonce);
}
var digest = calculateDigest({ha1:ctx.ha1, method:rq.method, nonce:ctx.nonce, nc:numberTo8Hex(ctx.nc), cnonce:cnonce, qop:qop, uri:uri, entity:rq.content});
if(digest === unq(response.response)) {
ctx.cnonce = cnonce;
ctx.uri = uri;
ctx.qop = qop;
return true;
}
return false;
}
exports.signResponse = function(ctx, rs) {
var nc = numberTo8Hex(ctx.nc);
rs.headers['authentication-info'] = {
qop: ctx.qop,
cnonce: q(ctx.cnonce),
nc: nc,
rspauth: q(calculateDigest({ha1:ctx.ha1, method:'', nonce:ctx.nonce, nc:nc, cnonce:ctx.cnonce, qop:ctx.qop, uri:ctx.uri, entity:rs.content}))
};
return rs;
}
function initClientContext(ctx, rs, creds) {
var challenge;
if(rs.status === 407) {
ctx.proxy = true;
challenge = findDigestRealm(rs.headers['proxy-authenticate'], creds.realm);
}
else
challenge = findDigestRealm(rs.headers['www-authenticate'], creds.realm);
if(ctx.nonce !== unq(challenge.nonce)) {
ctx.nonce = unq(challenge.nonce);
ctx.algorithm = unq(lowercase(challenge.algorithm));
ctx.qop = selectQop(lowercase(challenge.qop), ctx.qop);
if(ctx.qop) {
ctx.nc = 0;
ctx.cnonce = rbytes();
}
ctx.realm = unq(challenge.realm);
ctx.user = creds.user;
ctx.userhash = creds.hash || calculateUserRealmPasswordHash(creds.user, ctx.realm, creds.password);
ctx.ha1 = ctx.userhash;
if(ctx.algorithm === 'md5-sess')
ctx.ha1 = kd(ctx.ha1, ctx.nonce, ctx.cnonce);
ctx.domain = unq(challenge.domain);
}
ctx.opaque = unq(challenge.opaque);
}
exports.signRequest = function (ctx, rq, rs, creds) {
if(rs)
initClientContext(ctx, rs, creds);
var nc = ctx.nc !== undefined ? numberTo8Hex(++ctx.nc) : undefined;
ctx.uri = rq.uri;
var signature = {
scheme: 'Digest',
realm: q(ctx.realm),
username: q(ctx.user),
nonce: q(ctx.nonce),
uri: q(rq.uri),
nc: nc,
algorithm: q(ctx.algorithm),
cnonce: q(ctx.cnonce),
qop: ctx.qop,
opaque: q(ctx.opaque),
response: q(calculateDigest({ha1:ctx.ha1, method:rq.method, nonce:ctx.nonce, nc:nc, cnonce:ctx.cnonce, qop:ctx.qop, uri:ctx.uri, entity:rq.content}))
};
var hname = ctx.proxy ? 'proxy-authorization' : 'authorization';
rq.headers[hname] = (rq.headers[hname] || []).filter(function(x) { return unq(x.realm) !== ctx.realm; });
rq.headers[hname].push(signature);
return rq;
}
exports.authenticateResponse = function(ctx, rs) {
var signature = rs.headers[ctx.proxy ? 'proxy-authentication-info' : 'authentication-info'];
if(!signature) return undefined;
var digest=calculateDigest({ha1:ctx.ha1, method:'', nonce:ctx.nonce, nc:numberTo8Hex(ctx.nc), cnonce:ctx.cnonce, qop:ctx.qop, uri:ctx.uri, enity:rs.content});
if(digest === unq(signature.rspauth)) {
var nextnonce = unq(signature.nextnonce);
if(nextnonce && nextnonce !== ctx.nonce) {
ctx.nonce = nextnonce;
ctx.nc = 0;
if(ctx.algorithm === 'md5-sess')
ctx.ha1 = kd(ctx.userhash, ctx.nonce, ctx.cnonce);
}
return true;
}
return false;
}