21.03.2013 Views

Problem - Kevin Tafuro

Problem - Kevin Tafuro

Problem - Kevin Tafuro

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

while the two implementations share much of the same API, there are differences. In<br />

particular, the API for encryption services that we will be using in this recipe differs<br />

between the two. To determine which implementation is being used, we test for the<br />

existence of the KRB5_GENERAL__ preprocessor macro, which will be defined by the<br />

MIT implementation but not the Heimdal implementation.<br />

Given a krb5_keyblock object, you can determine whether DES was negotiated using<br />

the following function:<br />

#include <br />

int spc_krb5_isdes(krb5_keyblock *key) {<br />

#ifdef KRB5_GENERAL__<br />

if (key->enctype = = ENCTYPE_DES_CBC_CRC || key->enctype = = ENCTYPE_DES_CBC_MD4 ||<br />

key->enctype = = ENCTYPE_DES_CBC_MD5 || key->enctype = = ENCTYPE_DES_CBC_RAW)<br />

return 1;<br />

#else<br />

if (key->keytype = = ETYPE_DES_CBC_CRC || key->keytype = = ETYPE_DES_CBC_MD4 ||<br />

key->keytype = = ETYPE_DES_CBC_MD5 || key->keytype = = ETYPE_DES_CBC_NONE ||<br />

key->keytype = = ETYPE_DES_CFB64_NONE || key->keytype = = ETYPE_DES_PCBC_NONE)<br />

return 1;<br />

#endif<br />

return 0;<br />

}<br />

The krb5_context object and the krb5_keyblock object can then be used together as<br />

arguments to spc_krb5_encrypt( ), which we implement below. The function also<br />

requires a buffer that holds the data to be encrypted along with the size of the buffer,<br />

as well as a pointer to receive a dynamically allocated buffer that will hold the<br />

encrypted data on return, and a pointer to receive the size of the encrypted data<br />

buffer.<br />

#include <br />

#include <br />

#include <br />

#include <br />

int spc_krb5_encrypt(krb5_context ctx, krb5_keyblock *key, void *inbuf,<br />

size_t inlen, void **outbuf, size_t *outlen) {<br />

#ifdef KRB5_GENERAL__<br />

size_t blksz, newlen;<br />

krb5_data in_data;<br />

krb5_enc_data out_data;<br />

if (krb5_c_block_size(ctx, key->enctype, &blksz)) return 0;<br />

if (!(inlen % blksz)) newlen = inlen + blksz;<br />

else newlen = ((inlen + blksz - 1) / blksz) * blksz;<br />

in_data.magic = KV5M_DATA;<br />

in_data.length = newlen;<br />

in_data.data = malloc(newlen);<br />

if (!in_data.data) return 0;<br />

472 | Chapter 9: Networking<br />

This is the Title of the Book, eMatter Edition<br />

Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!