11181
|
1 /*
|
|
2 * Taken from RFC 2617
|
|
3 * Copyright (C) The Internet Society (1999). All Rights Reserved.
|
|
4
|
|
5 This document and translations of it may be copied and furnished to
|
|
6 others, and derivative works that comment on or otherwise explain it
|
|
7 or assist in its implementation may be prepared, copied, published
|
|
8 and distributed, in whole or in part, without restriction of any
|
|
9 kind, provided that the above copyright notice and this paragraph are
|
|
10 included on all such copies and derivative works. However, this
|
|
11 document itself may not be modified in any way, such as by removing
|
|
12 the copyright notice or references to the Internet Society or other
|
|
13 Internet organizations, except as needed for the purpose of
|
|
14 developing Internet standards in which case the procedures for
|
|
15 copyrights defined in the Internet Standards process must be
|
|
16 followed, or as required to translate it into languages other than
|
|
17 English.
|
|
18
|
|
19 The limited permissions granted above are perpetual and will not be
|
|
20 revoked by the Internet Society or its successors or assigns.
|
|
21
|
|
22 This document and the information contained herein is provided on an
|
|
23 "AS IS" basis and THE INTERNET SOCIETY AND THE INTERNET ENGINEERING
|
|
24 TASK FORCE DISCLAIMS ALL WARRANTIES, EXPRESS OR IMPLIED, INCLUDING
|
|
25 BUT NOT LIMITED TO ANY WARRANTY THAT THE USE OF THE INFORMATION
|
|
26 HEREIN WILL NOT INFRINGE ANY RIGHTS OR ANY IMPLIED WARRANTIES OF
|
|
27 MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
|
|
28 */
|
|
29 #include "cipher.h"
|
|
30
|
|
31 #include <string.h>
|
|
32 #include "digcalc.h"
|
|
33
|
|
34 void CvtHex(
|
|
35 IN HASH Bin,
|
|
36 OUT HASHHEX Hex
|
|
37 )
|
|
38 {
|
|
39 unsigned short i;
|
|
40 unsigned char j;
|
|
41
|
|
42 for (i = 0; i < HASHLEN; i++) {
|
|
43 j = (Bin[i] >> 4) & 0xf;
|
|
44 if (j <= 9)
|
|
45 Hex[i*2] = (j + '0');
|
|
46 else
|
|
47 Hex[i*2] = (j + 'a' - 10);
|
|
48 j = Bin[i] & 0xf;
|
|
49 if (j <= 9)
|
|
50 Hex[i*2+1] = (j + '0');
|
|
51 else
|
|
52 Hex[i*2+1] = (j + 'a' - 10);
|
|
53 };
|
|
54 Hex[HASHHEXLEN] = '\0';
|
|
55 };
|
|
56
|
|
57 /* calculate H(A1) as per spec */
|
|
58 void DigestCalcHA1(
|
|
59 IN char * pszAlg,
|
|
60 IN char * pszUserName,
|
|
61 IN char * pszRealm,
|
|
62 IN char * pszPassword,
|
|
63 IN char * pszNonce,
|
|
64 IN char * pszCNonce,
|
|
65 OUT HASHHEX SessionKey
|
|
66 )
|
|
67 {
|
|
68 GaimCipher *cipher;
|
|
69 GaimCipherContext *context;
|
|
70 HASH HA1;
|
|
71
|
|
72 cipher = gaim_ciphers_find_cipher("md5");
|
|
73 context = gaim_cipher_context_new(cipher, NULL);
|
11512
|
74 gaim_cipher_context_append(context, (guchar *)pszUserName, strlen(pszUserName));
|
|
75 gaim_cipher_context_append(context, (guchar *)":", 1);
|
|
76 gaim_cipher_context_append(context, (guchar *)pszRealm, strlen(pszRealm));
|
|
77 gaim_cipher_context_append(context, (guchar *)":", 1);
|
|
78 gaim_cipher_context_append(context, (guchar *)pszPassword, strlen(pszPassword));
|
11181
|
79 gaim_cipher_context_digest(context, sizeof(HA1), HA1, NULL);
|
|
80 if (strcmp(pszAlg, "md5-sess") == 0) {
|
|
81 context = gaim_cipher_context_new(cipher, NULL);
|
|
82 gaim_cipher_context_append(context, HA1, HASHLEN);
|
11512
|
83 gaim_cipher_context_append(context, (guchar *)":", 1);
|
|
84 gaim_cipher_context_append(context, (guchar *)pszNonce, strlen(pszNonce));
|
|
85 gaim_cipher_context_append(context, (guchar *)":", 1);
|
|
86 gaim_cipher_context_append(context, (guchar *)pszCNonce, strlen(pszCNonce));
|
11181
|
87 gaim_cipher_context_digest(context, sizeof(HA1), HA1, NULL);
|
|
88 };
|
|
89 CvtHex(HA1, SessionKey);
|
|
90 gaim_cipher_context_destroy(context);
|
|
91 };
|
|
92
|
|
93 /* calculate request-digest/response-digest as per HTTP Digest spec */
|
|
94 void DigestCalcResponse(
|
|
95 IN HASHHEX HA1, /* H(A1) */
|
|
96 IN char * pszNonce, /* nonce from server */
|
|
97 IN char * pszNonceCount, /* 8 hex digits */
|
|
98 IN char * pszCNonce, /* client nonce */
|
|
99 IN char * pszQop, /* qop-value: "", "auth", "auth-int" */
|
|
100 IN char * pszMethod, /* method from the request */
|
|
101 IN char * pszDigestUri, /* requested URL */
|
|
102 IN HASHHEX HEntity, /* H(entity body) if qop="auth-int" */
|
|
103 OUT HASHHEX Response /* request-digest or response-digest */
|
|
104 )
|
|
105 {
|
|
106 GaimCipher *cipher;
|
|
107 GaimCipherContext *context;
|
|
108 HASH HA2;
|
|
109 HASH RespHash;
|
|
110 HASHHEX HA2Hex;
|
|
111
|
|
112 // calculate H(A2)
|
|
113 cipher = gaim_ciphers_find_cipher("md5");
|
|
114 context = gaim_cipher_context_new(cipher, NULL);
|
11512
|
115 gaim_cipher_context_append(context, (guchar *)pszMethod, strlen(pszMethod));
|
|
116 gaim_cipher_context_append(context, (guchar *)":", 1);
|
|
117 gaim_cipher_context_append(context, (guchar *)pszDigestUri, strlen(pszDigestUri));
|
11181
|
118 if (strcmp(pszQop, "auth-int") == 0) {
|
11512
|
119 gaim_cipher_context_append(context, (guchar *)":", 1);
|
11181
|
120 gaim_cipher_context_append(context, HEntity, HASHHEXLEN);
|
|
121 };
|
|
122 gaim_cipher_context_digest(context, sizeof(HA2), HA2, NULL);
|
|
123 CvtHex(HA2, HA2Hex);
|
|
124
|
|
125 gaim_cipher_context_destroy(context);
|
|
126 // calculate response
|
|
127 context = gaim_cipher_context_new(cipher, NULL);
|
|
128 gaim_cipher_context_append(context, HA1, HASHHEXLEN);
|
11512
|
129 gaim_cipher_context_append(context, (guchar *)":", 1);
|
|
130 gaim_cipher_context_append(context, (guchar *)pszNonce, strlen(pszNonce));
|
|
131 gaim_cipher_context_append(context, (guchar *)":", 1);
|
11181
|
132 if (*pszQop) {
|
11512
|
133 gaim_cipher_context_append(context, (guchar *)pszNonceCount, strlen(pszNonceCount));
|
|
134 gaim_cipher_context_append(context, (guchar *)":", 1);
|
|
135 gaim_cipher_context_append(context, (guchar *)pszCNonce, strlen(pszCNonce));
|
|
136 gaim_cipher_context_append(context, (guchar *)":", 1);
|
|
137 gaim_cipher_context_append(context, (guchar *)pszQop, strlen(pszQop));
|
|
138 gaim_cipher_context_append(context, (guchar *)":", 1);
|
11181
|
139 };
|
|
140 gaim_cipher_context_append(context, HA2Hex, HASHHEXLEN);
|
|
141 gaim_cipher_context_digest(context, sizeof(RespHash), RespHash, NULL);
|
|
142 CvtHex(RespHash, Response);
|
|
143 gaim_cipher_context_destroy(context);
|
|
144 };
|