• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 /**
20  * class ServerCredentials
21  * @see https://github.com/grpc/grpc/tree/master/src/php/ext/grpc/server_credentials.c
22  */
23 
24 #include "server_credentials.h"
25 
26 #include <ext/spl/spl_exceptions.h>
27 #include <zend_exceptions.h>
28 
29 zend_class_entry *grpc_ce_server_credentials;
30 PHP_GRPC_DECLARE_OBJECT_HANDLER(server_credentials_ce_handlers)
31 
32 /* Frees and destroys an instace of wrapped_grpc_server_credentials */
33 PHP_GRPC_FREE_WRAPPED_FUNC_START(wrapped_grpc_server_credentials)
34   if (p->wrapped != NULL) {
35     grpc_server_credentials_release(p->wrapped);
36   }
PHP_GRPC_FREE_WRAPPED_FUNC_END()37 PHP_GRPC_FREE_WRAPPED_FUNC_END()
38 
39 /* Initializes an instace of wrapped_grpc_server_credentials to be
40  * associated with an object of a class specified by class_type */
41 php_grpc_zend_object create_wrapped_grpc_server_credentials(
42     zend_class_entry *class_type TSRMLS_DC) {
43   PHP_GRPC_ALLOC_CLASS_OBJECT(wrapped_grpc_server_credentials);
44   zend_object_std_init(&intern->std, class_type TSRMLS_CC);
45   object_properties_init(&intern->std, class_type);
46   PHP_GRPC_FREE_CLASS_OBJECT(wrapped_grpc_server_credentials,
47                              server_credentials_ce_handlers);
48 }
49 
grpc_php_wrap_server_credentials(grpc_server_credentials * wrapped TSRMLS_DC)50 zval *grpc_php_wrap_server_credentials(grpc_server_credentials
51                                        *wrapped TSRMLS_DC) {
52   zval *server_credentials_object;
53   PHP_GRPC_MAKE_STD_ZVAL(server_credentials_object);
54   object_init_ex(server_credentials_object, grpc_ce_server_credentials);
55   wrapped_grpc_server_credentials *server_credentials =
56     PHP_GRPC_GET_WRAPPED_OBJECT(wrapped_grpc_server_credentials,
57                                 server_credentials_object);
58   server_credentials->wrapped = wrapped;
59   return server_credentials_object;
60 }
61 
62 /**
63  * Create SSL credentials.
64  * @param string $pem_root_certs PEM encoding of the server root certificates
65  * @param string $pem_private_key PEM encoding of the client's private key
66  * @param string $pem_cert_chain PEM encoding of the client's certificate chain
67  * @return Credentials The new SSL credentials object
68  */
PHP_METHOD(ServerCredentials,createSsl)69 PHP_METHOD(ServerCredentials, createSsl) {
70   char *pem_root_certs = 0;
71   grpc_ssl_pem_key_cert_pair pem_key_cert_pair;
72 
73   php_grpc_int root_certs_length = 0;
74   php_grpc_int private_key_length;
75   php_grpc_int cert_chain_length;
76 
77   /* "s!ss" == 1 nullable string, 2 strings */
78   /* TODO: support multiple key cert pairs. */
79   if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s!ss", &pem_root_certs,
80                             &root_certs_length, &pem_key_cert_pair.private_key,
81                             &private_key_length, &pem_key_cert_pair.cert_chain,
82                             &cert_chain_length) == FAILURE) {
83     zend_throw_exception(spl_ce_InvalidArgumentException,
84                          "createSsl expects 3 strings", 1 TSRMLS_CC);
85     return;
86   }
87   /* TODO: add a client_certificate_request field in ServerCredentials and pass
88    * it as the last parameter. */
89   grpc_server_credentials *creds = grpc_ssl_server_credentials_create_ex(
90       pem_root_certs, &pem_key_cert_pair, 1,
91       GRPC_SSL_DONT_REQUEST_CLIENT_CERTIFICATE, NULL);
92   zval *creds_object = grpc_php_wrap_server_credentials(creds TSRMLS_CC);
93   RETURN_DESTROY_ZVAL(creds_object);
94 }
95 
96 ZEND_BEGIN_ARG_INFO_EX(arginfo_createSsl, 0, 0, 3)
97   ZEND_ARG_INFO(0, pem_root_certs)
98   ZEND_ARG_INFO(0, pem_private_key)
99   ZEND_ARG_INFO(0, pem_cert_chain)
100 ZEND_END_ARG_INFO()
101 
102 static zend_function_entry server_credentials_methods[] = {
103   PHP_ME(ServerCredentials, createSsl, arginfo_createSsl,
104          ZEND_ACC_PUBLIC | ZEND_ACC_STATIC)
105   PHP_FE_END
106  };
107 
grpc_init_server_credentials(TSRMLS_D)108 void grpc_init_server_credentials(TSRMLS_D) {
109   zend_class_entry ce;
110   INIT_CLASS_ENTRY(ce, "Grpc\\ServerCredentials", server_credentials_methods);
111   ce.create_object = create_wrapped_grpc_server_credentials;
112   grpc_ce_server_credentials = zend_register_internal_class(&ce TSRMLS_CC);
113   PHP_GRPC_INIT_HANDLER(wrapped_grpc_server_credentials,
114                         server_credentials_ce_handlers);
115 }
116