curl_setopt_ssl.phpt 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. --TEST--
  2. CURLOPT_SSL* basic client auth tests
  3. --EXTENSIONS--
  4. curl
  5. --SKIPIF--
  6. <?php
  7. if (!function_exists("proc_open")) die("skip no proc_open");
  8. exec('openssl version', $out, $code);
  9. if ($code > 0) die("skip couldn't locate openssl binary");
  10. if (PHP_OS_FAMILY === 'Windows') die('skip not for Windows');
  11. $curl_version = curl_version();
  12. if ($curl_version['version_number'] < 0x074700) {
  13. die("skip: blob options not supported for curl < 7.71.0");
  14. }
  15. ?>
  16. --FILE--
  17. <?php
  18. function check_error(CurlHandle $ch) {
  19. if (curl_errno($ch) !== 0) {
  20. echo "CURL ERROR: " . curl_errno($ch) . "\n";
  21. }
  22. }
  23. function check_response($response, $clientCertSubject) {
  24. if (strpos($response, $clientCertSubject) === false) {
  25. echo "client cert subject not in response\n";
  26. } else {
  27. echo "client cert subject in response\n";
  28. }
  29. }
  30. $clientCertSubject = "Subject: C=US, ST=TX, L=Clientlocation, O=Clientcompany, CN=clientname/emailAddress=test@example.com";
  31. // load server cert
  32. $serverCertPath = __DIR__ . DIRECTORY_SEPARATOR . 'curl_setopt_ssl_servercert.pem';
  33. $serverCert = file_get_contents($serverCertPath);
  34. // load server key
  35. $serverKeyPath = __DIR__ . DIRECTORY_SEPARATOR . 'curl_setopt_ssl_serverkey.pem';
  36. $serverKey = file_get_contents($serverKeyPath);
  37. // load client cert
  38. $clientCertPath = __DIR__ . DIRECTORY_SEPARATOR . 'curl_setopt_ssl_clientcert.pem';
  39. $clientCert = file_get_contents($clientCertPath);
  40. // load client key
  41. $clientKeyPath = __DIR__ . DIRECTORY_SEPARATOR . 'curl_setopt_ssl_clientkey.pem';
  42. $clientKey = file_get_contents($clientKeyPath);
  43. if ($serverCert === false
  44. || $serverKey === false
  45. || $clientCert === false
  46. || $clientKey === false
  47. ) {
  48. die('failed to load test certs and keys for files');
  49. }
  50. $port = 14430;
  51. // set up local server
  52. $cmd = "openssl s_server -key $serverKeyPath -cert $serverCertPath -accept $port -www -CAfile $clientCertPath -verify_return_error -Verify 1";
  53. $process = proc_open($cmd, [["pipe", "r"], ["pipe", "w"], ["pipe", "w"]], $pipes);
  54. if ($process === false) {
  55. die('failed to start server');
  56. }
  57. try {
  58. // Give the server time to start
  59. sleep(1);
  60. echo "case 1: client cert and key from string\n";
  61. $ch = curl_init("https://127.0.0.1:$port/");
  62. var_dump(curl_setopt($ch, CURLOPT_SSLCERT_BLOB, $clientCert));
  63. var_dump(curl_setopt($ch, CURLOPT_SSLKEY_BLOB, $clientKey));
  64. var_dump(curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false));
  65. var_dump(curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false));
  66. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  67. $response = curl_exec($ch);
  68. check_response($response, $clientCertSubject);
  69. check_error($ch);
  70. curl_close($ch);
  71. echo "\n";
  72. echo "case 2: empty client cert and key from string\n";
  73. $ch = curl_init("https://127.0.0.1:$port/");
  74. var_dump(curl_setopt($ch, CURLOPT_SSLCERT_BLOB, ''));
  75. var_dump(curl_setopt($ch, CURLOPT_SSLKEY_BLOB, $clientKey));
  76. var_dump(curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false));
  77. var_dump(curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false));
  78. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  79. $response = curl_exec($ch);
  80. check_response($response, $clientCertSubject);
  81. check_error($ch);
  82. curl_close($ch);
  83. echo "\n";
  84. echo "case 3: client cert and empty key from string\n";
  85. $ch = curl_init("https://127.0.0.1:$port/");
  86. var_dump(curl_setopt($ch, CURLOPT_SSLCERT_BLOB, $clientCert));
  87. var_dump(curl_setopt($ch, CURLOPT_SSLKEY_BLOB, ''));
  88. var_dump(curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false));
  89. var_dump(curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false));
  90. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  91. $response = curl_exec($ch);
  92. check_response($response, $clientCertSubject);
  93. check_error($ch);
  94. curl_close($ch);
  95. echo "\n";
  96. echo "case 4: client cert and key from file\n";
  97. $ch = curl_init("https://127.0.0.1:$port/");
  98. var_dump(curl_setopt($ch, CURLOPT_SSLCERT, $clientCertPath));
  99. var_dump(curl_setopt($ch, CURLOPT_SSLKEY, $clientKeyPath));
  100. var_dump(curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false));
  101. var_dump(curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false));
  102. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  103. $response = curl_exec($ch);
  104. check_response($response, $clientCertSubject);
  105. check_error($ch);
  106. curl_close($ch);
  107. echo "\n";
  108. echo "case 5: issuer cert from file\n";
  109. $ch = curl_init("https://127.0.0.1:$port/");
  110. var_dump(curl_setopt($ch, CURLOPT_CAINFO, $serverCertPath));
  111. var_dump(curl_setopt($ch, CURLOPT_ISSUERCERT, $serverCertPath));
  112. var_dump(curl_setopt($ch, CURLOPT_SSLCERT, $clientCertPath));
  113. var_dump(curl_setopt($ch, CURLOPT_SSLKEY, $clientKeyPath));
  114. var_dump(curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true));
  115. var_dump(curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false));
  116. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  117. $response = curl_exec($ch);
  118. check_response($response, $clientCertSubject);
  119. check_error($ch);
  120. curl_close($ch);
  121. echo "\n";
  122. echo "case 6: issuer cert from string\n";
  123. $ch = curl_init("https://127.0.0.1:$port/");
  124. var_dump(curl_setopt($ch, CURLOPT_CAINFO, $serverCertPath));
  125. var_dump(curl_setopt($ch, CURLOPT_ISSUERCERT_BLOB, $serverCert));
  126. var_dump(curl_setopt($ch, CURLOPT_SSLCERT, $clientCertPath));
  127. var_dump(curl_setopt($ch, CURLOPT_SSLKEY, $clientKeyPath));
  128. var_dump(curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true));
  129. var_dump(curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false));
  130. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  131. $response = curl_exec($ch);
  132. check_response($response, $clientCertSubject);
  133. check_error($ch);
  134. curl_close($ch);
  135. echo "\n";
  136. echo "case 7: empty issuer cert from string\n";
  137. $ch = curl_init("https://127.0.0.1:$port/");
  138. var_dump(curl_setopt($ch, CURLOPT_CAINFO, $serverCertPath));
  139. var_dump(curl_setopt($ch, CURLOPT_ISSUERCERT_BLOB, ''));
  140. var_dump(curl_setopt($ch, CURLOPT_SSLCERT, $clientCertPath));
  141. var_dump(curl_setopt($ch, CURLOPT_SSLKEY, $clientKeyPath));
  142. var_dump(curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true));
  143. var_dump(curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false));
  144. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  145. $response = curl_exec($ch);
  146. check_response($response, $clientCertSubject);
  147. check_error($ch);
  148. curl_close($ch);
  149. } finally {
  150. // clean up server process
  151. proc_terminate($process);
  152. proc_close($process);
  153. }
  154. ?>
  155. --EXPECT--
  156. case 1: client cert and key from string
  157. bool(true)
  158. bool(true)
  159. bool(true)
  160. bool(true)
  161. client cert subject in response
  162. case 2: empty client cert and key from string
  163. bool(true)
  164. bool(true)
  165. bool(true)
  166. bool(true)
  167. client cert subject not in response
  168. CURL ERROR: 58
  169. case 3: client cert and empty key from string
  170. bool(true)
  171. bool(true)
  172. bool(true)
  173. bool(true)
  174. client cert subject not in response
  175. CURL ERROR: 58
  176. case 4: client cert and key from file
  177. bool(true)
  178. bool(true)
  179. bool(true)
  180. bool(true)
  181. client cert subject in response
  182. case 5: issuer cert from file
  183. bool(true)
  184. bool(true)
  185. bool(true)
  186. bool(true)
  187. bool(true)
  188. bool(true)
  189. client cert subject in response
  190. case 6: issuer cert from string
  191. bool(true)
  192. bool(true)
  193. bool(true)
  194. bool(true)
  195. bool(true)
  196. bool(true)
  197. client cert subject in response
  198. case 7: empty issuer cert from string
  199. bool(true)
  200. bool(true)
  201. bool(true)
  202. bool(true)
  203. bool(true)
  204. bool(true)
  205. client cert subject not in response
  206. CURL ERROR: 83