Imported Upstream version 1.15.1
[platform/upstream/krb5.git] / src / plugins / preauth / pkinit / pkinit.h
1 /*
2  * COPYRIGHT (C) 2006,2007
3  * THE REGENTS OF THE UNIVERSITY OF MICHIGAN
4  * ALL RIGHTS RESERVED
5  *
6  * Permission is granted to use, copy, create derivative works
7  * and redistribute this software and such derivative works
8  * for any purpose, so long as the name of The University of
9  * Michigan is not used in any advertising or publicity
10  * pertaining to the use of distribution of this software
11  * without specific, written prior authorization.  If the
12  * above copyright notice or any other identification of the
13  * University of Michigan is included in any copy of any
14  * portion of this software, then the disclaimer below must
15  * also be included.
16  *
17  * THIS SOFTWARE IS PROVIDED AS IS, WITHOUT REPRESENTATION
18  * FROM THE UNIVERSITY OF MICHIGAN AS TO ITS FITNESS FOR ANY
19  * PURPOSE, AND WITHOUT WARRANTY BY THE UNIVERSITY OF
20  * MICHIGAN OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING
21  * WITHOUT LIMITATION THE IMPLIED WARRANTIES OF
22  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE
23  * REGENTS OF THE UNIVERSITY OF MICHIGAN SHALL NOT BE LIABLE
24  * FOR ANY DAMAGES, INCLUDING SPECIAL, INDIRECT, INCIDENTAL, OR
25  * CONSEQUENTIAL DAMAGES, WITH RESPECT TO ANY CLAIM ARISING
26  * OUT OF OR IN CONNECTION WITH THE USE OF THE SOFTWARE, EVEN
27  * IF IT HAS BEEN OR IS HEREAFTER ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGES.
29  */
30
31 #ifndef _PKINIT_H
32 #define _PKINIT_H
33
34 #include <k5-platform.h>
35 #include <krb5/krb5.h>
36 #include <krb5/preauth_plugin.h>
37 #include <k5-int-pkinit.h>
38 #include <profile.h>
39 #include "pkinit_accessor.h"
40 #include "pkinit_trace.h"
41
42 #ifndef WITHOUT_PKCS11
43 #include "pkcs11.h"
44
45 #define PKCS11_MODNAME "opensc-pkcs11.so"
46 #define PK_SIGLEN_GUESS 1000
47 #define PK_NOSLOT 999999
48 #endif
49
50 #define DH_PROTOCOL     1
51 #define RSA_PROTOCOL    2
52
53 #define TD_TRUSTED_CERTIFIERS 104
54 #define TD_INVALID_CERTIFICATES 105
55 #define TD_DH_PARAMETERS 109
56
57 #define PKINIT_CTX_MAGIC        0x05551212
58 #define PKINIT_REQ_CTX_MAGIC    0xdeadbeef
59 #define PKINIT_DEFERRED_ID_MAGIC    0x3ca20d21
60
61 #define PKINIT_DEFAULT_DH_MIN_BITS  2048
62 #define PKINIT_DH_MIN_CONFIG_BITS   1024
63
64 #define KRB5_CONF_KDCDEFAULTS                   "kdcdefaults"
65 #define KRB5_CONF_LIBDEFAULTS                   "libdefaults"
66 #define KRB5_CONF_REALMS                        "realms"
67 #define KRB5_CONF_PKINIT_ALLOW_UPN              "pkinit_allow_upn"
68 #define KRB5_CONF_PKINIT_ANCHORS                "pkinit_anchors"
69 #define KRB5_CONF_PKINIT_INDICATOR              "pkinit_indicator"
70 #define KRB5_CONF_PKINIT_CERT_MATCH             "pkinit_cert_match"
71 #define KRB5_CONF_PKINIT_DH_MIN_BITS            "pkinit_dh_min_bits"
72 #define KRB5_CONF_PKINIT_EKU_CHECKING           "pkinit_eku_checking"
73 #define KRB5_CONF_PKINIT_IDENTITIES             "pkinit_identities"
74 #define KRB5_CONF_PKINIT_IDENTITY               "pkinit_identity"
75 #define KRB5_CONF_PKINIT_KDC_HOSTNAME           "pkinit_kdc_hostname"
76 #define KRB5_CONF_PKINIT_KDC_OCSP               "pkinit_kdc_ocsp"
77 #define KRB5_CONF_PKINIT_POOL                   "pkinit_pool"
78 #define KRB5_CONF_PKINIT_REQUIRE_CRL_CHECKING   "pkinit_require_crl_checking"
79 #define KRB5_CONF_PKINIT_REVOKE                 "pkinit_revoke"
80
81 /* Make pkiDebug(fmt,...) print, or not.  */
82 #ifdef DEBUG
83 #define pkiDebug        printf
84 #else
85 /* Still evaluates for side effects.  */
86 static inline void pkiDebug (const char *fmt, ...) { }
87 /* This is better if the compiler doesn't inline variadic functions
88    well, but gcc will warn about "left-hand operand of comma
89    expression has no effect".  Still evaluates for side effects.  */
90 /* #define pkiDebug     (void) */
91 #endif
92
93 /* Solaris compiler doesn't grok __FUNCTION__
94  * hack for now.  Fix all the uses eventually. */
95 #define __FUNCTION__ __func__
96
97 /* Macros to deal with converting between various data types... */
98 #define PADATA_TO_KRB5DATA(pad, k5d) \
99     (k5d)->length = (pad)->length; (k5d)->data = (char *)(pad)->contents;
100 #define OCTETDATA_TO_KRB5DATA(octd, k5d) \
101     (k5d)->length = (octd)->length; (k5d)->data = (char *)(octd)->data;
102
103 extern const krb5_data dh_oid;
104
105 /*
106  * notes about crypto contexts:
107  *
108  * the basic idea is that there are crypto contexts that live at
109  * both the plugin level and request level. the identity context (that
110  * keeps info about your own certs and such) is separate because
111  * it is needed at different levels for the kdc and and the client.
112  * (the kdc's identity is at the plugin level, the client's identity
113  * information could change per-request.)
114  * the identity context is meant to have the entity's cert,
115  * a list of trusted and intermediate cas, a list of crls, and any
116  * pkcs11 information.  the req context is meant to have the
117  * received certificate and the DH related information. the plugin
118  * context is meant to have global crypto information, i.e., OIDs
119  * and constant DH parameter information.
120  */
121
122 /*
123  * plugin crypto context should keep plugin common information,
124  * eg., OIDs, known DHparams
125  */
126 typedef struct _pkinit_plg_crypto_context *pkinit_plg_crypto_context;
127
128 /*
129  * request crypto context should keep reqyest common information,
130  * eg., received credentials, DH parameters of this request
131  */
132 typedef struct _pkinit_req_crypto_context *pkinit_req_crypto_context;
133
134 /*
135  * identity context should keep information about credentials
136  * for the request, eg., my credentials, trusted ca certs,
137  * intermediate ca certs, crls, pkcs11 info
138  */
139 typedef struct _pkinit_identity_crypto_context *pkinit_identity_crypto_context;
140
141 /*
142  * this structure keeps information about the config options
143  */
144 typedef struct _pkinit_plg_opts {
145     int require_eku;        /* require EKU checking (default is true) */
146     int accept_secondary_eku;/* accept secondary EKU (default is false) */
147     int allow_upn;          /* allow UPN-SAN instead of pkinit-SAN */
148     int dh_or_rsa;          /* selects DH or RSA based pkinit */
149     int require_crl_checking; /* require CRL for a CA (default is false) */
150     int dh_min_bits;        /* minimum DH modulus size allowed */
151 } pkinit_plg_opts;
152
153 /*
154  * this structure keeps options used for a given request
155  */
156 typedef struct _pkinit_req_opts {
157     int require_eku;
158     int accept_secondary_eku;
159     int allow_upn;
160     int dh_or_rsa;
161     int require_crl_checking;
162     int dh_size;            /* initial request DH modulus size (default=1024) */
163     int require_hostname_match;
164 } pkinit_req_opts;
165
166 /*
167  * information about identity from config file or command line
168  */
169
170 typedef struct _pkinit_identity_opts {
171     char *identity;
172     char **identity_alt;
173     char **anchors;
174     char **intermediates;
175     char **crls;
176     char *ocsp;
177     int  idtype;
178     char *cert_filename;
179     char *key_filename;
180 #ifndef WITHOUT_PKCS11
181     char *p11_module_name;
182     CK_SLOT_ID slotid;
183     char *token_label;
184     char *cert_id_string;
185     char *cert_label;
186 #endif
187 } pkinit_identity_opts;
188
189
190 /*
191  * Client's plugin context
192  */
193 struct _pkinit_context {
194     int magic;
195     pkinit_plg_crypto_context cryptoctx;
196     pkinit_plg_opts *opts;
197     pkinit_identity_opts *idopts;
198 };
199 typedef struct _pkinit_context *pkinit_context;
200
201 /*
202  * Client's per-request context
203  */
204 struct _pkinit_req_context {
205     unsigned int magic;
206     pkinit_req_crypto_context cryptoctx;
207     pkinit_req_opts *opts;
208     pkinit_identity_crypto_context idctx;
209     pkinit_identity_opts *idopts;
210     int do_identity_matching;
211     krb5_preauthtype pa_type;
212     int rfc6112_kdc;
213     int identity_initialized;
214     int identity_prompted;
215     krb5_error_code identity_prompt_retval;
216 };
217 typedef struct _pkinit_req_context *pkinit_req_context;
218
219 /*
220  * KDC's (per-realm) plugin context
221  */
222 struct _pkinit_kdc_context {
223     int magic;
224     pkinit_plg_crypto_context cryptoctx;
225     pkinit_plg_opts *opts;
226     pkinit_identity_crypto_context idctx;
227     pkinit_identity_opts *idopts;
228     char *realmname;
229     unsigned int realmname_len;
230     char **auth_indicators;
231 };
232 typedef struct _pkinit_kdc_context *pkinit_kdc_context;
233
234 /*
235  * KDC's per-request context
236  */
237 struct _pkinit_kdc_req_context {
238     int magic;
239     pkinit_req_crypto_context cryptoctx;
240     krb5_auth_pack *rcv_auth_pack;
241     krb5_auth_pack_draft9 *rcv_auth_pack9;
242     krb5_preauthtype pa_type;
243 };
244 typedef struct _pkinit_kdc_req_context *pkinit_kdc_req_context;
245
246 /*
247  * Functions in pkinit_lib.c
248  */
249
250 krb5_error_code pkinit_init_req_opts(pkinit_req_opts **);
251 void pkinit_fini_req_opts(pkinit_req_opts *);
252
253 krb5_error_code pkinit_init_plg_opts(pkinit_plg_opts **);
254 void pkinit_fini_plg_opts(pkinit_plg_opts *);
255
256 krb5_error_code pkinit_init_identity_opts(pkinit_identity_opts **idopts);
257 void pkinit_fini_identity_opts(pkinit_identity_opts *idopts);
258 krb5_error_code pkinit_dup_identity_opts(pkinit_identity_opts *src_opts,
259                                          pkinit_identity_opts **dest_opts);
260
261 /*
262  * Functions in pkinit_identity.c
263  */
264 char * idtype2string(int idtype);
265 char * catype2string(int catype);
266
267 krb5_error_code pkinit_identity_initialize
268         (krb5_context context,                          /* IN */
269          pkinit_plg_crypto_context plg_cryptoctx,       /* IN */
270          pkinit_req_crypto_context req_cryptoctx,       /* IN */
271          pkinit_identity_opts *idopts,                  /* IN */
272          pkinit_identity_crypto_context id_cryptoctx,   /* IN/OUT */
273          krb5_clpreauth_callbacks cb,                   /* IN/OUT */
274          krb5_clpreauth_rock rock,                      /* IN/OUT */
275          krb5_principal princ);                         /* IN (optional) */
276
277 krb5_error_code pkinit_identity_prompt
278         (krb5_context context,                          /* IN */
279          pkinit_plg_crypto_context plg_cryptoctx,       /* IN */
280          pkinit_req_crypto_context req_cryptoctx,       /* IN */
281          pkinit_identity_opts *idopts,                  /* IN */
282          pkinit_identity_crypto_context id_cryptoctx,   /* IN/OUT */
283          krb5_clpreauth_callbacks cb,                   /* IN/OUT */
284          krb5_clpreauth_rock rock,                      /* IN/OUT */
285          int do_matching,                               /* IN */
286          krb5_principal princ);                         /* IN (optional) */
287
288 krb5_error_code pkinit_cert_matching
289         (krb5_context context,
290         pkinit_plg_crypto_context plg_cryptoctx,
291         pkinit_req_crypto_context req_cryptoctx,
292         pkinit_identity_crypto_context id_cryptoctx,
293         krb5_principal princ);
294
295 /*
296  * Client's list of identities for which it needs PINs or passwords
297  */
298 struct _pkinit_deferred_id {
299     int magic;
300     char *identity;
301     unsigned long ck_flags;
302     char *password;
303 };
304 typedef struct _pkinit_deferred_id *pkinit_deferred_id;
305
306 krb5_error_code pkinit_set_deferred_id
307         (pkinit_deferred_id **identities, const char *identity,
308          unsigned long ck_flags, const char *password);
309 const char * pkinit_find_deferred_id
310         (pkinit_deferred_id *identities, const char *identity);
311 unsigned long pkinit_get_deferred_id_flags
312         (pkinit_deferred_id *identities, const char *identity);
313 void pkinit_free_deferred_ids(pkinit_deferred_id *identities);
314
315 /*
316  * initialization and free functions
317  */
318 void init_krb5_pa_pk_as_req(krb5_pa_pk_as_req **in);
319 void init_krb5_pa_pk_as_req_draft9(krb5_pa_pk_as_req_draft9 **in);
320 void init_krb5_reply_key_pack(krb5_reply_key_pack **in);
321 void init_krb5_reply_key_pack_draft9(krb5_reply_key_pack_draft9 **in);
322
323 void init_krb5_pa_pk_as_rep(krb5_pa_pk_as_rep **in);
324 void init_krb5_pa_pk_as_rep_draft9(krb5_pa_pk_as_rep_draft9 **in);
325 void init_krb5_subject_pk_info(krb5_subject_pk_info **in);
326
327 void free_krb5_pa_pk_as_req(krb5_pa_pk_as_req **in);
328 void free_krb5_pa_pk_as_req_draft9(krb5_pa_pk_as_req_draft9 **in);
329 void free_krb5_reply_key_pack(krb5_reply_key_pack **in);
330 void free_krb5_reply_key_pack_draft9(krb5_reply_key_pack_draft9 **in);
331 void free_krb5_auth_pack(krb5_auth_pack **in);
332 void free_krb5_auth_pack_draft9(krb5_context, krb5_auth_pack_draft9 **in);
333 void free_krb5_pa_pk_as_rep(krb5_pa_pk_as_rep **in);
334 void free_krb5_pa_pk_as_rep_draft9(krb5_pa_pk_as_rep_draft9 **in);
335 void free_krb5_external_principal_identifier(krb5_external_principal_identifier ***in);
336 void free_krb5_algorithm_identifiers(krb5_algorithm_identifier ***in);
337 void free_krb5_algorithm_identifier(krb5_algorithm_identifier *in);
338 void free_krb5_kdc_dh_key_info(krb5_kdc_dh_key_info **in);
339 void free_krb5_subject_pk_info(krb5_subject_pk_info **in);
340 krb5_error_code pkinit_copy_krb5_data(krb5_data *dst, const krb5_data *src);
341
342
343 /*
344  * Functions in pkinit_profile.c
345  */
346 krb5_error_code pkinit_kdcdefault_strings
347         (krb5_context context, const char *realmname, const char *option,
348          char ***ret_value);
349 krb5_error_code pkinit_kdcdefault_string
350         (krb5_context context, const char *realmname, const char *option,
351          char **ret_value);
352 krb5_error_code pkinit_kdcdefault_boolean
353         (krb5_context context, const char *realmname, const char *option,
354          int default_value, int *ret_value);
355 krb5_error_code pkinit_kdcdefault_integer
356         (krb5_context context, const char *realmname, const char *option,
357          int default_value, int *ret_value);
358
359
360 krb5_error_code pkinit_libdefault_strings
361         (krb5_context context, const krb5_data *realm,
362          const char *option, char ***ret_value);
363 krb5_error_code pkinit_libdefault_string
364         (krb5_context context, const krb5_data *realm,
365          const char *option, char **ret_value);
366 krb5_error_code pkinit_libdefault_boolean
367         (krb5_context context, const krb5_data *realm, const char *option,
368          int default_value, int *ret_value);
369 krb5_error_code pkinit_libdefault_integer
370         (krb5_context context, const krb5_data *realm, const char *option,
371          int default_value, int *ret_value);
372
373 /*
374  * debugging functions
375  */
376 void print_buffer(const unsigned char *, unsigned int);
377 void print_buffer_bin(unsigned char *, unsigned int, char *);
378
379 /*
380  * Now get crypto function declarations
381  */
382 #include "pkinit_crypto.h"
383
384 #endif  /* _PKINIT_H */