PocketMine-MP 5.19.1 git-5cc1068cd43264d3363295eb8d6901e02f467897
PrepareEncryptionTask.php
1<?php
2
3/*
4 *
5 * ____ _ _ __ __ _ __ __ ____
6 * | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
7 * | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
8 * | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
9 * |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
10 *
11 * This program is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License as published by
13 * the Free Software Foundation, either version 3 of the License, or
14 * (at your option) any later version.
15 *
16 * @author PocketMine Team
17 * @link http://www.pocketmine.net/
18 *
19 *
20 */
21
22declare(strict_types=1);
23
24namespace pocketmine\network\mcpe\encryption;
25
29use function igbinary_serialize;
30use function igbinary_unserialize;
31use function openssl_error_string;
32use function openssl_pkey_get_details;
33use function openssl_pkey_new;
34use function random_bytes;
35
37
38 private const TLS_KEY_ON_COMPLETION = "completion";
39
40 private static ?\OpenSSLAsymmetricKey $SERVER_PRIVATE_KEY = null;
41
42 private string $serverPrivateKey;
43
44 private ?string $aesKey = null;
45 private ?string $handshakeJwt = null;
46
50 public function __construct(
51 private string $clientPub,
52 \Closure $onCompletion
53 ){
54 if(self::$SERVER_PRIVATE_KEY === null){
55 $serverPrivateKey = openssl_pkey_new(["ec" => ["curve_name" => "secp384r1"]]);
56 if($serverPrivateKey === false){
57 throw new \RuntimeException("openssl_pkey_new() failed: " . openssl_error_string());
58 }
59 self::$SERVER_PRIVATE_KEY = $serverPrivateKey;
60 }
61
62 $this->serverPrivateKey = igbinary_serialize(openssl_pkey_get_details(self::$SERVER_PRIVATE_KEY));
63 $this->storeLocal(self::TLS_KEY_ON_COMPLETION, $onCompletion);
64 }
65
66 public function onRun() : void{
68 $serverPrivDetails = igbinary_unserialize($this->serverPrivateKey);
69 $serverPriv = openssl_pkey_new($serverPrivDetails);
70 if($serverPriv === false) throw new AssumptionFailedError("Failed to restore server signing key from details");
71 $clientPub = JwtUtils::parseDerPublicKey($this->clientPub);
72 $sharedSecret = EncryptionUtils::generateSharedSecret($serverPriv, $clientPub);
73
74 $salt = random_bytes(16);
75 $this->aesKey = EncryptionUtils::generateKey($sharedSecret, $salt);
76 $this->handshakeJwt = EncryptionUtils::generateServerHandshakeJwt($serverPriv, $salt);
77 }
78
79 public function onCompletion() : void{
84 $callback = $this->fetchLocal(self::TLS_KEY_ON_COMPLETION);
85 if($this->aesKey === null || $this->handshakeJwt === null){
86 throw new AssumptionFailedError("Something strange happened here ...");
87 }
88 $callback($this->aesKey, $this->handshakeJwt);
89 }
90}
__construct(private string $clientPub, \Closure $onCompletion)
storeLocal(string $key, mixed $complexData)
Definition: AsyncTask.php:214