xref: /freebsd/crypto/heimdal/lib/krb5/mk_priv.c (revision 5e9cd1ae3e10592ed70e7575551cba1bbab04d84)
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: mk_priv.c,v 1.28 2000/08/18 06:48:07 assar Exp $");
37 
38 /*
39  *
40  */
41 
42 krb5_error_code
43 krb5_mk_priv(krb5_context context,
44 	     krb5_auth_context auth_context,
45 	     const krb5_data *userdata,
46 	     krb5_data *outbuf,
47 	     /*krb5_replay_data*/ void *outdata)
48 {
49   krb5_error_code ret;
50   KRB_PRIV s;
51   EncKrbPrivPart part;
52   u_char *buf;
53   size_t buf_size;
54   size_t len;
55   u_int32_t tmp_seq;
56   krb5_keyblock *key;
57   int32_t sec, usec;
58   KerberosTime sec2;
59   int usec2;
60   krb5_crypto crypto;
61 
62   /* XXX - Is this right? */
63 
64   if (auth_context->local_subkey)
65       key = auth_context->local_subkey;
66   else if (auth_context->remote_subkey)
67       key = auth_context->remote_subkey;
68   else
69       key = auth_context->keyblock;
70 
71   krb5_us_timeofday (context, &sec, &usec);
72 
73   part.user_data = *userdata;
74   sec2           = sec;
75   part.timestamp = &sec2;
76   usec2          = usec;
77   part.usec      = &usec2;
78   if (auth_context->flags & KRB5_AUTH_CONTEXT_DO_SEQUENCE) {
79     tmp_seq = auth_context->local_seqnumber;
80     part.seq_number = &tmp_seq;
81   } else {
82     part.seq_number = NULL;
83   }
84 
85   part.s_address = auth_context->local_address;
86   part.r_address = auth_context->remote_address;
87 
88   buf_size = 1024;
89   buf = malloc (buf_size);
90   if (buf == NULL)
91       return ENOMEM;
92 
93   krb5_data_zero (&s.enc_part.cipher);
94 
95   do {
96       ret = encode_EncKrbPrivPart (buf + buf_size - 1, buf_size,
97 				   &part, &len);
98       if (ret) {
99 	  if (ret == ASN1_OVERFLOW) {
100 	      u_char *tmp;
101 
102 	      buf_size *= 2;
103 	      tmp = realloc (buf, buf_size);
104 	      if (tmp == NULL) {
105 		  ret = ENOMEM;
106 		  goto fail;
107 	      }
108 	      buf = tmp;
109 	  } else {
110 	      goto fail;
111 	  }
112       }
113   } while(ret == ASN1_OVERFLOW);
114 
115   s.pvno = 5;
116   s.msg_type = krb_priv;
117   s.enc_part.etype = key->keytype;
118   s.enc_part.kvno = NULL;
119 
120   ret = krb5_crypto_init(context, key, 0, &crypto);
121   if (ret) {
122       free (buf);
123       return ret;
124   }
125   ret = krb5_encrypt (context,
126 		      crypto,
127 		      KRB5_KU_KRB_PRIV,
128 		      buf + buf_size - len,
129 		      len,
130 		      &s.enc_part.cipher);
131   krb5_crypto_destroy(context, crypto);
132   if (ret) {
133       free(buf);
134       return ret;
135   }
136 
137   do {
138       ret = encode_KRB_PRIV (buf + buf_size - 1, buf_size, &s, &len);
139 
140       if (ret){
141 	  if (ret == ASN1_OVERFLOW) {
142 	      u_char *tmp;
143 
144 	      buf_size *= 2;
145 	      tmp = realloc (buf, buf_size);
146 	      if (tmp == NULL) {
147 		  ret = ENOMEM;
148 		  goto fail;
149 	      }
150 	      buf = tmp;
151 	  } else {
152 	      goto fail;
153 	  }
154       }
155   } while(ret == ASN1_OVERFLOW);
156   krb5_data_free (&s.enc_part.cipher);
157 
158   outbuf->length = len;
159   outbuf->data   = malloc (len);
160   if (outbuf->data == NULL) {
161       free(buf);
162       return ENOMEM;
163   }
164   memcpy (outbuf->data, buf + buf_size - len, len);
165   free (buf);
166   if (auth_context->flags & KRB5_AUTH_CONTEXT_DO_SEQUENCE)
167       auth_context->local_seqnumber =
168 	  (auth_context->local_seqnumber + 1) & 0xFFFFFFFF;
169   return 0;
170 
171 fail:
172   free (buf);
173   krb5_data_free (&s.enc_part.cipher);
174   return ret;
175 }
176