2000-11-14 03:47:20 +08:00
|
|
|
/*
|
|
|
|
+----------------------------------------------------------------------+
|
2001-12-11 23:32:16 +08:00
|
|
|
| PHP Version 4 |
|
2000-11-14 03:47:20 +08:00
|
|
|
+----------------------------------------------------------------------+
|
2003-01-01 00:08:15 +08:00
|
|
|
| Copyright (c) 1997-2003 The PHP Group |
|
2000-11-14 03:47:20 +08:00
|
|
|
+----------------------------------------------------------------------+
|
|
|
|
| This source file is subject to version 2.02 of the PHP license, |
|
|
|
|
| that is bundled with this package in the file LICENSE, and is |
|
|
|
|
| available at through the world-wide-web at |
|
|
|
|
| http://www.php.net/license/2_02.txt. |
|
|
|
|
| If you did not receive a copy of the PHP license and are unable to |
|
|
|
|
| obtain it through the world-wide-web, please send a note to |
|
|
|
|
| license@php.net so we can mail you a copy immediately. |
|
|
|
|
+----------------------------------------------------------------------+
|
|
|
|
| Authors: Stig Venaas <venaas@php.net> |
|
2002-02-28 16:29:35 +08:00
|
|
|
| Wez Furlong <wez@thebrainroom.com> |
|
|
|
|
| Sascha Kettler <kettler@gmx.net> |
|
2000-11-14 03:47:20 +08:00
|
|
|
+----------------------------------------------------------------------+
|
|
|
|
*/
|
2001-04-02 07:06:15 +08:00
|
|
|
|
2000-11-14 03:47:20 +08:00
|
|
|
/* $Id$ */
|
|
|
|
|
2001-05-24 18:07:29 +08:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
2000-11-14 03:47:20 +08:00
|
|
|
#include "php.h"
|
|
|
|
#include "php_openssl.h"
|
|
|
|
|
|
|
|
/* PHP Includes */
|
|
|
|
#include "ext/standard/file.h"
|
|
|
|
#include "ext/standard/info.h"
|
|
|
|
|
|
|
|
/* OpenSSL includes */
|
|
|
|
#include <openssl/evp.h>
|
|
|
|
#include <openssl/x509.h>
|
2001-04-02 07:06:15 +08:00
|
|
|
#include <openssl/x509v3.h>
|
|
|
|
#include <openssl/crypto.h>
|
2000-11-14 17:54:25 +08:00
|
|
|
#include <openssl/pem.h>
|
2001-04-02 07:06:15 +08:00
|
|
|
#include <openssl/err.h>
|
2001-09-11 07:57:10 +08:00
|
|
|
#include <openssl/conf.h>
|
|
|
|
#include <openssl/rand.h>
|
|
|
|
|
|
|
|
#define DEFAULT_KEY_LENGTH 512
|
|
|
|
#define MIN_KEY_LENGTH 384
|
|
|
|
|
2000-11-14 17:54:25 +08:00
|
|
|
|
2001-09-08 01:09:56 +08:00
|
|
|
#define DEBUG_SMIME 0
|
|
|
|
|
2001-10-05 23:56:42 +08:00
|
|
|
static unsigned char arg2_force_ref[] =
|
2001-10-05 04:10:58 +08:00
|
|
|
{ 2, BYREF_NONE, BYREF_FORCE };
|
2001-10-05 23:56:42 +08:00
|
|
|
static unsigned char arg2and3_force_ref[] =
|
|
|
|
{ 3, BYREF_NONE, BYREF_FORCE, BYREF_FORCE };
|
2000-11-14 03:47:20 +08:00
|
|
|
|
2001-09-11 07:57:10 +08:00
|
|
|
enum php_openssl_key_type {
|
|
|
|
OPENSSL_KEYTYPE_RSA,
|
|
|
|
OPENSSL_KEYTYPE_DSA,
|
|
|
|
OPENSSL_KEYTYPE_DH,
|
|
|
|
OPENSSL_KEYTYPE_DEFAULT = OPENSSL_KEYTYPE_RSA
|
|
|
|
};
|
|
|
|
|
2001-06-05 21:12:10 +08:00
|
|
|
/* {{{ openssl_functions[]
|
|
|
|
*/
|
2000-11-14 03:47:20 +08:00
|
|
|
function_entry openssl_functions[] = {
|
2001-09-11 07:57:10 +08:00
|
|
|
/* public/private key functions */
|
|
|
|
PHP_FE(openssl_pkey_free, NULL)
|
|
|
|
PHP_FE(openssl_pkey_new, NULL)
|
2001-10-05 23:56:42 +08:00
|
|
|
PHP_FE(openssl_pkey_export, arg2_force_ref)
|
2001-09-11 08:33:25 +08:00
|
|
|
PHP_FE(openssl_pkey_export_to_file, NULL)
|
2001-09-11 07:57:10 +08:00
|
|
|
PHP_FE(openssl_pkey_get_private, NULL)
|
|
|
|
PHP_FE(openssl_pkey_get_public, NULL)
|
|
|
|
|
|
|
|
PHP_FALIAS(openssl_free_key, openssl_pkey_free, NULL)
|
|
|
|
PHP_FALIAS(openssl_get_privatekey, openssl_pkey_get_private, NULL)
|
|
|
|
PHP_FALIAS(openssl_get_publickey, openssl_pkey_get_public, NULL)
|
|
|
|
|
|
|
|
/* x.509 cert funcs */
|
|
|
|
PHP_FE(openssl_x509_read, NULL)
|
2001-04-02 07:06:15 +08:00
|
|
|
PHP_FE(openssl_x509_free, NULL)
|
2001-09-11 07:57:10 +08:00
|
|
|
PHP_FE(openssl_x509_parse, NULL)
|
2001-04-02 07:06:15 +08:00
|
|
|
PHP_FE(openssl_x509_checkpurpose, NULL)
|
2001-09-11 07:57:10 +08:00
|
|
|
PHP_FE(openssl_x509_check_private_key, NULL)
|
2001-10-05 23:56:42 +08:00
|
|
|
PHP_FE(openssl_x509_export, arg2_force_ref)
|
2001-09-11 08:33:25 +08:00
|
|
|
PHP_FE(openssl_x509_export_to_file, NULL)
|
2001-04-02 07:06:15 +08:00
|
|
|
|
2001-09-11 07:57:10 +08:00
|
|
|
/* CSR funcs */
|
2001-10-05 23:56:42 +08:00
|
|
|
PHP_FE(openssl_csr_new, arg2_force_ref)
|
|
|
|
PHP_FE(openssl_csr_export, arg2_force_ref)
|
2001-09-11 08:33:25 +08:00
|
|
|
PHP_FE(openssl_csr_export_to_file, NULL)
|
2001-09-11 07:57:10 +08:00
|
|
|
PHP_FE(openssl_csr_sign, NULL)
|
|
|
|
|
|
|
|
|
2001-10-05 23:56:42 +08:00
|
|
|
PHP_FE(openssl_sign, arg2_force_ref)
|
2000-11-14 03:47:20 +08:00
|
|
|
PHP_FE(openssl_verify, NULL)
|
2001-10-05 23:56:42 +08:00
|
|
|
PHP_FE(openssl_seal, arg2and3_force_ref)
|
|
|
|
PHP_FE(openssl_open, arg2_force_ref)
|
2001-09-11 07:57:10 +08:00
|
|
|
|
2001-04-02 07:06:15 +08:00
|
|
|
/* for S/MIME handling */
|
|
|
|
PHP_FE(openssl_pkcs7_verify, NULL)
|
|
|
|
PHP_FE(openssl_pkcs7_decrypt, NULL)
|
|
|
|
PHP_FE(openssl_pkcs7_sign, NULL)
|
|
|
|
PHP_FE(openssl_pkcs7_encrypt, NULL)
|
|
|
|
|
2001-10-05 23:56:42 +08:00
|
|
|
PHP_FE(openssl_private_encrypt, arg2_force_ref)
|
|
|
|
PHP_FE(openssl_private_decrypt, arg2_force_ref)
|
|
|
|
PHP_FE(openssl_public_encrypt, arg2_force_ref)
|
|
|
|
PHP_FE(openssl_public_decrypt, arg2_force_ref)
|
2001-05-05 07:42:11 +08:00
|
|
|
|
2001-04-02 07:06:15 +08:00
|
|
|
PHP_FE(openssl_error_string, NULL)
|
2000-11-14 03:47:20 +08:00
|
|
|
{NULL, NULL, NULL}
|
|
|
|
};
|
2001-06-05 21:12:10 +08:00
|
|
|
/* }}} */
|
2000-11-14 03:47:20 +08:00
|
|
|
|
2001-06-05 21:12:10 +08:00
|
|
|
/* {{{ openssl_module_entry
|
|
|
|
*/
|
2000-11-14 03:47:20 +08:00
|
|
|
zend_module_entry openssl_module_entry = {
|
2001-10-12 07:33:59 +08:00
|
|
|
STANDARD_MODULE_HEADER,
|
2000-11-14 03:47:20 +08:00
|
|
|
"openssl",
|
|
|
|
openssl_functions,
|
|
|
|
PHP_MINIT(openssl),
|
|
|
|
NULL,
|
2001-04-02 07:06:15 +08:00
|
|
|
NULL,
|
2000-11-14 03:47:20 +08:00
|
|
|
NULL,
|
|
|
|
PHP_MINFO(openssl),
|
2001-10-12 07:33:59 +08:00
|
|
|
NO_VERSION_YET,
|
2000-11-14 03:47:20 +08:00
|
|
|
STANDARD_MODULE_PROPERTIES
|
|
|
|
};
|
2001-06-05 21:12:10 +08:00
|
|
|
/* }}} */
|
2000-11-14 03:47:20 +08:00
|
|
|
|
|
|
|
#ifdef COMPILE_DL_OPENSSL
|
|
|
|
ZEND_GET_MODULE(openssl)
|
|
|
|
#endif
|
|
|
|
|
2000-11-14 17:54:25 +08:00
|
|
|
static int le_key;
|
|
|
|
static int le_x509;
|
2001-09-11 07:57:10 +08:00
|
|
|
static int le_csr;
|
|
|
|
|
|
|
|
/* {{{ resource destructors */
|
|
|
|
static void php_pkey_free(zend_rsrc_list_entry *rsrc TSRMLS_DC)
|
|
|
|
{
|
|
|
|
EVP_PKEY *pkey = (EVP_PKEY *)rsrc->ptr;
|
2002-03-26 08:03:11 +08:00
|
|
|
|
|
|
|
assert(pkey != NULL);
|
|
|
|
|
2001-09-11 07:57:10 +08:00
|
|
|
EVP_PKEY_free(pkey);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void php_x509_free(zend_rsrc_list_entry *rsrc TSRMLS_DC)
|
|
|
|
{
|
|
|
|
X509 *x509 = (X509 *)rsrc->ptr;
|
|
|
|
X509_free(x509);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void php_csr_free(zend_rsrc_list_entry *rsrc TSRMLS_DC)
|
|
|
|
{
|
|
|
|
X509_REQ * csr = (X509_REQ*)rsrc->ptr;
|
|
|
|
X509_REQ_free(csr);
|
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2002-11-10 13:19:40 +08:00
|
|
|
/* {{{ openssl safe_mode & open_basedir checks */
|
|
|
|
inline static int php_openssl_safe_mode_chk(char *filename TSRMLS_DC)
|
|
|
|
{
|
|
|
|
if (PG(safe_mode) && (!php_checkuid(filename, NULL, CHECKUID_CHECK_FILE_AND_DIR))) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (php_check_open_basedir(filename TSRMLS_CC)) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2001-09-11 07:57:10 +08:00
|
|
|
/* {{{ openssl -> PHP "bridging" */
|
|
|
|
/* true global; readonly after module startup */
|
2001-10-05 04:10:58 +08:00
|
|
|
static char default_ssl_conf_filename[MAXPATHLEN];
|
2001-09-11 07:57:10 +08:00
|
|
|
|
|
|
|
struct php_x509_request {
|
|
|
|
LHASH * global_config; /* Global SSL config */
|
|
|
|
LHASH * req_config; /* SSL config for this request */
|
|
|
|
const EVP_MD * md_alg;
|
|
|
|
const EVP_MD * digest;
|
|
|
|
char * section_name,
|
|
|
|
* config_filename,
|
|
|
|
* digest_name,
|
|
|
|
* extensions_section,
|
|
|
|
* request_extensions_section;
|
|
|
|
int priv_key_bits;
|
|
|
|
int priv_key_type;
|
|
|
|
|
|
|
|
int priv_key_encrypt;
|
|
|
|
|
|
|
|
EVP_PKEY * priv_key;
|
|
|
|
};
|
|
|
|
|
2001-04-02 07:06:15 +08:00
|
|
|
|
2001-08-01 07:47:35 +08:00
|
|
|
static X509 * php_openssl_x509_from_zval(zval ** val, int makeresource, long * resourceval TSRMLS_DC);
|
2001-07-30 16:24:42 +08:00
|
|
|
static EVP_PKEY * php_openssl_evp_from_zval(zval ** val, int public_key, char * passphrase, int makeresource, long * resourceval TSRMLS_DC);
|
2002-12-12 21:42:23 +08:00
|
|
|
static int php_openssl_is_private_key(EVP_PKEY* pkey TSRMLS_DC);
|
2001-07-30 16:24:42 +08:00
|
|
|
static X509_STORE * setup_verify(zval * calist TSRMLS_DC);
|
2001-04-02 07:06:15 +08:00
|
|
|
static STACK_OF(X509) * load_all_certs_from_file(char *certfile);
|
2001-09-11 07:57:10 +08:00
|
|
|
static X509_REQ * php_openssl_csr_from_zval(zval ** val, int makeresource, long * resourceval TSRMLS_DC);
|
|
|
|
static EVP_PKEY * php_openssl_generate_private_key(struct php_x509_request * req TSRMLS_DC);
|
|
|
|
|
|
|
|
|
2002-04-13 03:26:08 +08:00
|
|
|
static void add_assoc_name_entry(zval * val, char * key, X509_NAME * name, int shortname TSRMLS_DC)
|
2001-09-11 07:57:10 +08:00
|
|
|
{
|
|
|
|
zval * subitem;
|
|
|
|
int i;
|
|
|
|
char * sn, * ln;
|
|
|
|
int nid;
|
|
|
|
X509_NAME_ENTRY * ne;
|
|
|
|
ASN1_STRING * str;
|
|
|
|
ASN1_OBJECT * obj;
|
|
|
|
|
|
|
|
MAKE_STD_ZVAL(subitem);
|
|
|
|
array_init(subitem);
|
|
|
|
|
2002-12-11 06:18:58 +08:00
|
|
|
for (i = 0; i < X509_NAME_entry_count(name); i++) {
|
2001-09-11 07:57:10 +08:00
|
|
|
ne = X509_NAME_get_entry(name, i);
|
|
|
|
obj = X509_NAME_ENTRY_get_object(ne);
|
|
|
|
str = X509_NAME_ENTRY_get_data(ne);
|
|
|
|
nid = OBJ_obj2nid(obj);
|
2002-12-11 06:18:58 +08:00
|
|
|
if (shortname) {
|
2001-09-11 07:57:10 +08:00
|
|
|
sn = (char*)OBJ_nid2sn(nid);
|
|
|
|
add_assoc_stringl(subitem, sn, str->data, str->length, 1);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
ln = (char*)OBJ_nid2ln(nid);
|
|
|
|
add_assoc_stringl(subitem, ln, str->data, str->length, 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
zend_hash_update(HASH_OF(val), key, strlen(key) + 1, (void *)&subitem, sizeof(subitem), NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void add_assoc_asn1_string(zval * val, char * key, ASN1_STRING * str)
|
|
|
|
{
|
|
|
|
add_assoc_stringl(val, key, str->data, str->length, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static time_t asn1_time_to_time_t(ASN1_UTCTIME * timestr TSRMLS_DC)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
This is how the time string is formatted:
|
|
|
|
|
|
|
|
sprintf(p,"%02d%02d%02d%02d%02d%02dZ",ts->tm_year%100,
|
|
|
|
ts->tm_mon+1,ts->tm_mday,ts->tm_hour,ts->tm_min,ts->tm_sec);
|
|
|
|
*/
|
|
|
|
|
|
|
|
time_t ret;
|
|
|
|
struct tm thetime;
|
|
|
|
char * strbuf;
|
|
|
|
char * thestr;
|
|
|
|
long gmadjust = 0;
|
|
|
|
|
2002-12-11 06:18:58 +08:00
|
|
|
if (timestr->length < 13) {
|
2002-12-12 22:12:42 +08:00
|
|
|
php_error_docref(NULL TSRMLS_CC, E_WARNING, "extension author too lazy to parse %s correctly", timestr->data);
|
2001-09-11 07:57:10 +08:00
|
|
|
return (time_t)-1;
|
|
|
|
}
|
|
|
|
|
|
|
|
strbuf = estrdup(timestr->data);
|
|
|
|
|
|
|
|
|
|
|
|
memset(&thetime, 0, sizeof(thetime));
|
|
|
|
|
|
|
|
/* we work backwards so that we can use atoi more easily */
|
|
|
|
|
|
|
|
thestr = strbuf + timestr->length - 3;
|
|
|
|
|
|
|
|
thetime.tm_sec = atoi(thestr);
|
|
|
|
*thestr = '\0';
|
|
|
|
thestr -= 2;
|
|
|
|
thetime.tm_min = atoi(thestr);
|
|
|
|
*thestr = '\0';
|
|
|
|
thestr -= 2;
|
|
|
|
thetime.tm_hour = atoi(thestr);
|
|
|
|
*thestr = '\0';
|
|
|
|
thestr -= 2;
|
|
|
|
thetime.tm_mday = atoi(thestr);
|
|
|
|
*thestr = '\0';
|
|
|
|
thestr -= 2;
|
|
|
|
thetime.tm_mon = atoi(thestr)-1;
|
|
|
|
*thestr = '\0';
|
|
|
|
thestr -= 2;
|
|
|
|
thetime.tm_year = atoi(thestr);
|
|
|
|
|
|
|
|
if (thetime.tm_year < 68)
|
|
|
|
thetime.tm_year += 100;
|
|
|
|
|
|
|
|
thetime.tm_isdst = -1;
|
|
|
|
ret = mktime(&thetime);
|
|
|
|
|
|
|
|
#if HAVE_TM_GMTOFF
|
|
|
|
gmadjust = thetime.tm_gmtoff;
|
|
|
|
#else
|
|
|
|
/*
|
|
|
|
** If correcting for daylight savings time, we set the adjustment to
|
|
|
|
** the value of timezone - 3600 seconds. Otherwise, we need to overcorrect and
|
|
|
|
** set the adjustment to the main timezone + 3600 seconds.
|
|
|
|
*/
|
2002-12-12 20:18:44 +08:00
|
|
|
gmadjust = -(thetime.tm_isdst ? (long)timezone - 3600 : (long)timezone + 3600);
|
2001-09-11 07:57:10 +08:00
|
|
|
#endif
|
|
|
|
ret += gmadjust;
|
|
|
|
|
|
|
|
efree(strbuf);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline int php_openssl_config_check_syntax(
|
|
|
|
const char * section_label,
|
|
|
|
const char * config_filename,
|
|
|
|
const char * section,
|
|
|
|
LHASH * config TSRMLS_DC)
|
|
|
|
{
|
|
|
|
X509V3_CTX ctx;
|
|
|
|
|
|
|
|
X509V3_set_ctx_test(&ctx);
|
|
|
|
X509V3_set_conf_lhash(&ctx, config);
|
2002-12-11 06:18:58 +08:00
|
|
|
if (!X509V3_EXT_add_conf(config, &ctx, (char *)section, NULL)) {
|
|
|
|
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Error loading %s section %s of %s",
|
2001-09-11 07:57:10 +08:00
|
|
|
section_label,
|
|
|
|
section,
|
|
|
|
config_filename);
|
|
|
|
return FAILURE;
|
|
|
|
}
|
|
|
|
return SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int add_oid_section(struct php_x509_request * req TSRMLS_DC)
|
|
|
|
{
|
|
|
|
char * str;
|
|
|
|
STACK_OF(CONF_VALUE) * sktmp;
|
|
|
|
CONF_VALUE * cnf;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
str = CONF_get_string(req->req_config, NULL, "oid_section");
|
|
|
|
if (str == NULL)
|
|
|
|
return SUCCESS;
|
|
|
|
|
|
|
|
sktmp = CONF_get_section(req->req_config, str);
|
2002-12-11 06:18:58 +08:00
|
|
|
if (sktmp == NULL) {
|
|
|
|
php_error_docref(NULL TSRMLS_CC, E_WARNING, "problem loading oid section %s", str);
|
2001-09-11 07:57:10 +08:00
|
|
|
return FAILURE;
|
|
|
|
}
|
2002-12-11 06:18:58 +08:00
|
|
|
for (i = 0; i < sk_CONF_VALUE_num(sktmp); i++) {
|
2001-09-11 07:57:10 +08:00
|
|
|
cnf = sk_CONF_VALUE_value(sktmp, i);
|
2002-12-11 06:18:58 +08:00
|
|
|
if (OBJ_create(cnf->value, cnf->name, cnf->name) == NID_undef) {
|
|
|
|
php_error_docref(NULL TSRMLS_CC, E_WARNING, "problem creating object %s=%s",
|
2001-09-11 07:57:10 +08:00
|
|
|
cnf->name, cnf->value);
|
|
|
|
return FAILURE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define PHP_SSL_REQ_INIT(req) memset(req, 0, sizeof(*req))
|
2001-09-30 21:30:18 +08:00
|
|
|
#define PHP_SSL_REQ_DISPOSE(req) php_openssl_dispose_config(req TSRMLS_CC)
|
|
|
|
#define PHP_SSL_REQ_PARSE(req, zval) php_openssl_parse_config(req, zval TSRMLS_CC)
|
2001-09-11 07:57:10 +08:00
|
|
|
|
|
|
|
#define PHP_SSL_CONFIG_SYNTAX_CHECK(var) if (req->var && php_openssl_config_check_syntax(#var, \
|
2001-09-30 21:30:18 +08:00
|
|
|
req->config_filename, req->var, req->req_config TSRMLS_CC) == FAILURE) return FAILURE
|
2001-09-11 07:57:10 +08:00
|
|
|
|
|
|
|
#define SET_OPTIONAL_STRING_ARG(key, varname, defval) \
|
|
|
|
if (optional_args && zend_hash_find(Z_ARRVAL_P(optional_args), key, sizeof(key), (void**)&item) == SUCCESS) \
|
|
|
|
varname = Z_STRVAL_PP(item); \
|
|
|
|
else \
|
|
|
|
varname = defval
|
|
|
|
|
|
|
|
#define SET_OPTIONAL_LONG_ARG(key, varname, defval) \
|
|
|
|
if (optional_args && zend_hash_find(Z_ARRVAL_P(optional_args), key, sizeof(key), (void**)&item) == SUCCESS) \
|
|
|
|
varname = Z_LVAL_PP(item); \
|
|
|
|
else \
|
|
|
|
varname = defval
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static int php_openssl_parse_config(
|
|
|
|
struct php_x509_request * req,
|
|
|
|
zval * optional_args
|
|
|
|
TSRMLS_DC
|
|
|
|
)
|
|
|
|
{
|
|
|
|
char * str;
|
|
|
|
zval ** item;
|
|
|
|
|
|
|
|
SET_OPTIONAL_STRING_ARG("config", req->config_filename, default_ssl_conf_filename);
|
|
|
|
SET_OPTIONAL_STRING_ARG("config_section_name", req->section_name, "req");
|
|
|
|
req->global_config = CONF_load(NULL, default_ssl_conf_filename, NULL);
|
|
|
|
req->req_config = CONF_load(NULL, req->config_filename, NULL);
|
|
|
|
|
|
|
|
if (req->req_config == NULL)
|
|
|
|
return FAILURE;
|
|
|
|
|
|
|
|
|
|
|
|
/* read in the oids */
|
|
|
|
str = CONF_get_string(req->req_config, NULL, "oid_file");
|
2002-11-10 13:19:40 +08:00
|
|
|
if (str && !php_openssl_safe_mode_chk(str TSRMLS_CC)) {
|
|
|
|
BIO *oid_bio = BIO_new_file(str, "r");
|
2002-12-11 06:18:58 +08:00
|
|
|
if (oid_bio) {
|
2001-09-11 07:57:10 +08:00
|
|
|
OBJ_create_objects(oid_bio);
|
|
|
|
BIO_free(oid_bio);
|
|
|
|
}
|
|
|
|
}
|
2001-09-30 21:30:18 +08:00
|
|
|
if (add_oid_section(req TSRMLS_CC) == FAILURE)
|
2001-09-11 07:57:10 +08:00
|
|
|
return FAILURE;
|
|
|
|
|
|
|
|
SET_OPTIONAL_STRING_ARG("digest_alg", req->digest_name,
|
|
|
|
CONF_get_string(req->req_config, req->section_name, "default_md"));
|
|
|
|
SET_OPTIONAL_STRING_ARG("x509_extensions", req->extensions_section,
|
|
|
|
CONF_get_string(req->req_config, req->section_name, "x509_extensions"));
|
|
|
|
SET_OPTIONAL_STRING_ARG("req_extensions", req->extensions_section,
|
|
|
|
CONF_get_string(req->req_config, req->request_extensions_section, "req_extensions"));
|
|
|
|
SET_OPTIONAL_LONG_ARG("private_key_bits", req->priv_key_bits,
|
|
|
|
CONF_get_number(req->req_config, req->section_name, "default_bits"));
|
|
|
|
|
|
|
|
SET_OPTIONAL_LONG_ARG("private_key_type", req->priv_key_type, OPENSSL_KEYTYPE_DEFAULT);
|
|
|
|
|
2002-12-11 06:18:58 +08:00
|
|
|
if (optional_args && zend_hash_find(Z_ARRVAL_P(optional_args), "encrypt_key", sizeof("encrypt_key"), (void**)&item) == SUCCESS) {
|
2001-09-11 07:57:10 +08:00
|
|
|
req->priv_key_encrypt = Z_BVAL_PP(item);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
str = CONF_get_string(req->req_config, req->section_name, "encrypt_rsa_key");
|
|
|
|
if (str == NULL)
|
|
|
|
str = CONF_get_string(req->req_config, req->section_name, "encrypt_key");
|
|
|
|
if (str && strcmp(str, "no") == 0)
|
|
|
|
req->priv_key_encrypt = 0;
|
|
|
|
else
|
|
|
|
req->priv_key_encrypt = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* digest alg */
|
|
|
|
if (req->digest_name == NULL)
|
|
|
|
req->digest_name = CONF_get_string(req->req_config, req->section_name, "default_md");
|
|
|
|
if (req->digest_name)
|
|
|
|
req->digest = req->md_alg = EVP_get_digestbyname(req->digest_name);
|
|
|
|
if (req->md_alg == NULL)
|
|
|
|
req->md_alg = req->digest = EVP_md5();
|
|
|
|
|
|
|
|
PHP_SSL_CONFIG_SYNTAX_CHECK(extensions_section);
|
|
|
|
|
|
|
|
/* set the string mask */
|
|
|
|
str = CONF_get_string(req->req_config, req->section_name, "string_mask");
|
2002-12-11 06:18:58 +08:00
|
|
|
if (str && !ASN1_STRING_set_default_mask_asc(str)) {
|
|
|
|
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid global string mask setting %s", str);
|
2001-09-11 07:57:10 +08:00
|
|
|
return FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (req->request_extensions_section == NULL)
|
|
|
|
req->request_extensions_section = CONF_get_string(req->req_config, req->section_name, "req_extensions");
|
|
|
|
|
|
|
|
PHP_SSL_CONFIG_SYNTAX_CHECK(request_extensions_section);
|
|
|
|
|
|
|
|
return SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void php_openssl_dispose_config(struct php_x509_request * req TSRMLS_DC)
|
|
|
|
{
|
2002-12-11 06:18:58 +08:00
|
|
|
if (req->priv_key) {
|
2001-09-11 07:57:10 +08:00
|
|
|
EVP_PKEY_free(req->priv_key);
|
|
|
|
req->priv_key = NULL;
|
|
|
|
}
|
2002-12-11 06:18:58 +08:00
|
|
|
if (req->global_config) {
|
2001-09-11 07:57:10 +08:00
|
|
|
CONF_free(req->global_config);
|
|
|
|
req->global_config = NULL;
|
|
|
|
}
|
2002-12-11 06:18:58 +08:00
|
|
|
if (req->req_config) {
|
2001-09-11 07:57:10 +08:00
|
|
|
CONF_free(req->req_config);
|
|
|
|
req->req_config = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int php_openssl_load_rand_file(const char * file, int *egdsocket, int *seeded)
|
|
|
|
{
|
2001-10-05 04:10:58 +08:00
|
|
|
char buffer[MAXPATHLEN];
|
2001-09-11 07:57:10 +08:00
|
|
|
|
2002-12-11 15:29:51 +08:00
|
|
|
TSRMLS_FETCH();
|
|
|
|
|
2001-09-11 07:57:10 +08:00
|
|
|
*egdsocket = 0;
|
|
|
|
*seeded = 0;
|
|
|
|
|
|
|
|
#ifdef WINDOWS
|
|
|
|
RAND_screen();
|
|
|
|
#endif
|
|
|
|
if (file == NULL)
|
|
|
|
file = RAND_file_name(buffer, sizeof(buffer));
|
2002-12-11 06:18:58 +08:00
|
|
|
else if (RAND_egd(file) > 0) {
|
2001-09-11 07:57:10 +08:00
|
|
|
/* if the given filename is an EGD socket, don't
|
|
|
|
* write anything back to it */
|
|
|
|
*egdsocket = 1;
|
|
|
|
return SUCCESS;
|
|
|
|
}
|
2002-12-11 06:18:58 +08:00
|
|
|
if (file == NULL || !RAND_load_file(file, -1)) {
|
|
|
|
if (RAND_status() == 0) {
|
|
|
|
php_error_docref(NULL TSRMLS_CC, E_WARNING, "unable to load random state; not enough random data!");
|
2001-09-11 07:57:10 +08:00
|
|
|
return FAILURE;
|
|
|
|
}
|
|
|
|
return FAILURE;
|
|
|
|
}
|
|
|
|
*seeded = 1;
|
|
|
|
return SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int php_openssl_write_rand_file(const char * file, int egdsocket, int seeded)
|
|
|
|
{
|
2001-10-05 04:10:58 +08:00
|
|
|
char buffer[MAXPATHLEN];
|
2002-12-11 10:30:00 +08:00
|
|
|
|
|
|
|
TSRMLS_FETCH();
|
|
|
|
|
2002-12-11 06:18:58 +08:00
|
|
|
if (egdsocket || !seeded) {
|
2001-09-11 07:57:10 +08:00
|
|
|
/* if we did not manage to read the seed file, we should not write
|
|
|
|
* a low-entropy seed file back */
|
|
|
|
return FAILURE;
|
|
|
|
}
|
|
|
|
if (file == NULL)
|
|
|
|
file = RAND_file_name(buffer, sizeof(buffer));
|
2002-12-11 06:18:58 +08:00
|
|
|
if (file == NULL || !RAND_write_file(file)) {
|
|
|
|
php_error_docref(NULL TSRMLS_CC, E_WARNING, "unable to write random state");
|
2001-09-11 07:57:10 +08:00
|
|
|
return FAILURE;
|
|
|
|
}
|
|
|
|
return SUCCESS;
|
|
|
|
}
|
|
|
|
/* }}} */
|
2001-04-02 07:06:15 +08:00
|
|
|
|
2001-06-05 21:12:10 +08:00
|
|
|
/* {{{ PHP_MINIT_FUNCTION
|
|
|
|
*/
|
2000-11-14 03:47:20 +08:00
|
|
|
PHP_MINIT_FUNCTION(openssl)
|
|
|
|
{
|
2001-09-11 07:57:10 +08:00
|
|
|
char * config_filename;
|
|
|
|
|
|
|
|
le_key = zend_register_list_destructors_ex(php_pkey_free, NULL, "OpenSSL key", module_number);
|
|
|
|
le_x509 = zend_register_list_destructors_ex(php_x509_free, NULL, "OpenSSL X.509", module_number);
|
|
|
|
le_csr = zend_register_list_destructors_ex(php_csr_free, NULL, "OpenSSL X.509 CSR", module_number);
|
2001-04-02 07:06:15 +08:00
|
|
|
|
2000-11-18 03:17:07 +08:00
|
|
|
OpenSSL_add_all_ciphers();
|
2001-09-11 07:57:10 +08:00
|
|
|
OpenSSL_add_all_digests();
|
|
|
|
OpenSSL_add_all_algorithms();
|
2001-04-02 07:06:15 +08:00
|
|
|
|
|
|
|
ERR_load_ERR_strings();
|
|
|
|
ERR_load_crypto_strings();
|
|
|
|
ERR_load_EVP_strings();
|
|
|
|
|
|
|
|
/* purposes for cert purpose checking */
|
|
|
|
REGISTER_LONG_CONSTANT("X509_PURPOSE_SSL_CLIENT", X509_PURPOSE_SSL_CLIENT, CONST_CS|CONST_PERSISTENT);
|
|
|
|
REGISTER_LONG_CONSTANT("X509_PURPOSE_SSL_SERVER", X509_PURPOSE_SSL_SERVER, CONST_CS|CONST_PERSISTENT);
|
|
|
|
REGISTER_LONG_CONSTANT("X509_PURPOSE_NS_SSL_SERVER", X509_PURPOSE_NS_SSL_SERVER, CONST_CS|CONST_PERSISTENT);
|
|
|
|
REGISTER_LONG_CONSTANT("X509_PURPOSE_SMIME_SIGN", X509_PURPOSE_SMIME_SIGN, CONST_CS|CONST_PERSISTENT);
|
|
|
|
REGISTER_LONG_CONSTANT("X509_PURPOSE_SMIME_ENCRYPT", X509_PURPOSE_SMIME_ENCRYPT, CONST_CS|CONST_PERSISTENT);
|
|
|
|
REGISTER_LONG_CONSTANT("X509_PURPOSE_CRL_SIGN", X509_PURPOSE_CRL_SIGN, CONST_CS|CONST_PERSISTENT);
|
2002-05-01 16:22:17 +08:00
|
|
|
#ifdef X509_PURPOSE_ANY
|
2001-04-02 07:06:15 +08:00
|
|
|
REGISTER_LONG_CONSTANT("X509_PURPOSE_ANY", X509_PURPOSE_ANY, CONST_CS|CONST_PERSISTENT);
|
2002-05-01 16:22:17 +08:00
|
|
|
#endif
|
2001-04-02 07:06:15 +08:00
|
|
|
|
2001-04-03 07:14:01 +08:00
|
|
|
/* flags for S/MIME */
|
|
|
|
REGISTER_LONG_CONSTANT("PKCS7_DETACHED", PKCS7_DETACHED, CONST_CS|CONST_PERSISTENT);
|
|
|
|
REGISTER_LONG_CONSTANT("PKCS7_TEXT", PKCS7_TEXT, CONST_CS|CONST_PERSISTENT);
|
|
|
|
REGISTER_LONG_CONSTANT("PKCS7_NOINTERN", PKCS7_NOINTERN, CONST_CS|CONST_PERSISTENT);
|
|
|
|
REGISTER_LONG_CONSTANT("PKCS7_NOVERIFY", PKCS7_NOVERIFY, CONST_CS|CONST_PERSISTENT);
|
|
|
|
REGISTER_LONG_CONSTANT("PKCS7_NOCHAIN", PKCS7_NOCHAIN, CONST_CS|CONST_PERSISTENT);
|
|
|
|
REGISTER_LONG_CONSTANT("PKCS7_NOCERTS", PKCS7_NOCERTS, CONST_CS|CONST_PERSISTENT);
|
|
|
|
REGISTER_LONG_CONSTANT("PKCS7_NOATTR", PKCS7_NOATTR, CONST_CS|CONST_PERSISTENT);
|
|
|
|
REGISTER_LONG_CONSTANT("PKCS7_BINARY", PKCS7_BINARY, CONST_CS|CONST_PERSISTENT);
|
|
|
|
REGISTER_LONG_CONSTANT("PKCS7_NOSIGS", PKCS7_NOSIGS, CONST_CS|CONST_PERSISTENT);
|
|
|
|
|
2001-05-05 07:42:11 +08:00
|
|
|
REGISTER_LONG_CONSTANT("OPENSSL_PKCS1_PADDING",
|
|
|
|
RSA_PKCS1_PADDING,
|
|
|
|
CONST_CS|CONST_PERSISTENT);
|
|
|
|
REGISTER_LONG_CONSTANT("OPENSSL_SSLV23_PADDING",
|
|
|
|
RSA_SSLV23_PADDING,
|
|
|
|
CONST_CS|CONST_PERSISTENT);
|
|
|
|
REGISTER_LONG_CONSTANT("OPENSSL_NO_PADDING",
|
|
|
|
RSA_NO_PADDING,
|
|
|
|
CONST_CS|CONST_PERSISTENT);
|
|
|
|
REGISTER_LONG_CONSTANT("OPENSSL_PKCS1_OAEP_PADDING",
|
|
|
|
RSA_PKCS1_OAEP_PADDING,
|
|
|
|
CONST_CS|CONST_PERSISTENT);
|
|
|
|
|
2001-09-11 07:57:10 +08:00
|
|
|
/* Values for key types */
|
|
|
|
REGISTER_LONG_CONSTANT("OPENSSL_KEYTYPE_RSA",
|
|
|
|
OPENSSL_KEYTYPE_RSA, CONST_CS | CONST_PERSISTENT);
|
|
|
|
#ifndef NO_DSA
|
|
|
|
REGISTER_LONG_CONSTANT("OPENSSL_KEYTYPE_DSA",
|
|
|
|
OPENSSL_KEYTYPE_DSA, CONST_CS | CONST_PERSISTENT);
|
|
|
|
#endif
|
|
|
|
REGISTER_LONG_CONSTANT("OPENSSL_KEYTYPE_DH",
|
|
|
|
OPENSSL_KEYTYPE_DH, CONST_CS | CONST_PERSISTENT);
|
|
|
|
|
|
|
|
/* Determine default SSL configuration file */
|
|
|
|
config_filename = getenv("OPENSSL_CONF");
|
|
|
|
if (config_filename == NULL)
|
|
|
|
config_filename = getenv("SSLEAY_CONF");
|
2002-07-13 05:46:36 +08:00
|
|
|
|
|
|
|
/* default to 'openssl.cnf' if no environment variable is set */
|
|
|
|
if (config_filename == NULL) {
|
2001-09-11 07:57:10 +08:00
|
|
|
snprintf(default_ssl_conf_filename, sizeof(default_ssl_conf_filename), "%s/%s",
|
|
|
|
X509_get_default_cert_area(),
|
2002-07-13 05:46:36 +08:00
|
|
|
"openssl.cnf");
|
2001-09-11 07:57:10 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
strncpy(default_ssl_conf_filename, config_filename, sizeof(default_ssl_conf_filename));
|
|
|
|
|
2000-11-14 03:47:20 +08:00
|
|
|
return SUCCESS;
|
|
|
|
}
|
2001-06-05 21:12:10 +08:00
|
|
|
/* }}} */
|
2000-11-14 03:47:20 +08:00
|
|
|
|
2001-06-05 21:12:10 +08:00
|
|
|
/* {{{ PHP_MINFO_FUNCTION
|
|
|
|
*/
|
2000-11-14 03:47:20 +08:00
|
|
|
PHP_MINFO_FUNCTION(openssl)
|
|
|
|
{
|
|
|
|
php_info_print_table_start();
|
|
|
|
php_info_print_table_row(2, "OpenSSL support", "enabled");
|
|
|
|
php_info_print_table_row(2, "OpenSSL Version", OPENSSL_VERSION_TEXT);
|
|
|
|
php_info_print_table_end();
|
|
|
|
}
|
2001-06-05 21:12:10 +08:00
|
|
|
/* }}} */
|
2000-11-14 03:47:20 +08:00
|
|
|
|
2001-06-05 21:12:10 +08:00
|
|
|
/* {{{ PHP_MSHUTDOWN_FUNCTION
|
|
|
|
*/
|
2000-11-18 03:17:07 +08:00
|
|
|
PHP_MSHUTDOWN_FUNCTION(openssl)
|
|
|
|
{
|
|
|
|
EVP_cleanup();
|
|
|
|
return SUCCESS;
|
|
|
|
}
|
2001-06-05 21:12:10 +08:00
|
|
|
/* }}} */
|
2000-11-18 03:17:07 +08:00
|
|
|
|
2001-09-11 07:57:10 +08:00
|
|
|
/* {{{ x509 cert functions */
|
|
|
|
|
2001-06-05 21:12:10 +08:00
|
|
|
/* {{{ php_openssl_x509_from_zval
|
2001-04-02 07:06:15 +08:00
|
|
|
Given a zval, coerce it into an X509 object.
|
|
|
|
The zval can be:
|
|
|
|
. X509 resource created using openssl_read_x509()
|
|
|
|
. if it starts with file:// then it will be interpreted as the path to that cert
|
|
|
|
. it will be interpreted as the cert data
|
|
|
|
If you supply makeresource, the result will be registered as an x509 resource and
|
|
|
|
it's value returned in makeresource.
|
|
|
|
*/
|
2001-08-01 07:47:35 +08:00
|
|
|
static X509 * php_openssl_x509_from_zval(zval ** val, int makeresource, long * resourceval TSRMLS_DC)
|
2001-04-02 07:06:15 +08:00
|
|
|
{
|
|
|
|
X509 *cert = NULL;
|
|
|
|
|
|
|
|
if (resourceval)
|
|
|
|
*resourceval = -1;
|
|
|
|
|
2002-12-11 06:18:58 +08:00
|
|
|
if (Z_TYPE_PP(val) == IS_RESOURCE) {
|
2001-04-02 07:06:15 +08:00
|
|
|
/* is it an x509 resource ? */
|
|
|
|
void * what;
|
|
|
|
int type;
|
|
|
|
|
2001-07-30 12:58:07 +08:00
|
|
|
what = zend_fetch_resource(val TSRMLS_CC, -1, "OpenSSL X.509", &type, 1, le_x509);
|
2001-04-02 07:06:15 +08:00
|
|
|
if (!what)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
/* this is so callers can decide if they should free the X509 */
|
|
|
|
if (resourceval)
|
|
|
|
*resourceval = Z_LVAL_PP(val);
|
|
|
|
|
|
|
|
if (type == le_x509)
|
|
|
|
return (X509*)what;
|
|
|
|
|
|
|
|
/* other types could be used here - eg: file pointers and read in the data from them */
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
/* force it to be a string and check if it refers to a file */
|
|
|
|
convert_to_string_ex(val);
|
|
|
|
|
2002-12-11 06:18:58 +08:00
|
|
|
if (Z_STRLEN_PP(val) > 7 && memcmp(Z_STRVAL_PP(val), "file://", 7) == 0) {
|
2001-04-02 07:06:15 +08:00
|
|
|
/* read cert from the named file */
|
|
|
|
BIO *in;
|
|
|
|
|
2002-11-10 13:19:40 +08:00
|
|
|
if (php_openssl_safe_mode_chk(Z_STRVAL_PP(val) + 7 TSRMLS_CC)) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2001-04-02 07:06:15 +08:00
|
|
|
in = BIO_new_file(Z_STRVAL_PP(val) + 7, "r");
|
|
|
|
if (in == NULL)
|
|
|
|
return NULL;
|
|
|
|
cert = PEM_read_bio_X509(in, NULL, NULL, NULL);
|
|
|
|
BIO_free(in);
|
|
|
|
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
BIO *in;
|
|
|
|
|
|
|
|
in = BIO_new_mem_buf(Z_STRVAL_PP(val), Z_STRLEN_PP(val));
|
|
|
|
if (in == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
cert = (X509 *) PEM_ASN1_read_bio((char *(*)())d2i_X509,
|
|
|
|
PEM_STRING_X509, in,
|
|
|
|
NULL, NULL, NULL);
|
|
|
|
BIO_free(in);
|
|
|
|
}
|
|
|
|
|
2002-12-11 06:18:58 +08:00
|
|
|
if (cert && makeresource && resourceval) {
|
2001-04-02 07:06:15 +08:00
|
|
|
*resourceval = zend_list_insert(cert, le_x509);
|
|
|
|
}
|
|
|
|
return cert;
|
|
|
|
}
|
2001-09-11 07:57:10 +08:00
|
|
|
|
2001-06-05 21:12:10 +08:00
|
|
|
/* }}} */
|
2001-04-02 07:06:15 +08:00
|
|
|
|
2001-12-07 01:51:48 +08:00
|
|
|
/* {{{ proto bool openssl_x509_export_to_file(mixed x509, string outfilename [, bool notext = true])
|
2001-09-11 09:03:59 +08:00
|
|
|
Exports a CERT to file or a var */
|
2001-09-11 08:33:25 +08:00
|
|
|
PHP_FUNCTION(openssl_x509_export_to_file)
|
2001-04-02 07:06:15 +08:00
|
|
|
{
|
2001-09-11 07:57:10 +08:00
|
|
|
X509 * cert;
|
2001-09-11 08:33:25 +08:00
|
|
|
zval * zcert = NULL;
|
|
|
|
zend_bool notext = 1;
|
2001-09-11 07:57:10 +08:00
|
|
|
BIO * bio_out;
|
|
|
|
long certresource;
|
2001-09-11 08:33:25 +08:00
|
|
|
char * filename;
|
|
|
|
long filename_len;
|
2001-04-02 07:06:15 +08:00
|
|
|
|
2001-09-11 08:33:25 +08:00
|
|
|
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rs|b", &zcert, &filename, &filename_len, ¬ext) == FAILURE)
|
2001-09-11 07:57:10 +08:00
|
|
|
return;
|
2001-04-02 07:06:15 +08:00
|
|
|
|
2001-09-11 07:57:10 +08:00
|
|
|
RETVAL_FALSE;
|
2001-04-02 07:06:15 +08:00
|
|
|
|
2001-09-11 07:57:10 +08:00
|
|
|
cert = php_openssl_x509_from_zval(&zcert, 0, &certresource TSRMLS_CC);
|
2002-12-11 06:18:58 +08:00
|
|
|
if (cert == NULL) {
|
|
|
|
php_error_docref(NULL TSRMLS_CC, E_WARNING, "cannot get cert from parameter 1");
|
2001-09-11 07:57:10 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2002-11-10 13:19:40 +08:00
|
|
|
if (php_openssl_safe_mode_chk(filename TSRMLS_CC)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2001-09-11 08:33:25 +08:00
|
|
|
bio_out = BIO_new_file(filename, "w");
|
2002-12-11 06:18:58 +08:00
|
|
|
if (bio_out) {
|
2001-09-11 07:57:10 +08:00
|
|
|
if (!notext)
|
|
|
|
X509_print(bio_out, cert);
|
|
|
|
PEM_write_bio_X509(bio_out, cert);
|
2001-04-02 07:06:15 +08:00
|
|
|
|
2001-09-11 07:57:10 +08:00
|
|
|
RETVAL_TRUE;
|
|
|
|
}
|
2001-09-11 08:33:25 +08:00
|
|
|
else
|
2002-12-11 06:18:58 +08:00
|
|
|
php_error_docref(NULL TSRMLS_CC, E_WARNING, "error opening file %s", filename);
|
2001-09-11 08:33:25 +08:00
|
|
|
|
2001-09-11 07:57:10 +08:00
|
|
|
if (certresource == -1 && cert)
|
|
|
|
X509_free(cert);
|
2001-09-11 08:33:25 +08:00
|
|
|
|
|
|
|
BIO_free(bio_out);
|
|
|
|
|
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2001-12-07 01:51:48 +08:00
|
|
|
/* {{{ proto bool openssl_x509_export(mixed x509, string &out [, bool notext = true])
|
2001-09-11 09:03:59 +08:00
|
|
|
Exports a CERT to file or a var */
|
2001-09-11 08:33:25 +08:00
|
|
|
PHP_FUNCTION(openssl_x509_export)
|
|
|
|
{
|
|
|
|
X509 * cert;
|
|
|
|
zval * zcert = NULL, *zout=NULL;
|
|
|
|
zend_bool notext = 1;
|
|
|
|
BIO * bio_out;
|
|
|
|
long certresource;
|
|
|
|
char * bio_mem_ptr;
|
|
|
|
long bio_mem_len;
|
|
|
|
|
|
|
|
|
|
|
|
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rz|b", &zcert, &zout, ¬ext) == FAILURE)
|
|
|
|
return;
|
|
|
|
|
|
|
|
RETVAL_FALSE;
|
|
|
|
|
|
|
|
cert = php_openssl_x509_from_zval(&zcert, 0, &certresource TSRMLS_CC);
|
2002-12-11 06:18:58 +08:00
|
|
|
if (cert == NULL) {
|
|
|
|
php_error_docref(NULL TSRMLS_CC, E_WARNING, "cannot get cert from parameter 1");
|
2001-09-11 08:33:25 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
bio_out = BIO_new(BIO_s_mem());
|
|
|
|
if (!notext)
|
|
|
|
X509_print(bio_out, cert);
|
|
|
|
PEM_write_bio_X509(bio_out, cert);
|
|
|
|
|
|
|
|
bio_mem_len = BIO_get_mem_data(bio_out, &bio_mem_ptr);
|
|
|
|
ZVAL_STRINGL(zout, bio_mem_ptr, bio_mem_len, 1);
|
|
|
|
|
|
|
|
RETVAL_TRUE;
|
|
|
|
|
|
|
|
if (certresource == -1 && cert)
|
|
|
|
X509_free(cert);
|
|
|
|
|
2001-09-11 07:57:10 +08:00
|
|
|
BIO_free(bio_out);
|
2001-04-02 07:06:15 +08:00
|
|
|
|
2001-09-11 07:57:10 +08:00
|
|
|
}
|
|
|
|
/* }}} */
|
2001-04-02 07:06:15 +08:00
|
|
|
|
2001-09-11 07:57:10 +08:00
|
|
|
/* {{{ proto bool openssl_x509_check_private_key(mixed cert, mixed key)
|
2001-09-11 09:03:59 +08:00
|
|
|
Checks if a private key corresponds to a CERT */
|
2001-09-11 07:57:10 +08:00
|
|
|
PHP_FUNCTION(openssl_x509_check_private_key)
|
|
|
|
{
|
|
|
|
zval * zcert, *zkey;
|
|
|
|
X509 * cert = NULL;
|
|
|
|
EVP_PKEY * key = NULL;
|
|
|
|
long certresource = -1, keyresource = -1;
|
2001-04-02 07:06:15 +08:00
|
|
|
|
2001-09-11 07:57:10 +08:00
|
|
|
RETVAL_FALSE;
|
|
|
|
|
|
|
|
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "zz", &zcert, &zkey) == FAILURE)
|
|
|
|
return;
|
|
|
|
|
|
|
|
cert = php_openssl_x509_from_zval(&zcert, 0, &certresource TSRMLS_CC);
|
|
|
|
if (cert == NULL)
|
|
|
|
RETURN_FALSE;
|
|
|
|
|
|
|
|
key = php_openssl_evp_from_zval(&zkey, 0, "", 1, &keyresource TSRMLS_CC);
|
2002-12-11 06:18:58 +08:00
|
|
|
if (key) {
|
2001-09-11 07:57:10 +08:00
|
|
|
RETVAL_BOOL(X509_check_private_key(cert, key));
|
2001-04-02 07:06:15 +08:00
|
|
|
}
|
|
|
|
|
2001-09-11 07:57:10 +08:00
|
|
|
if (keyresource == -1 && key)
|
|
|
|
EVP_PKEY_free(key);
|
|
|
|
if (certresource == -1 && cert)
|
|
|
|
X509_free(cert);
|
|
|
|
}
|
|
|
|
/* }}} */
|
2001-04-02 07:06:15 +08:00
|
|
|
|
2001-09-11 09:03:59 +08:00
|
|
|
/* {{{ proto array openssl_x509_parse(mixed x509 [, bool shortnames=true])
|
|
|
|
Returns an array of the fields/values of the CERT */
|
2001-09-11 07:57:10 +08:00
|
|
|
PHP_FUNCTION(openssl_x509_parse)
|
|
|
|
{
|
|
|
|
zval * zcert;
|
|
|
|
X509 * cert = NULL;
|
|
|
|
long certresource = -1;
|
|
|
|
int i;
|
|
|
|
zend_bool useshortnames = 1;
|
|
|
|
char * tmpstr;
|
|
|
|
zval * subitem;
|
2001-04-02 07:06:15 +08:00
|
|
|
|
2001-09-11 07:57:10 +08:00
|
|
|
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z|b", &zcert, &useshortnames) == FAILURE)
|
|
|
|
return;
|
|
|
|
|
|
|
|
cert = php_openssl_x509_from_zval(&zcert, 0, &certresource TSRMLS_CC);
|
|
|
|
if (cert == NULL)
|
|
|
|
RETURN_FALSE;
|
|
|
|
|
|
|
|
array_init(return_value);
|
|
|
|
|
|
|
|
if (cert->name)
|
|
|
|
add_assoc_string(return_value, "name", cert->name, 1);
|
|
|
|
/* add_assoc_bool(return_value, "valid", cert->valid); */
|
|
|
|
|
2002-04-13 03:26:08 +08:00
|
|
|
add_assoc_name_entry(return_value, "subject", X509_get_subject_name(cert), useshortnames TSRMLS_CC);
|
2002-03-28 08:56:19 +08:00
|
|
|
/* hash as used in CA directories to lookup cert by subject name */
|
|
|
|
{
|
|
|
|
char buf[32];
|
|
|
|
snprintf(buf, sizeof(buf), "%08lx", X509_subject_name_hash(cert));
|
|
|
|
add_assoc_string(return_value, "hash", buf, 1);
|
|
|
|
}
|
|
|
|
|
2002-04-13 03:26:08 +08:00
|
|
|
add_assoc_name_entry(return_value, "issuer", X509_get_issuer_name(cert), useshortnames TSRMLS_CC);
|
2001-09-11 07:57:10 +08:00
|
|
|
add_assoc_long(return_value, "version", X509_get_version(cert));
|
|
|
|
add_assoc_long(return_value, "serialNumber", ASN1_INTEGER_get(X509_get_serialNumber(cert)));
|
|
|
|
|
|
|
|
add_assoc_asn1_string(return_value, "validFrom", X509_get_notBefore(cert));
|
|
|
|
add_assoc_asn1_string(return_value, "validTo", X509_get_notAfter(cert));
|
|
|
|
|
|
|
|
add_assoc_long(return_value, "validFrom_time_t", asn1_time_to_time_t(X509_get_notBefore(cert) TSRMLS_CC));
|
|
|
|
add_assoc_long(return_value, "validTo_time_t", asn1_time_to_time_t(X509_get_notAfter(cert) TSRMLS_CC));
|
|
|
|
|
|
|
|
tmpstr = X509_alias_get0(cert, NULL);
|
|
|
|
if (tmpstr)
|
|
|
|
add_assoc_string(return_value, "alias", tmpstr, 1);
|
|
|
|
|
|
|
|
/*
|
|
|
|
add_assoc_long(return_value, "signaturetypeLONG", X509_get_signature_type(cert));
|
|
|
|
add_assoc_string(return_value, "signaturetype", OBJ_nid2sn(X509_get_signature_type(cert)), 1);
|
|
|
|
add_assoc_string(return_value, "signaturetypeLN", OBJ_nid2ln(X509_get_signature_type(cert)), 1);
|
|
|
|
*/
|
|
|
|
MAKE_STD_ZVAL(subitem);
|
|
|
|
array_init(subitem);
|
|
|
|
|
|
|
|
/* NOTE: the purposes are added as integer keys - the keys match up to the X509_PURPOSE_SSL_XXX defines
|
|
|
|
in x509v3.h */
|
2002-12-11 06:18:58 +08:00
|
|
|
for (i = 0; i < X509_PURPOSE_get_count(); i++) {
|
2001-09-11 07:57:10 +08:00
|
|
|
int id, purpset;
|
|
|
|
char * pname;
|
|
|
|
X509_PURPOSE * purp;
|
|
|
|
zval * subsub;
|
|
|
|
|
|
|
|
MAKE_STD_ZVAL(subsub);
|
|
|
|
array_init(subsub);
|
|
|
|
|
|
|
|
purp = X509_PURPOSE_get0(i);
|
|
|
|
id = X509_PURPOSE_get_id(purp);
|
|
|
|
|
|
|
|
purpset = X509_check_purpose(cert, id, 0);
|
|
|
|
add_index_bool(subsub, 0, purpset);
|
|
|
|
|
|
|
|
purpset = X509_check_purpose(cert, id, 1);
|
|
|
|
add_index_bool(subsub, 1, purpset);
|
|
|
|
|
|
|
|
pname = useshortnames ? X509_PURPOSE_get0_sname(purp) : X509_PURPOSE_get0_name(purp);
|
|
|
|
add_index_string(subsub, 2, pname, 1);
|
|
|
|
|
|
|
|
/* NOTE: if purpset > 1 then it's a warning - we should mention it ? */
|
|
|
|
|
|
|
|
add_index_zval(subitem, id, subsub);
|
|
|
|
}
|
|
|
|
add_assoc_zval(return_value, "purposes", subitem);
|
|
|
|
|
|
|
|
if (certresource == -1 && cert)
|
|
|
|
X509_free(cert);
|
|
|
|
|
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
/* {{{ load_all_certs_from_file */
|
|
|
|
static STACK_OF(X509) * load_all_certs_from_file(char *certfile)
|
|
|
|
{
|
|
|
|
STACK_OF(X509_INFO) *sk=NULL;
|
|
|
|
STACK_OF(X509) *stack=NULL, *ret=NULL;
|
|
|
|
BIO *in=NULL;
|
|
|
|
X509_INFO *xi;
|
|
|
|
TSRMLS_FETCH();
|
|
|
|
|
|
|
|
if(!(stack = sk_X509_new_null())) {
|
2002-12-11 06:18:58 +08:00
|
|
|
php_error_docref(NULL TSRMLS_CC, E_ERROR, "memory allocation failure");
|
2001-09-11 07:57:10 +08:00
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
2002-11-10 13:19:40 +08:00
|
|
|
if (php_openssl_safe_mode_chk(certfile TSRMLS_CC)) {
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
2001-09-11 07:57:10 +08:00
|
|
|
if(!(in=BIO_new_file(certfile, "r"))) {
|
2002-12-11 06:18:58 +08:00
|
|
|
php_error_docref(NULL TSRMLS_CC, E_WARNING, "error opening the file, %s", certfile);
|
2001-09-11 07:57:10 +08:00
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* This loads from a file, a stack of x509/crl/pkey sets */
|
|
|
|
if(!(sk=PEM_X509_INFO_read_bio(in, NULL, NULL, NULL))) {
|
2002-12-11 06:18:58 +08:00
|
|
|
php_error_docref(NULL TSRMLS_CC, E_WARNING, "error reading the file, %s", certfile);
|
2001-09-11 07:57:10 +08:00
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* scan over it and pull out the certs */
|
|
|
|
while (sk_X509_INFO_num(sk))
|
|
|
|
{
|
|
|
|
xi=sk_X509_INFO_shift(sk);
|
|
|
|
if (xi->x509 != NULL)
|
|
|
|
{
|
|
|
|
sk_X509_push(stack,xi->x509);
|
|
|
|
xi->x509=NULL;
|
2001-04-02 07:06:15 +08:00
|
|
|
}
|
2001-09-11 07:57:10 +08:00
|
|
|
X509_INFO_free(xi);
|
2001-04-02 07:06:15 +08:00
|
|
|
}
|
2001-09-11 07:57:10 +08:00
|
|
|
if(!sk_X509_num(stack)) {
|
2002-12-11 06:18:58 +08:00
|
|
|
php_error_docref(NULL TSRMLS_CC, E_WARNING, "no certificates in file, %s", certfile);
|
2001-09-11 07:57:10 +08:00
|
|
|
sk_X509_free(stack);
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
ret=stack;
|
|
|
|
end:
|
|
|
|
BIO_free(in);
|
|
|
|
sk_X509_INFO_free(sk);
|
2001-04-02 07:06:15 +08:00
|
|
|
|
2001-09-11 07:57:10 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
/* {{{ check_cert */
|
|
|
|
static int check_cert(X509_STORE *ctx, X509 *x, STACK_OF(X509) *untrustedchain, int purpose)
|
|
|
|
{
|
|
|
|
int ret=0;
|
|
|
|
X509_STORE_CTX *csc;
|
|
|
|
TSRMLS_FETCH();
|
|
|
|
|
|
|
|
csc = X509_STORE_CTX_new();
|
|
|
|
if (csc == NULL)
|
|
|
|
{
|
2002-12-11 06:18:58 +08:00
|
|
|
php_error_docref(NULL TSRMLS_CC, E_ERROR, "memory allocation failure");
|
2001-09-11 07:57:10 +08:00
|
|
|
return 0;
|
2001-04-02 07:06:15 +08:00
|
|
|
}
|
2001-09-11 07:57:10 +08:00
|
|
|
X509_STORE_CTX_init(csc, ctx, x, untrustedchain);
|
2001-04-02 07:06:15 +08:00
|
|
|
|
2001-09-11 07:57:10 +08:00
|
|
|
if(purpose >= 0)
|
|
|
|
X509_STORE_CTX_set_purpose(csc, purpose);
|
|
|
|
|
|
|
|
ret = X509_verify_cert(csc);
|
|
|
|
X509_STORE_CTX_free(csc);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2001-09-11 09:03:59 +08:00
|
|
|
/* {{{ proto int openssl_x509_checkpurpose(mixed x509cert, int purpose, array cainfo [, string untrustedfile])
|
|
|
|
Checks the CERT to see if it can be used for the purpose in purpose. cainfo holds information about trusted CAs */
|
2001-09-11 07:57:10 +08:00
|
|
|
PHP_FUNCTION(openssl_x509_checkpurpose)
|
|
|
|
{
|
2001-09-11 08:33:25 +08:00
|
|
|
zval * zcert, * zcainfo = NULL;
|
2001-09-11 07:57:10 +08:00
|
|
|
X509_STORE * cainfo = NULL;
|
|
|
|
X509 * cert = NULL;
|
|
|
|
long certresource = -1;
|
|
|
|
STACK_OF(X509) * untrustedchain = NULL;
|
|
|
|
long purpose;
|
|
|
|
char * untrusted = NULL;
|
|
|
|
long untrusted_len;
|
|
|
|
|
2001-09-11 08:33:25 +08:00
|
|
|
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "zl|a!s", &zcert, &purpose, &zcainfo, &untrusted, &untrusted_len)
|
2001-09-11 07:57:10 +08:00
|
|
|
== FAILURE)
|
|
|
|
return;
|
|
|
|
|
|
|
|
RETVAL_LONG(-1);
|
|
|
|
|
2002-12-11 06:18:58 +08:00
|
|
|
if (untrusted) {
|
2001-09-11 07:57:10 +08:00
|
|
|
untrustedchain = load_all_certs_from_file(untrusted);
|
|
|
|
if (untrustedchain == NULL)
|
|
|
|
goto clean_exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
cainfo = setup_verify(zcainfo TSRMLS_CC);
|
|
|
|
if (cainfo == NULL)
|
|
|
|
goto clean_exit;
|
|
|
|
|
|
|
|
cert = php_openssl_x509_from_zval(&zcert, 0, &certresource TSRMLS_CC);
|
|
|
|
if (cert == NULL)
|
|
|
|
goto clean_exit;
|
|
|
|
|
2001-09-11 08:33:25 +08:00
|
|
|
RETVAL_LONG(check_cert(cainfo, cert, untrustedchain, purpose));
|
2001-09-11 07:57:10 +08:00
|
|
|
|
|
|
|
clean_exit:
|
|
|
|
if (certresource == 1 && cert)
|
2001-04-02 07:06:15 +08:00
|
|
|
X509_free(cert);
|
2001-09-11 07:57:10 +08:00
|
|
|
if (cainfo)
|
|
|
|
X509_STORE_free(cainfo);
|
|
|
|
if (untrustedchain)
|
|
|
|
sk_X509_pop_free(untrustedchain, X509_free);
|
|
|
|
}
|
|
|
|
/* }}} */
|
2001-04-02 07:06:15 +08:00
|
|
|
|
2001-09-11 07:57:10 +08:00
|
|
|
/* {{{ setup_verify
|
|
|
|
* calist is an array containing file and directory names. create a
|
|
|
|
* certificate store and add those certs to it for use in verification.
|
|
|
|
*/
|
|
|
|
static X509_STORE * setup_verify(zval * calist TSRMLS_DC)
|
|
|
|
{
|
|
|
|
X509_STORE *store;
|
|
|
|
X509_LOOKUP * dir_lookup, * file_lookup;
|
|
|
|
HashPosition pos;
|
|
|
|
int ndirs = 0, nfiles = 0;
|
|
|
|
|
|
|
|
store = X509_STORE_new();
|
|
|
|
|
|
|
|
if (store == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
2002-12-11 06:18:58 +08:00
|
|
|
if (calist && (Z_TYPE_P(calist) == IS_ARRAY)) {
|
2001-09-11 07:57:10 +08:00
|
|
|
zend_hash_internal_pointer_reset_ex(HASH_OF(calist), &pos);
|
2002-12-11 06:18:58 +08:00
|
|
|
for (;; zend_hash_move_forward_ex(HASH_OF(calist), &pos)) {
|
2001-09-11 07:57:10 +08:00
|
|
|
zval ** item;
|
|
|
|
struct stat sb;
|
|
|
|
|
|
|
|
if (zend_hash_get_current_data_ex(HASH_OF(calist), (void**)&item, &pos) == FAILURE)
|
|
|
|
break;
|
|
|
|
|
|
|
|
convert_to_string_ex(item);
|
|
|
|
|
2002-12-11 06:18:58 +08:00
|
|
|
if (VCWD_STAT(Z_STRVAL_PP(item), &sb) == -1) {
|
|
|
|
php_error_docref(NULL TSRMLS_CC, E_WARNING, "unable to stat %s", Z_STRVAL_PP(item));
|
2001-09-11 07:57:10 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2002-12-11 06:18:58 +08:00
|
|
|
if ((sb.st_mode & S_IFREG) == S_IFREG) {
|
2001-09-11 07:57:10 +08:00
|
|
|
file_lookup = X509_STORE_add_lookup(store, X509_LOOKUP_file());
|
|
|
|
if (file_lookup == NULL || !X509_LOOKUP_load_file(file_lookup, Z_STRVAL_PP(item), X509_FILETYPE_PEM))
|
2002-12-11 06:18:58 +08:00
|
|
|
php_error_docref(NULL TSRMLS_CC, E_WARNING, "error loading file %s", Z_STRVAL_PP(item));
|
2001-09-11 07:57:10 +08:00
|
|
|
else
|
|
|
|
nfiles++;
|
|
|
|
file_lookup = NULL;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
dir_lookup = X509_STORE_add_lookup(store, X509_LOOKUP_hash_dir());
|
|
|
|
if (dir_lookup == NULL || !X509_LOOKUP_add_dir(dir_lookup, Z_STRVAL_PP(item), X509_FILETYPE_PEM))
|
2002-12-11 06:18:58 +08:00
|
|
|
php_error_docref(NULL TSRMLS_CC, E_WARNING, "error loading directory %s", Z_STRVAL_PP(item));
|
2001-09-11 07:57:10 +08:00
|
|
|
else
|
|
|
|
ndirs++;
|
|
|
|
dir_lookup = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2002-12-11 06:18:58 +08:00
|
|
|
if (nfiles == 0) {
|
2001-09-11 07:57:10 +08:00
|
|
|
file_lookup = X509_STORE_add_lookup(store, X509_LOOKUP_file());
|
|
|
|
if (file_lookup)
|
|
|
|
X509_LOOKUP_load_file(file_lookup, NULL, X509_FILETYPE_DEFAULT);
|
|
|
|
}
|
2002-12-11 06:18:58 +08:00
|
|
|
if (ndirs == 0) {
|
2001-09-11 07:57:10 +08:00
|
|
|
dir_lookup = X509_STORE_add_lookup(store, X509_LOOKUP_hash_dir());
|
|
|
|
if (dir_lookup)
|
|
|
|
X509_LOOKUP_add_dir(dir_lookup, NULL, X509_FILETYPE_DEFAULT);
|
2001-04-02 07:06:15 +08:00
|
|
|
}
|
2001-09-11 07:57:10 +08:00
|
|
|
return store;
|
2001-04-02 07:06:15 +08:00
|
|
|
}
|
2001-06-05 21:12:10 +08:00
|
|
|
/* }}} */
|
2001-04-02 07:06:15 +08:00
|
|
|
|
2001-09-11 07:57:10 +08:00
|
|
|
/* {{{ proto resource openssl_x509_read(mixed cert)
|
2001-09-11 09:03:59 +08:00
|
|
|
Reads X.509 certificates */
|
2001-09-11 07:57:10 +08:00
|
|
|
PHP_FUNCTION(openssl_x509_read)
|
2001-05-05 07:42:11 +08:00
|
|
|
{
|
2001-09-11 07:57:10 +08:00
|
|
|
zval *cert;
|
|
|
|
X509 *x509;
|
2001-05-05 07:42:11 +08:00
|
|
|
|
2001-09-11 07:57:10 +08:00
|
|
|
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &cert) == FAILURE)
|
2001-09-08 01:09:56 +08:00
|
|
|
return;
|
2001-05-05 07:42:11 +08:00
|
|
|
|
2001-09-11 07:57:10 +08:00
|
|
|
Z_TYPE_P(return_value) = IS_RESOURCE;
|
|
|
|
x509 = php_openssl_x509_from_zval(&cert, 1, &Z_LVAL_P(return_value) TSRMLS_CC);
|
2001-05-05 07:42:11 +08:00
|
|
|
|
2001-09-11 07:57:10 +08:00
|
|
|
if (x509 == NULL) {
|
2002-12-11 06:18:58 +08:00
|
|
|
php_error_docref(NULL TSRMLS_CC, E_WARNING, "supplied parameter cannot be coerced into an X509 certificate!");
|
2001-05-05 07:42:11 +08:00
|
|
|
RETURN_FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2001-12-07 01:51:48 +08:00
|
|
|
/* {{{ proto void openssl_x509_free(resource x509)
|
2001-09-11 09:03:59 +08:00
|
|
|
Frees X.509 certificates */
|
2001-09-11 07:57:10 +08:00
|
|
|
PHP_FUNCTION(openssl_x509_free)
|
2001-05-05 07:42:11 +08:00
|
|
|
{
|
2001-09-11 07:57:10 +08:00
|
|
|
zval *x509;
|
|
|
|
X509 *cert;
|
2001-05-05 07:42:11 +08:00
|
|
|
|
2001-09-11 07:57:10 +08:00
|
|
|
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &x509) == FAILURE)
|
|
|
|
return;
|
2001-05-05 07:42:11 +08:00
|
|
|
|
2001-09-11 07:57:10 +08:00
|
|
|
ZEND_FETCH_RESOURCE(cert, X509 *, &x509, -1, "OpenSSL X.509", le_x509);
|
|
|
|
zend_list_delete(Z_LVAL_P(x509));
|
2001-05-05 07:42:11 +08:00
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2001-09-11 07:57:10 +08:00
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
/* {{{ x509 CSR functions */
|
|
|
|
|
|
|
|
/* {{{ php_openssl_make_REQ */
|
2002-04-13 03:26:08 +08:00
|
|
|
static int php_openssl_make_REQ(struct php_x509_request * req, X509_REQ * csr, zval * dn, zval * attribs TSRMLS_DC)
|
2001-05-05 07:42:11 +08:00
|
|
|
{
|
2001-09-11 07:57:10 +08:00
|
|
|
STACK_OF(CONF_VALUE) * dn_sk, *attr_sk = NULL;
|
|
|
|
char * str, *dn_sect, *attr_sect;
|
|
|
|
|
|
|
|
dn_sect = CONF_get_string(req->req_config, req->section_name, "distinguished_name");
|
|
|
|
if (dn_sect == NULL)
|
|
|
|
return FAILURE;
|
|
|
|
dn_sk = CONF_get_section(req->req_config, dn_sect);
|
|
|
|
if (dn_sk == NULL)
|
|
|
|
return FAILURE;
|
|
|
|
attr_sect = CONF_get_string(req->req_config, req->section_name, "attributes");
|
|
|
|
if (attr_sect == NULL)
|
|
|
|
attr_sk = NULL;
|
|
|
|
else {
|
|
|
|
attr_sk = CONF_get_section(req->req_config, attr_sect);
|
|
|
|
if (attr_sk == NULL)
|
|
|
|
return FAILURE;
|
|
|
|
}
|
|
|
|
/* setup the version number: version 1 */
|
2002-12-11 06:18:58 +08:00
|
|
|
if (X509_REQ_set_version(csr, 0L)) {
|
2001-09-11 07:57:10 +08:00
|
|
|
int i, nid;
|
|
|
|
char * type;
|
|
|
|
CONF_VALUE * v;
|
|
|
|
X509_NAME * subj;
|
|
|
|
HashPosition hpos;
|
|
|
|
zval ** item;
|
|
|
|
|
|
|
|
subj = X509_REQ_get_subject_name(csr);
|
|
|
|
/* apply values from the dn hash */
|
|
|
|
zend_hash_internal_pointer_reset_ex(HASH_OF(dn), &hpos);
|
2002-12-11 06:18:58 +08:00
|
|
|
while(zend_hash_get_current_data_ex(HASH_OF(dn), (void**)&item, &hpos) == SUCCESS) {
|
2001-09-11 07:57:10 +08:00
|
|
|
char * strindex; int strindexlen;
|
|
|
|
long intindex;
|
|
|
|
|
|
|
|
zend_hash_get_current_key_ex(HASH_OF(dn), &strindex, &strindexlen, &intindex, 0, &hpos);
|
2001-05-05 07:42:11 +08:00
|
|
|
|
2001-09-11 07:57:10 +08:00
|
|
|
convert_to_string_ex(item);
|
2001-05-05 07:42:11 +08:00
|
|
|
|
2002-12-11 06:18:58 +08:00
|
|
|
if (strindex) {
|
2001-09-11 07:57:10 +08:00
|
|
|
int nid;
|
|
|
|
|
|
|
|
nid = OBJ_txt2nid(strindex);
|
2002-12-11 06:18:58 +08:00
|
|
|
if (nid != NID_undef) {
|
2001-09-11 07:57:10 +08:00
|
|
|
if (!X509_NAME_add_entry_by_NID(subj, nid, MBSTRING_ASC,
|
|
|
|
(unsigned char*)Z_STRVAL_PP(item), -1, -1, 0))
|
|
|
|
{
|
2002-12-11 06:18:58 +08:00
|
|
|
php_error_docref(NULL TSRMLS_CC, E_WARNING, "dn: add_entry_by_NID %d -> %s (failed)", nid, Z_STRVAL_PP(item));
|
2001-09-11 07:57:10 +08:00
|
|
|
return FAILURE;
|
|
|
|
}
|
2002-12-11 06:18:58 +08:00
|
|
|
} else {
|
|
|
|
php_error_docref(NULL TSRMLS_CC, E_WARNING, "dn: %s is not a recognized name", strindex);
|
2001-09-11 07:57:10 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
zend_hash_move_forward_ex(HASH_OF(dn), &hpos);
|
|
|
|
}
|
2001-05-05 07:42:11 +08:00
|
|
|
|
2001-09-11 07:57:10 +08:00
|
|
|
/* Finally apply defaults from config file */
|
2002-12-11 06:18:58 +08:00
|
|
|
for(i = 0; i < sk_CONF_VALUE_num(dn_sk); i++) {
|
2001-09-11 07:57:10 +08:00
|
|
|
int len;
|
|
|
|
char buffer[200];
|
|
|
|
|
|
|
|
v = sk_CONF_VALUE_value(dn_sk, i);
|
|
|
|
type = v->name;
|
|
|
|
|
|
|
|
len = strlen(type);
|
|
|
|
if (len < sizeof("_default"))
|
|
|
|
continue;
|
|
|
|
len -= sizeof("_default") - 1;
|
2002-12-11 06:18:58 +08:00
|
|
|
if (strcmp("_default", type + len) != 0) {
|
2001-09-11 07:57:10 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(buffer, type, len);
|
|
|
|
buffer[len] = '\0';
|
|
|
|
type = buffer;
|
|
|
|
|
|
|
|
/* Skip past any leading X. X: X, etc to allow for multiple
|
|
|
|
* instances */
|
2002-12-11 06:18:58 +08:00
|
|
|
for (str = type; *str; str++) {
|
|
|
|
if (*str == ':' || *str == ',' || *str == '.') {
|
2001-09-11 07:57:10 +08:00
|
|
|
str++;
|
|
|
|
if (*str)
|
|
|
|
type = str;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* if it is already set, skip this */
|
|
|
|
nid = OBJ_txt2nid(type);
|
|
|
|
if (X509_NAME_get_index_by_NID(subj, nid, -1) >= 0)
|
|
|
|
continue;
|
|
|
|
zend_printf("%s not already set; using default of %s\n", type, v->value);
|
|
|
|
if (!X509_NAME_add_entry_by_txt(subj, type, MBSTRING_ASC, (unsigned char*)v->value, -1, -1, 0))
|
|
|
|
{
|
2002-12-11 06:18:58 +08:00
|
|
|
php_error_docref(NULL TSRMLS_CC, E_WARNING, "add_entry_by_txt %s -> %s (failed)", type, v->value);
|
2001-09-11 07:57:10 +08:00
|
|
|
return FAILURE;
|
|
|
|
}
|
2002-12-11 06:18:58 +08:00
|
|
|
if (!X509_NAME_entry_count(subj)) {
|
|
|
|
php_error_docref(NULL TSRMLS_CC, E_WARNING, "no objects specified in config file");
|
2001-09-11 07:57:10 +08:00
|
|
|
return FAILURE;
|
|
|
|
}
|
|
|
|
}
|
2002-12-11 06:18:58 +08:00
|
|
|
if (attribs) {
|
2001-09-11 07:57:10 +08:00
|
|
|
zend_hash_internal_pointer_reset_ex(HASH_OF(attribs), &hpos);
|
2002-12-11 06:18:58 +08:00
|
|
|
while(zend_hash_get_current_data_ex(HASH_OF(attribs), (void**)&item, &hpos) == SUCCESS) {
|
2001-09-11 07:57:10 +08:00
|
|
|
char * strindex; int strindexlen;
|
|
|
|
long intindex;
|
|
|
|
|
|
|
|
zend_hash_get_current_key_ex(HASH_OF(attribs), &strindex, &strindexlen, &intindex, 0, &hpos);
|
|
|
|
convert_to_string_ex(item);
|
|
|
|
|
2002-12-11 06:18:58 +08:00
|
|
|
if (strindex) {
|
2001-09-11 07:57:10 +08:00
|
|
|
int nid;
|
|
|
|
|
|
|
|
nid = OBJ_txt2nid(strindex);
|
2002-12-11 06:18:58 +08:00
|
|
|
if (nid != NID_undef) {
|
2001-09-11 07:57:10 +08:00
|
|
|
if (!X509_NAME_add_entry_by_NID(subj, nid, MBSTRING_ASC,
|
|
|
|
(unsigned char*)Z_STRVAL_PP(item), -1, -1, 0))
|
|
|
|
{
|
2002-12-11 06:18:58 +08:00
|
|
|
php_error_docref(NULL TSRMLS_CC, E_WARNING, "attribs: add_entry_by_NID %d -> %s (failed)", nid, Z_STRVAL_PP(item));
|
2001-09-11 07:57:10 +08:00
|
|
|
return FAILURE;
|
|
|
|
}
|
2002-12-11 06:18:58 +08:00
|
|
|
} else {
|
|
|
|
php_error_docref(NULL TSRMLS_CC, E_WARNING, "dn: %s is not a recognized name", strindex);
|
2001-09-11 07:57:10 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
zend_hash_move_forward_ex(HASH_OF(attribs), &hpos);
|
|
|
|
}
|
2002-12-11 06:18:58 +08:00
|
|
|
for (i = 0; i < sk_CONF_VALUE_num(attr_sk); i++) {
|
2001-09-11 07:57:10 +08:00
|
|
|
v = sk_CONF_VALUE_value(attr_sk, i);
|
|
|
|
/* if it is already set, skip this */
|
|
|
|
nid = OBJ_txt2nid(v->name);
|
|
|
|
if (X509_REQ_get_attr_by_NID(csr, nid, -1) >= 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (!X509_REQ_add1_attr_by_txt(csr, v->name, MBSTRING_ASC,
|
|
|
|
(unsigned char*)v->value, -1))
|
|
|
|
{
|
2002-12-11 06:18:58 +08:00
|
|
|
php_error_docref(NULL TSRMLS_CC, E_WARNING, "add1_attr_by_txt %s -> %s (failed)", v->name, v->value);
|
2001-09-11 07:57:10 +08:00
|
|
|
return FAILURE;
|
|
|
|
}
|
|
|
|
}
|
2001-05-05 07:42:11 +08:00
|
|
|
|
2001-09-11 07:57:10 +08:00
|
|
|
}
|
2001-05-05 07:42:11 +08:00
|
|
|
}
|
|
|
|
|
2001-09-11 07:57:10 +08:00
|
|
|
X509_REQ_set_pubkey(csr, req->priv_key);
|
|
|
|
return SUCCESS;
|
2001-05-05 07:42:11 +08:00
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2001-09-11 07:57:10 +08:00
|
|
|
/* {{{ php_openssl_csr_from_zval */
|
|
|
|
static X509_REQ * php_openssl_csr_from_zval(zval ** val, int makeresource, long * resourceval TSRMLS_DC)
|
2001-05-05 07:42:11 +08:00
|
|
|
{
|
2001-09-11 07:57:10 +08:00
|
|
|
X509_REQ * csr = NULL;
|
2002-04-19 15:56:41 +08:00
|
|
|
char * filename = NULL;
|
2001-09-11 07:57:10 +08:00
|
|
|
BIO * in;
|
2001-05-05 07:42:11 +08:00
|
|
|
|
2001-09-11 07:57:10 +08:00
|
|
|
if (resourceval)
|
|
|
|
*resourceval = -1;
|
2001-05-05 07:42:11 +08:00
|
|
|
|
2002-12-11 06:18:58 +08:00
|
|
|
if (Z_TYPE_PP(val) == IS_RESOURCE) {
|
2001-09-11 07:57:10 +08:00
|
|
|
void * what;
|
|
|
|
int type;
|
2001-05-05 07:42:11 +08:00
|
|
|
|
2001-09-11 07:57:10 +08:00
|
|
|
what = zend_fetch_resource(val TSRMLS_CC, -1, "OpenSSL X.509 CSR", &type, 1, le_csr);
|
2002-12-11 06:18:58 +08:00
|
|
|
if (what) {
|
2001-09-11 07:57:10 +08:00
|
|
|
if (resourceval)
|
|
|
|
*resourceval = Z_LVAL_PP(val);
|
|
|
|
return (X509_REQ*)what;
|
|
|
|
}
|
|
|
|
return NULL;
|
2001-05-05 07:42:11 +08:00
|
|
|
}
|
2001-09-11 07:57:10 +08:00
|
|
|
convert_to_string_ex(val);
|
2001-05-05 07:42:11 +08:00
|
|
|
|
2001-09-11 07:57:10 +08:00
|
|
|
if (Z_STRLEN_PP(val) > 7 && memcmp(Z_STRVAL_PP(val), "file://", 7) == 0)
|
|
|
|
filename = Z_STRVAL_PP(val) + 7;
|
2001-05-05 07:42:11 +08:00
|
|
|
|
2002-11-10 13:19:40 +08:00
|
|
|
if (filename) {
|
|
|
|
if (php_openssl_safe_mode_chk(filename TSRMLS_CC)) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2001-09-11 07:57:10 +08:00
|
|
|
in = BIO_new_file(filename, "r");
|
2002-11-10 13:19:40 +08:00
|
|
|
}
|
2001-09-11 07:57:10 +08:00
|
|
|
else
|
|
|
|
in = BIO_new_mem_buf(Z_STRVAL_PP(val), Z_STRLEN_PP(val));
|
|
|
|
|
|
|
|
csr = PEM_read_bio_X509_REQ(in, NULL,NULL,NULL);
|
|
|
|
BIO_free(in);
|
2001-05-05 07:42:11 +08:00
|
|
|
|
2001-09-11 07:57:10 +08:00
|
|
|
return csr;
|
2001-05-05 07:42:11 +08:00
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2001-12-07 01:51:48 +08:00
|
|
|
/* {{{ proto bool openssl_csr_export_to_file(resource csr, string outfilename [, bool notext=true])
|
2002-08-11 04:19:49 +08:00
|
|
|
Exports a CSR to file */
|
2001-09-11 08:33:25 +08:00
|
|
|
PHP_FUNCTION(openssl_csr_export_to_file)
|
2000-11-14 03:47:20 +08:00
|
|
|
{
|
2001-09-11 07:57:10 +08:00
|
|
|
X509_REQ * csr;
|
2001-09-11 08:33:25 +08:00
|
|
|
zval * zcsr = NULL;
|
|
|
|
zend_bool notext = 1;
|
|
|
|
char * filename = NULL; long filename_len;
|
2001-09-11 07:57:10 +08:00
|
|
|
BIO * bio_out;
|
|
|
|
long csr_resource;
|
2001-04-02 07:06:15 +08:00
|
|
|
|
2001-09-11 08:33:25 +08:00
|
|
|
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rs|b", &zcsr, &filename, &filename_len, ¬ext) == FAILURE)
|
2001-09-08 01:09:56 +08:00
|
|
|
return;
|
2001-04-02 07:06:15 +08:00
|
|
|
|
2001-09-11 07:57:10 +08:00
|
|
|
RETVAL_FALSE;
|
2000-11-18 03:49:13 +08:00
|
|
|
|
2001-09-11 07:57:10 +08:00
|
|
|
csr = php_openssl_csr_from_zval(&zcsr, 0, &csr_resource TSRMLS_CC);
|
2002-12-11 06:18:58 +08:00
|
|
|
if (csr == NULL) {
|
|
|
|
php_error_docref(NULL TSRMLS_CC, E_WARNING, "cannot get CSR from parameter 1");
|
2001-09-11 07:57:10 +08:00
|
|
|
return;
|
2000-11-14 17:54:25 +08:00
|
|
|
}
|
|
|
|
|
2002-11-10 13:19:40 +08:00
|
|
|
if (php_openssl_safe_mode_chk(filename TSRMLS_CC)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2001-09-11 08:33:25 +08:00
|
|
|
bio_out = BIO_new_file(filename, "w");
|
2002-12-11 06:18:58 +08:00
|
|
|
if (bio_out) {
|
2001-09-11 07:57:10 +08:00
|
|
|
if (!notext)
|
|
|
|
X509_REQ_print(bio_out, csr);
|
|
|
|
PEM_write_bio_X509_REQ(bio_out, csr);
|
2001-09-11 08:33:25 +08:00
|
|
|
RETVAL_TRUE;
|
2002-12-11 06:18:58 +08:00
|
|
|
} else {
|
|
|
|
php_error_docref(NULL TSRMLS_CC, E_WARNING, "error opening file %s", filename);
|
2001-09-11 08:33:25 +08:00
|
|
|
}
|
2001-04-02 07:06:15 +08:00
|
|
|
|
2001-09-11 08:33:25 +08:00
|
|
|
if (csr_resource == -1 && csr)
|
|
|
|
X509_REQ_free(csr);
|
2001-04-02 07:06:15 +08:00
|
|
|
|
2001-09-11 08:33:25 +08:00
|
|
|
BIO_free(bio_out);
|
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
|
|
|
|
|
2001-12-07 01:51:48 +08:00
|
|
|
/* {{{ proto bool openssl_csr_export(resource csr, string &out [, bool notext=true])
|
2001-09-11 09:03:59 +08:00
|
|
|
Exports a CSR to file or a var */
|
2001-09-11 08:33:25 +08:00
|
|
|
PHP_FUNCTION(openssl_csr_export)
|
|
|
|
{
|
|
|
|
X509_REQ * csr;
|
|
|
|
zval * zcsr = NULL, *zout=NULL;
|
|
|
|
zend_bool notext = 1;
|
|
|
|
BIO * bio_out;
|
|
|
|
long csr_resource;
|
|
|
|
char * bio_mem_ptr;
|
|
|
|
long bio_mem_len;
|
|
|
|
|
|
|
|
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rz|b", &zcsr, &zout, ¬ext) == FAILURE)
|
|
|
|
return;
|
|
|
|
|
|
|
|
RETVAL_FALSE;
|
|
|
|
|
|
|
|
csr = php_openssl_csr_from_zval(&zcsr, 0, &csr_resource TSRMLS_CC);
|
2002-12-11 06:18:58 +08:00
|
|
|
if (csr == NULL) {
|
|
|
|
php_error_docref(NULL TSRMLS_CC, E_WARNING, "cannot get CSR from parameter 1");
|
2001-09-11 08:33:25 +08:00
|
|
|
return;
|
2001-04-02 07:06:15 +08:00
|
|
|
}
|
2001-09-11 08:33:25 +08:00
|
|
|
|
|
|
|
/* export to a var */
|
|
|
|
|
|
|
|
bio_out = BIO_new(BIO_s_mem());
|
|
|
|
if (!notext)
|
|
|
|
X509_REQ_print(bio_out, csr);
|
|
|
|
PEM_write_bio_X509_REQ(bio_out, csr);
|
|
|
|
|
|
|
|
bio_mem_len = BIO_get_mem_data(bio_out, &bio_mem_ptr);
|
|
|
|
ZVAL_STRINGL(zout, bio_mem_ptr, bio_mem_len, 1);
|
|
|
|
|
|
|
|
RETVAL_TRUE;
|
|
|
|
|
2001-09-11 07:57:10 +08:00
|
|
|
if (csr_resource == -1 && csr)
|
|
|
|
X509_REQ_free(csr);
|
2001-09-11 08:33:25 +08:00
|
|
|
|
2001-09-11 07:57:10 +08:00
|
|
|
BIO_free(bio_out);
|
2001-04-02 07:06:15 +08:00
|
|
|
}
|
2001-09-11 07:57:10 +08:00
|
|
|
/* }}} */
|
2001-04-02 07:06:15 +08:00
|
|
|
|
2001-12-07 15:27:00 +08:00
|
|
|
/* {{{ proto resource openssl_csr_sign(mixed csr, mixed x509, mixed priv_key, long days)
|
2001-09-11 09:03:59 +08:00
|
|
|
Signs a cert with another CERT */
|
2001-09-11 07:57:10 +08:00
|
|
|
PHP_FUNCTION(openssl_csr_sign)
|
2001-04-02 07:06:15 +08:00
|
|
|
{
|
2001-09-11 07:57:10 +08:00
|
|
|
zval * zcert = NULL, *zcsr, *zpkey, *args = NULL;
|
|
|
|
long num_days;
|
|
|
|
X509 * cert = NULL, *new_cert = NULL;
|
|
|
|
X509_REQ * csr;
|
2002-04-19 15:56:41 +08:00
|
|
|
EVP_PKEY * key = NULL, *priv_key;
|
2001-09-11 07:57:10 +08:00
|
|
|
long csr_resource, certresource, keyresource;
|
|
|
|
int i;
|
|
|
|
struct php_x509_request req;
|
|
|
|
|
|
|
|
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "zz!zl|a!", &zcsr, &zcert, &zpkey, &num_days, &args) == FAILURE)
|
|
|
|
return;
|
2001-04-02 07:06:15 +08:00
|
|
|
|
2001-09-11 07:57:10 +08:00
|
|
|
RETVAL_FALSE;
|
|
|
|
PHP_SSL_REQ_INIT(&req);
|
|
|
|
|
|
|
|
csr = php_openssl_csr_from_zval(&zcsr, 0, &csr_resource TSRMLS_CC);
|
2002-12-11 06:18:58 +08:00
|
|
|
if (csr == NULL) {
|
|
|
|
php_error_docref(NULL TSRMLS_CC, E_WARNING, "cannot get CSR from parameter 1");
|
2001-09-11 07:57:10 +08:00
|
|
|
return;
|
|
|
|
}
|
2002-12-11 06:18:58 +08:00
|
|
|
if (zcert) {
|
2001-09-11 07:57:10 +08:00
|
|
|
cert = php_openssl_x509_from_zval(&zcert, 0, &certresource TSRMLS_CC);
|
2002-12-11 06:18:58 +08:00
|
|
|
if (cert == NULL) {
|
|
|
|
php_error_docref(NULL TSRMLS_CC, E_WARNING, "cannot get cert from parameter 2");
|
2001-09-11 07:57:10 +08:00
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
priv_key = php_openssl_evp_from_zval(&zpkey, 0, "", 1, &keyresource TSRMLS_CC);
|
2002-12-11 06:18:58 +08:00
|
|
|
if (priv_key == NULL) {
|
|
|
|
php_error_docref(NULL TSRMLS_CC, E_WARNING, "cannot get private key from parameter 3");
|
2001-09-11 07:57:10 +08:00
|
|
|
goto cleanup;
|
|
|
|
}
|
2002-12-11 06:18:58 +08:00
|
|
|
if (cert && !X509_check_private_key(cert, priv_key)) {
|
|
|
|
php_error_docref(NULL TSRMLS_CC, E_WARNING, "private key does not correspond to signing cert");
|
2001-09-11 07:57:10 +08:00
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (PHP_SSL_REQ_PARSE(&req, args) == FAILURE)
|
|
|
|
goto cleanup;
|
2001-04-02 07:06:15 +08:00
|
|
|
|
2001-09-11 07:57:10 +08:00
|
|
|
/* Check that the request matches the signature */
|
|
|
|
key = X509_REQ_get_pubkey(csr);
|
2002-12-11 06:18:58 +08:00
|
|
|
if (key == NULL) {
|
|
|
|
php_error_docref(NULL TSRMLS_CC, E_WARNING, "error unpacking public key");
|
2001-09-11 07:57:10 +08:00
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
i = X509_REQ_verify(csr, key);
|
2001-04-02 07:06:15 +08:00
|
|
|
|
2002-12-11 06:18:58 +08:00
|
|
|
if (i < 0) {
|
|
|
|
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Signature verification problems");
|
2001-09-11 07:57:10 +08:00
|
|
|
goto cleanup;
|
|
|
|
}
|
2002-12-11 06:18:58 +08:00
|
|
|
else if (i==0) {
|
|
|
|
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Signature did not match the certificate request");
|
2001-09-11 07:57:10 +08:00
|
|
|
goto cleanup;
|
2001-04-02 07:06:15 +08:00
|
|
|
}
|
2001-09-11 07:57:10 +08:00
|
|
|
|
|
|
|
/* Now we can get on with it */
|
|
|
|
|
|
|
|
new_cert = X509_new();
|
2002-12-11 06:18:58 +08:00
|
|
|
if (new_cert == NULL) {
|
|
|
|
php_error_docref(NULL TSRMLS_CC, E_WARNING, "No memory");
|
2001-09-11 07:57:10 +08:00
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
/* Version 3 cert */
|
|
|
|
if (!X509_set_version(new_cert, 3))
|
|
|
|
goto cleanup;
|
2001-04-02 07:06:15 +08:00
|
|
|
|
2001-09-11 07:57:10 +08:00
|
|
|
/* TODO: Allow specifying */
|
|
|
|
ASN1_INTEGER_set(X509_get_serialNumber(new_cert), 0L);
|
|
|
|
|
|
|
|
X509_set_subject_name(new_cert, X509_REQ_get_subject_name(csr));
|
2001-04-02 07:06:15 +08:00
|
|
|
|
2001-09-11 07:57:10 +08:00
|
|
|
if (cert == NULL)
|
|
|
|
cert = new_cert;
|
2001-04-02 07:06:15 +08:00
|
|
|
|
2001-09-11 07:57:10 +08:00
|
|
|
if (!X509_set_issuer_name(new_cert, X509_get_subject_name(cert)))
|
|
|
|
goto cleanup;
|
2001-04-02 07:06:15 +08:00
|
|
|
|
2001-09-11 07:57:10 +08:00
|
|
|
X509_gmtime_adj(X509_get_notBefore(new_cert), 0);
|
|
|
|
X509_gmtime_adj(X509_get_notAfter(new_cert), (long)60*60*24*num_days);
|
|
|
|
i = X509_set_pubkey(new_cert, key);
|
|
|
|
if (!i)
|
|
|
|
goto cleanup;
|
2001-04-02 07:06:15 +08:00
|
|
|
|
2002-12-11 06:18:58 +08:00
|
|
|
if (req.request_extensions_section) {
|
2001-09-11 07:57:10 +08:00
|
|
|
X509V3_CTX ctx;
|
|
|
|
|
|
|
|
X509V3_set_ctx(&ctx, cert, new_cert, csr, NULL, 0);
|
|
|
|
X509V3_set_conf_lhash(&ctx, req.req_config);
|
|
|
|
if (!X509V3_EXT_add_conf(req.req_config, &ctx, req.request_extensions_section, new_cert))
|
|
|
|
goto cleanup;
|
|
|
|
}
|
2001-04-02 07:06:15 +08:00
|
|
|
|
2001-09-11 07:57:10 +08:00
|
|
|
/* Now sign it */
|
2002-12-11 06:18:58 +08:00
|
|
|
if (!X509_sign(new_cert, priv_key, req.digest)) {
|
|
|
|
php_error_docref(NULL TSRMLS_CC, E_WARNING, "failed to sign it");
|
2001-09-11 07:57:10 +08:00
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Succeeded; lets return the cert */
|
|
|
|
RETVAL_RESOURCE(zend_list_insert(new_cert, le_x509));
|
|
|
|
new_cert = NULL;
|
|
|
|
|
|
|
|
cleanup:
|
2001-04-02 07:06:15 +08:00
|
|
|
|
2001-09-11 07:57:10 +08:00
|
|
|
if (cert == new_cert)
|
|
|
|
cert = NULL;
|
|
|
|
|
|
|
|
PHP_SSL_REQ_DISPOSE(&req);
|
2001-04-02 07:06:15 +08:00
|
|
|
|
2001-09-11 07:57:10 +08:00
|
|
|
if (keyresource == -1 && priv_key)
|
|
|
|
EVP_PKEY_free(priv_key);
|
|
|
|
if (key)
|
|
|
|
EVP_PKEY_free(key);
|
|
|
|
if (csr_resource == -1 && csr)
|
|
|
|
X509_REQ_free(csr);
|
|
|
|
if (certresource == -1 && cert)
|
|
|
|
X509_free(cert);
|
|
|
|
if (new_cert)
|
|
|
|
X509_free(new_cert);
|
|
|
|
}
|
|
|
|
/* }}} */
|
2000-11-14 17:54:25 +08:00
|
|
|
|
2001-09-11 09:03:59 +08:00
|
|
|
/* {{{ proto bool openssl_csr_new(array dn, resource &privkey [, array extraattribs, array configargs])
|
|
|
|
Generates a privkey and CSR */
|
2001-09-11 07:57:10 +08:00
|
|
|
PHP_FUNCTION(openssl_csr_new)
|
|
|
|
{
|
|
|
|
struct php_x509_request req;
|
|
|
|
zval * args = NULL, * dn, *attribs = NULL;
|
|
|
|
zval * out_pkey;
|
|
|
|
X509_REQ * csr = NULL;
|
|
|
|
int we_made_the_key = 1;
|
|
|
|
long key_resource;
|
|
|
|
|
|
|
|
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "az|a!a!", &dn, &out_pkey, &args, &attribs) == FAILURE)
|
|
|
|
return;
|
2001-04-02 07:06:15 +08:00
|
|
|
|
2001-09-11 07:57:10 +08:00
|
|
|
RETVAL_FALSE;
|
|
|
|
|
|
|
|
PHP_SSL_REQ_INIT(&req);
|
2001-04-02 07:06:15 +08:00
|
|
|
|
2001-09-11 07:57:10 +08:00
|
|
|
if (PHP_SSL_REQ_PARSE(&req, args) == SUCCESS)
|
|
|
|
{
|
|
|
|
/* Generate or use a private key */
|
2002-12-11 06:18:58 +08:00
|
|
|
if (Z_TYPE_P(out_pkey) != IS_NULL) {
|
2001-09-11 07:57:10 +08:00
|
|
|
req.priv_key = php_openssl_evp_from_zval(&out_pkey, 0, NULL, 0, &key_resource TSRMLS_CC);
|
|
|
|
if (req.priv_key != NULL)
|
|
|
|
we_made_the_key = 0;
|
|
|
|
}
|
|
|
|
if (req.priv_key == NULL)
|
|
|
|
php_openssl_generate_private_key(&req TSRMLS_CC);
|
|
|
|
if (req.priv_key == NULL)
|
2002-12-11 06:18:58 +08:00
|
|
|
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to generate a private key");
|
2001-09-11 07:57:10 +08:00
|
|
|
else {
|
|
|
|
csr = X509_REQ_new();
|
2002-12-11 06:18:58 +08:00
|
|
|
if (csr) {
|
|
|
|
if (php_openssl_make_REQ(&req, csr, dn, attribs TSRMLS_CC) == SUCCESS) {
|
2001-09-11 07:57:10 +08:00
|
|
|
X509V3_CTX ext_ctx;
|
|
|
|
|
|
|
|
X509V3_set_ctx(&ext_ctx, NULL, NULL, csr, NULL, 0);
|
|
|
|
X509V3_set_conf_lhash(&ext_ctx, req.req_config);
|
|
|
|
|
|
|
|
/* Add extensions */
|
|
|
|
if (req.request_extensions_section && !X509V3_EXT_REQ_add_conf(req.req_config,
|
|
|
|
&ext_ctx, req.request_extensions_section, csr))
|
|
|
|
{
|
2002-12-11 06:18:58 +08:00
|
|
|
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Error loading extension section %s", req.request_extensions_section);
|
2001-09-11 07:57:10 +08:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
RETVAL_TRUE;
|
|
|
|
|
2002-12-11 06:18:58 +08:00
|
|
|
if (X509_REQ_sign(csr, req.priv_key, req.digest)) {
|
2001-09-11 07:57:10 +08:00
|
|
|
|
|
|
|
RETVAL_RESOURCE(zend_list_insert(csr, le_csr));
|
|
|
|
csr = NULL;
|
2002-12-11 06:18:58 +08:00
|
|
|
} else {
|
|
|
|
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Error signing request");
|
2001-09-11 07:57:10 +08:00
|
|
|
}
|
|
|
|
|
2002-12-11 06:18:58 +08:00
|
|
|
if (we_made_the_key) {
|
2001-09-11 07:57:10 +08:00
|
|
|
/* and a resource for the private key */
|
|
|
|
ZVAL_RESOURCE(out_pkey, zend_list_insert(req.priv_key, le_key));
|
|
|
|
}
|
|
|
|
else if (key_resource != -1)
|
|
|
|
req.priv_key = NULL; /* make sure the cleanup code doesn't zap it! */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (csr)
|
|
|
|
X509_REQ_free(csr);
|
|
|
|
PHP_SSL_REQ_DISPOSE(&req);
|
2001-04-02 07:06:15 +08:00
|
|
|
}
|
2001-05-05 07:42:11 +08:00
|
|
|
/* }}} */
|
2001-04-02 07:06:15 +08:00
|
|
|
|
2001-09-11 07:57:10 +08:00
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
/* {{{ EVP Public/Private key functions */
|
|
|
|
|
|
|
|
/* {{{ php_openssl_evp_from_zval
|
|
|
|
Given a zval, coerce it into a EVP_PKEY object.
|
|
|
|
It can be:
|
|
|
|
1. private key resource from openssl_get_privatekey()
|
|
|
|
2. X509 resource -> public key will be extracted from it
|
|
|
|
3. if it starts with file:// interpreted as path to key file
|
|
|
|
4. interpreted as the data from the cert/key file and interpreted in same way as openssl_get_privatekey()
|
|
|
|
5. an array(0 => [items 2..4], 1 => passphrase)
|
2002-12-12 21:42:23 +08:00
|
|
|
6. if val is a string (possibly starting with file:///) and it is not an X509 certificate, then interpret as public key
|
2001-09-11 07:57:10 +08:00
|
|
|
NOTE: If you are requesting a private key but have not specified a passphrase, you should use an
|
|
|
|
empty string rather than NULL for the passphrase - NULL causes a passphrase prompt to be emitted in
|
|
|
|
the Apache error log!
|
|
|
|
*/
|
|
|
|
static EVP_PKEY * php_openssl_evp_from_zval(zval ** val, int public_key, char * passphrase, int makeresource, long * resourceval TSRMLS_DC)
|
2001-04-02 07:06:15 +08:00
|
|
|
{
|
2001-09-11 07:57:10 +08:00
|
|
|
EVP_PKEY * key = NULL;
|
2001-04-02 07:06:15 +08:00
|
|
|
X509 * cert = NULL;
|
2001-09-11 07:57:10 +08:00
|
|
|
int free_cert = 0;
|
|
|
|
long cert_res = -1;
|
|
|
|
char * filename = NULL;
|
2001-09-08 01:09:56 +08:00
|
|
|
|
2001-09-11 07:57:10 +08:00
|
|
|
if (resourceval)
|
|
|
|
*resourceval = -1;
|
2001-04-02 07:06:15 +08:00
|
|
|
|
2002-12-11 06:18:58 +08:00
|
|
|
if (Z_TYPE_PP(val) == IS_ARRAY) {
|
2001-09-11 07:57:10 +08:00
|
|
|
zval ** zphrase;
|
|
|
|
|
|
|
|
/* get passphrase */
|
2001-04-02 07:06:15 +08:00
|
|
|
|
2002-12-11 06:18:58 +08:00
|
|
|
if (zend_hash_index_find(HASH_OF(*val), 1, (void **)&zphrase) == FAILURE) {
|
|
|
|
php_error_docref(NULL TSRMLS_CC, E_WARNING, "key array must be of the form array(0 => key, 1 => phrase)");
|
2001-09-11 07:57:10 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
convert_to_string_ex(zphrase);
|
|
|
|
passphrase = Z_STRVAL_PP(zphrase);
|
2001-04-02 07:06:15 +08:00
|
|
|
|
2001-09-11 07:57:10 +08:00
|
|
|
/* now set val to be the key param and continue */
|
2002-12-11 06:18:58 +08:00
|
|
|
if (zend_hash_index_find(HASH_OF(*val), 0, (void **)&val) == FAILURE) {
|
|
|
|
php_error_docref(NULL TSRMLS_CC, E_WARNING, "key array must be of the form array(0 => key, 1 => phrase)");
|
2001-09-11 07:57:10 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
2001-04-02 07:06:15 +08:00
|
|
|
|
2002-12-11 06:18:58 +08:00
|
|
|
if (Z_TYPE_PP(val) == IS_RESOURCE) {
|
2001-09-11 07:57:10 +08:00
|
|
|
void * what;
|
|
|
|
int type;
|
2001-04-02 07:06:15 +08:00
|
|
|
|
2001-09-11 07:57:10 +08:00
|
|
|
what = zend_fetch_resource(val TSRMLS_CC, -1, "OpenSSL X.509/key", &type, 2, le_x509, le_key);
|
|
|
|
if (!what)
|
|
|
|
return NULL;
|
2001-04-02 07:06:15 +08:00
|
|
|
|
2001-09-11 07:57:10 +08:00
|
|
|
if (resourceval)
|
|
|
|
*resourceval = Z_LVAL_PP(val);
|
2001-04-02 07:06:15 +08:00
|
|
|
|
2002-12-11 06:18:58 +08:00
|
|
|
if (type == le_x509) {
|
2001-09-11 07:57:10 +08:00
|
|
|
/* extract key from cert, depending on public_key param */
|
|
|
|
cert = (X509*)what;
|
|
|
|
free_cert = 0;
|
|
|
|
}
|
2002-12-11 06:18:58 +08:00
|
|
|
else if (type == le_key) {
|
2002-12-12 21:42:23 +08:00
|
|
|
/* check whether it is actually a private key if requested */
|
2002-12-13 17:17:04 +08:00
|
|
|
if (!public_key && !php_openssl_is_private_key((EVP_PKEY*)what TSRMLS_CC))
|
2002-12-12 21:42:23 +08:00
|
|
|
{
|
|
|
|
php_error_docref(NULL TSRMLS_CC, E_WARNING, "supplied key param is a public key");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2001-09-11 07:57:10 +08:00
|
|
|
/* got the key - return it */
|
|
|
|
return (EVP_PKEY*)what;
|
|
|
|
}
|
2001-04-02 07:06:15 +08:00
|
|
|
|
2001-09-11 07:57:10 +08:00
|
|
|
/* other types could be used here - eg: file pointers and read in the data from them */
|
2001-04-02 07:06:15 +08:00
|
|
|
|
2001-09-11 07:57:10 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* force it to be a string and check if it refers to a file */
|
|
|
|
convert_to_string_ex(val);
|
2001-04-02 07:06:15 +08:00
|
|
|
|
2001-09-11 07:57:10 +08:00
|
|
|
if (Z_STRLEN_PP(val) > 7 && memcmp(Z_STRVAL_PP(val), "file://", 7) == 0)
|
|
|
|
filename = Z_STRVAL_PP(val) + 7;
|
2001-04-02 07:06:15 +08:00
|
|
|
|
2001-09-11 07:57:10 +08:00
|
|
|
/* it's an X509 file/cert of some kind, and we need to extract the data from that */
|
2002-12-11 06:18:58 +08:00
|
|
|
if (public_key) {
|
2001-09-11 07:57:10 +08:00
|
|
|
cert = php_openssl_x509_from_zval(val, 0, &cert_res TSRMLS_CC);
|
|
|
|
free_cert = (cert_res == -1);
|
|
|
|
/* actual extraction done later */
|
2002-12-12 21:42:23 +08:00
|
|
|
if (!cert) {
|
|
|
|
/* not a X509 certificate, try to retrieve public key */
|
|
|
|
BIO* in;
|
|
|
|
if (filename)
|
|
|
|
in = BIO_new_file(filename, "r");
|
|
|
|
else
|
|
|
|
in = BIO_new_mem_buf(Z_STRVAL_PP(val), Z_STRLEN_PP(val));
|
|
|
|
|
|
|
|
if (in == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
key = PEM_read_bio_PUBKEY(in, NULL,NULL, NULL);
|
|
|
|
BIO_free(in);
|
|
|
|
}
|
2001-09-11 07:57:10 +08:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* we want the private key */
|
2002-12-11 06:18:58 +08:00
|
|
|
if (filename) {
|
2002-11-10 13:19:40 +08:00
|
|
|
BIO *in;
|
|
|
|
if (php_openssl_safe_mode_chk(filename TSRMLS_CC)) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
in = BIO_new_file(filename, "r");
|
2001-09-11 07:57:10 +08:00
|
|
|
if (in == NULL)
|
|
|
|
return NULL;
|
|
|
|
key = PEM_read_bio_PrivateKey(in, NULL,NULL, passphrase);
|
|
|
|
BIO_free(in);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
BIO * b = BIO_new_mem_buf(Z_STRVAL_PP(val), Z_STRLEN_PP(val));
|
|
|
|
if (b == NULL)
|
|
|
|
return NULL;
|
2001-04-02 07:06:15 +08:00
|
|
|
|
2001-09-11 07:57:10 +08:00
|
|
|
key = (EVP_PKEY *) PEM_ASN1_read_bio((char *(*)())d2i_PrivateKey,
|
|
|
|
PEM_STRING_EVP_PKEY, b,
|
|
|
|
NULL, NULL, passphrase);
|
|
|
|
BIO_free(b);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2001-04-02 07:06:15 +08:00
|
|
|
|
2002-12-11 06:18:58 +08:00
|
|
|
if (public_key && cert && key == NULL) {
|
2001-09-11 07:57:10 +08:00
|
|
|
/* extract public key from X509 cert */
|
|
|
|
key = (EVP_PKEY *) X509_get_pubkey(cert);
|
2000-11-14 03:47:20 +08:00
|
|
|
}
|
2001-04-02 07:06:15 +08:00
|
|
|
|
2001-09-11 07:57:10 +08:00
|
|
|
if (free_cert && cert)
|
2001-04-02 07:06:15 +08:00
|
|
|
X509_free(cert);
|
2000-11-14 03:47:20 +08:00
|
|
|
|
2002-12-11 06:18:58 +08:00
|
|
|
if (key && makeresource && resourceval) {
|
2002-03-26 08:03:11 +08:00
|
|
|
*resourceval = ZEND_REGISTER_RESOURCE(NULL, key, le_key);
|
2001-09-11 07:57:10 +08:00
|
|
|
}
|
|
|
|
return key;
|
2000-11-14 03:47:20 +08:00
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2001-09-11 07:57:10 +08:00
|
|
|
/* {{{ php_openssl_generate_private_key */
|
|
|
|
static EVP_PKEY * php_openssl_generate_private_key(struct php_x509_request * req TSRMLS_DC)
|
2001-04-02 07:06:15 +08:00
|
|
|
{
|
2001-09-11 07:57:10 +08:00
|
|
|
char * randfile = NULL;
|
|
|
|
int egdsocket, seeded;
|
|
|
|
EVP_PKEY * return_val = NULL;
|
|
|
|
|
2002-12-11 06:18:58 +08:00
|
|
|
if (req->priv_key_bits < MIN_KEY_LENGTH) {
|
|
|
|
php_error_docref(NULL TSRMLS_CC, E_WARNING, "private key length is too short; it needs to be at least %d bits, not %d",
|
2001-09-11 07:57:10 +08:00
|
|
|
MIN_KEY_LENGTH, req->priv_key_bits);
|
|
|
|
return NULL;
|
2001-04-02 07:06:15 +08:00
|
|
|
}
|
|
|
|
|
2001-09-11 07:57:10 +08:00
|
|
|
randfile = CONF_get_string(req->req_config, req->section_name, "RANDFILE");
|
|
|
|
php_openssl_load_rand_file(randfile, &egdsocket, &seeded);
|
|
|
|
|
2002-12-11 06:18:58 +08:00
|
|
|
if ((req->priv_key = EVP_PKEY_new()) != NULL) {
|
|
|
|
switch(req->priv_key_type) {
|
2001-09-11 07:57:10 +08:00
|
|
|
case OPENSSL_KEYTYPE_RSA:
|
|
|
|
if (EVP_PKEY_assign_RSA(req->priv_key, RSA_generate_key(req->priv_key_bits, 0x10001, NULL, NULL)))
|
|
|
|
return_val = req->priv_key;
|
|
|
|
break;
|
|
|
|
default:
|
2002-12-11 06:18:58 +08:00
|
|
|
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unsupported private key type");
|
2001-04-02 07:06:15 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-09-11 07:57:10 +08:00
|
|
|
php_openssl_write_rand_file(randfile, egdsocket, seeded);
|
|
|
|
|
2002-12-11 06:18:58 +08:00
|
|
|
if (return_val == NULL) {
|
2001-09-11 07:57:10 +08:00
|
|
|
EVP_PKEY_free(req->priv_key);
|
|
|
|
req->priv_key = NULL;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return return_val;
|
2001-04-02 07:06:15 +08:00
|
|
|
}
|
2001-05-05 07:42:11 +08:00
|
|
|
/* }}} */
|
2001-04-02 07:06:15 +08:00
|
|
|
|
2002-12-12 21:42:23 +08:00
|
|
|
/* {{{ php_openssl_is_private_key
|
|
|
|
Check whether the supplied key is a private key by checking if the secret prime factors are set */
|
|
|
|
static int php_openssl_is_private_key(EVP_PKEY* pkey TSRMLS_DC)
|
|
|
|
{
|
|
|
|
assert(pkey != NULL);
|
|
|
|
|
|
|
|
switch (pkey->type) {
|
|
|
|
#ifndef NO_RSA
|
|
|
|
case EVP_PKEY_RSA:
|
|
|
|
case EVP_PKEY_RSA2:
|
|
|
|
assert(pkey->pkey.rsa != NULL);
|
|
|
|
|
|
|
|
if (NULL == pkey->pkey.rsa->p || NULL == pkey->pkey.rsa->q)
|
|
|
|
return 0;
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
#ifndef NO_DSA
|
|
|
|
case EVP_PKEY_DSA:
|
|
|
|
case EVP_PKEY_DSA1:
|
|
|
|
case EVP_PKEY_DSA2:
|
|
|
|
case EVP_PKEY_DSA3:
|
|
|
|
case EVP_PKEY_DSA4:
|
|
|
|
assert(pkey->pkey.dsa != NULL);
|
|
|
|
|
|
|
|
if (NULL == pkey->pkey.dsa->p || NULL == pkey->pkey.dsa->q || NULL == pkey->pkey.dsa->priv_key)
|
|
|
|
return 0;
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
#ifndef NO_DH
|
|
|
|
case EVP_PKEY_DH:
|
|
|
|
assert(pkey->pkey.dh != NULL);
|
|
|
|
|
|
|
|
if (NULL == pkey->pkey.dh->p || NULL == pkey->pkey.dh->priv_key)
|
|
|
|
return 0;
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
default:
|
|
|
|
php_error_docref(NULL TSRMLS_CC, E_WARNING, "key type not supported in this PHP build!");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2001-09-11 07:57:10 +08:00
|
|
|
/* {{{ proto resource openssl_pkey_new([array configargs])
|
2001-09-11 09:03:59 +08:00
|
|
|
Generates a new private key */
|
2001-09-11 07:57:10 +08:00
|
|
|
PHP_FUNCTION(openssl_pkey_new)
|
2001-04-02 07:06:15 +08:00
|
|
|
{
|
2001-09-11 07:57:10 +08:00
|
|
|
struct php_x509_request req;
|
|
|
|
zval * args = NULL;
|
|
|
|
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|a!", &args) == FAILURE)
|
|
|
|
return;
|
2001-04-02 07:06:15 +08:00
|
|
|
|
2001-09-11 07:57:10 +08:00
|
|
|
RETVAL_FALSE;
|
|
|
|
|
|
|
|
PHP_SSL_REQ_INIT(&req);
|
|
|
|
|
|
|
|
if (PHP_SSL_REQ_PARSE(&req, args) == SUCCESS)
|
2001-04-02 07:06:15 +08:00
|
|
|
{
|
2002-12-11 06:18:58 +08:00
|
|
|
if (php_openssl_generate_private_key(&req TSRMLS_CC)) {
|
2001-09-11 07:57:10 +08:00
|
|
|
/* pass back a key resource */
|
|
|
|
RETVAL_RESOURCE(zend_list_insert(req.priv_key, le_key));
|
|
|
|
/* make sure the cleanup code doesn't zap it! */
|
|
|
|
req.priv_key = NULL;
|
|
|
|
}
|
2001-04-02 07:06:15 +08:00
|
|
|
}
|
2001-09-11 07:57:10 +08:00
|
|
|
PHP_SSL_REQ_DISPOSE(&req);
|
2001-04-02 07:06:15 +08:00
|
|
|
}
|
2001-05-05 07:42:11 +08:00
|
|
|
/* }}} */
|
2001-04-02 07:06:15 +08:00
|
|
|
|
2001-09-11 09:03:59 +08:00
|
|
|
/* {{{ proto bool openssl_pkey_export_to_file(mixed key, string outfilename [, string passphrase, array config_args)
|
|
|
|
Gets an exportable representation of a key into a file */
|
2001-09-11 08:33:25 +08:00
|
|
|
PHP_FUNCTION(openssl_pkey_export_to_file)
|
|
|
|
{
|
|
|
|
struct php_x509_request req;
|
|
|
|
zval * zpkey, * args = NULL;
|
|
|
|
char * passphrase = NULL; long passphrase_len = 0;
|
|
|
|
char * filename = NULL; long filename_len = 0;
|
|
|
|
long key_resource = -1;
|
|
|
|
EVP_PKEY * key;
|
|
|
|
BIO * bio_out = NULL;
|
|
|
|
EVP_CIPHER * cipher;
|
|
|
|
|
|
|
|
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "zs|s!a!", &zpkey, &filename, &filename_len, &passphrase, &passphrase_len, &args) == FAILURE)
|
|
|
|
return;
|
|
|
|
|
|
|
|
RETVAL_FALSE;
|
|
|
|
|
2001-09-30 21:30:18 +08:00
|
|
|
key = php_openssl_evp_from_zval(&zpkey, 0, passphrase, 0, &key_resource TSRMLS_CC);
|
2001-09-11 08:33:25 +08:00
|
|
|
|
2002-12-11 06:18:58 +08:00
|
|
|
if (key == NULL) {
|
|
|
|
php_error_docref(NULL TSRMLS_CC, E_WARNING, "cannot get key from parameter 1");
|
2001-09-11 08:33:25 +08:00
|
|
|
RETURN_FALSE;
|
|
|
|
}
|
|
|
|
|
2002-11-10 13:19:40 +08:00
|
|
|
if (php_openssl_safe_mode_chk(filename TSRMLS_CC)) {
|
|
|
|
RETURN_FALSE;
|
|
|
|
}
|
|
|
|
|
2001-09-11 08:33:25 +08:00
|
|
|
PHP_SSL_REQ_INIT(&req);
|
|
|
|
|
|
|
|
if (PHP_SSL_REQ_PARSE(&req, args) == SUCCESS)
|
|
|
|
{
|
|
|
|
bio_out = BIO_new_file(filename, "w");
|
|
|
|
|
|
|
|
if (passphrase && req.priv_key_encrypt)
|
|
|
|
cipher = EVP_des_ede3_cbc();
|
|
|
|
else
|
|
|
|
cipher = NULL;
|
|
|
|
|
2002-12-11 06:18:58 +08:00
|
|
|
if (PEM_write_bio_PrivateKey(bio_out, key, cipher, passphrase, passphrase_len, NULL, NULL)) {
|
2001-09-11 08:33:25 +08:00
|
|
|
/* Success!
|
|
|
|
* If returning the output as a string, do so now */
|
|
|
|
RETVAL_TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
PHP_SSL_REQ_DISPOSE(&req);
|
|
|
|
|
2002-12-11 06:18:58 +08:00
|
|
|
if (key_resource == -1 && key) {
|
2001-09-11 08:33:25 +08:00
|
|
|
EVP_PKEY_free(key);
|
|
|
|
}
|
|
|
|
if (bio_out)
|
|
|
|
BIO_free(bio_out);
|
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2001-09-11 09:03:59 +08:00
|
|
|
/* {{{ proto bool openssl_pkey_export(mixed key, &mixed out [, string passphrase [, array config_args]])
|
|
|
|
Gets an exportable representation of a key into a string or file */
|
2001-09-11 07:57:10 +08:00
|
|
|
PHP_FUNCTION(openssl_pkey_export)
|
2001-04-02 07:06:15 +08:00
|
|
|
{
|
2001-09-11 07:57:10 +08:00
|
|
|
struct php_x509_request req;
|
|
|
|
zval * zpkey, * args = NULL, *out;
|
|
|
|
char * passphrase = NULL; long passphrase_len = 0;
|
|
|
|
long key_resource = -1;
|
|
|
|
EVP_PKEY * key;
|
|
|
|
BIO * bio_out = NULL;
|
|
|
|
EVP_CIPHER * cipher;
|
|
|
|
|
2001-09-11 08:33:25 +08:00
|
|
|
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "zz|s!a!", &zpkey, &out, &passphrase, &passphrase_len, &args) == FAILURE)
|
2001-09-08 01:09:56 +08:00
|
|
|
return;
|
2001-04-02 07:06:15 +08:00
|
|
|
|
2001-09-11 07:57:10 +08:00
|
|
|
RETVAL_FALSE;
|
2001-04-02 07:06:15 +08:00
|
|
|
|
2001-09-30 21:30:18 +08:00
|
|
|
key = php_openssl_evp_from_zval(&zpkey, 0, passphrase, 0, &key_resource TSRMLS_CC);
|
2001-04-02 07:06:15 +08:00
|
|
|
|
2002-12-11 06:18:58 +08:00
|
|
|
if (key == NULL) {
|
|
|
|
php_error_docref(NULL TSRMLS_CC, E_WARNING, "cannot get key from parameter 1");
|
2001-09-11 07:57:10 +08:00
|
|
|
RETURN_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
PHP_SSL_REQ_INIT(&req);
|
2001-04-02 07:06:15 +08:00
|
|
|
|
2001-09-11 07:57:10 +08:00
|
|
|
if (PHP_SSL_REQ_PARSE(&req, args) == SUCCESS)
|
|
|
|
{
|
2001-09-11 08:33:25 +08:00
|
|
|
bio_out = BIO_new(BIO_s_mem());
|
2001-09-11 07:57:10 +08:00
|
|
|
|
|
|
|
if (passphrase && req.priv_key_encrypt)
|
|
|
|
cipher = EVP_des_ede3_cbc();
|
|
|
|
else
|
|
|
|
cipher = NULL;
|
|
|
|
|
2002-12-11 06:18:58 +08:00
|
|
|
if (PEM_write_bio_PrivateKey(bio_out, key, cipher, passphrase, passphrase_len, NULL, NULL)) {
|
2001-09-11 07:57:10 +08:00
|
|
|
/* Success!
|
|
|
|
* If returning the output as a string, do so now */
|
2001-09-11 08:33:25 +08:00
|
|
|
|
|
|
|
char * bio_mem_ptr;
|
|
|
|
long bio_mem_len;
|
2001-09-11 07:57:10 +08:00
|
|
|
RETVAL_TRUE;
|
|
|
|
|
2001-09-11 08:33:25 +08:00
|
|
|
bio_mem_len = BIO_get_mem_data(bio_out, &bio_mem_ptr);
|
|
|
|
ZVAL_STRINGL(out, bio_mem_ptr, bio_mem_len, 1);
|
2001-09-11 07:57:10 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
PHP_SSL_REQ_DISPOSE(&req);
|
2001-04-02 07:06:15 +08:00
|
|
|
|
2002-12-11 06:18:58 +08:00
|
|
|
if (key_resource == -1 && key) {
|
2001-09-11 07:57:10 +08:00
|
|
|
EVP_PKEY_free(key);
|
|
|
|
}
|
|
|
|
if (bio_out)
|
|
|
|
BIO_free(bio_out);
|
2001-04-02 07:06:15 +08:00
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2001-09-11 07:57:10 +08:00
|
|
|
/* {{{ proto int openssl_pkey_get_public(mixed cert)
|
2001-09-11 09:03:59 +08:00
|
|
|
Gets public key from X.509 certificate */
|
2001-09-11 07:57:10 +08:00
|
|
|
PHP_FUNCTION(openssl_pkey_get_public)
|
2000-11-14 03:47:20 +08:00
|
|
|
{
|
2001-09-08 01:09:56 +08:00
|
|
|
zval *cert;
|
2000-11-14 03:47:20 +08:00
|
|
|
EVP_PKEY *pkey;
|
|
|
|
|
2001-09-10 22:30:10 +08:00
|
|
|
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &cert) == FAILURE)
|
2001-09-08 01:09:56 +08:00
|
|
|
return;
|
2000-11-14 17:54:25 +08:00
|
|
|
|
2001-09-11 07:57:10 +08:00
|
|
|
Z_TYPE_P(return_value) = IS_RESOURCE;
|
2001-09-08 01:09:56 +08:00
|
|
|
pkey = php_openssl_evp_from_zval(&cert, 1, NULL, 1, &Z_LVAL_P(return_value) TSRMLS_CC);
|
2000-11-14 17:54:25 +08:00
|
|
|
|
2001-04-02 07:06:15 +08:00
|
|
|
if (pkey == NULL) {
|
2000-11-14 17:54:25 +08:00
|
|
|
RETURN_FALSE;
|
|
|
|
}
|
2000-11-14 03:47:20 +08:00
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2001-09-11 07:57:10 +08:00
|
|
|
/* {{{ proto void openssl_pkey_free(int key)
|
2001-09-11 09:03:59 +08:00
|
|
|
Frees a key */
|
2001-09-11 07:57:10 +08:00
|
|
|
PHP_FUNCTION(openssl_pkey_free)
|
2000-11-14 03:47:20 +08:00
|
|
|
{
|
2001-09-08 01:09:56 +08:00
|
|
|
zval *key;
|
2000-11-14 03:47:20 +08:00
|
|
|
EVP_PKEY *pkey;
|
|
|
|
|
2001-09-10 22:30:10 +08:00
|
|
|
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &key) == FAILURE)
|
2001-09-08 01:09:56 +08:00
|
|
|
return;
|
2001-04-02 07:06:15 +08:00
|
|
|
|
2001-09-08 01:09:56 +08:00
|
|
|
ZEND_FETCH_RESOURCE(pkey, EVP_PKEY *, &key, -1, "OpenSSL key", le_key);
|
|
|
|
zend_list_delete(Z_LVAL_P(key));
|
2000-11-14 03:47:20 +08:00
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2001-09-11 07:57:10 +08:00
|
|
|
/* {{{ proto int openssl_pkey_get_private(string key [, string passphrase])
|
2001-09-11 09:03:59 +08:00
|
|
|
Gets private keys */
|
2001-09-11 07:57:10 +08:00
|
|
|
PHP_FUNCTION(openssl_pkey_get_private)
|
2001-04-02 07:06:15 +08:00
|
|
|
{
|
2001-09-08 01:09:56 +08:00
|
|
|
zval *cert;
|
2001-09-11 07:57:10 +08:00
|
|
|
EVP_PKEY *pkey;
|
|
|
|
char * passphrase = "";
|
2001-09-11 08:03:58 +08:00
|
|
|
long passphrase_len;
|
2001-09-11 07:57:10 +08:00
|
|
|
|
2001-09-11 08:03:58 +08:00
|
|
|
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z|s", &cert, &passphrase, &passphrase_len) == FAILURE)
|
2001-09-11 07:57:10 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
Z_TYPE_P(return_value) = IS_RESOURCE;
|
2002-03-26 08:03:11 +08:00
|
|
|
pkey = php_openssl_evp_from_zval(&cert, 0, passphrase, 1, &Z_LVAL_P(return_value) TSRMLS_CC);
|
2001-09-11 07:57:10 +08:00
|
|
|
|
|
|
|
if (pkey == NULL) {
|
2000-11-14 17:54:25 +08:00
|
|
|
RETURN_FALSE;
|
|
|
|
}
|
2001-04-02 07:06:15 +08:00
|
|
|
}
|
|
|
|
|
2001-09-11 07:57:10 +08:00
|
|
|
/* }}} */
|
|
|
|
|
2001-09-11 08:33:25 +08:00
|
|
|
/* }}} */
|
|
|
|
|
2001-09-11 07:57:10 +08:00
|
|
|
/* {{{ PKCS7 S/MIME functions */
|
|
|
|
|
2001-09-11 09:03:59 +08:00
|
|
|
/* {{{ proto bool openssl_pkcs7_verify(string filename, long flags [, string signerscerts [, array cainfo [, string extracerts]]])
|
|
|
|
Verifys that the data block is intact, the signer is who they say they are, and returns the CERTs of the signers */
|
2001-04-02 07:06:15 +08:00
|
|
|
PHP_FUNCTION(openssl_pkcs7_verify)
|
|
|
|
{
|
2002-04-19 15:56:41 +08:00
|
|
|
X509_STORE * store = NULL;
|
2001-09-08 01:09:56 +08:00
|
|
|
zval * cainfo = NULL;
|
2001-04-03 07:14:01 +08:00
|
|
|
STACK_OF(X509) *signers= NULL;
|
|
|
|
STACK_OF(X509) *others = NULL;
|
2001-04-02 07:06:15 +08:00
|
|
|
PKCS7 * p7 = NULL;
|
|
|
|
BIO * in = NULL, * datain = NULL;
|
2001-09-08 01:09:56 +08:00
|
|
|
long flags = 0;
|
|
|
|
char * filename; long filename_len;
|
|
|
|
char * extracerts = NULL; long extracerts_len;
|
|
|
|
char * signersfilename = NULL; long signersfilename_len;
|
2001-04-03 07:14:01 +08:00
|
|
|
|
2001-09-08 01:09:56 +08:00
|
|
|
RETVAL_LONG(-1);
|
2000-11-14 03:47:20 +08:00
|
|
|
|
2001-09-10 22:30:10 +08:00
|
|
|
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sl|sas", &filename, &filename_len,
|
2001-09-08 01:09:56 +08:00
|
|
|
&flags, &signersfilename, &signersfilename_len, &cainfo,
|
|
|
|
&extracerts, &extracerts_len) == FAILURE)
|
|
|
|
return;
|
2001-04-03 07:14:01 +08:00
|
|
|
|
2002-12-11 06:18:58 +08:00
|
|
|
if (extracerts) {
|
2001-09-08 01:09:56 +08:00
|
|
|
others = load_all_certs_from_file(extracerts);
|
|
|
|
if (others == NULL)
|
2001-04-03 07:14:01 +08:00
|
|
|
goto clean_exit;
|
2001-04-02 07:06:15 +08:00
|
|
|
}
|
2000-11-14 03:47:20 +08:00
|
|
|
|
2001-09-08 01:09:56 +08:00
|
|
|
flags = flags & ~PKCS7_DETACHED;
|
2001-04-02 07:06:15 +08:00
|
|
|
|
2001-09-08 01:09:56 +08:00
|
|
|
store = setup_verify(cainfo TSRMLS_CC);
|
2001-04-02 07:06:15 +08:00
|
|
|
|
|
|
|
if (!store)
|
2001-04-03 07:14:01 +08:00
|
|
|
goto clean_exit;
|
2001-04-02 07:06:15 +08:00
|
|
|
|
2002-11-10 13:19:40 +08:00
|
|
|
if (php_openssl_safe_mode_chk(filename TSRMLS_CC)) {
|
|
|
|
goto clean_exit;
|
|
|
|
}
|
|
|
|
|
2001-09-08 01:09:56 +08:00
|
|
|
in = BIO_new_file(filename, (flags & PKCS7_BINARY) ? "rb" : "r");
|
2001-04-02 07:06:15 +08:00
|
|
|
if (in == NULL)
|
2001-04-03 07:14:01 +08:00
|
|
|
goto clean_exit;
|
2001-04-02 07:06:15 +08:00
|
|
|
p7 = SMIME_read_PKCS7(in, &datain);
|
2002-12-11 06:18:58 +08:00
|
|
|
if (p7 == NULL) {
|
2001-09-08 01:09:56 +08:00
|
|
|
#if DEBUG_SMIME
|
|
|
|
zend_printf("SMIME_read_PKCS7 failed\n");
|
|
|
|
#endif
|
2001-04-03 07:14:01 +08:00
|
|
|
goto clean_exit;
|
2001-09-08 01:09:56 +08:00
|
|
|
}
|
|
|
|
#if DEBUG_SMIME
|
|
|
|
zend_printf("Calling PKCS7 verify\n");
|
|
|
|
#endif
|
2001-04-02 07:06:15 +08:00
|
|
|
|
2002-12-11 06:18:58 +08:00
|
|
|
if (PKCS7_verify(p7, others, store, datain, NULL, flags)) {
|
2001-04-02 07:06:15 +08:00
|
|
|
|
|
|
|
RETVAL_TRUE;
|
|
|
|
|
2002-11-10 13:19:40 +08:00
|
|
|
if (signersfilename) {
|
|
|
|
BIO *certout;
|
|
|
|
|
|
|
|
if (php_openssl_safe_mode_chk(filename TSRMLS_CC)) {
|
|
|
|
goto clean_exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
certout = BIO_new_file(signersfilename, "w");
|
2002-12-11 06:18:58 +08:00
|
|
|
if (certout) {
|
2001-04-02 07:06:15 +08:00
|
|
|
int i;
|
|
|
|
signers = PKCS7_get0_signers(p7, NULL, flags);
|
|
|
|
|
|
|
|
for(i = 0; i < sk_X509_num(signers); i++)
|
|
|
|
PEM_write_bio_X509(certout, sk_X509_value(signers, i));
|
|
|
|
|
|
|
|
BIO_free(certout);
|
|
|
|
sk_X509_free(signers);
|
|
|
|
}
|
2001-04-03 07:14:01 +08:00
|
|
|
else {
|
2002-12-11 06:18:58 +08:00
|
|
|
php_error_docref(NULL TSRMLS_CC, E_WARNING, "signature OK, but cannot open %s for writing",
|
|
|
|
signersfilename);
|
2001-04-03 07:14:01 +08:00
|
|
|
RETVAL_LONG(-1);
|
|
|
|
}
|
2001-04-02 07:06:15 +08:00
|
|
|
}
|
2001-04-03 07:14:01 +08:00
|
|
|
goto clean_exit;
|
2000-11-14 03:47:20 +08:00
|
|
|
}
|
2001-04-03 07:14:01 +08:00
|
|
|
else
|
|
|
|
RETVAL_FALSE;
|
|
|
|
clean_exit:
|
2001-04-02 07:06:15 +08:00
|
|
|
X509_STORE_free(store);
|
|
|
|
BIO_free(datain);
|
|
|
|
BIO_free(in);
|
|
|
|
PKCS7_free(p7);
|
2001-04-03 07:14:01 +08:00
|
|
|
sk_X509_free(others);
|
2000-11-14 03:47:20 +08:00
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2001-09-11 09:03:59 +08:00
|
|
|
/* {{{ proto bool openssl_pkcs7_encrypt(string infile, string outfile, mixed recipcerts, array headers [, long flags])
|
|
|
|
Encrypts the message in the file named infile with the certificates in recipcerts and output the result to the file named outfile */
|
2001-04-02 07:06:15 +08:00
|
|
|
PHP_FUNCTION(openssl_pkcs7_encrypt)
|
2000-11-14 03:47:20 +08:00
|
|
|
{
|
2001-09-08 01:09:56 +08:00
|
|
|
zval * zrecipcerts, * zheaders = NULL;
|
2001-04-02 07:06:15 +08:00
|
|
|
STACK_OF(X509) * recipcerts = NULL;
|
|
|
|
BIO * infile = NULL, * outfile = NULL;
|
2001-09-08 01:09:56 +08:00
|
|
|
long flags = 0;
|
2001-04-02 07:06:15 +08:00
|
|
|
PKCS7 * p7 = NULL;
|
|
|
|
HashPosition hpos;
|
|
|
|
zval ** zcertval;
|
|
|
|
X509 * cert;
|
|
|
|
EVP_CIPHER *cipher = NULL;
|
2001-08-21 20:57:53 +08:00
|
|
|
uint strindexlen;
|
|
|
|
ulong intindex;
|
2001-04-02 07:06:15 +08:00
|
|
|
char * strindex;
|
2001-09-08 01:09:56 +08:00
|
|
|
char * infilename = NULL; long infilename_len;
|
|
|
|
char * outfilename = NULL; long outfilename_len;
|
|
|
|
|
2001-04-02 07:06:15 +08:00
|
|
|
RETVAL_FALSE;
|
|
|
|
|
2001-09-10 22:30:10 +08:00
|
|
|
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ssza!|l", &infilename, &infilename_len,
|
2001-09-08 01:09:56 +08:00
|
|
|
&outfilename, &outfilename_len, &zrecipcerts, &zheaders, &flags) == FAILURE)
|
|
|
|
return;
|
2001-04-02 07:06:15 +08:00
|
|
|
|
2002-11-10 13:19:40 +08:00
|
|
|
if (php_openssl_safe_mode_chk(infilename TSRMLS_CC) || php_openssl_safe_mode_chk(outfilename TSRMLS_CC)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2001-09-08 01:09:56 +08:00
|
|
|
infile = BIO_new_file(infilename, "r");
|
2001-04-02 07:06:15 +08:00
|
|
|
if (infile == NULL)
|
|
|
|
goto clean_exit;
|
|
|
|
|
2001-09-08 01:09:56 +08:00
|
|
|
outfile = BIO_new_file(outfilename, "w");
|
2001-04-02 07:06:15 +08:00
|
|
|
if (outfile == NULL)
|
|
|
|
goto clean_exit;
|
|
|
|
|
|
|
|
recipcerts = sk_X509_new_null();
|
|
|
|
|
|
|
|
/* get certs */
|
2002-12-11 06:18:58 +08:00
|
|
|
if (Z_TYPE_P(zrecipcerts) == IS_ARRAY) {
|
2001-09-08 01:09:56 +08:00
|
|
|
zend_hash_internal_pointer_reset_ex(HASH_OF(zrecipcerts), &hpos);
|
2002-12-11 06:18:58 +08:00
|
|
|
while(zend_hash_get_current_data_ex(HASH_OF(zrecipcerts), (void**)&zcertval, &hpos) == SUCCESS) {
|
2001-04-03 07:14:01 +08:00
|
|
|
long certresource;
|
|
|
|
|
2001-08-01 07:47:35 +08:00
|
|
|
cert = php_openssl_x509_from_zval(zcertval, 0, &certresource TSRMLS_CC);
|
2001-04-03 07:14:01 +08:00
|
|
|
if (cert == NULL)
|
|
|
|
goto clean_exit;
|
|
|
|
|
2002-12-11 06:18:58 +08:00
|
|
|
if (certresource != -1) {
|
2001-04-03 07:14:01 +08:00
|
|
|
/* we shouldn't free this particular cert, as it is a resource.
|
|
|
|
make a copy and push that on the stack instead */
|
|
|
|
cert = X509_dup(cert);
|
|
|
|
if (cert == NULL)
|
|
|
|
goto clean_exit;
|
|
|
|
}
|
|
|
|
sk_X509_push(recipcerts, cert);
|
|
|
|
|
2001-09-08 01:09:56 +08:00
|
|
|
zend_hash_move_forward_ex(HASH_OF(zrecipcerts), &hpos);
|
2001-04-03 07:14:01 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* a single certificate */
|
2001-04-02 07:06:15 +08:00
|
|
|
long certresource;
|
|
|
|
|
2001-09-08 01:09:56 +08:00
|
|
|
cert = php_openssl_x509_from_zval(&zrecipcerts, 0, &certresource TSRMLS_CC);
|
2001-04-02 07:06:15 +08:00
|
|
|
if (cert == NULL)
|
|
|
|
goto clean_exit;
|
|
|
|
|
2002-12-11 06:18:58 +08:00
|
|
|
if (certresource != -1) {
|
2001-04-02 07:06:15 +08:00
|
|
|
/* we shouldn't free this particular cert, as it is a resource.
|
2001-04-03 07:14:01 +08:00
|
|
|
make a copy and push that on the stack instead */
|
2001-04-02 07:06:15 +08:00
|
|
|
cert = X509_dup(cert);
|
|
|
|
if (cert == NULL)
|
|
|
|
goto clean_exit;
|
|
|
|
}
|
|
|
|
sk_X509_push(recipcerts, cert);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* TODO: allow user to choose a different cipher */
|
|
|
|
cipher = EVP_rc2_40_cbc();
|
|
|
|
if (cipher == NULL)
|
|
|
|
goto clean_exit;
|
|
|
|
|
|
|
|
p7 = PKCS7_encrypt(recipcerts, infile, cipher, flags);
|
|
|
|
|
|
|
|
if (p7 == NULL)
|
|
|
|
goto clean_exit;
|
|
|
|
|
|
|
|
/* tack on extra headers */
|
2002-12-11 06:18:58 +08:00
|
|
|
if (zheaders) {
|
2001-09-08 01:09:56 +08:00
|
|
|
zend_hash_internal_pointer_reset_ex(HASH_OF(zheaders), &hpos);
|
2002-12-11 06:18:58 +08:00
|
|
|
while(zend_hash_get_current_data_ex(HASH_OF(zheaders), (void**)&zcertval, &hpos) == SUCCESS) {
|
2001-09-08 01:09:56 +08:00
|
|
|
zend_hash_get_current_key_ex(HASH_OF(zheaders), &strindex, &strindexlen, &intindex, 0, &hpos);
|
2001-04-02 07:06:15 +08:00
|
|
|
|
2001-09-08 01:09:56 +08:00
|
|
|
convert_to_string_ex(zcertval);
|
2001-04-02 07:06:15 +08:00
|
|
|
|
2001-09-08 01:09:56 +08:00
|
|
|
if (strindex)
|
|
|
|
BIO_printf(outfile, "%s: %s\n", strindex, Z_STRVAL_PP(zcertval));
|
|
|
|
else
|
|
|
|
BIO_printf(outfile, "%s\n", Z_STRVAL_PP(zcertval));
|
2001-04-02 07:06:15 +08:00
|
|
|
|
2001-09-08 01:09:56 +08:00
|
|
|
zend_hash_move_forward_ex(HASH_OF(zheaders), &hpos);
|
|
|
|
}
|
2001-04-02 07:06:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
BIO_reset(infile);
|
|
|
|
|
|
|
|
/* write the encrypted data */
|
|
|
|
SMIME_write_PKCS7(outfile, p7, infile, flags);
|
|
|
|
|
|
|
|
RETVAL_TRUE;
|
|
|
|
|
|
|
|
|
|
|
|
clean_exit:
|
|
|
|
PKCS7_free(p7);
|
|
|
|
BIO_free(infile);
|
|
|
|
BIO_free(outfile);
|
|
|
|
if (recipcerts)
|
|
|
|
sk_X509_pop_free(recipcerts, X509_free);
|
|
|
|
|
2000-11-14 03:47:20 +08:00
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2001-09-11 09:03:59 +08:00
|
|
|
/* {{{ proto bool openssl_pkcs7_sign(string infile, string outfile, mixed signcert, mixed signkey, array headers [, long flags [, string extracertsfilename]])
|
|
|
|
Signs the MIME message in the file named infile with signcert/signkey and output the result to file name outfile. headers lists plain text headers to exclude from the signed portion of the message, and should include to, from and subject as a minimum */
|
2001-04-02 07:06:15 +08:00
|
|
|
|
|
|
|
PHP_FUNCTION(openssl_pkcs7_sign)
|
|
|
|
{
|
2001-09-08 01:09:56 +08:00
|
|
|
zval * zcert, * zprivkey, * zheaders;
|
2001-04-02 07:06:15 +08:00
|
|
|
zval ** hval;
|
|
|
|
X509 * cert = NULL;
|
|
|
|
EVP_PKEY * privkey = NULL;
|
2001-09-08 01:09:56 +08:00
|
|
|
long flags = PKCS7_DETACHED;
|
2001-04-02 07:06:15 +08:00
|
|
|
PKCS7 * p7 = NULL;
|
|
|
|
BIO * infile = NULL, * outfile = NULL;
|
|
|
|
STACK_OF(X509) *others = NULL;
|
|
|
|
long certresource = -1, keyresource = -1;
|
2001-08-21 20:57:53 +08:00
|
|
|
ulong intindex;
|
|
|
|
uint strindexlen;
|
2001-04-02 07:06:15 +08:00
|
|
|
HashPosition hpos;
|
|
|
|
char * strindex;
|
2001-09-08 01:09:56 +08:00
|
|
|
char * infilename; long infilename_len;
|
|
|
|
char * outfilename; long outfilename_len;
|
|
|
|
char * extracertsfilename = NULL; long extracertsfilename_len;
|
|
|
|
|
2001-09-10 22:30:10 +08:00
|
|
|
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sszza!|ls",
|
2002-01-31 17:57:28 +08:00
|
|
|
&infilename, &infilename_len, &outfilename, &outfilename_len,
|
2001-09-08 01:09:56 +08:00
|
|
|
&zcert, &zprivkey, &zheaders, &flags, &extracertsfilename,
|
|
|
|
&extracertsfilename_len) == FAILURE)
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
2001-04-02 07:06:15 +08:00
|
|
|
RETVAL_FALSE;
|
|
|
|
|
2002-12-11 06:18:58 +08:00
|
|
|
if (extracertsfilename) {
|
2001-09-08 01:09:56 +08:00
|
|
|
others = load_all_certs_from_file(extracertsfilename);
|
2001-04-02 07:06:15 +08:00
|
|
|
if (others == NULL)
|
|
|
|
goto clean_exit;
|
|
|
|
}
|
|
|
|
|
2001-09-08 01:09:56 +08:00
|
|
|
privkey = php_openssl_evp_from_zval(&zprivkey, 0, "", 0, &keyresource TSRMLS_CC);
|
2002-12-11 06:18:58 +08:00
|
|
|
if (privkey == NULL) {
|
|
|
|
php_error_docref(NULL TSRMLS_CC, E_WARNING, "error getting private key");
|
2001-04-02 07:06:15 +08:00
|
|
|
goto clean_exit;
|
2001-04-02 09:31:08 +08:00
|
|
|
}
|
2001-04-02 07:06:15 +08:00
|
|
|
|
2001-09-11 07:57:10 +08:00
|
|
|
cert = php_openssl_x509_from_zval(&zcert, 0, &certresource TSRMLS_CC);
|
2002-12-11 06:18:58 +08:00
|
|
|
if (cert == NULL) {
|
|
|
|
php_error_docref(NULL TSRMLS_CC, E_WARNING, "error getting cert");
|
2001-09-11 07:57:10 +08:00
|
|
|
goto clean_exit;
|
|
|
|
}
|
|
|
|
|
2002-11-10 13:19:40 +08:00
|
|
|
if (php_openssl_safe_mode_chk(infilename TSRMLS_CC) || php_openssl_safe_mode_chk(outfilename TSRMLS_CC)) {
|
|
|
|
goto clean_exit;
|
|
|
|
}
|
|
|
|
|
2001-09-11 07:57:10 +08:00
|
|
|
infile = BIO_new_file(infilename, "r");
|
2002-12-11 06:18:58 +08:00
|
|
|
if (infile == NULL) {
|
|
|
|
php_error_docref(NULL TSRMLS_CC, E_WARNING, "error opening input file %s!", infilename);
|
2001-09-11 07:57:10 +08:00
|
|
|
goto clean_exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
outfile = BIO_new_file(outfilename, "w");
|
2002-12-11 06:18:58 +08:00
|
|
|
if (outfile == NULL) {
|
|
|
|
php_error_docref(NULL TSRMLS_CC, E_WARNING, "error opening output file %s!", outfilename);
|
2001-09-11 07:57:10 +08:00
|
|
|
goto clean_exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
p7 = PKCS7_sign(cert, privkey, others, infile, flags);
|
2002-12-11 06:18:58 +08:00
|
|
|
if (p7 == NULL) {
|
|
|
|
php_error_docref(NULL TSRMLS_CC, E_WARNING, "error creating PKCS7 structure!");
|
2001-09-11 07:57:10 +08:00
|
|
|
goto clean_exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
BIO_reset(infile);
|
|
|
|
|
|
|
|
/* tack on extra headers */
|
2002-12-11 06:18:58 +08:00
|
|
|
if (zheaders) {
|
2001-09-11 07:57:10 +08:00
|
|
|
zend_hash_internal_pointer_reset_ex(HASH_OF(zheaders), &hpos);
|
2002-12-11 06:18:58 +08:00
|
|
|
while(zend_hash_get_current_data_ex(HASH_OF(zheaders), (void**)&hval, &hpos) == SUCCESS) {
|
2001-09-11 07:57:10 +08:00
|
|
|
zend_hash_get_current_key_ex(HASH_OF(zheaders), &strindex, &strindexlen, &intindex, 0, &hpos);
|
|
|
|
|
|
|
|
convert_to_string_ex(hval);
|
|
|
|
|
|
|
|
if (strindex)
|
|
|
|
BIO_printf(outfile, "%s: %s\n", strindex, Z_STRVAL_PP(hval));
|
|
|
|
else
|
|
|
|
BIO_printf(outfile, "%s\n", Z_STRVAL_PP(hval));
|
|
|
|
|
|
|
|
zend_hash_move_forward_ex(HASH_OF(zheaders), &hpos);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* write the signed data */
|
|
|
|
SMIME_write_PKCS7(outfile, p7, infile, flags);
|
|
|
|
|
|
|
|
RETVAL_TRUE;
|
|
|
|
|
|
|
|
clean_exit:
|
|
|
|
PKCS7_free(p7);
|
|
|
|
BIO_free(infile);
|
|
|
|
BIO_free(outfile);
|
|
|
|
if (others)
|
|
|
|
sk_X509_pop_free(others, X509_free);
|
|
|
|
if (privkey && keyresource == -1)
|
|
|
|
EVP_PKEY_free(privkey);
|
|
|
|
if (cert && certresource == -1)
|
|
|
|
X509_free(cert);
|
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2001-09-11 09:03:59 +08:00
|
|
|
/* {{{ proto bool openssl_pkcs7_decrypt(string infilename, string outfilename, mixed recipcert [, mixed recipkey])
|
|
|
|
Decrypts the S/MIME message in the file name infilename and output the results to the file name outfilename. recipcert is a CERT for one of the recipients. recipkey specifies the private key matching recipcert, if recipcert does not include the key */
|
2001-09-11 07:57:10 +08:00
|
|
|
|
|
|
|
PHP_FUNCTION(openssl_pkcs7_decrypt)
|
|
|
|
{
|
|
|
|
zval * recipcert, * recipkey = NULL;
|
|
|
|
X509 * cert = NULL;
|
|
|
|
EVP_PKEY * key = NULL;
|
|
|
|
long certresval, keyresval;
|
|
|
|
BIO * in = NULL, * out = NULL, * datain = NULL;
|
|
|
|
PKCS7 * p7 = NULL;
|
|
|
|
char * infilename; long infilename_len;
|
|
|
|
char * outfilename; long outfilename_len;
|
|
|
|
|
|
|
|
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ssz|z", &infilename, &infilename_len,
|
|
|
|
&outfilename, &outfilename_len, &recipcert, &recipkey) == FAILURE)
|
|
|
|
return;
|
|
|
|
|
|
|
|
RETVAL_FALSE;
|
|
|
|
|
|
|
|
cert = php_openssl_x509_from_zval(&recipcert, 0, &certresval TSRMLS_CC);
|
2002-12-11 06:18:58 +08:00
|
|
|
if (cert == NULL) {
|
|
|
|
php_error_docref(NULL TSRMLS_CC, E_WARNING, "unable to coerce parameter 3 to x509 cert");
|
2001-09-11 07:57:10 +08:00
|
|
|
goto clean_exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
key = php_openssl_evp_from_zval(recipkey ? &recipkey : &recipcert, 0, "", 0, &keyresval TSRMLS_CC);
|
2002-12-11 06:18:58 +08:00
|
|
|
if (key == NULL) {
|
|
|
|
php_error_docref(NULL TSRMLS_CC, E_WARNING, "unable to get private key");
|
2001-09-11 07:57:10 +08:00
|
|
|
goto clean_exit;
|
|
|
|
}
|
2002-11-10 13:19:40 +08:00
|
|
|
|
|
|
|
if (php_openssl_safe_mode_chk(infilename TSRMLS_CC) || php_openssl_safe_mode_chk(outfilename TSRMLS_CC)) {
|
|
|
|
goto clean_exit;
|
|
|
|
}
|
2001-09-11 07:57:10 +08:00
|
|
|
|
|
|
|
in = BIO_new_file(infilename, "r");
|
2002-12-11 06:18:58 +08:00
|
|
|
if (in == NULL) {
|
2001-09-11 07:57:10 +08:00
|
|
|
goto clean_exit;
|
|
|
|
}
|
|
|
|
out = BIO_new_file(outfilename, "w");
|
2002-12-11 06:18:58 +08:00
|
|
|
if (out == NULL) {
|
2001-09-11 07:57:10 +08:00
|
|
|
goto clean_exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
p7 = SMIME_read_PKCS7(in, &datain);
|
|
|
|
|
|
|
|
if (p7 == NULL)
|
|
|
|
goto clean_exit;
|
|
|
|
|
|
|
|
if (PKCS7_decrypt(p7, key, cert, out, PKCS7_DETACHED))
|
|
|
|
RETVAL_TRUE;
|
|
|
|
|
|
|
|
clean_exit:
|
|
|
|
PKCS7_free(p7);
|
|
|
|
BIO_free(datain);
|
|
|
|
BIO_free(in);
|
|
|
|
BIO_free(out);
|
|
|
|
if (cert && certresval == -1)
|
|
|
|
X509_free(cert);
|
|
|
|
if (key && keyresval == -1)
|
|
|
|
EVP_PKEY_free(key);
|
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
/* {{{ proto bool openssl_private_encrypt(string data, string crypted, mixed key [, int padding])
|
2001-09-11 09:03:59 +08:00
|
|
|
Encrypts data with private key */
|
2001-09-11 07:57:10 +08:00
|
|
|
PHP_FUNCTION(openssl_private_encrypt)
|
|
|
|
{
|
|
|
|
zval *key, *crypted;
|
|
|
|
EVP_PKEY *pkey;
|
|
|
|
int cryptedlen;
|
|
|
|
unsigned char *cryptedbuf = NULL;
|
|
|
|
int successful = 0;
|
|
|
|
long keyresource = -1;
|
|
|
|
char * data;
|
|
|
|
long data_len, padding = RSA_PKCS1_PADDING;
|
|
|
|
|
|
|
|
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "szz|l", &data, &data_len, &crypted, &key, &padding) == FAILURE)
|
|
|
|
return;
|
|
|
|
|
|
|
|
RETVAL_FALSE;
|
|
|
|
|
|
|
|
pkey = php_openssl_evp_from_zval(&key, 0, "", 0, &keyresource TSRMLS_CC);
|
|
|
|
|
2002-12-11 06:18:58 +08:00
|
|
|
if (pkey == NULL) {
|
|
|
|
php_error_docref(NULL TSRMLS_CC, E_WARNING, "key param is not a valid private key");
|
2001-09-11 07:57:10 +08:00
|
|
|
RETURN_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
cryptedlen = EVP_PKEY_size(pkey);
|
|
|
|
cryptedbuf = emalloc(cryptedlen + 1);
|
|
|
|
|
2001-09-26 05:58:48 +08:00
|
|
|
switch (Z_TYPE_P(pkey)) {
|
2001-09-11 07:57:10 +08:00
|
|
|
case EVP_PKEY_RSA:
|
|
|
|
case EVP_PKEY_RSA2:
|
|
|
|
successful = (RSA_private_encrypt(data_len,
|
|
|
|
data,
|
|
|
|
cryptedbuf,
|
|
|
|
pkey->pkey.rsa,
|
|
|
|
padding) == cryptedlen);
|
|
|
|
break;
|
|
|
|
default:
|
2002-12-11 06:18:58 +08:00
|
|
|
php_error_docref(NULL TSRMLS_CC, E_WARNING, "key type not supported in this PHP build!");
|
2001-09-11 07:57:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (successful) {
|
|
|
|
zval_dtor(crypted);
|
|
|
|
cryptedbuf[cryptedlen] = '\0';
|
|
|
|
ZVAL_STRINGL(crypted, cryptedbuf, cryptedlen, 0);
|
|
|
|
cryptedbuf = NULL;
|
|
|
|
RETVAL_TRUE;
|
|
|
|
}
|
|
|
|
if (cryptedbuf)
|
|
|
|
efree(cryptedbuf);
|
|
|
|
if (keyresource == -1)
|
|
|
|
EVP_PKEY_free(pkey);
|
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2002-05-17 20:42:40 +08:00
|
|
|
/* {{{ proto bool openssl_private_decrypt(string data, string decrypted, mixed key [, int padding])
|
2001-09-11 09:03:59 +08:00
|
|
|
Decrypts data with private key */
|
2001-09-11 07:57:10 +08:00
|
|
|
PHP_FUNCTION(openssl_private_decrypt)
|
|
|
|
{
|
|
|
|
zval *key, *crypted;
|
|
|
|
EVP_PKEY *pkey;
|
|
|
|
int cryptedlen;
|
2002-04-19 15:56:41 +08:00
|
|
|
unsigned char *cryptedbuf = NULL;
|
2001-09-11 07:57:10 +08:00
|
|
|
unsigned char *crypttemp;
|
|
|
|
int successful = 0;
|
|
|
|
long padding = RSA_PKCS1_PADDING;
|
|
|
|
long keyresource = -1;
|
|
|
|
char * data;
|
|
|
|
long data_len;
|
|
|
|
|
|
|
|
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "szz|l", &data, &data_len, &crypted, &key, &padding) == FAILURE)
|
|
|
|
return;
|
|
|
|
|
|
|
|
RETVAL_FALSE;
|
|
|
|
|
|
|
|
pkey = php_openssl_evp_from_zval(&key, 0, "", 0, &keyresource TSRMLS_CC);
|
2002-12-11 06:18:58 +08:00
|
|
|
if (pkey == NULL) {
|
|
|
|
php_error_docref(NULL TSRMLS_CC, E_WARNING, "key parameter is not a valid private key");
|
2001-09-11 07:57:10 +08:00
|
|
|
RETURN_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
cryptedlen = EVP_PKEY_size(pkey);
|
|
|
|
crypttemp = emalloc(cryptedlen + 1);
|
|
|
|
|
2001-09-26 05:58:48 +08:00
|
|
|
switch (Z_TYPE_P(pkey)) {
|
2001-09-11 07:57:10 +08:00
|
|
|
case EVP_PKEY_RSA:
|
|
|
|
case EVP_PKEY_RSA2:
|
|
|
|
cryptedlen = RSA_private_decrypt(data_len,
|
|
|
|
data,
|
|
|
|
crypttemp,
|
|
|
|
pkey->pkey.rsa,
|
|
|
|
padding);
|
|
|
|
if (cryptedlen != -1) {
|
|
|
|
cryptedbuf = emalloc(cryptedlen + 1);
|
|
|
|
memcpy(cryptedbuf, crypttemp, cryptedlen);
|
|
|
|
successful = 1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
2002-12-11 06:18:58 +08:00
|
|
|
php_error_docref(NULL TSRMLS_CC, E_WARNING, "key type not supported in this PHP build!");
|
2001-04-02 09:31:08 +08:00
|
|
|
}
|
2001-04-02 07:06:15 +08:00
|
|
|
|
2001-09-11 07:57:10 +08:00
|
|
|
efree(crypttemp);
|
2001-04-02 07:06:15 +08:00
|
|
|
|
2001-09-11 07:57:10 +08:00
|
|
|
if (successful) {
|
|
|
|
zval_dtor(crypted);
|
|
|
|
cryptedbuf[cryptedlen] = '\0';
|
|
|
|
ZVAL_STRINGL(crypted, cryptedbuf, cryptedlen, 0);
|
|
|
|
cryptedbuf = NULL;
|
|
|
|
RETVAL_TRUE;
|
2001-04-02 09:31:08 +08:00
|
|
|
}
|
2001-04-02 07:06:15 +08:00
|
|
|
|
2001-09-11 07:57:10 +08:00
|
|
|
if (keyresource == -1)
|
|
|
|
EVP_PKEY_free(pkey);
|
|
|
|
if (cryptedbuf)
|
|
|
|
efree(cryptedbuf);
|
|
|
|
}
|
|
|
|
/* }}} */
|
2001-04-02 07:06:15 +08:00
|
|
|
|
2001-09-11 07:57:10 +08:00
|
|
|
/* {{{ proto bool openssl_public_encrypt(string data, string crypted, mixed key [, int padding])
|
2001-09-11 09:03:59 +08:00
|
|
|
Encrypts data with public key */
|
2001-09-11 07:57:10 +08:00
|
|
|
PHP_FUNCTION(openssl_public_encrypt)
|
|
|
|
{
|
|
|
|
zval *key, *crypted;
|
|
|
|
EVP_PKEY *pkey;
|
|
|
|
int cryptedlen;
|
|
|
|
unsigned char *cryptedbuf;
|
|
|
|
int successful = 0;
|
|
|
|
long keyresource = -1;
|
|
|
|
long padding = RSA_PKCS1_PADDING;
|
|
|
|
char * data;
|
|
|
|
long data_len;
|
|
|
|
|
|
|
|
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "szz|l", &data, &data_len, &crypted, &key, &padding) == FAILURE)
|
|
|
|
return;
|
2001-04-02 07:06:15 +08:00
|
|
|
|
2001-09-11 07:57:10 +08:00
|
|
|
RETVAL_FALSE;
|
|
|
|
|
|
|
|
pkey = php_openssl_evp_from_zval(&key, 1, NULL, 0, &keyresource TSRMLS_CC);
|
2002-12-11 06:18:58 +08:00
|
|
|
if (pkey == NULL) {
|
|
|
|
php_error_docref(NULL TSRMLS_CC, E_WARNING, "key parameter is not a valid public key");
|
2001-09-11 07:57:10 +08:00
|
|
|
RETURN_FALSE;
|
|
|
|
}
|
2001-04-02 07:06:15 +08:00
|
|
|
|
2001-09-11 07:57:10 +08:00
|
|
|
cryptedlen = EVP_PKEY_size(pkey);
|
|
|
|
cryptedbuf = emalloc(cryptedlen + 1);
|
2001-04-02 07:06:15 +08:00
|
|
|
|
2001-09-26 05:58:48 +08:00
|
|
|
switch (Z_TYPE_P(pkey)) {
|
2001-09-11 07:57:10 +08:00
|
|
|
case EVP_PKEY_RSA:
|
|
|
|
case EVP_PKEY_RSA2:
|
|
|
|
successful = (RSA_public_encrypt(data_len,
|
|
|
|
data,
|
|
|
|
cryptedbuf,
|
|
|
|
pkey->pkey.rsa,
|
|
|
|
padding) == cryptedlen);
|
|
|
|
break;
|
|
|
|
default:
|
2002-12-11 06:18:58 +08:00
|
|
|
php_error_docref(NULL TSRMLS_CC, E_WARNING, "key type not supported in this PHP build!");
|
2001-04-02 07:06:15 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2001-09-11 07:57:10 +08:00
|
|
|
if (successful) {
|
|
|
|
zval_dtor(crypted);
|
|
|
|
cryptedbuf[cryptedlen] = '\0';
|
|
|
|
ZVAL_STRINGL(crypted, cryptedbuf, cryptedlen, 0);
|
|
|
|
cryptedbuf = NULL;
|
|
|
|
RETVAL_TRUE;
|
|
|
|
}
|
|
|
|
if (keyresource == -1)
|
|
|
|
EVP_PKEY_free(pkey);
|
|
|
|
if (cryptedbuf)
|
|
|
|
efree(cryptedbuf);
|
2001-04-02 07:06:15 +08:00
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2001-09-11 07:57:10 +08:00
|
|
|
/* {{{ proto bool openssl_public_decrypt(string data, string crypted, resource key [, int padding])
|
2001-09-11 09:03:59 +08:00
|
|
|
Decrypts data with public key */
|
2001-09-11 07:57:10 +08:00
|
|
|
PHP_FUNCTION(openssl_public_decrypt)
|
2001-04-02 07:06:15 +08:00
|
|
|
{
|
2001-09-11 07:57:10 +08:00
|
|
|
zval *key, *crypted;
|
|
|
|
EVP_PKEY *pkey;
|
|
|
|
int cryptedlen;
|
2002-04-19 15:56:41 +08:00
|
|
|
unsigned char *cryptedbuf = NULL;
|
2001-09-11 07:57:10 +08:00
|
|
|
unsigned char *crypttemp;
|
|
|
|
int successful = 0;
|
|
|
|
long keyresource = -1;
|
|
|
|
long padding = RSA_PKCS1_PADDING;
|
|
|
|
char * data;
|
|
|
|
long data_len;
|
2001-04-02 07:06:15 +08:00
|
|
|
|
2001-09-11 07:57:10 +08:00
|
|
|
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "szz|l", &data, &data_len, &crypted, &key, &padding) == FAILURE)
|
2001-09-08 01:09:56 +08:00
|
|
|
return;
|
2001-04-02 07:06:15 +08:00
|
|
|
|
|
|
|
RETVAL_FALSE;
|
2001-09-11 07:57:10 +08:00
|
|
|
|
|
|
|
pkey = php_openssl_evp_from_zval(&key, 1, NULL, 0, &keyresource TSRMLS_CC);
|
2002-12-11 06:18:58 +08:00
|
|
|
if (pkey == NULL) {
|
|
|
|
php_error_docref(NULL TSRMLS_CC, E_WARNING, "key parameter is not a valid public key");
|
2001-09-11 07:57:10 +08:00
|
|
|
RETURN_FALSE;
|
2001-04-02 07:06:15 +08:00
|
|
|
}
|
|
|
|
|
2001-09-11 07:57:10 +08:00
|
|
|
cryptedlen = EVP_PKEY_size(pkey);
|
|
|
|
crypttemp = emalloc(cryptedlen + 1);
|
2001-04-02 07:06:15 +08:00
|
|
|
|
2001-09-26 05:58:48 +08:00
|
|
|
switch (Z_TYPE_P(pkey)) {
|
2001-09-11 07:57:10 +08:00
|
|
|
case EVP_PKEY_RSA:
|
|
|
|
case EVP_PKEY_RSA2:
|
|
|
|
cryptedlen = RSA_public_decrypt(data_len,
|
|
|
|
data,
|
|
|
|
crypttemp,
|
|
|
|
pkey->pkey.rsa,
|
|
|
|
padding);
|
|
|
|
if (cryptedlen != -1) {
|
|
|
|
cryptedbuf = emalloc(cryptedlen + 1);
|
|
|
|
memcpy(cryptedbuf, crypttemp, cryptedlen);
|
|
|
|
successful = 1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2002-12-11 06:18:58 +08:00
|
|
|
php_error_docref(NULL TSRMLS_CC, E_WARNING, "key type not supported in this PHP build!");
|
2001-09-11 07:57:10 +08:00
|
|
|
|
2001-04-02 07:06:15 +08:00
|
|
|
}
|
2001-09-11 07:57:10 +08:00
|
|
|
|
|
|
|
efree(crypttemp);
|
|
|
|
|
|
|
|
if (successful) {
|
|
|
|
zval_dtor(crypted);
|
|
|
|
cryptedbuf[cryptedlen] = '\0';
|
|
|
|
ZVAL_STRINGL(crypted, cryptedbuf, cryptedlen, 0);
|
|
|
|
cryptedbuf = NULL;
|
|
|
|
RETVAL_TRUE;
|
2001-04-02 07:06:15 +08:00
|
|
|
}
|
|
|
|
|
2001-09-11 07:57:10 +08:00
|
|
|
if (cryptedbuf)
|
|
|
|
efree(cryptedbuf);
|
|
|
|
if (keyresource == -1)
|
|
|
|
EVP_PKEY_free(pkey);
|
|
|
|
}
|
|
|
|
/* }}} */
|
2001-04-02 07:06:15 +08:00
|
|
|
|
2001-09-11 09:03:59 +08:00
|
|
|
/* {{{ proto mixed openssl_error_string(void)
|
|
|
|
Returns a description of the last error, and alters the index of the error messages. Returns false when the are no more messages */
|
2001-09-11 07:57:10 +08:00
|
|
|
PHP_FUNCTION(openssl_error_string)
|
|
|
|
{
|
|
|
|
char buf[512];
|
|
|
|
unsigned long val;
|
2001-04-02 07:06:15 +08:00
|
|
|
|
2002-12-11 06:18:58 +08:00
|
|
|
if (ZEND_NUM_ARGS() != 0) {
|
2001-09-11 07:57:10 +08:00
|
|
|
WRONG_PARAM_COUNT;
|
|
|
|
}
|
2001-04-02 07:06:15 +08:00
|
|
|
|
2001-09-11 07:57:10 +08:00
|
|
|
val = ERR_get_error();
|
|
|
|
if (val)
|
|
|
|
{
|
|
|
|
RETURN_STRING(ERR_error_string(val, buf), 1);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
RETURN_FALSE;
|
|
|
|
}
|
2001-04-02 07:06:15 +08:00
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
/* {{{ proto bool openssl_sign(string data, &string signature, mixed key)
|
2001-09-11 09:03:59 +08:00
|
|
|
Signs data */
|
2000-11-14 03:47:20 +08:00
|
|
|
PHP_FUNCTION(openssl_sign)
|
|
|
|
{
|
2001-09-08 01:09:56 +08:00
|
|
|
zval *key, *signature;
|
2000-11-14 03:47:20 +08:00
|
|
|
EVP_PKEY *pkey;
|
|
|
|
int siglen;
|
|
|
|
unsigned char *sigbuf;
|
2001-04-02 07:06:15 +08:00
|
|
|
long keyresource = -1;
|
2001-09-08 01:09:56 +08:00
|
|
|
char * data; long data_len;
|
2000-11-14 03:47:20 +08:00
|
|
|
EVP_MD_CTX md_ctx;
|
|
|
|
|
2001-09-10 22:30:10 +08:00
|
|
|
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "szz", &data, &data_len, &signature, &key) == FAILURE)
|
2001-09-08 01:09:56 +08:00
|
|
|
return;
|
2001-04-02 07:06:15 +08:00
|
|
|
|
2001-09-08 01:09:56 +08:00
|
|
|
pkey = php_openssl_evp_from_zval(&key, 0, "", 0, &keyresource TSRMLS_CC);
|
2002-12-11 06:18:58 +08:00
|
|
|
if (pkey == NULL) {
|
|
|
|
php_error_docref(NULL TSRMLS_CC, E_WARNING, "supplied key param cannot be coerced into a private key");
|
2001-04-02 07:06:15 +08:00
|
|
|
RETURN_FALSE;
|
|
|
|
}
|
|
|
|
|
2000-11-14 03:47:20 +08:00
|
|
|
siglen = EVP_PKEY_size(pkey);
|
|
|
|
sigbuf = emalloc(siglen + 1);
|
|
|
|
|
|
|
|
EVP_SignInit(&md_ctx, EVP_sha1());
|
2001-09-08 01:09:56 +08:00
|
|
|
EVP_SignUpdate(&md_ctx, data, data_len);
|
2000-11-14 03:47:20 +08:00
|
|
|
if (EVP_SignFinal (&md_ctx, sigbuf, &siglen, pkey)) {
|
2001-09-08 01:09:56 +08:00
|
|
|
zval_dtor(signature);
|
2000-11-14 03:47:20 +08:00
|
|
|
sigbuf[siglen] = '\0';
|
2001-09-08 01:09:56 +08:00
|
|
|
ZVAL_STRINGL(signature, sigbuf, siglen, 0);
|
2001-04-02 07:06:15 +08:00
|
|
|
RETVAL_TRUE;
|
2000-11-14 03:47:20 +08:00
|
|
|
} else {
|
|
|
|
efree(sigbuf);
|
2001-04-02 07:06:15 +08:00
|
|
|
RETVAL_FALSE;
|
2000-11-14 03:47:20 +08:00
|
|
|
}
|
2001-04-02 07:06:15 +08:00
|
|
|
if (keyresource == -1)
|
|
|
|
EVP_PKEY_free(pkey);
|
2000-11-14 03:47:20 +08:00
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2001-04-02 07:06:15 +08:00
|
|
|
/* {{{ proto int openssl_verify(string data, string signature, mixed key)
|
2001-09-11 09:03:59 +08:00
|
|
|
Verifys data */
|
2000-11-14 03:47:20 +08:00
|
|
|
PHP_FUNCTION(openssl_verify)
|
|
|
|
{
|
2001-09-08 01:09:56 +08:00
|
|
|
zval *key;
|
2000-11-14 03:47:20 +08:00
|
|
|
EVP_PKEY *pkey;
|
|
|
|
int err;
|
|
|
|
EVP_MD_CTX md_ctx;
|
2001-04-02 07:06:15 +08:00
|
|
|
long keyresource = -1;
|
2001-09-08 01:09:56 +08:00
|
|
|
char * data; long data_len;
|
|
|
|
char * signature; long signature_len;
|
2000-11-14 03:47:20 +08:00
|
|
|
|
2001-09-10 22:30:10 +08:00
|
|
|
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ssz", &data, &data_len,
|
2001-09-08 01:09:56 +08:00
|
|
|
&signature, &signature_len, &key) == FAILURE)
|
|
|
|
return;
|
|
|
|
|
|
|
|
pkey = php_openssl_evp_from_zval(&key, 1, NULL, 0, &keyresource TSRMLS_CC);
|
2002-12-11 06:18:58 +08:00
|
|
|
if (pkey == NULL) {
|
|
|
|
php_error_docref(NULL TSRMLS_CC, E_WARNING, "supplied key param cannot be coerced into a public key");
|
2001-04-02 07:06:15 +08:00
|
|
|
RETURN_FALSE;
|
|
|
|
}
|
2000-11-14 03:47:20 +08:00
|
|
|
|
|
|
|
EVP_VerifyInit (&md_ctx, EVP_sha1());
|
2001-09-08 01:09:56 +08:00
|
|
|
EVP_VerifyUpdate (&md_ctx, data, data_len);
|
|
|
|
err = EVP_VerifyFinal (&md_ctx, signature, signature_len, pkey);
|
2001-04-02 07:06:15 +08:00
|
|
|
|
|
|
|
if (keyresource == -1)
|
|
|
|
EVP_PKEY_free(pkey);
|
|
|
|
|
2000-11-14 03:47:20 +08:00
|
|
|
RETURN_LONG(err);
|
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2001-04-02 07:06:15 +08:00
|
|
|
/* {{{ proto int openssl_seal(string data, &string sealdata, &array ekeys, array pubkeys)
|
2001-09-11 09:03:59 +08:00
|
|
|
Seals data */
|
2000-11-14 03:47:20 +08:00
|
|
|
PHP_FUNCTION(openssl_seal)
|
|
|
|
{
|
2001-09-08 01:09:56 +08:00
|
|
|
zval *pubkeys, **pubkey, *sealdata, *ekeys;
|
2000-11-14 03:47:20 +08:00
|
|
|
HashTable *pubkeysht;
|
2001-04-02 07:06:15 +08:00
|
|
|
HashPosition pos;
|
2000-11-14 03:47:20 +08:00
|
|
|
EVP_PKEY **pkeys;
|
2001-04-02 07:06:15 +08:00
|
|
|
long * key_resources; /* so we know what to cleanup */
|
2000-11-14 17:54:25 +08:00
|
|
|
int i, len1, len2, *eksl, nkeys;
|
2001-04-02 07:06:15 +08:00
|
|
|
unsigned char *buf = NULL, **eks;
|
2001-09-08 01:09:56 +08:00
|
|
|
char * data; long data_len;
|
2000-11-14 03:47:20 +08:00
|
|
|
EVP_CIPHER_CTX ctx;
|
|
|
|
|
2001-09-10 22:30:10 +08:00
|
|
|
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "szza/",
|
2001-09-08 01:09:56 +08:00
|
|
|
&data, &data_len, &sealdata, &ekeys, &pubkeys) == FAILURE)
|
|
|
|
return;
|
|
|
|
|
|
|
|
pubkeysht = HASH_OF(pubkeys);
|
2000-11-14 03:47:20 +08:00
|
|
|
nkeys = pubkeysht ? zend_hash_num_elements(pubkeysht) : 0;
|
|
|
|
if (!nkeys) {
|
2002-12-11 06:18:58 +08:00
|
|
|
php_error_docref(NULL TSRMLS_CC, E_WARNING,
|
2001-04-02 07:06:15 +08:00
|
|
|
"Fourth argument to openssl_seal() must be a non-empty array");
|
|
|
|
RETURN_FALSE;
|
2000-11-14 03:47:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
pkeys = emalloc(nkeys * sizeof(*pkeys));
|
|
|
|
eksl = emalloc(nkeys * sizeof(*eksl));
|
|
|
|
eks = emalloc(nkeys * sizeof(*eks));
|
2001-04-02 07:06:15 +08:00
|
|
|
key_resources = emalloc(nkeys * sizeof(long));
|
|
|
|
|
|
|
|
/* get the public keys we are using to seal this data */
|
|
|
|
zend_hash_internal_pointer_reset_ex(pubkeysht, &pos);
|
2000-11-14 03:47:20 +08:00
|
|
|
i = 0;
|
2001-04-02 07:06:15 +08:00
|
|
|
while (zend_hash_get_current_data_ex(pubkeysht, (void **) &pubkey,
|
|
|
|
&pos) == SUCCESS) {
|
2001-07-30 16:24:42 +08:00
|
|
|
pkeys[i] = php_openssl_evp_from_zval(pubkey, 1, NULL, 0, &key_resources[i] TSRMLS_CC);
|
2002-12-11 06:18:58 +08:00
|
|
|
if (pkeys[i] == NULL) {
|
|
|
|
php_error_docref(NULL TSRMLS_CC, E_WARNING, "not a public key (%dth member of pubkeys)", i);
|
2001-04-02 07:06:15 +08:00
|
|
|
RETVAL_FALSE;
|
|
|
|
goto clean_exit;
|
|
|
|
}
|
2000-11-14 03:47:20 +08:00
|
|
|
eks[i] = emalloc(EVP_PKEY_size(pkeys[i]) + 1);
|
2001-04-02 07:06:15 +08:00
|
|
|
zend_hash_move_forward_ex(pubkeysht, &pos);
|
2000-11-14 03:47:20 +08:00
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
2001-01-07 19:36:15 +08:00
|
|
|
#if OPENSSL_VERSION_NUMBER >= 0x0090600fL
|
2000-11-14 03:47:20 +08:00
|
|
|
if (!EVP_EncryptInit(&ctx,EVP_rc4(),NULL,NULL)) {
|
2001-04-02 07:06:15 +08:00
|
|
|
RETVAL_FALSE;
|
|
|
|
goto clean_exit;
|
2000-11-14 03:47:20 +08:00
|
|
|
}
|
2001-01-07 19:36:15 +08:00
|
|
|
#else
|
|
|
|
EVP_EncryptInit(&ctx,EVP_rc4(),NULL,NULL);
|
|
|
|
#endif
|
2000-11-14 03:47:20 +08:00
|
|
|
|
2000-11-14 17:54:25 +08:00
|
|
|
#if 0
|
|
|
|
/* Need this if allow ciphers that require initialization vector */
|
2000-11-14 03:47:20 +08:00
|
|
|
ivlen = EVP_CIPHER_CTX_iv_length(&ctx);
|
2001-01-13 17:16:36 +08:00
|
|
|
iv = ivlen ? emalloc(ivlen + 1) : NULL;
|
2000-11-14 17:54:25 +08:00
|
|
|
#endif
|
2000-11-14 03:47:20 +08:00
|
|
|
/* allocate one byte extra to make room for \0 */
|
2001-09-08 01:09:56 +08:00
|
|
|
buf = emalloc(data_len + EVP_CIPHER_CTX_block_size(&ctx));
|
2000-11-14 03:47:20 +08:00
|
|
|
|
2001-01-07 19:36:15 +08:00
|
|
|
if (!EVP_SealInit(&ctx, EVP_rc4(), eks, eksl, NULL, pkeys, nkeys)
|
|
|
|
#if OPENSSL_VERSION_NUMBER >= 0x0090600fL
|
2001-09-08 01:09:56 +08:00
|
|
|
|| !EVP_SealUpdate(&ctx, buf, &len1, data, data_len)
|
2001-01-07 19:36:15 +08:00
|
|
|
#endif
|
2001-09-08 01:09:56 +08:00
|
|
|
)
|
|
|
|
{
|
2001-04-02 07:06:15 +08:00
|
|
|
RETVAL_FALSE;
|
2000-11-14 03:47:20 +08:00
|
|
|
efree(buf);
|
2001-04-02 07:06:15 +08:00
|
|
|
goto clean_exit;
|
|
|
|
|
2000-11-14 03:47:20 +08:00
|
|
|
}
|
|
|
|
|
2001-01-07 19:36:15 +08:00
|
|
|
#if OPENSSL_VERSION_NUMBER < 0x0090600fL
|
2001-09-08 01:09:56 +08:00
|
|
|
EVP_SealUpdate(&ctx, buf, &len1, data, data_len);
|
2001-01-07 19:36:15 +08:00
|
|
|
#endif
|
2000-11-14 03:47:20 +08:00
|
|
|
EVP_SealFinal(&ctx, buf + len1, &len2);
|
|
|
|
|
|
|
|
if (len1 + len2 > 0) {
|
2001-09-08 01:09:56 +08:00
|
|
|
zval_dtor(sealdata);
|
2000-11-14 03:47:20 +08:00
|
|
|
buf[len1 + len2] = '\0';
|
2001-04-02 07:06:15 +08:00
|
|
|
buf = erealloc(buf, len1 + len2 + 1);
|
2001-09-08 01:09:56 +08:00
|
|
|
ZVAL_STRINGL(sealdata, buf, len1 + len2, 0);
|
2000-11-14 03:47:20 +08:00
|
|
|
|
2001-09-08 01:09:56 +08:00
|
|
|
zval_dtor(ekeys);
|
|
|
|
if (array_init(ekeys) == FAILURE) {
|
2002-12-11 06:18:58 +08:00
|
|
|
php_error_docref(NULL TSRMLS_CC, E_ERROR, "Cannot initialize return value");
|
2001-04-02 07:06:15 +08:00
|
|
|
RETVAL_FALSE;
|
|
|
|
efree(buf);
|
|
|
|
goto clean_exit;
|
2000-11-14 03:47:20 +08:00
|
|
|
}
|
|
|
|
for (i=0; i<nkeys; i++) {
|
|
|
|
eks[i][eksl[i]] = '\0';
|
2001-09-08 01:09:56 +08:00
|
|
|
add_next_index_stringl(ekeys, erealloc(eks[i], eksl[i] + 1), eksl[i], 0);
|
2001-04-02 07:06:15 +08:00
|
|
|
eks[i] = NULL;
|
2000-11-14 03:47:20 +08:00
|
|
|
}
|
2000-11-14 17:54:25 +08:00
|
|
|
#if 0
|
|
|
|
/* If allow ciphers that need IV, we need this */
|
2000-11-14 03:47:20 +08:00
|
|
|
zval_dtor(*ivec);
|
|
|
|
if (ivlen) {
|
|
|
|
iv[ivlen] = '\0';
|
|
|
|
ZVAL_STRINGL(*ivec, erealloc(iv, ivlen + 1), ivlen, 0);
|
|
|
|
} else {
|
|
|
|
ZVAL_EMPTY_STRING(*ivec);
|
|
|
|
}
|
2000-11-14 17:54:25 +08:00
|
|
|
#endif
|
2001-04-02 07:06:15 +08:00
|
|
|
}
|
|
|
|
else
|
2000-11-14 03:47:20 +08:00
|
|
|
efree(buf);
|
2001-04-02 07:06:15 +08:00
|
|
|
|
|
|
|
RETVAL_LONG(len1 + len2);
|
|
|
|
|
|
|
|
clean_exit:
|
|
|
|
for (i=0; i<nkeys; i++) {
|
|
|
|
if (key_resources[i] == -1)
|
|
|
|
EVP_PKEY_free(pkeys[i]);
|
|
|
|
if (eks[i])
|
2000-11-14 03:47:20 +08:00
|
|
|
efree(eks[i]);
|
|
|
|
}
|
2001-04-02 07:06:15 +08:00
|
|
|
efree(eks);
|
|
|
|
efree(eksl);
|
|
|
|
efree(pkeys);
|
|
|
|
efree(key_resources);
|
2000-11-14 03:47:20 +08:00
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2001-04-02 07:06:15 +08:00
|
|
|
/* {{{ proto bool openssl_open(string data, &string opendata, string ekey, mixed privkey)
|
2001-09-11 09:03:59 +08:00
|
|
|
Opens data */
|
2000-11-14 03:47:20 +08:00
|
|
|
PHP_FUNCTION(openssl_open)
|
|
|
|
{
|
2001-09-08 01:09:56 +08:00
|
|
|
zval *privkey, *opendata;
|
2000-11-14 03:47:20 +08:00
|
|
|
EVP_PKEY *pkey;
|
2001-04-02 07:06:15 +08:00
|
|
|
int len1, len2;
|
|
|
|
unsigned char *buf;
|
|
|
|
long keyresource = -1;
|
2000-11-14 03:47:20 +08:00
|
|
|
EVP_CIPHER_CTX ctx;
|
2001-09-08 01:09:56 +08:00
|
|
|
char * data; long data_len;
|
|
|
|
char * ekey; long ekey_len;
|
2000-11-14 03:47:20 +08:00
|
|
|
|
2001-09-10 22:30:10 +08:00
|
|
|
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "szsz", &data, &data_len,
|
2001-09-08 01:09:56 +08:00
|
|
|
&opendata, &ekey, &ekey_len, &privkey) == FAILURE)
|
|
|
|
return;
|
2000-11-14 03:47:20 +08:00
|
|
|
|
2001-09-08 01:09:56 +08:00
|
|
|
pkey = php_openssl_evp_from_zval(&privkey, 0, "", 0, &keyresource TSRMLS_CC);
|
2002-12-11 06:18:58 +08:00
|
|
|
if (pkey == NULL) {
|
|
|
|
php_error_docref(NULL TSRMLS_CC, E_WARNING, "unable to coerce parameter 4 into a private key");
|
2001-04-02 07:06:15 +08:00
|
|
|
RETURN_FALSE;
|
|
|
|
}
|
2001-09-08 01:09:56 +08:00
|
|
|
buf = emalloc(data_len + 1);
|
2000-11-14 03:47:20 +08:00
|
|
|
|
2001-09-08 01:09:56 +08:00
|
|
|
if (EVP_OpenInit(&ctx, EVP_rc4(), ekey, ekey_len, NULL, pkey)
|
2001-01-07 19:36:15 +08:00
|
|
|
#if OPENSSL_VERSION_NUMBER >= 0x0090600fL
|
2001-09-08 01:09:56 +08:00
|
|
|
&& EVP_OpenUpdate(&ctx, buf, &len1, data, data_len)
|
2001-01-07 19:36:15 +08:00
|
|
|
#endif
|
2001-04-02 07:06:15 +08:00
|
|
|
) {
|
2001-01-07 19:36:15 +08:00
|
|
|
#if OPENSSL_VERSION_NUMBER < 0x0090600fL
|
2001-09-08 01:09:56 +08:00
|
|
|
EVP_OpenUpdate(&ctx, buf, &len1, data, data_len);
|
2001-01-07 19:36:15 +08:00
|
|
|
#endif
|
|
|
|
if (!EVP_OpenFinal(&ctx, buf + len1, &len2) ||
|
2001-04-02 07:06:15 +08:00
|
|
|
(len1 + len2 == 0)) {
|
2001-01-07 19:36:15 +08:00
|
|
|
efree(buf);
|
2001-04-02 07:06:15 +08:00
|
|
|
if (keyresource == -1)
|
|
|
|
EVP_PKEY_free(pkey);
|
2001-01-07 19:36:15 +08:00
|
|
|
RETURN_FALSE;
|
|
|
|
}
|
|
|
|
} else {
|
2000-11-14 03:47:20 +08:00
|
|
|
efree(buf);
|
2001-04-02 07:06:15 +08:00
|
|
|
if (keyresource == -1)
|
|
|
|
EVP_PKEY_free(pkey);
|
|
|
|
|
2000-11-14 03:47:20 +08:00
|
|
|
RETURN_FALSE;
|
|
|
|
}
|
2001-04-02 07:06:15 +08:00
|
|
|
if (keyresource == -1)
|
|
|
|
EVP_PKEY_free(pkey);
|
|
|
|
|
2001-09-08 01:09:56 +08:00
|
|
|
zval_dtor(opendata);
|
2000-11-14 03:47:20 +08:00
|
|
|
buf[len1 + len2] = '\0';
|
2001-09-08 01:09:56 +08:00
|
|
|
ZVAL_STRINGL(opendata, erealloc(buf, len1 + len2 + 1), len1 + len2, 0);
|
2000-11-14 03:47:20 +08:00
|
|
|
RETURN_TRUE;
|
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Local variables:
|
|
|
|
* tab-width: 8
|
|
|
|
* c-basic-offset: 8
|
|
|
|
* End:
|
2001-09-09 21:29:31 +08:00
|
|
|
* vim600: sw=4 ts=4 fdm=marker
|
|
|
|
* vim<600: sw=4 ts=4
|
2000-11-14 03:47:20 +08:00
|
|
|
*/
|