1 /*
2  * lib/krb5/krb/ser_adata.c
3  *
4  * Copyright 1995 by the Massachusetts Institute of Technology.
5  * All Rights Reserved.
6  *
7  * Export of this software from the United States of America may
8  *   require a specific license from the United States Government.
9  *   It is the responsibility of any person or organization contemplating
10  *   export to obtain such a license before exporting.
11  *
12  * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
13  * distribute this software and its documentation for any purpose and
14  * without fee is hereby granted, provided that the above copyright
15  * notice appear in all copies and that both that copyright notice and
16  * this permission notice appear in supporting documentation, and that
17  * the name of M.I.T. not be used in advertising or publicity pertaining
18  * to distribution of the software without specific, written prior
19  * permission.  Furthermore if you modify this software you must label
20  * your software as modified software and not distribute it in such a
21  * fashion that it might be confused with the original M.I.T. software.
22  * M.I.T. makes no representations about the suitability of
23  * this software for any purpose.  It is provided "as is" without express
24  * or implied warranty.
25  *
26  */
27 
28 /*
29  * ser_adata.c - Serialize a krb5_authdata structure.
30  */
31 #include "k5-int.h"
32 #include "int-proto.h"
33 
34 /*
35  * Routines to deal with externalizing the krb5_authdata:
36  *	krb5_authdata_size();
37  *	krb5_authdata_externalize();
38  *	krb5_authdata_internalize();
39  */
40 static krb5_error_code krb5_authdata_size
41 	(krb5_context, krb5_pointer, size_t *);
42 static krb5_error_code krb5_authdata_externalize
43 	(krb5_context, krb5_pointer, krb5_octet **, size_t *);
44 static krb5_error_code krb5_authdata_internalize
45 	(krb5_context,krb5_pointer *, krb5_octet **, size_t *);
46 
47 /* Local data */
48 static const krb5_ser_entry krb5_authdata_ser_entry = {
49     KV5M_AUTHDATA,			/* Type			*/
50     krb5_authdata_size,			/* Sizer routine	*/
51     krb5_authdata_externalize,		/* Externalize routine	*/
52     krb5_authdata_internalize		/* Internalize routine	*/
53 };
54 
55 /*
56  * krb5_authdata_esize()	- Determine the size required to externalize
57  *				  the krb5_authdata.
58  */
59 /*ARGSUSED*/
60 static krb5_error_code
krb5_authdata_size(krb5_context kcontext,krb5_pointer arg,size_t * sizep)61 krb5_authdata_size(krb5_context kcontext, krb5_pointer arg, size_t *sizep)
62 {
63     krb5_error_code	kret;
64     krb5_authdata	*authdata;
65 
66     /*
67      * krb5_authdata requires:
68      *	krb5_int32		for KV5M_AUTHDATA
69      *	krb5_int32		for ad_type
70      *	krb5_int32		for length
71      *	authdata->length	for contents
72      *	krb5_int32		for KV5M_AUTHDATA
73      */
74     kret = EINVAL;
75     /* Solaris Kerberos */
76     authdata = (krb5_authdata *) arg;
77     if (authdata) {
78 	*sizep += (sizeof(krb5_int32) +
79 		   sizeof(krb5_int32) +
80 		   sizeof(krb5_int32) +
81 		   sizeof(krb5_int32) +
82 		   (size_t) authdata->length);
83 	kret = 0;
84     }
85     return(kret);
86 }
87 
88 /*
89  * krb5_authdata_externalize()	- Externalize the krb5_authdata.
90  */
91 static krb5_error_code
krb5_authdata_externalize(krb5_context kcontext,krb5_pointer arg,krb5_octet ** buffer,size_t * lenremain)92 krb5_authdata_externalize(krb5_context kcontext, krb5_pointer arg, krb5_octet **buffer, size_t *lenremain)
93 {
94     krb5_error_code	kret;
95     krb5_authdata	*authdata;
96     size_t		required;
97     krb5_octet		*bp;
98     size_t		remain;
99 
100     required = 0;
101     bp = *buffer;
102     remain = *lenremain;
103     kret = EINVAL;
104     /* Solaris Kerberos */
105     authdata = (krb5_authdata *) arg;
106     if (authdata) {
107 	kret = ENOMEM;
108 	if (!krb5_authdata_size(kcontext, arg, &required) &&
109 	    (required <= remain)) {
110 	    /* Our identifier */
111 	    (void) krb5_ser_pack_int32(KV5M_AUTHDATA, &bp, &remain);
112 
113 	    /* Our ad_type */
114 	    (void) krb5_ser_pack_int32((krb5_int32) authdata->ad_type,
115 				       &bp, &remain);
116 
117 	    /* Our length */
118 	    (void) krb5_ser_pack_int32((krb5_int32) authdata->length,
119 				       &bp, &remain);
120 
121 	    /* Our contents */
122 	    (void) krb5_ser_pack_bytes(authdata->contents,
123 				       (size_t) authdata->length,
124 				       &bp, &remain);
125 
126 	    /* Finally, our trailer */
127 	    (void) krb5_ser_pack_int32(KV5M_AUTHDATA, &bp, &remain);
128 	    kret = 0;
129 	    *buffer = bp;
130 	    *lenremain = remain;
131 	}
132     }
133     return(kret);
134 }
135 
136 /*
137  * krb5_authdata_internalize()	- Internalize the krb5_authdata.
138  */
139 /*ARGSUSED*/
140 static krb5_error_code
krb5_authdata_internalize(krb5_context kcontext,krb5_pointer * argp,krb5_octet ** buffer,size_t * lenremain)141 krb5_authdata_internalize(krb5_context kcontext, krb5_pointer *argp, krb5_octet **buffer, size_t *lenremain)
142 {
143     krb5_error_code	kret;
144     krb5_authdata	*authdata;
145     krb5_int32		ibuf;
146     krb5_octet		*bp;
147     size_t		remain;
148 
149     bp = *buffer;
150     remain = *lenremain;
151     kret = EINVAL;
152     /* Read our magic number */
153     if (krb5_ser_unpack_int32(&ibuf, &bp, &remain))
154 	ibuf = 0;
155     if (ibuf == KV5M_AUTHDATA) {
156 	kret = ENOMEM;
157 
158 	/* Get a authdata */
159 	if ((remain >= (2*sizeof(krb5_int32))) &&
160 	    (authdata = (krb5_authdata *) MALLOC(sizeof(krb5_authdata)))) {
161 	    (void) memset(authdata, 0, sizeof(krb5_authdata));
162 
163 	    /* Get the ad_type */
164 	    (void) krb5_ser_unpack_int32(&ibuf, &bp, &remain);
165 	    authdata->ad_type = (krb5_authdatatype) ibuf;
166 
167 	    /* Get the length */
168 	    (void) krb5_ser_unpack_int32(&ibuf, &bp, &remain);
169 	    authdata->length = (int) ibuf;
170 
171 	    /* Get the string */
172 	    /* Solaris Kerberos */
173 	    authdata->contents = (krb5_octet *)
174 		 			MALLOC((size_t) (ibuf));
175 	    if ((authdata->contents) &&
176 		!(kret = krb5_ser_unpack_bytes(authdata->contents,
177 					       (size_t) ibuf,
178 					       &bp, &remain))) {
179 		if ((kret = krb5_ser_unpack_int32(&ibuf, &bp, &remain)))
180 		    ibuf = 0;
181 		if (ibuf == KV5M_AUTHDATA) {
182 		    authdata->magic = KV5M_AUTHDATA;
183 		    *buffer = bp;
184 		    *lenremain = remain;
185 		    *argp = (krb5_pointer) authdata;
186 		}
187 		else
188 		    kret = EINVAL;
189 	    }
190 	    if (kret) {
191 		if (authdata->contents)
192 		    FREE(authdata->contents, authdata->length);
193 		FREE(authdata, sizeof (krb5_authdata));
194 	    }
195 	}
196     }
197     return(kret);
198 }
199 
200 /*
201  * Register the authdata serializer.
202  */
203 krb5_error_code
krb5_ser_authdata_init(krb5_context kcontext)204 krb5_ser_authdata_init(krb5_context kcontext)
205 {
206     return(krb5_register_serializer(kcontext, &krb5_authdata_ser_entry));
207 }
208