1/*
2 * Copyright (c) 2006 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
5 *
6 * Portions Copyright (c) 2009 Apple Inc. All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * 1. Redistributions of source code must retain the above copyright
13 *    notice, this list of conditions and the following disclaimer.
14 *
15 * 2. Redistributions in binary form must reproduce the above copyright
16 *    notice, this list of conditions and the following disclaimer in the
17 *    documentation and/or other materials provided with the distribution.
18 *
19 * 3. Neither the name of the Institute nor the names of its contributors
20 *    may be used to endorse or promote products derived from this software
21 *    without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35
36#include "gssdigest.h"
37
38/*
39 * Allocate a scram context handle for the first provider that
40 * is up and running.
41 */
42OM_uint32
43_gss_scram_allocate_ctx(OM_uint32 *minor_status, const char *domain, scram_id_t *ctx)
44{
45    scram_id_t c;
46
47    *ctx = NULL;
48
49    c = calloc(1, sizeof(*c));
50    if (c == NULL) {
51	*minor_status = ENOMEM;
52	return GSS_S_FAILURE;
53    }
54
55    *ctx = c;
56
57    return GSS_S_COMPLETE;
58
59}
60
61/*
62 *
63 */
64
65OM_uint32
66_gss_scram_accept_sec_context
67(OM_uint32 * minor_status,
68 gss_ctx_id_t * context_handle,
69 const gss_cred_id_t acceptor_cred_handle,
70 const gss_buffer_t input_token_buffer,
71 const gss_channel_bindings_t input_chan_bindings,
72 gss_name_t * src_name,
73 gss_OID * mech_type,
74 gss_buffer_t output_token,
75 OM_uint32 * ret_flags,
76 OM_uint32 * time_rec,
77 gss_cred_id_t * delegated_cred_handle
78    )
79{
80    *minor_status = 0;
81
82    if (context_handle == NULL)
83	return GSS_S_FAILURE;
84
85    if (src_name)
86	*src_name = GSS_C_NO_NAME;
87    if (mech_type)
88	*mech_type = GSS_C_NO_OID;
89    if (ret_flags)
90	*ret_flags = 0;
91    if (time_rec)
92	*time_rec = 0;
93    if (delegated_cred_handle)
94	*delegated_cred_handle = GSS_C_NO_CREDENTIAL;
95
96#if 0
97    if (*context_handle == GSS_C_NO_CONTEXT) {
98	OM_uint32 major_status;
99	OM_uint32 retflags = 0;
100
101	_gss_mg_log(10, "scram-asc-s1");
102
103	major_status = _gss_scram_allocate_ctx(minor_status, NULL, &ctx);
104	if (major_status)
105	    return major_status;
106	*context_handle = (gss_ctx_id_t)ctx;
107
108	ctx->flags = retflags;
109
110	return GSS_S_CONTINUE_NEEDED;
111    } else {
112	OM_uint32 maj_stat;
113	size_t i;
114
115	if (input_token_buffer == GSS_C_NO_BUFFER)
116	    return GSS_S_FAILURE;
117
118	ctx = (scram_id_t)*context_handle;
119
120	data.data = input_token_buffer->value;
121	data.length = input_token_buffer->length;
122
123	ctx->client = strdup("lha");
124
125
126	_gss_mg_log(10, "scram-asc-s2");
127
128	if (src_name)
129	    *src_name = (gss_name_t)strdup(ctx->client);
130
131	if (mech_type)
132	    *mech_type = GSS_SCRAM_MECHANISM;
133	if (time_rec)
134	    *time_rec = GSS_C_INDEFINITE;
135
136	ctx->status |= STATUS_OPEN;
137
138	if (ret_flags)
139	    *ret_flags = ctx->flags;
140
141	return GSS_S_FAILURE;
142    }
143#else
144    return GSS_S_FAILURE;
145#endif
146}
147