17c478bdstevel@tonic-gate/*
27c478bdstevel@tonic-gate * lib/krb5/krb/copy_tick.c
37c478bdstevel@tonic-gate *
47c478bdstevel@tonic-gate * Copyright 1990,1991 by the Massachusetts Institute of Technology.
57c478bdstevel@tonic-gate * All Rights Reserved.
67c478bdstevel@tonic-gate *
77c478bdstevel@tonic-gate * Export of this software from the United States of America may
87c478bdstevel@tonic-gate *   require a specific license from the United States Government.
97c478bdstevel@tonic-gate *   It is the responsibility of any person or organization contemplating
107c478bdstevel@tonic-gate *   export to obtain such a license before exporting.
117c478bdstevel@tonic-gate *
127c478bdstevel@tonic-gate * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
137c478bdstevel@tonic-gate * distribute this software and its documentation for any purpose and
147c478bdstevel@tonic-gate * without fee is hereby granted, provided that the above copyright
157c478bdstevel@tonic-gate * notice appear in all copies and that both that copyright notice and
167c478bdstevel@tonic-gate * this permission notice appear in supporting documentation, and that
177c478bdstevel@tonic-gate * the name of M.I.T. not be used in advertising or publicity pertaining
187c478bdstevel@tonic-gate * to distribution of the software without specific, written prior
197c478bdstevel@tonic-gate * permission.  Furthermore if you modify this software you must label
207c478bdstevel@tonic-gate * your software as modified software and not distribute it in such a
217c478bdstevel@tonic-gate * fashion that it might be confused with the original M.I.T. software.
227c478bdstevel@tonic-gate * M.I.T. makes no representations about the suitability of
237c478bdstevel@tonic-gate * this software for any purpose.  It is provided "as is" without express
247c478bdstevel@tonic-gate * or implied warranty.
257c478bdstevel@tonic-gate *
267c478bdstevel@tonic-gate *
277c478bdstevel@tonic-gate * krb5_copy_ticket()
287c478bdstevel@tonic-gate */
297c478bdstevel@tonic-gate
30159d09aMark Phalan#include "k5-int.h"
317c478bdstevel@tonic-gate
327c478bdstevel@tonic-gatestatic krb5_error_code
33505d05cgtbkrb5_copy_enc_tkt_part(krb5_context context, const krb5_enc_tkt_part *partfrom, krb5_enc_tkt_part **partto)
347c478bdstevel@tonic-gate{
357c478bdstevel@tonic-gate    krb5_error_code retval;
367c478bdstevel@tonic-gate    krb5_enc_tkt_part *tempto;
377c478bdstevel@tonic-gate
387c478bdstevel@tonic-gate    if (!(tempto = (krb5_enc_tkt_part *)malloc(sizeof(*tempto))))
397c478bdstevel@tonic-gate	return ENOMEM;
407c478bdstevel@tonic-gate    *tempto = *partfrom;
417c478bdstevel@tonic-gate    retval = krb5_copy_keyblock(context, partfrom->session,
427c478bdstevel@tonic-gate				&tempto->session);
437c478bdstevel@tonic-gate    if (retval) {
447c478bdstevel@tonic-gate	krb5_xfree(tempto);
457c478bdstevel@tonic-gate	return retval;
467c478bdstevel@tonic-gate    }
477c478bdstevel@tonic-gate    retval = krb5_copy_principal(context, partfrom->client, &tempto->client);
487c478bdstevel@tonic-gate    if (retval) {
497c478bdstevel@tonic-gate	krb5_free_keyblock(context, tempto->session);
507c478bdstevel@tonic-gate	krb5_xfree(tempto);
517c478bdstevel@tonic-gate	return retval;
527c478bdstevel@tonic-gate    }
537c478bdstevel@tonic-gate    tempto->transited = partfrom->transited;
547c478bdstevel@tonic-gate    if (tempto->transited.tr_contents.length == 0) {
557c478bdstevel@tonic-gate	tempto->transited.tr_contents.data = 0;
567c478bdstevel@tonic-gate    } else {
577c478bdstevel@tonic-gate	tempto->transited.tr_contents.data =
587c478bdstevel@tonic-gate	  malloc(partfrom->transited.tr_contents.length);
597c478bdstevel@tonic-gate	if (!tempto->transited.tr_contents.data) {
607c478bdstevel@tonic-gate	    krb5_free_principal(context, tempto->client);
617c478bdstevel@tonic-gate	    krb5_free_keyblock(context, tempto->session);
627c478bdstevel@tonic-gate	    krb5_xfree(tempto);
637c478bdstevel@tonic-gate	    return ENOMEM;
647c478bdstevel@tonic-gate	}
657c478bdstevel@tonic-gate	memcpy((char *)tempto->transited.tr_contents.data,
667c478bdstevel@tonic-gate	       (char *)partfrom->transited.tr_contents.data,
677c478bdstevel@tonic-gate	       partfrom->transited.tr_contents.length);
687c478bdstevel@tonic-gate    }
697c478bdstevel@tonic-gate
707c478bdstevel@tonic-gate    retval = krb5_copy_addresses(context, partfrom->caddrs, &tempto->caddrs);
717c478bdstevel@tonic-gate    if (retval) {
727c478bdstevel@tonic-gate	krb5_xfree(tempto->transited.tr_contents.data);
737c478bdstevel@tonic-gate	krb5_free_principal(context, tempto->client);
747c478bdstevel@tonic-gate	krb5_free_keyblock(context, tempto->session);
757c478bdstevel@tonic-gate	krb5_xfree(tempto);
767c478bdstevel@tonic-gate	return retval;
777c478bdstevel@tonic-gate    }
787c478bdstevel@tonic-gate    if (partfrom->authorization_data) {
797c478bdstevel@tonic-gate	retval = krb5_copy_authdata(context, partfrom->authorization_data,
807c478bdstevel@tonic-gate				    &tempto->authorization_data);
817c478bdstevel@tonic-gate	if (retval) {
827c478bdstevel@tonic-gate	    krb5_free_addresses(context, tempto->caddrs);
837c478bdstevel@tonic-gate	    krb5_xfree(tempto->transited.tr_contents.data);
847c478bdstevel@tonic-gate	    krb5_free_principal(context, tempto->client);
857c478bdstevel@tonic-gate	    krb5_free_keyblock(context, tempto->session);
867c478bdstevel@tonic-gate	    krb5_xfree(tempto);
877c478bdstevel@tonic-gate	    return retval;
887c478bdstevel@tonic-gate	}
897c478bdstevel@tonic-gate    }
907c478bdstevel@tonic-gate    *partto = tempto;
917c478bdstevel@tonic-gate    return 0;
927c478bdstevel@tonic-gate}
937c478bdstevel@tonic-gate
94505d05cgtbkrb5_error_code KRB5_CALLCONV
95505d05cgtbkrb5_copy_ticket(krb5_context context, const krb5_ticket *from, krb5_ticket **pto)
967c478bdstevel@tonic-gate{
977c478bdstevel@tonic-gate    krb5_error_code retval;
987c478bdstevel@tonic-gate    krb5_ticket *tempto;
997c478bdstevel@tonic-gate    krb5_data *scratch;
1007c478bdstevel@tonic-gate
1017c478bdstevel@tonic-gate    if (!(tempto = (krb5_ticket *)malloc(sizeof(*tempto))))
1027c478bdstevel@tonic-gate	return ENOMEM;
1037c478bdstevel@tonic-gate    *tempto = *from;
1047c478bdstevel@tonic-gate    retval = krb5_copy_principal(context, from->server, &tempto->server);
1057c478bdstevel@tonic-gate    if (retval) {
1067c478bdstevel@tonic-gate	krb5_xfree(tempto);
1077c478bdstevel@tonic-gate	return retval;
1087c478bdstevel@tonic-gate    }
1097c478bdstevel@tonic-gate    retval = krb5_copy_data(context, &from->enc_part.ciphertext, &scratch);
1107c478bdstevel@tonic-gate    if (retval) {
1117c478bdstevel@tonic-gate	krb5_free_principal(context, tempto->server);
1127c478bdstevel@tonic-gate	krb5_xfree(tempto);
1137c478bdstevel@tonic-gate	return retval;
1147c478bdstevel@tonic-gate    }
1157c478bdstevel@tonic-gate    tempto->enc_part.ciphertext = *scratch;
1167c478bdstevel@tonic-gate    krb5_xfree(scratch);
1177c478bdstevel@tonic-gate    retval = krb5_copy_enc_tkt_part(context, from->enc_part2, &tempto->enc_part2);
1187c478bdstevel@tonic-gate    if (retval) {
1197c478bdstevel@tonic-gate	krb5_xfree(tempto->enc_part.ciphertext.data);
1207c478bdstevel@tonic-gate	krb5_free_principal(context, tempto->server);
1217c478bdstevel@tonic-gate	krb5_xfree(tempto);
1227c478bdstevel@tonic-gate	return retval;
1237c478bdstevel@tonic-gate    }
1247c478bdstevel@tonic-gate    *pto = tempto;
1257c478bdstevel@tonic-gate    return 0;
1267c478bdstevel@tonic-gate}
127