xref: /freebsd/crypto/heimdal/lib/krb5/mk_priv.c (revision 23f282aa31e9b6fceacd449020e936e98d6f2298)
1 /*
2  * Copyright (c) 1997, 1998, 1999 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.25 1999/12/02 17:05:11 joda 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   int 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   krb5_crypto_init(context, key, 0, &crypto);
121   ret = krb5_encrypt (context,
122 		      crypto,
123 		      KRB5_KU_KRB_PRIV,
124 		      buf + buf_size - len,
125 		      len,
126 		      &s.enc_part.cipher);
127   krb5_crypto_destroy(context, crypto);
128   if (ret) {
129       free(buf);
130       return ret;
131   }
132 
133   do {
134       ret = encode_KRB_PRIV (buf + buf_size - 1, buf_size, &s, &len);
135 
136       if (ret){
137 	  if (ret == ASN1_OVERFLOW) {
138 	      u_char *tmp;
139 
140 	      buf_size *= 2;
141 	      tmp = realloc (buf, buf_size);
142 	      if (tmp == NULL) {
143 		  ret = ENOMEM;
144 		  goto fail;
145 	      }
146 	      buf = tmp;
147 	  } else {
148 	      goto fail;
149 	  }
150       }
151   } while(ret == ASN1_OVERFLOW);
152   krb5_data_free (&s.enc_part.cipher);
153 
154   outbuf->length = len;
155   outbuf->data   = malloc (len);
156   if (outbuf->data == NULL) {
157       free(buf);
158       return ENOMEM;
159   }
160   memcpy (outbuf->data, buf + buf_size - len, len);
161   free (buf);
162   return 0;
163 
164 fail:
165   free (buf);
166   krb5_data_free (&s.enc_part.cipher);
167   return ret;
168 }
169