File: ed25519.phpt

package info (click to toggle)
php8.4 8.4.11-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 208,108 kB
  • sloc: ansic: 1,060,628; php: 35,345; sh: 11,866; cpp: 7,201; pascal: 4,913; javascript: 3,091; asm: 2,810; yacc: 2,411; makefile: 689; xml: 446; python: 301; awk: 148
file content (122 lines) | stat: -rw-r--r-- 3,247 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
--TEST--
openssl_*() with OPENSSL_KEYTYPE_ED25519
--EXTENSIONS--
openssl
--SKIPIF--
<?php
if (!defined("OPENSSL_KEYTYPE_ED25519")) die("skip ED25519 not available");
?>
--FILE--
<?php
echo "Testing openssl_pkey_new\n";
$key1 = openssl_pkey_new([ "private_key_type" => OPENSSL_KEYTYPE_ED25519 ]);
var_dump($key1);

$d1 = openssl_pkey_get_details($key1);
var_dump($d1["bits"] === 256);
var_dump(strlen($d1["key"]) === 113);
var_dump(strlen($d1["ed25519"]["priv_key"]) === 32);
var_dump($d1["type"] === OPENSSL_KEYTYPE_ED25519);

$key2 = openssl_pkey_new($d1);
var_dump($key2);

$d2 = openssl_pkey_get_details($key2);
// Compare array
var_dump($d1 == $d2);

// Check that the public key info is computed from the private key if it is missing.
$d1_priv = $d1;
unset($d1_priv["ed25519"]["pub_key"]);
$key3 = openssl_pkey_new($d1_priv);
var_dump($key3);
$d3 = openssl_pkey_get_details($key3);
var_dump($d1 == $d3);

// create public key from private
$d1_pub = $d1;
unset($d1_pub["ed25519"]["priv_key"]);
$pubkey1 = openssl_pkey_new($d1_pub);
var_dump($pubkey1);
$pubkey1_d = openssl_pkey_get_details($pubkey1);
var_dump($d1_pub == $pubkey1_d);

// sign and verify
echo "Testing openssl_sign and openssl_verify\n";
$payload = "somedata";
var_dump(openssl_sign($payload, $signature, $key1, 0));
var_dump(strlen($signature) === 64);
var_dump(openssl_verify($payload, $signature, $pubkey1, 0));


$dn = array(
    "countryName" => "BR",
    "stateOrProvinceName" => "Rio Grande do Sul",
    "localityName" => "Porto Alegre",
    "commonName" => "Henrique do N. Angelo",
    "emailAddress" => "hnangelo@php.net"
);
$config =  __DIR__ . DIRECTORY_SEPARATOR . 'openssl.cnf';
$args = array(
    "private_key_type" => OPENSSL_KEYTYPE_ED25519,
    "config" => $config,
    "digest_alg" => "null",
);

// openssl_csr_new creates a new public key pair if the key argument is null
echo "Testing openssl_csr_new with key generation\n";
$keyGenerate = null;
$csr = openssl_csr_new($dn, $keyGenerate, $args);
var_dump($keyGenerate);
var_dump($csr);

echo "Testing openssl_csr_new with existing key\n";
$csr = openssl_csr_new($dn, $key1, $args);
$pubkey_csr = openssl_pkey_get_details(openssl_csr_get_public_key($csr));
var_dump($pubkey_csr == $pubkey1_d);

echo "Testing openssl_csr_sign\n";
$x509 = openssl_csr_sign($csr, null, $key1, 365, $args);
var_dump($x509);

echo "Testing openssl_x509_{verify,check_private_key}\n";
var_dump(openssl_x509_check_private_key($x509, $key1));
var_dump(openssl_x509_verify($x509, $pubkey1));
var_dump(openssl_x509_check_private_key($x509, $keyGenerate));

?>
--EXPECTF--
Testing openssl_pkey_new
object(OpenSSLAsymmetricKey)#%d (0) {
}
bool(true)
bool(true)
bool(true)
bool(true)
object(OpenSSLAsymmetricKey)#%d (0) {
}
bool(true)
object(OpenSSLAsymmetricKey)#%d (0) {
}
bool(true)
object(OpenSSLAsymmetricKey)#%d (0) {
}
bool(true)
Testing openssl_sign and openssl_verify
bool(true)
bool(true)
int(1)
Testing openssl_csr_new with key generation
object(OpenSSLAsymmetricKey)#%d (0) {
}
object(OpenSSLCertificateSigningRequest)#%d (0) {
}
Testing openssl_csr_new with existing key
bool(true)
Testing openssl_csr_sign
object(OpenSSLCertificate)#%d (0) {
}
Testing openssl_x509_{verify,check_private_key}
bool(true)
int(1)
bool(false)