mk_rep.c revision 72445
155682Smarkm/*
272445Sassar * Copyright (c) 1997 - 2000 Kungliga Tekniska H�gskolan
355682Smarkm * (Royal Institute of Technology, Stockholm, Sweden).
455682Smarkm * All rights reserved.
555682Smarkm *
655682Smarkm * Redistribution and use in source and binary forms, with or without
755682Smarkm * modification, are permitted provided that the following conditions
855682Smarkm * are met:
955682Smarkm *
1055682Smarkm * 1. Redistributions of source code must retain the above copyright
1155682Smarkm *    notice, this list of conditions and the following disclaimer.
1255682Smarkm *
1355682Smarkm * 2. Redistributions in binary form must reproduce the above copyright
1455682Smarkm *    notice, this list of conditions and the following disclaimer in the
1555682Smarkm *    documentation and/or other materials provided with the distribution.
1655682Smarkm *
1755682Smarkm * 3. Neither the name of the Institute nor the names of its contributors
1855682Smarkm *    may be used to endorse or promote products derived from this software
1955682Smarkm *    without specific prior written permission.
2055682Smarkm *
2155682Smarkm * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
2255682Smarkm * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2355682Smarkm * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2455682Smarkm * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
2555682Smarkm * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2655682Smarkm * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2755682Smarkm * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2855682Smarkm * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2955682Smarkm * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3055682Smarkm * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3155682Smarkm * SUCH DAMAGE.
3255682Smarkm */
3355682Smarkm
3455682Smarkm#include <krb5_locl.h>
3555682Smarkm
3672445SassarRCSID("$Id: mk_rep.c,v 1.18 2000/12/06 20:57:23 joda Exp $");
3755682Smarkm
3855682Smarkmkrb5_error_code
3955682Smarkmkrb5_mk_rep(krb5_context context,
4072445Sassar	    krb5_auth_context auth_context,
4155682Smarkm	    krb5_data *outbuf)
4255682Smarkm{
4355682Smarkm  krb5_error_code ret;
4455682Smarkm  AP_REP ap;
4555682Smarkm  EncAPRepPart body;
4655682Smarkm  u_char *buf = NULL;
4755682Smarkm  size_t buf_size;
4855682Smarkm  size_t len;
4955682Smarkm  krb5_crypto crypto;
5055682Smarkm
5155682Smarkm  ap.pvno = 5;
5255682Smarkm  ap.msg_type = krb_ap_rep;
5355682Smarkm
5455682Smarkm  memset (&body, 0, sizeof(body));
5555682Smarkm
5672445Sassar  body.ctime = auth_context->authenticator->ctime;
5772445Sassar  body.cusec = auth_context->authenticator->cusec;
5855682Smarkm  body.subkey = NULL;
5972445Sassar  if (auth_context->flags & KRB5_AUTH_CONTEXT_DO_SEQUENCE) {
6055682Smarkm    krb5_generate_seq_number (context,
6172445Sassar			      auth_context->keyblock,
6272445Sassar			      &auth_context->local_seqnumber);
6355682Smarkm    body.seq_number = malloc (sizeof(*body.seq_number));
6455682Smarkm    if (body.seq_number == NULL)
6555682Smarkm	return ENOMEM;
6672445Sassar    *(body.seq_number) = auth_context->local_seqnumber;
6755682Smarkm  } else
6855682Smarkm    body.seq_number = NULL;
6955682Smarkm
7072445Sassar  ap.enc_part.etype = auth_context->keyblock->keytype;
7155682Smarkm  ap.enc_part.kvno  = NULL;
7255682Smarkm
7355682Smarkm  buf_size = length_EncAPRepPart(&body);
7455682Smarkm  buf = malloc (buf_size);
7555682Smarkm  if (buf == NULL) {
7655682Smarkm      free_EncAPRepPart (&body);
7755682Smarkm      return ENOMEM;
7855682Smarkm  }
7955682Smarkm
8055682Smarkm  ret = krb5_encode_EncAPRepPart (context,
8155682Smarkm				  buf + buf_size - 1,
8255682Smarkm				  buf_size,
8355682Smarkm				  &body,
8455682Smarkm				  &len);
8555682Smarkm
8655682Smarkm  free_EncAPRepPart (&body);
8772445Sassar  ret = krb5_crypto_init(context, auth_context->keyblock,
8872445Sassar			 0 /* ap.enc_part.etype */, &crypto);
8972445Sassar  if (ret) {
9072445Sassar      free (buf);
9172445Sassar      return ret;
9272445Sassar  }
9355682Smarkm  ret = krb5_encrypt (context,
9455682Smarkm		      crypto,
9555682Smarkm		      KRB5_KU_AP_REQ_ENC_PART,
9655682Smarkm		      buf + buf_size - len,
9755682Smarkm		      len,
9855682Smarkm		      &ap.enc_part.cipher);
9955682Smarkm  krb5_crypto_destroy(context, crypto);
10055682Smarkm  if (ret) {
10155682Smarkm      free(buf);
10255682Smarkm      return ret;
10355682Smarkm  }
10455682Smarkm
10555682Smarkm  buf_size = length_AP_REP(&ap);
10655682Smarkm  buf = realloc(buf, buf_size);
10755682Smarkm  if(buf == NULL) {
10855682Smarkm      free_AP_REP (&ap);
10955682Smarkm      return ENOMEM;
11055682Smarkm  }
11155682Smarkm  ret = encode_AP_REP (buf + buf_size - 1, buf_size, &ap, &len);
11255682Smarkm
11355682Smarkm  free_AP_REP (&ap);
11455682Smarkm
11555682Smarkm  if(len != buf_size)
11655682Smarkm      krb5_abortx(context, "krb5_mk_rep: encoded length != calculated length");
11755682Smarkm  outbuf->data = buf;
11855682Smarkm  outbuf->length = len;
11955682Smarkm  return 0;
12055682Smarkm}
121