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