2 Unix SMB/CIFS implementation.
4 Kerberos utility functions for GENSEC
6 Copyright (C) Andrew Bartlett <abartlet@samba.org> 2004-2005
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 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, see <http://www.gnu.org/licenses/>.
24 #include "system/kerberos.h"
25 #include "auth/kerberos/kerberos.h"
26 #include "auth/credentials/credentials.h"
27 #include "auth/credentials/credentials_proto.h"
28 #include "auth/credentials/credentials_krb5.h"
29 #include "auth/kerberos/kerberos_credentials.h"
31 #include "param/secrets.h"
33 struct principal_container {
34 struct smb_krb5_context *smb_krb5_context;
35 krb5_principal principal;
38 static krb5_error_code free_principal(struct principal_container *pc)
40 /* current heimdal - 0.6.3, which we need anyway, fixes segfaults here */
41 krb5_free_principal(pc->smb_krb5_context->krb5_context, pc->principal);
47 static krb5_error_code parse_principal(TALLOC_CTX *parent_ctx,
48 const char *princ_string,
49 struct smb_krb5_context *smb_krb5_context,
50 krb5_principal *princ,
51 const char **error_string)
54 struct principal_container *mem_ctx;
55 if (princ_string == NULL) {
60 ret = krb5_parse_name(smb_krb5_context->krb5_context,
64 (*error_string) = smb_get_krb5_error_message(smb_krb5_context->krb5_context, ret, parent_ctx);
68 mem_ctx = talloc(parent_ctx, struct principal_container);
70 (*error_string) = error_message(ENOMEM);
74 /* This song-and-dance effectivly puts the principal
75 * into talloc, so we can't loose it. */
76 mem_ctx->smb_krb5_context = talloc_reference(mem_ctx, smb_krb5_context);
77 mem_ctx->principal = *princ;
78 talloc_set_destructor(mem_ctx, free_principal);
82 static krb5_error_code principal_from_msg(TALLOC_CTX *parent_ctx,
83 struct ldb_message *msg,
84 struct smb_krb5_context *smb_krb5_context,
85 krb5_principal *principal,
87 const char **error_string)
91 const char *servicePrincipalName = ldb_msg_find_attr_as_string(msg, "servicePrincipalName", NULL);
92 const char *realm = ldb_msg_find_attr_as_string(msg, "realm", NULL);
93 const char *samAccountName = ldb_msg_find_attr_as_string(msg, "samAccountName", NULL);
94 struct principal_container *mem_ctx = talloc(parent_ctx, struct principal_container);
98 *error_string = "Cannot allocate mem_ctx";
102 tmp_ctx = talloc_new(mem_ctx);
104 talloc_free(mem_ctx);
105 *error_string = "Cannot allocate tmp_ctx";
110 *error_string = "Cannot have a kerberos secret in secrets.ldb without a realm";
114 upper_realm = strupper_talloc(tmp_ctx, realm);
116 talloc_free(mem_ctx);
117 *error_string = "Cannot allocate full upper case realm";
121 if (samAccountName) {
122 princ_string = talloc_asprintf(parent_ctx, "%s@%s", samAccountName, upper_realm);
124 *error_string = "Cannot allocate full samAccountName";
128 ret = krb5_make_principal(smb_krb5_context->krb5_context, principal, upper_realm, samAccountName,
130 } else if (servicePrincipalName) {
131 princ_string = talloc_asprintf(parent_ctx, "%s@%s", servicePrincipalName, upper_realm);
133 *error_string = "Cannot allocate full servicePrincipalName";
137 ret = krb5_parse_name(smb_krb5_context->krb5_context, princ_string, principal);
139 *error_string = "Cannot have a kerberos secret without a samAccountName or servicePrinipcalName!";
144 /* This song-and-dance effectivly puts the principal
145 * into talloc, so we can't loose it. */
146 mem_ctx->smb_krb5_context = talloc_reference(mem_ctx, smb_krb5_context);
147 mem_ctx->principal = *principal;
148 talloc_set_destructor(mem_ctx, free_principal);
150 *_princ_string = princ_string;
153 (*error_string) = smb_get_krb5_error_message(smb_krb5_context->krb5_context, ret, parent_ctx);
156 talloc_free(tmp_ctx);
160 static krb5_error_code salt_principal_from_msg(TALLOC_CTX *parent_ctx,
161 struct ldb_message *msg,
162 struct smb_krb5_context *smb_krb5_context,
163 krb5_principal *salt_princ,
164 const char **error_string)
166 const char *salt_principal = ldb_msg_find_attr_as_string(msg, "saltPrincipal", NULL);
167 const char *samAccountName = ldb_msg_find_attr_as_string(msg, "samAccountName", NULL);
168 const char *realm = ldb_msg_find_attr_as_string(msg, "realm", NULL);
169 if (salt_principal) {
170 return parse_principal(parent_ctx, salt_principal, smb_krb5_context, salt_princ, error_string);
171 } else if (samAccountName) {
173 char *machine_username;
179 struct principal_container *mem_ctx = talloc(parent_ctx, struct principal_container);
181 *error_string = "Cannot allocate mem_ctx";
185 tmp_ctx = talloc_new(mem_ctx);
187 talloc_free(mem_ctx);
188 *error_string = "Cannot allocate tmp_ctx";
193 *error_string = "Cannot have a kerberos secret in secrets.ldb without a realm";
197 machine_username = talloc_strdup(tmp_ctx, samAccountName);
198 if (!machine_username) {
199 talloc_free(mem_ctx);
200 *error_string = "Cannot duplicate samAccountName";
204 if (machine_username[strlen(machine_username)-1] == '$') {
205 machine_username[strlen(machine_username)-1] = '\0';
208 lower_realm = strlower_talloc(tmp_ctx, realm);
210 talloc_free(mem_ctx);
211 *error_string = "Cannot allocate to lower case realm";
215 upper_realm = strupper_talloc(tmp_ctx, realm);
217 talloc_free(mem_ctx);
218 *error_string = "Cannot allocate to upper case realm";
222 salt_body = talloc_asprintf(tmp_ctx, "%s.%s", machine_username,
224 talloc_free(lower_realm);
225 talloc_free(machine_username);
227 talloc_free(mem_ctx);
228 *error_string = "Cannot form salt principal body";
232 ret = krb5_make_principal(smb_krb5_context->krb5_context, salt_princ,
234 "host", salt_body, NULL);
236 /* This song-and-dance effectivly puts the principal
237 * into talloc, so we can't loose it. */
238 mem_ctx->smb_krb5_context = talloc_reference(mem_ctx, smb_krb5_context);
239 mem_ctx->principal = *salt_princ;
240 talloc_set_destructor(mem_ctx, free_principal);
242 (*error_string) = smb_get_krb5_error_message(smb_krb5_context->krb5_context, ret, parent_ctx);
243 talloc_free(tmp_ctx);
247 /* Catch the servicePrincipalName case */
248 return principal_from_msg(parent_ctx, msg, smb_krb5_context, salt_princ, NULL, error_string);
252 /* Obtain the principal set on this context. Requires a
253 * smb_krb5_context because we are doing krb5 principal parsing with
254 * the library routines. The returned princ is placed in the talloc
255 * system by means of a destructor (do *not* free). */
257 krb5_error_code principal_from_credentials(TALLOC_CTX *parent_ctx,
258 struct cli_credentials *credentials,
259 struct smb_krb5_context *smb_krb5_context,
260 krb5_principal *princ,
261 enum credentials_obtained *obtained,
262 const char **error_string)
265 const char *princ_string;
266 TALLOC_CTX *mem_ctx = talloc_new(parent_ctx);
268 (*error_string) = error_message(ENOMEM);
271 princ_string = cli_credentials_get_principal_and_obtained(credentials, mem_ctx, obtained);
273 (*error_string) = error_message(ENOMEM);
277 ret = parse_principal(parent_ctx, princ_string,
278 smb_krb5_context, princ, error_string);
279 talloc_free(mem_ctx);
283 /* Obtain the principal set on this context. Requires a
284 * smb_krb5_context because we are doing krb5 principal parsing with
285 * the library routines. The returned princ is placed in the talloc
286 * system by means of a destructor (do *not* free). */
288 krb5_error_code impersonate_principal_from_credentials(TALLOC_CTX *parent_ctx,
289 struct cli_credentials *credentials,
290 struct smb_krb5_context *smb_krb5_context,
291 krb5_principal *princ,
292 const char **error_string)
294 return parse_principal(parent_ctx, cli_credentials_get_impersonate_principal(credentials),
295 smb_krb5_context, princ, error_string);
299 * Return a freshly allocated ccache (destroyed by destructor on child
300 * of parent_ctx), for a given set of client credentials
303 krb5_error_code kinit_to_ccache(TALLOC_CTX *parent_ctx,
304 struct cli_credentials *credentials,
305 struct smb_krb5_context *smb_krb5_context,
307 enum credentials_obtained *obtained,
308 const char **error_string)
311 const char *password, *target_service;
313 krb5_principal princ;
314 krb5_principal impersonate_principal;
316 TALLOC_CTX *mem_ctx = talloc_new(parent_ctx);
317 krb5_get_init_creds_opt *krb_options;
320 (*error_string) = strerror(ENOMEM);
324 ret = principal_from_credentials(mem_ctx, credentials, smb_krb5_context, &princ, obtained, error_string);
326 talloc_free(mem_ctx);
330 ret = impersonate_principal_from_credentials(mem_ctx, credentials, smb_krb5_context, &impersonate_principal, error_string);
332 talloc_free(mem_ctx);
336 target_service = cli_credentials_get_target_service(credentials);
338 password = cli_credentials_get_password(credentials);
340 /* setup the krb5 options we want */
341 if ((ret = krb5_get_init_creds_opt_alloc(smb_krb5_context->krb5_context, &krb_options))) {
342 (*error_string) = talloc_asprintf(credentials, "krb5_get_init_creds_opt_alloc failed (%s)\n",
343 smb_get_krb5_error_message(smb_krb5_context->krb5_context,
345 talloc_free(mem_ctx);
349 /* get the defaults */
350 krb5_get_init_creds_opt_set_default_flags(smb_krb5_context->krb5_context, NULL, NULL, krb_options);
352 /* set if we want a forwardable ticket */
353 switch (cli_credentials_get_krb_forwardable(credentials)) {
354 case CRED_AUTO_KRB_FORWARDABLE:
356 case CRED_NO_KRB_FORWARDABLE:
357 krb5_get_init_creds_opt_set_forwardable(krb_options, FALSE);
359 case CRED_FORCE_KRB_FORWARDABLE:
360 krb5_get_init_creds_opt_set_forwardable(krb_options, TRUE);
367 ret = kerberos_kinit_password_cc(smb_krb5_context->krb5_context, ccache,
369 impersonate_principal, target_service,
372 } else if (impersonate_principal) {
373 (*error_string) = "INTERNAL error: Cannot impersonate principal with just a keyblock. A password must be specified in the credentials";
376 /* No password available, try to use a keyblock instead */
378 krb5_keyblock keyblock;
379 const struct samr_Password *mach_pwd;
380 mach_pwd = cli_credentials_get_nt_hash(credentials, mem_ctx);
382 talloc_free(mem_ctx);
383 (*error_string) = "kinit_to_ccache: No password available for kinit\n";
384 krb5_get_init_creds_opt_free(smb_krb5_context->krb5_context, krb_options);
387 ret = krb5_keyblock_init(smb_krb5_context->krb5_context,
388 ENCTYPE_ARCFOUR_HMAC,
389 mach_pwd->hash, sizeof(mach_pwd->hash),
393 ret = kerberos_kinit_keyblock_cc(smb_krb5_context->krb5_context, ccache,
395 target_service, krb_options,
397 krb5_free_keyblock_contents(smb_krb5_context->krb5_context, &keyblock);
401 if (ret == KRB5KRB_AP_ERR_SKEW || ret == KRB5_KDCREP_SKEW) {
402 /* Perhaps we have been given an invalid skew, so try again without it */
403 time_t t = time(NULL);
404 krb5_set_real_time(smb_krb5_context->krb5_context, t, 0);
406 /* not a skew problem */
411 krb5_get_init_creds_opt_free(smb_krb5_context->krb5_context, krb_options);
413 if (ret == KRB5KRB_AP_ERR_SKEW || ret == KRB5_KDCREP_SKEW) {
414 (*error_string) = talloc_asprintf(credentials, "kinit for %s failed (%s)\n",
415 cli_credentials_get_principal(credentials, mem_ctx),
416 smb_get_krb5_error_message(smb_krb5_context->krb5_context,
418 talloc_free(mem_ctx);
422 /* cope with ticket being in the future due to clock skew */
423 if ((unsigned)kdc_time > time(NULL)) {
424 time_t t = time(NULL);
425 int time_offset =(unsigned)kdc_time-t;
426 DEBUG(4,("Advancing clock by %d seconds to cope with clock skew\n", time_offset));
427 krb5_set_real_time(smb_krb5_context->krb5_context, t + time_offset + 1, 0);
430 if (ret == KRB5KDC_ERR_PREAUTH_FAILED && cli_credentials_wrong_password(credentials)) {
431 ret = kinit_to_ccache(parent_ctx,
439 (*error_string) = talloc_asprintf(credentials, "kinit for %s failed (%s)\n",
440 cli_credentials_get_principal(credentials, mem_ctx),
441 smb_get_krb5_error_message(smb_krb5_context->krb5_context,
443 talloc_free(mem_ctx);
446 talloc_free(mem_ctx);
450 static krb5_error_code free_keytab(struct keytab_container *ktc)
452 return krb5_kt_close(ktc->smb_krb5_context->krb5_context, ktc->keytab);
455 krb5_error_code smb_krb5_open_keytab(TALLOC_CTX *mem_ctx,
456 struct smb_krb5_context *smb_krb5_context,
457 const char *keytab_name, struct keytab_container **ktc)
461 ret = krb5_kt_resolve(smb_krb5_context->krb5_context, keytab_name, &keytab);
463 DEBUG(1,("failed to open krb5 keytab: %s\n",
464 smb_get_krb5_error_message(smb_krb5_context->krb5_context,
469 *ktc = talloc(mem_ctx, struct keytab_container);
474 (*ktc)->smb_krb5_context = talloc_reference(*ktc, smb_krb5_context);
475 (*ktc)->keytab = keytab;
476 talloc_set_destructor(*ktc, free_keytab);
481 static krb5_error_code keytab_add_keys(TALLOC_CTX *parent_ctx,
482 const char *princ_string,
483 krb5_principal princ,
484 krb5_principal salt_princ,
486 const char *password_s,
487 struct smb_krb5_context *smb_krb5_context,
488 krb5_enctype *enctypes,
494 TALLOC_CTX *mem_ctx = talloc_new(parent_ctx);
499 password.data = discard_const_p(char *, password_s);
500 password.length = strlen(password_s);
502 for (i=0; enctypes[i]; i++) {
503 krb5_keytab_entry entry;
504 ret = create_kerberos_key_from_string(smb_krb5_context->krb5_context,
505 salt_princ, &password, &entry.keyblock, enctypes[i]);
507 talloc_free(mem_ctx);
511 entry.principal = princ;
513 ret = krb5_kt_add_entry(smb_krb5_context->krb5_context, keytab, &entry);
515 DEBUG(1, ("Failed to add enctype %d entry for %s(kvno %d) to keytab: %s\n",
519 smb_get_krb5_error_message(smb_krb5_context->krb5_context,
521 talloc_free(mem_ctx);
522 krb5_free_keyblock_contents(smb_krb5_context->krb5_context, &entry.keyblock);
526 DEBUG(5, ("Added %s(kvno %d) to keytab (enctype %d)\n",
530 krb5_free_keyblock_contents(smb_krb5_context->krb5_context, &entry.keyblock);
532 talloc_free(mem_ctx);
536 static krb5_error_code create_keytab(TALLOC_CTX *parent_ctx,
537 struct ldb_message *msg,
538 struct smb_krb5_context *smb_krb5_context,
543 const char *password_s;
544 const char *old_secret;
546 uint32_t enctype_bitmap;
547 krb5_principal salt_princ;
548 krb5_principal princ;
550 krb5_enctype *enctypes;
551 const char *error_string;
553 TALLOC_CTX *mem_ctx = talloc_new(parent_ctx);
558 /* Get the principal we will store the new keytab entries under */
559 ret = principal_from_msg(mem_ctx, msg, smb_krb5_context, &princ, &princ_string, &error_string);
561 DEBUG(1,("create_keytab: getting krb5 principal from ldb message failed: %s\n", error_string));
562 talloc_free(mem_ctx);
566 /* The salt used to generate these entries may be different however, fetch that */
567 ret = salt_principal_from_msg(mem_ctx, msg,
569 &salt_princ, &error_string);
571 DEBUG(1,("create_keytab: makeing salt principal failed (%s)\n",
573 talloc_free(mem_ctx);
577 /* Finally, do the dance to get the password to put in the entry */
578 password_s = ldb_msg_find_attr_as_string(msg, "secret", NULL);
579 kvno = ldb_msg_find_attr_as_int(msg, "msDS-KeyVersionNumber", 0);
581 enctype_bitmap = (uint32_t)ldb_msg_find_attr_as_int(msg, "msDS-SupportedEncryptionTypes", ENC_ALL_TYPES);
583 ret = kerberos_enctype_bitmap_to_enctypes(mem_ctx, enctype_bitmap, &enctypes);
585 DEBUG(1,("create_keytab: generating list of encryption types failed (%s)\n",
586 smb_get_krb5_error_message(smb_krb5_context->krb5_context,
588 talloc_free(mem_ctx);
592 /* good, we actually have the real plaintext */
593 ret = keytab_add_keys(mem_ctx, princ_string, princ, salt_princ,
594 kvno, password_s, smb_krb5_context,
597 talloc_free(mem_ctx);
601 if (!add_old || kvno == 0) {
602 talloc_free(mem_ctx);
606 old_secret = ldb_msg_find_attr_as_string(msg, "priorSecret", NULL);
608 talloc_free(mem_ctx);
612 ret = keytab_add_keys(mem_ctx, princ_string, princ, salt_princ,
613 kvno - 1, old_secret, smb_krb5_context,
616 talloc_free(mem_ctx);
620 talloc_free(mem_ctx);
625 * Walk the keytab, looking for entries of this principal name, with KVNO other than current kvno -1.
627 * These entries are now stale, we only keep the current, and previous entries around.
629 * Inspired by the code in Samba3 for 'use kerberos keytab'.
633 static krb5_error_code remove_old_entries(TALLOC_CTX *parent_ctx,
634 struct ldb_message *msg,
635 bool delete_all_kvno,
636 struct smb_krb5_context *smb_krb5_context,
637 krb5_keytab keytab, bool *found_previous)
639 krb5_error_code ret, ret2;
640 krb5_kt_cursor cursor;
641 krb5_principal princ;
643 TALLOC_CTX *mem_ctx = talloc_new(parent_ctx);
645 const char *error_string;
651 *found_previous = false;
652 /* Get the principal we will store the new keytab entries under */
653 ret = principal_from_msg(mem_ctx, msg, smb_krb5_context, &princ, &princ_string, &error_string);
655 DEBUG(1,("remove_old_entries: getting krb5 principal from ldb message failed: %s\n", error_string));
656 talloc_free(mem_ctx);
660 kvno = ldb_msg_find_attr_as_int(msg, "msDS-KeyVersionNumber", 0);
662 /* for each entry in the keytab */
663 ret = krb5_kt_start_seq_get(smb_krb5_context->krb5_context, keytab, &cursor);
667 case HEIM_ERR_OPNOTSUPP:
670 /* no point enumerating if there isn't anything here */
671 talloc_free(mem_ctx);
674 DEBUG(1,("failed to open keytab for read of old entries: %s\n",
675 smb_get_krb5_error_message(smb_krb5_context->krb5_context,
677 talloc_free(mem_ctx);
682 krb5_keytab_entry entry;
683 ret = krb5_kt_next_entry(smb_krb5_context->krb5_context, keytab, &entry, &cursor);
687 /* if it matches our principal */
688 if (!krb5_kt_compare(smb_krb5_context->krb5_context, &entry, princ, 0, 0)) {
689 /* Free the entry, it wasn't the one we were looking for anyway */
690 krb5_kt_free_entry(smb_krb5_context->krb5_context, &entry);
694 /* delete it, if it is not kvno -1 */
695 if (entry.vno != (kvno - 1 )) {
696 /* Release the enumeration. We are going to
697 * have to start this from the top again,
698 * because deletes during enumeration may not
699 * always be consistant.
701 * Also, the enumeration locks a FILE: keytab
704 krb5_kt_end_seq_get(smb_krb5_context->krb5_context, keytab, &cursor);
706 ret = krb5_kt_remove_entry(smb_krb5_context->krb5_context, keytab, &entry);
707 krb5_kt_free_entry(smb_krb5_context->krb5_context, &entry);
709 /* Deleted: Restart from the top */
710 ret2 = krb5_kt_start_seq_get(smb_krb5_context->krb5_context, keytab, &cursor);
712 krb5_kt_free_entry(smb_krb5_context->krb5_context, &entry);
713 DEBUG(1,("failed to restart enumeration of keytab: %s\n",
714 smb_get_krb5_error_message(smb_krb5_context->krb5_context,
717 talloc_free(mem_ctx);
726 *found_previous = true;
729 /* Free the entry, we don't need it any more */
730 krb5_kt_free_entry(smb_krb5_context->krb5_context, &entry);
734 krb5_kt_end_seq_get(smb_krb5_context->krb5_context, keytab, &cursor);
744 DEBUG(1,("failed in deleting old entries for principal: %s: %s\n",
746 smb_get_krb5_error_message(smb_krb5_context->krb5_context,
749 talloc_free(mem_ctx);
753 krb5_error_code smb_krb5_update_keytab(struct smb_krb5_context *smb_krb5_context,
754 struct ldb_context *ldb,
755 struct ldb_message *msg,
756 bool delete_all_kvno)
760 TALLOC_CTX *mem_ctx = talloc_new(NULL);
761 struct keytab_container *keytab_container;
762 const char *keytab_name;
768 keytab_name = keytab_name_from_msg(mem_ctx, ldb, msg);
773 ret = smb_krb5_open_keytab(mem_ctx, smb_krb5_context, keytab_name, &keytab_container);
776 talloc_free(mem_ctx);
780 DEBUG(5, ("Opened keytab %s\n", keytab_name));
782 ret = remove_old_entries(mem_ctx, msg, delete_all_kvno,
783 smb_krb5_context, keytab_container->keytab, &found_previous);
785 talloc_free(mem_ctx);
789 if (!delete_all_kvno) {
790 /* Create a new keytab. If during the cleanout we found
791 * entires for kvno -1, then don't try and duplicate them.
792 * Otherwise, add kvno, and kvno -1 */
794 ret = create_keytab(mem_ctx, msg, smb_krb5_context,
795 keytab_container->keytab,
796 found_previous ? false : true);
798 talloc_free(mem_ctx);
802 krb5_error_code smb_krb5_create_memory_keytab(TALLOC_CTX *parent_ctx,
803 struct cli_credentials *machine_account,
804 struct smb_krb5_context *smb_krb5_context,
805 struct keytab_container **keytab_container)
808 TALLOC_CTX *mem_ctx = talloc_new(parent_ctx);
809 const char *rand_string;
810 const char *keytab_name;
811 struct ldb_message *msg;
816 *keytab_container = talloc(mem_ctx, struct keytab_container);
818 rand_string = generate_random_str(mem_ctx, 16);
820 talloc_free(mem_ctx);
824 keytab_name = talloc_asprintf(mem_ctx, "MEMORY:%s",
827 talloc_free(mem_ctx);
831 ret = smb_krb5_open_keytab(mem_ctx, smb_krb5_context, keytab_name, keytab_container);
836 msg = ldb_msg_new(mem_ctx);
838 talloc_free(mem_ctx);
841 ldb_msg_add_string(msg, "krb5Keytab", keytab_name);
842 ldb_msg_add_string(msg, "secret", cli_credentials_get_password(machine_account));
843 ldb_msg_add_string(msg, "samAccountName", cli_credentials_get_username(machine_account));
844 ldb_msg_add_string(msg, "realm", cli_credentials_get_realm(machine_account));
845 ldb_msg_add_fmt(msg, "msDS-KeyVersionNumber", "%d", (int)cli_credentials_get_kvno(machine_account));
847 ret = smb_krb5_update_keytab(smb_krb5_context, NULL, msg, false);
849 talloc_steal(parent_ctx, *keytab_container);
851 *keytab_container = NULL;
853 talloc_free(mem_ctx);
856 /* Translate between the IETF encryption type values and the Microsoft msDS-SupportedEncryptionTypes values */
857 uint32_t kerberos_enctype_to_bitmap(krb5_enctype enc_type_enum)
859 switch (enc_type_enum) {
860 case ENCTYPE_DES_CBC_CRC:
862 case ENCTYPE_DES_CBC_MD5:
864 case ENCTYPE_ARCFOUR_HMAC_MD5:
865 return ENC_RC4_HMAC_MD5;
866 case ENCTYPE_AES128_CTS_HMAC_SHA1_96:
867 return ENC_HMAC_SHA1_96_AES128;
868 case ENCTYPE_AES256_CTS_HMAC_SHA1_96:
869 return ENC_HMAC_SHA1_96_AES256;
875 /* Translate between the Microsoft msDS-SupportedEncryptionTypes values and the IETF encryption type values */
876 krb5_enctype kerberos_enctype_bitmap_to_enctype(uint32_t enctype_bitmap)
878 switch (enctype_bitmap) {
880 return ENCTYPE_DES_CBC_CRC;
882 return ENCTYPE_DES_CBC_MD5;
883 case ENC_RC4_HMAC_MD5:
884 return ENCTYPE_ARCFOUR_HMAC_MD5;
885 case ENC_HMAC_SHA1_96_AES128:
886 return ENCTYPE_AES128_CTS_HMAC_SHA1_96;
887 case ENC_HMAC_SHA1_96_AES256:
888 return ENCTYPE_AES256_CTS_HMAC_SHA1_96;
894 /* Return an array of krb5_enctype values */
895 krb5_error_code kerberos_enctype_bitmap_to_enctypes(TALLOC_CTX *mem_ctx, uint32_t enctype_bitmap, krb5_enctype **enctypes)
897 unsigned int i, j = 0;
898 *enctypes = talloc_zero_array(mem_ctx, krb5_enctype, (8*sizeof(enctype_bitmap))+1);
902 for (i=0; i<(8*sizeof(enctype_bitmap)); i++) {
903 uint32_t bit_value = (1 << i) & enctype_bitmap;
904 if (bit_value & enctype_bitmap) {
905 (*enctypes)[j] = kerberos_enctype_bitmap_to_enctype(bit_value);
906 if (!(*enctypes)[j]) {