[php-src] Issue #13995: Include JS WebCrypto raw format for openssl_get_publicKey
| From: | nwnisworking | Date: | Thu, 18 Apr 2024 06:40:53 +0000 |
| Subject: | [php-src] Issue #13995: Include JS WebCrypto raw format for openssl_get_publicKey | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-246784@lists.php.net to get a copy of this message | ||
Issue: https://github.com/php/php-src/issues/13995
Author: nwnisworking
### Description
With the inclusion of JS WebCrypto API, I believe it is necessary to support the exported raw format
when client sends their own public EC keys to the server.
```js
const { publicKey } = await crypto.subtle.generateKey({
name : 'ECDH',
namedCurve : 'P-256'
}, true, ['deriveBits'])
const exported_key = new Uint8Array(await crypto.subtle.exportKey('raw', publicKey))
fetch('key.php', {
method : 'post',
body : btoa(String.fromCharCode(...exported_key))
}).then(e=>e.text())
```
However, openssl_pkey_get_public only accepts PEM formatted string. This would mean a whole lot of
concatenating to do before we are able to get the public
<code>OpenSSLAsymmetricKey</code>
```php
$raw_format = base64_decode(file_get_content("php://input"));
// This header is only for P-256
$asn_1_header = pack('C*', ...[48, 89, 48, 19, 6, 7, 42, 134, 72, 206, 61, 2, 1, 6, 8, 42,
134, 72, 206, 61, 3, 1, 7, 3, 66, 0]);
var_dump(openssl_get_publicKey("-----BEGIN PUBLIC
KEY-----\n".base64_encode($asn_1_header.$raw_format)."\n-----END PUBLIC KEY-----"));
```
It would be pretty cool to simplify it so that it becomes the following.
```
openssl_get_publicKey($raw_format);
```
## Note
I know that WebCrypto API does allow exporting SPKI key but that is besides the point.