summaryrefslogtreecommitdiff
path: root/pjnath/src/pjnath/stun_auth.c
blob: 4e2701f6d80e9e4ac8a28f187fc43419313cc0fe (plain)
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
/* $Id$ */
/* 
 * Copyright (C) 2003-2005 Benny Prijono <benny@prijono.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA 
 */
#include <pjnath/stun_auth.h>
#include <pjnath/errno.h>
#include <pjlib-util/hmac_sha1.h>
#include <pjlib-util/sha1.h>
#include <pj/assert.h>
#include <pj/log.h>
#include <pj/string.h>

#define THIS_FILE   "stun_auth.c"

/* Duplicate credential */
PJ_DEF(void) pj_stun_auth_cred_dup( pj_pool_t *pool,
				      pj_stun_auth_cred *dst,
				      const pj_stun_auth_cred *src)
{
    dst->type = src->type;

    switch (src->type) {
    case PJ_STUN_AUTH_CRED_STATIC:
	pj_strdup(pool, &dst->data.static_cred.realm,
			&src->data.static_cred.realm);
	pj_strdup(pool, &dst->data.static_cred.username,
			&src->data.static_cred.username);
	dst->data.static_cred.data_type = src->data.static_cred.data_type;
	pj_strdup(pool, &dst->data.static_cred.data,
			&src->data.static_cred.data);
	pj_strdup(pool, &dst->data.static_cred.nonce,
			&src->data.static_cred.nonce);
	break;
    case PJ_STUN_AUTH_CRED_DYNAMIC:
	pj_memcpy(&dst->data.dyn_cred, &src->data.dyn_cred, 
		  sizeof(src->data.dyn_cred));
	break;
    }
}


PJ_INLINE(pj_uint16_t) GET_VAL16(const pj_uint8_t *pdu, unsigned pos)
{
    return (pj_uint16_t) ((pdu[pos] << 8) + pdu[pos+1]);
}


/* Send 401 response */
static pj_status_t create_challenge(pj_pool_t *pool,
				    const pj_stun_msg *msg,
				    int err_code,
				    const pj_str_t *err_msg,
				    const pj_str_t *realm,
				    const pj_str_t *nonce,
				    pj_stun_msg **p_response)
{
    pj_stun_msg *response;
    pj_str_t tmp_nonce;
    pj_status_t rc;

    rc = pj_stun_msg_create_response(pool, msg, 
				     err_code,  err_msg, &response);
    if (rc != PJ_SUCCESS)
	return rc;


    if (realm && realm->slen) {
	rc = pj_stun_msg_add_string_attr(pool, response,
					 PJ_STUN_ATTR_REALM, 
					 realm);
	if (rc != PJ_SUCCESS)
	    return rc;

	/* long term must include nonce */
	if (!nonce || nonce->slen == 0) {
	    tmp_nonce = pj_str("pjstun");
	    nonce = &tmp_nonce;
	}
    }

    if (nonce && nonce->slen) {
	rc = pj_stun_msg_add_string_attr(pool, response,
					 PJ_STUN_ATTR_NONCE, 
					 nonce);
	if (rc != PJ_SUCCESS)
	    return rc;
    }

    *p_response = response;

    return PJ_SUCCESS;
}


/* Verify credential in the request */
PJ_DEF(pj_status_t) pj_stun_authenticate_request(const pj_uint8_t *pkt,
					         unsigned pkt_len,
					         const pj_stun_msg *msg,
					         pj_stun_auth_cred *cred,
					         pj_pool_t *pool,
					         pj_stun_msg **p_response)
{
    pj_str_t realm, nonce, password;
    const pj_stun_msgint_attr *amsgi;
    unsigned amsgi_pos;
    const pj_stun_username_attr *auser;
    pj_bool_t username_ok;
    const pj_stun_realm_attr *arealm;
    const pj_stun_realm_attr *anonce;
    pj_hmac_sha1_context ctx;
    pj_uint8_t digest[PJ_SHA1_DIGEST_SIZE];
    pj_str_t key;
    pj_status_t status;

    /* msg and credential MUST be specified */
    PJ_ASSERT_RETURN(pkt && pkt_len && msg && cred, PJ_EINVAL);

    /* If p_response is specified, pool MUST be specified. */
    PJ_ASSERT_RETURN(!p_response || pool, PJ_EINVAL);

    if (p_response)
	*p_response = NULL;

    if (!PJ_STUN_IS_REQUEST(msg->hdr.type))
	p_response = NULL;

    /* Get realm and nonce */
    realm.slen = nonce.slen = 0;
    if (cred->type == PJ_STUN_AUTH_CRED_STATIC) {
	realm = cred->data.static_cred.realm;
	nonce = cred->data.static_cred.nonce;
    } else if (cred->type == PJ_STUN_AUTH_CRED_DYNAMIC) {
	status = cred->data.dyn_cred.get_auth(cred->data.dyn_cred.user_data,
					      pool, &realm, &nonce);
	if (status != PJ_SUCCESS)
	    return status;
    } else {
	pj_assert(!"Unexpected");
	return PJ_EBUG;
    }

    /* First check that MESSAGE-INTEGRITY is present */
    amsgi = (const pj_stun_msgint_attr*)
	    pj_stun_msg_find_attr(msg, PJ_STUN_ATTR_MESSAGE_INTEGRITY, 0);
    if (amsgi == NULL) {
	if (p_response) {
	    create_challenge(pool, msg, PJ_STUN_SC_UNAUTHORIZED, NULL,
			     &realm, &nonce, p_response);
	}
	return PJ_STATUS_FROM_STUN_CODE(PJ_STUN_SC_INTEGRITY_CHECK_FAILURE);
    }

    /* Next check that USERNAME is present */
    auser = (const pj_stun_username_attr*)
	    pj_stun_msg_find_attr(msg, PJ_STUN_ATTR_USERNAME, 0);
    if (auser == NULL) {
	if (p_response) {
	    create_challenge(pool, msg, PJ_STUN_SC_MISSING_USERNAME, NULL,
			     &realm, &nonce, p_response);
	}
	return PJ_STATUS_FROM_STUN_CODE(PJ_STUN_SC_MISSING_USERNAME);
    }

    /* Get REALM, if any */
    arealm = (const pj_stun_realm_attr*)
	     pj_stun_msg_find_attr(msg, PJ_STUN_ATTR_REALM, 0);

    /* Check if username match */
    if (cred->type == PJ_STUN_AUTH_CRED_STATIC) {
	username_ok = !pj_strcmp(&auser->value, 
				 &cred->data.static_cred.username);
	password = cred->data.static_cred.data;
    } else if (cred->type == PJ_STUN_AUTH_CRED_DYNAMIC) {
	int data_type = 0;
	pj_status_t rc;
	rc = cred->data.dyn_cred.get_password(msg, 
					      cred->data.dyn_cred.user_data,
					      (arealm?&arealm->value:NULL),
					      &auser->value, pool,
					      &data_type, &password);
	username_ok = (rc == PJ_SUCCESS);
    } else {
	username_ok = PJ_TRUE;
	password.slen = 0;
    }

    if (!username_ok) {
	/* Username mismatch */
	if (p_response) {
	    create_challenge(pool, msg, PJ_STUN_SC_UNKNOWN_USERNAME, NULL,
			     &realm, &nonce, p_response);
	}
	return PJ_STATUS_FROM_STUN_CODE(PJ_STUN_SC_UNKNOWN_USERNAME);
    }


    /* Get NONCE attribute */
    anonce = (pj_stun_nonce_attr*)
	     pj_stun_msg_find_attr(msg, PJ_STUN_ATTR_NONCE, 0);

    /* Check for long term/short term requirements. */
    if (realm.slen != 0 && arealm == NULL) {
	/* Long term credential is required and REALM is not present */
	if (p_response) {
	    create_challenge(pool, msg, PJ_STUN_SC_MISSING_REALM, NULL,
			     &realm, &nonce, p_response);
	}
	return PJ_STATUS_FROM_STUN_CODE(PJ_STUN_SC_MISSING_REALM);

    } else if (realm.slen != 0 && arealm != NULL) {
	/* We want long term, and REALM is present */

	/* NONCE must be present. */
	if (anonce == NULL && nonce.slen) {
	    if (p_response) {
		create_challenge(pool, msg, PJ_STUN_SC_MISSING_NONCE, 
				 NULL, &realm, &nonce, p_response);
	    }
	    return PJ_STATUS_FROM_STUN_CODE(PJ_STUN_SC_MISSING_NONCE);
	}

	/* Verify REALM matches */
	if (pj_stricmp(&arealm->value, &realm)) {
	    /* REALM doesn't match */
	    if (p_response) {
		create_challenge(pool, msg, PJ_STUN_SC_MISSING_REALM, 
				 NULL, &realm, &nonce, p_response);
	    }
	    return PJ_STATUS_FROM_STUN_CODE(PJ_STUN_SC_MISSING_REALM);
	}

	/* Valid case, will validate the message integrity later */

    } else if (realm.slen == 0 && arealm != NULL) {
	/* We want to use short term credential, but client uses long
	 * term credential. The draft doesn't mention anything about
	 * switching between long term and short term.
	 */
	
	/* For now just accept the credential, anyway it will probably
	 * cause wrong message integrity value later.
	 */
    } else if (realm.slen==0 && arealm == NULL) {
	/* Short term authentication is wanted, and one is supplied */

	/* Application MAY request NONCE to be supplied */
	if (nonce.slen != 0) {
	    if (p_response) {
		create_challenge(pool, msg, PJ_STUN_SC_MISSING_NONCE, 
				 NULL, &realm, &nonce, p_response);
	    }
	    return PJ_STATUS_FROM_STUN_CODE(PJ_STUN_SC_MISSING_NONCE);
	}
    }

    /* If NONCE is present, validate it */
    if (anonce) {
	pj_bool_t ok;

	if (cred->type == PJ_STUN_AUTH_CRED_DYNAMIC &&
	    cred->data.dyn_cred.verify_nonce != NULL) 
	{
	    ok=cred->data.dyn_cred.verify_nonce(msg, 
						cred->data.dyn_cred.user_data,
						(arealm?&arealm->value:NULL),
						&auser->value,
						&anonce->value);
	} else if (cred->type == PJ_STUN_AUTH_CRED_DYNAMIC) {
	    ok = PJ_TRUE;
	} else {
	    if (nonce.slen) {
		ok = !pj_strcmp(&anonce->value, &nonce);
	    } else {
		ok = PJ_TRUE;
	    }
	}

	if (!ok) {
	    if (p_response) {
		create_challenge(pool, msg, PJ_STUN_SC_STALE_NONCE, 
				 NULL, &realm, &nonce, p_response);
	    }
	    return PJ_STATUS_FROM_STUN_CODE(PJ_STUN_SC_MISSING_NONCE);
	}
    }

    /* Get the position of MESSAGE-INTEGRITY in the packet */
    amsgi_pos = 20+msg->hdr.length-24;
    if (GET_VAL16(pkt, amsgi_pos) == PJ_STUN_ATTR_MESSAGE_INTEGRITY) {
	/* Found MESSAGE-INTEGRITY as the last attribute */
    } else {
	amsgi_pos = 0;
    }
    
    if (amsgi_pos==0) {
	amsgi_pos = 20+msg->hdr.length-8-24;
	if (GET_VAL16(pkt, amsgi_pos) == PJ_STUN_ATTR_MESSAGE_INTEGRITY) {
	    /* Found MESSAGE-INTEGRITY before FINGERPRINT */
	} else {
	    amsgi_pos = 0;
	}
    }

    if (amsgi_pos==0) {
	pj_assert(!"Unable to find MESSAGE-INTEGRITY in the message!");
	return PJ_EBUG;
    }

    /* Calculate key */
    pj_stun_create_key(pool, &key, &realm, &auser->value, &password);

    /* Now calculate HMAC of the message, adding zero padding if necessary
     * to make the input 64 bytes aligned.
     */
    pj_hmac_sha1_init(&ctx, (pj_uint8_t*)key.ptr, key.slen);
    pj_hmac_sha1_update(&ctx, pkt, amsgi_pos);
    if (amsgi_pos & 63) {
	pj_uint8_t zeroes[64];
	pj_bzero(zeroes, sizeof(zeroes));
	pj_hmac_sha1_update(&ctx, zeroes, 64-(amsgi_pos & 63));
    }
    pj_hmac_sha1_final(&ctx, digest);

    /* Compare HMACs */
    if (pj_memcmp(amsgi->hmac, digest, 20)) {
	/* HMAC value mismatch */
	if (p_response) {
	    create_challenge(pool, msg, PJ_STUN_SC_INTEGRITY_CHECK_FAILURE,
			     NULL, &realm, &nonce, p_response);
	}
	return PJ_STATUS_FROM_STUN_CODE(PJ_STUN_SC_INTEGRITY_CHECK_FAILURE);
    }

    /* Everything looks okay! */
    return PJ_SUCCESS;
}


/* Determine if STUN message can be authenticated */
PJ_DEF(pj_bool_t) pj_stun_auth_valid_for_msg(const pj_stun_msg *msg)
{
    unsigned msg_type = msg->hdr.type;
    const pj_stun_errcode_attr *err_attr;

    /* STUN requests and success response can be authenticated */
    if (!PJ_STUN_IS_ERROR_RESPONSE(msg_type) && 
	!PJ_STUN_IS_INDICATION(msg_type))
    {
	return PJ_TRUE;
    }

    /* STUN Indication cannot be authenticated */
    if (PJ_STUN_IS_INDICATION(msg_type))
	return PJ_FALSE;

    /* Authentication for STUN error responses depend on the error
     * code.
     */
    err_attr = (const pj_stun_errcode_attr*)
	       pj_stun_msg_find_attr(msg, PJ_STUN_ATTR_ERROR_CODE, 0);
    if (err_attr == NULL) {
	PJ_LOG(4,(THIS_FILE, "STUN error code attribute not present in "
			     "error response"));
	return PJ_TRUE;
    }

    switch (err_attr->err_code) {
    case PJ_STUN_SC_UNAUTHORIZED:
    case PJ_STUN_SC_MISSING_USERNAME:
    case PJ_STUN_SC_MISSING_REALM:
    case PJ_STUN_SC_UNKNOWN_USERNAME:
    case PJ_STUN_SC_INTEGRITY_CHECK_FAILURE:
	return PJ_FALSE;
    default:
	return PJ_TRUE;
    }
}


/* Authenticate MESSAGE-INTEGRITY in the response */
PJ_DEF(pj_status_t) pj_stun_authenticate_response(const pj_uint8_t *pkt,
					          unsigned pkt_len,
					          const pj_stun_msg *msg,
					          const pj_str_t *key)
{
    const pj_stun_msgint_attr *amsgi;
    unsigned amsgi_pos;
    pj_hmac_sha1_context ctx;
    pj_uint8_t digest[PJ_SHA1_DIGEST_SIZE];

    PJ_ASSERT_RETURN(pkt && pkt_len && msg && key, PJ_EINVAL);

    /* First check that MESSAGE-INTEGRITY is present */
    amsgi = (const pj_stun_msgint_attr*)
	    pj_stun_msg_find_attr(msg, PJ_STUN_ATTR_MESSAGE_INTEGRITY, 0);
    if (amsgi == NULL) {
	return PJ_STATUS_FROM_STUN_CODE(PJ_STUN_SC_INTEGRITY_CHECK_FAILURE);
    }


    /* Check that message length is valid */
    if (msg->hdr.length < 24) {
	return PJNATH_EINSTUNMSGLEN;
    }

    /* Get the position of MESSAGE-INTEGRITY in the packet */
    amsgi_pos = 20+msg->hdr.length-24;
    if (GET_VAL16(pkt, amsgi_pos) == PJ_STUN_ATTR_MESSAGE_INTEGRITY) {
	/* Found MESSAGE-INTEGRITY as the last attribute */
    } else {
	amsgi_pos = 0;
    }
    
    if (amsgi_pos==0) {
	/* Check that message length is valid */
	if (msg->hdr.length < 32) {
	    return PJ_STATUS_FROM_STUN_CODE(PJ_STUN_SC_INTEGRITY_CHECK_FAILURE);
	}

	amsgi_pos = 20+msg->hdr.length-8-24;
	if (GET_VAL16(pkt, amsgi_pos) == PJ_STUN_ATTR_MESSAGE_INTEGRITY) {
	    /* Found MESSAGE-INTEGRITY before FINGERPRINT */
	} else {
	    amsgi_pos = 0;
	}
    }

    if (amsgi_pos==0) {
	return PJ_STATUS_FROM_STUN_CODE(PJ_STUN_SC_INTEGRITY_CHECK_FAILURE);
    }

    /* Now calculate HMAC of the message, adding zero padding if necessary
     * to make the input 64 bytes aligned.
     */
    pj_hmac_sha1_init(&ctx, (pj_uint8_t*)key->ptr, key->slen);
    pj_hmac_sha1_update(&ctx, pkt, amsgi_pos);
    if (amsgi_pos & 0x3F) {
	pj_uint8_t zeroes[64];
	pj_bzero(zeroes, sizeof(zeroes));
	pj_hmac_sha1_update(&ctx, zeroes, 64-(amsgi_pos & 0x3F));
    }
    pj_hmac_sha1_final(&ctx, digest);

    /* Compare HMACs */
    if (pj_memcmp(amsgi->hmac, digest, 20)) {
	/* HMAC value mismatch */
	return PJ_STATUS_FROM_STUN_CODE(PJ_STUN_SC_INTEGRITY_CHECK_FAILURE);
    }

    /* Everything looks okay! */
    return PJ_SUCCESS;
}