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