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.

int spc_verify_cert_hostname(X509 *cert, char *hostname) {<br />

int extcount, i, j, ok = 0;<br />

char name[256];<br />

X509_NAME *subj;<br />

const char *extstr;<br />

CONF_VALUE *nval;<br />

unsigned char *data;<br />

X509_EXTENSION *ext;<br />

X509V3_EXT_METHOD *meth;<br />

STACK_OF(CONF_VALUE) *val;<br />

if ((extcount = X509_get_ext_count(cert)) > 0) {<br />

for (i = 0; !ok && i < extcount; i++) {<br />

ext = X509_get_ext(cert, i);<br />

extstr = OBJ_nid2sn(OBJ_obj2nid(X509_EXTENSION_get_object(ext)));<br />

if (!strcasecmp(extstr, "subjectAltName")) {<br />

if (!(meth = X509V3_EXT_get(ext))) break;<br />

data = ext->value->data;<br />

val = meth->i2v(meth, meth->d2i(0, &data, ext->value->length), 0);<br />

for (j = 0; j < sk_CONF_VALUE_num(val); j++) {<br />

nval = sk_CONF_VALUE_value(val, j);<br />

if (!strcasecmp(nval->name, "DNS") && !strcasecmp(nval->value, hostname)) {<br />

ok = 1;<br />

break;<br />

}<br />

}<br />

}<br />

}<br />

}<br />

if (!ok && (subj = X509_get_subject_name(cert)) &&<br />

X509_NAME_get_text_by_NID(subj, NID_commonName, name, sizeof(name)) > 0) {<br />

name[sizeof(name) - 1] = '\0';<br />

if (!strcasecmp(name, hostname)) ok = 1;<br />

}<br />

return ok;<br />

}<br />

If you are using CryptoAPI on Windows, you will normally have a certificate as a<br />

CERT_CONTEXT object. The following code checks the hostname in that object:<br />

#include <br />

#include <br />

static LPWSTR fold_wide(LPWSTR str) {<br />

int len;<br />

LPWSTR wstr;<br />

if (!(len = FoldStringW(MAP_PRECOMPOSED, str, -1, 0, 0))) return 0;<br />

if (!(wstr = (LPWSTR)LocalAlloc(LMEM_FIXED, len * sizeof(WCHAR))))<br />

return 0;<br />

if (!FoldStringW(MAP_PRECOMPOSED, str, -1, wstr, len)) {<br />

Adding Hostname Checking to Certificate Verification | 541<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!