php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Return to Bug #38917
Patch spki.patch revision 2011-12-22 10:41 UTC by jason dot gerfen at gmail dot com
revision 2011-12-21 20:31 UTC by jason dot gerfen at gmail dot com
revision 2011-12-21 16:09 UTC by jason dot gerfen at gmail dot com
revision 2011-12-21 03:48 UTC by jason dot gerfen at gmail dot com
revision 2011-12-21 03:26 UTC by jason dot gerfen at gmail dot com
revision 2011-12-19 17:58 UTC by jason dot gerfen at gmail dot com
revision 2011-12-19 17:53 UTC by jason dot gerfen at gmail dot com
revision 2011-12-19 14:36 UTC by jason dot gerfen at gmail dot com
revision 2011-12-14 11:38 UTC by jason dot gerfen at gmail dot com
revision 2011-12-13 16:55 UTC by jason dot gerfen at gmail dot com
revision 2011-12-08 10:57 UTC by jason dot gerfen at gmail dot com
revision 2011-12-06 21:02 UTC by jason dot gerfen at gmail dot com
revision 2011-12-06 11:35 UTC by jason dot gerfen at gmail dot com
revision 2011-12-06 11:29 UTC by jason dot gerfen at gmail dot com

Patch spki.patch for OpenSSL related Bug #38917

Patch version 2011-12-06 21:02 UTC

Return to Bug #38917 | Download this patch
Patch Revisions: 2011-12-22 10:41 UTC | 2011-12-21 20:31 UTC | 2011-12-21 16:09 UTC | 2011-12-21 03:48 UTC | 2011-12-21 03:26 UTC | 2011-12-19 17:58 UTC | 2011-12-19 17:53 UTC | 2011-12-19 14:36 UTC | 2011-12-14 11:38 UTC | 2011-12-13 16:55 UTC | 2011-12-08 10:57 UTC | 2011-12-06 21:02 UTC | 2011-12-06 11:35 UTC | 2011-12-06 11:29 UTC

Developer: jason.gerfen@gmail.com

Line 1 (now 1), was 110 lines, now 95 lines

  --- php-5.3.8/ext/openssl/openssl.c	2011-07-25 05:42:53.000000000 -0600
 +++ php-5.3.8/ext/openssl/openssl.c	2011-12-13 09:12:25.000000000 -0700
 @@ -372,11 +372,39 @@
 +++ php-5.3.8-patched/ext/openssl/openssl.c	2011-12-06 11:38:55.470584042 -0700
 @@ -372,11 +372,30 @@
       ZEND_ARG_INFO(0, length)
       ZEND_ARG_INFO(1, result_is_strong)
   ZEND_END_ARG_INFO()
  +
  +ZEND_BEGIN_ARG_INFO_EX(arginfo_openssl_spki_new, 0, 0, 0)
  +    ZEND_ARG_INFO(0, privkey)
  +    ZEND_ARG_INFO(0, password)
 +    ZEND_ARG_INFO(0, spki_hash)
  +ZEND_END_ARG_INFO()
  +
  +ZEND_BEGIN_ARG_INFO(arginfo_openssl_spki_verify, 0)
  +    ZEND_ARG_INFO(0, spki)
  +ZEND_END_ARG_INFO()
  +
  +ZEND_BEGIN_ARG_INFO(arginfo_openssl_spki_export, 0)
 +    ZEND_ARG_INFO(0, spki)
 +ZEND_END_ARG_INFO()
 +
 +ZEND_BEGIN_ARG_INFO(arginfo_openssl_spki_export_challenge, 0)
 +    ZEND_ARG_INFO(0, spki)
 +ZEND_END_ARG_INFO()
 +
 +ZEND_BEGIN_ARG_INFO(arginfo_openssl_spki_details, 0)
  +    ZEND_ARG_INFO(0, spki)
  +ZEND_END_ARG_INFO()
   /* }}} */
  
 
   /* {{{ openssl_functions[]
    */
   const zend_function_entry openssl_functions[] = {
  +/* spki functions */
  +	PHP_FE(openssl_spki_new, 		arginfo_openssl_spki_new)
  +	PHP_FE(openssl_spki_verify,		arginfo_openssl_spki_verify)
  +	PHP_FE(openssl_spki_export,		arginfo_openssl_spki_export)
 + PHP_FE(openssl_spki_export_challenge,		arginfo_openssl_spki_export_challenge)
 + PHP_FE(openssl_spki_details,		arginfo_openssl_spki_details)
  +
   /* public/private key functions */
   	PHP_FE(openssl_pkey_free,			arginfo_openssl_pkey_free)
   	PHP_FE(openssl_pkey_new,			arginfo_openssl_pkey_new)
 @@ -1252,6 +1280,243 @@
 @@ -1252,6 +1271,151 @@
   }
   /* }}} */
  
 +/* {{{ proto string openssl_spki_new(mixed zpkey, string password)
 
 +/* {{{ proto string openssl_spki_new(mixed priv_key, string password)
  +   Creates new private key (or uses existing) and creates a new spki cert
  +   outputting results to var */
  +PHP_FUNCTION(openssl_spki_new)
  +{
  + zval * zout, * zpkey = NULL;
  + EVP_PKEY * pkey = NULL;
  + NETSCAPE_SPKI *spki=NULL;
  + char * password, * spkstr;
 + long keyresource = -1;
 + long keyresource;
  + const char *spkac = "SPKAC=";
  +
 + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rs|s", &zpkey, &password, &zout) == FAILURE) {
 +  return;
 + }
  + RETVAL_FALSE;
 +
 +
 + zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rs|s", &zpkey, &password, &zout);
 +
  + pkey = php_openssl_evp_from_zval(&zpkey, 0, password, 1, &keyresource TSRMLS_CC);
 +
 + if (pkey == NULL) {
 +  goto cleanup;
 + }
 +
 + if ((spki = NETSCAPE_SPKI_new()) == NULL) {
 +  goto cleanup;
 + }
 +
 + if (pkey == NULL) {
 +  php_error_docref(NULL TSRMLS_CC, E_WARNING, "cannot get private key from parameter 1");
 +  goto cleanup;
 + }
 +
 + if ((spki = NETSCAPE_SPKI_new()) == NULL) {
 +  php_error_docref(NULL TSRMLS_CC, E_WARNING, "cannot get spki interface");
 +  goto cleanup;
 + }
  +
  + if (password) {
  +  ASN1_STRING_set(spki->spkac->challenge, password, (int)strlen(password));
 + }
 + }
  +
  + if (!NETSCAPE_SPKI_set_pubkey(spki, pkey)) {
 +  php_error_docref(NULL TSRMLS_CC, E_WARNING, "cannot get public key from spki");
  +  goto cleanup;
  + }
  +
  + if (!NETSCAPE_SPKI_sign(spki, pkey, EVP_md5())) {
 +  goto cleanup;
 + }
 +
 + spkstr = NETSCAPE_SPKI_b64_encode(spki);
 +
 + char * s = malloc(snprintf(NULL, 0, "%s%s", spkac, spkstr)+1);
 +  php_error_docref(NULL TSRMLS_CC, E_WARNING, "cannot sign public key with spki");
 +  goto cleanup;
 + }
 +
 + spkstr = NETSCAPE_SPKI_b64_encode(spki);
 +
 + char * s = malloc(snprintf(NULL, 0, "%s%s", spkac, spkstr) + 1);
  + sprintf(s, "%s%s", spkac, spkstr);
 +
 + if (strlen(s)<=0) {
 +  goto cleanup;
 + }
 + RETURN_STRING(s, 1);
 +
 + RETVAL_STRINGL(s, strlen(s), 0);
  +
  +cleanup:
 + if (keyresource == -1 && spki) {
 + if (keyresource == -1 && pkey) {
  +  NETSCAPE_SPKI_free(spki);
 + }
 + if (keyresource == -1 && pkey) {
  +  EVP_PKEY_free(pkey);
  + }
 + if (keyresource == -1 && s) {
 +  free(s);
 + }
 + RETURN_NULL();
  +}
  +/* }}} */
  +
  +/* {{{ proto bool openssl_spki_verify(string spki)
Line 116 (now 101), was 186 lines, now 100 lines

  + EVP_PKEY *pkey = NULL;
  + NETSCAPE_SPKI *spki = NULL;
  +
  + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &spkstr, &spkstr_len) == FAILURE) {
 +  return;
 + }
 +
 +  php_error_docref(NULL TSRMLS_CC, E_WARNING, "missing argument 1");
 +  goto cleanup;
 + }
 +
  + if (!spkstr) {
 +  php_error_docref(NULL TSRMLS_CC, E_WARNING, "spki not found");
 +  goto cleanup;
 + }
 +
 + spki = NETSCAPE_SPKI_b64_decode(spkstr, -1);
 + if (!spki) {
 +  php_error_docref(NULL TSRMLS_CC, E_WARNING, "error decoding spki");
  +  goto cleanup;
 + }
 +
 + spki = NETSCAPE_SPKI_b64_decode(spkstr, -1);
 + if (!spki) {
 +  php_error_docref(NULL TSRMLS_CC, E_WARNING, "decode");
 +  goto cleanup;
  + }
  +
  + pkey = NETSCAPE_SPKI_get_pubkey(spki);
  + if (pkey == NULL) {
 +  php_error_docref(NULL TSRMLS_CC, E_WARNING, "pub");
 +  php_error_docref(NULL TSRMLS_CC, E_WARNING, "error getting public key from spki");
  +  goto cleanup;
 + }
 +
 + i = NETSCAPE_SPKI_verify(spki, pkey);
 + }
 +
 + i = NETSCAPE_SPKI_verify(spki, pkey);
  +
  + if (i > 0) {
  +  x = 1;
 + }
 + goto cleanup;
 +
 +cleanup:
 + if (spki) {
 +  NETSCAPE_SPKI_free(spki);
 + }
 + if (pkey) {
 +  EVP_PKEY_free(pkey);
 + }
 + RETURN_BOOL(x);
 +}
 +/* }}} */
 +
 + } else {
 +  x = 0;
 + }
 + goto cleanup;
 +
 +cleanup:
 + EVP_PKEY_free(pkey);
 + RETVAL_BOOL(x);
 +}
 +/* }}} */
 +
  +/* {{{ proto string openssl_spki_export(string spki)
  +   Exports public key from existing spki to var */
  +PHP_FUNCTION(openssl_spki_export)
  +{
  + int spkstr_len;
 + EVP_PKEY *pkey = NULL;
 + NETSCAPE_SPKI *spki = NULL;
 + BIO *out = BIO_new(BIO_s_mem());
 + BUF_MEM *bio_buf;
 + char *spkstr;
 +
 + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &spkstr, &spkstr_len) == FAILURE) {
 +  goto cleanup;
 + }
 +
 + if (!spkstr) {
 +  goto cleanup;
 + }
 +
 + spki = NETSCAPE_SPKI_b64_decode(spkstr, strlen(spkstr));
 + if (!spki) {
 +  goto cleanup;
 + }
 +
 + pkey = NETSCAPE_SPKI_get_pubkey(spki);
 + if (!pkey) {
 +  goto cleanup;
 + }
 +
 + PEM_write_bio_PUBKEY(out, pkey);
 + BIO_get_mem_ptr(out, &bio_buf);
 +
 + if (sizeof(bio_buf->data)<=0) {
 +  goto cleanup;
 + }
 +
 + char * s = malloc(snprintf(NULL, 0, "%s", bio_buf->data)+1);
 + sprintf(s, "%s", bio_buf->data);
 + RETURN_STRING(s, 1);
 +
 +cleanup:
 + if (spki) {
 +  NETSCAPE_SPKI_free(spki);
 + }
 + if (out) {
 +  BIO_free_all(out);
 + }
 + if (pkey) {
 +  EVP_PKEY_free(pkey);
 + }
 +}
 +/* }}} */
 +
 +/* {{{ proto string openssl_spki_export_challenge(string spki)
 +   Exports spkac challenge from existing spki to var */
 +PHP_FUNCTION(openssl_spki_export_challenge)
 +{
 + int spkstr_len;
 + NETSCAPE_SPKI *spki = NULL;
 + char *spkstr;
 +
 + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &spkstr, &spkstr_len) == FAILURE) {
 +  goto cleanup;
 + }
 +
 + if (!spkstr) {
 +  goto cleanup;
 + }
 +
 + spki = NETSCAPE_SPKI_b64_decode(spkstr, strlen(spkstr));
 + if (!spki) {
 +  goto cleanup;
 + }
 +
 + RETURN_STRING(ASN1_STRING_data(spki->spkac->challenge), 1);
 +
 +cleanup:
 + if (spki) {
 +  NETSCAPE_SPKI_free(spki);
 + }
 +}
 +/* }}} */
 +
 +/* {{{ proto string openssl_spki_details(string spki)
 +   Provides details from existing spki to var */
 +PHP_FUNCTION(openssl_spki_details)
 +{
 + int spkstr_len;
 + EVP_PKEY *pkey = NULL;
  + NETSCAPE_SPKI *spki = NULL;
 + BIO *out = BIO_new(BIO_s_mem());
 + BUF_MEM *bio_buf;
 + zval *zout;
 + BIO *out = BIO_new_fp(stdout, BIO_NOCLOSE);
  + char *spkstr;
 +
 +
 + RETVAL_FALSE;
 +
  + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &spkstr, &spkstr_len) == FAILURE) {
 +  return;
 +  php_error_docref(NULL TSRMLS_CC, E_WARNING, "missing argument 1");
 +  goto cleanup;
  + }
 + RETVAL_FALSE;
  +
  + if (!spkstr) {
 +  goto cleanup;
 + }
 +  php_error_docref(NULL TSRMLS_CC, E_WARNING, "spki not found");
 +  goto cleanup;
 + }
  +
  + spki = NETSCAPE_SPKI_b64_decode(spkstr, strlen(spkstr));
  + if (!spki) {
 +  php_error_docref(NULL TSRMLS_CC, E_WARNING, "error decoding spki");
 +  goto cleanup;
 + }
 +
 + pkey = NETSCAPE_SPKI_get_pubkey(spki);
 + if (!pkey) {
 +  php_error_docref(NULL TSRMLS_CC, E_WARNING, "error getting public key from spki");
  +  goto cleanup;
  + }
  +
 + NETSCAPE_SPKI_print(out, spki);
 + BIO_get_mem_ptr(out, &bio_buf);
 +
 + if (sizeof(bio_buf->data)<=0) {
 +  goto cleanup;
 + }
 +
 + char * s = malloc(snprintf(NULL, 0, "%s", bio_buf->data)+1);
 + sprintf(s, "%s", bio_buf->data);
 + RETURN_STRING(s, 1);
 + PEM_write_bio_PUBKEY(out, pkey);
 + goto cleanup;
  +
  +cleanup:
 + if (spki) {
 +  NETSCAPE_SPKI_free(spki);
 + }
 + BIO_free_all(out);
 + NETSCAPE_SPKI_free(spki);
 + EVP_PKEY_free(pkey);
 + BIO_free(out);
  +}
  +/* }}} */
  +
   /* {{{ proto bool openssl_x509_export(mixed x509, string &out [, bool notext = true])
      Exports a CERT to file or a var */
   PHP_FUNCTION(openssl_x509_export)
  --- php-5.3.8/ext/openssl/php_openssl.h	2010-12-31 19:19:59.000000000 -0700
 +++ php-5.3.8/ext/openssl/php_openssl.h	2011-12-12 12:47:26.000000000 -0700
 @@ -74,6 +74,12 @@
 +++ php-5.3.8-patched/ext/openssl/php_openssl.h	2011-12-06 11:26:59.986583142 -0700
 @@ -74,6 +74,10 @@
   PHP_FUNCTION(openssl_csr_sign);
   PHP_FUNCTION(openssl_csr_get_subject);
   PHP_FUNCTION(openssl_csr_get_public_key);
  +
  +PHP_FUNCTION(openssl_spki_new);
  +PHP_FUNCTION(openssl_spki_verify);
  +PHP_FUNCTION(openssl_spki_export);
 +PHP_FUNCTION(openssl_spki_export_challenge);
 +PHP_FUNCTION(openssl_spki_details);
   #else
  
 
   #define phpext_openssl_ptr NULL
 
PHP Copyright © 2001-2019 The PHP Group
All rights reserved.
Last updated: Fri Sep 20 07:01:26 2019 UTC