xref: /PHP-8.1/Zend/zend_system_id.c (revision 01b3fc03)
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: Sammy Kaye Powers <sammyk@php.net>                          |
14    |          Dmitry Stogov <dmitry@php.net>                              |
15    +----------------------------------------------------------------------+
16  */
17 
18 #include "php.h"
19 #include "zend_system_id.h"
20 #include "zend_extensions.h"
21 #include "ext/standard/md5.h"
22 #include "ext/hash/php_hash.h"
23 
24 ZEND_API char zend_system_id[32];
25 
26 static PHP_MD5_CTX context;
27 static int finalized = 0;
28 
zend_add_system_entropy(const char * module_name,const char * hook_name,const void * data,size_t size)29 ZEND_API ZEND_RESULT_CODE zend_add_system_entropy(const char *module_name, const char *hook_name, const void *data, size_t size)
30 {
31 	if (finalized == 0) {
32 		PHP_MD5Update(&context, module_name, strlen(module_name));
33 		PHP_MD5Update(&context, hook_name, strlen(hook_name));
34 		if (size) {
35 			PHP_MD5Update(&context, data, size);
36 		}
37 		return SUCCESS;
38 	}
39 	return FAILURE;
40 }
41 
42 #define ZEND_BIN_ID "BIN_" ZEND_TOSTR(SIZEOF_INT) ZEND_TOSTR(SIZEOF_LONG) ZEND_TOSTR(SIZEOF_SIZE_T) ZEND_TOSTR(SIZEOF_ZEND_LONG) ZEND_TOSTR(ZEND_MM_ALIGNMENT)
43 
zend_startup_system_id(void)44 void zend_startup_system_id(void)
45 {
46 	PHP_MD5Init(&context);
47 	PHP_MD5Update(&context, PHP_VERSION, sizeof(PHP_VERSION)-1);
48 	PHP_MD5Update(&context, ZEND_EXTENSION_BUILD_ID, sizeof(ZEND_EXTENSION_BUILD_ID)-1);
49 	PHP_MD5Update(&context, ZEND_BIN_ID, sizeof(ZEND_BIN_ID)-1);
50 	if (strstr(PHP_VERSION, "-dev") != 0) {
51 		/* Development versions may be changed from build to build */
52 		PHP_MD5Update(&context, __DATE__, sizeof(__DATE__)-1);
53 		PHP_MD5Update(&context, __TIME__, sizeof(__TIME__)-1);
54 	}
55 	zend_system_id[0] = '\0';
56 }
57 
58 #define ZEND_HOOK_AST_PROCESS      (1 << 0)
59 #define ZEND_HOOK_COMPILE_FILE     (1 << 1)
60 #define ZEND_HOOK_EXECUTE_EX       (1 << 2)
61 #define ZEND_HOOK_EXECUTE_INTERNAL (1 << 3)
62 
zend_finalize_system_id(void)63 void zend_finalize_system_id(void)
64 {
65 	unsigned char digest[16];
66 	zend_uchar hooks = 0;
67 
68 	if (zend_ast_process) {
69 		hooks |= ZEND_HOOK_AST_PROCESS;
70 	}
71 	if (zend_compile_file != compile_file) {
72 		hooks |= ZEND_HOOK_COMPILE_FILE;
73 	}
74 	if (zend_execute_ex != execute_ex) {
75 		hooks |= ZEND_HOOK_EXECUTE_EX;
76 	}
77 	if (zend_execute_internal) {
78 		hooks |= ZEND_HOOK_EXECUTE_INTERNAL;
79 	}
80 	PHP_MD5Update(&context, &hooks, sizeof hooks);
81 
82 	for (int16_t i = 0; i < 256; i++) {
83 		if (zend_get_user_opcode_handler((zend_uchar) i) != NULL) {
84 			PHP_MD5Update(&context, &i, sizeof i);
85 		}
86 	}
87 
88 	PHP_MD5Final(digest, &context);
89 	php_hash_bin2hex(zend_system_id, digest, sizeof digest);
90 	finalized = 1;
91 }
92