1 /*
2 *
3 * Copyright 2015 gRPC authors.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 */
18
19 #include "server_credentials.h"
20
21 #include <ext/spl/spl_exceptions.h>
22 #include <zend_exceptions.h>
23
24 zend_class_entry *grpc_ce_server_credentials;
25 PHP_GRPC_DECLARE_OBJECT_HANDLER(server_credentials_ce_handlers)
26
27 /* Frees and destroys an instace of wrapped_grpc_server_credentials */
28 PHP_GRPC_FREE_WRAPPED_FUNC_START(wrapped_grpc_server_credentials)
29 if (p->wrapped != NULL) {
30 grpc_server_credentials_release(p->wrapped);
31 }
PHP_GRPC_FREE_WRAPPED_FUNC_END()32 PHP_GRPC_FREE_WRAPPED_FUNC_END()
33
34 /* Initializes an instace of wrapped_grpc_server_credentials to be
35 * associated with an object of a class specified by class_type */
36 php_grpc_zend_object create_wrapped_grpc_server_credentials(
37 zend_class_entry *class_type TSRMLS_DC) {
38 PHP_GRPC_ALLOC_CLASS_OBJECT(wrapped_grpc_server_credentials);
39 zend_object_std_init(&intern->std, class_type TSRMLS_CC);
40 object_properties_init(&intern->std, class_type);
41 PHP_GRPC_FREE_CLASS_OBJECT(wrapped_grpc_server_credentials,
42 server_credentials_ce_handlers);
43 }
44
grpc_php_wrap_server_credentials(grpc_server_credentials * wrapped TSRMLS_DC)45 zval *grpc_php_wrap_server_credentials(grpc_server_credentials
46 *wrapped TSRMLS_DC) {
47 zval *server_credentials_object;
48 PHP_GRPC_MAKE_STD_ZVAL(server_credentials_object);
49 object_init_ex(server_credentials_object, grpc_ce_server_credentials);
50 wrapped_grpc_server_credentials *server_credentials =
51 PHP_GRPC_GET_WRAPPED_OBJECT(wrapped_grpc_server_credentials,
52 server_credentials_object);
53 server_credentials->wrapped = wrapped;
54 return server_credentials_object;
55 }
56
57 /**
58 * Create SSL credentials.
59 * @param string pem_root_certs PEM encoding of the server root certificates
60 * @param string pem_private_key PEM encoding of the client's private key
61 * @param string pem_cert_chain PEM encoding of the client's certificate chain
62 * @return Credentials The new SSL credentials object
63 */
PHP_METHOD(ServerCredentials,createSsl)64 PHP_METHOD(ServerCredentials, createSsl) {
65 char *pem_root_certs = 0;
66 grpc_ssl_pem_key_cert_pair pem_key_cert_pair;
67
68 php_grpc_int root_certs_length = 0;
69 php_grpc_int private_key_length;
70 php_grpc_int cert_chain_length;
71
72 /* "s!ss" == 1 nullable string, 2 strings */
73 /* TODO: support multiple key cert pairs. */
74 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s!ss", &pem_root_certs,
75 &root_certs_length, &pem_key_cert_pair.private_key,
76 &private_key_length, &pem_key_cert_pair.cert_chain,
77 &cert_chain_length) == FAILURE) {
78 zend_throw_exception(spl_ce_InvalidArgumentException,
79 "createSsl expects 3 strings", 1 TSRMLS_CC);
80 return;
81 }
82 /* TODO: add a client_certificate_request field in ServerCredentials and pass
83 * it as the last parameter. */
84 grpc_server_credentials *creds = grpc_ssl_server_credentials_create_ex(
85 pem_root_certs, &pem_key_cert_pair, 1,
86 GRPC_SSL_DONT_REQUEST_CLIENT_CERTIFICATE, NULL);
87 zval *creds_object = grpc_php_wrap_server_credentials(creds TSRMLS_CC);
88 RETURN_DESTROY_ZVAL(creds_object);
89 }
90
91 ZEND_BEGIN_ARG_INFO_EX(arginfo_createSsl, 0, 0, 3)
92 ZEND_ARG_INFO(0, pem_root_certs)
93 ZEND_ARG_INFO(0, pem_private_key)
94 ZEND_ARG_INFO(0, pem_cert_chain)
95 ZEND_END_ARG_INFO()
96
97 static zend_function_entry server_credentials_methods[] = {
98 PHP_ME(ServerCredentials, createSsl, arginfo_createSsl,
99 ZEND_ACC_PUBLIC | ZEND_ACC_STATIC)
100 PHP_FE_END
101 };
102
grpc_init_server_credentials(TSRMLS_D)103 void grpc_init_server_credentials(TSRMLS_D) {
104 zend_class_entry ce;
105 INIT_CLASS_ENTRY(ce, "Grpc\\ServerCredentials", server_credentials_methods);
106 ce.create_object = create_wrapped_grpc_server_credentials;
107 grpc_ce_server_credentials = zend_register_internal_class(&ce TSRMLS_CC);
108 PHP_GRPC_INIT_HANDLER(wrapped_grpc_server_credentials,
109 server_credentials_ce_handlers);
110 }
111