Annotation of embedaddon/php/ext/openssl/openssl.c, revision 1.1.1.2
1.1 misho 1: /*
2: +----------------------------------------------------------------------+
3: | PHP Version 5 |
4: +----------------------------------------------------------------------+
5: | Copyright (c) 1997-2012 The PHP Group |
6: +----------------------------------------------------------------------+
7: | This source file is subject to version 3.01 of the PHP license, |
8: | that is bundled with this package in the file LICENSE, and is |
9: | available through the world-wide-web at the following url: |
10: | http://www.php.net/license/3_01.txt |
11: | If you did not receive a copy of the PHP license and are unable to |
12: | obtain it through the world-wide-web, please send a note to |
13: | license@php.net so we can mail you a copy immediately. |
14: +----------------------------------------------------------------------+
15: | Authors: Stig Venaas <venaas@php.net> |
16: | Wez Furlong <wez@thebrainroom.com> |
17: | Sascha Kettler <kettler@gmx.net> |
18: | Pierre-Alain Joye <pierre@php.net> |
1.1.1.2 ! misho 19: | Marc Delling <delling@silpion.de> (PKCS12 functions) |
1.1 misho 20: +----------------------------------------------------------------------+
21: */
22:
1.1.1.2 ! misho 23: /* $Id$ */
1.1 misho 24:
25: #ifdef HAVE_CONFIG_H
26: #include "config.h"
27: #endif
28:
29: #include "php.h"
30: #include "php_openssl.h"
31:
32: /* PHP Includes */
33: #include "ext/standard/file.h"
34: #include "ext/standard/info.h"
35: #include "ext/standard/php_fopen_wrappers.h"
36: #include "ext/standard/md5.h"
37: #include "ext/standard/base64.h"
38:
1.1.1.2 ! misho 39: #if PHP_WIN32
! 40: # include "win32/winutil.h"
! 41: #endif
! 42:
1.1 misho 43: /* OpenSSL includes */
44: #include <openssl/evp.h>
45: #include <openssl/x509.h>
46: #include <openssl/x509v3.h>
47: #include <openssl/crypto.h>
48: #include <openssl/pem.h>
49: #include <openssl/err.h>
50: #include <openssl/conf.h>
51: #include <openssl/rand.h>
52: #include <openssl/ssl.h>
53: #include <openssl/pkcs12.h>
54:
55: /* Common */
56: #include <time.h>
57:
58: #ifdef NETWARE
59: #define timezone _timezone /* timezone is called _timezone in LibC */
60: #endif
61:
62: #define DEFAULT_KEY_LENGTH 512
63: #define MIN_KEY_LENGTH 384
64:
65: #define OPENSSL_ALGO_SHA1 1
66: #define OPENSSL_ALGO_MD5 2
67: #define OPENSSL_ALGO_MD4 3
68: #ifdef HAVE_OPENSSL_MD2_H
69: #define OPENSSL_ALGO_MD2 4
70: #endif
71: #define OPENSSL_ALGO_DSS1 5
72:
73: #define DEBUG_SMIME 0
74:
75: /* FIXME: Use the openssl constants instead of
76: * enum. It is now impossible to match real values
77: * against php constants. Also sorry to break the
78: * enum principles here, BC...
79: */
80: enum php_openssl_key_type {
81: OPENSSL_KEYTYPE_RSA,
82: OPENSSL_KEYTYPE_DSA,
83: OPENSSL_KEYTYPE_DH,
84: OPENSSL_KEYTYPE_DEFAULT = OPENSSL_KEYTYPE_RSA,
85: #ifdef EVP_PKEY_EC
86: OPENSSL_KEYTYPE_EC = OPENSSL_KEYTYPE_DH +1
87: #endif
88: };
89:
90: enum php_openssl_cipher_type {
91: PHP_OPENSSL_CIPHER_RC2_40,
92: PHP_OPENSSL_CIPHER_RC2_128,
93: PHP_OPENSSL_CIPHER_RC2_64,
94: PHP_OPENSSL_CIPHER_DES,
95: PHP_OPENSSL_CIPHER_3DES,
1.1.1.2 ! misho 96: PHP_OPENSSL_CIPHER_AES_128_CBC,
! 97: PHP_OPENSSL_CIPHER_AES_192_CBC,
! 98: PHP_OPENSSL_CIPHER_AES_256_CBC,
1.1 misho 99:
100: PHP_OPENSSL_CIPHER_DEFAULT = PHP_OPENSSL_CIPHER_RC2_40
101: };
102:
103: PHP_FUNCTION(openssl_get_md_methods);
104: PHP_FUNCTION(openssl_get_cipher_methods);
105:
106: PHP_FUNCTION(openssl_digest);
107: PHP_FUNCTION(openssl_encrypt);
108: PHP_FUNCTION(openssl_decrypt);
109: PHP_FUNCTION(openssl_cipher_iv_length);
110:
111: PHP_FUNCTION(openssl_dh_compute_key);
112: PHP_FUNCTION(openssl_random_pseudo_bytes);
113:
114: /* {{{ arginfo */
115: ZEND_BEGIN_ARG_INFO_EX(arginfo_openssl_x509_export_to_file, 0, 0, 2)
116: ZEND_ARG_INFO(0, x509)
117: ZEND_ARG_INFO(0, outfilename)
118: ZEND_ARG_INFO(0, notext)
119: ZEND_END_ARG_INFO()
120:
121: ZEND_BEGIN_ARG_INFO_EX(arginfo_openssl_x509_export, 0, 0, 2)
122: ZEND_ARG_INFO(0, x509)
123: ZEND_ARG_INFO(1, out)
124: ZEND_ARG_INFO(0, notext)
125: ZEND_END_ARG_INFO()
126:
127: ZEND_BEGIN_ARG_INFO(arginfo_openssl_x509_check_private_key, 0)
128: ZEND_ARG_INFO(0, cert)
129: ZEND_ARG_INFO(0, key)
130: ZEND_END_ARG_INFO()
131:
132: ZEND_BEGIN_ARG_INFO(arginfo_openssl_x509_parse, 0)
133: ZEND_ARG_INFO(0, x509)
134: ZEND_ARG_INFO(0, shortname)
135: ZEND_END_ARG_INFO()
136:
137: ZEND_BEGIN_ARG_INFO_EX(arginfo_openssl_x509_checkpurpose, 0, 0, 3)
138: ZEND_ARG_INFO(0, x509cert)
139: ZEND_ARG_INFO(0, purpose)
140: ZEND_ARG_INFO(0, cainfo) /* array */
141: ZEND_ARG_INFO(0, untrustedfile)
142: ZEND_END_ARG_INFO()
143:
144: ZEND_BEGIN_ARG_INFO(arginfo_openssl_x509_read, 0)
145: ZEND_ARG_INFO(0, cert)
146: ZEND_END_ARG_INFO()
147:
148: ZEND_BEGIN_ARG_INFO(arginfo_openssl_x509_free, 0)
149: ZEND_ARG_INFO(0, x509)
150: ZEND_END_ARG_INFO()
151:
152: ZEND_BEGIN_ARG_INFO_EX(arginfo_openssl_pkcs12_export_to_file, 0, 0, 4)
153: ZEND_ARG_INFO(0, x509)
154: ZEND_ARG_INFO(0, filename)
155: ZEND_ARG_INFO(0, priv_key)
156: ZEND_ARG_INFO(0, pass)
157: ZEND_ARG_INFO(0, args) /* array */
158: ZEND_END_ARG_INFO()
159:
160: ZEND_BEGIN_ARG_INFO(arginfo_openssl_pkcs12_export, 0)
161: ZEND_ARG_INFO(0, x509)
162: ZEND_ARG_INFO(1, out)
163: ZEND_ARG_INFO(0, priv_key)
164: ZEND_ARG_INFO(0, pass)
165: ZEND_ARG_INFO(0, args) /* array */
166: ZEND_END_ARG_INFO()
167:
168: ZEND_BEGIN_ARG_INFO(arginfo_openssl_pkcs12_read, 0)
169: ZEND_ARG_INFO(0, PKCS12)
170: ZEND_ARG_INFO(1, certs) /* array */
171: ZEND_ARG_INFO(0, pass)
172: ZEND_END_ARG_INFO()
173:
174: ZEND_BEGIN_ARG_INFO_EX(arginfo_openssl_csr_export_to_file, 0, 0, 2)
175: ZEND_ARG_INFO(0, csr)
176: ZEND_ARG_INFO(0, outfilename)
177: ZEND_ARG_INFO(0, notext)
178: ZEND_END_ARG_INFO()
179:
180: ZEND_BEGIN_ARG_INFO_EX(arginfo_openssl_csr_export, 0, 0, 2)
181: ZEND_ARG_INFO(0, csr)
182: ZEND_ARG_INFO(1, out)
183: ZEND_ARG_INFO(0, notext)
184: ZEND_END_ARG_INFO()
185:
186: ZEND_BEGIN_ARG_INFO_EX(arginfo_openssl_csr_sign, 0, 0, 4)
187: ZEND_ARG_INFO(0, csr)
188: ZEND_ARG_INFO(0, x509)
189: ZEND_ARG_INFO(0, priv_key)
190: ZEND_ARG_INFO(0, days)
191: ZEND_ARG_INFO(0, config_args) /* array */
192: ZEND_ARG_INFO(0, serial)
193: ZEND_END_ARG_INFO()
194:
195: ZEND_BEGIN_ARG_INFO_EX(arginfo_openssl_csr_new, 0, 0, 2)
196: ZEND_ARG_INFO(0, dn) /* array */
197: ZEND_ARG_INFO(1, privkey)
198: ZEND_ARG_INFO(0, configargs)
199: ZEND_ARG_INFO(0, extraattribs)
200: ZEND_END_ARG_INFO()
201:
202: ZEND_BEGIN_ARG_INFO(arginfo_openssl_csr_get_subject, 0)
203: ZEND_ARG_INFO(0, csr)
204: ZEND_END_ARG_INFO()
205:
206: ZEND_BEGIN_ARG_INFO(arginfo_openssl_csr_get_public_key, 0)
207: ZEND_ARG_INFO(0, csr)
208: ZEND_END_ARG_INFO()
209:
210: ZEND_BEGIN_ARG_INFO_EX(arginfo_openssl_pkey_new, 0, 0, 0)
211: ZEND_ARG_INFO(0, configargs) /* array */
212: ZEND_END_ARG_INFO()
213:
214: ZEND_BEGIN_ARG_INFO_EX(arginfo_openssl_pkey_export_to_file, 0, 0, 2)
215: ZEND_ARG_INFO(0, key)
216: ZEND_ARG_INFO(0, outfilename)
217: ZEND_ARG_INFO(0, passphrase)
218: ZEND_ARG_INFO(0, config_args) /* array */
219: ZEND_END_ARG_INFO()
220:
221: ZEND_BEGIN_ARG_INFO_EX(arginfo_openssl_pkey_export, 0, 0, 2)
222: ZEND_ARG_INFO(0, key)
223: ZEND_ARG_INFO(1, out)
224: ZEND_ARG_INFO(0, passphrase)
225: ZEND_ARG_INFO(0, config_args) /* array */
226: ZEND_END_ARG_INFO()
227:
228: ZEND_BEGIN_ARG_INFO(arginfo_openssl_pkey_get_public, 0)
229: ZEND_ARG_INFO(0, cert)
230: ZEND_END_ARG_INFO()
231:
232: ZEND_BEGIN_ARG_INFO(arginfo_openssl_pkey_free, 0)
233: ZEND_ARG_INFO(0, key)
234: ZEND_END_ARG_INFO()
235:
236: ZEND_BEGIN_ARG_INFO_EX(arginfo_openssl_pkey_get_private, 0, 0, 1)
237: ZEND_ARG_INFO(0, key)
238: ZEND_ARG_INFO(0, passphrase)
239: ZEND_END_ARG_INFO()
240:
241: ZEND_BEGIN_ARG_INFO(arginfo_openssl_pkey_get_details, 0)
242: ZEND_ARG_INFO(0, key)
243: ZEND_END_ARG_INFO()
244:
245: ZEND_BEGIN_ARG_INFO_EX(arginfo_openssl_pkcs7_verify, 0, 0, 2)
246: ZEND_ARG_INFO(0, filename)
247: ZEND_ARG_INFO(0, flags)
248: ZEND_ARG_INFO(0, signerscerts)
249: ZEND_ARG_INFO(0, cainfo) /* array */
250: ZEND_ARG_INFO(0, extracerts)
251: ZEND_ARG_INFO(0, content)
252: ZEND_END_ARG_INFO()
253:
254: ZEND_BEGIN_ARG_INFO_EX(arginfo_openssl_pkcs7_encrypt, 0, 0, 4)
255: ZEND_ARG_INFO(0, infile)
256: ZEND_ARG_INFO(0, outfile)
257: ZEND_ARG_INFO(0, recipcerts)
258: ZEND_ARG_INFO(0, headers) /* array */
259: ZEND_ARG_INFO(0, flags)
260: ZEND_ARG_INFO(0, cipher)
261: ZEND_END_ARG_INFO()
262:
263: ZEND_BEGIN_ARG_INFO_EX(arginfo_openssl_pkcs7_sign, 0, 0, 5)
264: ZEND_ARG_INFO(0, infile)
265: ZEND_ARG_INFO(0, outfile)
266: ZEND_ARG_INFO(0, signcert)
267: ZEND_ARG_INFO(0, signkey)
268: ZEND_ARG_INFO(0, headers) /* array */
269: ZEND_ARG_INFO(0, flags)
270: ZEND_ARG_INFO(0, extracertsfilename)
271: ZEND_END_ARG_INFO()
272:
273: ZEND_BEGIN_ARG_INFO_EX(arginfo_openssl_pkcs7_decrypt, 0, 0, 3)
274: ZEND_ARG_INFO(0, infilename)
275: ZEND_ARG_INFO(0, outfilename)
276: ZEND_ARG_INFO(0, recipcert)
277: ZEND_ARG_INFO(0, recipkey)
278: ZEND_END_ARG_INFO()
279:
280: ZEND_BEGIN_ARG_INFO_EX(arginfo_openssl_private_encrypt, 0, 0, 3)
281: ZEND_ARG_INFO(0, data)
282: ZEND_ARG_INFO(1, crypted)
283: ZEND_ARG_INFO(0, key)
284: ZEND_ARG_INFO(0, padding)
285: ZEND_END_ARG_INFO()
286:
287: ZEND_BEGIN_ARG_INFO_EX(arginfo_openssl_private_decrypt, 0, 0, 3)
288: ZEND_ARG_INFO(0, data)
289: ZEND_ARG_INFO(1, crypted)
290: ZEND_ARG_INFO(0, key)
291: ZEND_ARG_INFO(0, padding)
292: ZEND_END_ARG_INFO()
293:
294: ZEND_BEGIN_ARG_INFO_EX(arginfo_openssl_public_encrypt, 0, 0, 3)
295: ZEND_ARG_INFO(0, data)
296: ZEND_ARG_INFO(1, crypted)
297: ZEND_ARG_INFO(0, key)
298: ZEND_ARG_INFO(0, padding)
299: ZEND_END_ARG_INFO()
300:
301: ZEND_BEGIN_ARG_INFO_EX(arginfo_openssl_public_decrypt, 0, 0, 3)
302: ZEND_ARG_INFO(0, data)
303: ZEND_ARG_INFO(1, crypted)
304: ZEND_ARG_INFO(0, key)
305: ZEND_ARG_INFO(0, padding)
306: ZEND_END_ARG_INFO()
307:
308: ZEND_BEGIN_ARG_INFO(arginfo_openssl_error_string, 0)
309: ZEND_END_ARG_INFO()
310:
311: ZEND_BEGIN_ARG_INFO_EX(arginfo_openssl_sign, 0, 0, 3)
312: ZEND_ARG_INFO(0, data)
313: ZEND_ARG_INFO(1, signature)
314: ZEND_ARG_INFO(0, key)
315: ZEND_ARG_INFO(0, method)
316: ZEND_END_ARG_INFO()
317:
318: ZEND_BEGIN_ARG_INFO_EX(arginfo_openssl_verify, 0, 0, 3)
319: ZEND_ARG_INFO(0, data)
320: ZEND_ARG_INFO(0, signature)
321: ZEND_ARG_INFO(0, key)
322: ZEND_ARG_INFO(0, method)
323: ZEND_END_ARG_INFO()
324:
325: ZEND_BEGIN_ARG_INFO(arginfo_openssl_seal, 0)
326: ZEND_ARG_INFO(0, data)
327: ZEND_ARG_INFO(1, sealdata)
328: ZEND_ARG_INFO(1, ekeys) /* arary */
329: ZEND_ARG_INFO(0, pubkeys) /* array */
330: ZEND_END_ARG_INFO()
331:
332: ZEND_BEGIN_ARG_INFO(arginfo_openssl_open, 0)
333: ZEND_ARG_INFO(0, data)
334: ZEND_ARG_INFO(1, opendata)
335: ZEND_ARG_INFO(0, ekey)
336: ZEND_ARG_INFO(0, privkey)
337: ZEND_END_ARG_INFO()
338:
339: ZEND_BEGIN_ARG_INFO_EX(arginfo_openssl_get_md_methods, 0, 0, 0)
340: ZEND_ARG_INFO(0, aliases)
341: ZEND_END_ARG_INFO()
342:
343: ZEND_BEGIN_ARG_INFO_EX(arginfo_openssl_get_cipher_methods, 0, 0, 0)
344: ZEND_ARG_INFO(0, aliases)
345: ZEND_END_ARG_INFO()
346:
347: ZEND_BEGIN_ARG_INFO_EX(arginfo_openssl_digest, 0, 0, 2)
348: ZEND_ARG_INFO(0, data)
349: ZEND_ARG_INFO(0, method)
350: ZEND_ARG_INFO(0, raw_output)
351: ZEND_END_ARG_INFO()
352:
353: ZEND_BEGIN_ARG_INFO_EX(arginfo_openssl_encrypt, 0, 0, 3)
354: ZEND_ARG_INFO(0, data)
355: ZEND_ARG_INFO(0, method)
356: ZEND_ARG_INFO(0, password)
1.1.1.2 ! misho 357: ZEND_ARG_INFO(0, options)
1.1 misho 358: ZEND_ARG_INFO(0, iv)
359: ZEND_END_ARG_INFO()
360:
361: ZEND_BEGIN_ARG_INFO_EX(arginfo_openssl_decrypt, 0, 0, 3)
362: ZEND_ARG_INFO(0, data)
363: ZEND_ARG_INFO(0, method)
364: ZEND_ARG_INFO(0, password)
1.1.1.2 ! misho 365: ZEND_ARG_INFO(0, options)
1.1 misho 366: ZEND_ARG_INFO(0, iv)
367: ZEND_END_ARG_INFO()
368:
369: ZEND_BEGIN_ARG_INFO(arginfo_openssl_cipher_iv_length, 0)
370: ZEND_ARG_INFO(0, method)
371: ZEND_END_ARG_INFO()
372:
373: ZEND_BEGIN_ARG_INFO(arginfo_openssl_dh_compute_key, 0)
374: ZEND_ARG_INFO(0, pub_key)
375: ZEND_ARG_INFO(0, dh_key)
376: ZEND_END_ARG_INFO()
377:
378: ZEND_BEGIN_ARG_INFO_EX(arginfo_openssl_random_pseudo_bytes, 0, 0, 1)
379: ZEND_ARG_INFO(0, length)
380: ZEND_ARG_INFO(1, result_is_strong)
381: ZEND_END_ARG_INFO()
382: /* }}} */
383:
384: /* {{{ openssl_functions[]
385: */
386: const zend_function_entry openssl_functions[] = {
387: /* public/private key functions */
388: PHP_FE(openssl_pkey_free, arginfo_openssl_pkey_free)
389: PHP_FE(openssl_pkey_new, arginfo_openssl_pkey_new)
390: PHP_FE(openssl_pkey_export, arginfo_openssl_pkey_export)
391: PHP_FE(openssl_pkey_export_to_file, arginfo_openssl_pkey_export_to_file)
392: PHP_FE(openssl_pkey_get_private, arginfo_openssl_pkey_get_private)
393: PHP_FE(openssl_pkey_get_public, arginfo_openssl_pkey_get_public)
394: PHP_FE(openssl_pkey_get_details, arginfo_openssl_pkey_get_details)
395:
396: PHP_FALIAS(openssl_free_key, openssl_pkey_free, arginfo_openssl_pkey_free)
397: PHP_FALIAS(openssl_get_privatekey, openssl_pkey_get_private, arginfo_openssl_pkey_get_private)
398: PHP_FALIAS(openssl_get_publickey, openssl_pkey_get_public, arginfo_openssl_pkey_get_public)
399:
400: /* x.509 cert funcs */
401: PHP_FE(openssl_x509_read, arginfo_openssl_x509_read)
402: PHP_FE(openssl_x509_free, arginfo_openssl_x509_free)
403: PHP_FE(openssl_x509_parse, arginfo_openssl_x509_parse)
404: PHP_FE(openssl_x509_checkpurpose, arginfo_openssl_x509_checkpurpose)
405: PHP_FE(openssl_x509_check_private_key, arginfo_openssl_x509_check_private_key)
406: PHP_FE(openssl_x509_export, arginfo_openssl_x509_export)
407: PHP_FE(openssl_x509_export_to_file, arginfo_openssl_x509_export_to_file)
408:
409: /* PKCS12 funcs */
410: PHP_FE(openssl_pkcs12_export, arginfo_openssl_pkcs12_export)
411: PHP_FE(openssl_pkcs12_export_to_file, arginfo_openssl_pkcs12_export_to_file)
412: PHP_FE(openssl_pkcs12_read, arginfo_openssl_pkcs12_read)
413:
414: /* CSR funcs */
415: PHP_FE(openssl_csr_new, arginfo_openssl_csr_new)
416: PHP_FE(openssl_csr_export, arginfo_openssl_csr_export)
417: PHP_FE(openssl_csr_export_to_file, arginfo_openssl_csr_export_to_file)
418: PHP_FE(openssl_csr_sign, arginfo_openssl_csr_sign)
419: PHP_FE(openssl_csr_get_subject, arginfo_openssl_csr_get_subject)
420: PHP_FE(openssl_csr_get_public_key, arginfo_openssl_csr_get_public_key)
421:
422: PHP_FE(openssl_digest, arginfo_openssl_digest)
423: PHP_FE(openssl_encrypt, arginfo_openssl_encrypt)
424: PHP_FE(openssl_decrypt, arginfo_openssl_decrypt)
425: PHP_FE(openssl_cipher_iv_length, arginfo_openssl_cipher_iv_length)
426: PHP_FE(openssl_sign, arginfo_openssl_sign)
427: PHP_FE(openssl_verify, arginfo_openssl_verify)
428: PHP_FE(openssl_seal, arginfo_openssl_seal)
429: PHP_FE(openssl_open, arginfo_openssl_open)
430:
431: /* for S/MIME handling */
432: PHP_FE(openssl_pkcs7_verify, arginfo_openssl_pkcs7_verify)
433: PHP_FE(openssl_pkcs7_decrypt, arginfo_openssl_pkcs7_decrypt)
434: PHP_FE(openssl_pkcs7_sign, arginfo_openssl_pkcs7_sign)
435: PHP_FE(openssl_pkcs7_encrypt, arginfo_openssl_pkcs7_encrypt)
436:
437: PHP_FE(openssl_private_encrypt, arginfo_openssl_private_encrypt)
438: PHP_FE(openssl_private_decrypt, arginfo_openssl_private_decrypt)
439: PHP_FE(openssl_public_encrypt, arginfo_openssl_public_encrypt)
440: PHP_FE(openssl_public_decrypt, arginfo_openssl_public_decrypt)
441:
442: PHP_FE(openssl_get_md_methods, arginfo_openssl_get_md_methods)
443: PHP_FE(openssl_get_cipher_methods, arginfo_openssl_get_cipher_methods)
444:
445: PHP_FE(openssl_dh_compute_key, arginfo_openssl_dh_compute_key)
446:
447: PHP_FE(openssl_random_pseudo_bytes, arginfo_openssl_random_pseudo_bytes)
448: PHP_FE(openssl_error_string, arginfo_openssl_error_string)
449: PHP_FE_END
450: };
451: /* }}} */
452:
453: /* {{{ openssl_module_entry
454: */
455: zend_module_entry openssl_module_entry = {
456: STANDARD_MODULE_HEADER,
457: "openssl",
458: openssl_functions,
459: PHP_MINIT(openssl),
460: PHP_MSHUTDOWN(openssl),
461: NULL,
462: NULL,
463: PHP_MINFO(openssl),
464: NO_VERSION_YET,
465: STANDARD_MODULE_PROPERTIES
466: };
467: /* }}} */
468:
469: #ifdef COMPILE_DL_OPENSSL
470: ZEND_GET_MODULE(openssl)
471: #endif
472:
473: static int le_key;
474: static int le_x509;
475: static int le_csr;
476: static int ssl_stream_data_index;
477:
478: int php_openssl_get_x509_list_id(void) /* {{{ */
479: {
480: return le_x509;
481: }
482: /* }}} */
483:
484: /* {{{ resource destructors */
485: static void php_pkey_free(zend_rsrc_list_entry *rsrc TSRMLS_DC)
486: {
487: EVP_PKEY *pkey = (EVP_PKEY *)rsrc->ptr;
488:
489: assert(pkey != NULL);
490:
491: EVP_PKEY_free(pkey);
492: }
493:
494: static void php_x509_free(zend_rsrc_list_entry *rsrc TSRMLS_DC)
495: {
496: X509 *x509 = (X509 *)rsrc->ptr;
497: X509_free(x509);
498: }
499:
500: static void php_csr_free(zend_rsrc_list_entry *rsrc TSRMLS_DC)
501: {
502: X509_REQ * csr = (X509_REQ*)rsrc->ptr;
503: X509_REQ_free(csr);
504: }
505: /* }}} */
506:
1.1.1.2 ! misho 507: /* {{{ openssl open_basedir check */
! 508: inline static int php_openssl_open_base_dir_chk(char *filename TSRMLS_DC)
1.1 misho 509: {
510: if (php_check_open_basedir(filename TSRMLS_CC)) {
511: return -1;
512: }
1.1.1.2 ! misho 513:
1.1 misho 514: return 0;
515: }
516: /* }}} */
517:
518: /* openssl -> PHP "bridging" */
519: /* true global; readonly after module startup */
520: static char default_ssl_conf_filename[MAXPATHLEN];
521:
522: struct php_x509_request { /* {{{ */
523: #if OPENSSL_VERSION_NUMBER >= 0x10000002L
524: LHASH_OF(CONF_VALUE) * global_config; /* Global SSL config */
525: LHASH_OF(CONF_VALUE) * req_config; /* SSL config for this request */
526: #else
527: LHASH * global_config; /* Global SSL config */
528: LHASH * req_config; /* SSL config for this request */
529: #endif
530: const EVP_MD * md_alg;
531: const EVP_MD * digest;
532: char * section_name,
533: * config_filename,
534: * digest_name,
535: * extensions_section,
536: * request_extensions_section;
537: int priv_key_bits;
538: int priv_key_type;
539:
540: int priv_key_encrypt;
541:
542: EVP_PKEY * priv_key;
1.1.1.2 ! misho 543:
! 544: const EVP_CIPHER * priv_key_encrypt_cipher;
1.1 misho 545: };
546: /* }}} */
547:
548: static X509 * php_openssl_x509_from_zval(zval ** val, int makeresource, long * resourceval TSRMLS_DC);
549: static EVP_PKEY * php_openssl_evp_from_zval(zval ** val, int public_key, char * passphrase, int makeresource, long * resourceval TSRMLS_DC);
550: static int php_openssl_is_private_key(EVP_PKEY* pkey TSRMLS_DC);
551: static X509_STORE * setup_verify(zval * calist TSRMLS_DC);
552: static STACK_OF(X509) * load_all_certs_from_file(char *certfile);
553: static X509_REQ * php_openssl_csr_from_zval(zval ** val, int makeresource, long * resourceval TSRMLS_DC);
554: static EVP_PKEY * php_openssl_generate_private_key(struct php_x509_request * req TSRMLS_DC);
555:
556: static void add_assoc_name_entry(zval * val, char * key, X509_NAME * name, int shortname TSRMLS_DC) /* {{{ */
557: {
558: zval *subitem, *subentries;
559: int i, j = -1, last = -1, obj_cnt = 0;
560: char *sname;
561: int nid;
562: X509_NAME_ENTRY * ne;
563: ASN1_STRING * str = NULL;
564: ASN1_OBJECT * obj;
565:
566: if (key != NULL) {
567: MAKE_STD_ZVAL(subitem);
568: array_init(subitem);
569: } else {
570: subitem = val;
571: }
1.1.1.2 ! misho 572:
1.1 misho 573: for (i = 0; i < X509_NAME_entry_count(name); i++) {
574: unsigned char *to_add;
575: int to_add_len;
576:
577:
578: ne = X509_NAME_get_entry(name, i);
579: obj = X509_NAME_ENTRY_get_object(ne);
580: nid = OBJ_obj2nid(obj);
581: obj_cnt = 0;
582:
583: if (shortname) {
584: sname = (char *) OBJ_nid2sn(nid);
585: } else {
586: sname = (char *) OBJ_nid2ln(nid);
587: }
588:
589: MAKE_STD_ZVAL(subentries);
590: array_init(subentries);
591:
592: last = -1;
593: for (;;) {
594: j = X509_NAME_get_index_by_OBJ(name, obj, last);
595: if (j < 0) {
596: if (last != -1) break;
597: } else {
598: obj_cnt++;
599: ne = X509_NAME_get_entry(name, j);
600: str = X509_NAME_ENTRY_get_data(ne);
601: if (ASN1_STRING_type(str) != V_ASN1_UTF8STRING) {
602: to_add_len = ASN1_STRING_to_UTF8(&to_add, str);
603: if (to_add_len != -1) {
604: add_next_index_stringl(subentries, (char *)to_add, to_add_len, 1);
605: }
606: } else {
607: to_add = ASN1_STRING_data(str);
608: to_add_len = ASN1_STRING_length(str);
609: add_next_index_stringl(subentries, (char *)to_add, to_add_len, 1);
610: }
611: }
612: last = j;
613: }
614: i = last;
1.1.1.2 ! misho 615:
1.1 misho 616: if (obj_cnt > 1) {
617: add_assoc_zval_ex(subitem, sname, strlen(sname) + 1, subentries);
618: } else {
619: zval_dtor(subentries);
620: FREE_ZVAL(subentries);
621: if (obj_cnt && str && to_add_len > -1) {
622: add_assoc_stringl(subitem, sname, (char *)to_add, to_add_len, 1);
623: }
624: }
625: }
626: if (key != NULL) {
627: zend_hash_update(HASH_OF(val), key, strlen(key) + 1, (void *)&subitem, sizeof(subitem), NULL);
628: }
629: }
630: /* }}} */
631:
632: static void add_assoc_asn1_string(zval * val, char * key, ASN1_STRING * str) /* {{{ */
633: {
634: add_assoc_stringl(val, key, (char *)str->data, str->length, 1);
635: }
636: /* }}} */
637:
638: static time_t asn1_time_to_time_t(ASN1_UTCTIME * timestr TSRMLS_DC) /* {{{ */
639: {
640: /*
641: This is how the time string is formatted:
642:
643: snprintf(p, sizeof(p), "%02d%02d%02d%02d%02d%02dZ",ts->tm_year%100,
644: ts->tm_mon+1,ts->tm_mday,ts->tm_hour,ts->tm_min,ts->tm_sec);
645: */
646:
647: time_t ret;
648: struct tm thetime;
649: char * strbuf;
650: char * thestr;
651: long gmadjust = 0;
652:
653: if (timestr->length < 13) {
654: php_error_docref(NULL TSRMLS_CC, E_WARNING, "extension author too lazy to parse %s correctly", timestr->data);
655: return (time_t)-1;
656: }
657:
658: strbuf = estrdup((char *)timestr->data);
659:
660: memset(&thetime, 0, sizeof(thetime));
661:
662: /* we work backwards so that we can use atoi more easily */
663:
664: thestr = strbuf + timestr->length - 3;
665:
666: thetime.tm_sec = atoi(thestr);
667: *thestr = '\0';
668: thestr -= 2;
669: thetime.tm_min = atoi(thestr);
670: *thestr = '\0';
671: thestr -= 2;
672: thetime.tm_hour = atoi(thestr);
673: *thestr = '\0';
674: thestr -= 2;
675: thetime.tm_mday = atoi(thestr);
676: *thestr = '\0';
677: thestr -= 2;
678: thetime.tm_mon = atoi(thestr)-1;
679: *thestr = '\0';
680: thestr -= 2;
681: thetime.tm_year = atoi(thestr);
682:
683: if (thetime.tm_year < 68) {
684: thetime.tm_year += 100;
685: }
686:
687: thetime.tm_isdst = -1;
688: ret = mktime(&thetime);
689:
690: #if HAVE_TM_GMTOFF
691: gmadjust = thetime.tm_gmtoff;
692: #else
693: /*
694: ** If correcting for daylight savings time, we set the adjustment to
695: ** the value of timezone - 3600 seconds. Otherwise, we need to overcorrect and
696: ** set the adjustment to the main timezone + 3600 seconds.
697: */
698: gmadjust = -(thetime.tm_isdst ? (long)timezone - 3600 : (long)timezone + 3600);
699: #endif
700: ret += gmadjust;
701:
702: efree(strbuf);
703:
704: return ret;
705: }
706: /* }}} */
707:
708: #if OPENSSL_VERSION_NUMBER >= 0x10000002L
709: static inline int php_openssl_config_check_syntax(const char * section_label, const char * config_filename, const char * section, LHASH_OF(CONF_VALUE) * config TSRMLS_DC) /* {{{ */
710: #else
711: static inline int php_openssl_config_check_syntax(const char * section_label, const char * config_filename, const char * section, LHASH * config TSRMLS_DC)
712: #endif
713: {
714: X509V3_CTX ctx;
1.1.1.2 ! misho 715:
1.1 misho 716: X509V3_set_ctx_test(&ctx);
717: X509V3_set_conf_lhash(&ctx, config);
718: if (!X509V3_EXT_add_conf(config, &ctx, (char *)section, NULL)) {
719: php_error_docref(NULL TSRMLS_CC, E_WARNING, "Error loading %s section %s of %s",
720: section_label,
721: section,
722: config_filename);
723: return FAILURE;
724: }
725: return SUCCESS;
726: }
727: /* }}} */
728:
729: static int add_oid_section(struct php_x509_request * req TSRMLS_DC) /* {{{ */
730: {
731: char * str;
732: STACK_OF(CONF_VALUE) * sktmp;
733: CONF_VALUE * cnf;
734: int i;
735:
736: str = CONF_get_string(req->req_config, NULL, "oid_section");
737: if (str == NULL) {
738: return SUCCESS;
739: }
740: sktmp = CONF_get_section(req->req_config, str);
741: if (sktmp == NULL) {
742: php_error_docref(NULL TSRMLS_CC, E_WARNING, "problem loading oid section %s", str);
743: return FAILURE;
744: }
745: for (i = 0; i < sk_CONF_VALUE_num(sktmp); i++) {
746: cnf = sk_CONF_VALUE_value(sktmp, i);
747: if (OBJ_create(cnf->value, cnf->name, cnf->name) == NID_undef) {
748: php_error_docref(NULL TSRMLS_CC, E_WARNING, "problem creating object %s=%s", cnf->name, cnf->value);
749: return FAILURE;
750: }
751: }
752: return SUCCESS;
753: }
754: /* }}} */
755:
756: #define PHP_SSL_REQ_INIT(req) memset(req, 0, sizeof(*req))
757: #define PHP_SSL_REQ_DISPOSE(req) php_openssl_dispose_config(req TSRMLS_CC)
758: #define PHP_SSL_REQ_PARSE(req, zval) php_openssl_parse_config(req, zval TSRMLS_CC)
759:
760: #define PHP_SSL_CONFIG_SYNTAX_CHECK(var) if (req->var && php_openssl_config_check_syntax(#var, \
761: req->config_filename, req->var, req->req_config TSRMLS_CC) == FAILURE) return FAILURE
762:
763: #define SET_OPTIONAL_STRING_ARG(key, varname, defval) \
764: if (optional_args && zend_hash_find(Z_ARRVAL_P(optional_args), key, sizeof(key), (void**)&item) == SUCCESS) \
765: varname = Z_STRVAL_PP(item); \
766: else \
767: varname = defval
768:
769: #define SET_OPTIONAL_LONG_ARG(key, varname, defval) \
770: if (optional_args && zend_hash_find(Z_ARRVAL_P(optional_args), key, sizeof(key), (void**)&item) == SUCCESS) \
771: varname = Z_LVAL_PP(item); \
772: else \
773: varname = defval
774:
1.1.1.2 ! misho 775: static const EVP_CIPHER * php_openssl_get_evp_cipher_from_algo(long algo);
! 776:
! 777:
1.1 misho 778: static int php_openssl_parse_config(struct php_x509_request * req, zval * optional_args TSRMLS_DC) /* {{{ */
779: {
780: char * str;
781: zval ** item;
782:
783: SET_OPTIONAL_STRING_ARG("config", req->config_filename, default_ssl_conf_filename);
784: SET_OPTIONAL_STRING_ARG("config_section_name", req->section_name, "req");
785: req->global_config = CONF_load(NULL, default_ssl_conf_filename, NULL);
786: req->req_config = CONF_load(NULL, req->config_filename, NULL);
787:
788: if (req->req_config == NULL) {
789: return FAILURE;
790: }
791:
792: /* read in the oids */
793: str = CONF_get_string(req->req_config, NULL, "oid_file");
1.1.1.2 ! misho 794: if (str && !php_openssl_open_base_dir_chk(str TSRMLS_CC)) {
1.1 misho 795: BIO *oid_bio = BIO_new_file(str, "r");
796: if (oid_bio) {
797: OBJ_create_objects(oid_bio);
798: BIO_free(oid_bio);
799: }
800: }
801: if (add_oid_section(req TSRMLS_CC) == FAILURE) {
802: return FAILURE;
803: }
804: SET_OPTIONAL_STRING_ARG("digest_alg", req->digest_name,
805: CONF_get_string(req->req_config, req->section_name, "default_md"));
806: SET_OPTIONAL_STRING_ARG("x509_extensions", req->extensions_section,
807: CONF_get_string(req->req_config, req->section_name, "x509_extensions"));
808: SET_OPTIONAL_STRING_ARG("req_extensions", req->request_extensions_section,
809: CONF_get_string(req->req_config, req->section_name, "req_extensions"));
810: SET_OPTIONAL_LONG_ARG("private_key_bits", req->priv_key_bits,
811: CONF_get_number(req->req_config, req->section_name, "default_bits"));
812:
813: SET_OPTIONAL_LONG_ARG("private_key_type", req->priv_key_type, OPENSSL_KEYTYPE_DEFAULT);
814:
815: if (optional_args && zend_hash_find(Z_ARRVAL_P(optional_args), "encrypt_key", sizeof("encrypt_key"), (void**)&item) == SUCCESS) {
816: req->priv_key_encrypt = Z_BVAL_PP(item);
817: } else {
818: str = CONF_get_string(req->req_config, req->section_name, "encrypt_rsa_key");
819: if (str == NULL) {
820: str = CONF_get_string(req->req_config, req->section_name, "encrypt_key");
821: }
822: if (str && strcmp(str, "no") == 0) {
823: req->priv_key_encrypt = 0;
824: } else {
825: req->priv_key_encrypt = 1;
826: }
827: }
1.1.1.2 ! misho 828:
! 829: if (req->priv_key_encrypt && optional_args && zend_hash_find(Z_ARRVAL_P(optional_args), "encrypt_key_cipher", sizeof("encrypt_key_cipher"), (void**)&item) == SUCCESS) {
! 830: long cipher_algo = Z_LVAL_PP(item);
! 831: const EVP_CIPHER* cipher = php_openssl_get_evp_cipher_from_algo(cipher_algo);
! 832: if (cipher == NULL) {
! 833: php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unknown cipher algorithm for private key.");
! 834: return FAILURE;
! 835: } else {
! 836: req->priv_key_encrypt_cipher = cipher;
! 837: }
! 838: } else {
! 839: req->priv_key_encrypt_cipher = NULL;
! 840: }
! 841:
! 842:
! 843:
1.1 misho 844: /* digest alg */
845: if (req->digest_name == NULL) {
846: req->digest_name = CONF_get_string(req->req_config, req->section_name, "default_md");
847: }
848: if (req->digest_name) {
849: req->digest = req->md_alg = EVP_get_digestbyname(req->digest_name);
850: }
851: if (req->md_alg == NULL) {
852: req->md_alg = req->digest = EVP_md5();
853: }
854:
855: PHP_SSL_CONFIG_SYNTAX_CHECK(extensions_section);
856:
857: /* set the string mask */
858: str = CONF_get_string(req->req_config, req->section_name, "string_mask");
859: if (str && !ASN1_STRING_set_default_mask_asc(str)) {
860: php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid global string mask setting %s", str);
861: return FAILURE;
862: }
863:
864: PHP_SSL_CONFIG_SYNTAX_CHECK(request_extensions_section);
1.1.1.2 ! misho 865:
1.1 misho 866: return SUCCESS;
867: }
868: /* }}} */
869:
870: static void php_openssl_dispose_config(struct php_x509_request * req TSRMLS_DC) /* {{{ */
871: {
872: if (req->priv_key) {
873: EVP_PKEY_free(req->priv_key);
874: req->priv_key = NULL;
875: }
876: if (req->global_config) {
877: CONF_free(req->global_config);
878: req->global_config = NULL;
879: }
880: if (req->req_config) {
881: CONF_free(req->req_config);
882: req->req_config = NULL;
883: }
884: }
885: /* }}} */
886:
1.1.1.2 ! misho 887: static int php_openssl_load_rand_file(const char * file, int *egdsocket, int *seeded TSRMLS_DC) /* {{{ */
1.1 misho 888: {
889: char buffer[MAXPATHLEN];
890:
891: *egdsocket = 0;
892: *seeded = 0;
893:
894: if (file == NULL) {
895: file = RAND_file_name(buffer, sizeof(buffer));
896: } else if (RAND_egd(file) > 0) {
897: /* if the given filename is an EGD socket, don't
898: * write anything back to it */
899: *egdsocket = 1;
900: return SUCCESS;
901: }
902: if (file == NULL || !RAND_load_file(file, -1)) {
903: if (RAND_status() == 0) {
904: php_error_docref(NULL TSRMLS_CC, E_WARNING, "unable to load random state; not enough random data!");
905: return FAILURE;
906: }
907: return FAILURE;
908: }
909: *seeded = 1;
910: return SUCCESS;
911: }
912: /* }}} */
913:
914: static int php_openssl_write_rand_file(const char * file, int egdsocket, int seeded) /* {{{ */
915: {
916: char buffer[MAXPATHLEN];
917:
918: TSRMLS_FETCH();
919:
920: if (egdsocket || !seeded) {
921: /* if we did not manage to read the seed file, we should not write
922: * a low-entropy seed file back */
923: return FAILURE;
924: }
925: if (file == NULL) {
926: file = RAND_file_name(buffer, sizeof(buffer));
927: }
928: if (file == NULL || !RAND_write_file(file)) {
929: php_error_docref(NULL TSRMLS_CC, E_WARNING, "unable to write random state");
930: return FAILURE;
931: }
932: return SUCCESS;
933: }
934: /* }}} */
935:
936: static EVP_MD * php_openssl_get_evp_md_from_algo(long algo) { /* {{{ */
937: EVP_MD *mdtype;
938:
939: switch (algo) {
940: case OPENSSL_ALGO_SHA1:
941: mdtype = (EVP_MD *) EVP_sha1();
942: break;
943: case OPENSSL_ALGO_MD5:
944: mdtype = (EVP_MD *) EVP_md5();
945: break;
946: case OPENSSL_ALGO_MD4:
947: mdtype = (EVP_MD *) EVP_md4();
948: break;
949: #ifdef HAVE_OPENSSL_MD2_H
950: case OPENSSL_ALGO_MD2:
951: mdtype = (EVP_MD *) EVP_md2();
952: break;
953: #endif
954: case OPENSSL_ALGO_DSS1:
955: mdtype = (EVP_MD *) EVP_dss1();
956: break;
957: default:
958: return NULL;
959: break;
960: }
961: return mdtype;
962: }
963: /* }}} */
964:
965: static const EVP_CIPHER * php_openssl_get_evp_cipher_from_algo(long algo) { /* {{{ */
966: switch (algo) {
967: #ifndef OPENSSL_NO_RC2
968: case PHP_OPENSSL_CIPHER_RC2_40:
969: return EVP_rc2_40_cbc();
970: break;
971: case PHP_OPENSSL_CIPHER_RC2_64:
972: return EVP_rc2_64_cbc();
973: break;
974: case PHP_OPENSSL_CIPHER_RC2_128:
975: return EVP_rc2_cbc();
976: break;
977: #endif
978:
979: #ifndef OPENSSL_NO_DES
980: case PHP_OPENSSL_CIPHER_DES:
981: return EVP_des_cbc();
982: break;
983: case PHP_OPENSSL_CIPHER_3DES:
984: return EVP_des_ede3_cbc();
985: break;
986: #endif
1.1.1.2 ! misho 987:
! 988: #ifndef OPENSSL_NO_AES
! 989: case PHP_OPENSSL_CIPHER_AES_128_CBC:
! 990: return EVP_aes_128_cbc();
! 991: break;
! 992: case PHP_OPENSSL_CIPHER_AES_192_CBC:
! 993: return EVP_aes_192_cbc();
! 994: break;
! 995: case PHP_OPENSSL_CIPHER_AES_256_CBC:
! 996: return EVP_aes_256_cbc();
! 997: break;
! 998: #endif
! 999:
! 1000:
1.1 misho 1001: default:
1002: return NULL;
1003: break;
1004: }
1005: }
1006: /* }}} */
1007:
1008: /* {{{ PHP_MINIT_FUNCTION
1009: */
1010: PHP_MINIT_FUNCTION(openssl)
1011: {
1012: char * config_filename;
1013:
1014: le_key = zend_register_list_destructors_ex(php_pkey_free, NULL, "OpenSSL key", module_number);
1015: le_x509 = zend_register_list_destructors_ex(php_x509_free, NULL, "OpenSSL X.509", module_number);
1016: le_csr = zend_register_list_destructors_ex(php_csr_free, NULL, "OpenSSL X.509 CSR", module_number);
1017:
1018: SSL_library_init();
1019: OpenSSL_add_all_ciphers();
1020: OpenSSL_add_all_digests();
1021: OpenSSL_add_all_algorithms();
1022:
1.1.1.2 ! misho 1023: SSL_load_error_strings();
1.1 misho 1024:
1025: /* register a resource id number with OpenSSL so that we can map SSL -> stream structures in
1026: * OpenSSL callbacks */
1027: ssl_stream_data_index = SSL_get_ex_new_index(0, "PHP stream index", NULL, NULL, NULL);
1.1.1.2 ! misho 1028:
1.1 misho 1029: REGISTER_STRING_CONSTANT("OPENSSL_VERSION_TEXT", OPENSSL_VERSION_TEXT, CONST_CS|CONST_PERSISTENT);
1030: REGISTER_LONG_CONSTANT("OPENSSL_VERSION_NUMBER", OPENSSL_VERSION_NUMBER, CONST_CS|CONST_PERSISTENT);
1.1.1.2 ! misho 1031:
1.1 misho 1032: /* purposes for cert purpose checking */
1033: REGISTER_LONG_CONSTANT("X509_PURPOSE_SSL_CLIENT", X509_PURPOSE_SSL_CLIENT, CONST_CS|CONST_PERSISTENT);
1034: REGISTER_LONG_CONSTANT("X509_PURPOSE_SSL_SERVER", X509_PURPOSE_SSL_SERVER, CONST_CS|CONST_PERSISTENT);
1035: REGISTER_LONG_CONSTANT("X509_PURPOSE_NS_SSL_SERVER", X509_PURPOSE_NS_SSL_SERVER, CONST_CS|CONST_PERSISTENT);
1036: REGISTER_LONG_CONSTANT("X509_PURPOSE_SMIME_SIGN", X509_PURPOSE_SMIME_SIGN, CONST_CS|CONST_PERSISTENT);
1037: REGISTER_LONG_CONSTANT("X509_PURPOSE_SMIME_ENCRYPT", X509_PURPOSE_SMIME_ENCRYPT, CONST_CS|CONST_PERSISTENT);
1038: REGISTER_LONG_CONSTANT("X509_PURPOSE_CRL_SIGN", X509_PURPOSE_CRL_SIGN, CONST_CS|CONST_PERSISTENT);
1039: #ifdef X509_PURPOSE_ANY
1040: REGISTER_LONG_CONSTANT("X509_PURPOSE_ANY", X509_PURPOSE_ANY, CONST_CS|CONST_PERSISTENT);
1041: #endif
1042:
1043: /* signature algorithm constants */
1044: REGISTER_LONG_CONSTANT("OPENSSL_ALGO_SHA1", OPENSSL_ALGO_SHA1, CONST_CS|CONST_PERSISTENT);
1045: REGISTER_LONG_CONSTANT("OPENSSL_ALGO_MD5", OPENSSL_ALGO_MD5, CONST_CS|CONST_PERSISTENT);
1046: REGISTER_LONG_CONSTANT("OPENSSL_ALGO_MD4", OPENSSL_ALGO_MD4, CONST_CS|CONST_PERSISTENT);
1047: #ifdef HAVE_OPENSSL_MD2_H
1048: REGISTER_LONG_CONSTANT("OPENSSL_ALGO_MD2", OPENSSL_ALGO_MD2, CONST_CS|CONST_PERSISTENT);
1049: #endif
1050: REGISTER_LONG_CONSTANT("OPENSSL_ALGO_DSS1", OPENSSL_ALGO_DSS1, CONST_CS|CONST_PERSISTENT);
1051:
1052: /* flags for S/MIME */
1053: REGISTER_LONG_CONSTANT("PKCS7_DETACHED", PKCS7_DETACHED, CONST_CS|CONST_PERSISTENT);
1054: REGISTER_LONG_CONSTANT("PKCS7_TEXT", PKCS7_TEXT, CONST_CS|CONST_PERSISTENT);
1055: REGISTER_LONG_CONSTANT("PKCS7_NOINTERN", PKCS7_NOINTERN, CONST_CS|CONST_PERSISTENT);
1056: REGISTER_LONG_CONSTANT("PKCS7_NOVERIFY", PKCS7_NOVERIFY, CONST_CS|CONST_PERSISTENT);
1057: REGISTER_LONG_CONSTANT("PKCS7_NOCHAIN", PKCS7_NOCHAIN, CONST_CS|CONST_PERSISTENT);
1058: REGISTER_LONG_CONSTANT("PKCS7_NOCERTS", PKCS7_NOCERTS, CONST_CS|CONST_PERSISTENT);
1059: REGISTER_LONG_CONSTANT("PKCS7_NOATTR", PKCS7_NOATTR, CONST_CS|CONST_PERSISTENT);
1060: REGISTER_LONG_CONSTANT("PKCS7_BINARY", PKCS7_BINARY, CONST_CS|CONST_PERSISTENT);
1061: REGISTER_LONG_CONSTANT("PKCS7_NOSIGS", PKCS7_NOSIGS, CONST_CS|CONST_PERSISTENT);
1062:
1063: REGISTER_LONG_CONSTANT("OPENSSL_PKCS1_PADDING", RSA_PKCS1_PADDING, CONST_CS|CONST_PERSISTENT);
1064: REGISTER_LONG_CONSTANT("OPENSSL_SSLV23_PADDING", RSA_SSLV23_PADDING, CONST_CS|CONST_PERSISTENT);
1065: REGISTER_LONG_CONSTANT("OPENSSL_NO_PADDING", RSA_NO_PADDING, CONST_CS|CONST_PERSISTENT);
1066: REGISTER_LONG_CONSTANT("OPENSSL_PKCS1_OAEP_PADDING", RSA_PKCS1_OAEP_PADDING, CONST_CS|CONST_PERSISTENT);
1067:
1068: /* Ciphers */
1069: #ifndef OPENSSL_NO_RC2
1070: REGISTER_LONG_CONSTANT("OPENSSL_CIPHER_RC2_40", PHP_OPENSSL_CIPHER_RC2_40, CONST_CS|CONST_PERSISTENT);
1071: REGISTER_LONG_CONSTANT("OPENSSL_CIPHER_RC2_128", PHP_OPENSSL_CIPHER_RC2_128, CONST_CS|CONST_PERSISTENT);
1072: REGISTER_LONG_CONSTANT("OPENSSL_CIPHER_RC2_64", PHP_OPENSSL_CIPHER_RC2_64, CONST_CS|CONST_PERSISTENT);
1073: #endif
1074: #ifndef OPENSSL_NO_DES
1075: REGISTER_LONG_CONSTANT("OPENSSL_CIPHER_DES", PHP_OPENSSL_CIPHER_DES, CONST_CS|CONST_PERSISTENT);
1076: REGISTER_LONG_CONSTANT("OPENSSL_CIPHER_3DES", PHP_OPENSSL_CIPHER_3DES, CONST_CS|CONST_PERSISTENT);
1077: #endif
1.1.1.2 ! misho 1078: #ifndef OPENSSL_NO_AES
! 1079: REGISTER_LONG_CONSTANT("OPENSSL_CIPHER_AES_128_CBC", PHP_OPENSSL_CIPHER_AES_128_CBC, CONST_CS|CONST_PERSISTENT);
! 1080: REGISTER_LONG_CONSTANT("OPENSSL_CIPHER_AES_192_CBC", PHP_OPENSSL_CIPHER_AES_192_CBC, CONST_CS|CONST_PERSISTENT);
! 1081: REGISTER_LONG_CONSTANT("OPENSSL_CIPHER_AES_256_CBC", PHP_OPENSSL_CIPHER_AES_256_CBC, CONST_CS|CONST_PERSISTENT);
! 1082: #endif
1.1 misho 1083:
1084: /* Values for key types */
1085: REGISTER_LONG_CONSTANT("OPENSSL_KEYTYPE_RSA", OPENSSL_KEYTYPE_RSA, CONST_CS|CONST_PERSISTENT);
1086: #ifndef NO_DSA
1087: REGISTER_LONG_CONSTANT("OPENSSL_KEYTYPE_DSA", OPENSSL_KEYTYPE_DSA, CONST_CS|CONST_PERSISTENT);
1088: #endif
1089: REGISTER_LONG_CONSTANT("OPENSSL_KEYTYPE_DH", OPENSSL_KEYTYPE_DH, CONST_CS|CONST_PERSISTENT);
1090: #ifdef EVP_PKEY_EC
1091: REGISTER_LONG_CONSTANT("OPENSSL_KEYTYPE_EC", OPENSSL_KEYTYPE_EC, CONST_CS|CONST_PERSISTENT);
1092: #endif
1093:
1.1.1.2 ! misho 1094: REGISTER_LONG_CONSTANT("OPENSSL_RAW_DATA", OPENSSL_RAW_DATA, CONST_CS|CONST_PERSISTENT);
! 1095: REGISTER_LONG_CONSTANT("OPENSSL_ZERO_PADDING", OPENSSL_ZERO_PADDING, CONST_CS|CONST_PERSISTENT);
! 1096:
1.1 misho 1097: #if OPENSSL_VERSION_NUMBER >= 0x0090806fL && !defined(OPENSSL_NO_TLSEXT)
1098: /* SNI support included in OpenSSL >= 0.9.8j */
1099: REGISTER_LONG_CONSTANT("OPENSSL_TLSEXT_SERVER_NAME", 1, CONST_CS|CONST_PERSISTENT);
1100: #endif
1101:
1102: /* Determine default SSL configuration file */
1103: config_filename = getenv("OPENSSL_CONF");
1104: if (config_filename == NULL) {
1105: config_filename = getenv("SSLEAY_CONF");
1106: }
1107:
1108: /* default to 'openssl.cnf' if no environment variable is set */
1109: if (config_filename == NULL) {
1110: snprintf(default_ssl_conf_filename, sizeof(default_ssl_conf_filename), "%s/%s",
1111: X509_get_default_cert_area(),
1112: "openssl.cnf");
1113: } else {
1114: strlcpy(default_ssl_conf_filename, config_filename, sizeof(default_ssl_conf_filename));
1115: }
1116:
1117: php_stream_xport_register("ssl", php_openssl_ssl_socket_factory TSRMLS_CC);
1118: php_stream_xport_register("sslv3", php_openssl_ssl_socket_factory TSRMLS_CC);
1119: #ifndef OPENSSL_NO_SSL2
1120: php_stream_xport_register("sslv2", php_openssl_ssl_socket_factory TSRMLS_CC);
1121: #endif
1122: php_stream_xport_register("tls", php_openssl_ssl_socket_factory TSRMLS_CC);
1123:
1124: /* override the default tcp socket provider */
1125: php_stream_xport_register("tcp", php_openssl_ssl_socket_factory TSRMLS_CC);
1126:
1127: php_register_url_stream_wrapper("https", &php_stream_http_wrapper TSRMLS_CC);
1128: php_register_url_stream_wrapper("ftps", &php_stream_ftp_wrapper TSRMLS_CC);
1.1.1.2 ! misho 1129:
1.1 misho 1130: return SUCCESS;
1131: }
1132: /* }}} */
1133:
1134: /* {{{ PHP_MINFO_FUNCTION
1135: */
1136: PHP_MINFO_FUNCTION(openssl)
1137: {
1138: php_info_print_table_start();
1139: php_info_print_table_row(2, "OpenSSL support", "enabled");
1140: php_info_print_table_row(2, "OpenSSL Library Version", SSLeay_version(SSLEAY_VERSION));
1141: php_info_print_table_row(2, "OpenSSL Header Version", OPENSSL_VERSION_TEXT);
1142: php_info_print_table_end();
1143: }
1144: /* }}} */
1145:
1146: /* {{{ PHP_MSHUTDOWN_FUNCTION
1147: */
1148: PHP_MSHUTDOWN_FUNCTION(openssl)
1149: {
1150: EVP_cleanup();
1151:
1152: php_unregister_url_stream_wrapper("https" TSRMLS_CC);
1153: php_unregister_url_stream_wrapper("ftps" TSRMLS_CC);
1154:
1155: php_stream_xport_unregister("ssl" TSRMLS_CC);
1156: #ifndef OPENSSL_NO_SSL2
1157: php_stream_xport_unregister("sslv2" TSRMLS_CC);
1158: #endif
1159: php_stream_xport_unregister("sslv3" TSRMLS_CC);
1160: php_stream_xport_unregister("tls" TSRMLS_CC);
1161:
1162: /* reinstate the default tcp handler */
1163: php_stream_xport_register("tcp", php_stream_generic_socket_factory TSRMLS_CC);
1164:
1165: return SUCCESS;
1166: }
1167: /* }}} */
1168:
1169: /* {{{ x509 cert functions */
1170:
1171: /* {{{ php_openssl_x509_from_zval
1172: Given a zval, coerce it into an X509 object.
1173: The zval can be:
1174: . X509 resource created using openssl_read_x509()
1175: . if it starts with file:// then it will be interpreted as the path to that cert
1176: . it will be interpreted as the cert data
1177: If you supply makeresource, the result will be registered as an x509 resource and
1178: it's value returned in makeresource.
1179: */
1180: static X509 * php_openssl_x509_from_zval(zval ** val, int makeresource, long * resourceval TSRMLS_DC)
1181: {
1182: X509 *cert = NULL;
1183:
1184: if (resourceval) {
1185: *resourceval = -1;
1186: }
1187: if (Z_TYPE_PP(val) == IS_RESOURCE) {
1188: /* is it an x509 resource ? */
1189: void * what;
1190: int type;
1191:
1192: what = zend_fetch_resource(val TSRMLS_CC, -1, "OpenSSL X.509", &type, 1, le_x509);
1193: if (!what) {
1194: return NULL;
1195: }
1196: /* this is so callers can decide if they should free the X509 */
1197: if (resourceval) {
1198: *resourceval = Z_LVAL_PP(val);
1199: }
1200: if (type == le_x509) {
1201: return (X509*)what;
1202: }
1203: /* other types could be used here - eg: file pointers and read in the data from them */
1204:
1205: return NULL;
1206: }
1207:
1208: if (!(Z_TYPE_PP(val) == IS_STRING || Z_TYPE_PP(val) == IS_OBJECT)) {
1209: return NULL;
1210: }
1211:
1212: /* force it to be a string and check if it refers to a file */
1213: convert_to_string_ex(val);
1214:
1215: if (Z_STRLEN_PP(val) > 7 && memcmp(Z_STRVAL_PP(val), "file://", sizeof("file://") - 1) == 0) {
1216: /* read cert from the named file */
1217: BIO *in;
1218:
1.1.1.2 ! misho 1219: if (php_openssl_open_base_dir_chk(Z_STRVAL_PP(val) + (sizeof("file://") - 1) TSRMLS_CC)) {
1.1 misho 1220: return NULL;
1221: }
1222:
1223: in = BIO_new_file(Z_STRVAL_PP(val) + (sizeof("file://") - 1), "r");
1224: if (in == NULL) {
1225: return NULL;
1226: }
1227: cert = PEM_read_bio_X509(in, NULL, NULL, NULL);
1228: BIO_free(in);
1229: } else {
1230: BIO *in;
1231:
1232: in = BIO_new_mem_buf(Z_STRVAL_PP(val), Z_STRLEN_PP(val));
1233: if (in == NULL) {
1234: return NULL;
1235: }
1236: #ifdef TYPEDEF_D2I_OF
1237: cert = (X509 *) PEM_ASN1_read_bio((d2i_of_void *)d2i_X509, PEM_STRING_X509, in, NULL, NULL, NULL);
1238: #else
1239: cert = (X509 *) PEM_ASN1_read_bio((char *(*)())d2i_X509, PEM_STRING_X509, in, NULL, NULL, NULL);
1240: #endif
1241: BIO_free(in);
1242: }
1243:
1244: if (cert && makeresource && resourceval) {
1.1.1.2 ! misho 1245: *resourceval = zend_list_insert(cert, le_x509 TSRMLS_CC);
1.1 misho 1246: }
1247: return cert;
1248: }
1249:
1250: /* }}} */
1251:
1252: /* {{{ proto bool openssl_x509_export_to_file(mixed x509, string outfilename [, bool notext = true])
1253: Exports a CERT to file or a var */
1254: PHP_FUNCTION(openssl_x509_export_to_file)
1255: {
1256: X509 * cert;
1257: zval ** zcert;
1258: zend_bool notext = 1;
1259: BIO * bio_out;
1260: long certresource;
1261: char * filename;
1262: int filename_len;
1263:
1.1.1.2 ! misho 1264: if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "Zp|b", &zcert, &filename, &filename_len, ¬ext) == FAILURE) {
1.1 misho 1265: return;
1266: }
1267: RETVAL_FALSE;
1268:
1269: cert = php_openssl_x509_from_zval(zcert, 0, &certresource TSRMLS_CC);
1270: if (cert == NULL) {
1271: php_error_docref(NULL TSRMLS_CC, E_WARNING, "cannot get cert from parameter 1");
1272: return;
1273: }
1274:
1.1.1.2 ! misho 1275: if (php_openssl_open_base_dir_chk(filename TSRMLS_CC)) {
1.1 misho 1276: return;
1277: }
1278:
1279: bio_out = BIO_new_file(filename, "w");
1280: if (bio_out) {
1281: if (!notext) {
1282: X509_print(bio_out, cert);
1283: }
1284: PEM_write_bio_X509(bio_out, cert);
1285:
1286: RETVAL_TRUE;
1287: } else {
1288: php_error_docref(NULL TSRMLS_CC, E_WARNING, "error opening file %s", filename);
1289: }
1290: if (certresource == -1 && cert) {
1291: X509_free(cert);
1292: }
1293: BIO_free(bio_out);
1294: }
1295: /* }}} */
1296:
1297: /* {{{ proto bool openssl_x509_export(mixed x509, string &out [, bool notext = true])
1298: Exports a CERT to file or a var */
1299: PHP_FUNCTION(openssl_x509_export)
1300: {
1301: X509 * cert;
1302: zval ** zcert, *zout;
1303: zend_bool notext = 1;
1304: BIO * bio_out;
1305: long certresource;
1306:
1307: if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "Zz|b", &zcert, &zout, ¬ext) == FAILURE) {
1308: return;
1309: }
1310: RETVAL_FALSE;
1311:
1312: cert = php_openssl_x509_from_zval(zcert, 0, &certresource TSRMLS_CC);
1313: if (cert == NULL) {
1314: php_error_docref(NULL TSRMLS_CC, E_WARNING, "cannot get cert from parameter 1");
1315: return;
1316: }
1317:
1318: bio_out = BIO_new(BIO_s_mem());
1319: if (!notext) {
1320: X509_print(bio_out, cert);
1321: }
1322: if (PEM_write_bio_X509(bio_out, cert)) {
1323: BUF_MEM *bio_buf;
1324:
1325: zval_dtor(zout);
1326: BIO_get_mem_ptr(bio_out, &bio_buf);
1327: ZVAL_STRINGL(zout, bio_buf->data, bio_buf->length, 1);
1328:
1329: RETVAL_TRUE;
1330: }
1331:
1332: if (certresource == -1 && cert) {
1333: X509_free(cert);
1334: }
1335: BIO_free(bio_out);
1336: }
1337: /* }}} */
1338:
1339: /* {{{ proto bool openssl_x509_check_private_key(mixed cert, mixed key)
1340: Checks if a private key corresponds to a CERT */
1341: PHP_FUNCTION(openssl_x509_check_private_key)
1342: {
1343: zval ** zcert, **zkey;
1344: X509 * cert = NULL;
1345: EVP_PKEY * key = NULL;
1346: long certresource = -1, keyresource = -1;
1347:
1348: RETVAL_FALSE;
1.1.1.2 ! misho 1349:
1.1 misho 1350: if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ZZ", &zcert, &zkey) == FAILURE) {
1351: return;
1352: }
1353: cert = php_openssl_x509_from_zval(zcert, 0, &certresource TSRMLS_CC);
1354: if (cert == NULL) {
1355: RETURN_FALSE;
1.1.1.2 ! misho 1356: }
1.1 misho 1357: key = php_openssl_evp_from_zval(zkey, 0, "", 1, &keyresource TSRMLS_CC);
1358: if (key) {
1359: RETVAL_BOOL(X509_check_private_key(cert, key));
1360: }
1361:
1362: if (keyresource == -1 && key) {
1363: EVP_PKEY_free(key);
1364: }
1365: if (certresource == -1 && cert) {
1366: X509_free(cert);
1367: }
1368: }
1369: /* }}} */
1370:
1371: /* {{{ proto array openssl_x509_parse(mixed x509 [, bool shortnames=true])
1372: Returns an array of the fields/values of the CERT */
1373: PHP_FUNCTION(openssl_x509_parse)
1374: {
1375: zval ** zcert;
1376: X509 * cert = NULL;
1377: long certresource = -1;
1378: int i;
1379: zend_bool useshortnames = 1;
1380: char * tmpstr;
1381: zval * subitem;
1382: X509_EXTENSION *extension;
1383: char *extname;
1384: BIO *bio_out;
1385: BUF_MEM *bio_buf;
1386: char buf[256];
1387:
1388: if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "Z|b", &zcert, &useshortnames) == FAILURE) {
1389: return;
1390: }
1391: cert = php_openssl_x509_from_zval(zcert, 0, &certresource TSRMLS_CC);
1392: if (cert == NULL) {
1393: RETURN_FALSE;
1394: }
1395: array_init(return_value);
1396:
1397: if (cert->name) {
1398: add_assoc_string(return_value, "name", cert->name, 1);
1399: }
1400: /* add_assoc_bool(return_value, "valid", cert->valid); */
1401:
1402: add_assoc_name_entry(return_value, "subject", X509_get_subject_name(cert), useshortnames TSRMLS_CC);
1403: /* hash as used in CA directories to lookup cert by subject name */
1404: {
1405: char buf[32];
1406: snprintf(buf, sizeof(buf), "%08lx", X509_subject_name_hash(cert));
1407: add_assoc_string(return_value, "hash", buf, 1);
1408: }
1.1.1.2 ! misho 1409:
1.1 misho 1410: add_assoc_name_entry(return_value, "issuer", X509_get_issuer_name(cert), useshortnames TSRMLS_CC);
1411: add_assoc_long(return_value, "version", X509_get_version(cert));
1412:
1.1.1.2 ! misho 1413: add_assoc_string(return_value, "serialNumber", i2s_ASN1_INTEGER(NULL, X509_get_serialNumber(cert)), 1);
1.1 misho 1414:
1415: add_assoc_asn1_string(return_value, "validFrom", X509_get_notBefore(cert));
1416: add_assoc_asn1_string(return_value, "validTo", X509_get_notAfter(cert));
1417:
1418: add_assoc_long(return_value, "validFrom_time_t", asn1_time_to_time_t(X509_get_notBefore(cert) TSRMLS_CC));
1419: add_assoc_long(return_value, "validTo_time_t", asn1_time_to_time_t(X509_get_notAfter(cert) TSRMLS_CC));
1420:
1421: tmpstr = (char *)X509_alias_get0(cert, NULL);
1422: if (tmpstr) {
1423: add_assoc_string(return_value, "alias", tmpstr, 1);
1424: }
1425: /*
1426: add_assoc_long(return_value, "signaturetypeLONG", X509_get_signature_type(cert));
1427: add_assoc_string(return_value, "signaturetype", OBJ_nid2sn(X509_get_signature_type(cert)), 1);
1428: add_assoc_string(return_value, "signaturetypeLN", OBJ_nid2ln(X509_get_signature_type(cert)), 1);
1429: */
1430: MAKE_STD_ZVAL(subitem);
1431: array_init(subitem);
1432:
1433: /* NOTE: the purposes are added as integer keys - the keys match up to the X509_PURPOSE_SSL_XXX defines
1434: in x509v3.h */
1435: for (i = 0; i < X509_PURPOSE_get_count(); i++) {
1436: int id, purpset;
1437: char * pname;
1438: X509_PURPOSE * purp;
1439: zval * subsub;
1440:
1441: MAKE_STD_ZVAL(subsub);
1442: array_init(subsub);
1443:
1444: purp = X509_PURPOSE_get0(i);
1445: id = X509_PURPOSE_get_id(purp);
1446:
1447: purpset = X509_check_purpose(cert, id, 0);
1448: add_index_bool(subsub, 0, purpset);
1449:
1450: purpset = X509_check_purpose(cert, id, 1);
1451: add_index_bool(subsub, 1, purpset);
1452:
1453: pname = useshortnames ? X509_PURPOSE_get0_sname(purp) : X509_PURPOSE_get0_name(purp);
1454: add_index_string(subsub, 2, pname, 1);
1455:
1456: /* NOTE: if purpset > 1 then it's a warning - we should mention it ? */
1457:
1458: add_index_zval(subitem, id, subsub);
1459: }
1460: add_assoc_zval(return_value, "purposes", subitem);
1461:
1462: MAKE_STD_ZVAL(subitem);
1463: array_init(subitem);
1464:
1465:
1466: for (i = 0; i < X509_get_ext_count(cert); i++) {
1467: extension = X509_get_ext(cert, i);
1468: if (OBJ_obj2nid(X509_EXTENSION_get_object(extension)) != NID_undef) {
1469: extname = (char *)OBJ_nid2sn(OBJ_obj2nid(X509_EXTENSION_get_object(extension)));
1470: } else {
1471: OBJ_obj2txt(buf, sizeof(buf)-1, X509_EXTENSION_get_object(extension), 1);
1472: extname = buf;
1473: }
1474: bio_out = BIO_new(BIO_s_mem());
1475: if (X509V3_EXT_print(bio_out, extension, 0, 0)) {
1476: BIO_get_mem_ptr(bio_out, &bio_buf);
1477: add_assoc_stringl(subitem, extname, bio_buf->data, bio_buf->length, 1);
1478: } else {
1479: add_assoc_asn1_string(subitem, extname, X509_EXTENSION_get_data(extension));
1480: }
1481: BIO_free(bio_out);
1482: }
1483: add_assoc_zval(return_value, "extensions", subitem);
1484:
1485: if (certresource == -1 && cert) {
1486: X509_free(cert);
1487: }
1488: }
1489: /* }}} */
1490:
1491: /* {{{ load_all_certs_from_file */
1492: static STACK_OF(X509) * load_all_certs_from_file(char *certfile)
1493: {
1494: STACK_OF(X509_INFO) *sk=NULL;
1495: STACK_OF(X509) *stack=NULL, *ret=NULL;
1496: BIO *in=NULL;
1497: X509_INFO *xi;
1498: TSRMLS_FETCH();
1499:
1500: if(!(stack = sk_X509_new_null())) {
1501: php_error_docref(NULL TSRMLS_CC, E_ERROR, "memory allocation failure");
1502: goto end;
1503: }
1504:
1.1.1.2 ! misho 1505: if (php_openssl_open_base_dir_chk(certfile TSRMLS_CC)) {
1.1 misho 1506: sk_X509_free(stack);
1507: goto end;
1508: }
1509:
1510: if(!(in=BIO_new_file(certfile, "r"))) {
1511: php_error_docref(NULL TSRMLS_CC, E_WARNING, "error opening the file, %s", certfile);
1512: sk_X509_free(stack);
1513: goto end;
1514: }
1515:
1516: /* This loads from a file, a stack of x509/crl/pkey sets */
1517: if(!(sk=PEM_X509_INFO_read_bio(in, NULL, NULL, NULL))) {
1518: php_error_docref(NULL TSRMLS_CC, E_WARNING, "error reading the file, %s", certfile);
1519: sk_X509_free(stack);
1520: goto end;
1521: }
1522:
1523: /* scan over it and pull out the certs */
1524: while (sk_X509_INFO_num(sk)) {
1525: xi=sk_X509_INFO_shift(sk);
1526: if (xi->x509 != NULL) {
1527: sk_X509_push(stack,xi->x509);
1528: xi->x509=NULL;
1529: }
1530: X509_INFO_free(xi);
1531: }
1532: if(!sk_X509_num(stack)) {
1533: php_error_docref(NULL TSRMLS_CC, E_WARNING, "no certificates in file, %s", certfile);
1534: sk_X509_free(stack);
1535: goto end;
1536: }
1537: ret=stack;
1538: end:
1539: BIO_free(in);
1540: sk_X509_INFO_free(sk);
1541:
1542: return ret;
1543: }
1544: /* }}} */
1545:
1546: /* {{{ check_cert */
1547: static int check_cert(X509_STORE *ctx, X509 *x, STACK_OF(X509) *untrustedchain, int purpose)
1548: {
1549: int ret=0;
1550: X509_STORE_CTX *csc;
1551: TSRMLS_FETCH();
1552:
1553: csc = X509_STORE_CTX_new();
1554: if (csc == NULL) {
1555: php_error_docref(NULL TSRMLS_CC, E_ERROR, "memory allocation failure");
1556: return 0;
1557: }
1558: X509_STORE_CTX_init(csc, ctx, x, untrustedchain);
1559: if(purpose >= 0) {
1560: X509_STORE_CTX_set_purpose(csc, purpose);
1561: }
1562: ret = X509_verify_cert(csc);
1563: X509_STORE_CTX_free(csc);
1564:
1565: return ret;
1566: }
1567: /* }}} */
1568:
1569: /* {{{ proto int openssl_x509_checkpurpose(mixed x509cert, int purpose, array cainfo [, string untrustedfile])
1570: Checks the CERT to see if it can be used for the purpose in purpose. cainfo holds information about trusted CAs */
1571: PHP_FUNCTION(openssl_x509_checkpurpose)
1572: {
1573: zval ** zcert, * zcainfo = NULL;
1574: X509_STORE * cainfo = NULL;
1575: X509 * cert = NULL;
1576: long certresource = -1;
1577: STACK_OF(X509) * untrustedchain = NULL;
1578: long purpose;
1579: char * untrusted = NULL;
1580: int untrusted_len = 0, ret;
1581:
1582: if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "Zl|a!s", &zcert, &purpose, &zcainfo, &untrusted, &untrusted_len) == FAILURE) {
1583: return;
1584: }
1585:
1586: RETVAL_LONG(-1);
1587:
1588: if (untrusted) {
1589: untrustedchain = load_all_certs_from_file(untrusted);
1590: if (untrustedchain == NULL) {
1591: goto clean_exit;
1592: }
1593: }
1594:
1595: cainfo = setup_verify(zcainfo TSRMLS_CC);
1596: if (cainfo == NULL) {
1597: goto clean_exit;
1598: }
1599: cert = php_openssl_x509_from_zval(zcert, 0, &certresource TSRMLS_CC);
1600: if (cert == NULL) {
1601: goto clean_exit;
1602: }
1603:
1604: ret = check_cert(cainfo, cert, untrustedchain, purpose);
1605: if (ret != 0 && ret != 1) {
1606: RETVAL_LONG(ret);
1607: } else {
1608: RETVAL_BOOL(ret);
1609: }
1610:
1611: clean_exit:
1612: if (certresource == 1 && cert) {
1613: X509_free(cert);
1614: }
1.1.1.2 ! misho 1615: if (cainfo) {
! 1616: X509_STORE_free(cainfo);
1.1 misho 1617: }
1618: if (untrustedchain) {
1619: sk_X509_pop_free(untrustedchain, X509_free);
1620: }
1621: }
1622: /* }}} */
1623:
1624: /* {{{ setup_verify
1625: * calist is an array containing file and directory names. create a
1626: * certificate store and add those certs to it for use in verification.
1627: */
1628: static X509_STORE * setup_verify(zval * calist TSRMLS_DC)
1629: {
1630: X509_STORE *store;
1631: X509_LOOKUP * dir_lookup, * file_lookup;
1632: HashPosition pos;
1633: int ndirs = 0, nfiles = 0;
1634:
1635: store = X509_STORE_new();
1636:
1637: if (store == NULL) {
1638: return NULL;
1639: }
1640:
1641: if (calist && (Z_TYPE_P(calist) == IS_ARRAY)) {
1642: zend_hash_internal_pointer_reset_ex(HASH_OF(calist), &pos);
1643: for (;; zend_hash_move_forward_ex(HASH_OF(calist), &pos)) {
1644: zval ** item;
1645: struct stat sb;
1646:
1647: if (zend_hash_get_current_data_ex(HASH_OF(calist), (void**)&item, &pos) == FAILURE) {
1648: break;
1649: }
1650: convert_to_string_ex(item);
1651:
1652: if (VCWD_STAT(Z_STRVAL_PP(item), &sb) == -1) {
1653: php_error_docref(NULL TSRMLS_CC, E_WARNING, "unable to stat %s", Z_STRVAL_PP(item));
1654: continue;
1655: }
1656:
1657: if ((sb.st_mode & S_IFREG) == S_IFREG) {
1658: file_lookup = X509_STORE_add_lookup(store, X509_LOOKUP_file());
1659: if (file_lookup == NULL || !X509_LOOKUP_load_file(file_lookup, Z_STRVAL_PP(item), X509_FILETYPE_PEM)) {
1660: php_error_docref(NULL TSRMLS_CC, E_WARNING, "error loading file %s", Z_STRVAL_PP(item));
1661: } else {
1662: nfiles++;
1663: }
1664: file_lookup = NULL;
1665: } else {
1666: dir_lookup = X509_STORE_add_lookup(store, X509_LOOKUP_hash_dir());
1667: if (dir_lookup == NULL || !X509_LOOKUP_add_dir(dir_lookup, Z_STRVAL_PP(item), X509_FILETYPE_PEM)) {
1668: php_error_docref(NULL TSRMLS_CC, E_WARNING, "error loading directory %s", Z_STRVAL_PP(item));
1.1.1.2 ! misho 1669: } else {
1.1 misho 1670: ndirs++;
1671: }
1672: dir_lookup = NULL;
1673: }
1674: }
1675: }
1676: if (nfiles == 0) {
1677: file_lookup = X509_STORE_add_lookup(store, X509_LOOKUP_file());
1678: if (file_lookup) {
1679: X509_LOOKUP_load_file(file_lookup, NULL, X509_FILETYPE_DEFAULT);
1680: }
1681: }
1682: if (ndirs == 0) {
1683: dir_lookup = X509_STORE_add_lookup(store, X509_LOOKUP_hash_dir());
1684: if (dir_lookup) {
1685: X509_LOOKUP_add_dir(dir_lookup, NULL, X509_FILETYPE_DEFAULT);
1686: }
1687: }
1688: return store;
1689: }
1690: /* }}} */
1691:
1692: /* {{{ proto resource openssl_x509_read(mixed cert)
1693: Reads X.509 certificates */
1694: PHP_FUNCTION(openssl_x509_read)
1695: {
1696: zval **cert;
1697: X509 *x509;
1698:
1699: if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "Z", &cert) == FAILURE) {
1700: return;
1701: }
1702: Z_TYPE_P(return_value) = IS_RESOURCE;
1703: x509 = php_openssl_x509_from_zval(cert, 1, &Z_LVAL_P(return_value) TSRMLS_CC);
1704:
1705: if (x509 == NULL) {
1706: php_error_docref(NULL TSRMLS_CC, E_WARNING, "supplied parameter cannot be coerced into an X509 certificate!");
1707: RETURN_FALSE;
1708: }
1709: }
1710: /* }}} */
1711:
1712: /* {{{ proto void openssl_x509_free(resource x509)
1713: Frees X.509 certificates */
1714: PHP_FUNCTION(openssl_x509_free)
1715: {
1716: zval *x509;
1717: X509 *cert;
1718:
1719: if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &x509) == FAILURE) {
1720: return;
1721: }
1722: ZEND_FETCH_RESOURCE(cert, X509 *, &x509, -1, "OpenSSL X.509", le_x509);
1723: zend_list_delete(Z_LVAL_P(x509));
1724: }
1725: /* }}} */
1726:
1727: /* }}} */
1728:
1729: /* Pop all X509 from Stack and free them, free the stack afterwards */
1730: static void php_sk_X509_free(STACK_OF(X509) * sk) /* {{{ */
1731: {
1732: for (;;) {
1733: X509* x = sk_X509_pop(sk);
1734: if (!x) break;
1735: X509_free(x);
1736: }
1737: sk_X509_free(sk);
1738: }
1739: /* }}} */
1740:
1741: static STACK_OF(X509) * php_array_to_X509_sk(zval ** zcerts TSRMLS_DC) /* {{{ */
1742: {
1743: HashPosition hpos;
1744: zval ** zcertval;
1745: STACK_OF(X509) * sk = NULL;
1746: X509 * cert;
1747: long certresource;
1748:
1749: sk = sk_X509_new_null();
1750:
1751: /* get certs */
1752: if (Z_TYPE_PP(zcerts) == IS_ARRAY) {
1753: zend_hash_internal_pointer_reset_ex(HASH_OF(*zcerts), &hpos);
1754: while(zend_hash_get_current_data_ex(HASH_OF(*zcerts), (void**)&zcertval, &hpos) == SUCCESS) {
1755:
1756: cert = php_openssl_x509_from_zval(zcertval, 0, &certresource TSRMLS_CC);
1757: if (cert == NULL) {
1758: goto clean_exit;
1759: }
1760:
1761: if (certresource != -1) {
1762: cert = X509_dup(cert);
1.1.1.2 ! misho 1763:
1.1 misho 1764: if (cert == NULL) {
1765: goto clean_exit;
1766: }
1.1.1.2 ! misho 1767:
1.1 misho 1768: }
1769: sk_X509_push(sk, cert);
1770:
1771: zend_hash_move_forward_ex(HASH_OF(*zcerts), &hpos);
1772: }
1773: } else {
1774: /* a single certificate */
1775: cert = php_openssl_x509_from_zval(zcerts, 0, &certresource TSRMLS_CC);
1.1.1.2 ! misho 1776:
1.1 misho 1777: if (cert == NULL) {
1778: goto clean_exit;
1779: }
1780:
1781: if (certresource != -1) {
1782: cert = X509_dup(cert);
1783: if (cert == NULL) {
1784: goto clean_exit;
1785: }
1786: }
1787: sk_X509_push(sk, cert);
1788: }
1789:
1790: clean_exit:
1791: return sk;
1792: }
1793: /* }}} */
1794:
1795: /* {{{ proto bool openssl_pkcs12_export_to_file(mixed x509, string filename, mixed priv_key, string pass[, array args])
1796: Creates and exports a PKCS to file */
1797: PHP_FUNCTION(openssl_pkcs12_export_to_file)
1798: {
1799: X509 * cert = NULL;
1800: BIO * bio_out = NULL;
1801: PKCS12 * p12 = NULL;
1802: char * filename;
1803: char * friendly_name = NULL;
1804: int filename_len;
1805: char * pass;
1806: int pass_len;
1807: zval **zcert = NULL, *zpkey = NULL, *args = NULL;
1808: EVP_PKEY *priv_key = NULL;
1809: long certresource, keyresource;
1810: zval ** item;
1811: STACK_OF(X509) *ca = NULL;
1812:
1.1.1.2 ! misho 1813: if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "Zpzs|a", &zcert, &filename, &filename_len, &zpkey, &pass, &pass_len, &args) == FAILURE)
1.1 misho 1814: return;
1815:
1816: RETVAL_FALSE;
1817:
1818: cert = php_openssl_x509_from_zval(zcert, 0, &certresource TSRMLS_CC);
1819: if (cert == NULL) {
1820: php_error_docref(NULL TSRMLS_CC, E_WARNING, "cannot get cert from parameter 1");
1821: return;
1822: }
1823: priv_key = php_openssl_evp_from_zval(&zpkey, 0, "", 1, &keyresource TSRMLS_CC);
1824: if (priv_key == NULL) {
1825: php_error_docref(NULL TSRMLS_CC, E_WARNING, "cannot get private key from parameter 3");
1826: goto cleanup;
1827: }
1828: if (cert && !X509_check_private_key(cert, priv_key)) {
1829: php_error_docref(NULL TSRMLS_CC, E_WARNING, "private key does not correspond to cert");
1830: goto cleanup;
1831: }
1.1.1.2 ! misho 1832: if (php_openssl_open_base_dir_chk(filename TSRMLS_CC)) {
1.1 misho 1833: goto cleanup;
1834: }
1835:
1836: /* parse extra config from args array, promote this to an extra function */
1837: if (args && zend_hash_find(Z_ARRVAL_P(args), "friendly_name", sizeof("friendly_name"), (void**)&item) == SUCCESS)
1838: friendly_name = Z_STRVAL_PP(item);
1839: /* certpbe (default RC2-40)
1840: keypbe (default 3DES)
1841: friendly_caname
1842: */
1843:
1844: if (args && zend_hash_find(Z_ARRVAL_P(args), "extracerts", sizeof("extracerts"), (void**)&item) == SUCCESS)
1845: ca = php_array_to_X509_sk(item TSRMLS_CC);
1846: /* end parse extra config */
1847:
1848: /*PKCS12 *PKCS12_create(char *pass, char *name, EVP_PKEY *pkey, X509 *cert, STACK_OF(X509) *ca,
1849: int nid_key, int nid_cert, int iter, int mac_iter, int keytype);*/
1850:
1851: p12 = PKCS12_create(pass, friendly_name, priv_key, cert, ca, 0, 0, 0, 0, 0);
1852:
1.1.1.2 ! misho 1853: bio_out = BIO_new_file(filename, "w");
1.1 misho 1854: if (bio_out) {
1.1.1.2 ! misho 1855:
1.1 misho 1856: i2d_PKCS12_bio(bio_out, p12);
1857:
1858: RETVAL_TRUE;
1859: } else {
1860: php_error_docref(NULL TSRMLS_CC, E_WARNING, "error opening file %s", filename);
1861: }
1862:
1863: BIO_free(bio_out);
1864: PKCS12_free(p12);
1865: php_sk_X509_free(ca);
1.1.1.2 ! misho 1866:
1.1 misho 1867: cleanup:
1868:
1869: if (keyresource == -1 && priv_key) {
1870: EVP_PKEY_free(priv_key);
1871: }
1.1.1.2 ! misho 1872: if (certresource == -1 && cert) {
1.1 misho 1873: X509_free(cert);
1874: }
1875: }
1876: /* }}} */
1877:
1878: /* {{{ proto bool openssl_pkcs12_export(mixed x509, string &out, mixed priv_key, string pass[, array args])
1879: Creates and exports a PKCS12 to a var */
1880: PHP_FUNCTION(openssl_pkcs12_export)
1881: {
1882: X509 * cert = NULL;
1883: BIO * bio_out;
1884: PKCS12 * p12 = NULL;
1885: zval * zcert = NULL, *zout = NULL, *zpkey, *args = NULL;
1886: EVP_PKEY *priv_key = NULL;
1887: long certresource, keyresource;
1888: char * pass;
1889: int pass_len;
1890: char * friendly_name = NULL;
1891: zval ** item;
1892: STACK_OF(X509) *ca = NULL;
1893:
1894: if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "zzzs|a", &zcert, &zout, &zpkey, &pass, &pass_len, &args) == FAILURE)
1895: return;
1896:
1897: RETVAL_FALSE;
1.1.1.2 ! misho 1898:
1.1 misho 1899: cert = php_openssl_x509_from_zval(&zcert, 0, &certresource TSRMLS_CC);
1900: if (cert == NULL) {
1901: php_error_docref(NULL TSRMLS_CC, E_WARNING, "cannot get cert from parameter 1");
1902: return;
1903: }
1904: priv_key = php_openssl_evp_from_zval(&zpkey, 0, "", 1, &keyresource TSRMLS_CC);
1905: if (priv_key == NULL) {
1906: php_error_docref(NULL TSRMLS_CC, E_WARNING, "cannot get private key from parameter 3");
1907: goto cleanup;
1908: }
1909: if (cert && !X509_check_private_key(cert, priv_key)) {
1910: php_error_docref(NULL TSRMLS_CC, E_WARNING, "private key does not correspond to cert");
1911: goto cleanup;
1912: }
1913:
1914: /* parse extra config from args array, promote this to an extra function */
1915: if (args && zend_hash_find(Z_ARRVAL_P(args), "friendly_name", sizeof("friendly_name"), (void**)&item) == SUCCESS)
1916: friendly_name = Z_STRVAL_PP(item);
1917:
1918: if (args && zend_hash_find(Z_ARRVAL_P(args), "extracerts", sizeof("extracerts"), (void**)&item) == SUCCESS)
1919: ca = php_array_to_X509_sk(item TSRMLS_CC);
1920: /* end parse extra config */
1.1.1.2 ! misho 1921:
1.1 misho 1922: p12 = PKCS12_create(pass, friendly_name, priv_key, cert, ca, 0, 0, 0, 0, 0);
1923:
1924: bio_out = BIO_new(BIO_s_mem());
1925: if (i2d_PKCS12_bio(bio_out, p12)) {
1926: BUF_MEM *bio_buf;
1927:
1928: zval_dtor(zout);
1929: BIO_get_mem_ptr(bio_out, &bio_buf);
1930: ZVAL_STRINGL(zout, bio_buf->data, bio_buf->length, 1);
1931:
1932: RETVAL_TRUE;
1933: }
1934:
1935: BIO_free(bio_out);
1936: PKCS12_free(p12);
1937: php_sk_X509_free(ca);
1.1.1.2 ! misho 1938:
1.1 misho 1939: cleanup:
1940:
1941: if (keyresource == -1 && priv_key) {
1942: EVP_PKEY_free(priv_key);
1943: }
1.1.1.2 ! misho 1944: if (certresource == -1 && cert) {
1.1 misho 1945: X509_free(cert);
1946: }
1947: }
1948: /* }}} */
1949:
1950: /* {{{ proto bool openssl_pkcs12_read(string PKCS12, array &certs, string pass)
1951: Parses a PKCS12 to an array */
1952: PHP_FUNCTION(openssl_pkcs12_read)
1953: {
1954: zval *zout = NULL, *zextracerts, *zcert, *zpkey;
1955: char *pass, *zp12;
1956: int pass_len, zp12_len;
1957: PKCS12 * p12 = NULL;
1958: EVP_PKEY * pkey = NULL;
1959: X509 * cert = NULL;
1960: STACK_OF(X509) * ca = NULL;
1961: BIO * bio_in = NULL;
1962: int i;
1963:
1964: if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "szs", &zp12, &zp12_len, &zout, &pass, &pass_len) == FAILURE)
1965: return;
1966:
1967: RETVAL_FALSE;
1.1.1.2 ! misho 1968:
1.1 misho 1969: bio_in = BIO_new(BIO_s_mem());
1.1.1.2 ! misho 1970:
1.1 misho 1971: if(!BIO_write(bio_in, zp12, zp12_len))
1972: goto cleanup;
1.1.1.2 ! misho 1973:
1.1 misho 1974: if(d2i_PKCS12_bio(bio_in, &p12)) {
1975: if(PKCS12_parse(p12, pass, &pkey, &cert, &ca)) {
1976: BIO * bio_out;
1977:
1978: zval_dtor(zout);
1979: array_init(zout);
1980:
1981: bio_out = BIO_new(BIO_s_mem());
1982: if (PEM_write_bio_X509(bio_out, cert)) {
1983: BUF_MEM *bio_buf;
1984: BIO_get_mem_ptr(bio_out, &bio_buf);
1985: MAKE_STD_ZVAL(zcert);
1986: ZVAL_STRINGL(zcert, bio_buf->data, bio_buf->length, 1);
1987: add_assoc_zval(zout, "cert", zcert);
1988: }
1989: BIO_free(bio_out);
1990:
1991: bio_out = BIO_new(BIO_s_mem());
1992: if (PEM_write_bio_PrivateKey(bio_out, pkey, NULL, NULL, 0, 0, NULL)) {
1993: BUF_MEM *bio_buf;
1994: BIO_get_mem_ptr(bio_out, &bio_buf);
1995: MAKE_STD_ZVAL(zpkey);
1996: ZVAL_STRINGL(zpkey, bio_buf->data, bio_buf->length, 1);
1997: add_assoc_zval(zout, "pkey", zpkey);
1998: }
1999: BIO_free(bio_out);
2000:
2001: MAKE_STD_ZVAL(zextracerts);
2002: array_init(zextracerts);
1.1.1.2 ! misho 2003:
1.1 misho 2004: for (i=0;;i++) {
2005: zval * zextracert;
2006: X509* aCA = sk_X509_pop(ca);
2007: if (!aCA) break;
1.1.1.2 ! misho 2008:
1.1 misho 2009: bio_out = BIO_new(BIO_s_mem());
2010: if (PEM_write_bio_X509(bio_out, aCA)) {
2011: BUF_MEM *bio_buf;
2012: BIO_get_mem_ptr(bio_out, &bio_buf);
2013: MAKE_STD_ZVAL(zextracert);
2014: ZVAL_STRINGL(zextracert, bio_buf->data, bio_buf->length, 1);
2015: add_index_zval(zextracerts, i, zextracert);
1.1.1.2 ! misho 2016:
1.1 misho 2017: }
2018: BIO_free(bio_out);
2019:
2020: X509_free(aCA);
2021: }
2022: if(ca) {
2023: sk_X509_free(ca);
2024: add_assoc_zval(zout, "extracerts", zextracerts);
2025: } else {
2026: zval_dtor(zextracerts);
2027: }
1.1.1.2 ! misho 2028:
1.1 misho 2029: RETVAL_TRUE;
1.1.1.2 ! misho 2030:
1.1 misho 2031: PKCS12_free(p12);
2032: }
2033: }
1.1.1.2 ! misho 2034:
1.1 misho 2035: cleanup:
2036: if (bio_in) {
2037: BIO_free(bio_in);
2038: }
2039: if (pkey) {
2040: EVP_PKEY_free(pkey);
2041: }
1.1.1.2 ! misho 2042: if (cert) {
1.1 misho 2043: X509_free(cert);
2044: }
2045: }
2046: /* }}} */
2047:
2048: /* {{{ x509 CSR functions */
2049:
2050: /* {{{ php_openssl_make_REQ */
2051: static int php_openssl_make_REQ(struct php_x509_request * req, X509_REQ * csr, zval * dn, zval * attribs TSRMLS_DC)
2052: {
2053: STACK_OF(CONF_VALUE) * dn_sk, *attr_sk = NULL;
2054: char * str, *dn_sect, *attr_sect;
2055:
2056: dn_sect = CONF_get_string(req->req_config, req->section_name, "distinguished_name");
2057: if (dn_sect == NULL) {
2058: return FAILURE;
2059: }
2060: dn_sk = CONF_get_section(req->req_config, dn_sect);
1.1.1.2 ! misho 2061: if (dn_sk == NULL) {
1.1 misho 2062: return FAILURE;
2063: }
2064: attr_sect = CONF_get_string(req->req_config, req->section_name, "attributes");
2065: if (attr_sect == NULL) {
2066: attr_sk = NULL;
2067: } else {
2068: attr_sk = CONF_get_section(req->req_config, attr_sect);
2069: if (attr_sk == NULL) {
2070: return FAILURE;
2071: }
2072: }
2073: /* setup the version number: version 1 */
2074: if (X509_REQ_set_version(csr, 0L)) {
2075: int i, nid;
2076: char * type;
2077: CONF_VALUE * v;
2078: X509_NAME * subj;
2079: HashPosition hpos;
2080: zval ** item;
1.1.1.2 ! misho 2081:
1.1 misho 2082: subj = X509_REQ_get_subject_name(csr);
2083: /* apply values from the dn hash */
2084: zend_hash_internal_pointer_reset_ex(HASH_OF(dn), &hpos);
2085: while(zend_hash_get_current_data_ex(HASH_OF(dn), (void**)&item, &hpos) == SUCCESS) {
1.1.1.2 ! misho 2086: char * strindex = NULL;
1.1 misho 2087: uint strindexlen = 0;
2088: ulong intindex;
1.1.1.2 ! misho 2089:
1.1 misho 2090: zend_hash_get_current_key_ex(HASH_OF(dn), &strindex, &strindexlen, &intindex, 0, &hpos);
2091:
2092: convert_to_string_ex(item);
2093:
2094: if (strindex) {
2095: int nid;
2096:
2097: nid = OBJ_txt2nid(strindex);
2098: if (nid != NID_undef) {
1.1.1.2 ! misho 2099: if (!X509_NAME_add_entry_by_NID(subj, nid, MBSTRING_UTF8,
1.1 misho 2100: (unsigned char*)Z_STRVAL_PP(item), -1, -1, 0))
2101: {
1.1.1.2 ! misho 2102: php_error_docref(NULL TSRMLS_CC, E_WARNING,
! 2103: "dn: add_entry_by_NID %d -> %s (failed; check error"
! 2104: " queue and value of string_mask OpenSSL option "
! 2105: "if illegal characters are reported)",
! 2106: nid, Z_STRVAL_PP(item));
1.1 misho 2107: return FAILURE;
2108: }
2109: } else {
2110: php_error_docref(NULL TSRMLS_CC, E_WARNING, "dn: %s is not a recognized name", strindex);
2111: }
2112: }
2113: zend_hash_move_forward_ex(HASH_OF(dn), &hpos);
2114: }
2115:
2116: /* Finally apply defaults from config file */
2117: for(i = 0; i < sk_CONF_VALUE_num(dn_sk); i++) {
2118: int len;
2119: char buffer[200 + 1]; /*200 + \0 !*/
1.1.1.2 ! misho 2120:
1.1 misho 2121: v = sk_CONF_VALUE_value(dn_sk, i);
2122: type = v->name;
1.1.1.2 ! misho 2123:
1.1 misho 2124: len = strlen(type);
2125: if (len < sizeof("_default")) {
2126: continue;
2127: }
2128: len -= sizeof("_default") - 1;
2129: if (strcmp("_default", type + len) != 0) {
2130: continue;
2131: }
2132: if (len > 200) {
2133: len = 200;
2134: }
2135: memcpy(buffer, type, len);
2136: buffer[len] = '\0';
2137: type = buffer;
1.1.1.2 ! misho 2138:
1.1 misho 2139: /* Skip past any leading X. X: X, etc to allow for multiple
2140: * instances */
2141: for (str = type; *str; str++) {
2142: if (*str == ':' || *str == ',' || *str == '.') {
2143: str++;
2144: if (*str) {
2145: type = str;
2146: }
2147: break;
2148: }
2149: }
2150: /* if it is already set, skip this */
2151: nid = OBJ_txt2nid(type);
2152: if (X509_NAME_get_index_by_NID(subj, nid, -1) >= 0) {
2153: continue;
2154: }
1.1.1.2 ! misho 2155: if (!X509_NAME_add_entry_by_txt(subj, type, MBSTRING_UTF8, (unsigned char*)v->value, -1, -1, 0)) {
1.1 misho 2156: php_error_docref(NULL TSRMLS_CC, E_WARNING, "add_entry_by_txt %s -> %s (failed)", type, v->value);
2157: return FAILURE;
2158: }
2159: if (!X509_NAME_entry_count(subj)) {
2160: php_error_docref(NULL TSRMLS_CC, E_WARNING, "no objects specified in config file");
2161: return FAILURE;
2162: }
2163: }
2164: if (attribs) {
2165: zend_hash_internal_pointer_reset_ex(HASH_OF(attribs), &hpos);
2166: while(zend_hash_get_current_data_ex(HASH_OF(attribs), (void**)&item, &hpos) == SUCCESS) {
2167: char *strindex = NULL;
2168: uint strindexlen;
2169: ulong intindex;
2170:
2171: zend_hash_get_current_key_ex(HASH_OF(attribs), &strindex, &strindexlen, &intindex, 0, &hpos);
2172: convert_to_string_ex(item);
2173:
2174: if (strindex) {
2175: int nid;
2176:
2177: nid = OBJ_txt2nid(strindex);
2178: if (nid != NID_undef) {
1.1.1.2 ! misho 2179: if (!X509_NAME_add_entry_by_NID(subj, nid, MBSTRING_UTF8, (unsigned char*)Z_STRVAL_PP(item), -1, -1, 0)) {
1.1 misho 2180: php_error_docref(NULL TSRMLS_CC, E_WARNING, "attribs: add_entry_by_NID %d -> %s (failed)", nid, Z_STRVAL_PP(item));
2181: return FAILURE;
2182: }
2183: } else {
2184: php_error_docref(NULL TSRMLS_CC, E_WARNING, "dn: %s is not a recognized name", strindex);
2185: }
2186: }
2187: zend_hash_move_forward_ex(HASH_OF(attribs), &hpos);
2188: }
2189: for (i = 0; i < sk_CONF_VALUE_num(attr_sk); i++) {
2190: v = sk_CONF_VALUE_value(attr_sk, i);
2191: /* if it is already set, skip this */
2192: nid = OBJ_txt2nid(v->name);
2193: if (X509_REQ_get_attr_by_NID(csr, nid, -1) >= 0) {
2194: continue;
2195: }
1.1.1.2 ! misho 2196: if (!X509_REQ_add1_attr_by_txt(csr, v->name, MBSTRING_UTF8, (unsigned char*)v->value, -1)) {
! 2197: php_error_docref(NULL TSRMLS_CC, E_WARNING,
! 2198: "add1_attr_by_txt %s -> %s (failed; check error queue "
! 2199: "and value of string_mask OpenSSL option if illegal "
! 2200: "characters are reported)",
! 2201: v->name, v->value);
1.1 misho 2202: return FAILURE;
2203: }
2204: }
2205: }
2206: }
2207:
2208: X509_REQ_set_pubkey(csr, req->priv_key);
2209: return SUCCESS;
2210: }
2211: /* }}} */
2212:
2213: /* {{{ php_openssl_csr_from_zval */
2214: static X509_REQ * php_openssl_csr_from_zval(zval ** val, int makeresource, long * resourceval TSRMLS_DC)
2215: {
2216: X509_REQ * csr = NULL;
2217: char * filename = NULL;
2218: BIO * in;
1.1.1.2 ! misho 2219:
1.1 misho 2220: if (resourceval) {
2221: *resourceval = -1;
2222: }
2223: if (Z_TYPE_PP(val) == IS_RESOURCE) {
2224: void * what;
2225: int type;
2226:
2227: what = zend_fetch_resource(val TSRMLS_CC, -1, "OpenSSL X.509 CSR", &type, 1, le_csr);
2228: if (what) {
2229: if (resourceval) {
2230: *resourceval = Z_LVAL_PP(val);
2231: }
2232: return (X509_REQ*)what;
2233: }
2234: return NULL;
2235: } else if (Z_TYPE_PP(val) != IS_STRING) {
2236: return NULL;
2237: }
2238:
2239: if (Z_STRLEN_PP(val) > 7 && memcmp(Z_STRVAL_PP(val), "file://", sizeof("file://") - 1) == 0) {
2240: filename = Z_STRVAL_PP(val) + (sizeof("file://") - 1);
2241: }
2242: if (filename) {
1.1.1.2 ! misho 2243: if (php_openssl_open_base_dir_chk(filename TSRMLS_CC)) {
1.1 misho 2244: return NULL;
2245: }
2246: in = BIO_new_file(filename, "r");
2247: } else {
2248: in = BIO_new_mem_buf(Z_STRVAL_PP(val), Z_STRLEN_PP(val));
2249: }
2250: csr = PEM_read_bio_X509_REQ(in, NULL,NULL,NULL);
2251: BIO_free(in);
2252:
2253: return csr;
2254: }
2255: /* }}} */
2256:
2257: /* {{{ proto bool openssl_csr_export_to_file(resource csr, string outfilename [, bool notext=true])
2258: Exports a CSR to file */
2259: PHP_FUNCTION(openssl_csr_export_to_file)
2260: {
2261: X509_REQ * csr;
2262: zval * zcsr = NULL;
2263: zend_bool notext = 1;
2264: char * filename = NULL; int filename_len;
2265: BIO * bio_out;
2266: long csr_resource;
2267:
1.1.1.2 ! misho 2268: if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rp|b", &zcsr, &filename, &filename_len, ¬ext) == FAILURE) {
1.1 misho 2269: return;
2270: }
2271: RETVAL_FALSE;
2272:
2273: csr = php_openssl_csr_from_zval(&zcsr, 0, &csr_resource TSRMLS_CC);
2274: if (csr == NULL) {
2275: php_error_docref(NULL TSRMLS_CC, E_WARNING, "cannot get CSR from parameter 1");
2276: return;
2277: }
2278:
1.1.1.2 ! misho 2279: if (php_openssl_open_base_dir_chk(filename TSRMLS_CC)) {
1.1 misho 2280: return;
2281: }
2282:
2283: bio_out = BIO_new_file(filename, "w");
2284: if (bio_out) {
2285: if (!notext) {
2286: X509_REQ_print(bio_out, csr);
2287: }
2288: PEM_write_bio_X509_REQ(bio_out, csr);
2289: RETVAL_TRUE;
2290: } else {
2291: php_error_docref(NULL TSRMLS_CC, E_WARNING, "error opening file %s", filename);
2292: }
2293:
2294: if (csr_resource == -1 && csr) {
2295: X509_REQ_free(csr);
2296: }
2297: BIO_free(bio_out);
2298: }
2299: /* }}} */
2300:
2301: /* {{{ proto bool openssl_csr_export(resource csr, string &out [, bool notext=true])
2302: Exports a CSR to file or a var */
2303: PHP_FUNCTION(openssl_csr_export)
2304: {
2305: X509_REQ * csr;
2306: zval * zcsr = NULL, *zout=NULL;
2307: zend_bool notext = 1;
2308: BIO * bio_out;
2309:
2310: long csr_resource;
2311:
2312: if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rz|b", &zcsr, &zout, ¬ext) == FAILURE) {
2313: return;
2314: }
2315: RETVAL_FALSE;
2316:
2317: csr = php_openssl_csr_from_zval(&zcsr, 0, &csr_resource TSRMLS_CC);
2318: if (csr == NULL) {
2319: php_error_docref(NULL TSRMLS_CC, E_WARNING, "cannot get CSR from parameter 1");
2320: return;
2321: }
2322:
2323: /* export to a var */
2324:
2325: bio_out = BIO_new(BIO_s_mem());
2326: if (!notext) {
2327: X509_REQ_print(bio_out, csr);
2328: }
2329:
2330: if (PEM_write_bio_X509_REQ(bio_out, csr)) {
2331: BUF_MEM *bio_buf;
2332:
2333: BIO_get_mem_ptr(bio_out, &bio_buf);
2334: zval_dtor(zout);
2335: ZVAL_STRINGL(zout, bio_buf->data, bio_buf->length, 1);
2336:
2337: RETVAL_TRUE;
2338: }
2339:
2340: if (csr_resource == -1 && csr) {
2341: X509_REQ_free(csr);
2342: }
2343: BIO_free(bio_out);
2344: }
2345: /* }}} */
2346:
2347: /* {{{ proto resource openssl_csr_sign(mixed csr, mixed x509, mixed priv_key, long days [, array config_args [, long serial]])
2348: Signs a cert with another CERT */
2349: PHP_FUNCTION(openssl_csr_sign)
2350: {
2351: zval ** zcert = NULL, **zcsr, **zpkey, *args = NULL;
2352: long num_days;
2353: long serial = 0L;
2354: X509 * cert = NULL, *new_cert = NULL;
2355: X509_REQ * csr;
2356: EVP_PKEY * key = NULL, *priv_key = NULL;
2357: long csr_resource, certresource = 0, keyresource = -1;
2358: int i;
2359: struct php_x509_request req;
1.1.1.2 ! misho 2360:
1.1 misho 2361: if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ZZ!Zl|a!l", &zcsr, &zcert, &zpkey, &num_days, &args, &serial) == FAILURE)
2362: return;
2363:
2364: RETVAL_FALSE;
2365: PHP_SSL_REQ_INIT(&req);
1.1.1.2 ! misho 2366:
1.1 misho 2367: csr = php_openssl_csr_from_zval(zcsr, 0, &csr_resource TSRMLS_CC);
2368: if (csr == NULL) {
2369: php_error_docref(NULL TSRMLS_CC, E_WARNING, "cannot get CSR from parameter 1");
2370: return;
2371: }
2372: if (zcert) {
2373: cert = php_openssl_x509_from_zval(zcert, 0, &certresource TSRMLS_CC);
2374: if (cert == NULL) {
2375: php_error_docref(NULL TSRMLS_CC, E_WARNING, "cannot get cert from parameter 2");
2376: goto cleanup;
2377: }
2378: }
2379: priv_key = php_openssl_evp_from_zval(zpkey, 0, "", 1, &keyresource TSRMLS_CC);
2380: if (priv_key == NULL) {
2381: php_error_docref(NULL TSRMLS_CC, E_WARNING, "cannot get private key from parameter 3");
2382: goto cleanup;
2383: }
2384: if (cert && !X509_check_private_key(cert, priv_key)) {
2385: php_error_docref(NULL TSRMLS_CC, E_WARNING, "private key does not correspond to signing cert");
2386: goto cleanup;
2387: }
1.1.1.2 ! misho 2388:
1.1 misho 2389: if (PHP_SSL_REQ_PARSE(&req, args) == FAILURE) {
2390: goto cleanup;
2391: }
2392: /* Check that the request matches the signature */
2393: key = X509_REQ_get_pubkey(csr);
2394: if (key == NULL) {
2395: php_error_docref(NULL TSRMLS_CC, E_WARNING, "error unpacking public key");
2396: goto cleanup;
2397: }
2398: i = X509_REQ_verify(csr, key);
2399:
2400: if (i < 0) {
2401: php_error_docref(NULL TSRMLS_CC, E_WARNING, "Signature verification problems");
2402: goto cleanup;
2403: }
2404: else if (i == 0) {
2405: php_error_docref(NULL TSRMLS_CC, E_WARNING, "Signature did not match the certificate request");
2406: goto cleanup;
2407: }
1.1.1.2 ! misho 2408:
1.1 misho 2409: /* Now we can get on with it */
1.1.1.2 ! misho 2410:
1.1 misho 2411: new_cert = X509_new();
2412: if (new_cert == NULL) {
2413: php_error_docref(NULL TSRMLS_CC, E_WARNING, "No memory");
2414: goto cleanup;
2415: }
2416: /* Version 3 cert */
2417: if (!X509_set_version(new_cert, 2))
2418: goto cleanup;
2419:
2420: ASN1_INTEGER_set(X509_get_serialNumber(new_cert), serial);
1.1.1.2 ! misho 2421:
1.1 misho 2422: X509_set_subject_name(new_cert, X509_REQ_get_subject_name(csr));
2423:
2424: if (cert == NULL) {
2425: cert = new_cert;
2426: }
2427: if (!X509_set_issuer_name(new_cert, X509_get_subject_name(cert))) {
2428: goto cleanup;
2429: }
2430: X509_gmtime_adj(X509_get_notBefore(new_cert), 0);
2431: X509_gmtime_adj(X509_get_notAfter(new_cert), (long)60*60*24*num_days);
2432: i = X509_set_pubkey(new_cert, key);
2433: if (!i) {
2434: goto cleanup;
2435: }
2436: if (req.extensions_section) {
2437: X509V3_CTX ctx;
1.1.1.2 ! misho 2438:
1.1 misho 2439: X509V3_set_ctx(&ctx, cert, new_cert, csr, NULL, 0);
2440: X509V3_set_conf_lhash(&ctx, req.req_config);
2441: if (!X509V3_EXT_add_conf(req.req_config, &ctx, req.extensions_section, new_cert)) {
2442: goto cleanup;
2443: }
2444: }
2445:
2446: /* Now sign it */
2447: if (!X509_sign(new_cert, priv_key, req.digest)) {
2448: php_error_docref(NULL TSRMLS_CC, E_WARNING, "failed to sign it");
2449: goto cleanup;
2450: }
1.1.1.2 ! misho 2451:
1.1 misho 2452: /* Succeeded; lets return the cert */
1.1.1.2 ! misho 2453: RETVAL_RESOURCE(zend_list_insert(new_cert, le_x509 TSRMLS_CC));
1.1 misho 2454: new_cert = NULL;
1.1.1.2 ! misho 2455:
1.1 misho 2456: cleanup:
2457:
2458: if (cert == new_cert) {
2459: cert = NULL;
2460: }
2461: PHP_SSL_REQ_DISPOSE(&req);
2462:
2463: if (keyresource == -1 && priv_key) {
2464: EVP_PKEY_free(priv_key);
2465: }
2466: if (key) {
2467: EVP_PKEY_free(key);
2468: }
2469: if (csr_resource == -1 && csr) {
2470: X509_REQ_free(csr);
2471: }
1.1.1.2 ! misho 2472: if (certresource == -1 && cert) {
1.1 misho 2473: X509_free(cert);
2474: }
2475: if (new_cert) {
2476: X509_free(new_cert);
2477: }
2478: }
2479: /* }}} */
2480:
2481: /* {{{ proto bool openssl_csr_new(array dn, resource &privkey [, array configargs [, array extraattribs]])
2482: Generates a privkey and CSR */
2483: PHP_FUNCTION(openssl_csr_new)
2484: {
2485: struct php_x509_request req;
2486: zval * args = NULL, * dn, *attribs = NULL;
2487: zval * out_pkey;
2488: X509_REQ * csr = NULL;
2489: int we_made_the_key = 1;
2490: long key_resource;
1.1.1.2 ! misho 2491:
1.1 misho 2492: if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "az|a!a!", &dn, &out_pkey, &args, &attribs) == FAILURE) {
2493: return;
2494: }
2495: RETVAL_FALSE;
1.1.1.2 ! misho 2496:
1.1 misho 2497: PHP_SSL_REQ_INIT(&req);
2498:
2499: if (PHP_SSL_REQ_PARSE(&req, args) == SUCCESS) {
2500: /* Generate or use a private key */
2501: if (Z_TYPE_P(out_pkey) != IS_NULL) {
2502: req.priv_key = php_openssl_evp_from_zval(&out_pkey, 0, NULL, 0, &key_resource TSRMLS_CC);
2503: if (req.priv_key != NULL) {
2504: we_made_the_key = 0;
2505: }
2506: }
2507: if (req.priv_key == NULL) {
2508: php_openssl_generate_private_key(&req TSRMLS_CC);
2509: }
2510: if (req.priv_key == NULL) {
2511: php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to generate a private key");
2512: } else {
2513: csr = X509_REQ_new();
2514: if (csr) {
2515: if (php_openssl_make_REQ(&req, csr, dn, attribs TSRMLS_CC) == SUCCESS) {
2516: X509V3_CTX ext_ctx;
2517:
2518: X509V3_set_ctx(&ext_ctx, NULL, NULL, csr, NULL, 0);
2519: X509V3_set_conf_lhash(&ext_ctx, req.req_config);
2520:
2521: /* Add extensions */
2522: if (req.request_extensions_section && !X509V3_EXT_REQ_add_conf(req.req_config,
2523: &ext_ctx, req.request_extensions_section, csr))
2524: {
2525: php_error_docref(NULL TSRMLS_CC, E_WARNING, "Error loading extension section %s", req.request_extensions_section);
2526: } else {
2527: RETVAL_TRUE;
1.1.1.2 ! misho 2528:
1.1 misho 2529: if (X509_REQ_sign(csr, req.priv_key, req.digest)) {
1.1.1.2 ! misho 2530: RETVAL_RESOURCE(zend_list_insert(csr, le_csr TSRMLS_CC));
! 2531: csr = NULL;
1.1 misho 2532: } else {
2533: php_error_docref(NULL TSRMLS_CC, E_WARNING, "Error signing request");
2534: }
2535:
2536: if (we_made_the_key) {
2537: /* and a resource for the private key */
2538: zval_dtor(out_pkey);
1.1.1.2 ! misho 2539: ZVAL_RESOURCE(out_pkey, zend_list_insert(req.priv_key, le_key TSRMLS_CC));
1.1 misho 2540: req.priv_key = NULL; /* make sure the cleanup code doesn't zap it! */
2541: } else if (key_resource != -1) {
2542: req.priv_key = NULL; /* make sure the cleanup code doesn't zap it! */
2543: }
2544: }
2545: }
2546: else {
2547: if (!we_made_the_key) {
2548: /* if we have not made the key we are not supposed to zap it by calling dispose! */
2549: req.priv_key = NULL;
2550: }
2551: }
2552: }
2553: }
2554: }
2555: if (csr) {
2556: X509_REQ_free(csr);
2557: }
2558: PHP_SSL_REQ_DISPOSE(&req);
2559: }
2560: /* }}} */
2561:
2562: /* {{{ proto mixed openssl_csr_get_subject(mixed csr)
2563: Returns the subject of a CERT or FALSE on error */
2564: PHP_FUNCTION(openssl_csr_get_subject)
2565: {
2566: zval ** zcsr;
2567: zend_bool use_shortnames = 1;
2568: long csr_resource;
2569: X509_NAME * subject;
2570: X509_REQ * csr;
2571:
2572: if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "Z|b", &zcsr, &use_shortnames) == FAILURE) {
2573: return;
2574: }
2575:
2576: csr = php_openssl_csr_from_zval(zcsr, 0, &csr_resource TSRMLS_CC);
2577:
2578: if (csr == NULL) {
2579: RETURN_FALSE;
2580: }
2581:
2582: subject = X509_REQ_get_subject_name(csr);
2583:
2584: array_init(return_value);
2585: add_assoc_name_entry(return_value, NULL, subject, use_shortnames TSRMLS_CC);
2586: return;
2587: }
2588: /* }}} */
2589:
2590: /* {{{ proto mixed openssl_csr_get_public_key(mixed csr)
2591: Returns the subject of a CERT or FALSE on error */
2592: PHP_FUNCTION(openssl_csr_get_public_key)
2593: {
2594: zval ** zcsr;
2595: zend_bool use_shortnames = 1;
2596: long csr_resource;
2597:
2598: X509_REQ * csr;
2599: EVP_PKEY *tpubkey;
2600:
2601: if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "Z|b", &zcsr, &use_shortnames) == FAILURE) {
2602: return;
2603: }
2604:
2605: csr = php_openssl_csr_from_zval(zcsr, 0, &csr_resource TSRMLS_CC);
2606:
2607: if (csr == NULL) {
2608: RETURN_FALSE;
2609: }
2610:
2611: tpubkey=X509_REQ_get_pubkey(csr);
1.1.1.2 ! misho 2612: RETVAL_RESOURCE(zend_list_insert(tpubkey, le_key TSRMLS_CC));
1.1 misho 2613: return;
2614: }
2615: /* }}} */
2616:
2617: /* }}} */
2618:
2619: /* {{{ EVP Public/Private key functions */
2620:
2621: /* {{{ php_openssl_evp_from_zval
2622: Given a zval, coerce it into a EVP_PKEY object.
2623: It can be:
2624: 1. private key resource from openssl_get_privatekey()
2625: 2. X509 resource -> public key will be extracted from it
2626: 3. if it starts with file:// interpreted as path to key file
2627: 4. interpreted as the data from the cert/key file and interpreted in same way as openssl_get_privatekey()
2628: 5. an array(0 => [items 2..4], 1 => passphrase)
2629: 6. if val is a string (possibly starting with file:///) and it is not an X509 certificate, then interpret as public key
2630: NOTE: If you are requesting a private key but have not specified a passphrase, you should use an
2631: empty string rather than NULL for the passphrase - NULL causes a passphrase prompt to be emitted in
2632: the Apache error log!
2633: */
2634: static EVP_PKEY * php_openssl_evp_from_zval(zval ** val, int public_key, char * passphrase, int makeresource, long * resourceval TSRMLS_DC)
2635: {
2636: EVP_PKEY * key = NULL;
2637: X509 * cert = NULL;
2638: int free_cert = 0;
2639: long cert_res = -1;
2640: char * filename = NULL;
2641: zval tmp;
2642:
2643: Z_TYPE(tmp) = IS_NULL;
2644:
2645: #define TMP_CLEAN \
2646: if (Z_TYPE(tmp) == IS_STRING) {\
2647: zval_dtor(&tmp); \
2648: } \
2649: return NULL;
2650:
2651: if (resourceval) {
2652: *resourceval = -1;
2653: }
2654: if (Z_TYPE_PP(val) == IS_ARRAY) {
2655: zval ** zphrase;
1.1.1.2 ! misho 2656:
1.1 misho 2657: /* get passphrase */
2658:
2659: if (zend_hash_index_find(HASH_OF(*val), 1, (void **)&zphrase) == FAILURE) {
2660: php_error_docref(NULL TSRMLS_CC, E_WARNING, "key array must be of the form array(0 => key, 1 => phrase)");
2661: return NULL;
2662: }
1.1.1.2 ! misho 2663:
1.1 misho 2664: if (Z_TYPE_PP(zphrase) == IS_STRING) {
2665: passphrase = Z_STRVAL_PP(zphrase);
2666: } else {
2667: tmp = **zphrase;
2668: zval_copy_ctor(&tmp);
2669: convert_to_string(&tmp);
2670: passphrase = Z_STRVAL(tmp);
2671: }
2672:
2673: /* now set val to be the key param and continue */
2674: if (zend_hash_index_find(HASH_OF(*val), 0, (void **)&val) == FAILURE) {
2675: php_error_docref(NULL TSRMLS_CC, E_WARNING, "key array must be of the form array(0 => key, 1 => phrase)");
2676: TMP_CLEAN;
2677: }
2678: }
2679:
2680: if (Z_TYPE_PP(val) == IS_RESOURCE) {
2681: void * what;
2682: int type;
2683:
2684: what = zend_fetch_resource(val TSRMLS_CC, -1, "OpenSSL X.509/key", &type, 2, le_x509, le_key);
2685: if (!what) {
2686: TMP_CLEAN;
2687: }
1.1.1.2 ! misho 2688: if (resourceval) {
1.1 misho 2689: *resourceval = Z_LVAL_PP(val);
2690: }
2691: if (type == le_x509) {
2692: /* extract key from cert, depending on public_key param */
2693: cert = (X509*)what;
2694: free_cert = 0;
2695: } else if (type == le_key) {
2696: int is_priv;
2697:
2698: is_priv = php_openssl_is_private_key((EVP_PKEY*)what TSRMLS_CC);
2699:
2700: /* check whether it is actually a private key if requested */
2701: if (!public_key && !is_priv) {
2702: php_error_docref(NULL TSRMLS_CC, E_WARNING, "supplied key param is a public key");
2703: TMP_CLEAN;
2704: }
2705:
2706: if (public_key && is_priv) {
2707: php_error_docref(NULL TSRMLS_CC, E_WARNING, "Don't know how to get public key from this private key");
2708: TMP_CLEAN;
2709: } else {
2710: if (Z_TYPE(tmp) == IS_STRING) {
2711: zval_dtor(&tmp);
2712: }
2713: /* got the key - return it */
2714: return (EVP_PKEY*)what;
2715: }
2716: } else {
2717: /* other types could be used here - eg: file pointers and read in the data from them */
2718: TMP_CLEAN;
2719: }
2720: } else {
2721: /* force it to be a string and check if it refers to a file */
1.1.1.2 ! misho 2722: /* passing non string values leaks, object uses toString, it returns NULL
! 2723: * See bug38255.phpt
1.1 misho 2724: */
2725: if (!(Z_TYPE_PP(val) == IS_STRING || Z_TYPE_PP(val) == IS_OBJECT)) {
2726: TMP_CLEAN;
2727: }
2728: convert_to_string_ex(val);
2729:
2730: if (Z_STRLEN_PP(val) > 7 && memcmp(Z_STRVAL_PP(val), "file://", sizeof("file://") - 1) == 0) {
2731: filename = Z_STRVAL_PP(val) + (sizeof("file://") - 1);
2732: }
2733: /* it's an X509 file/cert of some kind, and we need to extract the data from that */
2734: if (public_key) {
2735: cert = php_openssl_x509_from_zval(val, 0, &cert_res TSRMLS_CC);
2736: free_cert = (cert_res == -1);
2737: /* actual extraction done later */
2738: if (!cert) {
2739: /* not a X509 certificate, try to retrieve public key */
2740: BIO* in;
2741: if (filename) {
2742: in = BIO_new_file(filename, "r");
2743: } else {
2744: in = BIO_new_mem_buf(Z_STRVAL_PP(val), Z_STRLEN_PP(val));
2745: }
2746: if (in == NULL) {
2747: TMP_CLEAN;
2748: }
2749: key = PEM_read_bio_PUBKEY(in, NULL,NULL, NULL);
2750: BIO_free(in);
2751: }
2752: } else {
2753: /* we want the private key */
2754: BIO *in;
2755:
2756: if (filename) {
1.1.1.2 ! misho 2757: if (php_openssl_open_base_dir_chk(filename TSRMLS_CC)) {
1.1 misho 2758: TMP_CLEAN;
2759: }
2760: in = BIO_new_file(filename, "r");
2761: } else {
2762: in = BIO_new_mem_buf(Z_STRVAL_PP(val), Z_STRLEN_PP(val));
2763: }
2764:
2765: if (in == NULL) {
2766: TMP_CLEAN;
2767: }
2768: key = PEM_read_bio_PrivateKey(in, NULL,NULL, passphrase);
2769: BIO_free(in);
2770: }
2771: }
2772:
2773: if (public_key && cert && key == NULL) {
2774: /* extract public key from X509 cert */
2775: key = (EVP_PKEY *) X509_get_pubkey(cert);
2776: }
2777:
2778: if (free_cert && cert) {
2779: X509_free(cert);
2780: }
2781: if (key && makeresource && resourceval) {
2782: *resourceval = ZEND_REGISTER_RESOURCE(NULL, key, le_key);
2783: }
2784: if (Z_TYPE(tmp) == IS_STRING) {
2785: zval_dtor(&tmp);
2786: }
2787: return key;
2788: }
2789: /* }}} */
2790:
2791: /* {{{ php_openssl_generate_private_key */
2792: static EVP_PKEY * php_openssl_generate_private_key(struct php_x509_request * req TSRMLS_DC)
2793: {
2794: char * randfile = NULL;
2795: int egdsocket, seeded;
2796: EVP_PKEY * return_val = NULL;
1.1.1.2 ! misho 2797:
1.1 misho 2798: if (req->priv_key_bits < MIN_KEY_LENGTH) {
2799: php_error_docref(NULL TSRMLS_CC, E_WARNING, "private key length is too short; it needs to be at least %d bits, not %d",
2800: MIN_KEY_LENGTH, req->priv_key_bits);
2801: return NULL;
2802: }
2803:
2804: randfile = CONF_get_string(req->req_config, req->section_name, "RANDFILE");
1.1.1.2 ! misho 2805: php_openssl_load_rand_file(randfile, &egdsocket, &seeded TSRMLS_CC);
! 2806:
1.1 misho 2807: if ((req->priv_key = EVP_PKEY_new()) != NULL) {
2808: switch(req->priv_key_type) {
2809: case OPENSSL_KEYTYPE_RSA:
2810: if (EVP_PKEY_assign_RSA(req->priv_key, RSA_generate_key(req->priv_key_bits, 0x10001, NULL, NULL))) {
2811: return_val = req->priv_key;
2812: }
2813: break;
2814: #if !defined(NO_DSA) && defined(HAVE_DSA_DEFAULT_METHOD)
2815: case OPENSSL_KEYTYPE_DSA:
2816: {
2817: DSA *dsapar = DSA_generate_parameters(req->priv_key_bits, NULL, 0, NULL, NULL, NULL, NULL);
2818: if (dsapar) {
2819: DSA_set_method(dsapar, DSA_get_default_method());
2820: if (DSA_generate_key(dsapar)) {
2821: if (EVP_PKEY_assign_DSA(req->priv_key, dsapar)) {
2822: return_val = req->priv_key;
2823: }
2824: } else {
2825: DSA_free(dsapar);
2826: }
2827: }
2828: }
2829: break;
2830: #endif
2831: #if !defined(NO_DH)
2832: case OPENSSL_KEYTYPE_DH:
2833: {
2834: DH *dhpar = DH_generate_parameters(req->priv_key_bits, 2, NULL, NULL);
2835: int codes = 0;
2836:
2837: if (dhpar) {
2838: DH_set_method(dhpar, DH_get_default_method());
2839: if (DH_check(dhpar, &codes) && codes == 0 && DH_generate_key(dhpar)) {
2840: if (EVP_PKEY_assign_DH(req->priv_key, dhpar)) {
2841: return_val = req->priv_key;
2842: }
2843: } else {
2844: DH_free(dhpar);
2845: }
2846: }
2847: }
2848: break;
2849: #endif
2850: default:
2851: php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unsupported private key type");
2852: }
2853: }
2854:
2855: php_openssl_write_rand_file(randfile, egdsocket, seeded);
1.1.1.2 ! misho 2856:
1.1 misho 2857: if (return_val == NULL) {
2858: EVP_PKEY_free(req->priv_key);
2859: req->priv_key = NULL;
2860: return NULL;
2861: }
1.1.1.2 ! misho 2862:
1.1 misho 2863: return return_val;
2864: }
2865: /* }}} */
2866:
2867: /* {{{ php_openssl_is_private_key
2868: Check whether the supplied key is a private key by checking if the secret prime factors are set */
2869: static int php_openssl_is_private_key(EVP_PKEY* pkey TSRMLS_DC)
2870: {
2871: assert(pkey != NULL);
2872:
2873: switch (pkey->type) {
2874: #ifndef NO_RSA
2875: case EVP_PKEY_RSA:
2876: case EVP_PKEY_RSA2:
2877: assert(pkey->pkey.rsa != NULL);
2878: if (pkey->pkey.rsa != NULL && (NULL == pkey->pkey.rsa->p || NULL == pkey->pkey.rsa->q)) {
2879: return 0;
2880: }
2881: break;
2882: #endif
2883: #ifndef NO_DSA
2884: case EVP_PKEY_DSA:
2885: case EVP_PKEY_DSA1:
2886: case EVP_PKEY_DSA2:
2887: case EVP_PKEY_DSA3:
2888: case EVP_PKEY_DSA4:
2889: assert(pkey->pkey.dsa != NULL);
2890:
1.1.1.2 ! misho 2891: if (NULL == pkey->pkey.dsa->p || NULL == pkey->pkey.dsa->q || NULL == pkey->pkey.dsa->priv_key){
1.1 misho 2892: return 0;
2893: }
2894: break;
2895: #endif
2896: #ifndef NO_DH
2897: case EVP_PKEY_DH:
2898: assert(pkey->pkey.dh != NULL);
2899:
2900: if (NULL == pkey->pkey.dh->p || NULL == pkey->pkey.dh->priv_key) {
2901: return 0;
2902: }
2903: break;
2904: #endif
2905: default:
2906: php_error_docref(NULL TSRMLS_CC, E_WARNING, "key type not supported in this PHP build!");
2907: break;
2908: }
2909: return 1;
2910: }
2911: /* }}} */
2912:
2913: #define OPENSSL_PKEY_GET_BN(_type, _name) do { \
2914: if (pkey->pkey._type->_name != NULL) { \
2915: int len = BN_num_bytes(pkey->pkey._type->_name); \
2916: char *str = emalloc(len + 1); \
2917: BN_bn2bin(pkey->pkey._type->_name, (unsigned char*)str); \
2918: str[len] = 0; \
2919: add_assoc_stringl(_type, #_name, str, len, 0); \
2920: } \
2921: } while (0)
2922:
2923: #define OPENSSL_PKEY_SET_BN(_ht, _type, _name) do { \
2924: zval **bn; \
2925: if (zend_hash_find(_ht, #_name, sizeof(#_name), (void**)&bn) == SUCCESS && \
2926: Z_TYPE_PP(bn) == IS_STRING) { \
2927: _type->_name = BN_bin2bn( \
2928: (unsigned char*)Z_STRVAL_PP(bn), \
2929: Z_STRLEN_PP(bn), NULL); \
2930: } \
2931: } while (0);
2932:
2933:
2934: /* {{{ proto resource openssl_pkey_new([array configargs])
2935: Generates a new private key */
2936: PHP_FUNCTION(openssl_pkey_new)
2937: {
2938: struct php_x509_request req;
2939: zval * args = NULL;
2940: zval **data;
2941:
2942: if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|a!", &args) == FAILURE) {
2943: return;
2944: }
2945: RETVAL_FALSE;
2946:
2947: if (args && Z_TYPE_P(args) == IS_ARRAY) {
2948: EVP_PKEY *pkey;
2949:
2950: if (zend_hash_find(Z_ARRVAL_P(args), "rsa", sizeof("rsa"), (void**)&data) == SUCCESS &&
2951: Z_TYPE_PP(data) == IS_ARRAY) {
2952: pkey = EVP_PKEY_new();
2953: if (pkey) {
2954: RSA *rsa = RSA_new();
2955: if (rsa) {
2956: OPENSSL_PKEY_SET_BN(Z_ARRVAL_PP(data), rsa, n);
2957: OPENSSL_PKEY_SET_BN(Z_ARRVAL_PP(data), rsa, e);
2958: OPENSSL_PKEY_SET_BN(Z_ARRVAL_PP(data), rsa, d);
2959: OPENSSL_PKEY_SET_BN(Z_ARRVAL_PP(data), rsa, p);
2960: OPENSSL_PKEY_SET_BN(Z_ARRVAL_PP(data), rsa, q);
2961: OPENSSL_PKEY_SET_BN(Z_ARRVAL_PP(data), rsa, dmp1);
2962: OPENSSL_PKEY_SET_BN(Z_ARRVAL_PP(data), rsa, dmq1);
2963: OPENSSL_PKEY_SET_BN(Z_ARRVAL_PP(data), rsa, iqmp);
2964: if (rsa->n && rsa->d) {
2965: if (EVP_PKEY_assign_RSA(pkey, rsa)) {
1.1.1.2 ! misho 2966: RETURN_RESOURCE(zend_list_insert(pkey, le_key TSRMLS_CC));
1.1 misho 2967: }
2968: }
2969: RSA_free(rsa);
2970: }
2971: EVP_PKEY_free(pkey);
2972: }
2973: RETURN_FALSE;
2974: } else if (zend_hash_find(Z_ARRVAL_P(args), "dsa", sizeof("dsa"), (void**)&data) == SUCCESS &&
2975: Z_TYPE_PP(data) == IS_ARRAY) {
2976: pkey = EVP_PKEY_new();
2977: if (pkey) {
2978: DSA *dsa = DSA_new();
2979: if (dsa) {
2980: OPENSSL_PKEY_SET_BN(Z_ARRVAL_PP(data), dsa, p);
2981: OPENSSL_PKEY_SET_BN(Z_ARRVAL_PP(data), dsa, q);
2982: OPENSSL_PKEY_SET_BN(Z_ARRVAL_PP(data), dsa, g);
2983: OPENSSL_PKEY_SET_BN(Z_ARRVAL_PP(data), dsa, priv_key);
2984: OPENSSL_PKEY_SET_BN(Z_ARRVAL_PP(data), dsa, pub_key);
2985: if (dsa->p && dsa->q && dsa->g) {
2986: if (!dsa->priv_key && !dsa->pub_key) {
2987: DSA_generate_key(dsa);
2988: }
2989: if (EVP_PKEY_assign_DSA(pkey, dsa)) {
1.1.1.2 ! misho 2990: RETURN_RESOURCE(zend_list_insert(pkey, le_key TSRMLS_CC));
1.1 misho 2991: }
2992: }
2993: DSA_free(dsa);
2994: }
2995: EVP_PKEY_free(pkey);
2996: }
2997: RETURN_FALSE;
2998: } else if (zend_hash_find(Z_ARRVAL_P(args), "dh", sizeof("dh"), (void**)&data) == SUCCESS &&
2999: Z_TYPE_PP(data) == IS_ARRAY) {
3000: pkey = EVP_PKEY_new();
3001: if (pkey) {
3002: DH *dh = DH_new();
3003: if (dh) {
3004: OPENSSL_PKEY_SET_BN(Z_ARRVAL_PP(data), dh, p);
3005: OPENSSL_PKEY_SET_BN(Z_ARRVAL_PP(data), dh, g);
3006: OPENSSL_PKEY_SET_BN(Z_ARRVAL_PP(data), dh, priv_key);
3007: OPENSSL_PKEY_SET_BN(Z_ARRVAL_PP(data), dh, pub_key);
3008: if (dh->p && dh->g) {
3009: if (!dh->pub_key) {
3010: DH_generate_key(dh);
3011: }
3012: if (EVP_PKEY_assign_DH(pkey, dh)) {
1.1.1.2 ! misho 3013: RETURN_RESOURCE(zend_list_insert(pkey, le_key TSRMLS_CC));
1.1 misho 3014: }
3015: }
3016: DH_free(dh);
3017: }
3018: EVP_PKEY_free(pkey);
3019: }
3020: RETURN_FALSE;
3021: }
1.1.1.2 ! misho 3022: }
1.1 misho 3023:
3024: PHP_SSL_REQ_INIT(&req);
3025:
3026: if (PHP_SSL_REQ_PARSE(&req, args) == SUCCESS)
3027: {
3028: if (php_openssl_generate_private_key(&req TSRMLS_CC)) {
3029: /* pass back a key resource */
1.1.1.2 ! misho 3030: RETVAL_RESOURCE(zend_list_insert(req.priv_key, le_key TSRMLS_CC));
1.1 misho 3031: /* make sure the cleanup code doesn't zap it! */
3032: req.priv_key = NULL;
3033: }
3034: }
3035: PHP_SSL_REQ_DISPOSE(&req);
3036: }
3037: /* }}} */
3038:
3039: /* {{{ proto bool openssl_pkey_export_to_file(mixed key, string outfilename [, string passphrase, array config_args)
3040: Gets an exportable representation of a key into a file */
3041: PHP_FUNCTION(openssl_pkey_export_to_file)
3042: {
3043: struct php_x509_request req;
3044: zval ** zpkey, * args = NULL;
3045: char * passphrase = NULL; int passphrase_len = 0;
3046: char * filename = NULL; int filename_len = 0;
3047: long key_resource = -1;
3048: EVP_PKEY * key;
3049: BIO * bio_out = NULL;
3050: const EVP_CIPHER * cipher;
3051:
1.1.1.2 ! misho 3052: if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "Zp|s!a!", &zpkey, &filename, &filename_len, &passphrase, &passphrase_len, &args) == FAILURE) {
1.1 misho 3053: return;
3054: }
1.1.1.2 ! misho 3055: RETVAL_FALSE;
1.1 misho 3056:
3057: key = php_openssl_evp_from_zval(zpkey, 0, passphrase, 0, &key_resource TSRMLS_CC);
3058:
3059: if (key == NULL) {
3060: php_error_docref(NULL TSRMLS_CC, E_WARNING, "cannot get key from parameter 1");
3061: RETURN_FALSE;
3062: }
1.1.1.2 ! misho 3063:
! 3064: if (php_openssl_open_base_dir_chk(filename TSRMLS_CC)) {
1.1 misho 3065: RETURN_FALSE;
3066: }
1.1.1.2 ! misho 3067:
1.1 misho 3068: PHP_SSL_REQ_INIT(&req);
3069:
3070: if (PHP_SSL_REQ_PARSE(&req, args) == SUCCESS) {
3071: bio_out = BIO_new_file(filename, "w");
3072:
3073: if (passphrase && req.priv_key_encrypt) {
1.1.1.2 ! misho 3074: if (req.priv_key_encrypt_cipher) {
! 3075: cipher = req.priv_key_encrypt_cipher;
! 3076: } else {
! 3077: cipher = (EVP_CIPHER *) EVP_des_ede3_cbc();
! 3078: }
1.1 misho 3079: } else {
3080: cipher = NULL;
3081: }
3082: if (PEM_write_bio_PrivateKey(bio_out, key, cipher, (unsigned char *)passphrase, passphrase_len, NULL, NULL)) {
3083: /* Success!
3084: * If returning the output as a string, do so now */
3085: RETVAL_TRUE;
3086: }
3087: }
3088: PHP_SSL_REQ_DISPOSE(&req);
3089:
3090: if (key_resource == -1 && key) {
3091: EVP_PKEY_free(key);
3092: }
3093: if (bio_out) {
3094: BIO_free(bio_out);
3095: }
3096: }
3097: /* }}} */
3098:
3099: /* {{{ proto bool openssl_pkey_export(mixed key, &mixed out [, string passphrase [, array config_args]])
3100: Gets an exportable representation of a key into a string or file */
3101: PHP_FUNCTION(openssl_pkey_export)
3102: {
3103: struct php_x509_request req;
3104: zval ** zpkey, * args = NULL, *out;
3105: char * passphrase = NULL; int passphrase_len = 0;
3106: long key_resource = -1;
3107: EVP_PKEY * key;
3108: BIO * bio_out = NULL;
3109: const EVP_CIPHER * cipher;
1.1.1.2 ! misho 3110:
1.1 misho 3111: if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "Zz|s!a!", &zpkey, &out, &passphrase, &passphrase_len, &args) == FAILURE) {
3112: return;
3113: }
3114: RETVAL_FALSE;
3115:
3116: key = php_openssl_evp_from_zval(zpkey, 0, passphrase, 0, &key_resource TSRMLS_CC);
3117:
3118: if (key == NULL) {
3119: php_error_docref(NULL TSRMLS_CC, E_WARNING, "cannot get key from parameter 1");
3120: RETURN_FALSE;
3121: }
1.1.1.2 ! misho 3122:
1.1 misho 3123: PHP_SSL_REQ_INIT(&req);
3124:
3125: if (PHP_SSL_REQ_PARSE(&req, args) == SUCCESS) {
3126: bio_out = BIO_new(BIO_s_mem());
3127:
3128: if (passphrase && req.priv_key_encrypt) {
1.1.1.2 ! misho 3129: if (req.priv_key_encrypt_cipher) {
! 3130: cipher = req.priv_key_encrypt_cipher;
! 3131: } else {
! 3132: cipher = (EVP_CIPHER *) EVP_des_ede3_cbc();
! 3133: }
1.1 misho 3134: } else {
3135: cipher = NULL;
3136: }
3137: if (PEM_write_bio_PrivateKey(bio_out, key, cipher, (unsigned char *)passphrase, passphrase_len, NULL, NULL)) {
3138: /* Success!
3139: * If returning the output as a string, do so now */
3140:
3141: char * bio_mem_ptr;
3142: long bio_mem_len;
3143: RETVAL_TRUE;
3144:
3145: bio_mem_len = BIO_get_mem_data(bio_out, &bio_mem_ptr);
3146: zval_dtor(out);
3147: ZVAL_STRINGL(out, bio_mem_ptr, bio_mem_len, 1);
3148: }
3149: }
3150: PHP_SSL_REQ_DISPOSE(&req);
3151:
3152: if (key_resource == -1 && key) {
3153: EVP_PKEY_free(key);
3154: }
3155: if (bio_out) {
3156: BIO_free(bio_out);
3157: }
3158: }
3159: /* }}} */
3160:
3161: /* {{{ proto int openssl_pkey_get_public(mixed cert)
3162: Gets public key from X.509 certificate */
3163: PHP_FUNCTION(openssl_pkey_get_public)
3164: {
3165: zval **cert;
3166: EVP_PKEY *pkey;
3167:
3168: if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "Z", &cert) == FAILURE) {
3169: return;
3170: }
3171: Z_TYPE_P(return_value) = IS_RESOURCE;
3172: pkey = php_openssl_evp_from_zval(cert, 1, NULL, 1, &Z_LVAL_P(return_value) TSRMLS_CC);
3173:
3174: if (pkey == NULL) {
3175: RETURN_FALSE;
3176: }
3177: }
3178: /* }}} */
3179:
3180: /* {{{ proto void openssl_pkey_free(int key)
3181: Frees a key */
3182: PHP_FUNCTION(openssl_pkey_free)
3183: {
3184: zval *key;
3185: EVP_PKEY *pkey;
3186:
3187: if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &key) == FAILURE) {
3188: return;
3189: }
3190: ZEND_FETCH_RESOURCE(pkey, EVP_PKEY *, &key, -1, "OpenSSL key", le_key);
3191: zend_list_delete(Z_LVAL_P(key));
3192: }
3193: /* }}} */
3194:
3195: /* {{{ proto int openssl_pkey_get_private(string key [, string passphrase])
3196: Gets private keys */
3197: PHP_FUNCTION(openssl_pkey_get_private)
3198: {
3199: zval **cert;
3200: EVP_PKEY *pkey;
3201: char * passphrase = "";
3202: int passphrase_len = sizeof("")-1;
3203:
3204: if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "Z|s", &cert, &passphrase, &passphrase_len) == FAILURE) {
3205: return;
3206: }
3207: Z_TYPE_P(return_value) = IS_RESOURCE;
3208: pkey = php_openssl_evp_from_zval(cert, 0, passphrase, 1, &Z_LVAL_P(return_value) TSRMLS_CC);
3209:
3210: if (pkey == NULL) {
3211: RETURN_FALSE;
3212: }
3213: }
3214:
3215: /* }}} */
3216:
3217: /* {{{ proto resource openssl_pkey_get_details(resource key)
3218: returns an array with the key details (bits, pkey, type)*/
3219: PHP_FUNCTION(openssl_pkey_get_details)
3220: {
3221: zval *key;
3222: EVP_PKEY *pkey;
3223: BIO *out;
3224: unsigned int pbio_len;
3225: char *pbio;
3226: long ktype;
3227:
3228: if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &key) == FAILURE) {
3229: return;
3230: }
3231: ZEND_FETCH_RESOURCE(pkey, EVP_PKEY *, &key, -1, "OpenSSL key", le_key);
3232: if (!pkey) {
3233: RETURN_FALSE;
3234: }
3235: out = BIO_new(BIO_s_mem());
3236: PEM_write_bio_PUBKEY(out, pkey);
3237: pbio_len = BIO_get_mem_data(out, &pbio);
3238:
3239: array_init(return_value);
3240: add_assoc_long(return_value, "bits", EVP_PKEY_bits(pkey));
3241: add_assoc_stringl(return_value, "key", pbio, pbio_len, 1);
1.1.1.2 ! misho 3242: /*TODO: Use the real values once the openssl constants are used
1.1 misho 3243: * See the enum at the top of this file
3244: */
3245: switch (EVP_PKEY_type(pkey->type)) {
3246: case EVP_PKEY_RSA:
3247: case EVP_PKEY_RSA2:
3248: ktype = OPENSSL_KEYTYPE_RSA;
3249:
3250: if (pkey->pkey.rsa != NULL) {
3251: zval *rsa;
3252:
3253: ALLOC_INIT_ZVAL(rsa);
3254: array_init(rsa);
3255: OPENSSL_PKEY_GET_BN(rsa, n);
3256: OPENSSL_PKEY_GET_BN(rsa, e);
3257: OPENSSL_PKEY_GET_BN(rsa, d);
3258: OPENSSL_PKEY_GET_BN(rsa, p);
3259: OPENSSL_PKEY_GET_BN(rsa, q);
3260: OPENSSL_PKEY_GET_BN(rsa, dmp1);
3261: OPENSSL_PKEY_GET_BN(rsa, dmq1);
3262: OPENSSL_PKEY_GET_BN(rsa, iqmp);
3263: add_assoc_zval(return_value, "rsa", rsa);
3264: }
3265:
1.1.1.2 ! misho 3266: break;
1.1 misho 3267: case EVP_PKEY_DSA:
3268: case EVP_PKEY_DSA2:
3269: case EVP_PKEY_DSA3:
3270: case EVP_PKEY_DSA4:
3271: ktype = OPENSSL_KEYTYPE_DSA;
3272:
3273: if (pkey->pkey.dsa != NULL) {
3274: zval *dsa;
3275:
3276: ALLOC_INIT_ZVAL(dsa);
3277: array_init(dsa);
3278: OPENSSL_PKEY_GET_BN(dsa, p);
3279: OPENSSL_PKEY_GET_BN(dsa, q);
3280: OPENSSL_PKEY_GET_BN(dsa, g);
3281: OPENSSL_PKEY_GET_BN(dsa, priv_key);
3282: OPENSSL_PKEY_GET_BN(dsa, pub_key);
3283: add_assoc_zval(return_value, "dsa", dsa);
3284: }
3285: break;
3286: case EVP_PKEY_DH:
1.1.1.2 ! misho 3287:
1.1 misho 3288: ktype = OPENSSL_KEYTYPE_DH;
3289:
3290: if (pkey->pkey.dh != NULL) {
3291: zval *dh;
3292:
3293: ALLOC_INIT_ZVAL(dh);
3294: array_init(dh);
3295: OPENSSL_PKEY_GET_BN(dh, p);
3296: OPENSSL_PKEY_GET_BN(dh, g);
3297: OPENSSL_PKEY_GET_BN(dh, priv_key);
3298: OPENSSL_PKEY_GET_BN(dh, pub_key);
3299: add_assoc_zval(return_value, "dh", dh);
3300: }
3301:
3302: break;
1.1.1.2 ! misho 3303: #ifdef EVP_PKEY_EC
1.1 misho 3304: case EVP_PKEY_EC:
3305: ktype = OPENSSL_KEYTYPE_EC;
3306: break;
3307: #endif
3308: default:
3309: ktype = -1;
3310: break;
3311: }
3312: add_assoc_long(return_value, "type", ktype);
3313:
3314: BIO_free(out);
3315: }
3316: /* }}} */
3317:
3318: /* }}} */
3319:
3320: /* {{{ PKCS7 S/MIME functions */
3321:
3322: /* {{{ proto bool openssl_pkcs7_verify(string filename, long flags [, string signerscerts [, array cainfo [, string extracerts [, string content]]]])
3323: Verifys that the data block is intact, the signer is who they say they are, and returns the CERTs of the signers */
3324: PHP_FUNCTION(openssl_pkcs7_verify)
3325: {
3326: X509_STORE * store = NULL;
3327: zval * cainfo = NULL;
3328: STACK_OF(X509) *signers= NULL;
3329: STACK_OF(X509) *others = NULL;
3330: PKCS7 * p7 = NULL;
3331: BIO * in = NULL, * datain = NULL, * dataout = NULL;
3332: long flags = 0;
3333: char * filename; int filename_len;
3334: char * extracerts = NULL; int extracerts_len = 0;
3335: char * signersfilename = NULL; int signersfilename_len = 0;
3336: char * datafilename = NULL; int datafilename_len = 0;
1.1.1.2 ! misho 3337:
1.1 misho 3338: RETVAL_LONG(-1);
3339:
1.1.1.2 ! misho 3340: if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "pl|papp", &filename, &filename_len,
1.1 misho 3341: &flags, &signersfilename, &signersfilename_len, &cainfo,
3342: &extracerts, &extracerts_len, &datafilename, &datafilename_len) == FAILURE) {
3343: return;
3344: }
1.1.1.2 ! misho 3345:
1.1 misho 3346: if (extracerts) {
3347: others = load_all_certs_from_file(extracerts);
3348: if (others == NULL) {
3349: goto clean_exit;
3350: }
3351: }
3352:
3353: flags = flags & ~PKCS7_DETACHED;
3354:
3355: store = setup_verify(cainfo TSRMLS_CC);
3356:
3357: if (!store) {
3358: goto clean_exit;
3359: }
1.1.1.2 ! misho 3360: if (php_openssl_open_base_dir_chk(filename TSRMLS_CC)) {
1.1 misho 3361: goto clean_exit;
3362: }
3363:
3364: in = BIO_new_file(filename, (flags & PKCS7_BINARY) ? "rb" : "r");
3365: if (in == NULL) {
3366: goto clean_exit;
3367: }
3368: p7 = SMIME_read_PKCS7(in, &datain);
3369: if (p7 == NULL) {
3370: #if DEBUG_SMIME
3371: zend_printf("SMIME_read_PKCS7 failed\n");
3372: #endif
3373: goto clean_exit;
3374: }
3375:
3376: if (datafilename) {
3377:
1.1.1.2 ! misho 3378: if (php_openssl_open_base_dir_chk(datafilename TSRMLS_CC)) {
1.1 misho 3379: goto clean_exit;
3380: }
3381:
3382: dataout = BIO_new_file(datafilename, "w");
3383: if (dataout == NULL) {
3384: goto clean_exit;
3385: }
3386: }
3387: #if DEBUG_SMIME
3388: zend_printf("Calling PKCS7 verify\n");
3389: #endif
3390:
3391: if (PKCS7_verify(p7, others, store, datain, dataout, flags)) {
3392:
3393: RETVAL_TRUE;
3394:
3395: if (signersfilename) {
3396: BIO *certout;
1.1.1.2 ! misho 3397:
! 3398: if (php_openssl_open_base_dir_chk(signersfilename TSRMLS_CC)) {
1.1 misho 3399: goto clean_exit;
3400: }
1.1.1.2 ! misho 3401:
1.1 misho 3402: certout = BIO_new_file(signersfilename, "w");
3403: if (certout) {
3404: int i;
3405: signers = PKCS7_get0_signers(p7, NULL, flags);
3406:
3407: for(i = 0; i < sk_X509_num(signers); i++) {
3408: PEM_write_bio_X509(certout, sk_X509_value(signers, i));
3409: }
3410: BIO_free(certout);
3411: sk_X509_free(signers);
3412: } else {
3413: php_error_docref(NULL TSRMLS_CC, E_WARNING, "signature OK, but cannot open %s for writing", signersfilename);
3414: RETVAL_LONG(-1);
3415: }
3416: }
3417: goto clean_exit;
3418: } else {
3419: RETVAL_FALSE;
3420: }
3421: clean_exit:
3422: X509_STORE_free(store);
3423: BIO_free(datain);
3424: BIO_free(in);
3425: BIO_free(dataout);
3426: PKCS7_free(p7);
3427: sk_X509_free(others);
3428: }
3429: /* }}} */
3430:
3431: /* {{{ proto bool openssl_pkcs7_encrypt(string infile, string outfile, mixed recipcerts, array headers [, long flags [, long cipher]])
3432: Encrypts the message in the file named infile with the certificates in recipcerts and output the result to the file named outfile */
3433: PHP_FUNCTION(openssl_pkcs7_encrypt)
3434: {
3435: zval ** zrecipcerts, * zheaders = NULL;
3436: STACK_OF(X509) * recipcerts = NULL;
3437: BIO * infile = NULL, * outfile = NULL;
3438: long flags = 0;
3439: PKCS7 * p7 = NULL;
3440: HashPosition hpos;
3441: zval ** zcertval;
3442: X509 * cert;
3443: const EVP_CIPHER *cipher = NULL;
3444: long cipherid = PHP_OPENSSL_CIPHER_DEFAULT;
3445: uint strindexlen;
3446: ulong intindex;
3447: char * strindex;
3448: char * infilename = NULL; int infilename_len;
3449: char * outfilename = NULL; int outfilename_len;
1.1.1.2 ! misho 3450:
1.1 misho 3451: RETVAL_FALSE;
3452:
1.1.1.2 ! misho 3453: if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ppZa!|ll", &infilename, &infilename_len,
1.1 misho 3454: &outfilename, &outfilename_len, &zrecipcerts, &zheaders, &flags, &cipherid) == FAILURE)
3455: return;
3456:
3457:
1.1.1.2 ! misho 3458: if (php_openssl_open_base_dir_chk(infilename TSRMLS_CC) || php_openssl_open_base_dir_chk(outfilename TSRMLS_CC)) {
1.1 misho 3459: return;
3460: }
3461:
3462: infile = BIO_new_file(infilename, "r");
3463: if (infile == NULL) {
3464: goto clean_exit;
3465: }
3466:
3467: outfile = BIO_new_file(outfilename, "w");
1.1.1.2 ! misho 3468: if (outfile == NULL) {
1.1 misho 3469: goto clean_exit;
3470: }
3471:
3472: recipcerts = sk_X509_new_null();
3473:
3474: /* get certs */
3475: if (Z_TYPE_PP(zrecipcerts) == IS_ARRAY) {
3476: zend_hash_internal_pointer_reset_ex(HASH_OF(*zrecipcerts), &hpos);
3477: while(zend_hash_get_current_data_ex(HASH_OF(*zrecipcerts), (void**)&zcertval, &hpos) == SUCCESS) {
3478: long certresource;
3479:
3480: cert = php_openssl_x509_from_zval(zcertval, 0, &certresource TSRMLS_CC);
3481: if (cert == NULL) {
3482: goto clean_exit;
3483: }
3484:
3485: if (certresource != -1) {
3486: /* we shouldn't free this particular cert, as it is a resource.
3487: make a copy and push that on the stack instead */
3488: cert = X509_dup(cert);
3489: if (cert == NULL) {
3490: goto clean_exit;
3491: }
3492: }
3493: sk_X509_push(recipcerts, cert);
3494:
3495: zend_hash_move_forward_ex(HASH_OF(*zrecipcerts), &hpos);
3496: }
3497: } else {
3498: /* a single certificate */
3499: long certresource;
3500:
3501: cert = php_openssl_x509_from_zval(zrecipcerts, 0, &certresource TSRMLS_CC);
3502: if (cert == NULL) {
3503: goto clean_exit;
3504: }
3505:
3506: if (certresource != -1) {
3507: /* we shouldn't free this particular cert, as it is a resource.
3508: make a copy and push that on the stack instead */
3509: cert = X509_dup(cert);
3510: if (cert == NULL) {
3511: goto clean_exit;
3512: }
3513: }
3514: sk_X509_push(recipcerts, cert);
3515: }
3516:
3517: /* sanity check the cipher */
3518: cipher = php_openssl_get_evp_cipher_from_algo(cipherid);
3519: if (cipher == NULL) {
3520: /* shouldn't happen */
3521: php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to get cipher");
3522: goto clean_exit;
3523: }
3524:
3525: p7 = PKCS7_encrypt(recipcerts, infile, (EVP_CIPHER*)cipher, flags);
3526:
3527: if (p7 == NULL) {
3528: goto clean_exit;
3529: }
3530:
3531: /* tack on extra headers */
3532: if (zheaders) {
3533: zend_hash_internal_pointer_reset_ex(HASH_OF(zheaders), &hpos);
3534: while(zend_hash_get_current_data_ex(HASH_OF(zheaders), (void**)&zcertval, &hpos) == SUCCESS) {
3535: strindex = NULL;
3536: zend_hash_get_current_key_ex(HASH_OF(zheaders), &strindex, &strindexlen, &intindex, 0, &hpos);
3537:
3538: convert_to_string_ex(zcertval);
3539:
3540: if (strindex) {
3541: BIO_printf(outfile, "%s: %s\n", strindex, Z_STRVAL_PP(zcertval));
3542: } else {
3543: BIO_printf(outfile, "%s\n", Z_STRVAL_PP(zcertval));
3544: }
3545:
3546: zend_hash_move_forward_ex(HASH_OF(zheaders), &hpos);
3547: }
3548: }
3549:
3550: (void)BIO_reset(infile);
3551:
3552: /* write the encrypted data */
3553: SMIME_write_PKCS7(outfile, p7, infile, flags);
3554:
3555: RETVAL_TRUE;
3556:
3557: clean_exit:
3558: PKCS7_free(p7);
3559: BIO_free(infile);
3560: BIO_free(outfile);
3561: if (recipcerts) {
3562: sk_X509_pop_free(recipcerts, X509_free);
3563: }
3564: }
3565: /* }}} */
3566:
3567: /* {{{ proto bool openssl_pkcs7_sign(string infile, string outfile, mixed signcert, mixed signkey, array headers [, long flags [, string extracertsfilename]])
3568: 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 */
3569:
3570: PHP_FUNCTION(openssl_pkcs7_sign)
3571: {
3572: zval ** zcert, ** zprivkey, * zheaders;
3573: zval ** hval;
3574: X509 * cert = NULL;
3575: EVP_PKEY * privkey = NULL;
3576: long flags = PKCS7_DETACHED;
3577: PKCS7 * p7 = NULL;
3578: BIO * infile = NULL, * outfile = NULL;
3579: STACK_OF(X509) *others = NULL;
3580: long certresource = -1, keyresource = -1;
3581: ulong intindex;
3582: uint strindexlen;
3583: HashPosition hpos;
3584: char * strindex;
3585: char * infilename; int infilename_len;
3586: char * outfilename; int outfilename_len;
3587: char * extracertsfilename = NULL; int extracertsfilename_len;
3588:
1.1.1.2 ! misho 3589: if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ppZZa!|lp",
1.1 misho 3590: &infilename, &infilename_len, &outfilename, &outfilename_len,
3591: &zcert, &zprivkey, &zheaders, &flags, &extracertsfilename,
3592: &extracertsfilename_len) == FAILURE) {
3593: return;
3594: }
3595:
1.1.1.2 ! misho 3596: RETVAL_FALSE;
1.1 misho 3597:
3598: if (extracertsfilename) {
3599: others = load_all_certs_from_file(extracertsfilename);
1.1.1.2 ! misho 3600: if (others == NULL) {
1.1 misho 3601: goto clean_exit;
3602: }
3603: }
3604:
3605: privkey = php_openssl_evp_from_zval(zprivkey, 0, "", 0, &keyresource TSRMLS_CC);
3606: if (privkey == NULL) {
3607: php_error_docref(NULL TSRMLS_CC, E_WARNING, "error getting private key");
3608: goto clean_exit;
3609: }
3610:
3611: cert = php_openssl_x509_from_zval(zcert, 0, &certresource TSRMLS_CC);
3612: if (cert == NULL) {
3613: php_error_docref(NULL TSRMLS_CC, E_WARNING, "error getting cert");
3614: goto clean_exit;
3615: }
3616:
1.1.1.2 ! misho 3617: if (php_openssl_open_base_dir_chk(infilename TSRMLS_CC) || php_openssl_open_base_dir_chk(outfilename TSRMLS_CC)) {
1.1 misho 3618: goto clean_exit;
3619: }
3620:
3621: infile = BIO_new_file(infilename, "r");
3622: if (infile == NULL) {
3623: php_error_docref(NULL TSRMLS_CC, E_WARNING, "error opening input file %s!", infilename);
3624: goto clean_exit;
3625: }
3626:
3627: outfile = BIO_new_file(outfilename, "w");
3628: if (outfile == NULL) {
3629: php_error_docref(NULL TSRMLS_CC, E_WARNING, "error opening output file %s!", outfilename);
3630: goto clean_exit;
3631: }
3632:
3633: p7 = PKCS7_sign(cert, privkey, others, infile, flags);
3634: if (p7 == NULL) {
3635: php_error_docref(NULL TSRMLS_CC, E_WARNING, "error creating PKCS7 structure!");
3636: goto clean_exit;
3637: }
3638:
3639: (void)BIO_reset(infile);
3640:
3641: /* tack on extra headers */
3642: if (zheaders) {
3643: zend_hash_internal_pointer_reset_ex(HASH_OF(zheaders), &hpos);
3644: while(zend_hash_get_current_data_ex(HASH_OF(zheaders), (void**)&hval, &hpos) == SUCCESS) {
3645: strindex = NULL;
3646: zend_hash_get_current_key_ex(HASH_OF(zheaders), &strindex, &strindexlen, &intindex, 0, &hpos);
3647:
3648: convert_to_string_ex(hval);
3649:
3650: if (strindex) {
3651: BIO_printf(outfile, "%s: %s\n", strindex, Z_STRVAL_PP(hval));
3652: } else {
3653: BIO_printf(outfile, "%s\n", Z_STRVAL_PP(hval));
3654: }
3655: zend_hash_move_forward_ex(HASH_OF(zheaders), &hpos);
3656: }
3657: }
3658: /* write the signed data */
3659: SMIME_write_PKCS7(outfile, p7, infile, flags);
3660:
3661: RETVAL_TRUE;
3662:
3663: clean_exit:
3664: PKCS7_free(p7);
3665: BIO_free(infile);
3666: BIO_free(outfile);
3667: if (others) {
3668: sk_X509_pop_free(others, X509_free);
3669: }
3670: if (privkey && keyresource == -1) {
3671: EVP_PKEY_free(privkey);
3672: }
3673: if (cert && certresource == -1) {
3674: X509_free(cert);
3675: }
3676: }
3677: /* }}} */
3678:
3679: /* {{{ proto bool openssl_pkcs7_decrypt(string infilename, string outfilename, mixed recipcert [, mixed recipkey])
3680: 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 */
3681:
3682: PHP_FUNCTION(openssl_pkcs7_decrypt)
3683: {
3684: zval ** recipcert, ** recipkey = NULL;
3685: X509 * cert = NULL;
3686: EVP_PKEY * key = NULL;
3687: long certresval, keyresval;
3688: BIO * in = NULL, * out = NULL, * datain = NULL;
3689: PKCS7 * p7 = NULL;
3690: char * infilename; int infilename_len;
3691: char * outfilename; int outfilename_len;
3692:
1.1.1.2 ! misho 3693: if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ppZ|Z", &infilename, &infilename_len,
1.1 misho 3694: &outfilename, &outfilename_len, &recipcert, &recipkey) == FAILURE) {
3695: return;
3696: }
3697:
1.1.1.2 ! misho 3698: RETVAL_FALSE;
1.1 misho 3699:
3700: cert = php_openssl_x509_from_zval(recipcert, 0, &certresval TSRMLS_CC);
3701: if (cert == NULL) {
3702: php_error_docref(NULL TSRMLS_CC, E_WARNING, "unable to coerce parameter 3 to x509 cert");
3703: goto clean_exit;
3704: }
3705:
3706: key = php_openssl_evp_from_zval(recipkey ? recipkey : recipcert, 0, "", 0, &keyresval TSRMLS_CC);
3707: if (key == NULL) {
3708: php_error_docref(NULL TSRMLS_CC, E_WARNING, "unable to get private key");
3709: goto clean_exit;
3710: }
1.1.1.2 ! misho 3711:
! 3712: if (php_openssl_open_base_dir_chk(infilename TSRMLS_CC) || php_openssl_open_base_dir_chk(outfilename TSRMLS_CC)) {
1.1 misho 3713: goto clean_exit;
3714: }
3715:
3716: in = BIO_new_file(infilename, "r");
3717: if (in == NULL) {
3718: goto clean_exit;
3719: }
3720: out = BIO_new_file(outfilename, "w");
3721: if (out == NULL) {
3722: goto clean_exit;
3723: }
3724:
3725: p7 = SMIME_read_PKCS7(in, &datain);
3726:
3727: if (p7 == NULL) {
3728: goto clean_exit;
3729: }
1.1.1.2 ! misho 3730: if (PKCS7_decrypt(p7, key, cert, out, PKCS7_DETACHED)) {
1.1 misho 3731: RETVAL_TRUE;
3732: }
3733: clean_exit:
3734: PKCS7_free(p7);
3735: BIO_free(datain);
3736: BIO_free(in);
3737: BIO_free(out);
3738: if (cert && certresval == -1) {
3739: X509_free(cert);
3740: }
3741: if (key && keyresval == -1) {
3742: EVP_PKEY_free(key);
3743: }
3744: }
3745: /* }}} */
3746:
3747: /* }}} */
3748:
3749: /* {{{ proto bool openssl_private_encrypt(string data, string &crypted, mixed key [, int padding])
3750: Encrypts data with private key */
3751: PHP_FUNCTION(openssl_private_encrypt)
3752: {
3753: zval **key, *crypted;
3754: EVP_PKEY *pkey;
3755: int cryptedlen;
3756: unsigned char *cryptedbuf = NULL;
3757: int successful = 0;
3758: long keyresource = -1;
3759: char * data;
3760: int data_len;
3761: long padding = RSA_PKCS1_PADDING;
3762:
1.1.1.2 ! misho 3763: if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "szZ|l", &data, &data_len, &crypted, &key, &padding) == FAILURE) {
1.1 misho 3764: return;
3765: }
3766: RETVAL_FALSE;
3767:
3768: pkey = php_openssl_evp_from_zval(key, 0, "", 0, &keyresource TSRMLS_CC);
3769:
3770: if (pkey == NULL) {
3771: php_error_docref(NULL TSRMLS_CC, E_WARNING, "key param is not a valid private key");
3772: RETURN_FALSE;
3773: }
3774:
3775: cryptedlen = EVP_PKEY_size(pkey);
3776: cryptedbuf = emalloc(cryptedlen + 1);
3777:
3778: switch (pkey->type) {
3779: case EVP_PKEY_RSA:
3780: case EVP_PKEY_RSA2:
1.1.1.2 ! misho 3781: successful = (RSA_private_encrypt(data_len,
! 3782: (unsigned char *)data,
! 3783: cryptedbuf,
! 3784: pkey->pkey.rsa,
1.1 misho 3785: padding) == cryptedlen);
3786: break;
3787: default:
3788: php_error_docref(NULL TSRMLS_CC, E_WARNING, "key type not supported in this PHP build!");
3789: }
3790:
3791: if (successful) {
3792: zval_dtor(crypted);
3793: cryptedbuf[cryptedlen] = '\0';
3794: ZVAL_STRINGL(crypted, (char *)cryptedbuf, cryptedlen, 0);
3795: cryptedbuf = NULL;
3796: RETVAL_TRUE;
3797: }
3798: if (cryptedbuf) {
3799: efree(cryptedbuf);
3800: }
1.1.1.2 ! misho 3801: if (keyresource == -1) {
1.1 misho 3802: EVP_PKEY_free(pkey);
3803: }
3804: }
3805: /* }}} */
3806:
3807: /* {{{ proto bool openssl_private_decrypt(string data, string &decrypted, mixed key [, int padding])
3808: Decrypts data with private key */
3809: PHP_FUNCTION(openssl_private_decrypt)
3810: {
3811: zval **key, *crypted;
3812: EVP_PKEY *pkey;
3813: int cryptedlen;
3814: unsigned char *cryptedbuf = NULL;
3815: unsigned char *crypttemp;
3816: int successful = 0;
3817: long padding = RSA_PKCS1_PADDING;
3818: long keyresource = -1;
3819: char * data;
3820: int data_len;
3821:
3822: if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "szZ|l", &data, &data_len, &crypted, &key, &padding) == FAILURE) {
3823: return;
3824: }
3825: RETVAL_FALSE;
3826:
3827: pkey = php_openssl_evp_from_zval(key, 0, "", 0, &keyresource TSRMLS_CC);
3828: if (pkey == NULL) {
3829: php_error_docref(NULL TSRMLS_CC, E_WARNING, "key parameter is not a valid private key");
3830: RETURN_FALSE;
3831: }
3832:
3833: cryptedlen = EVP_PKEY_size(pkey);
3834: crypttemp = emalloc(cryptedlen + 1);
3835:
3836: switch (pkey->type) {
3837: case EVP_PKEY_RSA:
3838: case EVP_PKEY_RSA2:
1.1.1.2 ! misho 3839: cryptedlen = RSA_private_decrypt(data_len,
! 3840: (unsigned char *)data,
! 3841: crypttemp,
! 3842: pkey->pkey.rsa,
1.1 misho 3843: padding);
3844: if (cryptedlen != -1) {
3845: cryptedbuf = emalloc(cryptedlen + 1);
3846: memcpy(cryptedbuf, crypttemp, cryptedlen);
3847: successful = 1;
3848: }
3849: break;
3850: default:
3851: php_error_docref(NULL TSRMLS_CC, E_WARNING, "key type not supported in this PHP build!");
3852: }
3853:
3854: efree(crypttemp);
3855:
3856: if (successful) {
3857: zval_dtor(crypted);
3858: cryptedbuf[cryptedlen] = '\0';
3859: ZVAL_STRINGL(crypted, (char *)cryptedbuf, cryptedlen, 0);
3860: cryptedbuf = NULL;
3861: RETVAL_TRUE;
3862: }
3863:
3864: if (keyresource == -1) {
3865: EVP_PKEY_free(pkey);
3866: }
1.1.1.2 ! misho 3867: if (cryptedbuf) {
1.1 misho 3868: efree(cryptedbuf);
3869: }
3870: }
3871: /* }}} */
3872:
3873: /* {{{ proto bool openssl_public_encrypt(string data, string &crypted, mixed key [, int padding])
3874: Encrypts data with public key */
3875: PHP_FUNCTION(openssl_public_encrypt)
3876: {
3877: zval **key, *crypted;
3878: EVP_PKEY *pkey;
3879: int cryptedlen;
3880: unsigned char *cryptedbuf;
3881: int successful = 0;
3882: long keyresource = -1;
3883: long padding = RSA_PKCS1_PADDING;
3884: char * data;
3885: int data_len;
3886:
3887: if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "szZ|l", &data, &data_len, &crypted, &key, &padding) == FAILURE)
3888: return;
3889:
3890: RETVAL_FALSE;
1.1.1.2 ! misho 3891:
1.1 misho 3892: pkey = php_openssl_evp_from_zval(key, 1, NULL, 0, &keyresource TSRMLS_CC);
3893: if (pkey == NULL) {
3894: php_error_docref(NULL TSRMLS_CC, E_WARNING, "key parameter is not a valid public key");
3895: RETURN_FALSE;
3896: }
3897:
3898: cryptedlen = EVP_PKEY_size(pkey);
3899: cryptedbuf = emalloc(cryptedlen + 1);
3900:
3901: switch (pkey->type) {
3902: case EVP_PKEY_RSA:
3903: case EVP_PKEY_RSA2:
1.1.1.2 ! misho 3904: successful = (RSA_public_encrypt(data_len,
! 3905: (unsigned char *)data,
! 3906: cryptedbuf,
! 3907: pkey->pkey.rsa,
1.1 misho 3908: padding) == cryptedlen);
3909: break;
3910: default:
3911: php_error_docref(NULL TSRMLS_CC, E_WARNING, "key type not supported in this PHP build!");
3912:
3913: }
3914:
3915: if (successful) {
3916: zval_dtor(crypted);
3917: cryptedbuf[cryptedlen] = '\0';
3918: ZVAL_STRINGL(crypted, (char *)cryptedbuf, cryptedlen, 0);
3919: cryptedbuf = NULL;
3920: RETVAL_TRUE;
3921: }
3922: if (keyresource == -1) {
3923: EVP_PKEY_free(pkey);
3924: }
3925: if (cryptedbuf) {
3926: efree(cryptedbuf);
3927: }
3928: }
3929: /* }}} */
3930:
3931: /* {{{ proto bool openssl_public_decrypt(string data, string &crypted, resource key [, int padding])
3932: Decrypts data with public key */
3933: PHP_FUNCTION(openssl_public_decrypt)
3934: {
3935: zval **key, *crypted;
3936: EVP_PKEY *pkey;
3937: int cryptedlen;
3938: unsigned char *cryptedbuf = NULL;
3939: unsigned char *crypttemp;
3940: int successful = 0;
3941: long keyresource = -1;
3942: long padding = RSA_PKCS1_PADDING;
3943: char * data;
3944: int data_len;
3945:
3946: if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "szZ|l", &data, &data_len, &crypted, &key, &padding) == FAILURE) {
3947: return;
3948: }
3949: RETVAL_FALSE;
1.1.1.2 ! misho 3950:
1.1 misho 3951: pkey = php_openssl_evp_from_zval(key, 1, NULL, 0, &keyresource TSRMLS_CC);
3952: if (pkey == NULL) {
3953: php_error_docref(NULL TSRMLS_CC, E_WARNING, "key parameter is not a valid public key");
3954: RETURN_FALSE;
3955: }
3956:
3957: cryptedlen = EVP_PKEY_size(pkey);
3958: crypttemp = emalloc(cryptedlen + 1);
3959:
3960: switch (pkey->type) {
3961: case EVP_PKEY_RSA:
3962: case EVP_PKEY_RSA2:
1.1.1.2 ! misho 3963: cryptedlen = RSA_public_decrypt(data_len,
! 3964: (unsigned char *)data,
! 3965: crypttemp,
! 3966: pkey->pkey.rsa,
1.1 misho 3967: padding);
3968: if (cryptedlen != -1) {
3969: cryptedbuf = emalloc(cryptedlen + 1);
3970: memcpy(cryptedbuf, crypttemp, cryptedlen);
3971: successful = 1;
3972: }
3973: break;
1.1.1.2 ! misho 3974:
1.1 misho 3975: default:
3976: php_error_docref(NULL TSRMLS_CC, E_WARNING, "key type not supported in this PHP build!");
1.1.1.2 ! misho 3977:
1.1 misho 3978: }
3979:
3980: efree(crypttemp);
3981:
3982: if (successful) {
3983: zval_dtor(crypted);
3984: cryptedbuf[cryptedlen] = '\0';
3985: ZVAL_STRINGL(crypted, (char *)cryptedbuf, cryptedlen, 0);
3986: cryptedbuf = NULL;
3987: RETVAL_TRUE;
3988: }
3989:
3990: if (cryptedbuf) {
3991: efree(cryptedbuf);
3992: }
3993: if (keyresource == -1) {
3994: EVP_PKEY_free(pkey);
3995: }
3996: }
3997: /* }}} */
3998:
3999: /* {{{ proto mixed openssl_error_string(void)
4000: Returns a description of the last error, and alters the index of the error messages. Returns false when the are no more messages */
4001: PHP_FUNCTION(openssl_error_string)
4002: {
4003: char buf[512];
4004: unsigned long val;
4005:
4006: if (zend_parse_parameters_none() == FAILURE) {
4007: return;
4008: }
4009:
4010: val = ERR_get_error();
4011: if (val) {
4012: RETURN_STRING(ERR_error_string(val, buf), 1);
4013: } else {
4014: RETURN_FALSE;
4015: }
4016: }
4017: /* }}} */
4018:
4019: /* {{{ proto bool openssl_sign(string data, &string signature, mixed key[, mixed method])
4020: Signs data */
4021: PHP_FUNCTION(openssl_sign)
4022: {
4023: zval **key, *signature;
4024: EVP_PKEY *pkey;
4025: int siglen;
4026: unsigned char *sigbuf;
4027: long keyresource = -1;
4028: char * data;
4029: int data_len;
4030: EVP_MD_CTX md_ctx;
4031: zval *method = NULL;
4032: long signature_algo = OPENSSL_ALGO_SHA1;
4033: const EVP_MD *mdtype;
4034:
4035: if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "szZ|z", &data, &data_len, &signature, &key, &method) == FAILURE) {
4036: return;
4037: }
4038: pkey = php_openssl_evp_from_zval(key, 0, "", 0, &keyresource TSRMLS_CC);
4039: if (pkey == NULL) {
4040: php_error_docref(NULL TSRMLS_CC, E_WARNING, "supplied key param cannot be coerced into a private key");
4041: RETURN_FALSE;
4042: }
4043:
4044: if (method == NULL || Z_TYPE_P(method) == IS_LONG) {
4045: if (method != NULL) {
4046: signature_algo = Z_LVAL_P(method);
4047: }
4048: mdtype = php_openssl_get_evp_md_from_algo(signature_algo);
4049: } else if (Z_TYPE_P(method) == IS_STRING) {
4050: mdtype = EVP_get_digestbyname(Z_STRVAL_P(method));
4051: } else {
4052: php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unknown signature algorithm.");
4053: RETURN_FALSE;
4054: }
4055: if (!mdtype) {
4056: php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unknown signature algorithm.");
4057: RETURN_FALSE;
4058: }
4059:
4060: siglen = EVP_PKEY_size(pkey);
4061: sigbuf = emalloc(siglen + 1);
4062:
4063: EVP_SignInit(&md_ctx, mdtype);
4064: EVP_SignUpdate(&md_ctx, data, data_len);
4065: if (EVP_SignFinal (&md_ctx, sigbuf,(unsigned int *)&siglen, pkey)) {
4066: zval_dtor(signature);
4067: sigbuf[siglen] = '\0';
4068: ZVAL_STRINGL(signature, (char *)sigbuf, siglen, 0);
4069: RETVAL_TRUE;
4070: } else {
4071: efree(sigbuf);
4072: RETVAL_FALSE;
4073: }
4074: EVP_MD_CTX_cleanup(&md_ctx);
4075: if (keyresource == -1) {
4076: EVP_PKEY_free(pkey);
4077: }
4078: }
4079: /* }}} */
4080:
4081: /* {{{ proto int openssl_verify(string data, string signature, mixed key[, mixed method])
4082: Verifys data */
4083: PHP_FUNCTION(openssl_verify)
4084: {
4085: zval **key;
4086: EVP_PKEY *pkey;
4087: int err;
4088: EVP_MD_CTX md_ctx;
4089: const EVP_MD *mdtype;
4090: long keyresource = -1;
4091: char * data; int data_len;
4092: char * signature; int signature_len;
4093: zval *method = NULL;
4094: long signature_algo = OPENSSL_ALGO_SHA1;
1.1.1.2 ! misho 4095:
1.1 misho 4096: if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ssZ|z", &data, &data_len, &signature, &signature_len, &key, &method) == FAILURE) {
4097: return;
4098: }
4099:
4100: if (method == NULL || Z_TYPE_P(method) == IS_LONG) {
4101: if (method != NULL) {
4102: signature_algo = Z_LVAL_P(method);
4103: }
4104: mdtype = php_openssl_get_evp_md_from_algo(signature_algo);
4105: } else if (Z_TYPE_P(method) == IS_STRING) {
4106: mdtype = EVP_get_digestbyname(Z_STRVAL_P(method));
4107: } else {
4108: php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unknown signature algorithm.");
4109: RETURN_FALSE;
4110: }
4111: if (!mdtype) {
4112: php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unknown signature algorithm.");
4113: RETURN_FALSE;
4114: }
4115:
4116: pkey = php_openssl_evp_from_zval(key, 1, NULL, 0, &keyresource TSRMLS_CC);
4117: if (pkey == NULL) {
4118: php_error_docref(NULL TSRMLS_CC, E_WARNING, "supplied key param cannot be coerced into a public key");
4119: RETURN_FALSE;
4120: }
4121:
4122: EVP_VerifyInit (&md_ctx, mdtype);
4123: EVP_VerifyUpdate (&md_ctx, data, data_len);
4124: err = EVP_VerifyFinal (&md_ctx, (unsigned char *)signature, signature_len, pkey);
4125: EVP_MD_CTX_cleanup(&md_ctx);
4126:
4127: if (keyresource == -1) {
4128: EVP_PKEY_free(pkey);
4129: }
4130: RETURN_LONG(err);
4131: }
4132: /* }}} */
4133:
4134: /* {{{ proto int openssl_seal(string data, &string sealdata, &array ekeys, array pubkeys)
4135: Seals data */
4136: PHP_FUNCTION(openssl_seal)
4137: {
4138: zval *pubkeys, **pubkey, *sealdata, *ekeys;
4139: HashTable *pubkeysht;
4140: HashPosition pos;
4141: EVP_PKEY **pkeys;
4142: long * key_resources; /* so we know what to cleanup */
4143: int i, len1, len2, *eksl, nkeys;
4144: unsigned char *buf = NULL, **eks;
4145: char * data; int data_len;
4146: char *method =NULL;
4147: int method_len = 0;
4148: const EVP_CIPHER *cipher;
4149: EVP_CIPHER_CTX ctx;
4150:
4151: if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "szza/|s", &data, &data_len, &sealdata, &ekeys, &pubkeys, &method, &method_len) == FAILURE) {
4152: return;
4153: }
1.1.1.2 ! misho 4154:
1.1 misho 4155: pubkeysht = HASH_OF(pubkeys);
4156: nkeys = pubkeysht ? zend_hash_num_elements(pubkeysht) : 0;
4157: if (!nkeys) {
4158: php_error_docref(NULL TSRMLS_CC, E_WARNING, "Fourth argument to openssl_seal() must be a non-empty array");
4159: RETURN_FALSE;
4160: }
4161:
4162: if (method) {
4163: cipher = EVP_get_cipherbyname(method);
4164: if (!cipher) {
4165: php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unknown signature algorithm.");
4166: RETURN_FALSE;
4167: }
4168: } else {
4169: cipher = EVP_rc4();
4170: }
4171:
4172: pkeys = safe_emalloc(nkeys, sizeof(*pkeys), 0);
4173: eksl = safe_emalloc(nkeys, sizeof(*eksl), 0);
4174: eks = safe_emalloc(nkeys, sizeof(*eks), 0);
4175: memset(eks, 0, sizeof(*eks) * nkeys);
4176: key_resources = safe_emalloc(nkeys, sizeof(long), 0);
4177: memset(key_resources, 0, sizeof(*key_resources) * nkeys);
4178:
4179: /* get the public keys we are using to seal this data */
4180: zend_hash_internal_pointer_reset_ex(pubkeysht, &pos);
4181: i = 0;
4182: while (zend_hash_get_current_data_ex(pubkeysht, (void **) &pubkey,
4183: &pos) == SUCCESS) {
4184: pkeys[i] = php_openssl_evp_from_zval(pubkey, 1, NULL, 0, &key_resources[i] TSRMLS_CC);
4185: if (pkeys[i] == NULL) {
4186: php_error_docref(NULL TSRMLS_CC, E_WARNING, "not a public key (%dth member of pubkeys)", i+1);
4187: RETVAL_FALSE;
4188: goto clean_exit;
4189: }
4190: eks[i] = emalloc(EVP_PKEY_size(pkeys[i]) + 1);
4191: zend_hash_move_forward_ex(pubkeysht, &pos);
4192: i++;
4193: }
4194:
4195: if (!EVP_EncryptInit(&ctx,cipher,NULL,NULL)) {
4196: RETVAL_FALSE;
4197: goto clean_exit;
4198: }
4199:
4200: #if 0
4201: /* Need this if allow ciphers that require initialization vector */
4202: ivlen = EVP_CIPHER_CTX_iv_length(&ctx);
4203: iv = ivlen ? emalloc(ivlen + 1) : NULL;
4204: #endif
4205: /* allocate one byte extra to make room for \0 */
4206: buf = emalloc(data_len + EVP_CIPHER_CTX_block_size(&ctx));
4207:
4208: if (!EVP_SealInit(&ctx, cipher, eks, eksl, NULL, pkeys, nkeys) || !EVP_SealUpdate(&ctx, buf, &len1, (unsigned char *)data, data_len)) {
4209: RETVAL_FALSE;
4210: efree(buf);
4211: goto clean_exit;
4212: }
4213:
4214: EVP_SealFinal(&ctx, buf + len1, &len2);
4215:
4216: if (len1 + len2 > 0) {
4217: zval_dtor(sealdata);
4218: buf[len1 + len2] = '\0';
4219: buf = erealloc(buf, len1 + len2 + 1);
4220: ZVAL_STRINGL(sealdata, (char *)buf, len1 + len2, 0);
4221:
4222: zval_dtor(ekeys);
4223: array_init(ekeys);
4224: for (i=0; i<nkeys; i++) {
4225: eks[i][eksl[i]] = '\0';
4226: add_next_index_stringl(ekeys, erealloc(eks[i], eksl[i] + 1), eksl[i], 0);
4227: eks[i] = NULL;
4228: }
4229: #if 0
4230: /* If allow ciphers that need IV, we need this */
4231: zval_dtor(*ivec);
4232: if (ivlen) {
4233: iv[ivlen] = '\0';
4234: ZVAL_STRINGL(*ivec, erealloc(iv, ivlen + 1), ivlen, 0);
4235: } else {
4236: ZVAL_EMPTY_STRING(*ivec);
4237: }
4238: #endif
4239: } else {
4240: efree(buf);
4241: }
4242: RETVAL_LONG(len1 + len2);
4243:
4244: clean_exit:
4245: for (i=0; i<nkeys; i++) {
4246: if (key_resources[i] == -1) {
4247: EVP_PKEY_free(pkeys[i]);
4248: }
1.1.1.2 ! misho 4249: if (eks[i]) {
1.1 misho 4250: efree(eks[i]);
4251: }
4252: }
4253: efree(eks);
4254: efree(eksl);
4255: efree(pkeys);
4256: efree(key_resources);
4257: }
4258: /* }}} */
4259:
4260: /* {{{ proto bool openssl_open(string data, &string opendata, string ekey, mixed privkey)
4261: Opens data */
4262: PHP_FUNCTION(openssl_open)
4263: {
4264: zval **privkey, *opendata;
4265: EVP_PKEY *pkey;
4266: int len1, len2;
4267: unsigned char *buf;
4268: long keyresource = -1;
4269: EVP_CIPHER_CTX ctx;
4270: char * data; int data_len;
4271: char * ekey; int ekey_len;
4272: char *method =NULL;
4273: int method_len = 0;
4274: const EVP_CIPHER *cipher;
4275:
4276: if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "szsZ|s", &data, &data_len, &opendata, &ekey, &ekey_len, &privkey, &method, &method_len) == FAILURE) {
4277: return;
4278: }
4279:
4280: pkey = php_openssl_evp_from_zval(privkey, 0, "", 0, &keyresource TSRMLS_CC);
4281: if (pkey == NULL) {
4282: php_error_docref(NULL TSRMLS_CC, E_WARNING, "unable to coerce parameter 4 into a private key");
4283: RETURN_FALSE;
4284: }
4285:
4286: if (method) {
4287: cipher = EVP_get_cipherbyname(method);
4288: if (!cipher) {
4289: php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unknown signature algorithm.");
4290: RETURN_FALSE;
4291: }
4292: } else {
4293: cipher = EVP_rc4();
4294: }
1.1.1.2 ! misho 4295:
1.1 misho 4296: buf = emalloc(data_len + 1);
4297:
4298: if (EVP_OpenInit(&ctx, cipher, (unsigned char *)ekey, ekey_len, NULL, pkey) && EVP_OpenUpdate(&ctx, buf, &len1, (unsigned char *)data, data_len)) {
4299: if (!EVP_OpenFinal(&ctx, buf + len1, &len2) || (len1 + len2 == 0)) {
4300: efree(buf);
1.1.1.2 ! misho 4301: if (keyresource == -1) {
1.1 misho 4302: EVP_PKEY_free(pkey);
4303: }
4304: RETURN_FALSE;
4305: }
4306: } else {
4307: efree(buf);
4308: if (keyresource == -1) {
4309: EVP_PKEY_free(pkey);
4310: }
4311: RETURN_FALSE;
4312: }
4313: if (keyresource == -1) {
4314: EVP_PKEY_free(pkey);
4315: }
4316: zval_dtor(opendata);
4317: buf[len1 + len2] = '\0';
4318: ZVAL_STRINGL(opendata, erealloc(buf, len1 + len2 + 1), len1 + len2, 0);
4319: RETURN_TRUE;
4320: }
4321: /* }}} */
4322:
4323: /* SSL verification functions */
4324:
4325: #define GET_VER_OPT(name) (stream->context && SUCCESS == php_stream_context_get_option(stream->context, "ssl", name, &val))
4326: #define GET_VER_OPT_STRING(name, str) if (GET_VER_OPT(name)) { convert_to_string_ex(val); str = Z_STRVAL_PP(val); }
4327:
4328: static int verify_callback(int preverify_ok, X509_STORE_CTX *ctx) /* {{{ */
4329: {
4330: php_stream *stream;
4331: SSL *ssl;
4332: X509 *err_cert;
4333: int err, depth, ret;
4334: zval **val;
4335:
4336: ret = preverify_ok;
4337:
4338: /* determine the status for the current cert */
4339: err_cert = X509_STORE_CTX_get_current_cert(ctx);
4340: err = X509_STORE_CTX_get_error(ctx);
4341: depth = X509_STORE_CTX_get_error_depth(ctx);
4342:
4343: /* conjure the stream & context to use */
4344: ssl = X509_STORE_CTX_get_ex_data(ctx, SSL_get_ex_data_X509_STORE_CTX_idx());
4345: stream = (php_stream*)SSL_get_ex_data(ssl, ssl_stream_data_index);
4346:
4347: /* if allow_self_signed is set, make sure that verification succeeds */
4348: if (err == X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT && GET_VER_OPT("allow_self_signed") && zval_is_true(*val)) {
4349: ret = 1;
4350: }
4351:
4352: /* check the depth */
4353: if (GET_VER_OPT("verify_depth")) {
4354: convert_to_long_ex(val);
4355:
4356: if (depth > Z_LVAL_PP(val)) {
4357: ret = 0;
4358: X509_STORE_CTX_set_error(ctx, X509_V_ERR_CERT_CHAIN_TOO_LONG);
4359: }
4360: }
4361:
4362: return ret;
4363:
4364: }
4365: /* }}} */
4366:
4367: int php_openssl_apply_verification_policy(SSL *ssl, X509 *peer, php_stream *stream TSRMLS_DC) /* {{{ */
4368: {
4369: zval **val = NULL;
4370: char *cnmatch = NULL;
4371: X509_NAME *name;
4372: char buf[1024];
4373: int err;
4374:
4375: /* verification is turned off */
4376: if (!(GET_VER_OPT("verify_peer") && zval_is_true(*val))) {
4377: return SUCCESS;
4378: }
4379:
4380: if (peer == NULL) {
4381: php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not get peer certificate");
4382: return FAILURE;
4383: }
4384:
4385: err = SSL_get_verify_result(ssl);
4386: switch (err) {
4387: case X509_V_OK:
4388: /* fine */
4389: break;
4390: case X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT:
4391: if (GET_VER_OPT("allow_self_signed") && zval_is_true(*val)) {
4392: /* allowed */
4393: break;
4394: }
4395: /* not allowed, so fall through */
4396: default:
4397: php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not verify peer: code:%d %s", err, X509_verify_cert_error_string(err));
4398: return FAILURE;
4399: }
4400:
4401: /* if the cert passed the usual checks, apply our own local policies now */
4402:
4403: name = X509_get_subject_name(peer);
4404:
4405: /* Does the common name match ? (used primarily for https://) */
4406: GET_VER_OPT_STRING("CN_match", cnmatch);
4407: if (cnmatch) {
4408: int match = 0;
4409: int name_len = X509_NAME_get_text_by_NID(name, NID_commonName, buf, sizeof(buf));
4410:
4411: if (name_len == -1) {
4412: php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to locate peer certificate CN");
4413: return FAILURE;
4414: } else if (name_len != strlen(buf)) {
4415: php_error_docref(NULL TSRMLS_CC, E_WARNING, "Peer certificate CN=`%.*s' is malformed", name_len, buf);
4416: return FAILURE;
4417: }
4418:
4419: match = strcmp(cnmatch, buf) == 0;
4420: if (!match && strlen(buf) > 3 && buf[0] == '*' && buf[1] == '.') {
4421: /* Try wildcard */
4422:
4423: if (strchr(buf+2, '.')) {
4424: char *tmp = strstr(cnmatch, buf+1);
4425:
4426: match = tmp && strcmp(tmp, buf+2) && tmp == strchr(cnmatch, '.');
4427: }
4428: }
4429:
4430: if (!match) {
4431: /* didn't match */
4432: php_error_docref(NULL TSRMLS_CC, E_WARNING, "Peer certificate CN=`%.*s' did not match expected CN=`%s'", name_len, buf, cnmatch);
4433: return FAILURE;
4434: }
4435: }
4436:
4437: return SUCCESS;
4438: }
4439: /* }}} */
4440:
4441: static int passwd_callback(char *buf, int num, int verify, void *data) /* {{{ */
4442: {
4443: php_stream *stream = (php_stream *)data;
4444: zval **val = NULL;
4445: char *passphrase = NULL;
4446: /* TODO: could expand this to make a callback into PHP user-space */
4447:
4448: GET_VER_OPT_STRING("passphrase", passphrase);
4449:
4450: if (passphrase) {
4451: if (Z_STRLEN_PP(val) < num - 1) {
4452: memcpy(buf, Z_STRVAL_PP(val), Z_STRLEN_PP(val)+1);
4453: return Z_STRLEN_PP(val);
4454: }
4455: }
4456: return 0;
4457: }
4458: /* }}} */
4459:
4460: SSL *php_SSL_new_from_context(SSL_CTX *ctx, php_stream *stream TSRMLS_DC) /* {{{ */
4461: {
4462: zval **val = NULL;
4463: char *cafile = NULL;
4464: char *capath = NULL;
4465: char *certfile = NULL;
4466: char *cipherlist = NULL;
4467: int ok = 1;
4468:
4469: ERR_clear_error();
4470:
4471: /* look at context options in the stream and set appropriate verification flags */
4472: if (GET_VER_OPT("verify_peer") && zval_is_true(*val)) {
4473:
4474: /* turn on verification callback */
4475: SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, verify_callback);
4476:
4477: /* CA stuff */
4478: GET_VER_OPT_STRING("cafile", cafile);
4479: GET_VER_OPT_STRING("capath", capath);
4480:
4481: if (cafile || capath) {
4482: if (!SSL_CTX_load_verify_locations(ctx, cafile, capath)) {
4483: php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to set verify locations `%s' `%s'", cafile, capath);
4484: return NULL;
4485: }
4486: }
4487:
4488: if (GET_VER_OPT("verify_depth")) {
4489: convert_to_long_ex(val);
4490: SSL_CTX_set_verify_depth(ctx, Z_LVAL_PP(val));
4491: }
4492: } else {
4493: SSL_CTX_set_verify(ctx, SSL_VERIFY_NONE, NULL);
4494: }
4495:
4496: /* callback for the passphrase (for localcert) */
4497: if (GET_VER_OPT("passphrase")) {
4498: SSL_CTX_set_default_passwd_cb_userdata(ctx, stream);
4499: SSL_CTX_set_default_passwd_cb(ctx, passwd_callback);
4500: }
4501:
4502: GET_VER_OPT_STRING("ciphers", cipherlist);
4503: if (!cipherlist) {
4504: cipherlist = "DEFAULT";
4505: }
4506: if (SSL_CTX_set_cipher_list(ctx, cipherlist) != 1) {
4507: return NULL;
4508: }
4509:
4510: GET_VER_OPT_STRING("local_cert", certfile);
4511: if (certfile) {
4512: X509 *cert = NULL;
4513: EVP_PKEY *key = NULL;
4514: SSL *tmpssl;
4515: char resolved_path_buff[MAXPATHLEN];
4516: const char * private_key = NULL;
4517:
4518: if (VCWD_REALPATH(certfile, resolved_path_buff)) {
4519: /* a certificate to use for authentication */
4520: if (SSL_CTX_use_certificate_chain_file(ctx, resolved_path_buff) != 1) {
4521: php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to set local cert chain file `%s'; Check that your cafile/capath settings include details of your certificate and its issuer", certfile);
4522: return NULL;
4523: }
4524: GET_VER_OPT_STRING("local_pk", private_key);
4525:
4526: if (private_key) {
4527: char resolved_path_buff_pk[MAXPATHLEN];
4528: if (VCWD_REALPATH(private_key, resolved_path_buff_pk)) {
4529: if (SSL_CTX_use_PrivateKey_file(ctx, resolved_path_buff_pk, SSL_FILETYPE_PEM) != 1) {
4530: php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to set private key file `%s'", resolved_path_buff_pk);
4531: return NULL;
4532: }
4533: }
4534: } else {
4535: if (SSL_CTX_use_PrivateKey_file(ctx, resolved_path_buff, SSL_FILETYPE_PEM) != 1) {
4536: php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to set private key file `%s'", resolved_path_buff);
4537: return NULL;
1.1.1.2 ! misho 4538: }
1.1 misho 4539: }
4540:
4541: tmpssl = SSL_new(ctx);
4542: cert = SSL_get_certificate(tmpssl);
4543:
4544: if (cert) {
4545: key = X509_get_pubkey(cert);
4546: EVP_PKEY_copy_parameters(key, SSL_get_privatekey(tmpssl));
4547: EVP_PKEY_free(key);
4548: }
4549: SSL_free(tmpssl);
4550:
4551: if (!SSL_CTX_check_private_key(ctx)) {
4552: php_error_docref(NULL TSRMLS_CC, E_WARNING, "Private key does not match certificate!");
4553: }
4554: }
4555: }
4556: if (ok) {
4557: SSL *ssl = SSL_new(ctx);
4558:
4559: if (ssl) {
4560: /* map SSL => stream */
4561: SSL_set_ex_data(ssl, ssl_stream_data_index, stream);
4562: }
4563: return ssl;
4564: }
4565:
4566: return NULL;
4567: }
4568: /* }}} */
4569:
4570: static void openssl_add_method_or_alias(const OBJ_NAME *name, void *arg) /* {{{ */
4571: {
4572: add_next_index_string((zval*)arg, (char*)name->name, 1);
4573: }
4574: /* }}} */
4575:
4576: static void openssl_add_method(const OBJ_NAME *name, void *arg) /* {{{ */
4577: {
4578: if (name->alias == 0) {
4579: add_next_index_string((zval*)arg, (char*)name->name, 1);
4580: }
4581: }
4582: /* }}} */
4583:
4584: /* {{{ proto array openssl_get_md_methods([bool aliases = false])
4585: Return array of available digest methods */
4586: PHP_FUNCTION(openssl_get_md_methods)
4587: {
4588: zend_bool aliases = 0;
4589:
4590: if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|b", &aliases) == FAILURE) {
4591: return;
4592: }
4593: array_init(return_value);
4594: OBJ_NAME_do_all_sorted(OBJ_NAME_TYPE_MD_METH,
1.1.1.2 ! misho 4595: aliases ? openssl_add_method_or_alias: openssl_add_method,
1.1 misho 4596: return_value);
4597: }
4598: /* }}} */
4599:
4600: /* {{{ proto array openssl_get_cipher_methods([bool aliases = false])
4601: Return array of available cipher methods */
4602: PHP_FUNCTION(openssl_get_cipher_methods)
4603: {
4604: zend_bool aliases = 0;
4605:
4606: if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|b", &aliases) == FAILURE) {
4607: return;
4608: }
4609: array_init(return_value);
4610: OBJ_NAME_do_all_sorted(OBJ_NAME_TYPE_CIPHER_METH,
1.1.1.2 ! misho 4611: aliases ? openssl_add_method_or_alias: openssl_add_method,
1.1 misho 4612: return_value);
4613: }
4614: /* }}} */
4615:
4616: /* {{{ proto string openssl_digest(string data, string method [, bool raw_output=false])
4617: Computes digest hash value for given data using given method, returns raw or binhex encoded string */
4618: PHP_FUNCTION(openssl_digest)
4619: {
4620: zend_bool raw_output = 0;
4621: char *data, *method;
4622: int data_len, method_len;
4623: const EVP_MD *mdtype;
4624: EVP_MD_CTX md_ctx;
4625: int siglen;
4626: unsigned char *sigbuf;
4627:
4628: if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss|b", &data, &data_len, &method, &method_len, &raw_output) == FAILURE) {
4629: return;
4630: }
4631: mdtype = EVP_get_digestbyname(method);
4632: if (!mdtype) {
4633: php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unknown signature algorithm");
4634: RETURN_FALSE;
4635: }
4636:
4637: siglen = EVP_MD_size(mdtype);
4638: sigbuf = emalloc(siglen + 1);
4639:
4640: EVP_DigestInit(&md_ctx, mdtype);
4641: EVP_DigestUpdate(&md_ctx, (unsigned char *)data, data_len);
4642: if (EVP_DigestFinal (&md_ctx, (unsigned char *)sigbuf, (unsigned int *)&siglen)) {
4643: if (raw_output) {
4644: sigbuf[siglen] = '\0';
4645: RETVAL_STRINGL((char *)sigbuf, siglen, 0);
4646: } else {
4647: int digest_str_len = siglen * 2;
4648: char *digest_str = emalloc(digest_str_len + 1);
4649:
4650: make_digest_ex(digest_str, sigbuf, siglen);
4651: efree(sigbuf);
4652: RETVAL_STRINGL(digest_str, digest_str_len, 0);
4653: }
4654: } else {
4655: efree(sigbuf);
4656: RETVAL_FALSE;
4657: }
4658: }
4659: /* }}} */
4660:
4661: static zend_bool php_openssl_validate_iv(char **piv, int *piv_len, int iv_required_len TSRMLS_DC)
4662: {
4663: char *iv_new;
4664:
4665: /* Best case scenario, user behaved */
4666: if (*piv_len == iv_required_len) {
4667: return 0;
4668: }
4669:
4670: iv_new = ecalloc(1, iv_required_len + 1);
4671:
4672: if (*piv_len <= 0) {
4673: /* BC behavior */
4674: *piv_len = iv_required_len;
4675: *piv = iv_new;
4676: return 1;
4677: }
4678:
4679: if (*piv_len < iv_required_len) {
4680: php_error_docref(NULL TSRMLS_CC, E_WARNING, "IV passed is only %d bytes long, cipher expects an IV of precisely %d bytes, padding with \\0", *piv_len, iv_required_len);
4681: memcpy(iv_new, *piv, *piv_len);
4682: *piv_len = iv_required_len;
4683: *piv = iv_new;
4684: return 1;
4685: }
4686:
4687: php_error_docref(NULL TSRMLS_CC, E_WARNING, "IV passed is %d bytes long which is longer than the %d expected by selected cipher, truncating", *piv_len, iv_required_len);
4688: memcpy(iv_new, *piv, iv_required_len);
4689: *piv_len = iv_required_len;
4690: *piv = iv_new;
4691: return 1;
4692:
4693: }
4694:
1.1.1.2 ! misho 4695: /* {{{ proto string openssl_encrypt(string data, string method, string password [, long options=0 [, string $iv='']])
1.1 misho 4696: Encrypts given data with given method and key, returns raw or base64 encoded string */
4697: PHP_FUNCTION(openssl_encrypt)
4698: {
1.1.1.2 ! misho 4699: long options = 0;
1.1 misho 4700: char *data, *method, *password, *iv = "";
4701: int data_len, method_len, password_len, iv_len = 0, max_iv_len;
4702: const EVP_CIPHER *cipher_type;
4703: EVP_CIPHER_CTX cipher_ctx;
1.1.1.2 ! misho 4704: int i=0, outlen, keylen;
1.1 misho 4705: unsigned char *outbuf, *key;
4706: zend_bool free_iv;
4707:
1.1.1.2 ! misho 4708: if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sss|ls", &data, &data_len, &method, &method_len, &password, &password_len, &options, &iv, &iv_len) == FAILURE) {
1.1 misho 4709: return;
4710: }
4711: cipher_type = EVP_get_cipherbyname(method);
4712: if (!cipher_type) {
4713: php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unknown cipher algorithm");
4714: RETURN_FALSE;
4715: }
4716:
4717: keylen = EVP_CIPHER_key_length(cipher_type);
4718: if (keylen > password_len) {
4719: key = emalloc(keylen);
4720: memset(key, 0, keylen);
4721: memcpy(key, password, password_len);
4722: } else {
4723: key = (unsigned char*)password;
4724: }
4725:
4726: max_iv_len = EVP_CIPHER_iv_length(cipher_type);
4727: if (iv_len <= 0 && max_iv_len > 0) {
4728: php_error_docref(NULL TSRMLS_CC, E_WARNING, "Using an empty Initialization Vector (iv) is potentially insecure and not recommended");
4729: }
4730: free_iv = php_openssl_validate_iv(&iv, &iv_len, max_iv_len TSRMLS_CC);
4731:
4732: outlen = data_len + EVP_CIPHER_block_size(cipher_type);
4733: outbuf = emalloc(outlen + 1);
4734:
4735: EVP_EncryptInit(&cipher_ctx, cipher_type, NULL, NULL);
4736: if (password_len > keylen) {
4737: EVP_CIPHER_CTX_set_key_length(&cipher_ctx, password_len);
4738: }
4739: EVP_EncryptInit_ex(&cipher_ctx, NULL, NULL, key, (unsigned char *)iv);
1.1.1.2 ! misho 4740: if (options & OPENSSL_ZERO_PADDING) {
! 4741: EVP_CIPHER_CTX_set_padding(&cipher_ctx, 0);
! 4742: }
1.1 misho 4743: if (data_len > 0) {
4744: EVP_EncryptUpdate(&cipher_ctx, outbuf, &i, (unsigned char *)data, data_len);
4745: }
4746: outlen = i;
4747: if (EVP_EncryptFinal(&cipher_ctx, (unsigned char *)outbuf + i, &i)) {
4748: outlen += i;
1.1.1.2 ! misho 4749: if (options & OPENSSL_RAW_DATA) {
1.1 misho 4750: outbuf[outlen] = '\0';
4751: RETVAL_STRINGL((char *)outbuf, outlen, 0);
4752: } else {
4753: int base64_str_len;
4754: char *base64_str;
4755:
4756: base64_str = (char*)php_base64_encode(outbuf, outlen, &base64_str_len);
4757: efree(outbuf);
4758: RETVAL_STRINGL(base64_str, base64_str_len, 0);
4759: }
4760: } else {
4761: efree(outbuf);
4762: RETVAL_FALSE;
4763: }
4764: if (key != (unsigned char*)password) {
4765: efree(key);
4766: }
4767: if (free_iv) {
4768: efree(iv);
4769: }
4770: EVP_CIPHER_CTX_cleanup(&cipher_ctx);
4771: }
4772: /* }}} */
4773:
1.1.1.2 ! misho 4774: /* {{{ proto string openssl_decrypt(string data, string method, string password [, long options=0 [, string $iv = '']])
1.1 misho 4775: Takes raw or base64 encoded string and dectupt it using given method and key */
4776: PHP_FUNCTION(openssl_decrypt)
4777: {
1.1.1.2 ! misho 4778: long options = 0;
1.1 misho 4779: char *data, *method, *password, *iv = "";
4780: int data_len, method_len, password_len, iv_len = 0;
4781: const EVP_CIPHER *cipher_type;
4782: EVP_CIPHER_CTX cipher_ctx;
4783: int i, outlen, keylen;
4784: unsigned char *outbuf, *key;
4785: int base64_str_len;
4786: char *base64_str = NULL;
4787: zend_bool free_iv;
4788:
1.1.1.2 ! misho 4789: if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sss|ls", &data, &data_len, &method, &method_len, &password, &password_len, &options, &iv, &iv_len) == FAILURE) {
1.1 misho 4790: return;
4791: }
4792:
4793: if (!method_len) {
4794: php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unknown cipher algorithm");
4795: RETURN_FALSE;
4796: }
4797:
4798: cipher_type = EVP_get_cipherbyname(method);
4799: if (!cipher_type) {
4800: php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unknown cipher algorithm");
4801: RETURN_FALSE;
4802: }
4803:
1.1.1.2 ! misho 4804: if (!(options & OPENSSL_RAW_DATA)) {
1.1 misho 4805: base64_str = (char*)php_base64_decode((unsigned char*)data, data_len, &base64_str_len);
1.1.1.2 ! misho 4806: if (!base64_str) {
! 4807: php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to base64 decode the input");
! 4808: RETURN_FALSE;
! 4809: }
1.1 misho 4810: data_len = base64_str_len;
4811: data = base64_str;
4812: }
4813:
4814: keylen = EVP_CIPHER_key_length(cipher_type);
4815: if (keylen > password_len) {
4816: key = emalloc(keylen);
4817: memset(key, 0, keylen);
4818: memcpy(key, password, password_len);
4819: } else {
4820: key = (unsigned char*)password;
4821: }
4822:
4823: free_iv = php_openssl_validate_iv(&iv, &iv_len, EVP_CIPHER_iv_length(cipher_type) TSRMLS_CC);
4824:
4825: outlen = data_len + EVP_CIPHER_block_size(cipher_type);
4826: outbuf = emalloc(outlen + 1);
4827:
4828: EVP_DecryptInit(&cipher_ctx, cipher_type, NULL, NULL);
4829: if (password_len > keylen) {
4830: EVP_CIPHER_CTX_set_key_length(&cipher_ctx, password_len);
4831: }
4832: EVP_DecryptInit_ex(&cipher_ctx, NULL, NULL, key, (unsigned char *)iv);
1.1.1.2 ! misho 4833: if (options & OPENSSL_ZERO_PADDING) {
! 4834: EVP_CIPHER_CTX_set_padding(&cipher_ctx, 0);
! 4835: }
1.1 misho 4836: EVP_DecryptUpdate(&cipher_ctx, outbuf, &i, (unsigned char *)data, data_len);
4837: outlen = i;
4838: if (EVP_DecryptFinal(&cipher_ctx, (unsigned char *)outbuf + i, &i)) {
4839: outlen += i;
4840: outbuf[outlen] = '\0';
4841: RETVAL_STRINGL((char *)outbuf, outlen, 0);
4842: } else {
4843: efree(outbuf);
4844: RETVAL_FALSE;
4845: }
4846: if (key != (unsigned char*)password) {
4847: efree(key);
4848: }
4849: if (free_iv) {
4850: efree(iv);
4851: }
4852: if (base64_str) {
4853: efree(base64_str);
4854: }
4855: EVP_CIPHER_CTX_cleanup(&cipher_ctx);
4856: }
4857: /* }}} */
4858:
4859: /* {{{ proto int openssl_cipher_iv_length(string $method) */
4860: PHP_FUNCTION(openssl_cipher_iv_length)
4861: {
4862: char *method;
4863: int method_len;
4864: const EVP_CIPHER *cipher_type;
4865:
4866: if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &method, &method_len) == FAILURE) {
4867: return;
4868: }
4869:
4870: if (!method_len) {
4871: php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unknown cipher algorithm");
4872: RETURN_FALSE;
4873: }
4874:
4875: cipher_type = EVP_get_cipherbyname(method);
4876: if (!cipher_type) {
4877: php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unknown cipher algorithm");
4878: RETURN_FALSE;
4879: }
4880:
4881: RETURN_LONG(EVP_CIPHER_iv_length(cipher_type));
4882: }
4883: /* }}} */
4884:
4885:
4886: /* {{{ proto string openssl_dh_compute_key(string pub_key, resource dh_key)
4887: Computes shared sicret for public value of remote DH key and local DH key */
4888: PHP_FUNCTION(openssl_dh_compute_key)
4889: {
4890: zval *key;
4891: char *pub_str;
4892: int pub_len;
4893: EVP_PKEY *pkey;
4894: BIGNUM *pub;
4895: char *data;
4896: int len;
4897:
4898: if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sr", &pub_str, &pub_len, &key) == FAILURE) {
4899: return;
4900: }
4901: ZEND_FETCH_RESOURCE(pkey, EVP_PKEY *, &key, -1, "OpenSSL key", le_key);
4902: if (!pkey || EVP_PKEY_type(pkey->type) != EVP_PKEY_DH || !pkey->pkey.dh) {
4903: RETURN_FALSE;
4904: }
4905:
4906: pub = BN_bin2bn((unsigned char*)pub_str, pub_len, NULL);
4907:
4908: data = emalloc(DH_size(pkey->pkey.dh) + 1);
4909: len = DH_compute_key((unsigned char*)data, pub, pkey->pkey.dh);
4910:
4911: if (len >= 0) {
4912: data[len] = 0;
4913: RETVAL_STRINGL(data, len, 0);
4914: } else {
4915: efree(data);
4916: RETVAL_FALSE;
4917: }
4918:
4919: BN_free(pub);
4920: }
4921: /* }}} */
4922:
4923: /* {{{ proto string openssl_random_pseudo_bytes(integer length [, &bool returned_strong_result])
4924: Returns a string of the length specified filled with random pseudo bytes */
4925: PHP_FUNCTION(openssl_random_pseudo_bytes)
4926: {
4927: long buffer_length;
4928: unsigned char *buffer = NULL;
4929: zval *zstrong_result_returned = NULL;
4930: int strong_result = 0;
4931:
4932: if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l|z", &buffer_length, &zstrong_result_returned) == FAILURE) {
4933: return;
4934: }
4935:
4936: if (buffer_length <= 0) {
4937: RETURN_FALSE;
4938: }
4939:
4940: if (zstrong_result_returned) {
4941: zval_dtor(zstrong_result_returned);
4942: ZVAL_BOOL(zstrong_result_returned, 0);
4943: }
4944:
4945: buffer = emalloc(buffer_length + 1);
4946:
1.1.1.2 ! misho 4947: #ifdef PHP_WIN32
! 4948: strong_result = 1;
! 4949: /* random/urandom equivalent on Windows */
! 4950: if (php_win32_get_random_bytes(buffer, (size_t) buffer_length) == FAILURE) {
! 4951: efree(buffer);
! 4952: if (zstrong_result_returned) {
! 4953: ZVAL_BOOL(zstrong_result_returned, 0);
! 4954: }
! 4955: RETURN_FALSE;
! 4956: }
! 4957: #else
1.1 misho 4958: if ((strong_result = RAND_pseudo_bytes(buffer, buffer_length)) < 0) {
4959: efree(buffer);
1.1.1.2 ! misho 4960: if (zstrong_result_returned) {
! 4961: ZVAL_BOOL(zstrong_result_returned, 0);
! 4962: }
1.1 misho 4963: RETURN_FALSE;
4964: }
1.1.1.2 ! misho 4965: #endif
1.1 misho 4966:
4967: buffer[buffer_length] = 0;
4968: RETVAL_STRINGL((char *)buffer, buffer_length, 0);
4969:
4970: if (zstrong_result_returned) {
4971: ZVAL_BOOL(zstrong_result_returned, strong_result);
4972: }
4973: }
4974: /* }}} */
4975:
4976: /*
4977: * Local variables:
4978: * tab-width: 8
4979: * c-basic-offset: 8
4980: * End:
4981: * vim600: sw=4 ts=4 fdm=marker
4982: * vim<600: sw=4 ts=4
4983: */
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>