Move do_subject() to apps.c and rename it to parse_name(). The
rationale behind the move is that it's use by several applications. The rationale behind the name change is that it describes what the function does a bit better.
This commit is contained in:
parent
7ce9e425bc
commit
6d5ffb591b
137
apps/apps.c
137
apps/apps.c
@ -1961,3 +1961,140 @@ void free_index(CA_DB *db)
|
|||||||
OPENSSL_free(db);
|
OPENSSL_free(db);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* subject is expected to be in the format /type0=value0/type1=value1/type2=...
|
||||||
|
* where characters may be escaped by \
|
||||||
|
*/
|
||||||
|
X509_NAME *parse_name(char *subject, long chtype, int multirdn)
|
||||||
|
{
|
||||||
|
size_t buflen = strlen(subject)+1; /* to copy the types and values into. due to escaping, the copy can only become shorter */
|
||||||
|
char *buf = OPENSSL_malloc(buflen);
|
||||||
|
size_t max_ne = buflen / 2 + 1; /* maximum number of name elements */
|
||||||
|
char **ne_types = OPENSSL_malloc(max_ne * sizeof (char *));
|
||||||
|
char **ne_values = OPENSSL_malloc(max_ne * sizeof (char *));
|
||||||
|
int *mval = OPENSSL_malloc (max_ne * sizeof (int));
|
||||||
|
|
||||||
|
char *sp = subject, *bp = buf;
|
||||||
|
int i, ne_num = 0;
|
||||||
|
|
||||||
|
X509_NAME *n = NULL;
|
||||||
|
int nid;
|
||||||
|
|
||||||
|
if (!buf || !ne_types || !ne_values)
|
||||||
|
{
|
||||||
|
BIO_printf(bio_err, "malloc error\n");
|
||||||
|
goto error;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (*subject != '/')
|
||||||
|
{
|
||||||
|
BIO_printf(bio_err, "Subject does not start with '/'.\n");
|
||||||
|
goto error;
|
||||||
|
}
|
||||||
|
sp++; /* skip leading / */
|
||||||
|
|
||||||
|
/* no multivalued RDN by default */
|
||||||
|
mval[ne_num] = 0;
|
||||||
|
|
||||||
|
while (*sp)
|
||||||
|
{
|
||||||
|
/* collect type */
|
||||||
|
ne_types[ne_num] = bp;
|
||||||
|
while (*sp)
|
||||||
|
{
|
||||||
|
if (*sp == '\\') /* is there anything to escape in the type...? */
|
||||||
|
{
|
||||||
|
if (*++sp)
|
||||||
|
*bp++ = *sp++;
|
||||||
|
else
|
||||||
|
{
|
||||||
|
BIO_printf(bio_err, "escape character at end of string\n");
|
||||||
|
goto error;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (*sp == '=')
|
||||||
|
{
|
||||||
|
sp++;
|
||||||
|
*bp++ = '\0';
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
*bp++ = *sp++;
|
||||||
|
}
|
||||||
|
if (!*sp)
|
||||||
|
{
|
||||||
|
BIO_printf(bio_err, "end of string encountered while processing type of subject name element #%d\n", ne_num);
|
||||||
|
goto error;
|
||||||
|
}
|
||||||
|
ne_values[ne_num] = bp;
|
||||||
|
while (*sp)
|
||||||
|
{
|
||||||
|
if (*sp == '\\')
|
||||||
|
{
|
||||||
|
if (*++sp)
|
||||||
|
*bp++ = *sp++;
|
||||||
|
else
|
||||||
|
{
|
||||||
|
BIO_printf(bio_err, "escape character at end of string\n");
|
||||||
|
goto error;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (*sp == '/')
|
||||||
|
{
|
||||||
|
sp++;
|
||||||
|
/* no multivalued RDN by default */
|
||||||
|
mval[ne_num+1] = 0;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
else if (*sp == '+' && multirdn)
|
||||||
|
{
|
||||||
|
/* a not escaped + signals a mutlivalued RDN */
|
||||||
|
sp++;
|
||||||
|
mval[ne_num+1] = -1;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
*bp++ = *sp++;
|
||||||
|
}
|
||||||
|
*bp++ = '\0';
|
||||||
|
ne_num++;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!(n = X509_NAME_new()))
|
||||||
|
goto error;
|
||||||
|
|
||||||
|
for (i = 0; i < ne_num; i++)
|
||||||
|
{
|
||||||
|
if ((nid=OBJ_txt2nid(ne_types[i])) == NID_undef)
|
||||||
|
{
|
||||||
|
BIO_printf(bio_err, "Subject Attribute %s has no known NID, skipped\n", ne_types[i]);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!*ne_values[i])
|
||||||
|
{
|
||||||
|
BIO_printf(bio_err, "No value provided for Subject Attribute %s, skipped\n", ne_types[i]);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!X509_NAME_add_entry_by_NID(n, nid, chtype, (unsigned char*)ne_values[i], -1,-1,mval[i]))
|
||||||
|
goto error;
|
||||||
|
}
|
||||||
|
|
||||||
|
OPENSSL_free(ne_values);
|
||||||
|
OPENSSL_free(ne_types);
|
||||||
|
OPENSSL_free(buf);
|
||||||
|
return n;
|
||||||
|
|
||||||
|
error:
|
||||||
|
X509_NAME_free(n);
|
||||||
|
if (ne_values)
|
||||||
|
OPENSSL_free(ne_values);
|
||||||
|
if (ne_types)
|
||||||
|
OPENSSL_free(ne_types);
|
||||||
|
if (buf)
|
||||||
|
OPENSSL_free(buf);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
138
apps/ca.c
138
apps/ca.c
@ -1676,7 +1676,7 @@ static int do_body(X509 **xret, EVP_PKEY *pkey, X509 *x509, const EVP_MD *dgst,
|
|||||||
|
|
||||||
if (subj)
|
if (subj)
|
||||||
{
|
{
|
||||||
X509_NAME *n = do_subject(subj, MBSTRING_ASC, multirdn);
|
X509_NAME *n = parse_name(subj, MBSTRING_ASC, multirdn);
|
||||||
|
|
||||||
if (!n)
|
if (!n)
|
||||||
{
|
{
|
||||||
@ -2843,142 +2843,6 @@ int make_revoked(X509_REVOKED *rev, char *str)
|
|||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
* subject is expected to be in the format /type0=value0/type1=value1/type2=...
|
|
||||||
* where characters may be escaped by \
|
|
||||||
*/
|
|
||||||
X509_NAME *do_subject(char *subject, long chtype, int multirdn)
|
|
||||||
{
|
|
||||||
size_t buflen = strlen(subject)+1; /* to copy the types and values into. due to escaping, the copy can only become shorter */
|
|
||||||
char *buf = OPENSSL_malloc(buflen);
|
|
||||||
size_t max_ne = buflen / 2 + 1; /* maximum number of name elements */
|
|
||||||
char **ne_types = OPENSSL_malloc(max_ne * sizeof (char *));
|
|
||||||
char **ne_values = OPENSSL_malloc(max_ne * sizeof (char *));
|
|
||||||
int *mval = OPENSSL_malloc (max_ne * sizeof (int));
|
|
||||||
|
|
||||||
char *sp = subject, *bp = buf;
|
|
||||||
int i, ne_num = 0;
|
|
||||||
|
|
||||||
X509_NAME *n = NULL;
|
|
||||||
int nid;
|
|
||||||
|
|
||||||
if (!buf || !ne_types || !ne_values)
|
|
||||||
{
|
|
||||||
BIO_printf(bio_err, "malloc error\n");
|
|
||||||
goto error;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (*subject != '/')
|
|
||||||
{
|
|
||||||
BIO_printf(bio_err, "Subject does not start with '/'.\n");
|
|
||||||
goto error;
|
|
||||||
}
|
|
||||||
sp++; /* skip leading / */
|
|
||||||
|
|
||||||
/* no multivalued RDN by default */
|
|
||||||
mval[ne_num] = 0;
|
|
||||||
|
|
||||||
while (*sp)
|
|
||||||
{
|
|
||||||
/* collect type */
|
|
||||||
ne_types[ne_num] = bp;
|
|
||||||
while (*sp)
|
|
||||||
{
|
|
||||||
if (*sp == '\\') /* is there anything to escape in the type...? */
|
|
||||||
{
|
|
||||||
if (*++sp)
|
|
||||||
*bp++ = *sp++;
|
|
||||||
else
|
|
||||||
{
|
|
||||||
BIO_printf(bio_err, "escape character at end of string\n");
|
|
||||||
goto error;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else if (*sp == '=')
|
|
||||||
{
|
|
||||||
sp++;
|
|
||||||
*bp++ = '\0';
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
*bp++ = *sp++;
|
|
||||||
}
|
|
||||||
if (!*sp)
|
|
||||||
{
|
|
||||||
BIO_printf(bio_err, "end of string encountered while processing type of subject name element #%d\n", ne_num);
|
|
||||||
goto error;
|
|
||||||
}
|
|
||||||
ne_values[ne_num] = bp;
|
|
||||||
while (*sp)
|
|
||||||
{
|
|
||||||
if (*sp == '\\')
|
|
||||||
{
|
|
||||||
if (*++sp)
|
|
||||||
*bp++ = *sp++;
|
|
||||||
else
|
|
||||||
{
|
|
||||||
BIO_printf(bio_err, "escape character at end of string\n");
|
|
||||||
goto error;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else if (*sp == '/')
|
|
||||||
{
|
|
||||||
sp++;
|
|
||||||
/* no multivalued RDN by default */
|
|
||||||
mval[ne_num+1] = 0;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
else if (*sp == '+' && multirdn)
|
|
||||||
{
|
|
||||||
/* a not escaped + signals a mutlivalued RDN */
|
|
||||||
sp++;
|
|
||||||
mval[ne_num+1] = -1;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
*bp++ = *sp++;
|
|
||||||
}
|
|
||||||
*bp++ = '\0';
|
|
||||||
ne_num++;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!(n = X509_NAME_new()))
|
|
||||||
goto error;
|
|
||||||
|
|
||||||
for (i = 0; i < ne_num; i++)
|
|
||||||
{
|
|
||||||
if ((nid=OBJ_txt2nid(ne_types[i])) == NID_undef)
|
|
||||||
{
|
|
||||||
BIO_printf(bio_err, "Subject Attribute %s has no known NID, skipped\n", ne_types[i]);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!*ne_values[i])
|
|
||||||
{
|
|
||||||
BIO_printf(bio_err, "No value provided for Subject Attribute %s, skipped\n", ne_types[i]);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!X509_NAME_add_entry_by_NID(n, nid, chtype, (unsigned char*)ne_values[i], -1,-1,mval[i]))
|
|
||||||
goto error;
|
|
||||||
}
|
|
||||||
|
|
||||||
OPENSSL_free(ne_values);
|
|
||||||
OPENSSL_free(ne_types);
|
|
||||||
OPENSSL_free(buf);
|
|
||||||
return n;
|
|
||||||
|
|
||||||
error:
|
|
||||||
X509_NAME_free(n);
|
|
||||||
if (ne_values)
|
|
||||||
OPENSSL_free(ne_values);
|
|
||||||
if (ne_types)
|
|
||||||
OPENSSL_free(ne_types);
|
|
||||||
if (buf)
|
|
||||||
OPENSSL_free(buf);
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
int old_entry_print(BIO *bp, ASN1_OBJECT *obj, ASN1_STRING *str)
|
int old_entry_print(BIO *bp, ASN1_OBJECT *obj, ASN1_STRING *str)
|
||||||
{
|
{
|
||||||
char buf[25],*pbuf, *p;
|
char buf[25],*pbuf, *p;
|
||||||
|
@ -1248,7 +1248,7 @@ static int build_subject(X509_REQ *req, char *subject, unsigned long chtype, int
|
|||||||
{
|
{
|
||||||
X509_NAME *n;
|
X509_NAME *n;
|
||||||
|
|
||||||
if (!(n = do_subject(subject, chtype, multirdn)))
|
if (!(n = parse_name(subject, chtype, multirdn)))
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
if (!X509_REQ_set_subject_name(req, n))
|
if (!X509_REQ_set_subject_name(req, n))
|
||||||
|
Loading…
x
Reference in New Issue
Block a user