6 * Copyright (C) 2009 pier11 <pier11@kinozal.tv>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30 //#include "sip-sec-mech.h"
33 #include "sip-sec-ntlm.h"
34 #define sip_sec_acquire_cred__NTLM sip_sec_acquire_cred__ntlm
35 //#include "sip-sec-krb5.h"
36 #define sip_sec_acquire_cred__Kerberos NULL
40 #include "sip-sec-sspi.h"
41 #define sip_sec_acquire_cred__NTLM sip_sec_acquire_cred__sspi
42 #define sip_sec_acquire_cred__Kerberos sip_sec_acquire_cred__sspi
45 #include "sip-sec-ntlm.h"
46 #define sip_sec_acquire_cred__NTLM sip_sec_acquire_cred__ntlm
47 #define sip_sec_acquire_cred__Kerberos NULL
53 gchar
*purple_base64_encode(const guchar
*data
, gsize len
);
54 guchar
*purple_base64_decode(const char *str
, gsize
*ret_len
);
56 /* sip_sec API method */
57 char * sip_sec_init_context(SipSecContext
*context
, const char *mech
,
58 const char *domain
, const char *username
, const char *password
,
60 const char *input_toked_base64
)
62 SipSecCred cred_handle_p
;
65 sip_sec_acquire_cred_func acquire_cred_func
= !strncmp("Kerberos", mech
, strlen(mech
)) ?
66 sip_sec_acquire_cred__Kerberos
: sip_sec_acquire_cred__NTLM
;
68 ret2
= (*acquire_cred_func
)(&cred_handle_p
, context
, domain
, username
, password
);
71 sip_uint32 ret3
, ret4
;
77 SipSecBuffer out_buff
;
78 gchar
*out_buff_base64
;
80 ret3
= (*((struct sip_sec_context_struct
*) *context
)->init_context_func
)(cred_handle_p
, *context
,
84 out_buff_base64
= purple_base64_encode(out_buff
.value
, out_buff
.length
);
85 //Type1 (empty) to send
87 if (ret3
== SIP_SEC_I_CONTINUE_NEEDED
) {
89 SipSecBuffer out_buff
;
92 in_buff
.value
= purple_base64_decode(input_toked_base64
, &(in_buff
.length
));
94 ret4
= (*((struct sip_sec_context_struct
*) *context
)->init_context_func
)(cred_handle_p
, *context
,
100 g_free(out_buff_base64
);
101 out_buff_base64
= purple_base64_encode(out_buff
.value
, out_buff
.length
);
104 return out_buff_base64
;
108 sip_sec_destroy_context(SipSecContext context
)
110 if (context
) (*((struct sip_sec_context_struct
*) context
)->destroy_context_func
)(context
);
113 char * sip_sec_make_signature(SipSecContext context
, const char *message
)
115 SipSecBuffer signature
;
117 if(((*((struct sip_sec_context_struct
*) context
)->make_signature_func
)(context
, message
, &signature
)) != SIP_SEC_E_OK
) {
118 purple_debug_info("sipe", "ERROR: sip_sec_make_signature failed. Unable to sign message!\n");
121 char *signature_hex
= bytes_to_hex_str(&signature
);
122 free_bytes_buffer(&signature
);
123 return signature_hex
;
126 int sip_sec_verify_signature(SipSecContext context
, const char* message
, const char* signature_hex
)
128 SipSecBuffer signature
;
130 sip_uint32 res
= SIP_SEC_E_INTERNAL_ERROR
;
132 hex_str_to_bytes(signature_hex
, &signature
);
133 res
= (*((struct sip_sec_context_struct
*) context
)->verify_signature_func
)(context
, message
, signature
);
134 free_bytes_buffer(&signature
);
139 // Utility Methods //
141 void hex_str_to_bytes(const char *hex_str
, SipSecBuffer
*bytes
)
147 bytes
->length
= strlen(hex_str
)/2;
148 bytes
->value
= g_malloc(bytes
->length
);
150 buff
= (guint8
*)bytes
->value
;
151 for (i
= 0; i
< bytes
->length
; i
++) {
152 two_digits
[0] = hex_str
[i
* 2];
153 two_digits
[1] = hex_str
[i
* 2 + 1];
154 two_digits
[2] = '\0';
155 guint8 tmp
= (guint8
)strtoul(two_digits
, NULL
, 16);
160 void free_bytes_buffer(SipSecBuffer
*bytes
)
162 g_free(bytes
->value
);
165 char *bytes_to_hex_str(SipSecBuffer
*bytes
)
167 guint8
*buff
= (guint8
*)bytes
->value
;
168 char *res
= g_malloc(bytes
->length
* 2 + 1);
170 for (i
= 0, j
= 0; i
< bytes
->length
; i
++, j
+=2) {
171 sprintf(&res
[j
], "%02X", buff
[i
]);