Hide gak_fct interface and arguments in clpreauth
[krb5.git] / src / lib / krb5 / krb / preauth_ec.c
1 /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 /* lib/krb5/krb/preauth_ec.c - Encrypted Challenge clpreauth module */
3 /*
4  * Copyright (C) 2009, 2011 by the Massachusetts Institute of Technology.
5  * All rights reserved.
6  *
7  * Export of this software from the United States of America may
8  *   require a specific license from the United States Government.
9  *   It is the responsibility of any person or organization contemplating
10  *   export to 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 M.I.T. not be used in advertising or publicity pertaining
18  * to distribution of the software without specific, written prior
19  * permission.  Furthermore if you modify this software you must label
20  * your software as modified software and not distribute it in such a
21  * fashion that it might be confused with the original M.I.T. software.
22  * M.I.T. makes no representations about the suitability of
23  * this software for any purpose.  It is provided "as is" without express
24  * or implied warranty.
25  */
26
27 /*
28  * Implement Encrypted Challenge fast factor from
29  * draft-ietf-krb-wg-preauth-framework
30  */
31
32 #include <k5-int.h>
33 #include <krb5/preauth_plugin.h>
34 #include "int-proto.h"
35
36 static int
37 ec_flags(krb5_context context, krb5_preauthtype pa_type)
38 {
39     return PA_REAL;
40 }
41
42 static krb5_error_code
43 ec_process(krb5_context context, krb5_clpreauth_moddata moddata,
44            krb5_clpreauth_modreq modreq, krb5_get_init_creds_opt *opt,
45            krb5_clpreauth_callbacks cb,
46            krb5_clpreauth_rock rock, krb5_kdc_req *request,
47            krb5_data *encoded_request_body,
48            krb5_data *encoded_previous_request, krb5_pa_data *padata,
49            krb5_prompter_fct prompter, void *prompter_data,
50            krb5_pa_data ***out_padata)
51 {
52     krb5_error_code retval = 0;
53     krb5_keyblock *challenge_key = NULL, *armor_key, *as_key;
54
55     armor_key = cb->fast_armor(context, rock);
56     retval = cb->get_as_key(context, rock, &as_key);
57     if (retval == 0 && padata->length) {
58         krb5_enc_data *enc = NULL;
59         krb5_data scratch;
60         scratch.length = padata->length;
61         scratch.data = (char *) padata->contents;
62         retval = krb5_c_fx_cf2_simple(context,armor_key, "kdcchallengearmor",
63                                       as_key, "challengelongterm",
64                                       &challenge_key);
65         if (retval == 0)
66             retval = decode_krb5_enc_data(&scratch, &enc);
67         scratch.data = NULL;
68         if (retval == 0) {
69             scratch.data = malloc(enc->ciphertext.length);
70             scratch.length = enc->ciphertext.length;
71             if (scratch.data == NULL)
72                 retval = ENOMEM;
73         }
74         if (retval == 0)
75             retval = krb5_c_decrypt(context, challenge_key,
76                                     KRB5_KEYUSAGE_ENC_CHALLENGE_KDC, NULL,
77                                     enc, &scratch);
78         /*
79          * Per draft 11 of the preauth framework, the client MAY but is not
80          * required to actually check the timestamp from the KDC other than to
81          * confirm it decrypts. This code does not perform that check.
82          */
83         if (scratch.data)
84             krb5_free_data_contents(context, &scratch);
85         /* If we had a callback to assert that the KDC is verified, we would
86          * call it here. */
87         if (enc)
88             krb5_free_enc_data(context, enc);
89     } else if (retval == 0) { /*No padata; we send*/
90         krb5_enc_data enc;
91         krb5_pa_data **pa = NULL;
92         krb5_data *encoded_ts = NULL;
93         krb5_pa_enc_ts ts;
94         enc.ciphertext.data = NULL;
95         retval = krb5_us_timeofday(context, &ts.patimestamp, &ts.pausec);
96         if (retval == 0)
97             retval = encode_krb5_pa_enc_ts(&ts, &encoded_ts);
98         if (retval == 0)
99             retval = krb5_c_fx_cf2_simple(context,
100                                           armor_key, "clientchallengearmor",
101                                           as_key, "challengelongterm",
102                                           &challenge_key);
103         if (retval == 0)
104             retval = krb5_encrypt_helper(context, challenge_key,
105                                          KRB5_KEYUSAGE_ENC_CHALLENGE_CLIENT,
106                                          encoded_ts, &enc);
107         if (encoded_ts)
108             krb5_free_data(context, encoded_ts);
109         encoded_ts = NULL;
110         if (retval == 0) {
111             retval = encode_krb5_enc_data(&enc, &encoded_ts);
112             krb5_free_data_contents(context, &enc.ciphertext);
113         }
114         if (retval == 0) {
115             pa = calloc(2, sizeof(krb5_pa_data *));
116             if (pa == NULL)
117                 retval = ENOMEM;
118         }
119         if (retval == 0) {
120             pa[0] = calloc(1, sizeof(krb5_pa_data));
121             if (pa[0] == NULL)
122                 retval = ENOMEM;
123         }
124         if (retval == 0) {
125             pa[0]->length = encoded_ts->length;
126             pa[0]->contents = (unsigned char *) encoded_ts->data;
127             pa[0]->pa_type = KRB5_PADATA_ENCRYPTED_CHALLENGE;
128             encoded_ts->data = NULL;
129             *out_padata = pa;
130             pa = NULL;
131         }
132         free(pa);
133         krb5_free_data(context, encoded_ts);
134     }
135     if (challenge_key)
136         krb5_free_keyblock(context, challenge_key);
137     return retval;
138 }
139
140
141 static krb5_preauthtype ec_types[] = {
142     KRB5_PADATA_ENCRYPTED_CHALLENGE, 0};
143
144 krb5_error_code
145 clpreauth_encrypted_challenge_initvt(krb5_context context, int maj_ver,
146                                      int min_ver, krb5_plugin_vtable vtable)
147 {
148     krb5_clpreauth_vtable vt;
149
150     if (maj_ver != 1)
151         return KRB5_PLUGIN_VER_NOTSUPP;
152     vt = (krb5_clpreauth_vtable)vtable;
153     vt->name = "encrypted_challenge";
154     vt->pa_type_list = ec_types;
155     vt->flags = ec_flags;
156     vt->process = ec_process;
157     return 0;
158 }