comparison common/certhelp.c @ 259:20d515604daa

Added new module with helper functions to parse certs. Currently only stuff to get O and CN from Subject DN.
author Sascha Wilde <wilde@intevation.de>
date Tue, 01 Apr 2014 15:41:11 +0200
parents
children e7a8b70021b6
comparison
equal deleted inserted replaced
258:bf8c74992724 259:20d515604daa
1 #include <stdlib.h>
2
3 #include "certhelp.h"
4 #include "debug.h"
5 #include "errorcodes.h"
6 #include "strhelp.h"
7
8 char *
9 get_oid_valstr(x509_name *namebuf, unsigned char *oid)
10 {
11 char *str = NULL;
12 size_t oid_len = strlen((char *)oid);
13 while ( namebuf != NULL )
14 {
15 if ( (namebuf->oid.len == oid_len) &&
16 (memcmp(namebuf->oid.p, oid, oid_len) == 0) )
17 {
18 str = xstrndup((char *)namebuf->val.p, namebuf->val.len);
19 break;
20 }
21 namebuf = namebuf->next;
22 }
23 return str;
24 }
25
26 char *
27 x509_parse_subject(unsigned char *derdata, size_t derlen,
28 unsigned char *oid)
29 {
30 x509_crt chain;
31 char *str;
32
33 x509_crt_init(&chain);
34 if (x509_crt_parse_der(&chain, derdata, derlen) != 0)
35 {
36 DEBUGPRINTF("FATAL: Could not parse certificate!");
37 exit(ERR_INVALID_CERT);
38 }
39 else
40 {
41 str = get_oid_valstr(&(chain.subject), oid);
42 x509_crt_free(&chain);
43 }
44 return str;
45 }

http://wald.intevation.org/projects/trustbridge/