security: add API to return context name
[siplcs.git] / src / core / sip-sec-basic.c
blob55fa5f39d52feb73a7e59984a8494647aa58bb0f
1 /**
2 * @file sip-sec-basic.c
4 * pidgin-sipe
6 * Copyright (C) 2013 SIPE Project <http://sipe.sourceforge.net/>
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
24 * Implementation for HTTP "WWW-Authenticate: Basic" scheme (RFC1945).
27 #include <string.h>
29 #include <glib.h>
31 #include "sipe-common.h"
32 #include "sip-sec.h"
33 #include "sip-sec-mech.h"
34 #include "sip-sec-basic.h"
35 #include "sipe-backend.h"
37 /* Security context for Basic */
38 typedef struct _context_basic {
39 struct sip_sec_context common;
40 gchar *token;
41 guint length;
42 } *context_basic;
44 /* sip-sec-mech.h API implementation for Basic */
46 static gboolean
47 sip_sec_acquire_cred__basic(SipSecContext context,
48 SIPE_UNUSED_PARAMETER const gchar *domain,
49 const gchar *username,
50 const gchar *password)
52 context_basic ctx = (context_basic) context;
54 SIPE_DEBUG_INFO_NOFORMAT("sip_sec_acquire_cred__basic: entering");
56 if (!username || !password)
57 return(FALSE);
59 /* calculate Basic token (RFC1945 section 11.1) */
60 ctx->token = g_strdup_printf("%s:%s", username, password);
61 ctx->length = strlen(ctx->token);
63 return(TRUE);
66 static gboolean
67 sip_sec_init_sec_context__basic(SipSecContext context,
68 SIPE_UNUSED_PARAMETER SipSecBuffer in_buff,
69 SipSecBuffer *out_buff,
70 SIPE_UNUSED_PARAMETER const gchar *service_name)
72 context_basic ctx = (context_basic) context;
74 out_buff->length = ctx->length;
75 out_buff->value = (guint8 *) g_strdup(ctx->token);
77 return(TRUE);
80 static gboolean
81 sip_sec_make_signature__basic(SIPE_UNUSED_PARAMETER SipSecContext context,
82 SIPE_UNUSED_PARAMETER const gchar *message,
83 SIPE_UNUSED_PARAMETER SipSecBuffer *signature)
85 /* No implementation needed, as Basic is not used for SIP */
86 return(FALSE);
89 static gboolean
90 sip_sec_verify_signature__basic(SIPE_UNUSED_PARAMETER SipSecContext context,
91 SIPE_UNUSED_PARAMETER const gchar *message,
92 SIPE_UNUSED_PARAMETER SipSecBuffer signature)
94 /* No implementation needed, as Basic is not used for SIP */
95 return(FALSE);
98 static void
99 sip_sec_destroy_sec_context__basic(SipSecContext context)
101 context_basic ctx = (context_basic) context;
103 g_free(ctx->token);
104 g_free(ctx);
107 static const gchar *
108 sip_sec_context_name__basic(SIPE_UNUSED_PARAMETER SipSecContext context)
110 return("Basic");
113 SipSecContext
114 sip_sec_create_context__basic(SIPE_UNUSED_PARAMETER guint type)
116 context_basic context = g_malloc0(sizeof(struct _context_basic));
117 if (!context) return(NULL);
119 context->common.acquire_cred_func = sip_sec_acquire_cred__basic;
120 context->common.init_context_func = sip_sec_init_sec_context__basic;
121 context->common.destroy_context_func = sip_sec_destroy_sec_context__basic;
122 context->common.make_signature_func = sip_sec_make_signature__basic;
123 context->common.verify_signature_func = sip_sec_verify_signature__basic;
124 context->common.context_name_func = sip_sec_context_name__basic;
126 return((SipSecContext) context);
130 Local Variables:
131 mode: c
132 c-file-style: "bsd"
133 indent-tabs-mode: t
134 tab-width: 8
135 End: