1 /*
2  * lib/krb5/krb/ser_addr.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_addr.c - Serialize a krb5_address structure.
30  */
31 #include "k5-int.h"
32 #include "int-proto.h"
33 
34 /*
35  * Routines to deal with externalizing the krb5_address:
36  *	krb5_address_size();
37  *	krb5_address_externalize();
38  *	krb5_address_internalize();
39  */
40 static krb5_error_code krb5_address_size
41 	(krb5_context, krb5_pointer, size_t *);
42 static krb5_error_code krb5_address_externalize
43 	(krb5_context, krb5_pointer, krb5_octet **, size_t *);
44 static krb5_error_code krb5_address_internalize
45 	(krb5_context,krb5_pointer *, krb5_octet **, size_t *);
46 
47 /* Local data */
48 static const krb5_ser_entry krb5_address_ser_entry = {
49     KV5M_ADDRESS,			/* Type			*/
50     krb5_address_size,		/* Sizer routine	*/
51     krb5_address_externalize,		/* Externalize routine	*/
52     krb5_address_internalize		/* Internalize routine	*/
53 };
54 
55 /*
56  * krb5_address_size()	- Determine the size required to externalize
57  *				  the krb5_address.
58  */
59 /*ARGSUSED*/
60 static krb5_error_code
krb5_address_size(krb5_context kcontext,krb5_pointer arg,size_t * sizep)61 krb5_address_size(krb5_context kcontext, krb5_pointer arg, size_t *sizep)
62 {
63     krb5_error_code	kret;
64     krb5_address	*address;
65 
66     /*
67      * krb5_address requires:
68      *	krb5_int32		for KV5M_ADDRESS
69      *	krb5_int32		for addrtype
70      *	krb5_int32		for length
71      *	address->length		for contents
72      *	krb5_int32		for KV5M_ADDRESS
73      */
74     kret = EINVAL;
75     /* Solaris Kerberos */
76     address = (krb5_address *) arg;
77     if (address) {
78 	*sizep += (sizeof(krb5_int32) +
79 		   sizeof(krb5_int32) +
80 		   sizeof(krb5_int32) +
81 		   sizeof(krb5_int32) +
82 		   (size_t) address->length);
83 	kret = 0;
84     }
85     return(kret);
86 }
87 
88 /*
89  * krb5_address_externalize()	- Externalize the krb5_address.
90  */
91 static krb5_error_code
krb5_address_externalize(krb5_context kcontext,krb5_pointer arg,krb5_octet ** buffer,size_t * lenremain)92 krb5_address_externalize(krb5_context kcontext, krb5_pointer arg, krb5_octet **buffer, size_t *lenremain)
93 {
94     krb5_error_code	kret;
95     krb5_address	*address;
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     address = (krb5_address *) arg;
106     if (address) {
107 	kret = ENOMEM;
108 	if (!krb5_address_size(kcontext, arg, &required) &&
109 	    (required <= remain)) {
110 	    /* Our identifier */
111 	    (void) krb5_ser_pack_int32(KV5M_ADDRESS, &bp, &remain);
112 
113 	    /* Our addrtype */
114 	    (void) krb5_ser_pack_int32((krb5_int32) address->addrtype,
115 				       &bp, &remain);
116 
117 	    /* Our length */
118 	    (void) krb5_ser_pack_int32((krb5_int32) address->length,
119 				       &bp, &remain);
120 
121 	    /* Our contents */
122 	    (void) krb5_ser_pack_bytes(address->contents,
123 				       (size_t) address->length,
124 				       &bp, &remain);
125 
126 	    /* Finally, our trailer */
127 	    (void) krb5_ser_pack_int32(KV5M_ADDRESS, &bp, &remain);
128 
129 	    kret = 0;
130 	    *buffer = bp;
131 	    *lenremain = remain;
132 	}
133     }
134     return(kret);
135 }
136 
137 /*
138  * krb5_address_internalize()	- Internalize the krb5_address.
139  */
140 
141 /*ARGSUSED*/
142 static krb5_error_code
krb5_address_internalize(krb5_context kcontext,krb5_pointer * argp,krb5_octet ** buffer,size_t * lenremain)143 krb5_address_internalize(krb5_context kcontext, krb5_pointer *argp, krb5_octet **buffer, size_t *lenremain)
144 {
145     krb5_error_code	kret;
146     krb5_address	*address;
147     krb5_int32		ibuf;
148     krb5_octet		*bp;
149     size_t		remain;
150 
151     bp = *buffer;
152     remain = *lenremain;
153     kret = EINVAL;
154     /* Read our magic number */
155     if (krb5_ser_unpack_int32(&ibuf, &bp, &remain))
156 	ibuf = 0;
157     if (ibuf == KV5M_ADDRESS) {
158 	kret = ENOMEM;
159 
160 	/* Get a address */
161 	if ((remain >= (2*sizeof(krb5_int32))) &&
162 	    (address = (krb5_address *) MALLOC(sizeof(krb5_address)))) {
163 	    (void) memset(address, 0, sizeof(krb5_address));
164 
165 	    address->magic = KV5M_ADDRESS;
166 
167 	    /* Get the addrtype */
168 	    (void) krb5_ser_unpack_int32(&ibuf, &bp, &remain);
169 	    address->addrtype = (krb5_addrtype) ibuf;
170 
171 	    /* Get the length */
172 	    (void) krb5_ser_unpack_int32(&ibuf, &bp, &remain);
173 	    address->length = (int) ibuf;
174 
175 	    /* Get the string */
176 	    /* Solaris Kerberos */
177 	    address->contents = (krb5_octet *) MALLOC((size_t) (ibuf));
178 	    if ((address->contents) &&
179 		!(kret = krb5_ser_unpack_bytes(address->contents,
180 					       (size_t) ibuf,
181 					       &bp, &remain))) {
182 		/* Get the trailer */
183 		if ((kret = krb5_ser_unpack_int32(&ibuf, &bp, &remain)))
184 		    ibuf = 0;
185 
186 		if (!kret && (ibuf == KV5M_ADDRESS)) {
187 		    address->magic = KV5M_ADDRESS;
188 		    *buffer = bp;
189 		    *lenremain = remain;
190 		    *argp = (krb5_pointer) address;
191 		}
192 		else
193 		    kret = EINVAL;
194 	    }
195 	    if (kret) {
196 		if (address->contents)
197 		    FREE(address->contents, address->length);
198 		FREE(address, sizeof (krb5_address));
199 	    }
200 	}
201     }
202     return(kret);
203 }
204 
205 /*
206  * Register the address serializer.
207  */
208 krb5_error_code
krb5_ser_address_init(krb5_context kcontext)209 krb5_ser_address_init(krb5_context kcontext)
210 {
211     return(krb5_register_serializer(kcontext, &krb5_address_ser_entry));
212 }
213