Imported Upstream version 1.17
[platform/upstream/krb5.git] / src / lib / crypto / krb / string_to_key.c
1 /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 /*
3  * Copyright (C) 1998 by the FundsXpress, INC.
4  *
5  * All rights reserved.
6  *
7  * Export of this software from the United States of America may require
8  * a specific license from the United States Government.  It is the
9  * responsibility of any person or organization contemplating export to
10  * obtain such a license before exporting.
11  *
12  * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
13  * distribute this software and its documentation for any purpose and
14  * without fee is hereby granted, provided that the above copyright
15  * notice appear in all copies and that both that copyright notice and
16  * this permission notice appear in supporting documentation, and that
17  * the name of FundsXpress. not be used in advertising or publicity pertaining
18  * to distribution of the software without specific, written prior
19  * permission.  FundsXpress makes no representations about the suitability of
20  * this software for any purpose.  It is provided "as is" without express
21  * or implied warranty.
22  *
23  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
24  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
25  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
26  */
27
28 #include "crypto_int.h"
29
30 krb5_error_code KRB5_CALLCONV
31 krb5_c_string_to_key(krb5_context context, krb5_enctype enctype,
32                      const krb5_data *string, const krb5_data *salt,
33                      krb5_keyblock *key)
34 {
35     return krb5_c_string_to_key_with_params(context, enctype, string, salt,
36                                             NULL, key);
37 }
38
39 krb5_error_code KRB5_CALLCONV
40 krb5_c_string_to_key_with_params(krb5_context context, krb5_enctype enctype,
41                                  const krb5_data *string,
42                                  const krb5_data *salt,
43                                  const krb5_data *params, krb5_keyblock *key)
44 {
45     krb5_error_code ret;
46     krb5_data empty = empty_data();
47     const struct krb5_keytypes *ktp;
48     size_t keylength;
49
50     ktp = find_enctype(enctype);
51     if (ktp == NULL)
52         return KRB5_BAD_ENCTYPE;
53     keylength = ktp->enc->keylength;
54
55     /* For compatibility with past behavior, treat a null salt as empty. */
56     if (salt == NULL)
57         salt = ∅
58
59     /* Fail gracefully if someone is using the old AFS string-to-key hack. */
60     if (salt->length == SALT_TYPE_AFS_LENGTH)
61         return EINVAL;
62
63     key->contents = malloc(keylength);
64     if (key->contents == NULL)
65         return ENOMEM;
66
67     key->magic = KV5M_KEYBLOCK;
68     key->enctype = enctype;
69     key->length = keylength;
70
71     ret = (*ktp->str2key)(ktp, string, salt, params, key);
72     if (ret) {
73         zapfree(key->contents, keylength);
74         key->length = 0;
75         key->contents = NULL;
76     }
77
78     return ret;
79 }