1 /* $NetBSD: gss_mo.c,v 1.1.1.2 2014/04/24 12:45:29 pettai Exp $ */
4 * Copyright (c) 2010 Kungliga Tekniska Högskolan
5 * (Royal Institute of Technology, Stockholm, Sweden).
8 * Portions Copyright (c) 2010 Apple Inc. All rights reserved.
9 * Portions Copyright (c) 2010 PADL Software Pty Ltd. All rights reserved.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
22 * 3. Neither the name of the Institute nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
26 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 #include "mech_locl.h"
41 #include <crypto-headers.h>
44 get_option_def(int def
, gss_const_OID mech
, gss_mo_desc
*mo
, gss_buffer_t value
)
50 _gss_mo_get_option_1(gss_const_OID mech
, gss_mo_desc
*mo
, gss_buffer_t value
)
52 return get_option_def(1, mech
, mo
, value
);
56 _gss_mo_get_option_0(gss_const_OID mech
, gss_mo_desc
*mo
, gss_buffer_t value
)
58 return get_option_def(0, mech
, mo
, value
);
62 _gss_mo_get_ctx_as_string(gss_const_OID mech
, gss_mo_desc
*mo
, gss_buffer_t value
)
65 value
->value
= strdup((char *)mo
->ctx
);
66 if (value
->value
== NULL
)
68 value
->length
= strlen((char *)mo
->ctx
);
70 return GSS_S_COMPLETE
;
73 GSSAPI_LIB_FUNCTION
int GSSAPI_LIB_CALL
74 gss_mo_set(gss_const_OID mech
, gss_const_OID option
,
75 int enable
, gss_buffer_t value
)
77 gssapi_mech_interface m
;
80 if ((m
= __gss_get_mechanism(mech
)) == NULL
)
81 return GSS_S_BAD_MECH
;
83 for (n
= 0; n
< m
->gm_mo_num
; n
++)
84 if (gss_oid_equal(option
, m
->gm_mo
[n
].option
) && m
->gm_mo
[n
].set
)
85 return m
->gm_mo
[n
].set(mech
, &m
->gm_mo
[n
], enable
, value
);
87 return GSS_S_UNAVAILABLE
;
90 GSSAPI_LIB_FUNCTION
int GSSAPI_LIB_CALL
91 gss_mo_get(gss_const_OID mech
, gss_const_OID option
, gss_buffer_t value
)
93 gssapi_mech_interface m
;
96 _mg_buffer_zero(value
);
98 if ((m
= __gss_get_mechanism(mech
)) == NULL
)
99 return GSS_S_BAD_MECH
;
101 for (n
= 0; n
< m
->gm_mo_num
; n
++)
102 if (gss_oid_equal(option
, m
->gm_mo
[n
].option
) && m
->gm_mo
[n
].get
)
103 return m
->gm_mo
[n
].get(mech
, &m
->gm_mo
[n
], value
);
105 return GSS_S_UNAVAILABLE
;
109 add_all_mo(gssapi_mech_interface m
, gss_OID_set
*options
, OM_uint32 mask
)
114 for (n
= 0; n
< m
->gm_mo_num
; n
++)
115 if ((m
->gm_mo
[n
].flags
& mask
) == mask
)
116 gss_add_oid_set_member(&minor
, m
->gm_mo
[n
].option
, options
);
119 GSSAPI_LIB_FUNCTION
void GSSAPI_LIB_CALL
120 gss_mo_list(gss_const_OID mech
, gss_OID_set
*options
)
122 gssapi_mech_interface m
;
123 OM_uint32 major
, minor
;
128 *options
= GSS_C_NO_OID_SET
;
130 if ((m
= __gss_get_mechanism(mech
)) == NULL
)
133 major
= gss_create_empty_oid_set(&minor
, options
);
134 if (major
!= GSS_S_COMPLETE
)
137 add_all_mo(m
, options
, 0);
140 GSSAPI_LIB_FUNCTION OM_uint32 GSSAPI_LIB_CALL
141 gss_mo_name(gss_const_OID mech
, gss_const_OID option
, gss_buffer_t name
)
143 gssapi_mech_interface m
;
147 return GSS_S_BAD_NAME
;
149 if ((m
= __gss_get_mechanism(mech
)) == NULL
)
150 return GSS_S_BAD_MECH
;
152 for (n
= 0; n
< m
->gm_mo_num
; n
++) {
153 if (gss_oid_equal(option
, m
->gm_mo
[n
].option
)) {
155 * If there is no name, its because its a GSS_C_MA and
156 * there is already a table for that.
158 if (m
->gm_mo
[n
].name
) {
159 name
->value
= strdup(m
->gm_mo
[n
].name
);
160 if (name
->value
== NULL
)
161 return GSS_S_BAD_NAME
;
162 name
->length
= strlen(m
->gm_mo
[n
].name
);
163 return GSS_S_COMPLETE
;
166 return gss_display_mech_attr(&junk
, option
,
171 return GSS_S_BAD_NAME
;
175 * Helper function to allow NULL name
179 mo_value(const gss_const_OID mech
, gss_const_OID option
, gss_buffer_t name
)
182 return GSS_S_COMPLETE
;
184 return gss_mo_get(mech
, option
, name
);
187 /* code derived from draft-ietf-cat-sasl-gssapi-01 */
188 static char basis_32
[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567";
191 make_sasl_name(OM_uint32
*minor
, const gss_OID mech
, char sasl_name
[16])
195 u_char hdr
[2], hash
[20], *h
= hash
;
197 if (mech
->length
> 127)
198 return GSS_S_BAD_MECH
;
201 hdr
[1] = mech
->length
;
203 ctx
= EVP_MD_CTX_create();
204 EVP_DigestInit_ex(ctx
, EVP_sha1(), NULL
);
205 EVP_DigestUpdate(ctx
, hdr
, 2);
206 EVP_DigestUpdate(ctx
, mech
->elements
, mech
->length
);
207 EVP_DigestFinal_ex(ctx
, hash
, NULL
);
209 memcpy(p
, "GS2-", 4);
212 *p
++ = basis_32
[(h
[0] >> 3)];
213 *p
++ = basis_32
[((h
[0] & 7) << 2) | (h
[1] >> 6)];
214 *p
++ = basis_32
[(h
[1] & 0x3f) >> 1];
215 *p
++ = basis_32
[((h
[1] & 1) << 4) | (h
[2] >> 4)];
216 *p
++ = basis_32
[((h
[2] & 0xf) << 1) | (h
[3] >> 7)];
217 *p
++ = basis_32
[(h
[3] & 0x7f) >> 2];
218 *p
++ = basis_32
[((h
[3] & 3) << 3) | (h
[4] >> 5)];
219 *p
++ = basis_32
[(h
[4] & 0x1f)];
220 *p
++ = basis_32
[(h
[5] >> 3)];
221 *p
++ = basis_32
[((h
[5] & 7) << 2) | (h
[6] >> 6)];
222 *p
++ = basis_32
[(h
[6] & 0x3f) >> 1];
226 return GSS_S_COMPLETE
;
230 * gss_inquire_saslname_for_mech() wrapper that uses MIT SPI
233 inquire_saslname_for_mech_compat(OM_uint32
*minor
,
234 const gss_OID desired_mech
,
235 gss_buffer_t sasl_mech_name
,
236 gss_buffer_t mech_name
,
237 gss_buffer_t mech_description
)
239 struct gss_mech_compat_desc_struct
*gmc
;
240 gssapi_mech_interface m
;
243 m
= __gss_get_mechanism(desired_mech
);
245 return GSS_S_BAD_MECH
;
249 if (gmc
!= NULL
&& gmc
->gmc_inquire_saslname_for_mech
!= NULL
) {
250 major
= gmc
->gmc_inquire_saslname_for_mech(minor
,
256 major
= GSS_S_UNAVAILABLE
;
263 * Returns different protocol names and description of the mechanism.
265 * @param minor_status minor status code
266 * @param desired_mech mech list query
267 * @param sasl_mech_name SASL GS2 protocol name
268 * @param mech_name gssapi protocol name
269 * @param mech_description description of gssapi mech
271 * @return returns GSS_S_COMPLETE or a error code.
276 GSSAPI_LIB_FUNCTION OM_uint32 GSSAPI_LIB_CALL
277 gss_inquire_saslname_for_mech(OM_uint32
*minor_status
,
278 const gss_OID desired_mech
,
279 gss_buffer_t sasl_mech_name
,
280 gss_buffer_t mech_name
,
281 gss_buffer_t mech_description
)
285 _mg_buffer_zero(sasl_mech_name
);
286 _mg_buffer_zero(mech_name
);
287 _mg_buffer_zero(mech_description
);
292 if (desired_mech
== NULL
)
293 return GSS_S_BAD_MECH
;
295 major
= mo_value(desired_mech
, GSS_C_MA_SASL_MECH_NAME
, sasl_mech_name
);
296 if (major
== GSS_S_COMPLETE
) {
298 major
= mo_value(desired_mech
, GSS_C_MA_MECH_NAME
, mech_name
);
299 if (GSS_ERROR(major
))
302 major
= mo_value(desired_mech
, GSS_C_MA_MECH_DESCRIPTION
, mech_description
);
303 if (GSS_ERROR(major
))
307 if (GSS_ERROR(major
)) {
308 /* API-as-SPI compatibility */
309 major
= inquire_saslname_for_mech_compat(minor_status
,
316 if (GSS_ERROR(major
)) {
317 /* Algorithmically dervied SASL mechanism name */
319 gss_buffer_desc tmp
= { sizeof(buf
) - 1, buf
};
321 major
= make_sasl_name(minor_status
, desired_mech
, buf
);
322 if (GSS_ERROR(major
))
325 major
= _gss_copy_buffer(minor_status
, &tmp
, sasl_mech_name
);
326 if (GSS_ERROR(major
))
334 * Find a mech for a sasl name
336 * @param minor_status minor status code
337 * @param sasl_mech_name
340 * @return returns GSS_S_COMPLETE or an error code.
343 GSSAPI_LIB_FUNCTION OM_uint32 GSSAPI_LIB_CALL
344 gss_inquire_mech_for_saslname(OM_uint32
*minor_status
,
345 const gss_buffer_t sasl_mech_name
,
348 struct _gss_mech_switch
*m
;
349 gss_buffer_desc name
;
350 OM_uint32 major
, junk
;
357 HEIM_SLIST_FOREACH(m
, &_gss_mechs
, gm_link
) {
358 struct gss_mech_compat_desc_struct
*gmc
;
361 major
= mo_value(&m
->gm_mech_oid
, GSS_C_MA_SASL_MECH_NAME
, &name
);
362 if (major
== GSS_S_COMPLETE
&&
363 name
.length
== sasl_mech_name
->length
&&
364 memcmp(name
.value
, sasl_mech_name
->value
, name
.length
) == 0) {
365 gss_release_buffer(&junk
, &name
);
366 *mech_type
= &m
->gm_mech_oid
;
367 return GSS_S_COMPLETE
;
369 gss_release_buffer(&junk
, &name
);
371 if (GSS_ERROR(major
)) {
372 /* API-as-SPI compatibility */
373 gmc
= m
->gm_mech
.gm_compat
;
374 if (gmc
&& gmc
->gmc_inquire_mech_for_saslname
) {
375 major
= gmc
->gmc_inquire_mech_for_saslname(minor_status
,
378 if (major
== GSS_S_COMPLETE
)
379 return GSS_S_COMPLETE
;
383 if (GSS_ERROR(major
)) {
384 /* Algorithmically dervied SASL mechanism name */
385 if (sasl_mech_name
->length
== 16 &&
386 make_sasl_name(minor_status
, &m
->gm_mech_oid
, buf
) == GSS_S_COMPLETE
&&
387 memcmp(buf
, sasl_mech_name
->value
, 16) == 0) {
388 *mech_type
= &m
->gm_mech_oid
;
389 return GSS_S_COMPLETE
;
394 return GSS_S_BAD_MECH
;
398 * Test mechanism against indicated attributes using both Heimdal and
402 test_mech_attrs(gssapi_mech_interface mi
,
403 gss_const_OID_set mech_attrs
,
404 gss_const_OID_set against_attrs
,
410 if (against_attrs
== GSS_C_NO_OID_SET
)
413 for (n
= 0; n
< against_attrs
->count
; n
++) {
414 for (m
= 0; m
< mi
->gm_mo_num
; m
++) {
415 eq
= gss_oid_equal(mi
->gm_mo
[m
].option
,
416 &against_attrs
->elements
[n
]);
420 if (mech_attrs
!= GSS_C_NO_OID_SET
) {
421 for (m
= 0; m
< mech_attrs
->count
; m
++) {
422 eq
= gss_oid_equal(&mech_attrs
->elements
[m
],
423 &against_attrs
->elements
[n
]);
436 * Return set of mechanism that fullfill the criteria
438 * @param minor_status minor status code
439 * @param desired_mech_attrs
440 * @param except_mech_attrs
441 * @param critical_mech_attrs
442 * @param mechs returned mechs, free with gss_release_oid_set().
444 * @return returns GSS_S_COMPLETE or an error code.
447 GSSAPI_LIB_FUNCTION OM_uint32 GSSAPI_LIB_CALL
448 gss_indicate_mechs_by_attrs(OM_uint32
* minor_status
,
449 gss_const_OID_set desired_mech_attrs
,
450 gss_const_OID_set except_mech_attrs
,
451 gss_const_OID_set critical_mech_attrs
,
454 struct _gss_mech_switch
*ms
;
455 gss_OID_set mech_attrs
= GSS_C_NO_OID_SET
;
456 gss_OID_set known_mech_attrs
= GSS_C_NO_OID_SET
;
459 major
= gss_create_empty_oid_set(minor_status
, mechs
);
460 if (GSS_ERROR(major
))
465 HEIM_SLIST_FOREACH(ms
, &_gss_mechs
, gm_link
) {
466 gssapi_mech_interface mi
= &ms
->gm_mech
;
467 struct gss_mech_compat_desc_struct
*gmc
= mi
->gm_compat
;
470 if (gmc
&& gmc
->gmc_inquire_attrs_for_mech
) {
471 major
= gmc
->gmc_inquire_attrs_for_mech(minor_status
,
475 if (GSS_ERROR(major
))
480 * Test mechanism supports all of desired_mech_attrs;
481 * none of except_mech_attrs;
482 * and knows of all critical_mech_attrs.
484 if (test_mech_attrs(mi
, mech_attrs
, desired_mech_attrs
, 0) &&
485 test_mech_attrs(mi
, mech_attrs
, except_mech_attrs
, 1) &&
486 test_mech_attrs(mi
, known_mech_attrs
, critical_mech_attrs
, 0)) {
487 major
= gss_add_oid_set_member(minor_status
, &mi
->gm_mech_oid
, mechs
);
490 gss_release_oid_set(&tmp
, &mech_attrs
);
491 gss_release_oid_set(&tmp
, &known_mech_attrs
);
493 if (GSS_ERROR(major
))
501 * List support attributes for a mech and/or all mechanisms.
503 * @param minor_status minor status code
504 * @param mech given together with mech_attr will return the list of
505 * attributes for mechanism, can optionally be GSS_C_NO_OID.
506 * @param mech_attr see mech parameter, can optionally be NULL,
507 * release with gss_release_oid_set().
508 * @param known_mech_attrs all attributes for mechanisms supported,
509 * release with gss_release_oid_set().
514 GSSAPI_LIB_FUNCTION OM_uint32 GSSAPI_LIB_CALL
515 gss_inquire_attrs_for_mech(OM_uint32
* minor_status
,
517 gss_OID_set
*mech_attr
,
518 gss_OID_set
*known_mech_attrs
)
520 OM_uint32 major
, junk
;
522 if (known_mech_attrs
)
523 *known_mech_attrs
= GSS_C_NO_OID_SET
;
525 if (mech_attr
&& mech
) {
526 gssapi_mech_interface m
;
527 struct gss_mech_compat_desc_struct
*gmc
;
529 if ((m
= __gss_get_mechanism(mech
)) == NULL
) {
531 return GSS_S_BAD_MECH
;
536 if (gmc
&& gmc
->gmc_inquire_attrs_for_mech
) {
537 major
= gmc
->gmc_inquire_attrs_for_mech(minor_status
,
542 major
= gss_create_empty_oid_set(minor_status
, mech_attr
);
543 if (major
== GSS_S_COMPLETE
)
544 add_all_mo(m
, mech_attr
, GSS_MO_MA
);
546 if (GSS_ERROR(major
))
550 if (known_mech_attrs
) {
551 struct _gss_mech_switch
*m
;
553 if (*known_mech_attrs
== GSS_C_NO_OID_SET
) {
554 major
= gss_create_empty_oid_set(minor_status
, known_mech_attrs
);
555 if (GSS_ERROR(major
)) {
557 gss_release_oid_set(&junk
, mech_attr
);
564 HEIM_SLIST_FOREACH(m
, &_gss_mechs
, gm_link
)
565 add_all_mo(&m
->gm_mech
, known_mech_attrs
, GSS_MO_MA
);
569 return GSS_S_COMPLETE
;
573 * Return names and descriptions of mech attributes
575 * @param minor_status minor status code
581 * @return returns GSS_S_COMPLETE or an error code.
584 GSSAPI_LIB_FUNCTION OM_uint32 GSSAPI_LIB_CALL
585 gss_display_mech_attr(OM_uint32
* minor_status
,
586 gss_const_OID mech_attr
,
588 gss_buffer_t short_desc
,
589 gss_buffer_t long_desc
)
591 struct _gss_oid_name_table
*ma
= NULL
;
595 _mg_buffer_zero(name
);
596 _mg_buffer_zero(short_desc
);
597 _mg_buffer_zero(long_desc
);
602 for (n
= 0; ma
== NULL
&& _gss_ont_ma
[n
].oid
; n
++)
603 if (gss_oid_equal(mech_attr
, _gss_ont_ma
[n
].oid
))
604 ma
= &_gss_ont_ma
[n
];
607 return GSS_S_BAD_MECH_ATTR
;
611 bd
.value
= rk_UNCONST(ma
->name
);
612 bd
.length
= strlen(ma
->name
);
613 major
= _gss_copy_buffer(minor_status
, &bd
, name
);
614 if (major
!= GSS_S_COMPLETE
)
620 bd
.value
= rk_UNCONST(ma
->short_desc
);
621 bd
.length
= strlen(ma
->short_desc
);
622 major
= _gss_copy_buffer(minor_status
, &bd
, short_desc
);
623 if (major
!= GSS_S_COMPLETE
)
629 bd
.value
= rk_UNCONST(ma
->long_desc
);
630 bd
.length
= strlen(ma
->long_desc
);
631 major
= _gss_copy_buffer(minor_status
, &bd
, long_desc
);
632 if (major
!= GSS_S_COMPLETE
)
636 return GSS_S_COMPLETE
;