1 /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 /* lib/krb5/krb/ser_addr.c - Serialize krb5_address structure */
3 /*
4 * Copyright 1995, 2008 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 #include "k5-int.h"
28 #include "int-proto.h"
29
30 krb5_error_code
k5_size_address(krb5_address * address,size_t * sizep)31 k5_size_address(krb5_address *address, size_t *sizep)
32 {
33 krb5_error_code kret;
34
35 /*
36 * krb5_address requires:
37 * krb5_int32 for KV5M_ADDRESS
38 * krb5_int32 for addrtype
39 * krb5_int32 for length
40 * address->length for contents
41 * krb5_int32 for KV5M_ADDRESS
42 */
43 kret = EINVAL;
44 if (address != NULL) {
45 *sizep += (sizeof(krb5_int32) +
46 sizeof(krb5_int32) +
47 sizeof(krb5_int32) +
48 sizeof(krb5_int32) +
49 (size_t) address->length);
50 kret = 0;
51 }
52 return(kret);
53 }
54
55 krb5_error_code
k5_externalize_address(krb5_address * address,krb5_octet ** buffer,size_t * lenremain)56 k5_externalize_address(krb5_address *address,
57 krb5_octet **buffer, size_t *lenremain)
58 {
59 krb5_error_code kret;
60 size_t required;
61 krb5_octet *bp;
62 size_t remain;
63
64 required = 0;
65 bp = *buffer;
66 remain = *lenremain;
67 kret = EINVAL;
68 if (address != NULL) {
69 kret = ENOMEM;
70 if (!k5_size_address(address, &required) && required <= remain) {
71 /* Our identifier */
72 (void) krb5_ser_pack_int32(KV5M_ADDRESS, &bp, &remain);
73
74 /* Our addrtype */
75 (void) krb5_ser_pack_int32((krb5_int32) address->addrtype,
76 &bp, &remain);
77
78 /* Our length */
79 (void) krb5_ser_pack_int32((krb5_int32) address->length,
80 &bp, &remain);
81
82 /* Our contents */
83 (void) krb5_ser_pack_bytes(address->contents,
84 (size_t) address->length,
85 &bp, &remain);
86
87 /* Finally, our trailer */
88 (void) krb5_ser_pack_int32(KV5M_ADDRESS, &bp, &remain);
89
90 kret = 0;
91 *buffer = bp;
92 *lenremain = remain;
93 }
94 }
95 return(kret);
96 }
97
98 krb5_error_code
k5_internalize_address(krb5_address ** argp,krb5_octet ** buffer,size_t * lenremain)99 k5_internalize_address(krb5_address **argp,
100 krb5_octet **buffer, size_t *lenremain)
101 {
102 krb5_error_code kret;
103 krb5_address *address;
104 krb5_int32 ibuf;
105 krb5_octet *bp;
106 size_t remain;
107
108 bp = *buffer;
109 remain = *lenremain;
110 kret = EINVAL;
111 /* Read our magic number */
112 if (krb5_ser_unpack_int32(&ibuf, &bp, &remain))
113 ibuf = 0;
114 if (ibuf == KV5M_ADDRESS) {
115 kret = ENOMEM;
116
117 /* Get a address */
118 if ((remain >= (2*sizeof(krb5_int32))) &&
119 (address = (krb5_address *) calloc(1, sizeof(krb5_address)))) {
120
121 address->magic = KV5M_ADDRESS;
122
123 /* Get the addrtype */
124 (void) krb5_ser_unpack_int32(&ibuf, &bp, &remain);
125 address->addrtype = (krb5_addrtype) ibuf;
126
127 /* Get the length */
128 (void) krb5_ser_unpack_int32(&ibuf, &bp, &remain);
129 address->length = (int) ibuf;
130
131 /* Get the string */
132 if ((address->contents = (krb5_octet *) malloc((size_t) (ibuf))) &&
133 !(kret = krb5_ser_unpack_bytes(address->contents,
134 (size_t) ibuf,
135 &bp, &remain))) {
136 /* Get the trailer */
137 if ((kret = krb5_ser_unpack_int32(&ibuf, &bp, &remain)))
138 ibuf = 0;
139
140 if (!kret && (ibuf == KV5M_ADDRESS)) {
141 address->magic = KV5M_ADDRESS;
142 *buffer = bp;
143 *lenremain = remain;
144 *argp = address;
145 }
146 else
147 kret = EINVAL;
148 }
149 if (kret) {
150 if (address->contents)
151 free(address->contents);
152 free(address);
153 }
154 }
155 }
156 return(kret);
157 }
158