1--TEST--
2openssl_spki_export() tests for exporting public key
3--SKIPIF--
4<?php
5if (!extension_loaded("openssl")) die("skip");
6if (!@openssl_pkey_new()) die("skip cannot create private key");
7?>
8--FILE--
9<?php
10
11/* array of private key sizes to test */
12$key_sizes = array(1024, 2048, 4096);
13$pkeys = array();
14foreach ($key_sizes as $key_size) {
15    $key_file = "file://" . __DIR__ . "/private_rsa_" . $key_size . ".key";
16    $pkeys[] = openssl_pkey_get_private($key_file);
17}
18
19
20/* array of available hashings to test */
21$algo = array(
22    OPENSSL_ALGO_MD4,
23    OPENSSL_ALGO_MD5,
24    OPENSSL_ALGO_SHA1,
25    OPENSSL_ALGO_SHA224,
26    OPENSSL_ALGO_SHA256,
27    OPENSSL_ALGO_SHA384,
28    OPENSSL_ALGO_SHA512,
29    OPENSSL_ALGO_RMD160
30);
31
32/* loop over key sizes for test */
33foreach ($pkeys as $pkey) {
34
35    /* loop to create and verify results */
36    foreach ($algo as $value) {
37        $spkac = openssl_spki_new($pkey, _uuid(), $value);
38        echo openssl_spki_export(preg_replace('/SPKAC=/', '', $spkac));
39    }
40}
41
42/* generate a random challenge */
43function _uuid() {
44    return sprintf('%04x%04x-%04x-%04x-%04x-%04x%04x%04x', mt_rand(0, 0xffff),
45        mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0x0fff) | 0x4000,
46        mt_rand(0, 0x3fff) | 0x8000, mt_rand(0, 0xffff),
47        mt_rand(0, 0xffff), mt_rand(0, 0xffff));
48}
49
50
51?>
52--EXPECTREGEX--
53\-\-\-\-\-BEGIN PUBLIC KEY\-\-\-\-\-.*\-\-\-\-\-END PUBLIC KEY\-\-\-\-\-
54\-\-\-\-\-BEGIN PUBLIC KEY\-\-\-\-\-.*\-\-\-\-\-END PUBLIC KEY\-\-\-\-\-
55\-\-\-\-\-BEGIN PUBLIC KEY\-\-\-\-\-.*\-\-\-\-\-END PUBLIC KEY\-\-\-\-\-
56\-\-\-\-\-BEGIN PUBLIC KEY\-\-\-\-\-.*\-\-\-\-\-END PUBLIC KEY\-\-\-\-\-
57\-\-\-\-\-BEGIN PUBLIC KEY\-\-\-\-\-.*\-\-\-\-\-END PUBLIC KEY\-\-\-\-\-
58\-\-\-\-\-BEGIN PUBLIC KEY\-\-\-\-\-.*\-\-\-\-\-END PUBLIC KEY\-\-\-\-\-
59\-\-\-\-\-BEGIN PUBLIC KEY\-\-\-\-\-.*\-\-\-\-\-END PUBLIC KEY\-\-\-\-\-
60\-\-\-\-\-BEGIN PUBLIC KEY\-\-\-\-\-.*\-\-\-\-\-END PUBLIC KEY\-\-\-\-\-
61