xref: /freebsd/crypto/heimdal/lib/krb5/rd_priv.c (revision a79b71281cd63ad7a6cc43a6d5673a2510b51630)
1 /*
2  * Copyright (c) 1997 - 2000 Kungliga Tekniska H�gskolan
3  * (Royal Institute of Technology, Stockholm, Sweden).
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * 3. Neither the name of the Institute nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33 
34 #include <krb5_locl.h>
35 
36 RCSID("$Id: rd_priv.c,v 1.23 2000/02/06 05:20:13 assar Exp $");
37 
38 krb5_error_code
39 krb5_rd_priv(krb5_context context,
40 	     krb5_auth_context auth_context,
41 	     const krb5_data *inbuf,
42 	     krb5_data *outbuf,
43 	     /*krb5_replay_data*/ void *outdata)
44 {
45   krb5_error_code ret;
46   KRB_PRIV priv;
47   EncKrbPrivPart part;
48   size_t len;
49   krb5_data plain;
50   krb5_keyblock *key;
51   krb5_crypto crypto;
52 
53   memset(&priv, 0, sizeof(priv));
54   ret = decode_KRB_PRIV (inbuf->data, inbuf->length, &priv, &len);
55   if (ret)
56       goto failure;
57   if (priv.pvno != 5) {
58       ret = KRB5KRB_AP_ERR_BADVERSION;
59       goto failure;
60   }
61   if (priv.msg_type != krb_priv) {
62       ret = KRB5KRB_AP_ERR_MSG_TYPE;
63       goto failure;
64   }
65 
66   /* XXX - Is this right? */
67 
68   if (auth_context->local_subkey)
69       key = auth_context->local_subkey;
70   else if (auth_context->remote_subkey)
71       key = auth_context->remote_subkey;
72   else
73       key = auth_context->keyblock;
74 
75   krb5_crypto_init(context, key, 0, &crypto);
76   ret = krb5_decrypt_EncryptedData(context,
77 				   crypto,
78 				   KRB5_KU_KRB_PRIV,
79 				   &priv.enc_part,
80 				   &plain);
81   krb5_crypto_destroy(context, crypto);
82   if (ret)
83       goto failure;
84 
85   ret = decode_EncKrbPrivPart (plain.data, plain.length, &part, &len);
86   krb5_data_free (&plain);
87   if (ret)
88       goto failure;
89 
90   /* check sender address */
91 
92   if (part.s_address
93       && auth_context->remote_address
94       && !krb5_address_compare (context,
95 				auth_context->remote_address,
96 				part.s_address)) {
97       ret = KRB5KRB_AP_ERR_BADADDR;
98       goto failure_part;
99   }
100 
101   /* check receiver address */
102 
103   if (part.r_address
104       && auth_context->local_address
105       && !krb5_address_compare (context,
106 				auth_context->local_address,
107 				part.r_address)) {
108       ret = KRB5KRB_AP_ERR_BADADDR;
109       goto failure_part;
110   }
111 
112   /* check timestamp */
113   if (auth_context->flags & KRB5_AUTH_CONTEXT_DO_TIME) {
114       krb5_timestamp sec;
115 
116       krb5_timeofday (context, &sec);
117       if (part.timestamp == NULL ||
118 	  part.usec      == NULL ||
119 	  abs(*part.timestamp - sec) > context->max_skew) {
120 	  ret = KRB5KRB_AP_ERR_SKEW;
121 	  goto failure_part;
122       }
123   }
124 
125   /* XXX - check replay cache */
126 
127   /* check sequence number */
128   if (auth_context->flags & KRB5_AUTH_CONTEXT_DO_SEQUENCE) {
129     if (part.seq_number == NULL ||
130 	*part.seq_number != ++auth_context->remote_seqnumber) {
131       ret = KRB5KRB_AP_ERR_BADORDER;
132       goto failure_part;
133     }
134   }
135 
136   ret = krb5_data_copy (outbuf, part.user_data.data, part.user_data.length);
137   if (ret)
138       goto failure_part;
139 
140   free_EncKrbPrivPart (&part);
141   free_KRB_PRIV (&priv);
142   return 0;
143 
144 failure_part:
145   free_EncKrbPrivPart (&part);
146 
147 failure:
148   free_KRB_PRIV (&priv);
149   return ret;
150 }
151