From d84461437ce3bb495c0144f409a00795203a1188 Mon Sep 17 00:00:00 2001 From: fengweihao Date: Thu, 26 Sep 2019 15:04:19 +0800 Subject: [PATCH] =?UTF-8?q?*=20=E4=BF=AE=E6=94=B9sanlist=E4=B8=ADCN?= =?UTF-8?q?=E4=BB=A5=E8=A1=8C=E4=B8=BA=E5=88=86=E5=89=B2=E7=9A=84=E8=AF=BB?= =?UTF-8?q?=E5=8F=96=E6=96=B9=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/x509.c | 38 ++++++++++++-------------------------- 1 file changed, 12 insertions(+), 26 deletions(-) diff --git a/src/x509.c b/src/x509.c index 3f25559..e58428e 100644 --- a/src/x509.c +++ b/src/x509.c @@ -901,24 +901,14 @@ int add_cert_ctx(X509_NAME* name, char* ctx[], int num) 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); - } + X509_NAME_add_entry_by_txt(name, "CN", MBSTRING_UTF8, (unsigned char*)subject, -1, -1, 0); free(subject); } static int x509_set_altname(X509 *crt, int type, const char *sanfile, char **subjectname) { - int ret = 0; + int ret = 0, num = 0; GENERAL_NAMES *gens = NULL; GENERAL_NAME *gen = NULL; ASN1_IA5STRING *ia5 = NULL; @@ -931,23 +921,17 @@ static int x509_set_altname(X509 *crt, int type, const char *sanfile, char **sub if (buff == NULL){ goto finish; } - char seps1[] = "\n", seps[] = ";"; - char *sanline=NULL, *host = NULL; - 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); - } + char *sanline = NULL, *host = NULL; + char seps[] = ";"; + sanline = strtok(buff, seps); while (sanline) { asprintf(&host, "%s", sanline); + if (num == 0) + { + *subjectname = host; + goto next; + } gen = GENERAL_NAME_new(); if (gen == NULL) goto out; @@ -968,11 +952,13 @@ static int x509_set_altname(X509 *crt, int type, const char *sanfile, char **sub sk_GENERAL_NAME_push(gens, gen); gen = NULL; free(host); +next: sanline = strtok(NULL, seps); if (sanline == NULL || strlen(sanline) == 1) { break; } + num++; } if (!X509_add1_ext_i2d(crt, NID_subject_alt_name, gens, 0, 0)) goto out;