Better error reporting through com_err
[mech_eap.git] / pseudo_random.c
1 /*
2  * Copyright (c) 2010, JANET(UK)
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  *
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * 3. Neither the name of JANET(UK) nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  */
32 /*
33  * Copyright 2009 by the Massachusetts Institute of Technology.
34  * All Rights Reserved.
35  *
36  * Export of this software from the United States of America may
37  *   require a specific license from the United States Government.
38  *   It is the responsibility of any person or organization contemplating
39  *   export to obtain such a license before exporting.
40  *
41  * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
42  * distribute this software and its documentation for any purpose and
43  * without fee is hereby granted, provided that the above copyright
44  * notice appear in all copies and that both that copyright notice and
45  * this permission notice appear in supporting documentation, and that
46  * the name of M.I.T. not be used in advertising or publicity pertaining
47  * to distribution of the software without specific, written prior
48  * permission.  Furthermore if you modify this software you must label
49  * your software as modified software and not distribute it in such a
50  * fashion that it might be confused with the original M.I.T. software.
51  * M.I.T. makes no representations about the suitability of
52  * this software for any purpose.  It is provided "as is" without express
53  * or implied warranty.
54  */
55
56 #include "gssapiP_eap.h"
57
58 OM_uint32
59 gss_pseudo_random(OM_uint32 *minor,
60                   gss_ctx_id_t ctx,
61                   int prf_key,
62                   const gss_buffer_t prf_in,
63                   ssize_t desired_output_len,
64                   gss_buffer_t prf_out)
65 {
66     krb5_error_code code;
67     int i;
68     OM_uint32 tmpMinor;
69     size_t prflen;
70     krb5_data t, ns;
71     unsigned char *p;
72     krb5_context krbContext;
73
74     prf_out->length = 0;
75     prf_out->value = NULL;
76
77     if (ctx == GSS_C_NO_CONTEXT) {
78         *minor = EINVAL;
79         return GSS_S_NO_CONTEXT;
80     }
81
82     *minor = 0;
83
84     GSSEAP_MUTEX_LOCK(&ctx->mutex);
85
86     if (!CTX_IS_ESTABLISHED(ctx)) {
87         GSSEAP_MUTEX_UNLOCK(&ctx->mutex);
88         *minor = GSSEAP_CONTEXT_INCOMPLETE;
89         return GSS_S_NO_CONTEXT;
90     }
91
92     GSSEAP_KRB_INIT(&krbContext);
93
94     t.length = 0;
95     t.data = NULL;
96
97     ns.length = 0;
98     ns.data = NULL;
99
100     if (prf_key != GSS_C_PRF_KEY_PARTIAL &&
101         prf_key != GSS_C_PRF_KEY_FULL) {
102         code = EINVAL;
103         goto cleanup;
104     }
105
106     prf_out->value = GSSEAP_MALLOC(desired_output_len);
107     if (prf_out->value == NULL) {
108         code = ENOMEM;
109         goto cleanup;
110     }
111     prf_out->length = desired_output_len;
112
113     code = krb5_c_prf_length(krbContext,
114                              ctx->encryptionType,
115                              &prflen);
116     if (code != 0)
117         goto cleanup;
118
119     ns.length = 4 + prf_in->length;
120     ns.data = GSSEAP_MALLOC(ns.length);
121     if (ns.data == NULL) {
122         code = ENOMEM;
123         goto cleanup;
124     }
125
126     t.length = prflen;
127     t.data = GSSEAP_MALLOC(t.length);
128     if (t.data == NULL) {
129         code = ENOMEM;
130         goto cleanup;
131     }
132
133     memcpy(ns.data + 4, prf_in->value, prf_in->length);
134     i = 0;
135     p = (unsigned char *)prf_out->value;
136     while (desired_output_len > 0) {
137         store_uint32_be(i, ns.data);
138
139         code = krb5_c_prf(krbContext, &ctx->rfc3961Key, &ns, &t);
140         if (code != 0)
141             goto cleanup;
142
143         memcpy(p, t.data, MIN(t.length, desired_output_len));
144
145         p += t.length;
146         desired_output_len -= t.length;
147         i++;
148     }
149
150 cleanup:
151     GSSEAP_MUTEX_UNLOCK(&ctx->mutex);
152
153     if (code != 0)
154         gss_release_buffer(&tmpMinor, prf_out);
155     krb5_free_data_contents(krbContext, &ns);
156     krb5_free_data_contents(krbContext, &t);
157
158     *minor = code;
159
160     return (code == 0) ? GSS_S_COMPLETE : GSS_S_FAILURE;
161 }