Skip to content

Commit

Permalink
compilable php7 version (#278)
Browse files Browse the repository at this point in the history
* fix comparison signed/unsigned

* php7 with cell/message/keygenerator

* compilable extension with session

* previous arg format
  • Loading branch information
Lagovas authored and vixentael committed Jan 15, 2018
1 parent 43e75b0 commit 08911a6
Show file tree
Hide file tree
Showing 12 changed files with 836 additions and 6 deletions.
35 changes: 35 additions & 0 deletions src/wrappers/themis/php7/config.m4
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#
# Copyright (c) 2015 Cossack Labs Limited
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

PHP_ARG_ENABLE(phpthemis, whether to enable themis support,[ --enable-phpthemis Enable themis support])

if test "$PHP_PHPTHEMIS" = "yes"; then
AC_DEFINE(HAVE_PHPTHEMIS, 1, [Whether you have themis])
for i in /usr/local /usr; do
if test -r $i/lib/libthemis.a && test -r $i/lib/libsoter.a; then
THEMIS_DIR=$i
AC_MSG_RESULT(themis found in $i)
fi
done

if test -z "$THEMIS_DIR"; then
AC_MSG_RESULT(not found)
AC_MSG_ERROR(Please reinstall the libthemis distribution)
fi
PHP_ADD_LIBRARY_WITH_PATH(themis, $THEMIS_DIR/lib, PHPTHEMIS_SHARED_LIBADD)
PHP_SUBST(PHPTHEMIS_SHARED_LIBADD)
PHP_NEW_EXTENSION(phpthemis, php_themis.c php_cell.c php_key_generator.c php_message.c php_session.c, $ext_shared)
fi
211 changes: 211 additions & 0 deletions src/wrappers/themis/php7/php_cell.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,211 @@
/*
* Copyright (c) 2015 Cossack Labs Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <themis/themis_error.h>

#include "php.h"
#include "php_cell.h"
#include "zend_exceptions.h"
#include <themis/themis.h>


ZEND_FUNCTION(phpthemis_scell_seal_encrypt){
char* key;
int key_length;
char* message;
int message_length;
char* context=NULL;
int context_length=0;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss|s", &key, &key_length, &message, &message_length, &context, &context_length) == FAILURE) {
zend_throw_exception(zend_exception_get_default(TSRMLS_C), "Error: phpthemis_scell_seal_encrypt: invalid parameters.", 0 TSRMLS_CC);
RETURN_NULL();
}
size_t encrypted_message_length=0;
if(themis_secure_cell_encrypt_seal((uint8_t*)key, key_length, (uint8_t*)context, context_length, (uint8_t*)message, message_length, NULL, &encrypted_message_length)!=THEMIS_BUFFER_TOO_SMALL){
zend_throw_exception(zend_exception_get_default(TSRMLS_C), "Error: phpthemis_scell_seal_encrypt: encrypted message length determination failed.", 0 TSRMLS_CC);
RETURN_NULL();
}
char* encrypted_message=emalloc((int)encrypted_message_length);
if(encrypted_message==NULL){
zend_throw_exception(zend_exception_get_default(TSRMLS_C), "Error: phpthemis_scell_seal_encrypt: not enough memory.", 0 TSRMLS_CC);
RETURN_NULL();
}
if(themis_secure_cell_encrypt_seal((uint8_t*)key, key_length, (uint8_t*)context, context_length, (uint8_t*)message, message_length, (uint8_t*)encrypted_message, &encrypted_message_length)!=THEMIS_SUCCESS){
zend_throw_exception(zend_exception_get_default(TSRMLS_C), "Error: phpthemis_scell_seal_encrypt: encryption failed.", 0 TSRMLS_CC);
RETURN_NULL();
}
ZVAL_STRINGL(return_value, encrypted_message, (int)encrypted_message_length);
return;
}

ZEND_FUNCTION(phpthemis_scell_seal_decrypt){
char* key;
int key_length;
char* message;
int message_length;
char* context=NULL;
int context_length=0;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss|s", &key, &key_length, &message, &message_length, &context, &context_length) == FAILURE) {
zend_throw_exception(zend_exception_get_default(TSRMLS_C), "Error: phpthemis_scell_seal_decrypt: invalid parameters.", 0 TSRMLS_CC);
RETURN_NULL();
}
size_t decrypted_message_length=0;
if(themis_secure_cell_decrypt_seal((uint8_t*)key, key_length, (uint8_t*)context, context_length, (uint8_t*)message, message_length, NULL, &decrypted_message_length)!=THEMIS_BUFFER_TOO_SMALL){
zend_throw_exception(zend_exception_get_default(TSRMLS_C), "Error: phpthemis_scell_seal_decrypt: decrypted message length determination failed.", 0 TSRMLS_CC);
RETURN_NULL();
}
char* decrypted_message=emalloc((int)decrypted_message_length);
if(decrypted_message==NULL){
zend_throw_exception(zend_exception_get_default(TSRMLS_C), "Error: phpthemis_scell_seal_decrypt: not enough memory.", 0 TSRMLS_CC);
RETURN_NULL();
}
if(themis_secure_cell_decrypt_seal((uint8_t*)key, key_length, (uint8_t*)context, context_length, (uint8_t*)message, message_length, (uint8_t*)decrypted_message, &decrypted_message_length)!=THEMIS_SUCCESS){
zend_throw_exception(zend_exception_get_default(TSRMLS_C), "Error: phpthemis_scell_seal_decrypt: decription failed.", 0 TSRMLS_CC);
RETURN_NULL();
}
ZVAL_STRINGL(return_value, decrypted_message, (int)decrypted_message_length);
return;
}

ZEND_FUNCTION(phpthemis_scell_token_protect_encrypt){
//zend_string* key;
char* key;
size_t key_length;
char* message;
size_t message_length;
char* context=NULL;
size_t context_length=0;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss|s", &key, &key_length, &message, &message_length, &context, &context_length) == FAILURE) {
zend_throw_exception(zend_exception_get_default(TSRMLS_C), "Error: phpthemis_scell_token_protect_encrypt: invalid parameters.", 0 TSRMLS_CC);
RETURN_NULL();
}
size_t encrypted_message_length=0;
size_t additional_auth_data_length=0;
if(themis_secure_cell_encrypt_token_protect((uint8_t*)key, key_length, (uint8_t*)context, context_length, (uint8_t*)message, message_length, NULL, &additional_auth_data_length, NULL, &encrypted_message_length)!=THEMIS_BUFFER_TOO_SMALL){
zend_throw_exception(zend_exception_get_default(TSRMLS_C), "Error: phpthemis_scell_token_protect_encrypt: encrypted message length determination failed.", 0 TSRMLS_CC);
RETURN_NULL();
}
char* encrypted_message=emalloc((int)encrypted_message_length);
if(encrypted_message==NULL){
zend_throw_exception(zend_exception_get_default(TSRMLS_C), "Error: phpthemis_scell_token_protect_encrypt: not enough memory.", 0 TSRMLS_CC);
RETURN_NULL();
}
char* additional_auth_data=emalloc((int)additional_auth_data_length);
if(additional_auth_data==NULL){
zend_throw_exception(zend_exception_get_default(TSRMLS_C), "Error: phpthemis_scell_token_protect_encrypt: not enough memory.", 0 TSRMLS_CC);
RETURN_NULL();
}
if(themis_secure_cell_encrypt_token_protect((uint8_t*)key, key_length, (uint8_t*)context, context_length, (uint8_t*)message, message_length, (uint8_t*)additional_auth_data, &additional_auth_data_length, (uint8_t*)encrypted_message, &encrypted_message_length)!=THEMIS_SUCCESS){
zend_throw_exception(zend_exception_get_default(TSRMLS_C), "Error: phpthemis_scell_token_protect_encrypt: encryption failed.", 0 TSRMLS_CC);
RETURN_NULL();
}
array_init(return_value);
add_assoc_stringl(return_value, "encrypted_message", encrypted_message, encrypted_message_length);
add_assoc_stringl(return_value, "token", additional_auth_data, additional_auth_data_length);
return;
}

ZEND_FUNCTION(phpthemis_scell_token_protect_decrypt){
char* key;
int key_length;
char* message;
int message_length;
char* additional_auth_data;
int additional_auth_data_length;
char* context=NULL;
int context_length=0;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sss|s", &key, &key_length, &message, &message_length, &additional_auth_data, &additional_auth_data_length, &context, &context_length) == FAILURE) {
zend_throw_exception(zend_exception_get_default(TSRMLS_C), "Error: phpthemis_scell_token_protect_decrypt: invalid parameters.", 0 TSRMLS_CC);
RETURN_NULL();
}
size_t decrypted_message_length=0;
if(themis_secure_cell_decrypt_token_protect((uint8_t*)key, key_length, (uint8_t*)context, context_length, (uint8_t*)message, message_length, (uint8_t*)additional_auth_data, additional_auth_data_length, NULL, &decrypted_message_length)!=THEMIS_BUFFER_TOO_SMALL){
zend_throw_exception(zend_exception_get_default(TSRMLS_C), "Error: phpthemis_scell_token_protect_decrypt: decrypt message length determination failed.", 0 TSRMLS_CC);
RETURN_NULL();
}
char* decrypted_message=emalloc((int)decrypted_message_length);
if(decrypted_message==NULL){
zend_throw_exception(zend_exception_get_default(TSRMLS_C), "Error: phpthemis_scell_token_protect_decrypt: not enough memory.", 0 TSRMLS_CC);
RETURN_NULL();
}
if(themis_secure_cell_decrypt_token_protect((uint8_t*)key, key_length, (uint8_t*)context, context_length, (uint8_t*)message, message_length, (uint8_t*)additional_auth_data, additional_auth_data_length, (uint8_t*)decrypted_message, &decrypted_message_length)!=THEMIS_SUCCESS){
zend_throw_exception(zend_exception_get_default(TSRMLS_C), "Error: phpthemis_scell_token_protect_decrypt: decryption failed.", 0 TSRMLS_CC);
RETURN_NULL();
}
ZVAL_STRINGL(return_value, decrypted_message, (int)decrypted_message_length);
return;
}

ZEND_FUNCTION(phpthemis_scell_context_imprint_encrypt){
char* key;
int key_length;
char* message;
int message_length;
char* context;
int context_length;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sss", &key, &key_length, &message, &message_length, &context, &context_length) == FAILURE) {
zend_throw_exception(zend_exception_get_default(TSRMLS_C), "Error: phpthemis_scell_context_imprint_encrypt: invalid parameters.", 0 TSRMLS_CC);
RETURN_NULL();
}
size_t encrypted_message_length=0;
if(themis_secure_cell_encrypt_context_imprint((uint8_t*)key, key_length, (uint8_t*)message, message_length, (uint8_t*)context, context_length, NULL, &encrypted_message_length)!=THEMIS_BUFFER_TOO_SMALL){
zend_throw_exception(zend_exception_get_default(TSRMLS_C), "Error: phpthemis_scell_context_imprint_encrypt: encrypt message length determination failed.", 0 TSRMLS_CC);
RETURN_NULL();
}
char* encrypted_message=emalloc((int)encrypted_message_length);
if(encrypted_message==NULL){
zend_throw_exception(zend_exception_get_default(TSRMLS_C), "Error: phpthemis_scell_context_imprint_encrypt: not enough memory.", 0 TSRMLS_CC);
RETURN_NULL();
}
if(themis_secure_cell_encrypt_context_imprint((uint8_t*)key, key_length, (uint8_t*)message, message_length, (uint8_t*)context, context_length, (uint8_t*)encrypted_message, &encrypted_message_length)!=THEMIS_SUCCESS){
zend_throw_exception(zend_exception_get_default(TSRMLS_C), "Error: phpthemis_scell_context_imprint_encrypt: encryption failed.", 0 TSRMLS_CC);
RETURN_NULL();
}
ZVAL_STRINGL(return_value, encrypted_message, (int)encrypted_message_length);
return;
}

ZEND_FUNCTION(phpthemis_scell_context_imprint_decrypt){
char* key;
int key_length;
char* message;
int message_length;
char* context=NULL;
int context_length=0;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sss", &key, &key_length, &message, &message_length, &context, &context_length) == FAILURE) {
zend_throw_exception(zend_exception_get_default(TSRMLS_C), "Error: phpthemis_scell_context_imprint_decrypt: invalid parameters.", 0 TSRMLS_CC);
RETURN_NULL();
}
size_t decrypted_message_length=0;
if(themis_secure_cell_decrypt_context_imprint((uint8_t*)key, key_length, (uint8_t*)message, message_length, (uint8_t*)context, context_length, NULL, &decrypted_message_length)!=THEMIS_BUFFER_TOO_SMALL){
zend_throw_exception(zend_exception_get_default(TSRMLS_C), "Error: phpthemis_scell_context_imprint_decrypt: decrypt message length determination failed.", 0 TSRMLS_CC);
RETURN_NULL();
}
char* decrypted_message=emalloc((int)decrypted_message_length);
if(decrypted_message==NULL){
zend_throw_exception(zend_exception_get_default(TSRMLS_C), "Error: phpthemis_scell_context_imprint_decrypt: not enough memory.", 0 TSRMLS_CC);
RETURN_NULL();
}
if(themis_secure_cell_decrypt_context_imprint((uint8_t*)key, key_length, (uint8_t*)message, message_length, (uint8_t*)context, context_length, (uint8_t*)decrypted_message, &decrypted_message_length)!=THEMIS_SUCCESS){
zend_throw_exception(zend_exception_get_default(TSRMLS_C), "Error: phpthemis_scell_context_imprint_decrypt: decrypting failure.", 0 TSRMLS_CC);
RETURN_NULL();
}
ZVAL_STRINGL(return_value, decrypted_message, (int)decrypted_message_length);
return;
}
26 changes: 26 additions & 0 deletions src/wrappers/themis/php7/php_cell.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Copyright (c) 2015 Cossack Labs Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef THEMIS_PHP_CELL_H
#define THEMIS_PHP_CELL_H

ZEND_FUNCTION(phpthemis_scell_seal_encrypt);
ZEND_FUNCTION(phpthemis_scell_seal_decrypt);
ZEND_FUNCTION(phpthemis_scell_token_protect_encrypt);
ZEND_FUNCTION(phpthemis_scell_token_protect_decrypt);
ZEND_FUNCTION(phpthemis_scell_context_imprint_encrypt);
ZEND_FUNCTION(phpthemis_scell_context_imprint_decrypt);

#endif //THEMIS_PHP_CELL_H
68 changes: 68 additions & 0 deletions src/wrappers/themis/php7/php_key_generator.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* Copyright (c) 2015 Cossack Labs Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "php_key_generator.h"

ZEND_FUNCTION(phpthemis_gen_rsa_key_pair){
size_t private_key_length;
size_t public_key_length;
if(themis_gen_rsa_key_pair(NULL, &private_key_length, NULL, &public_key_length)!=THEMIS_BUFFER_TOO_SMALL){
zend_throw_exception(zend_exception_get_default(TSRMLS_C), "Error: themis_gen_rsa_key_pair: invalid parameters.", 0 TSRMLS_CC);
RETURN_NULL();
}
char* private_key=emalloc(private_key_length);
if(private_key==NULL){
zend_throw_exception(zend_exception_get_default(TSRMLS_C), "Error: themis_gen_rsa_key_pair: not enough memory.", 0 TSRMLS_CC);
RETURN_NULL();
}
char* public_key=emalloc(public_key_length);
if(public_key==NULL){
zend_throw_exception(zend_exception_get_default(TSRMLS_C), "Error: themis_gen_rsa_key_pair: not enough memory.", 0 TSRMLS_CC);
RETURN_NULL();
}
if(themis_gen_rsa_key_pair((uint8_t*)private_key, &private_key_length, (uint8_t*)public_key, &public_key_length)!=THEMIS_SUCCESS){
zend_throw_exception(zend_exception_get_default(TSRMLS_C), "Error: themis_gen_rsa_key_pair: generation failed.", 0 TSRMLS_CC);
RETURN_NULL();
}
array_init(return_value);
add_assoc_stringl(return_value, "private_key", private_key, private_key_length);
add_assoc_stringl(return_value, "public_key", public_key, public_key_length);
}

ZEND_FUNCTION(phpthemis_gen_ec_key_pair){
size_t private_key_length;
size_t public_key_length;
if(themis_gen_ec_key_pair(NULL, &private_key_length, NULL, &public_key_length)!=THEMIS_BUFFER_TOO_SMALL){
zend_throw_exception(zend_exception_get_default(TSRMLS_C), "Error: themis_gen_ec_key_pair: invalid parameters.", 0 TSRMLS_CC);
RETURN_NULL();
}
char* private_key=emalloc(private_key_length);
if(private_key==NULL){
zend_throw_exception(zend_exception_get_default(TSRMLS_C), "Error: themis_gen_ec_key_pair: not enough memory.", 0 TSRMLS_CC);
RETURN_NULL();
}
char* public_key=emalloc(public_key_length);
if(public_key==NULL){
zend_throw_exception(zend_exception_get_default(TSRMLS_C), "Error: themis_gen_ec_key_pair: not enough memory.", 0 TSRMLS_CC);
RETURN_NULL();
}
if(themis_gen_ec_key_pair((uint8_t*)private_key, &private_key_length, (uint8_t*)public_key, &public_key_length)!=THEMIS_SUCCESS){
zend_throw_exception(zend_exception_get_default(TSRMLS_C), "Error: themis_gen_ec_key_pair: generation failed.", 0 TSRMLS_CC);
RETURN_NULL();
}
array_init(return_value);
add_assoc_stringl(return_value, "private_key", private_key, private_key_length);
add_assoc_stringl(return_value, "public_key", public_key, public_key_length);
}
28 changes: 28 additions & 0 deletions src/wrappers/themis/php7/php_key_generator.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Copyright (c) 2015 Cossack Labs Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef THEMIS_KEY_GENERATOR_H
#define THEMIS_KEY_GENERATOR_H

#include <themis/themis_error.h>

#include "php.h"
#include "zend_exceptions.h"
#include <themis/themis.h>

ZEND_FUNCTION(phpthemis_gen_rsa_key_pair);
ZEND_FUNCTION(phpthemis_gen_ec_key_pair);

#endif //THEMIS_KEY_GENERATOR_H
Loading

0 comments on commit 08911a6

Please sign in to comment.