1--TEST--
2openssl_*() with OPENSSL_KEYTYPE_EC for ec custom params
3--EXTENSIONS--
4openssl
5--SKIPIF--
6<?php
7if (!defined("OPENSSL_KEYTYPE_EC")) die("skip EC disabled");
8?>
9--FILE--
10<?php
11// EC - generate keypair with curve_name
12echo "Testing openssl_pkey_new with ec curve_name\n";
13$curve_name = openssl_get_curve_names()[0];
14
15$ec = openssl_pkey_new(array(
16    'ec'=> array(
17        'curve_name' => $curve_name,
18    )
19));
20
21var_dump($ec);
22$details = openssl_pkey_get_details($ec);
23$ec_details = $details['ec'];
24var_dump($ec_details['curve_name'] === $curve_name);
25
26// EC - generate keypair from priv_key "d" with explicit parameters (OSCCA WAPIP192v1 Elliptic curve)
27echo "Testing openssl_pkey_new with ec explicit parameters\n";
28$d = hex2bin('8D0AC65AAEA0D6B96254C65817D4A143A9E7A03876F1A37D');
29$x = hex2bin('98E07AAD50C31F9189EBE6B8B5C70E5DEE59D7A8BC344CC6');
30$y = hex2bin('6109D3D96E52D0867B9D05D72D07BE5876A3D973E0E96792');
31$p = hex2bin('BDB6F4FE3E8B1D9E0DA8C0D46F4C318CEFE4AFE3B6B8551F');
32$a = hex2bin('BB8E5E8FBC115E139FE6A814FE48AAA6F0ADA1AA5DF91985');
33$b = hex2bin('1854BEBDC31B21B7AEFC80AB0ECD10D5B1B3308E6DBF11C1');
34$g_x = hex2bin('4AD5F7048DE709AD51236DE65E4D4B482C836DC6E4106640');
35$g_y = hex2bin('02BB3A02D4AAADACAE24817A4CA3A1B014B5270432DB27D2');
36$order = hex2bin('BDB6F4FE3E8B1D9E0DA8C0D40FC962195DFAE76F56564677');
37
38$ec = openssl_pkey_new(array(
39    'ec'=> array(
40        'p' => $p,
41        'a' => $a,
42        'b' => $b,
43        'order' => $order,
44        'g_x' => $g_x,
45        'g_y' => $g_y,
46        'd' => $d,
47    )
48));
49
50$details = openssl_pkey_get_details($ec);
51$ec_details = $details['ec'];
52var_dump($ec_details['x'] === $x);
53var_dump($ec_details['y'] === $y);
54var_dump($ec_details['d'] === $d);
55
56echo "Testing openssl_pkey_new with ec missing params \n";
57// EC - invalid curve_name
58$ec = openssl_pkey_new(array(
59    'ec'=> array(
60        'curve_name' => 'invalid_curve_name',
61    )
62));
63var_dump($ec);
64
65// EC - missing all params
66$ec = openssl_pkey_new(array(
67    'ec'=> array()
68));
69var_dump($ec);
70
71// EC - missing "p" param
72$ec = openssl_pkey_new(array(
73    'ec'=> array(
74        'a' => $a,
75        'b' => $b,
76        'order' => $order
77    )
78));
79var_dump($ec);
80
81// EC - missing "generator" or "g_x" and "g_y" param
82$ec = openssl_pkey_new(array(
83    'ec'=> array(
84        'p' => $p,
85        'a' => $a,
86        'b' => $b,
87        'order' => $order
88    )
89));
90var_dump($ec);
91?>
92--EXPECTF--
93Testing openssl_pkey_new with ec curve_name
94object(OpenSSLAsymmetricKey)#%d (0) {
95}
96bool(true)
97Testing openssl_pkey_new with ec explicit parameters
98bool(true)
99bool(true)
100bool(true)
101Testing openssl_pkey_new with ec missing params
102
103Warning: openssl_pkey_new(): Unknown elliptic curve (short) name invalid_curve_name in %s on line %d
104bool(false)
105
106Warning: openssl_pkey_new(): Missing params: curve_name in %s on line %d
107bool(false)
108
109Warning: openssl_pkey_new(): Missing params: curve_name or p, a, b, order in %s on line %d
110bool(false)
111
112Warning: openssl_pkey_new(): Missing params: generator or g_x and g_y in %s on line %d
113bool(false)
114