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