-
Notifications
You must be signed in to change notification settings - Fork 7.8k
/
Copy pathopenssl.c
4487 lines (3834 loc) · 114 KB
/
openssl.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
+----------------------------------------------------------------------+
| Copyright (c) The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| https://www.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| license@php.net so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Authors: Stig Venaas <venaas@php.net> |
| Wez Furlong <wez@thebrainroom.com> |
| Sascha Kettler <kettler@gmx.net> |
| Pierre-Alain Joye <pierre@php.net> |
| Marc Delling <delling@silpion.de> (PKCS12 functions) |
| Jakub Zelenka <bukka@php.net> |
| Eliot Lear <lear@ofcourseimright.com> |
+----------------------------------------------------------------------+
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include "php.h"
#include "php_ini.h"
#include "php_openssl.h"
#include "php_openssl_backend.h"
#include "zend_attributes.h"
#include "zend_exceptions.h"
/* PHP Includes */
#include "ext/standard/file.h"
#include "ext/standard/info.h"
#include "ext/standard/php_fopen_wrappers.h"
#include "ext/standard/md5.h" /* For make_digest_ex() */
#include "ext/standard/base64.h"
#ifdef PHP_WIN32
# include "win32/winutil.h"
#endif
/* OpenSSL includes */
#include <openssl/evp.h>
#include <openssl/x509.h>
#include <openssl/x509v3.h>
#include <openssl/crypto.h>
#include <openssl/pem.h>
#include <openssl/err.h>
#include <openssl/conf.h>
#include <openssl/rand.h>
#include <openssl/ssl.h>
#include <openssl/pkcs12.h>
#include <openssl/cms.h>
#if PHP_OPENSSL_API_VERSION >= 0x30000
#include <openssl/core_names.h>
#include <openssl/param_build.h>
#include <openssl/provider.h>
#endif
ZEND_DECLARE_MODULE_GLOBALS(openssl)
#include "openssl_arginfo.h"
/* OpenSSLCertificate class */
zend_class_entry *php_openssl_certificate_ce;
static zend_object_handlers php_openssl_certificate_object_handlers;
bool php_openssl_is_certificate_ce(zval *val)
{
return Z_TYPE_P(val) == IS_OBJECT && Z_OBJCE_P(val) == php_openssl_certificate_ce;
}
static zend_object *php_openssl_certificate_create_object(zend_class_entry *class_type) {
php_openssl_certificate_object *intern = zend_object_alloc(sizeof(php_openssl_certificate_object), class_type);
zend_object_std_init(&intern->std, class_type);
object_properties_init(&intern->std, class_type);
return &intern->std;
}
static zend_function *php_openssl_certificate_get_constructor(zend_object *object) {
zend_throw_error(NULL, "Cannot directly construct OpenSSLCertificate, use openssl_x509_read() instead");
return NULL;
}
static void php_openssl_certificate_free_obj(zend_object *object)
{
php_openssl_certificate_object *x509_object = php_openssl_certificate_from_obj(object);
X509_free(x509_object->x509);
zend_object_std_dtor(&x509_object->std);
}
/* OpenSSLCertificateSigningRequest class */
static zend_class_entry *php_openssl_request_ce;
static zend_object_handlers php_openssl_request_object_handlers;
bool php_openssl_is_request_ce(zval *val)
{
return Z_TYPE_P(val) == IS_OBJECT && Z_OBJCE_P(val) == php_openssl_request_ce;
}
static zend_object *php_openssl_request_create_object(zend_class_entry *class_type) {
php_openssl_request_object *intern = zend_object_alloc(sizeof(php_openssl_request_object), class_type);
zend_object_std_init(&intern->std, class_type);
object_properties_init(&intern->std, class_type);
return &intern->std;
}
static zend_function *php_openssl_request_get_constructor(zend_object *object) {
zend_throw_error(NULL, "Cannot directly construct OpenSSLCertificateSigningRequest, use openssl_csr_new() instead");
return NULL;
}
static void php_openssl_request_free_obj(zend_object *object)
{
php_openssl_request_object *x509_request = php_openssl_request_from_obj(object);
X509_REQ_free(x509_request->csr);
zend_object_std_dtor(&x509_request->std);
}
/* OpenSSLAsymmetricKey class */
static zend_class_entry *php_openssl_pkey_ce;
static zend_object_handlers php_openssl_pkey_object_handlers;
bool php_openssl_is_pkey_ce(zval *val)
{
return Z_TYPE_P(val) == IS_OBJECT && Z_OBJCE_P(val) == php_openssl_pkey_ce;
}
void php_openssl_pkey_object_init(zval *zv, EVP_PKEY *pkey, bool is_private)
{
object_init_ex(zv, php_openssl_pkey_ce);
php_openssl_pkey_object *obj = Z_OPENSSL_PKEY_P(zv);
obj->pkey = pkey;
obj->is_private = is_private;
}
static zend_object *php_openssl_pkey_create_object(zend_class_entry *class_type)
{
php_openssl_pkey_object *intern = zend_object_alloc(sizeof(php_openssl_pkey_object), class_type);
zend_object_std_init(&intern->std, class_type);
object_properties_init(&intern->std, class_type);
return &intern->std;
}
static zend_function *php_openssl_pkey_get_constructor(zend_object *object)
{
zend_throw_error(NULL, "Cannot directly construct OpenSSLAsymmetricKey, use openssl_pkey_new() instead");
return NULL;
}
static void php_openssl_pkey_free_obj(zend_object *object)
{
php_openssl_pkey_object *key_object = php_openssl_pkey_from_obj(object);
EVP_PKEY_free(key_object->pkey);
zend_object_std_dtor(&key_object->std);
}
#if defined(HAVE_OPENSSL_ARGON2)
static const zend_module_dep openssl_deps[] = {
ZEND_MOD_REQUIRED("standard")
ZEND_MOD_END
};
#endif
/* {{{ openssl_module_entry */
zend_module_entry openssl_module_entry = {
#if defined(HAVE_OPENSSL_ARGON2)
STANDARD_MODULE_HEADER_EX, NULL,
openssl_deps,
#else
STANDARD_MODULE_HEADER,
#endif
"openssl",
ext_functions,
PHP_MINIT(openssl),
PHP_MSHUTDOWN(openssl),
NULL,
NULL,
PHP_MINFO(openssl),
PHP_OPENSSL_VERSION,
PHP_MODULE_GLOBALS(openssl),
PHP_GINIT(openssl),
PHP_GSHUTDOWN(openssl),
NULL,
STANDARD_MODULE_PROPERTIES_EX
};
/* }}} */
#ifdef COMPILE_DL_OPENSSL
ZEND_GET_MODULE(openssl)
#endif
/* {{{ php_openssl_store_errors */
void php_openssl_store_errors(void)
{
struct php_openssl_errors *errors;
int error_code = ERR_get_error();
if (!error_code) {
return;
}
if (!OPENSSL_G(errors)) {
OPENSSL_G(errors) = pecalloc(1, sizeof(struct php_openssl_errors), 1);
}
errors = OPENSSL_G(errors);
do {
errors->top = (errors->top + 1) % ERR_NUM_ERRORS;
if (errors->top == errors->bottom) {
errors->bottom = (errors->bottom + 1) % ERR_NUM_ERRORS;
}
errors->buffer[errors->top] = error_code;
} while ((error_code = ERR_get_error()));
}
/* }}} */
/* {{{ php_openssl_errors_set_mark */
void php_openssl_errors_set_mark(void) {
if (!OPENSSL_G(errors)) {
return;
}
if (!OPENSSL_G(errors_mark)) {
OPENSSL_G(errors_mark) = pecalloc(1, sizeof(struct php_openssl_errors), 1);
}
memcpy(OPENSSL_G(errors_mark), OPENSSL_G(errors), sizeof(struct php_openssl_errors));
}
/* }}} */
/* {{{ php_openssl_errors_restore_mark */
void php_openssl_errors_restore_mark(void) {
if (!OPENSSL_G(errors)) {
return;
}
struct php_openssl_errors *errors = OPENSSL_G(errors);
if (!OPENSSL_G(errors_mark)) {
errors->top = 0;
errors->bottom = 0;
} else {
memcpy(errors, OPENSSL_G(errors_mark), sizeof(struct php_openssl_errors));
}
}
/* }}} */
/* openssl file path check error function */
static void php_openssl_check_path_error(uint32_t arg_num, int type, const char *format, ...)
{
va_list va;
const char *arg_name;
va_start(va, format);
if (type == E_ERROR) {
zend_argument_error_variadic(zend_ce_value_error, arg_num, format, va);
} else {
arg_name = get_active_function_arg_name(arg_num);
php_verror(NULL, arg_name, type, format, va);
}
va_end(va);
}
/* openssl file path check extended */
bool php_openssl_check_path_ex(
const char *file_path, size_t file_path_len, char *real_path, uint32_t arg_num,
bool contains_file_protocol, bool is_from_array, const char *option_name)
{
const char *fs_file_path;
size_t fs_file_path_len;
const char *error_msg = NULL;
int error_type = E_WARNING;
if (file_path_len == 0) {
real_path[0] = '\0';
return true;
}
if (contains_file_protocol) {
size_t path_prefix_len = sizeof("file://") - 1;
if (file_path_len <= path_prefix_len) {
return false;
}
fs_file_path = file_path + path_prefix_len;
fs_file_path_len = file_path_len - path_prefix_len;
} else {
fs_file_path = file_path;
fs_file_path_len = file_path_len;
}
if (CHECK_NULL_PATH(fs_file_path, fs_file_path_len)) {
error_msg = "must not contain any null bytes";
error_type = E_ERROR;
} else if (expand_filepath(fs_file_path, real_path) == NULL) {
error_msg = "must be a valid file path";
}
if (error_msg != NULL) {
if (arg_num == 0) {
const char *option_title = option_name ? option_name : "unknown";
const char *option_label = is_from_array ? "array item" : "option";
php_error_docref(NULL, E_WARNING, "Path for %s %s %s",
option_title, option_label, error_msg);
} else if (is_from_array && option_name != NULL) {
php_openssl_check_path_error(
arg_num, error_type, "option %s array item %s", option_name, error_msg);
} else if (is_from_array) {
php_openssl_check_path_error(arg_num, error_type, "array item %s", error_msg);
} else if (option_name != NULL) {
php_openssl_check_path_error(
arg_num, error_type, "option %s %s", option_name, error_msg);
} else {
php_openssl_check_path_error(arg_num, error_type, "%s", error_msg);
}
} else if (!php_check_open_basedir(real_path)) {
return true;
}
return false;
}
static int ssl_stream_data_index;
php_stream* php_openssl_get_stream_from_ssl_handle(const SSL *ssl)
{
return (php_stream*)SSL_get_ex_data(ssl, ssl_stream_data_index);
}
int php_openssl_get_ssl_stream_data_index(void)
{
return ssl_stream_data_index;
}
/* {{{ INI Settings */
PHP_INI_BEGIN()
PHP_INI_ENTRY("openssl.cafile", NULL, PHP_INI_PERDIR, NULL)
PHP_INI_ENTRY("openssl.capath", NULL, PHP_INI_PERDIR, NULL)
PHP_INI_END()
/* }}} */
/* {{{ PHP_MINIT_FUNCTION */
PHP_MINIT_FUNCTION(openssl)
{
php_openssl_certificate_ce = register_class_OpenSSLCertificate();
php_openssl_certificate_ce->create_object = php_openssl_certificate_create_object;
php_openssl_certificate_ce->default_object_handlers = &php_openssl_certificate_object_handlers;
memcpy(&php_openssl_certificate_object_handlers, &std_object_handlers, sizeof(zend_object_handlers));
php_openssl_certificate_object_handlers.offset = XtOffsetOf(php_openssl_certificate_object, std);
php_openssl_certificate_object_handlers.free_obj = php_openssl_certificate_free_obj;
php_openssl_certificate_object_handlers.get_constructor = php_openssl_certificate_get_constructor;
php_openssl_certificate_object_handlers.clone_obj = NULL;
php_openssl_certificate_object_handlers.compare = zend_objects_not_comparable;
php_openssl_request_ce = register_class_OpenSSLCertificateSigningRequest();
php_openssl_request_ce->create_object = php_openssl_request_create_object;
php_openssl_request_ce->default_object_handlers = &php_openssl_request_object_handlers;
memcpy(&php_openssl_request_object_handlers, &std_object_handlers, sizeof(zend_object_handlers));
php_openssl_request_object_handlers.offset = XtOffsetOf(php_openssl_request_object, std);
php_openssl_request_object_handlers.free_obj = php_openssl_request_free_obj;
php_openssl_request_object_handlers.get_constructor = php_openssl_request_get_constructor;
php_openssl_request_object_handlers.clone_obj = NULL;
php_openssl_request_object_handlers.compare = zend_objects_not_comparable;
php_openssl_pkey_ce = register_class_OpenSSLAsymmetricKey();
php_openssl_pkey_ce->create_object = php_openssl_pkey_create_object;
php_openssl_pkey_ce->default_object_handlers = &php_openssl_pkey_object_handlers;
memcpy(&php_openssl_pkey_object_handlers, &std_object_handlers, sizeof(zend_object_handlers));
php_openssl_pkey_object_handlers.offset = XtOffsetOf(php_openssl_pkey_object, std);
php_openssl_pkey_object_handlers.free_obj = php_openssl_pkey_free_obj;
php_openssl_pkey_object_handlers.get_constructor = php_openssl_pkey_get_constructor;
php_openssl_pkey_object_handlers.clone_obj = NULL;
php_openssl_pkey_object_handlers.compare = zend_objects_not_comparable;
register_openssl_symbols(module_number);
php_openssl_backend_init();
/* register a resource id number with OpenSSL so that we can map SSL -> stream structures in
* OpenSSL callbacks */
ssl_stream_data_index = SSL_get_ex_new_index(0, "PHP stream index", NULL, NULL, NULL);
php_stream_xport_register("ssl", php_openssl_ssl_socket_factory);
#ifndef OPENSSL_NO_SSL3
php_stream_xport_register("sslv3", php_openssl_ssl_socket_factory);
#endif
php_stream_xport_register("tls", php_openssl_ssl_socket_factory);
php_stream_xport_register("tlsv1.0", php_openssl_ssl_socket_factory);
php_stream_xport_register("tlsv1.1", php_openssl_ssl_socket_factory);
php_stream_xport_register("tlsv1.2", php_openssl_ssl_socket_factory);
php_stream_xport_register("tlsv1.3", php_openssl_ssl_socket_factory);
/* override the default tcp socket provider */
php_stream_xport_register("tcp", php_openssl_ssl_socket_factory);
php_register_url_stream_wrapper("https", &php_stream_http_wrapper);
php_register_url_stream_wrapper("ftps", &php_stream_ftp_wrapper);
REGISTER_INI_ENTRIES();
#if defined(HAVE_OPENSSL_ARGON2)
if (FAILURE == PHP_MINIT(openssl_pwhash)(INIT_FUNC_ARGS_PASSTHRU)) {
return FAILURE;
}
#endif
return SUCCESS;
}
/* }}} */
/* {{{ PHP_GINIT_FUNCTION */
PHP_GINIT_FUNCTION(openssl)
{
#if defined(COMPILE_DL_OPENSSL) && defined(ZTS)
ZEND_TSRMLS_CACHE_UPDATE();
#endif
openssl_globals->errors = NULL;
openssl_globals->errors_mark = NULL;
}
/* }}} */
/* {{{ PHP_GSHUTDOWN_FUNCTION */
PHP_GSHUTDOWN_FUNCTION(openssl)
{
if (openssl_globals->errors) {
pefree(openssl_globals->errors, 1);
}
if (openssl_globals->errors_mark) {
pefree(openssl_globals->errors_mark, 1);
}
}
/* }}} */
/* {{{ PHP_MINFO_FUNCTION */
PHP_MINFO_FUNCTION(openssl)
{
php_info_print_table_start();
php_info_print_table_row(2, "OpenSSL support", "enabled");
php_info_print_table_row(2, "OpenSSL Library Version", OpenSSL_version(OPENSSL_VERSION));
php_info_print_table_row(2, "OpenSSL Header Version", OPENSSL_VERSION_TEXT);
php_info_print_table_row(2, "Openssl default config", php_openssl_get_conf_filename());
php_info_print_table_end();
DISPLAY_INI_ENTRIES();
}
/* }}} */
/* {{{ PHP_MSHUTDOWN_FUNCTION */
PHP_MSHUTDOWN_FUNCTION(openssl)
{
php_openssl_backend_shutdown();
php_unregister_url_stream_wrapper("https");
php_unregister_url_stream_wrapper("ftps");
php_stream_xport_unregister("ssl");
#ifndef OPENSSL_NO_SSL3
php_stream_xport_unregister("sslv3");
#endif
php_stream_xport_unregister("tls");
php_stream_xport_unregister("tlsv1.0");
php_stream_xport_unregister("tlsv1.1");
php_stream_xport_unregister("tlsv1.2");
php_stream_xport_unregister("tlsv1.3");
/* reinstate the default tcp handler */
php_stream_xport_register("tcp", php_stream_generic_socket_factory);
UNREGISTER_INI_ENTRIES();
return SUCCESS;
}
/* }}} */
/* {{{ x509 cert functions */
/* {{{ Retrieve an array mapping available certificate locations */
PHP_FUNCTION(openssl_get_cert_locations)
{
if (zend_parse_parameters_none() == FAILURE) {
RETURN_THROWS();
}
array_init(return_value);
php_openssl_set_cert_locations(return_value);
}
/* }}} */
/* {{{ Exports a CERT to file or a var */
PHP_FUNCTION(openssl_x509_export_to_file)
{
X509 *cert;
zend_object *cert_obj;
zend_string *cert_str;
bool notext = 1;
BIO * bio_out;
char * filename, file_path[MAXPATHLEN];
size_t filename_len;
ZEND_PARSE_PARAMETERS_START(2, 3)
Z_PARAM_OBJ_OF_CLASS_OR_STR(cert_obj, php_openssl_certificate_ce, cert_str)
Z_PARAM_PATH(filename, filename_len)
Z_PARAM_OPTIONAL
Z_PARAM_BOOL(notext)
ZEND_PARSE_PARAMETERS_END();
RETVAL_FALSE;
cert = php_openssl_x509_from_param(cert_obj, cert_str, 1);
if (cert == NULL) {
php_error_docref(NULL, E_WARNING, "X.509 Certificate cannot be retrieved");
return;
}
if (!php_openssl_check_path(filename, filename_len, file_path, 2)) {
goto exit_cleanup_cert;
}
bio_out = BIO_new_file(file_path, PHP_OPENSSL_BIO_MODE_W(PKCS7_BINARY));
if (bio_out) {
if (!notext && !X509_print(bio_out, cert)) {
php_openssl_store_errors();
}
if (!PEM_write_bio_X509(bio_out, cert)) {
php_openssl_store_errors();
}
RETVAL_TRUE;
} else {
php_openssl_store_errors();
php_error_docref(NULL, E_WARNING, "Error opening file %s", file_path);
}
if (!BIO_free(bio_out)) {
php_openssl_store_errors();
}
exit_cleanup_cert:
if (cert_str) {
X509_free(cert);
}
}
/* }}} */
/* {{{ Creates new private key (or uses existing) and creates a new spki cert
outputting results to var */
PHP_FUNCTION(openssl_spki_new)
{
size_t challenge_len;
char * challenge = NULL, *spkstr = NULL;
zend_string * s = NULL;
const char *spkac = "SPKAC=";
zend_long algo = OPENSSL_ALGO_MD5;
zval *zpkey = NULL;
EVP_PKEY *pkey = NULL;
NETSCAPE_SPKI *spki=NULL;
const EVP_MD *mdtype;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "Os|l", &zpkey, php_openssl_pkey_ce, &challenge, &challenge_len, &algo) == FAILURE) {
RETURN_THROWS();
}
RETVAL_FALSE;
PHP_OPENSSL_CHECK_SIZE_T_TO_INT(challenge_len, challenge, 2);
pkey = php_openssl_pkey_from_zval(zpkey, 0, challenge, challenge_len, 1);
if (pkey == NULL) {
if (!EG(exception)) {
php_error_docref(NULL, E_WARNING, "Unable to use supplied private key");
}
goto cleanup;
}
mdtype = php_openssl_get_evp_md_from_algo(algo);
if (!mdtype) {
php_error_docref(NULL, E_WARNING, "Unknown digest algorithm");
goto cleanup;
}
if ((spki = NETSCAPE_SPKI_new()) == NULL) {
php_openssl_store_errors();
php_error_docref(NULL, E_WARNING, "Unable to create new SPKAC");
goto cleanup;
}
if (challenge) {
if (!ASN1_STRING_set(spki->spkac->challenge, challenge, (int)challenge_len)) {
php_openssl_store_errors();
php_error_docref(NULL, E_WARNING, "Unable to set challenge data");
goto cleanup;
}
}
if (!NETSCAPE_SPKI_set_pubkey(spki, pkey)) {
php_openssl_store_errors();
php_error_docref(NULL, E_WARNING, "Unable to embed public key");
goto cleanup;
}
if (!NETSCAPE_SPKI_sign(spki, pkey, mdtype)) {
php_openssl_store_errors();
php_error_docref(NULL, E_WARNING, "Unable to sign with specified digest algorithm");
goto cleanup;
}
spkstr = NETSCAPE_SPKI_b64_encode(spki);
if (!spkstr){
php_openssl_store_errors();
php_error_docref(NULL, E_WARNING, "Unable to encode SPKAC");
goto cleanup;
}
s = zend_string_concat2(spkac, strlen(spkac), spkstr, strlen(spkstr));
OPENSSL_free(spkstr);
RETVAL_STR(s);
goto cleanup;
cleanup:
EVP_PKEY_free(pkey);
if (spki != NULL) {
NETSCAPE_SPKI_free(spki);
}
if (s && ZSTR_LEN(s) <= 0) {
RETVAL_FALSE;
}
}
/* }}} */
/* {{{ Verifies spki returns boolean */
PHP_FUNCTION(openssl_spki_verify)
{
size_t spkstr_len;
int i = 0, spkstr_cleaned_len = 0;
char *spkstr, * spkstr_cleaned = NULL;
EVP_PKEY *pkey = NULL;
NETSCAPE_SPKI *spki = NULL;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &spkstr, &spkstr_len) == FAILURE) {
RETURN_THROWS();
}
RETVAL_FALSE;
spkstr_cleaned = emalloc(spkstr_len + 1);
spkstr_cleaned_len = (int)(spkstr_len - php_openssl_spki_cleanup(spkstr, spkstr_cleaned));
if (spkstr_cleaned_len == 0) {
php_error_docref(NULL, E_WARNING, "Invalid SPKAC");
goto cleanup;
}
spki = NETSCAPE_SPKI_b64_decode(spkstr_cleaned, spkstr_cleaned_len);
if (spki == NULL) {
php_openssl_store_errors();
php_error_docref(NULL, E_WARNING, "Unable to decode supplied SPKAC");
goto cleanup;
}
pkey = X509_PUBKEY_get(spki->spkac->pubkey);
if (pkey == NULL) {
php_openssl_store_errors();
php_error_docref(NULL, E_WARNING, "Unable to acquire signed public key");
goto cleanup;
}
i = NETSCAPE_SPKI_verify(spki, pkey);
goto cleanup;
cleanup:
if (spki != NULL) {
NETSCAPE_SPKI_free(spki);
}
EVP_PKEY_free(pkey);
if (spkstr_cleaned != NULL) {
efree(spkstr_cleaned);
}
if (i > 0) {
RETVAL_TRUE;
} else {
php_openssl_store_errors();
}
}
/* }}} */
/* {{{ Exports public key from existing spki to var */
PHP_FUNCTION(openssl_spki_export)
{
size_t spkstr_len;
char *spkstr, * spkstr_cleaned = NULL;
int spkstr_cleaned_len;
EVP_PKEY *pkey = NULL;
NETSCAPE_SPKI *spki = NULL;
BIO *out = NULL;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &spkstr, &spkstr_len) == FAILURE) {
RETURN_THROWS();
}
RETVAL_FALSE;
spkstr_cleaned = emalloc(spkstr_len + 1);
spkstr_cleaned_len = (int)(spkstr_len - php_openssl_spki_cleanup(spkstr, spkstr_cleaned));
if (spkstr_cleaned_len == 0) {
php_error_docref(NULL, E_WARNING, "Invalid SPKAC");
goto cleanup;
}
spki = NETSCAPE_SPKI_b64_decode(spkstr_cleaned, spkstr_cleaned_len);
if (spki == NULL) {
php_openssl_store_errors();
php_error_docref(NULL, E_WARNING, "Unable to decode supplied SPKAC");
goto cleanup;
}
pkey = X509_PUBKEY_get(spki->spkac->pubkey);
if (pkey == NULL) {
php_openssl_store_errors();
php_error_docref(NULL, E_WARNING, "Unable to acquire signed public key");
goto cleanup;
}
out = BIO_new(BIO_s_mem());
if (out && PEM_write_bio_PUBKEY(out, pkey)) {
BUF_MEM *bio_buf;
BIO_get_mem_ptr(out, &bio_buf);
RETVAL_STRINGL((char *)bio_buf->data, bio_buf->length);
} else {
php_openssl_store_errors();
}
goto cleanup;
cleanup:
if (spki != NULL) {
NETSCAPE_SPKI_free(spki);
}
BIO_free_all(out);
EVP_PKEY_free(pkey);
if (spkstr_cleaned != NULL) {
efree(spkstr_cleaned);
}
}
/* }}} */
/* {{{ Exports spkac challenge from existing spki to var */
PHP_FUNCTION(openssl_spki_export_challenge)
{
size_t spkstr_len;
char *spkstr, * spkstr_cleaned = NULL;
int spkstr_cleaned_len;
NETSCAPE_SPKI *spki = NULL;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &spkstr, &spkstr_len) == FAILURE) {
RETURN_THROWS();
}
RETVAL_FALSE;
spkstr_cleaned = emalloc(spkstr_len + 1);
spkstr_cleaned_len = (int)(spkstr_len - php_openssl_spki_cleanup(spkstr, spkstr_cleaned));
if (spkstr_cleaned_len == 0) {
php_error_docref(NULL, E_WARNING, "Invalid SPKAC");
goto cleanup;
}
spki = NETSCAPE_SPKI_b64_decode(spkstr_cleaned, spkstr_cleaned_len);
if (spki == NULL) {
php_openssl_store_errors();
php_error_docref(NULL, E_WARNING, "Unable to decode SPKAC");
goto cleanup;
}
RETVAL_STRING((const char *)ASN1_STRING_get0_data(spki->spkac->challenge));
goto cleanup;
cleanup:
if (spkstr_cleaned != NULL) {
efree(spkstr_cleaned);
}
if (spki) {
NETSCAPE_SPKI_free(spki);
}
}
/* }}} */
/* {{{ Exports a CERT to file or a var */
PHP_FUNCTION(openssl_x509_export)
{
X509 *cert;
zend_object *cert_obj;
zend_string *cert_str;
zval *zout;
bool notext = 1;
BIO * bio_out;
ZEND_PARSE_PARAMETERS_START(2, 3)
Z_PARAM_OBJ_OF_CLASS_OR_STR(cert_obj, php_openssl_certificate_ce, cert_str)
Z_PARAM_ZVAL(zout)
Z_PARAM_OPTIONAL
Z_PARAM_BOOL(notext)
ZEND_PARSE_PARAMETERS_END();
RETVAL_FALSE;
cert = php_openssl_x509_from_param(cert_obj, cert_str, 1);
if (cert == NULL) {
php_error_docref(NULL, E_WARNING, "X.509 Certificate cannot be retrieved");
return;
}
bio_out = BIO_new(BIO_s_mem());
if (!bio_out) {
php_openssl_store_errors();
goto cleanup;
}
if (!notext && !X509_print(bio_out, cert)) {
php_openssl_store_errors();
}
if (PEM_write_bio_X509(bio_out, cert)) {
BUF_MEM *bio_buf;
BIO_get_mem_ptr(bio_out, &bio_buf);
ZEND_TRY_ASSIGN_REF_STRINGL(zout, bio_buf->data, bio_buf->length);
RETVAL_TRUE;
} else {
php_openssl_store_errors();
}
BIO_free(bio_out);
cleanup:
if (cert_str) {
X509_free(cert);
}
}
/* }}} */
PHP_FUNCTION(openssl_x509_fingerprint)
{
X509 *cert;
zend_object *cert_obj;
zend_string *cert_str;
bool raw_output = 0;
char *method = "sha1";
size_t method_len;
zend_string *fingerprint;
ZEND_PARSE_PARAMETERS_START(1, 3)
Z_PARAM_OBJ_OF_CLASS_OR_STR(cert_obj, php_openssl_certificate_ce, cert_str)
Z_PARAM_OPTIONAL
Z_PARAM_STRING(method, method_len)
Z_PARAM_BOOL(raw_output)
ZEND_PARSE_PARAMETERS_END();
cert = php_openssl_x509_from_param(cert_obj, cert_str, 1);
if (cert == NULL) {
php_error_docref(NULL, E_WARNING, "X.509 Certificate cannot be retrieved");
RETURN_FALSE;
}
fingerprint = php_openssl_x509_fingerprint(cert, method, raw_output);
if (fingerprint) {
RETVAL_STR(fingerprint);
} else {
RETVAL_FALSE;
}
if (cert_str) {
X509_free(cert);
}
}
/* {{{ Checks if a private key corresponds to a CERT */
PHP_FUNCTION(openssl_x509_check_private_key)
{
X509 *cert;
zend_object *cert_obj;
zend_string *cert_str;
zval *zkey;
EVP_PKEY * key = NULL;
ZEND_PARSE_PARAMETERS_START(2, 2)
Z_PARAM_OBJ_OF_CLASS_OR_STR(cert_obj, php_openssl_certificate_ce, cert_str)
Z_PARAM_ZVAL(zkey)
ZEND_PARSE_PARAMETERS_END();
cert = php_openssl_x509_from_param(cert_obj, cert_str, 1);
if (cert == NULL) {
RETURN_FALSE;
}
RETVAL_FALSE;
key = php_openssl_pkey_from_zval(zkey, 0, "", 0, 2);
if (key) {
RETVAL_BOOL(X509_check_private_key(cert, key));
EVP_PKEY_free(key);
}
if (cert_str) {
X509_free(cert);
}
}
/* }}} */
/* {{{ Verifies the signature of certificate cert using public key key */
PHP_FUNCTION(openssl_x509_verify)
{
X509 *cert;
zend_object *cert_obj;
zend_string *cert_str;
zval *zkey;
EVP_PKEY * key = NULL;
int err = -1;
ZEND_PARSE_PARAMETERS_START(2, 2)
Z_PARAM_OBJ_OF_CLASS_OR_STR(cert_obj, php_openssl_certificate_ce, cert_str)
Z_PARAM_ZVAL(zkey)
ZEND_PARSE_PARAMETERS_END();
cert = php_openssl_x509_from_param(cert_obj, cert_str, 1);
if (cert == NULL) {
RETURN_LONG(err);
}
key = php_openssl_pkey_from_zval(zkey, 1, NULL, 0, 2);
if (key != NULL) {
err = X509_verify(cert, key);
if (err < 0) {
php_openssl_store_errors();
}
EVP_PKEY_free(key);
}
if (cert_str) {
X509_free(cert);
}
RETURN_LONG(err);
}
/* }}} */
/* {{{ Returns an array of the fields/values of the CERT */
PHP_FUNCTION(openssl_x509_parse)
{
X509 *cert;
zend_object *cert_obj;
zend_string *cert_str;
int i, sig_nid;
bool useshortnames = 1;
char * tmpstr;
zval subitem;
X509_EXTENSION *extension;
X509_NAME *subject_name;
char *cert_name;
char *extname;
BIO *bio_out;
BUF_MEM *bio_buf;
ASN1_INTEGER *asn1_serial;
BIGNUM *bn_serial;
char *str_serial;
char *hex_serial;
char buf[256];
ZEND_PARSE_PARAMETERS_START(1, 2)
Z_PARAM_OBJ_OF_CLASS_OR_STR(cert_obj, php_openssl_certificate_ce, cert_str)
Z_PARAM_OPTIONAL
Z_PARAM_BOOL(useshortnames)