xref: /minix3/crypto/external/bsd/heimdal/dist/lib/krb5/rd_rep.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1 /*	$NetBSD: rd_rep.c,v 1.1.1.2 2014/04/24 12:45:51 pettai Exp $	*/
2 
3 /*
4  * Copyright (c) 1997 - 2001 Kungliga Tekniska Högskolan
5  * (Royal Institute of Technology, Stockholm, Sweden).
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * 3. Neither the name of the Institute nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  */
35 
36 #include "krb5_locl.h"
37 
38 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
krb5_rd_rep(krb5_context context,krb5_auth_context auth_context,const krb5_data * inbuf,krb5_ap_rep_enc_part ** repl)39 krb5_rd_rep(krb5_context context,
40 	    krb5_auth_context auth_context,
41 	    const krb5_data *inbuf,
42 	    krb5_ap_rep_enc_part **repl)
43 {
44     krb5_error_code ret;
45     AP_REP ap_rep;
46     size_t len;
47     krb5_data data;
48     krb5_crypto crypto;
49 
50     krb5_data_zero (&data);
51 
52     ret = decode_AP_REP(inbuf->data, inbuf->length, &ap_rep, &len);
53     if (ret)
54 	return ret;
55     if (ap_rep.pvno != 5) {
56 	ret = KRB5KRB_AP_ERR_BADVERSION;
57 	krb5_clear_error_message (context);
58 	goto out;
59     }
60     if (ap_rep.msg_type != krb_ap_rep) {
61 	ret = KRB5KRB_AP_ERR_MSG_TYPE;
62 	krb5_clear_error_message (context);
63 	goto out;
64     }
65 
66     ret = krb5_crypto_init(context, auth_context->keyblock, 0, &crypto);
67     if (ret)
68 	goto out;
69     ret = krb5_decrypt_EncryptedData (context,
70 				      crypto,
71 				      KRB5_KU_AP_REQ_ENC_PART,
72 				      &ap_rep.enc_part,
73 				      &data);
74     krb5_crypto_destroy(context, crypto);
75     if (ret)
76 	goto out;
77 
78     *repl = malloc(sizeof(**repl));
79     if (*repl == NULL) {
80 	ret = ENOMEM;
81 	krb5_set_error_message(context, ret, N_("malloc: out of memory", ""));
82 	goto out;
83     }
84     ret = decode_EncAPRepPart(data.data, data.length, *repl, &len);
85     if (ret) {
86 	krb5_set_error_message(context, ret, N_("Failed to decode EncAPRepPart", ""));
87 	return ret;
88     }
89 
90     if (auth_context->flags & KRB5_AUTH_CONTEXT_DO_TIME) {
91 	if ((*repl)->ctime != auth_context->authenticator->ctime ||
92 	    (*repl)->cusec != auth_context->authenticator->cusec)
93 	{
94 	    krb5_free_ap_rep_enc_part(context, *repl);
95 	    *repl = NULL;
96 	    ret = KRB5KRB_AP_ERR_MUT_FAIL;
97 	    krb5_clear_error_message (context);
98 	    goto out;
99 	}
100     }
101     if ((*repl)->seq_number)
102 	krb5_auth_con_setremoteseqnumber(context, auth_context,
103 					 *((*repl)->seq_number));
104     if ((*repl)->subkey)
105 	krb5_auth_con_setremotesubkey(context, auth_context, (*repl)->subkey);
106 
107  out:
108     krb5_data_free (&data);
109     free_AP_REP (&ap_rep);
110     return ret;
111 }
112 
113 KRB5_LIB_FUNCTION void KRB5_LIB_CALL
krb5_free_ap_rep_enc_part(krb5_context context,krb5_ap_rep_enc_part * val)114 krb5_free_ap_rep_enc_part (krb5_context context,
115 			   krb5_ap_rep_enc_part *val)
116 {
117     if (val) {
118 	free_EncAPRepPart (val);
119 	free (val);
120     }
121 }
122