You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
tbm-utils/source/x509.c

44 lines
739 B

#include <stdlib.h>
#include <string.h>
#include <x509.h>
X509 *X509_open_cert(const char *path)
{
X509 *cert;
FILE *fp;
if (!(fp = fopen(path, "r")))
return NULL;
cert = PEM_read_X509(fp, NULL, NULL, NULL);
fclose(fp);
return cert;
}
char *X509_get_common_name(X509 *cert)
{
X509_NAME *subj;
X509_NAME_ENTRY *entry;
ASN1_STRING *data;
unsigned char *cn;
int idx;
if (!(subj = X509_get_subject_name(cert)))
return NULL;
if ((idx = X509_NAME_get_index_by_NID(subj, NID_commonName, -1)) < 0)
return NULL;
if (!(entry = X509_NAME_get_entry(subj, idx)))
return NULL;
if (!(data = X509_NAME_ENTRY_get_data(entry)))
return NULL;
if (ASN1_STRING_to_UTF8(&cn, data) == 0)
return NULL;
return (char *)cn;
}