/* certinfo.c -- Functions to parse and process the X509 TLS id string * * GPLv2 - Copyright (C) 2008 David Sommerseth * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; version 2 * of the License. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * */ #include #include #include #include #include #define comp_attrib(s, v) ( (v == NULL || strlen_nullsafe(v) < 1) ? 0 : (strcmp(v, s) == 0) ) certinfo *parse_tlsid(const char *input) { char tmp[130], *mainp, *origptr, *sub, *tok, *tok2; certinfo *ret = NULL; if( (input == NULL) || strlen(input) < 5) return NULL; ret = (certinfo *) malloc(sizeof(certinfo)+2); bzero(ret, sizeof(certinfo)+2); bzero(&tmp, 130); mainp = strdup(input); origptr = mainp; tok = strsep(&mainp, "/\0"); while( tok != NULL ) { if( (tok != NULL) && (strlen(tok) > 0 ) ) { sub = strdup(tok); tok2 = strsep(&sub, "=\0"); if( comp_attrib("O\0", tok2) ) { ret->org = strdup(strsep(&sub, "=\0")); } else if( comp_attrib("CN\0", tok2) ) { ret->common_name = strdup(strsep(&sub, "=\0")); } else if( comp_attrib("emailAddress\0", tok2) ) { ret->email = strdup(strsep(&sub, "=\0")); } if( tok2 != NULL ) { free(tok2); tok2 = NULL; } } tok = strsep(&mainp, "/\0"); } free(origptr); mainp = NULL; origptr = NULL; /* Make sure we at least have empty NULL terminated strings */ if( ret->org == NULL ) { ret->org = strdup("\0"); } if( ret->common_name == NULL ) { ret->common_name = strdup("\0"); } if( ret->email == NULL ) { ret->email = strdup("\0"); } return ret; } void free_certinfo(certinfo *p) { if( p == NULL ) return; if( p->org != NULL ) free(p->org); if( p->common_name != NULL ) free(p->common_name); if( p->email != NULL ) free(p->email); free(p); }