2 * Copyright (c) 1997 - 2011 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the Institute nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 struct hx509_certs_data
;
37 struct krb5_pk_identity
;
40 struct AlgorithmIdentifier
;
41 struct _krb5_krb_auth_data
;
42 typedef struct krb5_pk_init_ctx_data
*krb5_pk_init_ctx
;
43 struct krb5_dh_moduli
;
44 struct _krb5_key_data
;
45 struct _krb5_encryption_type
;
46 struct _krb5_key_type
;
47 #include <pkinit_asn1.h>
48 #include <krb5-private.h>
52 * free all the memory used by (len, keys)
56 hdb_free_keys(krb5_context context
, int len
, Key
*keys
)
60 for (i
= 0; i
< len
; i
++) {
63 if (keys
[i
].salt
!= NULL
) {
64 free_Salt(keys
[i
].salt
);
68 krb5_free_keyblock_contents(context
, &keys
[i
].key
);
74 * for each entry in `default_keys' try to parse it as a sequence
75 * of etype:salttype:salt, syntax of this if something like:
76 * [(des|des3|etype):](pw-salt|afs3)[:string], if etype is omitted it
77 * means all etypes, and if string is omitted is means the default
78 * string (for that principal). Additional special values:
81 * afs or afs3 == des:afs3-salt
84 static const krb5_enctype des_etypes
[] = {
85 KRB5_ENCTYPE_DES_CBC_MD5
,
86 KRB5_ENCTYPE_DES_CBC_MD4
,
87 KRB5_ENCTYPE_DES_CBC_CRC
90 static const krb5_enctype all_etypes
[] = {
91 KRB5_ENCTYPE_AES256_CTS_HMAC_SHA1_96
,
92 KRB5_ENCTYPE_DES3_CBC_SHA1
,
93 KRB5_ENCTYPE_ARCFOUR_HMAC_MD5
96 static krb5_error_code
97 parse_key_set(krb5_context context
, const char *key
,
98 krb5_enctype
**ret_enctypes
, size_t *ret_num_enctypes
,
99 krb5_salt
*salt
, krb5_principal principal
)
104 int i
, num_enctypes
= 0;
106 const krb5_enctype
*enctypes
= NULL
;
111 *ret_enctypes
= NULL
;
112 *ret_num_enctypes
= 0;
114 /* split p in a list of :-separated strings */
115 for(num_buf
= 0; num_buf
< 3; num_buf
++)
116 if(strsep_copy(&p
, ":", buf
[num_buf
], sizeof(buf
[num_buf
])) == -1)
119 salt
->saltvalue
.data
= NULL
;
120 salt
->saltvalue
.length
= 0;
122 for(i
= 0; i
< num_buf
; i
++) {
123 if(enctypes
== NULL
&& num_buf
> 1) {
124 /* this might be a etype specifier */
125 /* XXX there should be a string_to_etypes handling
126 special cases like `des' and `all' */
127 if(strcmp(buf
[i
], "des") == 0) {
128 enctypes
= des_etypes
;
129 num_enctypes
= sizeof(des_etypes
)/sizeof(des_etypes
[0]);
130 } else if(strcmp(buf
[i
], "des3") == 0) {
131 e
= KRB5_ENCTYPE_DES3_CBC_SHA1
;
135 ret
= krb5_string_to_enctype(context
, buf
[i
], &e
);
144 if(salt
->salttype
== 0) {
145 /* interpret string as a salt specifier, if no etype
146 is set, this sets default values */
147 /* XXX should perhaps use string_to_salttype, but that
149 if(strcmp(buf
[i
], "pw-salt") == 0) {
150 if(enctypes
== NULL
) {
151 enctypes
= all_etypes
;
152 num_enctypes
= sizeof(all_etypes
)/sizeof(all_etypes
[0]);
154 salt
->salttype
= KRB5_PW_SALT
;
155 } else if(strcmp(buf
[i
], "afs3-salt") == 0) {
156 if(enctypes
== NULL
) {
157 enctypes
= des_etypes
;
158 num_enctypes
= sizeof(des_etypes
)/sizeof(des_etypes
[0]);
160 salt
->salttype
= KRB5_AFS3_SALT
;
166 /* if there is a final string, use it as the string to
167 salt with, this is mostly useful with null salt for
168 v4 compat, and a cell name for afs compat */
169 salt
->saltvalue
.data
= strdup(buf
[i
]);
170 if (salt
->saltvalue
.data
== NULL
) {
171 krb5_set_error_message(context
, ENOMEM
, "malloc: out of memory");
174 salt
->saltvalue
.length
= strlen(buf
[i
]);
178 if(enctypes
== NULL
|| salt
->salttype
== 0) {
179 krb5_set_error_message(context
, EINVAL
, "bad value for default_keys `%s'", key
);
183 /* if no salt was specified make up default salt */
184 if(salt
->saltvalue
.data
== NULL
) {
185 if(salt
->salttype
== KRB5_PW_SALT
) {
186 ret
= krb5_get_pw_salt(context
, principal
, salt
);
189 } else if(salt
->salttype
== KRB5_AFS3_SALT
) {
190 krb5_const_realm realm
= krb5_principal_get_realm(context
, principal
);
191 salt
->saltvalue
.data
= strdup(realm
);
192 if(salt
->saltvalue
.data
== NULL
) {
193 krb5_set_error_message(context
, ENOMEM
,
194 "out of memory while "
195 "parsing salt specifiers");
198 strlwr(salt
->saltvalue
.data
);
199 salt
->saltvalue
.length
= strlen(realm
);
203 *ret_enctypes
= malloc(sizeof(enctypes
[0]) * num_enctypes
);
204 if (*ret_enctypes
== NULL
) {
205 krb5_free_salt(context
, *salt
);
206 krb5_set_error_message(context
, ENOMEM
, "malloc: out of memory");
209 memcpy(*ret_enctypes
, enctypes
, sizeof(enctypes
[0]) * num_enctypes
);
210 *ret_num_enctypes
= num_enctypes
;
216 * This function prunes an HDB entry's keys that are too old to have been used
217 * to mint still valid tickets (based on the entry's maximum ticket lifetime).
219 * @param context Context
220 * @param entry HDB entry
223 hdb_prune_keys(krb5_context context
, hdb_entry
*entry
)
226 HDB_Ext_KeySet
*keys
;
229 ext
= hdb_find_extension(entry
, choice_HDB_extension_data_hist_keys
);
232 keys
= &ext
->data
.u
.hist_keys
;
235 /* Optionally drop key history for keys older than now - max_life */
236 if (entry
->max_life
!= NULL
&& nelem
> 0
237 && krb5_config_get_bool_default(context
, NULL
, FALSE
,
238 "kadmin", "prune-key-history", NULL
)) {
240 time_t ceiling
= time(NULL
) - *entry
->max_life
;
241 time_t keep_time
= 0;
245 * Compute most recent key timestamp that predates the current time
246 * by at least the entry's maximum ticket lifetime.
248 for (i
= 0; i
< nelem
; ++i
) {
249 elem
= &keys
->val
[i
];
250 if (elem
->set_time
&& *elem
->set_time
< ceiling
251 && (keep_time
== 0 || *elem
->set_time
> keep_time
))
252 keep_time
= *elem
->set_time
;
255 /* Drop obsolete entries */
257 for (i
= 0; i
< nelem
; /* see below */) {
258 elem
= &keys
->val
[i
];
259 if (elem
->set_time
&& *elem
->set_time
< keep_time
) {
260 remove_HDB_Ext_KeySet(keys
, i
);
262 * Removing the i'th element shifts the tail down, continue
263 * at same index with reduced upper bound.
277 * This function adds an HDB entry's current keyset to the entry's key
278 * history. The current keyset is left alone; the caller is responsible
281 * @param context Context
282 * @param entry HDB entry
285 hdb_add_current_keys_to_history(krb5_context context
, hdb_entry
*entry
)
287 krb5_boolean replace
= FALSE
;
290 HDB_Ext_KeySet
*keys
;
291 hdb_keyset newkeyset
;
294 if (entry
->keys
.len
== 0)
295 return 0; /* nothing to do */
297 ext
= hdb_find_extension(entry
, choice_HDB_extension_data_hist_keys
);
300 ext
= calloc(1, sizeof (*ext
));
302 return krb5_enomem(context
);
304 ext
->data
.element
= choice_HDB_extension_data_hist_keys
;
306 keys
= &ext
->data
.u
.hist_keys
;
308 ext
->mandatory
= FALSE
;
311 * Copy in newest old keyset
313 ret
= hdb_entry_get_pw_change_time(entry
, &newtime
);
317 memset(&newkeyset
, 0, sizeof(newkeyset
));
318 newkeyset
.keys
= entry
->keys
;
319 newkeyset
.kvno
= entry
->kvno
;
320 newkeyset
.set_time
= &newtime
;
322 ret
= add_HDB_Ext_KeySet(keys
, &newkeyset
);
327 /* hdb_replace_extension() deep-copies ext; what a waste */
328 ret
= hdb_replace_extension(context
, entry
, ext
);
333 ret
= hdb_prune_keys(context
, entry
);
338 if (replace
&& ext
) {
339 free_HDB_extension(ext
);
346 * This function adds a key to an HDB entry's key history.
348 * @param context Context
349 * @param entry HDB entry
350 * @param kvno Key version number of the key to add to the history
351 * @param key The Key to add
354 hdb_add_history_key(krb5_context context
, hdb_entry
*entry
, krb5_kvno kvno
, Key
*key
)
358 HDB_Ext_KeySet
*hist_keys
;
363 memset(&keyset
, 0, sizeof (keyset
));
364 memset(&ext
, 0, sizeof (ext
));
366 extp
= hdb_find_extension(entry
, choice_HDB_extension_data_hist_keys
);
368 ext
.data
.element
= choice_HDB_extension_data_hist_keys
;
372 extp
->mandatory
= FALSE
;
373 hist_keys
= &extp
->data
.u
.hist_keys
;
375 for (i
= 0; i
< hist_keys
->len
; i
++) {
376 if (hist_keys
->val
[i
].kvno
== kvno
) {
377 ret
= add_Keys(&hist_keys
->val
[i
].keys
, key
);
383 ret
= add_Keys(&keyset
.keys
, key
);
386 ret
= add_HDB_Ext_KeySet(hist_keys
, &keyset
);
390 ret
= hdb_replace_extension(context
, entry
, &ext
);
396 free_hdb_keyset(&keyset
);
397 free_HDB_extension(&ext
);
403 * This function changes an hdb_entry's kvno, swapping the current key
404 * set with a historical keyset. If no historical keys are found then
405 * an error is returned (the caller can still set entry->kvno directly).
407 * @param context krb5_context
408 * @param new_kvno New kvno for the entry
409 * @param entry hdb_entry to modify
412 hdb_change_kvno(krb5_context context
, krb5_kvno new_kvno
, hdb_entry
*entry
)
417 HDB_Ext_KeySet
*hist_keys
;
422 if (entry
->kvno
== new_kvno
)
425 extp
= hdb_find_extension(entry
, choice_HDB_extension_data_hist_keys
);
427 memset(&ext
, 0, sizeof (ext
));
428 ext
.data
.element
= choice_HDB_extension_data_hist_keys
;
432 memset(&keyset
, 0, sizeof (keyset
));
433 hist_keys
= &extp
->data
.u
.hist_keys
;
434 for (i
= 0; i
< hist_keys
->len
; i
++) {
435 if (hist_keys
->val
[i
].kvno
== new_kvno
) {
437 ret
= copy_hdb_keyset(&hist_keys
->val
[i
], &keyset
);
440 ret
= remove_HDB_Ext_KeySet(hist_keys
, i
);
448 return HDB_ERR_KVNO_NOT_FOUND
;
450 ret
= hdb_add_current_keys_to_history(context
, entry
);
454 /* Note: we do nothing with keyset.set_time */
455 entry
->kvno
= new_kvno
;
456 entry
->keys
= keyset
.keys
; /* shortcut */
457 memset(&keyset
.keys
, 0, sizeof (keyset
.keys
));
460 free_hdb_keyset(&keyset
);
465 static krb5_error_code
466 add_enctype_to_key_set(Key
**key_set
, size_t *nkeyset
,
467 krb5_enctype enctype
, krb5_salt
*salt
)
472 memset(&key
, 0, sizeof(key
));
474 tmp
= realloc(*key_set
, (*nkeyset
+ 1) * sizeof((*key_set
)[0]));
480 key
.key
.keytype
= enctype
;
481 key
.key
.keyvalue
.length
= 0;
482 key
.key
.keyvalue
.data
= NULL
;
485 key
.salt
= calloc(1, sizeof(*key
.salt
));
486 if (key
.salt
== NULL
) {
491 key
.salt
->type
= salt
->salttype
;
492 krb5_data_zero (&key
.salt
->salt
);
494 ret
= krb5_data_copy(&key
.salt
->salt
,
495 salt
->saltvalue
.data
,
496 salt
->saltvalue
.length
);
504 (*key_set
)[*nkeyset
] = key
;
514 ks_tuple2str(krb5_context context
, int n_ks_tuple
,
515 krb5_key_salt_tuple
*ks_tuple
, char ***ks_tuple_strs
)
520 krb5_error_code rc
= KRB5_PROG_ETYPE_NOSUPP
;
522 *ks_tuple_strs
= NULL
;
526 if ((ksnames
= calloc(n_ks_tuple
+ 1, sizeof (*ksnames
))) == NULL
)
529 for (i
= 0; i
< n_ks_tuple
; i
++) {
530 if (krb5_enctype_to_string(context
, ks_tuple
[i
].ks_enctype
, &ename
))
532 if (krb5_salttype_to_string(context
, ks_tuple
[i
].ks_enctype
,
533 ks_tuple
[i
].ks_salttype
, &sname
))
536 if (asprintf(&ksnames
[i
], "%s:%s", ename
, sname
) == -1) {
547 *ks_tuple_strs
= ksnames
;
551 for (i
= 0; i
< n_ks_tuple
; i
++)
562 glob_rules_keys(krb5_context context
, krb5_const_principal principal
)
564 const krb5_config_binding
*list
;
565 krb5_principal pattern
;
568 list
= krb5_config_get_list(context
, NULL
, "kadmin",
569 "default_key_rules", NULL
);
574 if (list
->type
== krb5_config_string
) {
575 ret
= krb5_parse_name(context
, list
->name
, &pattern
);
577 ret
= krb5_principal_match(context
, principal
, pattern
);
578 krb5_free_principal(context
, pattern
);
580 return krb5_config_get_strings(context
, list
,
591 * NIST guidance in Section 5.1 of [SP800-132] requires that a portion
592 * of the salt of at least 128 bits shall be randomly generated.
594 static krb5_error_code
595 add_random_to_salt(krb5_context context
, krb5_salt
*in
, krb5_salt
*out
)
599 unsigned char random
[16];
603 krb5_generate_random_block(random
, sizeof(random
));
605 slen
= rk_base64_encode(random
, sizeof(random
), &s
);
609 ret
= krb5_data_alloc(&out
->saltvalue
, slen
+ in
->saltvalue
.length
);
615 p
= out
->saltvalue
.data
;
617 memcpy(&p
[slen
], in
->saltvalue
.data
, in
->saltvalue
.length
);
619 out
->salttype
= in
->salttype
;
626 * Generate the `key_set' from the [kadmin]default_keys statement. If
627 * `no_salt' is set, salt is not important (and will not be set) since
628 * it's random keys that is going to be created.
632 hdb_generate_key_set(krb5_context context
, krb5_principal principal
,
633 krb5_key_salt_tuple
*ks_tuple
, int n_ks_tuple
,
634 Key
**ret_key_set
, size_t *nkeyset
, int no_salt
)
636 char **ktypes
= NULL
;
641 char **ks_tuple_strs
;
642 char **config_ktypes
= NULL
;
643 static const char *default_keytypes
[] = {
644 "aes256-cts-hmac-sha1-96:pw-salt",
645 "des3-cbc-sha1:pw-salt",
646 "arcfour-hmac-md5:pw-salt",
650 if ((ret
= ks_tuple2str(context
, n_ks_tuple
, ks_tuple
, &ks_tuple_strs
)))
653 ktypes
= ks_tuple_strs
;
654 if (ktypes
== NULL
) {
655 ktypes
= glob_rules_keys(context
, principal
);
657 if (ktypes
== NULL
) {
658 config_ktypes
= krb5_config_get_strings(context
, NULL
, "kadmin",
659 "default_keys", NULL
);
660 ktypes
= config_ktypes
;
663 ktypes
= (char **)(intptr_t)default_keytypes
;
665 *ret_key_set
= key_set
= NULL
;
668 for(kp
= ktypes
; kp
&& *kp
; kp
++) {
671 krb5_enctype
*enctypes
;
676 if(strcmp(p
, "v5") == 0)
678 else if(strcmp(p
, "v4") == 0)
680 else if(strcmp(p
, "afs") == 0 || strcmp(p
, "afs3") == 0)
682 else if (strcmp(p
, "arcfour-hmac-md5") == 0)
683 p
= "arcfour-hmac-md5:pw-salt";
685 memset(&salt
, 0, sizeof(salt
));
687 ret
= parse_key_set(context
, p
,
688 &enctypes
, &num_enctypes
, &salt
, principal
);
690 krb5_warn(context
, ret
, "bad value for default_keys `%s'", *kp
);
695 for (i
= 0; i
< num_enctypes
; i
++) {
696 krb5_salt
*saltp
= no_salt
? NULL
: &salt
;
699 /* find duplicates */
700 for (j
= 0; j
< *nkeyset
; j
++) {
704 if (k
->key
.keytype
== enctypes
[i
]) {
707 if (k
->salt
== NULL
&& salt
.salttype
== KRB5_PW_SALT
)
709 if (k
->salt
->type
== salt
.salttype
&&
710 k
->salt
->salt
.length
== salt
.saltvalue
.length
&&
711 memcmp(k
->salt
->salt
.data
, salt
.saltvalue
.data
,
712 salt
.saltvalue
.length
) == 0)
716 /* not a duplicate, lets add it */
720 memset(&rsalt
, 0, sizeof(rsalt
));
722 /* prepend salt with randomness if required */
724 _krb5_enctype_requires_random_salt(context
, enctypes
[i
])) {
726 ret
= add_random_to_salt(context
, &salt
, &rsalt
);
730 ret
= add_enctype_to_key_set(&key_set
, nkeyset
, enctypes
[i
],
732 krb5_free_salt(context
, rsalt
);
736 krb5_free_salt(context
, salt
);
741 krb5_free_salt(context
, salt
);
744 *ret_key_set
= key_set
;
747 if (config_ktypes
!= NULL
)
748 krb5_config_free_strings(config_ktypes
);
750 for(kp
= ks_tuple_strs
; kp
&& *kp
; kp
++)
755 krb5_warn(context
, ret
,
756 "failed to parse the [kadmin]default_keys values");
758 for (i
= 0; i
< *nkeyset
; i
++)
759 free_Key(&key_set
[i
]);
761 } else if (*nkeyset
== 0) {
763 "failed to parse any of the [kadmin]default_keys values");
764 ret
= EINVAL
; /* XXX */
772 hdb_generate_key_set_password(krb5_context context
,
773 krb5_principal principal
,
774 const char *password
,
775 krb5_key_salt_tuple
*ks_tuple
, int n_ks_tuple
,
776 Key
**keys
, size_t *num_keys
)
781 ret
= hdb_generate_key_set(context
, principal
, ks_tuple
, n_ks_tuple
,
786 for (i
= 0; i
< (*num_keys
); i
++) {
788 Key
*key
= &(*keys
)[i
];
790 salt
.salttype
= key
->salt
->type
;
791 salt
.saltvalue
.length
= key
->salt
->salt
.length
;
792 salt
.saltvalue
.data
= key
->salt
->salt
.data
;
794 ret
= krb5_string_to_key_salt (context
,
804 hdb_free_keys (context
, *num_keys
, *keys
);