PocketMine-MP 5.35.1 git-e32e836dad793a3a3c8ddd8927c00e112b1e576a
Loading...
Searching...
No Matches
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 //make sure the key is valid before we break the stack trace
55 //TODO: maybe in the future we should require OpenSSLAsymmetricKey here instead of string
56 JwtUtils::parseDerPublicKey($this->clientPub);
57 if(self::$SERVER_PRIVATE_KEY === null){
58 $serverPrivateKey = openssl_pkey_new(["ec" => ["curve_name" => "secp384r1"]]);
59 if($serverPrivateKey === false){
60 throw new \RuntimeException("openssl_pkey_new() failed: " . openssl_error_string());
61 }
62 self::$SERVER_PRIVATE_KEY = $serverPrivateKey;
63 }
64
65 $this->serverPrivateKey = igbinary_serialize(openssl_pkey_get_details(self::$SERVER_PRIVATE_KEY));
66 $this->storeLocal(self::TLS_KEY_ON_COMPLETION, $onCompletion);
67 }
68
69 public function onRun() : void{
71 $serverPrivDetails = igbinary_unserialize($this->serverPrivateKey);
72 $serverPriv = openssl_pkey_new($serverPrivDetails);
73 if($serverPriv === false) throw new AssumptionFailedError("Failed to restore server signing key from details");
74 $clientPub = JwtUtils::parseDerPublicKey($this->clientPub);
75 $sharedSecret = EncryptionUtils::generateSharedSecret($serverPriv, $clientPub);
76
77 $salt = random_bytes(16);
78 $this->aesKey = EncryptionUtils::generateKey($sharedSecret, $salt);
79 $this->handshakeJwt = EncryptionUtils::generateServerHandshakeJwt($serverPriv, $salt);
80 }
81
82 public function onCompletion() : void{
87 $callback = $this->fetchLocal(self::TLS_KEY_ON_COMPLETION);
88 if($this->aesKey === null || $this->handshakeJwt === null){
89 throw new AssumptionFailedError("Something strange happened here ...");
90 }
91 $callback($this->aesKey, $this->handshakeJwt);
92 }
93}
__construct(private string $clientPub, \Closure $onCompletion)
storeLocal(string $key, mixed $complexData)