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