1 /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 /* lib/gssapi/krb5/prf.c */
3 /*
4 * Copyright 2009 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 #include "k5-int.h"
28 #include "gssapiP_krb5.h"
29
30 #ifndef MIN /* Usually found in <sys/param.h>. */
31 #define MIN(_a,_b) ((_a)<(_b)?(_a):(_b))
32 #endif
33
34 OM_uint32 KRB5_CALLCONV
krb5_gss_pseudo_random(OM_uint32 * minor_status,gss_ctx_id_t context,int prf_key,const gss_buffer_t prf_in,ssize_t desired_output_len,gss_buffer_t prf_out)35 krb5_gss_pseudo_random(OM_uint32 *minor_status,
36 gss_ctx_id_t context,
37 int prf_key,
38 const gss_buffer_t prf_in,
39 ssize_t desired_output_len,
40 gss_buffer_t prf_out)
41 {
42 krb5_error_code code;
43 krb5_key key = NULL;
44 krb5_gss_ctx_id_t ctx;
45 int i;
46 OM_uint32 minor;
47 size_t prflen;
48 krb5_data t, ns;
49 unsigned char *p;
50
51 prf_out->length = 0;
52 prf_out->value = NULL;
53
54 t.length = 0;
55 t.data = NULL;
56
57 ns.length = 0;
58 ns.data = NULL;
59
60 ctx = (krb5_gss_ctx_id_t)context;
61 if (ctx->terminated || !ctx->established) {
62 *minor_status = KG_CTX_INCOMPLETE;
63 return GSS_S_NO_CONTEXT;
64 }
65
66 switch (prf_key) {
67 case GSS_C_PRF_KEY_FULL:
68 if (ctx->have_acceptor_subkey) {
69 key = ctx->acceptor_subkey;
70 break;
71 }
72 /* fallthrough */
73 case GSS_C_PRF_KEY_PARTIAL:
74 key = ctx->subkey;
75 break;
76 default:
77 code = EINVAL;
78 goto cleanup;
79 }
80
81 if (key == NULL) {
82 code = EINVAL;
83 goto cleanup;
84 }
85
86 if (desired_output_len == 0)
87 return GSS_S_COMPLETE;
88
89 prf_out->value = gssalloc_malloc(desired_output_len);
90 if (prf_out->value == NULL) {
91 code = KG_INPUT_TOO_LONG;
92 goto cleanup;
93 }
94 prf_out->length = desired_output_len;
95
96 code = krb5_c_prf_length(ctx->k5_context,
97 krb5_k_key_enctype(ctx->k5_context, key),
98 &prflen);
99 if (code != 0)
100 goto cleanup;
101
102 ns.length = 4 + prf_in->length;
103 ns.data = k5alloc(ns.length, &code);
104 if (ns.data == NULL) {
105 code = KG_INPUT_TOO_LONG;
106 goto cleanup;
107 }
108
109 t.length = prflen;
110 t.data = k5alloc(t.length, &code);
111 if (t.data == NULL)
112 goto cleanup;
113
114 memcpy(ns.data + 4, prf_in->value, prf_in->length);
115 i = 0;
116 p = (unsigned char *)prf_out->value;
117 while (desired_output_len > 0) {
118 store_32_be(i, ns.data);
119
120 code = krb5_k_prf(ctx->k5_context, key, &ns, &t);
121 if (code != 0)
122 goto cleanup;
123
124 memcpy(p, t.data, MIN(t.length, desired_output_len));
125
126 p += t.length;
127 desired_output_len -= t.length;
128 i++;
129 }
130
131 cleanup:
132 if (code != 0)
133 gss_release_buffer(&minor, prf_out);
134 krb5_free_data_contents(ctx->k5_context, &ns);
135 krb5_free_data_contents(ctx->k5_context, &t);
136
137 *minor_status = (OM_uint32)code;
138 return (code == 0) ? GSS_S_COMPLETE : GSS_S_FAILURE;
139 }
140