1 /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 /* lib/krb5/krb/ser_adata.c - Serialize krb5_authdata 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_authdata(krb5_authdata * authdata,size_t * sizep)31 k5_size_authdata(krb5_authdata *authdata, size_t *sizep)
32 {
33 krb5_error_code kret;
34
35 /*
36 * krb5_authdata requires:
37 * krb5_int32 for KV5M_AUTHDATA
38 * krb5_int32 for ad_type
39 * krb5_int32 for length
40 * authdata->length for contents
41 * krb5_int32 for KV5M_AUTHDATA
42 */
43 kret = EINVAL;
44 if (authdata != NULL) {
45 *sizep += (sizeof(krb5_int32) +
46 sizeof(krb5_int32) +
47 sizeof(krb5_int32) +
48 sizeof(krb5_int32) +
49 (size_t) authdata->length);
50 kret = 0;
51 }
52 return(kret);
53 }
54
55 krb5_error_code
k5_externalize_authdata(krb5_authdata * authdata,krb5_octet ** buffer,size_t * lenremain)56 k5_externalize_authdata(krb5_authdata *authdata,
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 (authdata != NULL) {
69 kret = ENOMEM;
70 if (!k5_size_authdata(authdata, &required) && required <= remain) {
71 /* Our identifier */
72 (void) krb5_ser_pack_int32(KV5M_AUTHDATA, &bp, &remain);
73
74 /* Our ad_type */
75 (void) krb5_ser_pack_int32((krb5_int32) authdata->ad_type,
76 &bp, &remain);
77
78 /* Our length */
79 (void) krb5_ser_pack_int32((krb5_int32) authdata->length,
80 &bp, &remain);
81
82 /* Our contents */
83 (void) krb5_ser_pack_bytes(authdata->contents,
84 (size_t) authdata->length,
85 &bp, &remain);
86
87 /* Finally, our trailer */
88 (void) krb5_ser_pack_int32(KV5M_AUTHDATA, &bp, &remain);
89 kret = 0;
90 *buffer = bp;
91 *lenremain = remain;
92 }
93 }
94 return(kret);
95 }
96
97 krb5_error_code
k5_internalize_authdata(krb5_authdata ** argp,krb5_octet ** buffer,size_t * lenremain)98 k5_internalize_authdata(krb5_authdata **argp,
99 krb5_octet **buffer, size_t *lenremain)
100 {
101 krb5_error_code kret;
102 krb5_authdata *authdata;
103 krb5_int32 ibuf;
104 krb5_octet *bp;
105 size_t remain;
106
107 bp = *buffer;
108 remain = *lenremain;
109 kret = EINVAL;
110 /* Read our magic number */
111 if (krb5_ser_unpack_int32(&ibuf, &bp, &remain))
112 ibuf = 0;
113 if (ibuf == KV5M_AUTHDATA) {
114 kret = ENOMEM;
115
116 /* Get a authdata */
117 if ((remain >= (2*sizeof(krb5_int32))) &&
118 (authdata = (krb5_authdata *) calloc(1, sizeof(krb5_authdata)))) {
119
120 /* Get the ad_type */
121 (void) krb5_ser_unpack_int32(&ibuf, &bp, &remain);
122 authdata->ad_type = (krb5_authdatatype) ibuf;
123
124 /* Get the length */
125 (void) krb5_ser_unpack_int32(&ibuf, &bp, &remain);
126 authdata->length = (int) ibuf;
127
128 /* Get the string */
129 if ((authdata->contents = (krb5_octet *)
130 malloc((size_t) (ibuf))) &&
131 !(kret = krb5_ser_unpack_bytes(authdata->contents,
132 (size_t) ibuf,
133 &bp, &remain))) {
134 if ((kret = krb5_ser_unpack_int32(&ibuf, &bp, &remain)))
135 ibuf = 0;
136 if (ibuf == KV5M_AUTHDATA) {
137 authdata->magic = KV5M_AUTHDATA;
138 *buffer = bp;
139 *lenremain = remain;
140 *argp = authdata;
141 }
142 else
143 kret = EINVAL;
144 }
145 if (kret) {
146 if (authdata->contents)
147 free(authdata->contents);
148 free(authdata);
149 }
150 }
151 }
152 return(kret);
153 }
154