Skip to content

Commit

Permalink
Implement request #48520: openssl_csr_new should allow multiple value…
Browse files Browse the repository at this point in the history
…s/fields in dn

Closes GH-12984
  • Loading branch information
bukka committed Dec 21, 2023
1 parent e8fde6b commit 48ebe58
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 25 deletions.
2 changes: 2 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ Opcache:
OpenSSL:
. Fixed bug #80269 (OpenSSL sets Subject wrong with extraattribs parameter).
(Jakub Zelenka)
. Implement request #48520 (openssl_csr_new - allow multiple values in DN).
(Jakub Zelenka)

PDO:
. Fixed setAttribute and getAttribute (SakiTakamachi)
Expand Down
2 changes: 2 additions & 0 deletions UPGRADING
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,8 @@ PHP 8.4 UPGRADE NOTES
- OpenSSL:
. The extra_attributes parameter in openssl_csr_new sets CSR attributes
instead of subject DN which was incorrectly done previously.
. The dn parameter in openssl_csr_new allows setting array of values for
a single entry.

- PDO:
. getAttribute, enabled to get the value of ATTR_STRINGIFY_FETCHES.
Expand Down
61 changes: 36 additions & 25 deletions ext/openssl/openssl.c
Original file line number Diff line number Diff line change
Expand Up @@ -2863,8 +2863,29 @@ PHP_FUNCTION(openssl_pkcs12_read)

/* {{{ x509 CSR functions */

/* {{{ php_openssl_make_REQ */
static int php_openssl_make_REQ(struct php_x509_request * req, X509_REQ * csr, zval * dn, zval * attribs)
static zend_result php_openssl_csr_add_subj_entry(zval *item, X509_NAME *subj, int nid)
{
zend_string *str_item = zval_try_get_string(item);
if (UNEXPECTED(!str_item)) {
return FAILURE;
}
if (!X509_NAME_add_entry_by_NID(subj, nid, MBSTRING_UTF8,
(unsigned char*)ZSTR_VAL(str_item), -1, -1, 0))
{
php_openssl_store_errors();
php_error_docref(NULL, E_WARNING,
"dn: add_entry_by_NID %d -> %s (failed; check error"
" queue and value of string_mask OpenSSL option "
"if illegal characters are reported)",
nid, ZSTR_VAL(str_item));
zend_string_release(str_item);
return FAILURE;
}
zend_string_release(str_item);
return SUCCESS;
}

static zend_result php_openssl_csr_make(struct php_x509_request * req, X509_REQ * csr, zval * dn, zval * attribs)
{
STACK_OF(CONF_VALUE) * dn_sk, *attr_sk = NULL;
char * str, *dn_sect, *attr_sect;
Expand Down Expand Up @@ -2892,35 +2913,27 @@ static int php_openssl_make_REQ(struct php_x509_request * req, X509_REQ * csr, z
/* setup the version number: version 1 */
if (X509_REQ_set_version(csr, 0L)) {
int i, nid;
char * type;
CONF_VALUE * v;
X509_NAME * subj;
zval * item;
zend_string * strindex = NULL;
char *type;
CONF_VALUE *v;
X509_NAME *subj;
zval *item, *subitem;
zend_string *strindex = NULL;

subj = X509_REQ_get_subject_name(csr);
/* apply values from the dn hash */
ZEND_HASH_FOREACH_STR_KEY_VAL(Z_ARRVAL_P(dn), strindex, item) {
if (strindex) {
int nid = OBJ_txt2nid(ZSTR_VAL(strindex));
if (nid != NID_undef) {
zend_string *str_item = zval_try_get_string(item);
if (UNEXPECTED(!str_item)) {
return FAILURE;
}
if (!X509_NAME_add_entry_by_NID(subj, nid, MBSTRING_UTF8,
(unsigned char*)ZSTR_VAL(str_item), -1, -1, 0))
{
php_openssl_store_errors();
php_error_docref(NULL, E_WARNING,
"dn: add_entry_by_NID %d -> %s (failed; check error"
" queue and value of string_mask OpenSSL option "
"if illegal characters are reported)",
nid, ZSTR_VAL(str_item));
zend_string_release(str_item);
if (Z_TYPE_P(item) == IS_ARRAY) {
ZEND_HASH_FOREACH_NUM_KEY_VAL(Z_ARRVAL_P(item), i, subitem) {
if (php_openssl_csr_add_subj_entry(subitem, subj, nid) == FAILURE) {
return FAILURE;
}
} ZEND_HASH_FOREACH_END();
} else if (php_openssl_csr_add_subj_entry(item, subj, nid) == FAILURE) {
return FAILURE;
}
zend_string_release(str_item);
} else {
php_error_docref(NULL, E_WARNING, "dn: %s is not a recognized name", ZSTR_VAL(strindex));
}
Expand Down Expand Up @@ -3029,8 +3042,6 @@ static int php_openssl_make_REQ(struct php_x509_request * req, X509_REQ * csr, z
}
return SUCCESS;
}
/* }}} */


static X509_REQ *php_openssl_csr_from_str(zend_string *csr_str, uint32_t arg_num)
{
Expand Down Expand Up @@ -3370,7 +3381,7 @@ PHP_FUNCTION(openssl_csr_new)
} else {
csr = X509_REQ_new();
if (csr) {
if (php_openssl_make_REQ(&req, csr, dn, attribs) == SUCCESS) {
if (php_openssl_csr_make(&req, csr, dn, attribs) == SUCCESS) {
X509V3_CTX ext_ctx;

X509V3_set_ctx(&ext_ctx, NULL, NULL, csr, NULL, 0);
Expand Down

2 comments on commit 48ebe58

@bukka
Copy link
Member Author

@bukka bukka commented on 48ebe58 Dec 22, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh I forgot to git add a test

@bukka
Copy link
Member Author

@bukka bukka commented on 48ebe58 Dec 22, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PR in #13003

Please sign in to comment.