aheinecke@404: /* Copyright (C) 2014 by Bundesamt für Sicherheit in der Informationstechnik aheinecke@404: * Software engineering by Intevation GmbH aheinecke@404: * aheinecke@404: * This file is Free Software under the GNU GPL (v>=2) aheinecke@404: * and comes with ABSOLUTELY NO WARRANTY! aheinecke@404: * See LICENSE.txt for details. aheinecke@404: */ wilde@259: #include wilde@259: wilde@259: #include "certhelp.h" wilde@260: #include "logging.h" wilde@259: #include "errorcodes.h" wilde@259: #include "strhelp.h" wilde@259: wilde@259: char * wilde@259: get_oid_valstr(x509_name *namebuf, unsigned char *oid) wilde@259: { wilde@259: char *str = NULL; wilde@259: size_t oid_len = strlen((char *)oid); wilde@259: while ( namebuf != NULL ) wilde@259: { wilde@259: if ( (namebuf->oid.len == oid_len) && wilde@259: (memcmp(namebuf->oid.p, oid, oid_len) == 0) ) wilde@259: { wilde@259: str = xstrndup((char *)namebuf->val.p, namebuf->val.len); wilde@259: break; wilde@259: } wilde@259: namebuf = namebuf->next; wilde@259: } wilde@259: return str; wilde@259: } wilde@259: wilde@259: char * wilde@259: x509_parse_subject(unsigned char *derdata, size_t derlen, wilde@259: unsigned char *oid) wilde@259: { wilde@259: x509_crt chain; wilde@259: char *str; wilde@259: wilde@259: x509_crt_init(&chain); wilde@259: if (x509_crt_parse_der(&chain, derdata, derlen) != 0) wilde@259: { aheinecke@332: ERRORPRINTF("Could not parse certificate!\n"); aheinecke@332: return NULL; wilde@259: } wilde@259: else wilde@259: { wilde@259: str = get_oid_valstr(&(chain.subject), oid); wilde@259: x509_crt_free(&chain); wilde@259: } wilde@259: return str; wilde@259: } andre@1288: andre@1288: #ifdef WIN32 andre@1288: PCCERT_CONTEXT andre@1288: b64_to_cert_context(char *b64_data, size_t b64_size) andre@1288: { andre@1288: size_t buf_size = 0; andre@1288: char *buf = NULL; andre@1288: PCCERT_CONTEXT pCert = NULL; andre@1288: int ret = -1; andre@1288: andre@1288: ret = str_base64_decode (&buf, &buf_size, b64_data, b64_size); andre@1288: andre@1288: if (ret != 0) andre@1288: { andre@1288: ERRORPRINTF ("decoding certificate failed\n"); andre@1288: return NULL; andre@1288: } andre@1288: andre@1288: pCert = CertCreateContext (CERT_STORE_CERTIFICATE_CONTEXT, andre@1288: X509_ASN_ENCODING | PKCS_7_ASN_ENCODING, andre@1288: (const PBYTE) buf, andre@1288: (DWORD) buf_size, andre@1288: 0, andre@1288: NULL); andre@1288: free (buf); /* Windows has a copy */ andre@1288: andre@1288: if (pCert == NULL) andre@1288: { andre@1288: char *error = getLastErrorMsg(); andre@1288: if (error) andre@1288: { andre@1288: ERRORPRINTF ("Failed to create cert context: %s \n", error); andre@1288: free (error); andre@1288: } andre@1288: return NULL; andre@1288: } andre@1288: return pCert; andre@1288: } andre@1288: #endif