From e157d0d3dbff55d75e104f6522b4849cc1616d92 Mon Sep 17 00:00:00 2001 From: fengweihao Date: Tue, 3 Sep 2019 15:00:17 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E4=BB=8Esanlist=E4=B8=AD?= =?UTF-8?q?=E8=8E=B7=E5=8F=96subjectname=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/x509.c | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 58 insertions(+), 4 deletions(-) diff --git a/src/x509.c b/src/x509.c index fb1f44a..e40ad77 100644 --- a/src/x509.c +++ b/src/x509.c @@ -876,7 +876,45 @@ char *str_trim(const char *str) return strRet; } -static int set_altname(X509 *crt, int type, const char *sanfile) +int add_cert_ctx(X509_NAME* name, char* ctx[], int num) +{ + int i = 0; + int max = 0; + + int item[] = {NID_commonName, NID_countryName, + NID_stateOrProvinceName, NID_localityName, + NID_organizationName, NID_organizationalUnitName, + NID_pkcs9_emailAddress}; + + max = sizeof(item)/sizeof(item[0]); + max = max > num ? num : max; + + for(i = 0; i< max; ++i){ + if(!X509_NAME_add_entry_by_NID(name, item[i], MBSTRING_UTF8, (unsigned char *)ctx[i], -1, -1, 0)){ + } + } + + return 1; +} + +static void x509_set_subject(X509 *x509, char *subject) +{ +#define SUBJECT_NAME_MAX 126 + char seps[] = ","; + char *item = strtok(subject, seps); + char key[SUBJECT_NAME_MAX] = {0}, value[SUBJECT_NAME_MAX] = {0}; + + X509_NAME *name = X509_get_subject_name(x509); + while (item) + { + sscanf(item, " %[^=]=%s", key, value); + X509_NAME_add_entry_by_txt(name, key, MBSTRING_UTF8, (unsigned char*)value, -1, -1, 0); + item = strtok(NULL, seps); + } + free(subject); +} + +static int x509_set_altname(X509 *crt, int type, const char *sanfile, char **subjectname) { int ret = 0; GENERAL_NAMES *gens = NULL; @@ -891,9 +929,20 @@ static int set_altname(X509 *crt, int type, const char *sanfile) if (buff == NULL){ goto finish; } + char seps1[] = "\n", seps[] = ";"; char *sanline=NULL, *host = NULL; - char seps[] = ";"; - sanline = strtok(buff, seps); + if (buff[0] != '\n') + { + char *subject = strtok(buff, seps1); + if (subject != NULL) + { + *subjectname = strdup(subject); + } + sanline = strtok(NULL, seps); + }else + { + sanline = strtok(buff+1, seps); + } while (sanline) { asprintf(&host, "%s", sanline); @@ -947,7 +996,12 @@ int x509_check_host(const char *sanfile, const char *urlfile) X509 *x509 = make_cert(); if (x509 == NULL) return -1; - set_altname(x509, GEN_DNS, sanfile); + char *subject = NULL; + x509_set_altname(x509, GEN_DNS, sanfile,&subject); + if (subject != NULL) + { + x509_set_subject(x509, subject); + } fp = fopen(urlfile, "r"); assert(fp != NULL); while(fgets(line, LINE_SIZE - 1, fp))