xref: /freebsd/crypto/krb5/src/lib/gssapi/krb5/util_cksum.c (revision 7f2fe78b9dd5f51c821d771b63d2e096f6fd49e9)
1*7f2fe78bSCy Schubert /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2*7f2fe78bSCy Schubert /*
3*7f2fe78bSCy Schubert  * Copyright 1993 by OpenVision Technologies, Inc.
4*7f2fe78bSCy Schubert  *
5*7f2fe78bSCy Schubert  * Permission to use, copy, modify, distribute, and sell this software
6*7f2fe78bSCy Schubert  * and its documentation for any purpose is hereby granted without fee,
7*7f2fe78bSCy Schubert  * provided that the above copyright notice appears in all copies and
8*7f2fe78bSCy Schubert  * that both that copyright notice and this permission notice appear in
9*7f2fe78bSCy Schubert  * supporting documentation, and that the name of OpenVision not be used
10*7f2fe78bSCy Schubert  * in advertising or publicity pertaining to distribution of the software
11*7f2fe78bSCy Schubert  * without specific, written prior permission. OpenVision makes no
12*7f2fe78bSCy Schubert  * representations about the suitability of this software for any
13*7f2fe78bSCy Schubert  * purpose.  It is provided "as is" without express or implied warranty.
14*7f2fe78bSCy Schubert  *
15*7f2fe78bSCy Schubert  * OPENVISION DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
16*7f2fe78bSCy Schubert  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
17*7f2fe78bSCy Schubert  * EVENT SHALL OPENVISION BE LIABLE FOR ANY SPECIAL, INDIRECT OR
18*7f2fe78bSCy Schubert  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
19*7f2fe78bSCy Schubert  * USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
20*7f2fe78bSCy Schubert  * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
21*7f2fe78bSCy Schubert  * PERFORMANCE OF THIS SOFTWARE.
22*7f2fe78bSCy Schubert  */
23*7f2fe78bSCy Schubert 
24*7f2fe78bSCy Schubert #include "gssapiP_krb5.h"
25*7f2fe78bSCy Schubert #ifdef HAVE_MEMORY_H
26*7f2fe78bSCy Schubert #include <memory.h>
27*7f2fe78bSCy Schubert #endif
28*7f2fe78bSCy Schubert 
29*7f2fe78bSCy Schubert /* Checksumming the channel bindings always uses plain MD5.  */
30*7f2fe78bSCy Schubert krb5_error_code
kg_checksum_channel_bindings(context,cb,cksum)31*7f2fe78bSCy Schubert kg_checksum_channel_bindings(context, cb, cksum)
32*7f2fe78bSCy Schubert     krb5_context context;
33*7f2fe78bSCy Schubert     gss_channel_bindings_t cb;
34*7f2fe78bSCy Schubert     krb5_checksum *cksum;
35*7f2fe78bSCy Schubert {
36*7f2fe78bSCy Schubert     struct k5buf buf;
37*7f2fe78bSCy Schubert     size_t sumlen;
38*7f2fe78bSCy Schubert     krb5_data plaind;
39*7f2fe78bSCy Schubert     krb5_error_code code;
40*7f2fe78bSCy Schubert 
41*7f2fe78bSCy Schubert     /* initialize the the cksum */
42*7f2fe78bSCy Schubert     code = krb5_c_checksum_length(context, CKSUMTYPE_RSA_MD5, &sumlen);
43*7f2fe78bSCy Schubert     if (code)
44*7f2fe78bSCy Schubert         return(code);
45*7f2fe78bSCy Schubert 
46*7f2fe78bSCy Schubert     cksum->checksum_type = CKSUMTYPE_RSA_MD5;
47*7f2fe78bSCy Schubert     cksum->length = sumlen;
48*7f2fe78bSCy Schubert     cksum->magic = KV5M_CHECKSUM;
49*7f2fe78bSCy Schubert 
50*7f2fe78bSCy Schubert     /* generate a buffer full of zeros if no cb specified */
51*7f2fe78bSCy Schubert 
52*7f2fe78bSCy Schubert     if (cb == GSS_C_NO_CHANNEL_BINDINGS) {
53*7f2fe78bSCy Schubert         if ((cksum->contents = (krb5_octet *) xmalloc(cksum->length)) == NULL) {
54*7f2fe78bSCy Schubert             return(ENOMEM);
55*7f2fe78bSCy Schubert         }
56*7f2fe78bSCy Schubert         memset(cksum->contents, '\0', cksum->length);
57*7f2fe78bSCy Schubert         return(0);
58*7f2fe78bSCy Schubert     }
59*7f2fe78bSCy Schubert 
60*7f2fe78bSCy Schubert     k5_buf_init_dynamic(&buf);
61*7f2fe78bSCy Schubert     k5_buf_add_uint32_le(&buf, cb->initiator_addrtype);
62*7f2fe78bSCy Schubert     k5_buf_add_uint32_le(&buf, cb->initiator_address.length);
63*7f2fe78bSCy Schubert     k5_buf_add_len(&buf, cb->initiator_address.value,
64*7f2fe78bSCy Schubert                    cb->initiator_address.length);
65*7f2fe78bSCy Schubert     k5_buf_add_uint32_le(&buf, cb->acceptor_addrtype);
66*7f2fe78bSCy Schubert     k5_buf_add_uint32_le(&buf, cb->acceptor_address.length);
67*7f2fe78bSCy Schubert     k5_buf_add_len(&buf, cb->acceptor_address.value,
68*7f2fe78bSCy Schubert                    cb->acceptor_address.length);
69*7f2fe78bSCy Schubert     k5_buf_add_uint32_le(&buf, cb->application_data.length);
70*7f2fe78bSCy Schubert     k5_buf_add_len(&buf, cb->application_data.value,
71*7f2fe78bSCy Schubert                    cb->application_data.length);
72*7f2fe78bSCy Schubert     code = k5_buf_status(&buf);
73*7f2fe78bSCy Schubert     if (code)
74*7f2fe78bSCy Schubert         return code;
75*7f2fe78bSCy Schubert 
76*7f2fe78bSCy Schubert     /* checksum the data */
77*7f2fe78bSCy Schubert 
78*7f2fe78bSCy Schubert     plaind = make_data(buf.data, buf.len);
79*7f2fe78bSCy Schubert     code = krb5_c_make_checksum(context, CKSUMTYPE_RSA_MD5, 0, 0,
80*7f2fe78bSCy Schubert                                 &plaind, cksum);
81*7f2fe78bSCy Schubert     k5_buf_free(&buf);
82*7f2fe78bSCy Schubert     return code;
83*7f2fe78bSCy Schubert }
84*7f2fe78bSCy Schubert 
85*7f2fe78bSCy Schubert krb5_error_code
kg_make_checksum_iov_v1(krb5_context context,krb5_cksumtype type,size_t cksum_len,krb5_key seq,krb5_key enc,krb5_keyusage sign_usage,gss_iov_buffer_desc * iov,int iov_count,int toktype,krb5_checksum * checksum)86*7f2fe78bSCy Schubert kg_make_checksum_iov_v1(krb5_context context,
87*7f2fe78bSCy Schubert                         krb5_cksumtype type,
88*7f2fe78bSCy Schubert                         size_t cksum_len,
89*7f2fe78bSCy Schubert                         krb5_key seq,
90*7f2fe78bSCy Schubert                         krb5_key enc,
91*7f2fe78bSCy Schubert                         krb5_keyusage sign_usage,
92*7f2fe78bSCy Schubert                         gss_iov_buffer_desc *iov,
93*7f2fe78bSCy Schubert                         int iov_count,
94*7f2fe78bSCy Schubert                         int toktype,
95*7f2fe78bSCy Schubert                         krb5_checksum *checksum)
96*7f2fe78bSCy Schubert {
97*7f2fe78bSCy Schubert     krb5_error_code code;
98*7f2fe78bSCy Schubert     gss_iov_buffer_desc *header;
99*7f2fe78bSCy Schubert     krb5_crypto_iov *kiov;
100*7f2fe78bSCy Schubert     int i = 0, j;
101*7f2fe78bSCy Schubert     size_t conf_len = 0, token_header_len;
102*7f2fe78bSCy Schubert 
103*7f2fe78bSCy Schubert     header = kg_locate_header_iov(iov, iov_count, toktype);
104*7f2fe78bSCy Schubert     assert(header != NULL);
105*7f2fe78bSCy Schubert 
106*7f2fe78bSCy Schubert     kiov = calloc(iov_count + 3, sizeof(krb5_crypto_iov));
107*7f2fe78bSCy Schubert     if (kiov == NULL)
108*7f2fe78bSCy Schubert         return ENOMEM;
109*7f2fe78bSCy Schubert 
110*7f2fe78bSCy Schubert     /* Checksum over ( Header | Confounder | Data | Pad ) */
111*7f2fe78bSCy Schubert     if (toktype == KG_TOK_WRAP_MSG)
112*7f2fe78bSCy Schubert         conf_len = kg_confounder_size(context, enc->keyblock.enctype);
113*7f2fe78bSCy Schubert 
114*7f2fe78bSCy Schubert     /* Checksum output */
115*7f2fe78bSCy Schubert     kiov[i].flags = KRB5_CRYPTO_TYPE_CHECKSUM;
116*7f2fe78bSCy Schubert     kiov[i].data.length = checksum->length;
117*7f2fe78bSCy Schubert     kiov[i].data.data = xmalloc(checksum->length);
118*7f2fe78bSCy Schubert     if (kiov[i].data.data == NULL) {
119*7f2fe78bSCy Schubert         xfree(kiov);
120*7f2fe78bSCy Schubert         return ENOMEM;
121*7f2fe78bSCy Schubert     }
122*7f2fe78bSCy Schubert     i++;
123*7f2fe78bSCy Schubert 
124*7f2fe78bSCy Schubert     /* Header | SND_SEQ | SGN_CKSUM | Confounder */
125*7f2fe78bSCy Schubert     token_header_len = 16 + cksum_len + conf_len;
126*7f2fe78bSCy Schubert 
127*7f2fe78bSCy Schubert     /* Header (calculate from end because of variable length ASN.1 header) */
128*7f2fe78bSCy Schubert     kiov[i].flags = KRB5_CRYPTO_TYPE_SIGN_ONLY;
129*7f2fe78bSCy Schubert     kiov[i].data.length = 8;
130*7f2fe78bSCy Schubert     kiov[i].data.data = (char *)header->buffer.value + header->buffer.length - token_header_len;
131*7f2fe78bSCy Schubert     i++;
132*7f2fe78bSCy Schubert 
133*7f2fe78bSCy Schubert     /* Confounder */
134*7f2fe78bSCy Schubert     if (toktype == KG_TOK_WRAP_MSG) {
135*7f2fe78bSCy Schubert         kiov[i].flags = KRB5_CRYPTO_TYPE_DATA;
136*7f2fe78bSCy Schubert         kiov[i].data.length = conf_len;
137*7f2fe78bSCy Schubert         kiov[i].data.data = (char *)header->buffer.value + header->buffer.length - conf_len;
138*7f2fe78bSCy Schubert         i++;
139*7f2fe78bSCy Schubert     }
140*7f2fe78bSCy Schubert 
141*7f2fe78bSCy Schubert     for (j = 0; j < iov_count; j++) {
142*7f2fe78bSCy Schubert         kiov[i].flags = kg_translate_flag_iov(iov[j].type);
143*7f2fe78bSCy Schubert         kiov[i].data.length = iov[j].buffer.length;
144*7f2fe78bSCy Schubert         kiov[i].data.data = (char *)iov[j].buffer.value;
145*7f2fe78bSCy Schubert         i++;
146*7f2fe78bSCy Schubert     }
147*7f2fe78bSCy Schubert 
148*7f2fe78bSCy Schubert     code = krb5_k_make_checksum_iov(context, type, seq, sign_usage, kiov, i);
149*7f2fe78bSCy Schubert     if (code == 0) {
150*7f2fe78bSCy Schubert         checksum->length = kiov[0].data.length;
151*7f2fe78bSCy Schubert         checksum->contents = (unsigned char *)kiov[0].data.data;
152*7f2fe78bSCy Schubert     } else
153*7f2fe78bSCy Schubert         free(kiov[0].data.data);
154*7f2fe78bSCy Schubert 
155*7f2fe78bSCy Schubert     xfree(kiov);
156*7f2fe78bSCy Schubert 
157*7f2fe78bSCy Schubert     return code;
158*7f2fe78bSCy Schubert }
159*7f2fe78bSCy Schubert 
160*7f2fe78bSCy Schubert static krb5_error_code
checksum_iov_v3(krb5_context context,krb5_cksumtype type,size_t rrc,krb5_key key,krb5_keyusage sign_usage,gss_iov_buffer_desc * iov,int iov_count,int toktype,krb5_boolean verify,krb5_boolean * valid)161*7f2fe78bSCy Schubert checksum_iov_v3(krb5_context context,
162*7f2fe78bSCy Schubert                 krb5_cksumtype type,
163*7f2fe78bSCy Schubert                 size_t rrc,
164*7f2fe78bSCy Schubert                 krb5_key key,
165*7f2fe78bSCy Schubert                 krb5_keyusage sign_usage,
166*7f2fe78bSCy Schubert                 gss_iov_buffer_desc *iov,
167*7f2fe78bSCy Schubert                 int iov_count,
168*7f2fe78bSCy Schubert                 int toktype,
169*7f2fe78bSCy Schubert                 krb5_boolean verify,
170*7f2fe78bSCy Schubert                 krb5_boolean *valid)
171*7f2fe78bSCy Schubert {
172*7f2fe78bSCy Schubert     krb5_error_code code;
173*7f2fe78bSCy Schubert     gss_iov_buffer_desc *header;
174*7f2fe78bSCy Schubert     gss_iov_buffer_desc *trailer;
175*7f2fe78bSCy Schubert     krb5_crypto_iov *kiov;
176*7f2fe78bSCy Schubert     size_t kiov_count;
177*7f2fe78bSCy Schubert     int i = 0, j;
178*7f2fe78bSCy Schubert     unsigned int k5_checksumlen;
179*7f2fe78bSCy Schubert 
180*7f2fe78bSCy Schubert     if (verify)
181*7f2fe78bSCy Schubert         *valid = FALSE;
182*7f2fe78bSCy Schubert 
183*7f2fe78bSCy Schubert     code = krb5_c_crypto_length(context, key->keyblock.enctype, KRB5_CRYPTO_TYPE_CHECKSUM, &k5_checksumlen);
184*7f2fe78bSCy Schubert     if (code != 0)
185*7f2fe78bSCy Schubert         return code;
186*7f2fe78bSCy Schubert 
187*7f2fe78bSCy Schubert     header = kg_locate_header_iov(iov, iov_count, toktype);
188*7f2fe78bSCy Schubert     assert(header != NULL);
189*7f2fe78bSCy Schubert 
190*7f2fe78bSCy Schubert     trailer = kg_locate_iov(iov, iov_count, GSS_IOV_BUFFER_TYPE_TRAILER);
191*7f2fe78bSCy Schubert     assert(rrc != 0 || trailer != NULL);
192*7f2fe78bSCy Schubert 
193*7f2fe78bSCy Schubert     if (trailer == NULL) {
194*7f2fe78bSCy Schubert         if (rrc != k5_checksumlen)
195*7f2fe78bSCy Schubert             return KRB5_BAD_MSIZE;
196*7f2fe78bSCy Schubert         if (header->buffer.length != 16 + k5_checksumlen)
197*7f2fe78bSCy Schubert             return KRB5_BAD_MSIZE;
198*7f2fe78bSCy Schubert     } else if (trailer->buffer.length != k5_checksumlen)
199*7f2fe78bSCy Schubert         return KRB5_BAD_MSIZE;
200*7f2fe78bSCy Schubert 
201*7f2fe78bSCy Schubert     kiov_count = 2 + iov_count;
202*7f2fe78bSCy Schubert     kiov = (krb5_crypto_iov *)xmalloc(kiov_count * sizeof(krb5_crypto_iov));
203*7f2fe78bSCy Schubert     if (kiov == NULL)
204*7f2fe78bSCy Schubert         return ENOMEM;
205*7f2fe78bSCy Schubert 
206*7f2fe78bSCy Schubert     /* Checksum over ( Data | Header ) */
207*7f2fe78bSCy Schubert 
208*7f2fe78bSCy Schubert     /* Data */
209*7f2fe78bSCy Schubert     for (j = 0; j < iov_count; j++) {
210*7f2fe78bSCy Schubert         kiov[i].flags = kg_translate_flag_iov(iov[j].type);
211*7f2fe78bSCy Schubert         kiov[i].data.length = iov[j].buffer.length;
212*7f2fe78bSCy Schubert         kiov[i].data.data = (char *)iov[j].buffer.value;
213*7f2fe78bSCy Schubert         i++;
214*7f2fe78bSCy Schubert     }
215*7f2fe78bSCy Schubert 
216*7f2fe78bSCy Schubert     /* Header */
217*7f2fe78bSCy Schubert     kiov[i].flags = KRB5_CRYPTO_TYPE_SIGN_ONLY;
218*7f2fe78bSCy Schubert     kiov[i].data.length = 16;
219*7f2fe78bSCy Schubert     kiov[i].data.data = (char *)header->buffer.value;
220*7f2fe78bSCy Schubert     i++;
221*7f2fe78bSCy Schubert 
222*7f2fe78bSCy Schubert     /* Checksum */
223*7f2fe78bSCy Schubert     kiov[i].flags = KRB5_CRYPTO_TYPE_CHECKSUM;
224*7f2fe78bSCy Schubert     if (trailer == NULL) {
225*7f2fe78bSCy Schubert         kiov[i].data.length = header->buffer.length - 16;
226*7f2fe78bSCy Schubert         kiov[i].data.data = (char *)header->buffer.value + 16;
227*7f2fe78bSCy Schubert     } else {
228*7f2fe78bSCy Schubert         kiov[i].data.length = trailer->buffer.length;
229*7f2fe78bSCy Schubert         kiov[i].data.data = (char *)trailer->buffer.value;
230*7f2fe78bSCy Schubert     }
231*7f2fe78bSCy Schubert     i++;
232*7f2fe78bSCy Schubert 
233*7f2fe78bSCy Schubert     if (verify)
234*7f2fe78bSCy Schubert         code = krb5_k_verify_checksum_iov(context, type, key, sign_usage, kiov, kiov_count, valid);
235*7f2fe78bSCy Schubert     else
236*7f2fe78bSCy Schubert         code = krb5_k_make_checksum_iov(context, type, key, sign_usage, kiov, kiov_count);
237*7f2fe78bSCy Schubert 
238*7f2fe78bSCy Schubert     xfree(kiov);
239*7f2fe78bSCy Schubert 
240*7f2fe78bSCy Schubert     return code;
241*7f2fe78bSCy Schubert }
242*7f2fe78bSCy Schubert 
243*7f2fe78bSCy Schubert krb5_error_code
kg_make_checksum_iov_v3(krb5_context context,krb5_cksumtype type,size_t rrc,krb5_key key,krb5_keyusage sign_usage,gss_iov_buffer_desc * iov,int iov_count,int toktype)244*7f2fe78bSCy Schubert kg_make_checksum_iov_v3(krb5_context context,
245*7f2fe78bSCy Schubert                         krb5_cksumtype type,
246*7f2fe78bSCy Schubert                         size_t rrc,
247*7f2fe78bSCy Schubert                         krb5_key key,
248*7f2fe78bSCy Schubert                         krb5_keyusage sign_usage,
249*7f2fe78bSCy Schubert                         gss_iov_buffer_desc *iov,
250*7f2fe78bSCy Schubert                         int iov_count,
251*7f2fe78bSCy Schubert                         int toktype)
252*7f2fe78bSCy Schubert {
253*7f2fe78bSCy Schubert     return checksum_iov_v3(context, type, rrc, key,
254*7f2fe78bSCy Schubert                            sign_usage, iov, iov_count, toktype, 0, NULL);
255*7f2fe78bSCy Schubert }
256*7f2fe78bSCy Schubert 
257*7f2fe78bSCy Schubert krb5_error_code
kg_verify_checksum_iov_v3(krb5_context context,krb5_cksumtype type,size_t rrc,krb5_key key,krb5_keyusage sign_usage,gss_iov_buffer_desc * iov,int iov_count,int toktype,krb5_boolean * valid)258*7f2fe78bSCy Schubert kg_verify_checksum_iov_v3(krb5_context context,
259*7f2fe78bSCy Schubert                           krb5_cksumtype type,
260*7f2fe78bSCy Schubert                           size_t rrc,
261*7f2fe78bSCy Schubert                           krb5_key key,
262*7f2fe78bSCy Schubert                           krb5_keyusage sign_usage,
263*7f2fe78bSCy Schubert                           gss_iov_buffer_desc *iov,
264*7f2fe78bSCy Schubert                           int iov_count,
265*7f2fe78bSCy Schubert                           int toktype,
266*7f2fe78bSCy Schubert                           krb5_boolean *valid)
267*7f2fe78bSCy Schubert {
268*7f2fe78bSCy Schubert     return checksum_iov_v3(context, type, rrc, key,
269*7f2fe78bSCy Schubert                            sign_usage, iov, iov_count, toktype, 1, valid);
270*7f2fe78bSCy Schubert }
271