auth/kerberos: Move gse_get_session_key() to common code and use in gensec_gssapi
[ira/wip.git] / auth / kerberos / gssapi_pac.c
1 /*
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.
6
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.
11
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.
16
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/>.
19 */
20
21 #include "includes.h"
22 #ifdef HAVE_KRB5
23
24 #include "libcli/auth/krb5_wrap.h"
25 #include "lib/util/asn1.h"
26
27 #if 0
28 /* FIXME - need proper configure/waf test
29  * to determine if gss_mech_krb5 and friends
30  * exist. JRA.
31  */
32 /*
33  * These are not exported by Solaris -lkrb5
34  * Maybe move to libreplace somewhere?
35  */
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" },
43         { 0, 0 }
44 };
45
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;
49 #endif
50
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"
54 #endif
55
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
59 };
60
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"
64 #endif
65
66 gss_OID_desc gse_sesskeytype_oid = {
67         GSS_KRB5_SESSION_KEY_ENCTYPE_OID_LENGTH,
68         (void *)GSS_KRB5_SESSION_KEY_ENCTYPE_OID
69 };
70
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"
75
76 NTSTATUS gssapi_obtain_pac_blob(TALLOC_CTX *mem_ctx,
77                                 gss_ctx_id_t gssapi_context,
78                                 gss_name_t gss_client_name,
79                                 DATA_BLOB *pac_blob)
80 {
81         NTSTATUS status;
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
89         };
90         int more = -1;
91         int authenticated = false;
92         int complete = false;
93
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);
98
99         if (gss_maj != 0) {
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,
106                                             pac_buffer.length);
107
108                 if (!pac_blob->data) {
109                         status = NT_STATUS_NO_MEMORY;
110                 } else {
111                         status = NT_STATUS_OK;
112                 }
113
114                 gss_maj = gss_release_buffer(&gss_min, &pac_buffer);
115                 gss_maj = gss_release_buffer(&gss_min, &pac_display_buffer);
116                 return status;
117         } else {
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;
123         }
124
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
129         };
130
131         gss_buffer_set_t set = GSS_C_NO_BUFFER_SET;
132
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
136          * validates) */
137         gss_maj = gss_inquire_sec_context_by_oid(
138                                 &gss_min, gssapi_context,
139                                 &pac_data_oid, &set);
140
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)));
148         } else {
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;
153                 }
154
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;
160                 } else {
161                         status = NT_STATUS_OK;
162                 }
163
164                 gss_maj = gss_release_buffer_set(&gss_min, &set);
165                 return status;
166         }
167 #else
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"));
170 #endif
171         return NT_STATUS_ACCESS_DENIED;
172 }
173
174 NTSTATUS gssapi_get_session_key(TALLOC_CTX *mem_ctx,
175                                 gss_ctx_id_t gssapi_context,
176                                 DATA_BLOB *session_key, 
177                                 uint32_t *keytype)
178 {
179         OM_uint32 gss_min, gss_maj;
180         gss_buffer_set_t set = GSS_C_NO_BUFFER_SET;
181
182         gss_maj = gss_inquire_sec_context_by_oid(
183                                 &gss_min, gssapi_context,
184                                 &gse_sesskey_inq_oid, &set);
185         if (gss_maj) {
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;
189         }
190
191         if ((set == GSS_C_NO_BUFFER_SET) ||
192             (set->count == 0)) {
193 #ifdef HAVE_GSSKRB5_GET_SUBKEY
194                 krb5_keyblock *subkey;
195                 gss_maj = gsskrb5_get_subkey(&gss_min,
196                                              gssapi_context,
197                                              &subkey);
198                 if (gss_maj != 0) {
199                         DEBUG(1, ("NO session key for this mech\n"));
200                         return NT_STATUS_NO_USER_SESSION_KEY;
201                 }
202                 if (session_key) {
203                         *session_key = data_blob_talloc(mem_ctx,
204                                                         KRB5_KEY_DATA(subkey), KRB5_KEY_LENGTH(subkey));
205                 }
206                 if (keytype) {
207                         *keytype = KRB5_KEY_TYPE(subkey);
208                 }
209                 krb5_free_keyblock(NULL /* should be krb5_context */, subkey);
210                 return NT_STATUS_OK;
211 #else
212                 DEBUG(0, ("gss_inquire_sec_context_by_oid returned unknown "
213                           "OID for data in results:\n"));
214                 dump_data(1, (uint8_t *)set->elements[1].value,
215                              set->elements[1].length);
216                 return NT_STATUS_NO_USER_SESSION_KEY;
217 #endif
218         }
219
220         if (session_key) {
221                 *session_key = data_blob_talloc(mem_ctx, set->elements[0].value,
222                                                 set->elements[0].length);
223         }
224
225         if (keytype) {
226                 char *oid;
227                 char *p, *q = NULL;
228                 
229                 if (set->count < 2
230                     || memcmp(set->elements[1].value,
231                               gse_sesskeytype_oid.elements,
232                               gse_sesskeytype_oid.length) != 0) {
233                         /* Perhaps a non-krb5 session key */
234                         *keytype = 0;
235                         gss_maj = gss_release_buffer_set(&gss_min, &set);
236                         return NT_STATUS_OK;
237                 }
238                 if (!ber_read_OID_String(mem_ctx,
239                                          data_blob_const(set->elements[1].value,
240                                                          set->elements[1].length), &oid)) {
241                         TALLOC_FREE(oid);
242                         gss_maj = gss_release_buffer_set(&gss_min, &set);
243                         return NT_STATUS_INVALID_PARAMETER;
244                 }
245                 p = strrchr(oid, '.');
246                 if (!p) {
247                         TALLOC_FREE(oid);
248                         gss_maj = gss_release_buffer_set(&gss_min, &set);
249                         return NT_STATUS_INVALID_PARAMETER;
250                 } else {
251                         p++;
252                         *keytype = strtoul(p, &q, 10);
253                         if (q == NULL || *q != '\0') {
254                                 TALLOC_FREE(oid);
255                                 return NT_STATUS_INVALID_PARAMETER;
256                         }
257                 }
258                 TALLOC_FREE(oid);
259         }
260         
261         gss_maj = gss_release_buffer_set(&gss_min, &set);
262         return NT_STATUS_OK;
263 }
264
265 #endif