xref: /freebsd/crypto/krb5/src/ccapi/lib/ccapi_credentials.c (revision 7f2fe78b9dd5f51c821d771b63d2e096f6fd49e9)
1 /* ccapi/lib/ccapi_credentials.c */
2 /*
3  * Copyright 2006 Massachusetts Institute of Technology.
4  * All Rights Reserved.
5  *
6  * Export of this software from the United States of America may
7  * require a specific license from the United States Government.
8  * It is the responsibility of any person or organization contemplating
9  * export to obtain such a license before exporting.
10  *
11  * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
12  * distribute this software and its documentation for any purpose and
13  * without fee is hereby granted, provided that the above copyright
14  * notice appear in all copies and that both that copyright notice and
15  * this permission notice appear in supporting documentation, and that
16  * the name of M.I.T. not be used in advertising or publicity pertaining
17  * to distribution of the software without specific, written prior
18  * permission.  Furthermore if you modify this software you must label
19  * your software as modified software and not distribute it in such a
20  * fashion that it might be confused with the original M.I.T. software.
21  * M.I.T. makes no representations about the suitability of
22  * this software for any purpose.  It is provided "as is" without express
23  * or implied warranty.
24  */
25 
26 #include "ccapi_credentials.h"
27 
28 #include "ccapi_string.h"
29 
30 /* ------------------------------------------------------------------------ */
31 
32 typedef struct cci_credentials_d {
33     cc_credentials_union *data;
34     cc_credentials_f *functions;
35 #if TARGET_OS_MAC
36     cc_credentials_f *vector_functions;
37 #endif
38     cci_identifier_t identifier;
39 } *cci_credentials_t;
40 
41 /* ------------------------------------------------------------------------ */
42 
43 struct cci_credentials_d cci_credentials_initializer = {
44     NULL,
45     NULL
46     VECTOR_FUNCTIONS_INITIALIZER,
47     NULL
48 };
49 
50 cc_credentials_f cci_credentials_f_initializer = {
51     ccapi_credentials_release,
52     ccapi_credentials_compare
53 };
54 
55 cc_credentials_union cci_credentials_union_initializer = {
56     0,
57     { NULL }
58 };
59 
60 /* ------------------------------------------------------------------------ */
61 
cci_credentials_read(cc_credentials_t * out_credentials,k5_ipc_stream in_stream)62 cc_int32 cci_credentials_read (cc_credentials_t *out_credentials,
63                                k5_ipc_stream      in_stream)
64 {
65     cc_int32 err = ccNoError;
66     cci_credentials_t credentials = NULL;
67 
68     if (!out_credentials) { err = cci_check_error (ccErrBadParam); }
69     if (!in_stream      ) { err = cci_check_error (ccErrBadParam); }
70 
71     if (!err) {
72         credentials = malloc (sizeof (*credentials));
73         if (credentials) {
74             *credentials = cci_credentials_initializer;
75         } else {
76             err = cci_check_error (ccErrNoMem);
77         }
78     }
79 
80     if (!err) {
81         credentials->functions = malloc (sizeof (*credentials->functions));
82         if (credentials->functions) {
83             *credentials->functions = cci_credentials_f_initializer;
84         } else {
85             err = cci_check_error (ccErrNoMem);
86         }
87     }
88 
89     if (!err) {
90         err = cci_identifier_read (&credentials->identifier, in_stream);
91     }
92 
93     if (!err) {
94         err = cci_credentials_union_read (&credentials->data, in_stream);
95     }
96 
97     if (!err) {
98         *out_credentials = (cc_credentials_t) credentials;
99         credentials = NULL; /* take ownership */
100     }
101 
102     if (credentials) { ccapi_credentials_release ((cc_credentials_t) credentials); }
103 
104     return cci_check_error (err);
105 }
106 
107 /* ------------------------------------------------------------------------ */
108 
cci_credentials_write(cc_credentials_t in_credentials,k5_ipc_stream in_stream)109 cc_int32 cci_credentials_write (cc_credentials_t in_credentials,
110                                 k5_ipc_stream     in_stream)
111 {
112     cc_int32 err = ccNoError;
113     cci_credentials_t credentials = (cci_credentials_t) in_credentials;
114 
115     if (!in_credentials) { err = cci_check_error (ccErrBadParam); }
116     if (!in_stream     ) { err = cci_check_error (ccErrBadParam); }
117 
118     if (!err) {
119         err = cci_identifier_write (credentials->identifier, in_stream);
120     }
121 
122     return cci_check_error (err);
123 }
124 
125 /* ------------------------------------------------------------------------ */
126 
ccapi_credentials_compare(cc_credentials_t in_credentials,cc_credentials_t in_compare_to_credentials,cc_uint32 * out_equal)127 cc_int32 ccapi_credentials_compare (cc_credentials_t  in_credentials,
128                                     cc_credentials_t  in_compare_to_credentials,
129                                     cc_uint32        *out_equal)
130 {
131     cc_int32 err = ccNoError;
132     cci_credentials_t credentials = (cci_credentials_t) in_credentials;
133     cci_credentials_t compare_to_credentials = (cci_credentials_t) in_compare_to_credentials;
134 
135     if (!in_credentials           ) { err = cci_check_error (ccErrBadParam); }
136     if (!in_compare_to_credentials) { err = cci_check_error (ccErrBadParam); }
137     if (!out_equal                ) { err = cci_check_error (ccErrBadParam); }
138 
139     if (!err) {
140         err = cci_identifier_compare (credentials->identifier,
141                                       compare_to_credentials->identifier,
142                                       out_equal);
143     }
144 
145     return cci_check_error (err);
146 }
147 
148 /* ------------------------------------------------------------------------ */
149 
ccapi_credentials_release(cc_credentials_t io_credentials)150 cc_int32 ccapi_credentials_release (cc_credentials_t io_credentials)
151 {
152     cc_int32 err = ccNoError;
153     cci_credentials_t credentials = (cci_credentials_t) io_credentials;
154 
155     if (!io_credentials) { err = ccErrBadParam; }
156 
157     if (!err) {
158         cci_credentials_union_release (credentials->data);
159         free ((char *) credentials->functions);
160         cci_identifier_release (credentials->identifier);
161         free (credentials);
162     }
163 
164     return err;
165 }
166