21.03.2013 Views

Problem - Kevin Tafuro

Problem - Kevin Tafuro

Problem - Kevin Tafuro

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

only work you really need to do is to decide on which message digest algorithm to<br />

use. MD5 is still the most popular algorithm, but we recommend using something<br />

stronger, such as SHA1. MD5 only has a 16-byte output, and there are known<br />

attacks against it, whereas SHA1 has a 20-byte output, and there are no known<br />

attacks against it.<br />

#include <br />

#include <br />

#include <br />

#include <br />

int spc_fingerprint_cert(X509 *cert, EVP_MD *digest, unsigned char *fingerprint,<br />

int *fingerprint_length) {<br />

if (*fingerprint_length < EVP_MD_size(digest))<br />

return 0;<br />

if (!X509_digest(cert, digest, fingerprint, fingerprint_length))<br />

return 0;<br />

return *fingerprint_length;<br />

}<br />

int spc_fingerprint_equal(unsigned char *fp1, int fp1len, unsigned char *fp2,<br />

int fp2len) {<br />

return (fp1len = = fp2len && !memcmp(fp1, fp2, fp1len));<br />

}<br />

Using CryptoAPI on Windows, computing the fingerprint of a certificate is also very<br />

simple. A single call to CryptHashCertificate( ) with the certificate’s CERT_CONTEXT<br />

object is all that’s necessary. The following implementation of SpcFingerPrintCert( )<br />

makes two calls so that it can verify that the buffer is big enough to hold the hash.<br />

#include <br />

#include <br />

DWORD SpcFingerPrintCert(PCCERT_CONTEXT pCertContext, ALG_ID Algid,<br />

BYTE *pbFingerPrint, DWORD *pcbFingerPrint) {<br />

DWORD cbComputedHash;<br />

if (!CryptHashCertificate(0, Algid, 0, pCertContext->pbCertEncoded,<br />

pCertContext->cbCertEncoded, 0, &cbComputedHash))<br />

return 0;<br />

if (*pcbFingerPrint < cbComputedHash) return 0;<br />

CryptHashCertificate(0, Algid, 0, pCertContext->pbCertEncoded,<br />

pCertContext->cbCertEncoded, pbFingerPrint,<br />

pcbFingerPrint);<br />

return *pcbFingerPrint;<br />

}<br />

int SpcFingerPrintEqual(BYTE *pbFingerPrint1, DWORD cbFingerPrint1,<br />

BYTE *pbFingerPrint2, DWORD cbFingerPrint2) {<br />

return (cbFingerPrint1 = = cbFingerPrint2 &&<br />

!memcmp(pbFingerPrint1, pbFingerPrint2, cbFingerPrint1));<br />

}<br />

Using a Whitelist to Verify Certificates | 545<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!