1 /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 /* lib/krb5/krb/ser_key.c - Serialize krb5_keyblock 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_keyblock(krb5_keyblock *keyblock, size_t *sizep)
36 * krb5_keyblock requires:
37 * krb5_int32 for KV5M_KEYBLOCK
38 * krb5_int32 for enctype
39 * krb5_int32 for length
40 * keyblock->length for contents
41 * krb5_int32 for KV5M_KEYBLOCK
44 if (keyblock != NULL) {
45 *sizep += 4 * sizeof(krb5_int32) + keyblock->length;
52 k5_externalize_keyblock(krb5_keyblock *keyblock,
53 krb5_octet **buffer, size_t *lenremain)
64 if (keyblock != NULL) {
66 if (!k5_size_keyblock(keyblock, &required) && required <= remain) {
68 (void) krb5_ser_pack_int32(KV5M_KEYBLOCK, &bp, &remain);
71 (void) krb5_ser_pack_int32((krb5_int32) keyblock->enctype,
75 (void) krb5_ser_pack_int32((krb5_int32) keyblock->length,
79 (void) krb5_ser_pack_bytes(keyblock->contents,
80 (size_t) keyblock->length,
83 /* Finally, our trailer */
84 (void) krb5_ser_pack_int32(KV5M_KEYBLOCK, &bp, &remain);
95 k5_internalize_keyblock(krb5_keyblock **argp,
96 krb5_octet **buffer, size_t *lenremain)
99 krb5_keyblock *keyblock;
107 /* Read our magic number */
108 if (krb5_ser_unpack_int32(&ibuf, &bp, &remain))
110 if (ibuf == KV5M_KEYBLOCK) {
114 if ((remain >= (3*sizeof(krb5_int32))) &&
115 (keyblock = (krb5_keyblock *) calloc(1, sizeof(krb5_keyblock)))) {
116 /* Get the enctype */
117 (void) krb5_ser_unpack_int32(&ibuf, &bp, &remain);
118 keyblock->enctype = (krb5_enctype) ibuf;
121 (void) krb5_ser_unpack_int32(&ibuf, &bp, &remain);
122 keyblock->length = (int) ibuf;
125 if ((keyblock->contents = (krb5_octet *) malloc((size_t) (ibuf)))&&
126 !(kret = krb5_ser_unpack_bytes(keyblock->contents,
129 kret = krb5_ser_unpack_int32(&ibuf, &bp, &remain);
130 if (!kret && (ibuf == KV5M_KEYBLOCK)) {
134 keyblock->magic = KV5M_KEYBLOCK;
141 if (keyblock->contents)
142 free(keyblock->contents);