"Fossies" - the Fresh Open Source Software Archive

Member "php-8.0.28-src/ext/openssl/tests/001.phpt" (14 Feb 2023, 2048 Bytes) of package /windows/www/php-8.0.28-src.zip:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) PHP source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 --TEST--
    2 OpenSSL private key functions
    3 --SKIPIF--
    4 <?php
    5 if (!extension_loaded("openssl")) die("skip");
    6 if (!@openssl_pkey_new()) die("skip cannot create private key");
    7 ?>
    8 --FILE--
    9 <?php
   10 echo "Creating private key\n";
   11 
   12 $conf = array('config' => __DIR__ . DIRECTORY_SEPARATOR . 'openssl.cnf');
   13 $privkey = openssl_pkey_new($conf);
   14 
   15 if ($privkey === false) {
   16     die("failed to create private key");
   17 }
   18 
   19 $passphrase = "banana";
   20 $key_file_name = __DIR__ . '/001-tmp.key';
   21 if ($key_file_name === false) {
   22     die("failed to get a temporary filename!");
   23 }
   24 
   25 echo "Export key to file\n";
   26 
   27 if (!openssl_pkey_export_to_file($privkey, $key_file_name, $passphrase, $conf)) {
   28     die("failed to export to file $key_file_name");
   29 }
   30 var_dump($privkey instanceof OpenSSLAsymmetricKey);
   31 
   32 echo "Load key from file - array syntax\n";
   33 
   34 $loaded_key = openssl_pkey_get_private(array("file://$key_file_name", $passphrase));
   35 
   36 if ($loaded_key === false) {
   37     die("failed to load key using array syntax");
   38 }
   39 
   40 openssl_pkey_free($loaded_key);
   41 
   42 echo "Load key using direct syntax\n";
   43 
   44 $loaded_key = openssl_pkey_get_private("file://$key_file_name", $passphrase);
   45 
   46 if ($loaded_key === false) {
   47     die("failed to load key using direct syntax");
   48 }
   49 
   50 openssl_pkey_free($loaded_key);
   51 
   52 echo "Load key manually and use string syntax\n";
   53 
   54 $key_content = file_get_contents($key_file_name);
   55 $loaded_key = openssl_pkey_get_private($key_content, $passphrase);
   56 
   57 if ($loaded_key === false) {
   58     die("failed to load key using string syntax");
   59 }
   60 openssl_pkey_free($loaded_key);
   61 
   62 echo "OK!\n";
   63 
   64 ?>
   65 --CLEAN--
   66 <?php
   67 $key_file_name = __DIR__ . DIRECTORY_SEPARATOR . '001-tmp.key';
   68 @unlink($key_file_name);
   69 ?>
   70 --EXPECTF--
   71 Creating private key
   72 Export key to file
   73 bool(true)
   74 Load key from file - array syntax
   75 
   76 Deprecated: Function openssl_pkey_free() is deprecated in %s on line %d
   77 Load key using direct syntax
   78 
   79 Deprecated: Function openssl_pkey_free() is deprecated in %s on line %d
   80 Load key manually and use string syntax
   81 
   82 Deprecated: Function openssl_pkey_free() is deprecated in %s on line %d
   83 OK!