Table of contents

Include OpenSSL support of php 5 in Mac OS

PHP Jul 07, 2020 Viewed 88 Comments 0

Issue

Using the openssl_encrypt() method in Mac OS, something went wrong.

Call to undefined function openssl_encrypt()

To use PHP's OpenSSL support I must also compile PHP --with-openssl[=DIR].

$ brew install openssl
$ ./configure --prefix=/opt/php --with-openssl=/Volumes/develop/opt/openssl-1.1.1g
$ make
$ sudo make install

There is no problem in PHP 7, but in PHP 5.4.45/5.5.38/5.6.40, when run the make command, and finally aborted due to error.

/Users/cpm/Downloads/packages/php-5.4.45/ext/openssl/openssl.c:1433:15: error: incomplete definition of type 'struct X509_extension_st'
        p = extension->value->data;
            ~~~~~~~~~^
/Users/cpm/Downloads/packages/php-5.4.45/ext/openssl/openssl.c:1434:20: error: incomplete definition of type 'struct X509_extension_st'
        length = extension->value->length;
                 ~~~~~~~~~^
/Users/cpm/Downloads/packages/php-5.4.45/ext/openssl/openssl.c:1511:10: error: incomplete definition of type 'struct x509_st'
        if (cert->name) {
            ~~~~^
/Users/cpm/Downloads/packages/php-5.4.45/ext/openssl/openssl.c:1512:46: error: incomplete definition of type 'struct x509_st'
                add_assoc_string(return_value, "name", cert->name, 1);
                                                       ~~~~^
/Users/cpm/Downloads/packages/php-5.4.45/ext/openssl/openssl.c:3002:14: error: incomplete definition of type 'struct evp_pkey_st'
        switch (pkey->type) {
                ~~~~^
/Users/cpm/Downloads/packages/php-5.4.45/ext/openssl/openssl.c:3007:47: error: incomplete definition of type 'struct evp_pkey_st'
                        if (pkey->pkey.rsa != NULL && (NULL == pkey->pkey.rsa->p || NULL == pkey->pkey.rsa->q)) {
                                                               ~~~~^
/Users/cpm/Downloads/packages/php-5.4.45/ext/openssl/openssl.c:3007:76: error: incomplete definition of type 'struct evp_pkey_st'
                        if (pkey->pkey.rsa != NULL && (NULL == pkey->pkey.rsa->p || NULL == pkey->pkey.rsa->q)) {
                                                                                            ~~~~^
/Users/cpm/Downloads/packages/php-5.4.45/ext/openssl/openssl.c:3020:20: error: incomplete definition of type 'struct evp_pkey_st'
                        if (NULL == pkey->pkey.dsa->p || NULL == pkey->pkey.dsa->q || NULL == pkey->pkey.dsa->priv_key){
                                    ~~~~^
/Users/cpm/Downloads/packages/php-5.4.45/ext/openssl/openssl.c:3020:49: error: incomplete definition of type 'struct evp_pkey_st'
                        if (NULL == pkey->pkey.dsa->p || NULL == pkey->pkey.dsa->q || NULL == pkey->pkey.dsa->priv_key){
                                                                 ~~~~^
/Users/cpm/Downloads/packages/php-5.4.45/ext/openssl/openssl.c:3020:78: error: incomplete definition of type 'struct evp_pkey_st'
                        if (NULL == pkey->pkey.dsa->p || NULL == pkey->pkey.dsa->q || NULL == pkey->pkey.dsa->priv_key){
                                                                                              ~~~~^
/Users/cpm/Downloads/packages/php-5.4.45/ext/openssl/openssl.c:3029:20: error: incomplete definition of type 'struct evp_pkey_st'
                        if (NULL == pkey->pkey.dh->p || NULL == pkey->pkey.dh->priv_key) {
                                    ~~~~^
/Users/cpm/Downloads/packages/php-5.4.45/ext/openssl/openssl.c:3029:48: error: incomplete definition of type 'struct evp_pkey_st'
                        if (NULL == pkey->pkey.dh->p || NULL == pkey->pkey.dh->priv_key) {
                                                                ~~~~^
/Users/cpm/Downloads/packages/php-5.4.45/ext/openssl/openssl.c:3038:45: error: incomplete definition of type 'struct evp_pkey_st'
                        if ( NULL == EC_KEY_get0_private_key(pkey->pkey.ec)) {
                                                             ~~~~^
/Users/cpm/Downloads/packages/php-5.4.45/ext/openssl/openssl.c:3094:6: error: incomplete definition of type 'struct rsa_st'
                                        OPENSSL_PKEY_SET_BN(Z_ARRVAL_PP(data), rsa, n);
                                        ^                                      ~~~
/Users/cpm/Downloads/packages/php-5.4.45/ext/openssl/openssl.c:3095:6: error: incomplete definition of type 'struct rsa_st'
                                        OPENSSL_PKEY_SET_BN(Z_ARRVAL_PP(data), rsa, e);
                                        ^                                      ~~~
/Users/cpm/Downloads/packages/php-5.4.45/ext/openssl/openssl.c:3096:6: error: incomplete definition of type 'struct rsa_st'
                                        OPENSSL_PKEY_SET_BN(Z_ARRVAL_PP(data), rsa, d);
                                        ^                                      ~~~
/Users/cpm/Downloads/packages/php-5.4.45/ext/openssl/openssl.c:3097:6: error: incomplete definition of type 'struct rsa_st'
                                        OPENSSL_PKEY_SET_BN(Z_ARRVAL_PP(data), rsa, p);
                                        ^                                      ~~~
/Users/cpm/Downloads/packages/php-5.4.45/ext/openssl/openssl.c:3098:6: error: incomplete definition of type 'struct rsa_st'
                                        OPENSSL_PKEY_SET_BN(Z_ARRVAL_PP(data), rsa, q);
                                        ^                                      ~~~

Solution

PHP 5 cannot be maked with the latest version of openssl, so we can use the older the version of openssl.

$ curl -O https://www.openssl.org/source/old/1.0.2/openssl-1.0.2u.tar.gz
$ tar xf openssl-1.0.2u.tar.gz
$ cd openssl-1.0.2u
$ ./Configure darwin64-x86_64-cc --prefix=/opt/openssl-1.0.2u
$ make
$ sudo make install

Change the path of -with-openssl and configure PHP again.

$ ./configure --prefix=/opt/php --with-openssl=/opt/openssl-1.0.2u
$ make
$ sudo make install
Updated Jul 07, 2020