summaryrefslogtreecommitdiffstats
path: root/src/lib/kadm/adm_kt_enc.c
blob: c63012361ffd92549d8e22c64ea10178037aa275 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/*
 * lib/kadm/adm_kt_enc.c
 *
 * Copyright 1995 by the Massachusetts Institute of Technology.
 * All Rights Reserved.
 *
 * Export of this software from the United States of America may
 *   require a specific license from the United States Government.
 *   It is the responsibility of any person or organization contemplating
 *   export to obtain such a license before exporting.
 *
 * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
 * distribute this software and its documentation for any purpose and
 * without fee is hereby granted, provided that the above copyright
 * notice appear in all copies and that both that copyright notice and
 * this permission notice appear in supporting documentation, and that
 * the name of M.I.T. not be used in advertising or publicity pertaining
 * to distribution of the software without specific, written prior
 * permission.  M.I.T. makes no representations about the suitability of
 * this software for any purpose.  It is provided "as is" without express
 * or implied warranty.
 *
 */

/*
 * adm_kt_enc.c	- Encode keytab entry according to protocol.
 */
#include "k5-int.h"
#include "adm.h"

/*
 * krb5_adm_ktent_to_proto()	- Convert a keytab entry into an external
 *				  list of reply components.
 *
 * Successful callers must free the storage for complistp and complistp->data
 * either manually or by using krb5_free_adm_data().
 */
krb5_error_code
krb5_adm_ktent_to_proto(kcontext, ktentp, ncompp, complistp)
    krb5_context	kcontext;
    krb5_keytab_entry	*ktentp;
    krb5_int32		*ncompp;
    krb5_data		**complistp;
{
    krb5_error_code	kret;
    krb5_data		*clist;
    krb5_int32		nents;

    kret = ENOMEM;
    nents = 0;
    if (clist = (krb5_data *) malloc((size_t) KRB5_ADM_KT_NCOMPS *
				     sizeof(krb5_data))) {
	memset((char *) clist, 0, ((size_t) KRB5_ADM_KT_NCOMPS *
				   sizeof(krb5_data)));
	/*
	 * Fill in the principal field.
	 */
	if (kret = krb5_unparse_name(kcontext,
				     ktentp->principal,
				     &clist[KRB5_ADM_KT_PRINCIPAL].data))
	    goto done;
	clist[KRB5_ADM_KT_PRINCIPAL].length =
	    strlen(clist[KRB5_ADM_KT_PRINCIPAL].data);
	nents++;

	/*
	 * Fill in timestamp.
	 */
	if (kret = krb5_timeofday(kcontext, &ktentp->timestamp))
	    goto done;
	if (clist[KRB5_ADM_KT_TIMESTAMP].data = 
	    (char *) malloc(sizeof(krb5_ui_4))) {
	    clist[KRB5_ADM_KT_TIMESTAMP].length = sizeof(krb5_ui_4);
	    clist[KRB5_ADM_KT_TIMESTAMP].data[0] =
		(char) ((ktentp->timestamp >> 24) & 0xff);
	    clist[KRB5_ADM_KT_TIMESTAMP].data[1] =
		(char) ((ktentp->timestamp >> 16) & 0xff);
	    clist[KRB5_ADM_KT_TIMESTAMP].data[2] =
		(char) ((ktentp->timestamp >> 8) & 0xff);
	    clist[KRB5_ADM_KT_TIMESTAMP].data[3] =
		(char) (ktentp->timestamp & 0xff);
	    nents++;
	}
	else {
	    kret = ENOMEM;
	    goto done;
	}

	/*
	 * Fill in vno.
	 */
	if (clist[KRB5_ADM_KT_VNO].data = 
	    (char *) malloc(sizeof(krb5_ui_4))) {
	    clist[KRB5_ADM_KT_VNO].length = sizeof(krb5_ui_4);
	    clist[KRB5_ADM_KT_VNO].data[0] = (ktentp->vno >> 24) & 0xff;
	    clist[KRB5_ADM_KT_VNO].data[1] = (ktentp->vno >> 16) & 0xff;
	    clist[KRB5_ADM_KT_VNO].data[2] = (ktentp->vno >> 8) & 0xff;
	    clist[KRB5_ADM_KT_VNO].data[3] = ktentp->vno & 0xff;
	    nents++;
	}
	else {
	    kret = ENOMEM;
	    goto done;
	}

	/*
	 * Fill in key_keytype.
	 */
	if (clist[KRB5_ADM_KT_KEY_KEYTYPE].data = 
	    (char *) malloc(sizeof(krb5_ui_4))) {
	    clist[KRB5_ADM_KT_KEY_KEYTYPE].length = sizeof(krb5_ui_4);
	    clist[KRB5_ADM_KT_KEY_KEYTYPE].data[0] =
		(ktentp->key.keytype >> 24) & 0xff;
	    clist[KRB5_ADM_KT_KEY_KEYTYPE].data[1] =
		(ktentp->key.keytype >> 16) & 0xff;
	    clist[KRB5_ADM_KT_KEY_KEYTYPE].data[2] =
		(ktentp->key.keytype >> 8) & 0xff;
	    clist[KRB5_ADM_KT_KEY_KEYTYPE].data[3] =
		ktentp->key.keytype & 0xff;
	    nents++;
	}
	else {
	    kret = ENOMEM;
	    goto done;
	}

	/*
	 * Fill in key_etype.
	 */
	if (clist[KRB5_ADM_KT_KEY_ETYPE].data = 
	    (char *) malloc(sizeof(krb5_ui_4))) {
	    clist[KRB5_ADM_KT_KEY_ETYPE].length = sizeof(krb5_ui_4);
	    clist[KRB5_ADM_KT_KEY_ETYPE].data[0] =
		(ktentp->key.etype >> 24) & 0xff;
	    clist[KRB5_ADM_KT_KEY_ETYPE].data[1] =
		(ktentp->key.etype >> 16) & 0xff;
	    clist[KRB5_ADM_KT_KEY_ETYPE].data[2] =
		(ktentp->key.etype >> 8) & 0xff;
	    clist[KRB5_ADM_KT_KEY_ETYPE].data[3] =
		ktentp->key.etype & 0xff;
	    nents++;
	}
	else {
	    kret = ENOMEM;
	    goto done;
	}

	/*
	 * Fill in key_key.
	 */
	if (clist[KRB5_ADM_KT_KEY_KEY].data = 
	    (char *) malloc((size_t) ktentp->key.length)) {
	    memcpy(clist[KRB5_ADM_KT_KEY_KEY].data,
		   ktentp->key.contents,
		   (size_t) ktentp->key.length);
	    clist[KRB5_ADM_KT_KEY_KEY].length = ktentp->key.length;
	    nents++;
	    kret = 0;
	}
	else
	    kret = ENOMEM;
    }
 done:
    if (kret) {
	if (clist) {
	    int i;
	    for (i=0; i<KRB5_ADM_KT_NCOMPS; i++) {
		if (clist[i].data) {
		    memset(clist[i].data, 0, (size_t) clist[i].length);
		    krb5_xfree(clist[i].data);
		}
	    }
	    krb5_xfree(clist);
	}
	clist = (krb5_data *) NULL;
	nents = 0;
    }
    *complistp = clist;
    *ncompp = nents;
    return(kret);
}