2 * This file is part of the Nice GLib ICE library.
4 * (C) 2008-2009 Collabora Ltd.
5 * Contact: Youness Alaoui
6 * (C) 2007-2009 Nokia Corporation. All rights reserved.
7 * Contact: Rémi Denis-Courmont
9 * The contents of this file are subject to the Mozilla Public License Version
10 * 1.1 (the "License"); you may not use this file except in compliance with
11 * the License. You may obtain a copy of the License at
12 * http://www.mozilla.org/MPL/
14 * Software distributed under the License is distributed on an "AS IS" basis,
15 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
16 * for the specific language governing rights and limitations under the
19 * The Original Code is the Nice GLib ICE library.
21 * The Initial Developers of the Original Code are Collabora Ltd and Nokia
22 * Corporation. All Rights Reserved.
25 * Youness Alaoui, Collabora Ltd.
26 * Rémi Denis-Courmont, Nokia
28 * Alternatively, the contents of this file may be used under the terms of the
29 * the GNU Lesser General Public License Version 2.1 (the "LGPL"), in which
30 * case the provisions of LGPL are applicable instead of those above. If you
31 * wish to allow use of your version of this file only under the terms of the
32 * LGPL and not to allow others to use your version of this file under the
33 * MPL, indicate your decision by deleting the provisions above and replace
34 * them with the notice and other provisions required by the LGPL. If you do
35 * not delete the provisions above, a recipient may use your version of this
36 * file under either the MPL or the LGPL.
47 #include "stunmessage.h"
53 void stun_sha1 (const uint8_t *msg
, size_t len
, size_t msg_len
, uint8_t *sha
,
54 const void *key
, size_t keylen
, int padding
)
56 uint16_t fakelen
= htons (msg_len
);
57 const uint8_t *vector
[4];
59 uint8_t pad_char
[64] = {0};
66 vector
[1] = (const uint8_t *)&fakelen
;
69 lengths
[2] = len
- 28;
72 /* RFC 3489 specifies that the message's size should be 64 bytes,
73 and \x00 padding should be done */
74 if (padding
&& ((len
- 24) % 64) > 0) {
75 uint16_t pad_size
= 64 - ((len
- 24) % 64);
78 lengths
[3] = pad_size
;
82 hmac_sha1_vector(key
, keylen
, num_elements
, vector
, lengths
, sha
);
85 static const uint8_t *priv_trim_var (const uint8_t *var
, size_t *var_len
)
87 const uint8_t *ptr
= var
;
93 while(ptr
[*var_len
-1] == '"' ||
94 ptr
[*var_len
-1] == 0) {
102 void stun_hash_creds (const uint8_t *realm
, size_t realm_len
,
103 const uint8_t *username
, size_t username_len
,
104 const uint8_t *password
, size_t password_len
,
105 unsigned char md5
[16])
108 const uint8_t *username_trimmed
= priv_trim_var (username
, &username_len
);
109 const uint8_t *password_trimmed
= priv_trim_var (password
, &password_len
);
110 const uint8_t *realm_trimmed
= priv_trim_var (realm
, &realm_len
);
111 const uint8_t *colon
= (uint8_t *)":";
114 MD5Update (&ctx
, username_trimmed
, username_len
);
115 MD5Update (&ctx
, colon
, 1);
116 MD5Update (&ctx
, realm_trimmed
, realm_len
);
117 MD5Update (&ctx
, colon
, 1);
118 MD5Update (&ctx
, password_trimmed
, password_len
);
119 MD5Final (md5
, &ctx
);
123 void stun_make_transid (StunTransactionId id
)