2 Unix SMB/CIFS implementation.
3 kerberos authorization data (PAC) utility library
4 Copyright (C) Andrew Bartlett <abartlet@samba.org> 2011
5 Copyright (C) Simo Sorce 2010.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
24 #include "libcli/auth/krb5_wrap.h"
25 #include "auth/kerberos/pac_utils.h"
28 /* FIXME - need proper configure/waf test
29 * to determine if gss_mech_krb5 and friends
33 * These are not exported by Solaris -lkrb5
34 * Maybe move to libreplace somewhere?
36 static const gss_OID_desc krb5_gss_oid_array
[] = {
37 /* this is the official, rfc-specified OID */
38 { 9, "\052\206\110\206\367\022\001\002\002" },
39 /* this is the pre-RFC mech OID */
40 { 5, "\053\005\001\005\002" },
41 /* this is the unofficial, incorrect mech OID emitted by MS */
42 { 9, "\052\206\110\202\367\022\001\002\002" },
46 const gss_OID_desc
* const gss_mech_krb5
= krb5_gss_oid_array
+0;
47 const gss_OID_desc
* const gss_mech_krb5_old
= krb5_gss_oid_array
+1;
48 const gss_OID_desc
* const gss_mech_krb5_wrong
= krb5_gss_oid_array
+2;
51 #ifndef GSS_KRB5_INQ_SSPI_SESSION_KEY_OID
52 #define GSS_KRB5_INQ_SSPI_SESSION_KEY_OID_LENGTH 11
53 #define GSS_KRB5_INQ_SSPI_SESSION_KEY_OID "\x2a\x86\x48\x86\xf7\x12\x01\x02\x02\x05\x05"
56 gss_OID_desc gse_sesskey_inq_oid
= {
57 GSS_KRB5_INQ_SSPI_SESSION_KEY_OID_LENGTH
,
58 (void *)GSS_KRB5_INQ_SSPI_SESSION_KEY_OID
61 #ifndef GSS_KRB5_SESSION_KEY_ENCTYPE_OID
62 #define GSS_KRB5_SESSION_KEY_ENCTYPE_OID_LENGTH 10
63 #define GSS_KRB5_SESSION_KEY_ENCTYPE_OID "\x2a\x86\x48\x86\xf7\x12\x01\x02\x02\x04"
66 gss_OID_desc gse_sesskeytype_oid
= {
67 GSS_KRB5_SESSION_KEY_ENCTYPE_OID_LENGTH
,
68 (void *)GSS_KRB5_SESSION_KEY_ENCTYPE_OID
71 /* The Heimdal OID for getting the PAC */
72 #define EXTRACT_PAC_AUTHZ_DATA_FROM_SEC_CONTEXT_OID_LENGTH 8
73 /* EXTRACTION OID AUTHZ ID */
74 #define EXTRACT_PAC_AUTHZ_DATA_FROM_SEC_CONTEXT_OID "\x2a\x85\x70\x2b\x0d\x03" "\x81\x00"
76 NTSTATUS
gssapi_obtain_pac_blob(TALLOC_CTX
*mem_ctx
,
77 gss_ctx_id_t gssapi_context
,
78 gss_name_t gss_client_name
,
82 OM_uint32 gss_maj
, gss_min
;
83 #ifdef HAVE_GSS_GET_NAME_ATTRIBUTE
84 gss_buffer_desc pac_buffer
;
85 gss_buffer_desc pac_display_buffer
;
86 gss_buffer_desc pac_name
= {
87 .value
= discard_const("urn:mspac:"),
88 .length
= sizeof("urn:mspac:")-1
91 int authenticated
= false;
94 gss_maj
= gss_get_name_attribute(
95 &gss_min
, gss_client_name
, &pac_name
,
96 &authenticated
, &complete
,
97 &pac_buffer
, &pac_display_buffer
, &more
);
100 DEBUG(0, ("obtaining PAC via GSSAPI gss_get_name_attribute failed: %s\n",
101 gssapi_error_string(mem_ctx
, gss_maj
, gss_min
, gss_mech_krb5
)));
102 return NT_STATUS_ACCESS_DENIED
;
103 } else if (authenticated
&& complete
) {
104 /* The PAC blob is returned directly */
105 *pac_blob
= data_blob_talloc(mem_ctx
, pac_buffer
.value
,
108 if (!pac_blob
->data
) {
109 status
= NT_STATUS_NO_MEMORY
;
111 status
= NT_STATUS_OK
;
114 gss_maj
= gss_release_buffer(&gss_min
, &pac_buffer
);
115 gss_maj
= gss_release_buffer(&gss_min
, &pac_display_buffer
);
118 DEBUG(0, ("obtaining PAC via GSSAPI failed: authenticated: %s, complete: %s, more: %s\n",
119 authenticated
? "true" : "false",
120 complete
? "true" : "false",
121 more
? "true" : "false"));
122 return NT_STATUS_ACCESS_DENIED
;
125 #elif defined(HAVE_GSS_INQUIRE_SEC_CONTEXT_BY_OID)
126 gss_OID_desc pac_data_oid
= {
127 .elements
= discard_const(EXTRACT_PAC_AUTHZ_DATA_FROM_SEC_CONTEXT_OID
),
128 .length
= EXTRACT_PAC_AUTHZ_DATA_FROM_SEC_CONTEXT_OID_LENGTH
131 gss_buffer_set_t set
= GSS_C_NO_BUFFER_SET
;
133 /* If we didn't have the routine to get a verified, validated
134 * PAC (supplied only by MIT at the time of writing), then try
135 * with the Heimdal OID (fetches the PAC directly and always
137 gss_maj
= gss_inquire_sec_context_by_oid(
138 &gss_min
, gssapi_context
,
139 &pac_data_oid
, &set
);
141 /* First check for the error MIT gives for an unknown OID */
142 if (gss_maj
== GSS_S_UNAVAILABLE
) {
143 DEBUG(1, ("unable to obtain a PAC against this GSSAPI library. "
144 "GSSAPI secured connections are available only with Heimdal or MIT Kerberos >= 1.8\n"));
145 } else if (gss_maj
!= 0) {
146 DEBUG(2, ("obtaining PAC via GSSAPI gss_inqiure_sec_context_by_oid (Heimdal OID) failed: %s\n",
147 gssapi_error_string(mem_ctx
, gss_maj
, gss_min
, gss_mech_krb5
)));
149 if (set
== GSS_C_NO_BUFFER_SET
) {
150 DEBUG(0, ("gss_inquire_sec_context_by_oid returned unknown "
151 "data in results.\n"));
152 return NT_STATUS_INTERNAL_ERROR
;
155 /* The PAC blob is returned directly */
156 *pac_blob
= data_blob_talloc(mem_ctx
, set
->elements
[0].value
,
157 set
->elements
[0].length
);
158 if (!pac_blob
->data
) {
159 status
= NT_STATUS_NO_MEMORY
;
161 status
= NT_STATUS_OK
;
164 gss_maj
= gss_release_buffer_set(&gss_min
, &set
);
168 DEBUG(1, ("unable to obtain a PAC against this GSSAPI library. "
169 "GSSAPI secured connections are available only with Heimdal or MIT Kerberos >= 1.8\n"));
171 return NT_STATUS_ACCESS_DENIED
;
174 NTSTATUS
gssapi_get_session_key(TALLOC_CTX
*mem_ctx
,
175 gss_ctx_id_t gssapi_context
,
176 DATA_BLOB
*session_key
,
179 OM_uint32 gss_min
, gss_maj
;
180 gss_buffer_set_t set
= GSS_C_NO_BUFFER_SET
;
182 gss_maj
= gss_inquire_sec_context_by_oid(
183 &gss_min
, gssapi_context
,
184 &gse_sesskey_inq_oid
, &set
);
186 DEBUG(0, ("gss_inquire_sec_context_by_oid failed [%s]\n",
187 gssapi_error_string(mem_ctx
, gss_maj
, gss_min
, gss_mech_krb5
)));
188 return NT_STATUS_NO_USER_SESSION_KEY
;
191 if ((set
== GSS_C_NO_BUFFER_SET
) ||
193 #ifdef HAVE_GSSKRB5_GET_SUBKEY
194 krb5_keyblock
*subkey
;
195 gss_maj
= gsskrb5_get_subkey(&gss_min
,
199 DEBUG(1, ("NO session key for this mech\n"));
200 return NT_STATUS_NO_USER_SESSION_KEY
;
203 *session_key
= data_blob_talloc(mem_ctx
,
204 KRB5_KEY_DATA(subkey
), KRB5_KEY_LENGTH(subkey
));
207 *keytype
= KRB5_KEY_TYPE(subkey
);
209 krb5_free_keyblock(NULL
/* should be krb5_context */, subkey
);
212 DEBUG(0, ("gss_inquire_sec_context_by_oid didn't return any session key (and no alternative method available)\n"));
213 return NT_STATUS_NO_USER_SESSION_KEY
;
218 *session_key
= data_blob_talloc(mem_ctx
, set
->elements
[0].value
,
219 set
->elements
[0].length
);
226 if (set
->count
< 2) {
228 #ifdef HAVE_GSSKRB5_GET_SUBKEY
229 krb5_keyblock
*subkey
;
230 gss_maj
= gsskrb5_get_subkey(&gss_min
,
234 *keytype
= KRB5_KEY_TYPE(subkey
);
235 krb5_free_keyblock(NULL
/* should be krb5_context */, subkey
);
242 gss_maj
= gss_release_buffer_set(&gss_min
, &set
);
246 } else if (memcmp(set
->elements
[1].value
,
247 gse_sesskeytype_oid
.elements
,
248 gse_sesskeytype_oid
.length
) != 0) {
249 /* Perhaps a non-krb5 session key */
251 gss_maj
= gss_release_buffer_set(&gss_min
, &set
);
254 p
= set
->elements
[1].value
+ gse_sesskeytype_oid
.length
;
255 diflen
= set
->elements
[1].length
- gse_sesskeytype_oid
.length
;
257 gss_maj
= gss_release_buffer_set(&gss_min
, &set
);
258 return NT_STATUS_INVALID_PARAMETER
;
261 for (i
= 0; i
< diflen
; i
++) {
262 *keytype
= (*keytype
<< 7) | (p
[i
] & 0x7f);
263 if (i
+ 1 != diflen
&& (p
[i
] & 0x80) == 0) {
264 gss_maj
= gss_release_buffer_set(&gss_min
, &set
);
265 return NT_STATUS_INVALID_PARAMETER
;
270 gss_maj
= gss_release_buffer_set(&gss_min
, &set
);