certinfo.c

Go to the documentation of this file.
00001 /* certinfo.c  --  Functions to parse and process the X509 TLS id string
00002  *
00003  *  GPLv2 only - Copyright (C) 2008 - 2010
00004  *               David Sommerseth <dazo@users.sourceforge.net>
00005  *
00006  *  This program is free software; you can redistribute it and/or
00007  *  modify it under the terms of the GNU General Public License
00008  *  as published by the Free Software Foundation; version 2
00009  *  of the License.
00010  *
00011  *  This program is distributed in the hope that it will be useful,
00012  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  *  GNU General Public License for more details.
00015  *
00016  *  You should have received a copy of the GNU General Public License
00017  *  along with this program; if not, write to the Free Software
00018  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
00019  *
00020  */
00021 
00032 #include <stdio.h>
00033 #include <stdlib.h>
00034 #include <string.h>
00035 
00036 #include <eurephia_nullsafe.h>
00037 #include <certinfo.h>
00038 
00047 #define comp_attrib(s, v) ( (v == NULL || strlen_nullsafe(v) < 1) ? 0 : (strcmp(v, s) == 0) )
00048 
00056 certinfo *parse_tlsid(const char *input) {
00057         char tmp[130], *mainp, *origptr, *sub, *tok, *tok2;
00058         certinfo *ret = NULL;
00059 
00060         if( (input == NULL) || strlen(input) < 5)
00061                 return NULL;
00062 
00063         ret = (certinfo *) malloc_nullsafe(NULL, sizeof(certinfo)+2);
00064         memset(&tmp, 0, 130);
00065 
00066         mainp = strdup(input);
00067         origptr = mainp;
00068         tok = strsep(&mainp, "/\0");
00069         while( tok != NULL ) {
00070                 if( (tok != NULL) && (strlen(tok) > 0 ) ) {
00071                         sub = strdup(tok);
00072                         tok2 = strsep(&sub, "=\0");
00073 
00074                         if( comp_attrib("O\0", tok2) ) {
00075                                 ret->org = strdup(strsep(&sub, "=\0"));
00076                         } else if( comp_attrib("CN\0", tok2) ) {
00077                                 ret->common_name = strdup(strsep(&sub, "=\0"));
00078                         } else if( comp_attrib("emailAddress\0", tok2) ) {
00079                                 ret->email = strdup(strsep(&sub, "=\0"));
00080                         }
00081                         if( tok2 != NULL ) {
00082                                 free(tok2); tok2 = NULL;
00083                         }
00084                 }
00085                 tok = strsep(&mainp, "/\0");
00086         }
00087         free(origptr); mainp = NULL; origptr = NULL;
00088 
00089         /* Make sure we at least have empty NULL terminated strings */
00090         if( ret->org == NULL ) {
00091                 ret->org = strdup("\0");
00092         }
00093         if( ret->common_name == NULL ) {
00094                 ret->common_name = strdup("\0");
00095         }
00096         if( ret->email == NULL ) {
00097                 ret->email = strdup("\0");
00098         }
00099 
00100         return ret;
00101 }
00102 
00103 
00109 void free_certinfo(certinfo *p) {
00110         if( p == NULL )
00111                 return;
00112 
00113         if( p->digest != NULL )
00114                 free(p->digest);
00115         if( p->org != NULL )
00116                 free(p->org);
00117         if( p->common_name != NULL )
00118                 free(p->common_name);
00119         if( p->email != NULL )
00120                 free(p->email);
00121 
00122         free(p);
00123 }
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines