1 /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 /* lib/krb5/krb/ser_cksum.c - Serialize krb5_checksum structure */
4 * Copyright 1995, 2008 by the Massachusetts Institute of Technology.
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.
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.
28 #include "int-proto.h"
31 k5_size_checksum(krb5_checksum *checksum, size_t *sizep)
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
44 if (checksum != NULL) {
45 *sizep += (sizeof(krb5_int32) +
49 (size_t) checksum->length);
56 k5_externalize_checksum(krb5_checksum *checksum,
57 krb5_octet **buffer, size_t *lenremain)
68 if (checksum != NULL) {
70 if (!k5_size_checksum(checksum, &required) && required <= remain) {
72 (void) krb5_ser_pack_int32(KV5M_CHECKSUM, &bp, &remain);
74 /* Our checksum_type */
75 (void) krb5_ser_pack_int32((krb5_int32) checksum->checksum_type,
79 (void) krb5_ser_pack_int32((krb5_int32) checksum->length,
83 (void) krb5_ser_pack_bytes(checksum->contents,
84 (size_t) checksum->length,
87 /* Finally, our trailer */
88 (void) krb5_ser_pack_int32(KV5M_CHECKSUM, &bp, &remain);
99 k5_internalize_checksum(krb5_checksum **argp,
100 krb5_octet **buffer, size_t *lenremain)
102 krb5_error_code kret;
103 krb5_checksum *checksum;
111 /* Read our magic number */
112 if (krb5_ser_unpack_int32(&ibuf, &bp, &remain))
114 if (ibuf == KV5M_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;
125 (void) krb5_ser_unpack_int32(&ibuf, &bp, &remain);
126 checksum->length = (int) ibuf;
130 ((checksum->contents = (krb5_octet *)
131 malloc((size_t) (ibuf))) &&
132 !(kret = krb5_ser_unpack_bytes(checksum->contents,
136 /* Get the trailer */
137 kret = krb5_ser_unpack_int32(&ibuf, &bp, &remain);
138 if (!kret && (ibuf == KV5M_CHECKSUM)) {
139 checksum->magic = KV5M_CHECKSUM;
148 if (checksum->contents)
149 free(checksum->contents);