Add the name of the guy who found the AIM/ICQ crash
[pidgin-git.git] / libpurple / cipher.c
blob57e5d697c1a09c3603f509756b9ec4910db00210
1 /*
2 * purple
4 * Purple is the legal property of its developers, whose names are too numerous
5 * to list here. Please refer to the COPYRIGHT file distributed with this
6 * source distribution.
8 * Original des taken from gpg
10 * des.c - DES and Triple-DES encryption/decryption Algorithm
11 * Copyright (C) 1998 Free Software Foundation, Inc.
13 * Please see below for more legal information!
15 * According to the definition of DES in FIPS PUB 46-2 from December 1993.
16 * For a description of triple encryption, see:
17 * Bruce Schneier: Applied Cryptography. Second Edition.
18 * John Wiley & Sons, 1996. ISBN 0-471-12845-7. Pages 358 ff.
20 * This file is part of GnuPG.
22 * This program is free software; you can redistribute it and/or modify
23 * it under the terms of the GNU General Public License as published by
24 * the Free Software Foundation; either version 2 of the License, or
25 * (at your option) any later version.
27 * This program is distributed in the hope that it will be useful,
28 * but WITHOUT ANY WARRANTY; without even the implied warranty of
29 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30 * GNU General Public License for more details.
32 * You should have received a copy of the GNU General Public License
33 * along with this program; if not, write to the Free Software
34 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
36 #include "internal.h"
37 #include "cipher.h"
38 #include "dbus-maybe.h"
39 #include "debug.h"
40 #include "signals.h"
41 #include "value.h"
43 /*******************************************************************************
44 * Structs
45 ******************************************************************************/
46 struct _PurpleCipher {
47 gchar *name; /**< Internal name - used for searching */
48 PurpleCipherOps *ops; /**< Operations supported by this cipher */
49 guint ref; /**< Reference count */
52 struct _PurpleCipherContext {
53 PurpleCipher *cipher; /**< Cipher this context is under */
54 gpointer data; /**< Internal cipher state data */
57 /******************************************************************************
58 * Globals
59 *****************************************************************************/
60 static GList *ciphers = NULL;
62 /******************************************************************************
63 * PurpleCipher API
64 *****************************************************************************/
65 const gchar *
66 purple_cipher_get_name(PurpleCipher *cipher) {
67 g_return_val_if_fail(cipher, NULL);
69 return cipher->name;
72 guint
73 purple_cipher_get_capabilities(PurpleCipher *cipher) {
74 PurpleCipherOps *ops = NULL;
75 guint caps = 0;
77 g_return_val_if_fail(cipher, 0);
79 ops = cipher->ops;
80 g_return_val_if_fail(ops, 0);
82 if(ops->set_option)
83 caps |= PURPLE_CIPHER_CAPS_SET_OPT;
84 if(ops->get_option)
85 caps |= PURPLE_CIPHER_CAPS_GET_OPT;
86 if(ops->init)
87 caps |= PURPLE_CIPHER_CAPS_INIT;
88 if(ops->reset)
89 caps |= PURPLE_CIPHER_CAPS_RESET;
90 if(ops->uninit)
91 caps |= PURPLE_CIPHER_CAPS_UNINIT;
92 if(ops->set_iv)
93 caps |= PURPLE_CIPHER_CAPS_SET_IV;
94 if(ops->append)
95 caps |= PURPLE_CIPHER_CAPS_APPEND;
96 if(ops->digest)
97 caps |= PURPLE_CIPHER_CAPS_DIGEST;
98 if(ops->encrypt)
99 caps |= PURPLE_CIPHER_CAPS_ENCRYPT;
100 if(ops->decrypt)
101 caps |= PURPLE_CIPHER_CAPS_DECRYPT;
102 if(ops->set_salt)
103 caps |= PURPLE_CIPHER_CAPS_SET_SALT;
104 if(ops->get_salt_size)
105 caps |= PURPLE_CIPHER_CAPS_GET_SALT_SIZE;
106 if(ops->set_key)
107 caps |= PURPLE_CIPHER_CAPS_SET_KEY;
108 if(ops->get_key_size)
109 caps |= PURPLE_CIPHER_CAPS_GET_KEY_SIZE;
110 if(ops->set_batch_mode)
111 caps |= PURPLE_CIPHER_CAPS_SET_BATCH_MODE;
112 if(ops->get_batch_mode)
113 caps |= PURPLE_CIPHER_CAPS_GET_BATCH_MODE;
114 if(ops->get_block_size)
115 caps |= PURPLE_CIPHER_CAPS_GET_BLOCK_SIZE;
116 if(ops->set_key_with_len)
117 caps |= PURPLE_CIPHER_CAPS_SET_KEY_WITH_LEN;
119 return caps;
122 gboolean
123 purple_cipher_digest_region(const gchar *name, const guchar *data,
124 size_t data_len, size_t in_len,
125 guchar digest[], size_t *out_len)
127 PurpleCipher *cipher;
128 PurpleCipherContext *context;
129 gboolean ret = FALSE;
131 g_return_val_if_fail(name, FALSE);
132 g_return_val_if_fail(data, FALSE);
134 cipher = purple_ciphers_find_cipher(name);
136 g_return_val_if_fail(cipher, FALSE);
138 if(!cipher->ops->append || !cipher->ops->digest) {
139 purple_debug_warning("cipher", "purple_cipher_region failed: "
140 "the %s cipher does not support appending and or "
141 "digesting.", cipher->name);
142 return FALSE;
145 context = purple_cipher_context_new(cipher, NULL);
146 purple_cipher_context_append(context, data, data_len);
147 ret = purple_cipher_context_digest(context, in_len, digest, out_len);
148 purple_cipher_context_destroy(context);
150 return ret;
153 /******************************************************************************
154 * PurpleCiphers API
155 *****************************************************************************/
156 PurpleCipher *
157 purple_ciphers_find_cipher(const gchar *name) {
158 PurpleCipher *cipher;
159 GList *l;
161 g_return_val_if_fail(name, NULL);
163 for(l = ciphers; l; l = l->next) {
164 cipher = PURPLE_CIPHER(l->data);
166 if(!g_ascii_strcasecmp(cipher->name, name))
167 return cipher;
170 return NULL;
173 PurpleCipher *
174 purple_ciphers_register_cipher(const gchar *name, PurpleCipherOps *ops) {
175 PurpleCipher *cipher = NULL;
177 g_return_val_if_fail(name, NULL);
178 g_return_val_if_fail(ops, NULL);
179 g_return_val_if_fail(!purple_ciphers_find_cipher(name), NULL);
181 cipher = g_new0(PurpleCipher, 1);
182 PURPLE_DBUS_REGISTER_POINTER(cipher, PurpleCipher);
184 cipher->name = g_strdup(name);
185 cipher->ops = ops;
187 ciphers = g_list_append(ciphers, cipher);
189 purple_signal_emit(purple_ciphers_get_handle(), "cipher-added", cipher);
191 return cipher;
194 gboolean
195 purple_ciphers_unregister_cipher(PurpleCipher *cipher) {
196 g_return_val_if_fail(cipher, FALSE);
197 g_return_val_if_fail(cipher->ref == 0, FALSE);
199 purple_signal_emit(purple_ciphers_get_handle(), "cipher-removed", cipher);
201 ciphers = g_list_remove(ciphers, cipher);
203 g_free(cipher->name);
205 PURPLE_DBUS_UNREGISTER_POINTER(cipher);
206 g_free(cipher);
208 return TRUE;
211 GList *
212 purple_ciphers_get_ciphers() {
213 return ciphers;
216 /******************************************************************************
217 * PurpleCipher Subsystem API
218 *****************************************************************************/
219 gpointer
220 purple_ciphers_get_handle() {
221 static gint handle;
223 return &handle;
226 /* These are implemented in the purple-ciphers sublibrary built in the ciphers
227 * directory. We could put a header file in there, but it's less hassle for
228 * the developer to just add it here since they have to register it here as
229 * well.
231 PurpleCipherOps *purple_des_cipher_get_ops();
232 PurpleCipherOps *purple_des3_cipher_get_ops();
233 PurpleCipherOps *purple_hmac_cipher_get_ops();
234 PurpleCipherOps *purple_md4_cipher_get_ops();
235 PurpleCipherOps *purple_md5_cipher_get_ops();
236 PurpleCipherOps *purple_rc4_cipher_get_ops();
237 PurpleCipherOps *purple_sha1_cipher_get_ops();
238 PurpleCipherOps *purple_sha256_cipher_get_ops();
240 void
241 purple_ciphers_init() {
242 gpointer handle;
244 handle = purple_ciphers_get_handle();
246 purple_signal_register(handle, "cipher-added",
247 purple_marshal_VOID__POINTER, NULL, 1,
248 purple_value_new(PURPLE_TYPE_SUBTYPE,
249 PURPLE_SUBTYPE_CIPHER));
250 purple_signal_register(handle, "cipher-removed",
251 purple_marshal_VOID__POINTER, NULL, 1,
252 purple_value_new(PURPLE_TYPE_SUBTYPE,
253 PURPLE_SUBTYPE_CIPHER));
255 purple_ciphers_register_cipher("md5", purple_md5_cipher_get_ops());
256 purple_ciphers_register_cipher("sha1", purple_sha1_cipher_get_ops());
257 purple_ciphers_register_cipher("sha256", purple_sha256_cipher_get_ops());
258 purple_ciphers_register_cipher("md4", purple_md4_cipher_get_ops());
259 purple_ciphers_register_cipher("hmac", purple_hmac_cipher_get_ops());
260 purple_ciphers_register_cipher("des", purple_des_cipher_get_ops());
261 purple_ciphers_register_cipher("des3", purple_des3_cipher_get_ops());
262 purple_ciphers_register_cipher("rc4", purple_rc4_cipher_get_ops());
265 void
266 purple_ciphers_uninit() {
267 PurpleCipher *cipher;
268 GList *l, *ll;
270 for(l = ciphers; l; l = ll) {
271 ll = l->next;
273 cipher = PURPLE_CIPHER(l->data);
274 purple_ciphers_unregister_cipher(cipher);
277 g_list_free(ciphers);
279 purple_signals_unregister_by_instance(purple_ciphers_get_handle());
282 /******************************************************************************
283 * PurpleCipherContext API
284 *****************************************************************************/
285 void
286 purple_cipher_context_set_option(PurpleCipherContext *context, const gchar *name,
287 gpointer value)
289 PurpleCipher *cipher = NULL;
291 g_return_if_fail(context);
292 g_return_if_fail(name);
294 cipher = context->cipher;
295 g_return_if_fail(cipher);
297 if(cipher->ops && cipher->ops->set_option)
298 cipher->ops->set_option(context, name, value);
299 else
300 purple_debug_warning("cipher", "the %s cipher does not support the "
301 "set_option operation\n", cipher->name);
304 gpointer
305 purple_cipher_context_get_option(PurpleCipherContext *context, const gchar *name) {
306 PurpleCipher *cipher = NULL;
308 g_return_val_if_fail(context, NULL);
309 g_return_val_if_fail(name, NULL);
311 cipher = context->cipher;
312 g_return_val_if_fail(cipher, NULL);
314 if(cipher->ops && cipher->ops->get_option)
315 return cipher->ops->get_option(context, name);
316 else {
317 purple_debug_warning("cipher", "the %s cipher does not support the "
318 "get_option operation\n", cipher->name);
320 return NULL;
324 PurpleCipherContext *
325 purple_cipher_context_new(PurpleCipher *cipher, void *extra) {
326 PurpleCipherContext *context = NULL;
328 g_return_val_if_fail(cipher, NULL);
330 cipher->ref++;
332 context = g_new0(PurpleCipherContext, 1);
333 context->cipher = cipher;
335 if(cipher->ops->init)
336 cipher->ops->init(context, extra);
338 return context;
341 PurpleCipherContext *
342 purple_cipher_context_new_by_name(const gchar *name, void *extra) {
343 PurpleCipher *cipher;
345 g_return_val_if_fail(name, NULL);
347 cipher = purple_ciphers_find_cipher(name);
349 g_return_val_if_fail(cipher, NULL);
351 return purple_cipher_context_new(cipher, extra);
354 void
355 purple_cipher_context_reset(PurpleCipherContext *context, void *extra) {
356 PurpleCipher *cipher = NULL;
358 g_return_if_fail(context);
360 cipher = context->cipher;
361 g_return_if_fail(cipher);
363 if(cipher->ops && cipher->ops->reset)
364 context->cipher->ops->reset(context, extra);
367 void
368 purple_cipher_context_destroy(PurpleCipherContext *context) {
369 PurpleCipher *cipher = NULL;
371 g_return_if_fail(context);
373 cipher = context->cipher;
374 g_return_if_fail(cipher);
376 cipher->ref--;
378 if(cipher->ops && cipher->ops->uninit)
379 cipher->ops->uninit(context);
381 memset(context, 0, sizeof(*context));
382 g_free(context);
383 context = NULL;
386 void
387 purple_cipher_context_set_iv(PurpleCipherContext *context, guchar *iv, size_t len)
389 PurpleCipher *cipher = NULL;
391 g_return_if_fail(context);
392 g_return_if_fail(iv);
394 cipher = context->cipher;
395 g_return_if_fail(cipher);
397 if(cipher->ops && cipher->ops->set_iv)
398 cipher->ops->set_iv(context, iv, len);
399 else
400 purple_debug_warning("cipher", "the %s cipher does not support the set"
401 "initialization vector operation\n", cipher->name);
404 void
405 purple_cipher_context_append(PurpleCipherContext *context, const guchar *data,
406 size_t len)
408 PurpleCipher *cipher = NULL;
410 g_return_if_fail(context);
412 cipher = context->cipher;
413 g_return_if_fail(cipher);
415 if(cipher->ops && cipher->ops->append)
416 cipher->ops->append(context, data, len);
417 else
418 purple_debug_warning("cipher", "the %s cipher does not support the append "
419 "operation\n", cipher->name);
422 gboolean
423 purple_cipher_context_digest(PurpleCipherContext *context, size_t in_len,
424 guchar digest[], size_t *out_len)
426 PurpleCipher *cipher = NULL;
428 g_return_val_if_fail(context, FALSE);
430 cipher = context->cipher;
432 if(cipher->ops && cipher->ops->digest)
433 return cipher->ops->digest(context, in_len, digest, out_len);
434 else {
435 purple_debug_warning("cipher", "the %s cipher does not support the digest "
436 "operation\n", cipher->name);
437 return FALSE;
441 gboolean
442 purple_cipher_context_digest_to_str(PurpleCipherContext *context, size_t in_len,
443 gchar digest_s[], size_t *out_len)
445 /* 8k is a bit excessive, will tweak later. */
446 guchar digest[BUF_LEN * 4];
447 gint n = 0;
448 size_t dlen = 0;
450 g_return_val_if_fail(context, FALSE);
451 g_return_val_if_fail(digest_s, FALSE);
453 if(!purple_cipher_context_digest(context, sizeof(digest), digest, &dlen))
454 return FALSE;
456 /* in_len must be greater than dlen * 2 so we have room for the NUL. */
457 if(in_len <= dlen * 2)
458 return FALSE;
460 for(n = 0; n < dlen; n++)
461 sprintf(digest_s + (n * 2), "%02x", digest[n]);
463 digest_s[n * 2] = '\0';
465 if(out_len)
466 *out_len = dlen * 2;
468 return TRUE;
471 gint
472 purple_cipher_context_encrypt(PurpleCipherContext *context, const guchar data[],
473 size_t len, guchar output[], size_t *outlen)
475 PurpleCipher *cipher = NULL;
477 g_return_val_if_fail(context, -1);
479 cipher = context->cipher;
480 g_return_val_if_fail(cipher, -1);
482 if(cipher->ops && cipher->ops->encrypt)
483 return cipher->ops->encrypt(context, data, len, output, outlen);
484 else {
485 purple_debug_warning("cipher", "the %s cipher does not support the encrypt"
486 "operation\n", cipher->name);
488 if(outlen)
489 *outlen = -1;
491 return -1;
495 gint
496 purple_cipher_context_decrypt(PurpleCipherContext *context, const guchar data[],
497 size_t len, guchar output[], size_t *outlen)
499 PurpleCipher *cipher = NULL;
501 g_return_val_if_fail(context, -1);
503 cipher = context->cipher;
504 g_return_val_if_fail(cipher, -1);
506 if(cipher->ops && cipher->ops->decrypt)
507 return cipher->ops->decrypt(context, data, len, output, outlen);
508 else {
509 purple_debug_warning("cipher", "the %s cipher does not support the decrypt"
510 "operation\n", cipher->name);
512 if(outlen)
513 *outlen = -1;
515 return -1;
519 void
520 purple_cipher_context_set_salt(PurpleCipherContext *context, guchar *salt) {
521 PurpleCipher *cipher = NULL;
523 g_return_if_fail(context);
525 cipher = context->cipher;
526 g_return_if_fail(cipher);
528 if(cipher->ops && cipher->ops->set_salt)
529 cipher->ops->set_salt(context, salt);
530 else
531 purple_debug_warning("cipher", "the %s cipher does not support the "
532 "set_salt operation\n", cipher->name);
535 size_t
536 purple_cipher_context_get_salt_size(PurpleCipherContext *context) {
537 PurpleCipher *cipher = NULL;
539 g_return_val_if_fail(context, -1);
541 cipher = context->cipher;
542 g_return_val_if_fail(cipher, -1);
544 if(cipher->ops && cipher->ops->get_salt_size)
545 return cipher->ops->get_salt_size(context);
546 else {
547 purple_debug_warning("cipher", "the %s cipher does not support the "
548 "get_salt_size operation\n", cipher->name);
550 return -1;
554 void
555 purple_cipher_context_set_key(PurpleCipherContext *context, const guchar *key) {
556 PurpleCipher *cipher = NULL;
558 g_return_if_fail(context);
560 cipher = context->cipher;
561 g_return_if_fail(cipher);
563 if(cipher->ops && cipher->ops->set_key)
564 cipher->ops->set_key(context, key);
565 else
566 purple_debug_warning("cipher", "the %s cipher does not support the "
567 "set_key operation\n", cipher->name);
570 size_t
571 purple_cipher_context_get_key_size(PurpleCipherContext *context) {
572 PurpleCipher *cipher = NULL;
574 g_return_val_if_fail(context, -1);
576 cipher = context->cipher;
577 g_return_val_if_fail(cipher, -1);
579 if(cipher->ops && cipher->ops->get_key_size)
580 return cipher->ops->get_key_size(context);
581 else {
582 purple_debug_warning("cipher", "the %s cipher does not support the "
583 "get_key_size operation\n", cipher->name);
585 return -1;
589 void
590 purple_cipher_context_set_batch_mode(PurpleCipherContext *context,
591 PurpleCipherBatchMode mode)
593 PurpleCipher *cipher = NULL;
595 g_return_if_fail(context);
597 cipher = context->cipher;
598 g_return_if_fail(cipher);
600 if(cipher->ops && cipher->ops->set_batch_mode)
601 cipher->ops->set_batch_mode(context, mode);
602 else
603 purple_debug_warning("cipher", "The %s cipher does not support the "
604 "set_batch_mode operation\n", cipher->name);
607 PurpleCipherBatchMode
608 purple_cipher_context_get_batch_mode(PurpleCipherContext *context)
610 PurpleCipher *cipher = NULL;
612 g_return_val_if_fail(context, -1);
614 cipher = context->cipher;
615 g_return_val_if_fail(cipher, -1);
617 if(cipher->ops && cipher->ops->get_batch_mode)
618 return cipher->ops->get_batch_mode(context);
619 else {
620 purple_debug_warning("cipher", "The %s cipher does not support the "
621 "get_batch_mode operation\n", cipher->name);
622 return -1;
626 size_t
627 purple_cipher_context_get_block_size(PurpleCipherContext *context)
629 PurpleCipher *cipher = NULL;
631 g_return_val_if_fail(context, -1);
633 cipher = context->cipher;
634 g_return_val_if_fail(cipher, -1);
636 if(cipher->ops && cipher->ops->get_block_size)
637 return cipher->ops->get_block_size(context);
638 else {
639 purple_debug_warning("cipher", "The %s cipher does not support the "
640 "get_block_size operation\n", cipher->name);
641 return -1;
645 void
646 purple_cipher_context_set_key_with_len(PurpleCipherContext *context,
647 const guchar *key, size_t len)
649 PurpleCipher *cipher = NULL;
651 g_return_if_fail(context);
653 cipher = context->cipher;
654 g_return_if_fail(cipher);
656 if(cipher->ops && cipher->ops->set_key_with_len)
657 cipher->ops->set_key_with_len(context, key, len);
658 else
659 purple_debug_warning("cipher", "The %s cipher does not support the "
660 "set_key_with_len operation\n", cipher->name);
663 void
664 purple_cipher_context_set_data(PurpleCipherContext *context, gpointer data) {
665 g_return_if_fail(context);
667 context->data = data;
670 gpointer
671 purple_cipher_context_get_data(PurpleCipherContext *context) {
672 g_return_val_if_fail(context, NULL);
674 return context->data;
677 gchar *purple_cipher_http_digest_calculate_session_key(
678 const gchar *algorithm,
679 const gchar *username,
680 const gchar *realm,
681 const gchar *password,
682 const gchar *nonce,
683 const gchar *client_nonce)
685 PurpleCipher *cipher;
686 PurpleCipherContext *context;
687 gchar hash[33]; /* We only support MD5. */
689 g_return_val_if_fail(username != NULL, NULL);
690 g_return_val_if_fail(realm != NULL, NULL);
691 g_return_val_if_fail(password != NULL, NULL);
692 g_return_val_if_fail(nonce != NULL, NULL);
694 /* Check for a supported algorithm. */
695 g_return_val_if_fail(algorithm == NULL ||
696 *algorithm == '\0' ||
697 g_ascii_strcasecmp(algorithm, "MD5") ||
698 g_ascii_strcasecmp(algorithm, "MD5-sess"), NULL);
700 cipher = purple_ciphers_find_cipher("md5");
701 g_return_val_if_fail(cipher != NULL, NULL);
703 context = purple_cipher_context_new(cipher, NULL);
705 purple_cipher_context_append(context, (guchar *)username, strlen(username));
706 purple_cipher_context_append(context, (guchar *)":", 1);
707 purple_cipher_context_append(context, (guchar *)realm, strlen(realm));
708 purple_cipher_context_append(context, (guchar *)":", 1);
709 purple_cipher_context_append(context, (guchar *)password, strlen(password));
711 if (algorithm != NULL && !g_ascii_strcasecmp(algorithm, "MD5-sess"))
713 guchar digest[16];
715 if (client_nonce == NULL)
717 purple_cipher_context_destroy(context);
718 purple_debug_error("cipher", "Required client_nonce missing for MD5-sess digest calculation.\n");
719 return NULL;
722 purple_cipher_context_digest(context, sizeof(digest), digest, NULL);
723 purple_cipher_context_destroy(context);
725 context = purple_cipher_context_new(cipher, NULL);
726 purple_cipher_context_append(context, digest, sizeof(digest));
727 purple_cipher_context_append(context, (guchar *)":", 1);
728 purple_cipher_context_append(context, (guchar *)nonce, strlen(nonce));
729 purple_cipher_context_append(context, (guchar *)":", 1);
730 purple_cipher_context_append(context, (guchar *)client_nonce, strlen(client_nonce));
733 purple_cipher_context_digest_to_str(context, sizeof(hash), hash, NULL);
734 purple_cipher_context_destroy(context);
736 return g_strdup(hash);
739 gchar *purple_cipher_http_digest_calculate_response(
740 const gchar *algorithm,
741 const gchar *method,
742 const gchar *digest_uri,
743 const gchar *qop,
744 const gchar *entity,
745 const gchar *nonce,
746 const gchar *nonce_count,
747 const gchar *client_nonce,
748 const gchar *session_key)
750 PurpleCipher *cipher;
751 PurpleCipherContext *context;
752 static gchar hash2[33]; /* We only support MD5. */
754 g_return_val_if_fail(method != NULL, NULL);
755 g_return_val_if_fail(digest_uri != NULL, NULL);
756 g_return_val_if_fail(nonce != NULL, NULL);
757 g_return_val_if_fail(session_key != NULL, NULL);
759 /* Check for a supported algorithm. */
760 g_return_val_if_fail(algorithm == NULL ||
761 *algorithm == '\0' ||
762 g_ascii_strcasecmp(algorithm, "MD5") ||
763 g_ascii_strcasecmp(algorithm, "MD5-sess"), NULL);
765 /* Check for a supported "quality of protection". */
766 g_return_val_if_fail(qop == NULL ||
767 *qop == '\0' ||
768 g_ascii_strcasecmp(qop, "auth") ||
769 g_ascii_strcasecmp(qop, "auth-int"), NULL);
771 cipher = purple_ciphers_find_cipher("md5");
772 g_return_val_if_fail(cipher != NULL, NULL);
774 context = purple_cipher_context_new(cipher, NULL);
776 purple_cipher_context_append(context, (guchar *)method, strlen(method));
777 purple_cipher_context_append(context, (guchar *)":", 1);
778 purple_cipher_context_append(context, (guchar *)digest_uri, strlen(digest_uri));
780 if (qop != NULL && !g_ascii_strcasecmp(qop, "auth-int"))
782 PurpleCipherContext *context2;
783 gchar entity_hash[33];
785 if (entity == NULL)
787 purple_cipher_context_destroy(context);
788 purple_debug_error("cipher", "Required entity missing for auth-int digest calculation.\n");
789 return NULL;
792 context2 = purple_cipher_context_new(cipher, NULL);
793 purple_cipher_context_append(context2, (guchar *)entity, strlen(entity));
794 purple_cipher_context_digest_to_str(context2, sizeof(entity_hash), entity_hash, NULL);
795 purple_cipher_context_destroy(context2);
797 purple_cipher_context_append(context, (guchar *)":", 1);
798 purple_cipher_context_append(context, (guchar *)entity_hash, strlen(entity_hash));
801 purple_cipher_context_digest_to_str(context, sizeof(hash2), hash2, NULL);
802 purple_cipher_context_destroy(context);
804 context = purple_cipher_context_new(cipher, NULL);
805 purple_cipher_context_append(context, (guchar *)session_key, strlen(session_key));
806 purple_cipher_context_append(context, (guchar *)":", 1);
807 purple_cipher_context_append(context, (guchar *)nonce, strlen(nonce));
808 purple_cipher_context_append(context, (guchar *)":", 1);
810 if (qop != NULL && *qop != '\0')
812 if (nonce_count == NULL)
814 purple_cipher_context_destroy(context);
815 purple_debug_error("cipher", "Required nonce_count missing for digest calculation.\n");
816 return NULL;
819 if (client_nonce == NULL)
821 purple_cipher_context_destroy(context);
822 purple_debug_error("cipher", "Required client_nonce missing for digest calculation.\n");
823 return NULL;
826 purple_cipher_context_append(context, (guchar *)nonce_count, strlen(nonce_count));
827 purple_cipher_context_append(context, (guchar *)":", 1);
828 purple_cipher_context_append(context, (guchar *)client_nonce, strlen(client_nonce));
829 purple_cipher_context_append(context, (guchar *)":", 1);
831 purple_cipher_context_append(context, (guchar *)qop, strlen(qop));
833 purple_cipher_context_append(context, (guchar *)":", 1);
836 purple_cipher_context_append(context, (guchar *)hash2, strlen(hash2));
837 purple_cipher_context_digest_to_str(context, sizeof(hash2), hash2, NULL);
838 purple_cipher_context_destroy(context);
840 return g_strdup(hash2);