xref: /PHP-8.1/ext/openssl/php_openssl.h (revision a80ae97d)
1 /*
2    +----------------------------------------------------------------------+
3    | Copyright (c) The PHP Group                                          |
4    +----------------------------------------------------------------------+
5    | This source file is subject to version 3.01 of the PHP license,      |
6    | that is bundled with this package in the file LICENSE, and is        |
7    | available through the world-wide-web at the following url:           |
8    | https://www.php.net/license/3_01.txt                                 |
9    | If you did not receive a copy of the PHP license and are unable to   |
10    | obtain it through the world-wide-web, please send a note to          |
11    | license@php.net so we can mail you a copy immediately.               |
12    +----------------------------------------------------------------------+
13    | Authors: Stig Venaas <venaas@php.net>                                |
14    |          Wez Furlong <wez@thebrainroom.com                           |
15    +----------------------------------------------------------------------+
16  */
17 
18 #ifndef PHP_OPENSSL_H
19 #define PHP_OPENSSL_H
20 /* HAVE_OPENSSL would include SSL MySQL stuff */
21 #ifdef HAVE_OPENSSL_EXT
22 extern zend_module_entry openssl_module_entry;
23 #define phpext_openssl_ptr &openssl_module_entry
24 
25 #include "php_version.h"
26 #define PHP_OPENSSL_VERSION PHP_VERSION
27 
28 #include <openssl/opensslv.h>
29 #if defined(LIBRESSL_VERSION_NUMBER)
30 /* LibreSSL version check */
31 #if LIBRESSL_VERSION_NUMBER < 0x20700000L
32 #define PHP_OPENSSL_API_VERSION 0x10001
33 #else
34 #define PHP_OPENSSL_API_VERSION 0x10100
35 #endif
36 #else
37 /* OpenSSL version check */
38 #if OPENSSL_VERSION_NUMBER < 0x10100000L
39 #define PHP_OPENSSL_API_VERSION 0x10002
40 #elif OPENSSL_VERSION_NUMBER < 0x30000000L
41 #define PHP_OPENSSL_API_VERSION 0x10100
42 #else
43 #define PHP_OPENSSL_API_VERSION 0x30000
44 #endif
45 #endif
46 
47 #define OPENSSL_RAW_DATA 1
48 #define OPENSSL_ZERO_PADDING 2
49 #define OPENSSL_DONT_ZERO_PAD_KEY 4
50 
51 #define OPENSSL_ERROR_X509_PRIVATE_KEY_VALUES_MISMATCH 0x0B080074
52 
53 /* Used for client-initiated handshake renegotiation DoS protection*/
54 #define OPENSSL_DEFAULT_RENEG_LIMIT 2
55 #define OPENSSL_DEFAULT_RENEG_WINDOW 300
56 #define OPENSSL_DEFAULT_STREAM_VERIFY_DEPTH 9
57 #define OPENSSL_DEFAULT_STREAM_CIPHERS "ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES128-GCM-SHA256:" \
58 	"ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:" \
59 	"DHE-DSS-AES128-GCM-SHA256:kEDH+AESGCM:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA256:" \
60 	"ECDHE-RSA-AES128-SHA:ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES256-SHA384:ECDHE-ECDSA-AES256-SHA384:" \
61 	"ECDHE-RSA-AES256-SHA:ECDHE-ECDSA-AES256-SHA:DHE-RSA-AES128-SHA256:DHE-RSA-AES128-SHA:" \
62 	"DHE-DSS-AES128-SHA256:DHE-RSA-AES256-SHA256:DHE-DSS-AES256-SHA:DHE-RSA-AES256-SHA:AES128-GCM-SHA256:" \
63 	"AES256-GCM-SHA384:AES128:AES256:HIGH:!SSLv2:!aNULL:!eNULL:!EXPORT:!DES:!MD5:!RC4:!ADH"
64 
65 #include <openssl/err.h>
66 
67 #ifdef PHP_WIN32
68 #	define PHP_OPENSSL_API __declspec(dllexport)
69 #elif defined(__GNUC__) && __GNUC__ >= 4
70 #	define PHP_OPENSSL_API __attribute__((visibility("default")))
71 #else
72 #	define PHP_OPENSSL_API
73 #endif
74 
75 struct php_openssl_errors {
76 	int buffer[ERR_NUM_ERRORS];
77 	int top;
78 	int bottom;
79 };
80 
81 ZEND_BEGIN_MODULE_GLOBALS(openssl)
82 	struct php_openssl_errors *errors;
83 ZEND_END_MODULE_GLOBALS(openssl)
84 
85 #define OPENSSL_G(v) ZEND_MODULE_GLOBALS_ACCESSOR(openssl, v)
86 
87 #if defined(ZTS) && defined(COMPILE_DL_OPENSSL)
88 ZEND_TSRMLS_CACHE_EXTERN();
89 #endif
90 
91 php_stream_transport_factory_func php_openssl_ssl_socket_factory;
92 
93 void php_openssl_store_errors(void);
94 
95 PHP_OPENSSL_API zend_long php_openssl_cipher_iv_length(const char *method);
96 PHP_OPENSSL_API zend_string* php_openssl_random_pseudo_bytes(zend_long length);
97 PHP_OPENSSL_API zend_string* php_openssl_encrypt(
98 	const char *data, size_t data_len,
99 	const char *method, size_t method_len,
100 	const char *password, size_t password_len,
101 	zend_long options,
102 	const char *iv, size_t iv_len,
103 	zval *tag, zend_long tag_len,
104 	const char *aad, size_t aad_len);
105 PHP_OPENSSL_API zend_string* php_openssl_decrypt(
106 	const char *data, size_t data_len,
107 	const char *method, size_t method_len,
108 	const char *password, size_t password_len,
109 	zend_long options,
110 	const char *iv, size_t iv_len,
111 	const char *tag, zend_long tag_len,
112 	const char *aad, size_t aad_len);
113 
114 /* OpenSSLCertificate class */
115 
116 typedef struct _php_openssl_certificate_object {
117 	X509 *x509;
118 	zend_object std;
119 } php_openssl_certificate_object;
120 
121 extern zend_class_entry *php_openssl_certificate_ce;
122 
php_openssl_certificate_from_obj(zend_object * obj)123 static inline php_openssl_certificate_object *php_openssl_certificate_from_obj(zend_object *obj) {
124 	return (php_openssl_certificate_object *)((char *)(obj) - XtOffsetOf(php_openssl_certificate_object, std));
125 }
126 
127 #define Z_OPENSSL_CERTIFICATE_P(zv) php_openssl_certificate_from_obj(Z_OBJ_P(zv))
128 
129 PHP_MINIT_FUNCTION(openssl);
130 PHP_MSHUTDOWN_FUNCTION(openssl);
131 PHP_MINFO_FUNCTION(openssl);
132 PHP_GINIT_FUNCTION(openssl);
133 PHP_GSHUTDOWN_FUNCTION(openssl);
134 
135 #ifdef PHP_WIN32
136 #define PHP_OPENSSL_BIO_MODE_R(flags) (((flags) & PKCS7_BINARY) ? "rb" : "r")
137 #define PHP_OPENSSL_BIO_MODE_W(flags) (((flags) & PKCS7_BINARY) ? "wb" : "w")
138 #else
139 #define PHP_OPENSSL_BIO_MODE_R(flags) "r"
140 #define PHP_OPENSSL_BIO_MODE_W(flags) "w"
141 #endif
142 
143 #else
144 
145 #define phpext_openssl_ptr NULL
146 
147 #endif
148 
149 
150 #endif
151