Skip to content

Commit 8dc6136

Browse files
committed
- add openssl_csr_get_subject() and openssl_csr_get_public_key()
1 parent 118a6a9 commit 8dc6136

File tree

3 files changed

+70
-7
lines changed

3 files changed

+70
-7
lines changed

NEWS

+1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ PHP NEWS
2929
. Added openssl_pkey_get_details, returns the details of a key
3030
. Added x509 v3 extensions support
3131
. Added a new constant OPENSSL_KEYTYPE_EC
32+
. Added openssl_csr_get_subject() and openssl_csr_get_public_key()
3233

3334
- Fixed overflow on 64bit systems in str_repeat() and wordwrap(). (Stefan E.)
3435
- Disabled CURLOPT_FOLLOWLOCATION in curl when open_basedir or safe_mode are

ext/openssl/openssl.c

+67-6
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,8 @@ zend_function_entry openssl_functions[] = {
113113
PHP_FE(openssl_csr_export, second_arg_force_ref)
114114
PHP_FE(openssl_csr_export_to_file, NULL)
115115
PHP_FE(openssl_csr_sign, NULL)
116+
PHP_FE(openssl_csr_get_subject, NULL)
117+
PHP_FE(openssl_csr_get_public_key, NULL)
116118

117119
PHP_FE(openssl_sign, second_arg_force_ref)
118120
PHP_FE(openssl_verify, NULL)
@@ -248,9 +250,13 @@ static void add_assoc_name_entry(zval * val, char * key, X509_NAME * name, int s
248250
ASN1_STRING * str = NULL;
249251
ASN1_OBJECT * obj;
250252

251-
MAKE_STD_ZVAL(subitem);
252-
array_init(subitem);
253-
253+
if (key != NULL) {
254+
MAKE_STD_ZVAL(subitem);
255+
array_init(subitem);
256+
} else {
257+
subitem = val;
258+
}
259+
254260
for (i = 0; i < X509_NAME_entry_count(name); i++) {
255261
ne = X509_NAME_get_entry(name, i);
256262
obj = X509_NAME_ENTRY_get_object(ne);
@@ -291,7 +297,9 @@ static void add_assoc_name_entry(zval * val, char * key, X509_NAME * name, int s
291297
}
292298
}
293299
}
294-
zend_hash_update(HASH_OF(val), key, strlen(key) + 1, (void *)&subitem, sizeof(subitem), NULL);
300+
if (key != NULL) {
301+
zend_hash_update(HASH_OF(val), key, strlen(key) + 1, (void *)&subitem, sizeof(subitem), NULL);
302+
}
295303
}
296304
/* }}} */
297305

@@ -1527,8 +1535,6 @@ PHP_FUNCTION(openssl_csr_export_to_file)
15271535
}
15281536
/* }}} */
15291537

1530-
1531-
15321538
/* {{{ proto bool openssl_csr_export(resource csr, string &out [, bool notext=true])
15331539
Exports a CSR to file or a var */
15341540
PHP_FUNCTION(openssl_csr_export)
@@ -1789,6 +1795,61 @@ PHP_FUNCTION(openssl_csr_new)
17891795
}
17901796
/* }}} */
17911797

1798+
/* {{{ proto mixed openssl_csr_get_subject(mixed csr)
1799+
Returns the subject of a CERT or FALSE on error */
1800+
PHP_FUNCTION(openssl_csr_get_subject)
1801+
{
1802+
zval * zcsr;
1803+
zend_bool use_shortnames = 1;
1804+
long csr_resource;
1805+
X509_NAME * subject;
1806+
X509_REQ * csr;
1807+
1808+
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z|b", &zcsr, &use_shortnames) == FAILURE) {
1809+
return;
1810+
}
1811+
1812+
csr = php_openssl_csr_from_zval(&zcsr, 0, &csr_resource TSRMLS_CC);
1813+
1814+
if (csr == NULL) {
1815+
RETURN_FALSE;
1816+
}
1817+
1818+
subject = X509_REQ_get_subject_name(csr);
1819+
1820+
array_init(return_value);
1821+
add_assoc_name_entry(return_value, NULL, subject, use_shortnames TSRMLS_CC);
1822+
return;
1823+
}
1824+
/* }}} */
1825+
1826+
/* {{{ proto mixed openssl_csr_get_public_key(mixed csr)
1827+
Returns the subject of a CERT or FALSE on error */
1828+
PHP_FUNCTION(openssl_csr_get_public_key)
1829+
{
1830+
zval * zcsr;
1831+
zend_bool use_shortnames = 1;
1832+
long csr_resource;
1833+
1834+
X509_REQ * csr;
1835+
EVP_PKEY *tpubkey;
1836+
1837+
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z|b", &zcsr, &use_shortnames) == FAILURE) {
1838+
return;
1839+
}
1840+
1841+
csr = php_openssl_csr_from_zval(&zcsr, 0, &csr_resource TSRMLS_CC);
1842+
1843+
if (csr == NULL) {
1844+
RETURN_FALSE;
1845+
}
1846+
1847+
tpubkey=X509_REQ_get_pubkey(csr);
1848+
RETVAL_RESOURCE(zend_list_insert(tpubkey, le_key));
1849+
return;
1850+
}
1851+
/* }}} */
1852+
17921853
/* }}} */
17931854

17941855
/* {{{ EVP Public/Private key functions */

ext/openssl/php_openssl.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,8 @@ PHP_FUNCTION(openssl_csr_new);
6767
PHP_FUNCTION(openssl_csr_export);
6868
PHP_FUNCTION(openssl_csr_export_to_file);
6969
PHP_FUNCTION(openssl_csr_sign);
70-
70+
PHP_FUNCTION(openssl_csr_get_subject);
71+
PHP_FUNCTION(openssl_csr_get_public_key);
7172
#else
7273

7374
#define phpext_openssl_ptr NULL

0 commit comments

Comments
 (0)