xref: /php-src/ext/openssl/tests/ed25519.phpt (revision 5f2a0c83)
1--TEST--
2openssl_*() with OPENSSL_KEYTYPE_ED25519
3--EXTENSIONS--
4openssl
5--SKIPIF--
6<?php
7if (!defined("OPENSSL_KEYTYPE_ED25519")) die("skip ED25519 not available");
8?>
9--FILE--
10<?php
11echo "Testing openssl_pkey_new\n";
12$key1 = openssl_pkey_new([ "private_key_type" => OPENSSL_KEYTYPE_ED25519 ]);
13var_dump($key1);
14
15$d1 = openssl_pkey_get_details($key1);
16var_dump($d1["bits"] === 256);
17var_dump(strlen($d1["key"]) === 113);
18var_dump(strlen($d1["ed25519"]["priv_key"]) === 32);
19var_dump($d1["type"] === OPENSSL_KEYTYPE_ED25519);
20
21$key2 = openssl_pkey_new($d1);
22var_dump($key2);
23
24$d2 = openssl_pkey_get_details($key2);
25// Compare array
26var_dump($d1 == $d2);
27
28// Check that the public key info is computed from the private key if it is missing.
29$d1_priv = $d1;
30unset($d1_priv["ed25519"]["pub_key"]);
31$key3 = openssl_pkey_new($d1_priv);
32var_dump($key3);
33$d3 = openssl_pkey_get_details($key3);
34var_dump($d1 == $d3);
35
36// create public key from private
37$d1_pub = $d1;
38unset($d1_pub["ed25519"]["priv_key"]);
39$pubkey1 = openssl_pkey_new($d1_pub);
40var_dump($pubkey1);
41$pubkey1_d = openssl_pkey_get_details($pubkey1);
42var_dump($d1_pub == $pubkey1_d);
43
44// sign and verify
45echo "Testing openssl_sign and openssl_verify\n";
46$payload = "somedata";
47var_dump(openssl_sign($payload, $signature, $key1, 0));
48var_dump(strlen($signature) === 64);
49var_dump(openssl_verify($payload, $signature, $pubkey1, 0));
50
51
52$dn = array(
53    "countryName" => "BR",
54    "stateOrProvinceName" => "Rio Grande do Sul",
55    "localityName" => "Porto Alegre",
56    "commonName" => "Henrique do N. Angelo",
57    "emailAddress" => "hnangelo@php.net"
58);
59$config =  __DIR__ . DIRECTORY_SEPARATOR . 'openssl.cnf';
60$args = array(
61    "private_key_type" => OPENSSL_KEYTYPE_ED25519,
62    "config" => $config,
63    "digest_alg" => "null",
64);
65
66// openssl_csr_new creates a new public key pair if the key argument is null
67echo "Testing openssl_csr_new with key generation\n";
68$keyGenerate = null;
69$csr = openssl_csr_new($dn, $keyGenerate, $args);
70var_dump($keyGenerate);
71var_dump($csr);
72
73echo "Testing openssl_csr_new with existing key\n";
74$csr = openssl_csr_new($dn, $key1, $args);
75$pubkey_csr = openssl_pkey_get_details(openssl_csr_get_public_key($csr));
76var_dump($pubkey_csr == $pubkey1_d);
77
78echo "Testing openssl_csr_sign\n";
79$x509 = openssl_csr_sign($csr, null, $key1, 365, $args);
80var_dump($x509);
81
82echo "Testing openssl_x509_{verify,check_private_key}\n";
83var_dump(openssl_x509_check_private_key($x509, $key1));
84var_dump(openssl_x509_verify($x509, $pubkey1));
85var_dump(openssl_x509_check_private_key($x509, $keyGenerate));
86
87?>
88--EXPECTF--
89Testing openssl_pkey_new
90object(OpenSSLAsymmetricKey)#%d (0) {
91}
92bool(true)
93bool(true)
94bool(true)
95bool(true)
96object(OpenSSLAsymmetricKey)#%d (0) {
97}
98bool(true)
99object(OpenSSLAsymmetricKey)#%d (0) {
100}
101bool(true)
102object(OpenSSLAsymmetricKey)#%d (0) {
103}
104bool(true)
105Testing openssl_sign and openssl_verify
106bool(true)
107bool(true)
108int(1)
109Testing openssl_csr_new with key generation
110object(OpenSSLAsymmetricKey)#%d (0) {
111}
112object(OpenSSLCertificateSigningRequest)#%d (0) {
113}
114Testing openssl_csr_new with existing key
115bool(true)
116Testing openssl_csr_sign
117object(OpenSSLCertificate)#%d (0) {
118}
119Testing openssl_x509_{verify,check_private_key}
120bool(true)
121int(1)
122bool(false)
123