3cd1ebcae24a6e2284fbe3807852f2d8431457b2
[krb5.git] / src / lib / crypto / krb / keyed_checksum_types.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 "k5-int.h"
29 #include "etypes.h"
30 #include "cksumtypes.h"
31
32 static krb5_boolean
33 is_keyed_for(const struct krb5_cksumtypes *ctp,
34              const struct krb5_keytypes *ktp)
35 {
36     if (ctp->flags & CKSUM_UNKEYED)
37         return FALSE;
38     return (!ctp->enc || ktp->enc == ctp->enc);
39 }
40
41 krb5_error_code KRB5_CALLCONV
42 krb5_c_keyed_checksum_types(krb5_context context, krb5_enctype enctype,
43                             unsigned int *count, krb5_cksumtype **cksumtypes)
44 {
45     unsigned int i, c, nctypes;
46     krb5_cksumtype *ctypes;
47     const struct krb5_cksumtypes *ctp;
48     const struct krb5_keytypes *ktp;
49
50     *count = 0;
51     *cksumtypes = NULL;
52
53     ktp = find_enctype(enctype);
54     if (ktp == NULL)
55         return KRB5_BAD_ENCTYPE;
56
57     nctypes = 0;
58     for (i = 0; i < krb5int_cksumtypes_length; i++) {
59         ctp = &krb5int_cksumtypes_list[i];
60         if (is_keyed_for(ctp, ktp))
61             nctypes++;
62     }
63
64     ctypes = malloc(nctypes * sizeof(krb5_cksumtype));
65     if (ctypes == NULL)
66         return ENOMEM;
67
68     c = 0;
69     for (i = 0; i < krb5int_cksumtypes_length; i++) {
70         ctp = &krb5int_cksumtypes_list[i];
71         if (is_keyed_for(ctp, ktp))
72             ctypes[c++] = ctp->ctype;
73     }
74
75     *count = nctypes;
76     *cksumtypes = ctypes;
77     return 0;
78 }
79
80 void KRB5_CALLCONV
81 krb5_free_cksumtypes(krb5_context context, krb5_cksumtype *val)
82 {
83     free(val);
84 }