2018-12-14 01:53:09 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Zotlabs\Lib;
|
|
|
|
|
2021-03-28 22:37:49 +00:00
|
|
|
use phpseclib\Crypt\RSA;
|
|
|
|
use phpseclib\Math\BigInteger;
|
2018-12-14 01:53:09 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Keyutils
|
|
|
|
* Convert RSA keys between various formats
|
|
|
|
*/
|
|
|
|
class Keyutils {
|
|
|
|
|
2021-03-28 22:37:49 +00:00
|
|
|
/**
|
|
|
|
* @param string $m modulo
|
|
|
|
* @param string $e exponent
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public static function meToPem($m, $e) {
|
2018-12-14 01:53:09 +00:00
|
|
|
|
2021-03-28 22:37:49 +00:00
|
|
|
$rsa = new RSA();
|
|
|
|
$rsa->loadKey([
|
|
|
|
'e' => new BigInteger($e, 256),
|
|
|
|
'n' => new BigInteger($m, 256)
|
|
|
|
]);
|
|
|
|
return $rsa->getPublicKey();
|
2018-12-14 01:53:09 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-03-28 22:37:49 +00:00
|
|
|
/**
|
|
|
|
* @param string key
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public static function rsaToPem($key) {
|
2018-12-14 01:53:09 +00:00
|
|
|
|
2021-03-28 22:37:49 +00:00
|
|
|
$rsa = new RSA();
|
|
|
|
$rsa->setPublicKey($key);
|
2018-12-14 01:53:09 +00:00
|
|
|
|
2021-03-28 22:37:49 +00:00
|
|
|
return $rsa->getPublicKey(RSA::PUBLIC_FORMAT_PKCS8);
|
2018-12-14 01:53:09 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-03-28 22:37:49 +00:00
|
|
|
/**
|
|
|
|
* @param string key
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public static function pemToRsa($key) {
|
2018-12-14 01:53:09 +00:00
|
|
|
|
2021-03-28 22:37:49 +00:00
|
|
|
$rsa = new RSA();
|
|
|
|
$rsa->setPublicKey($key);
|
2018-12-14 01:53:09 +00:00
|
|
|
|
2021-03-28 22:37:49 +00:00
|
|
|
return $rsa->getPublicKey(RSA::PUBLIC_FORMAT_PKCS1);
|
2018-12-14 01:53:09 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-03-28 22:37:49 +00:00
|
|
|
/**
|
|
|
|
* @param string $key key
|
|
|
|
* @param string $m reference modulo
|
|
|
|
* @param string $e reference exponent
|
|
|
|
*/
|
|
|
|
public static function pemToMe($key, &$m, &$e) {
|
2018-12-14 01:53:09 +00:00
|
|
|
|
2021-03-28 22:37:49 +00:00
|
|
|
$rsa = new RSA();
|
|
|
|
$rsa->loadKey($key);
|
|
|
|
$rsa->setPublicKey();
|
2018-12-14 01:53:09 +00:00
|
|
|
|
2021-03-28 22:37:49 +00:00
|
|
|
$m = $rsa->modulus->toBytes();
|
|
|
|
$e = $rsa->exponent->toBytes();
|
2018-12-14 01:53:09 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-03-28 22:37:49 +00:00
|
|
|
/**
|
|
|
|
* @param string $pubkey
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public static function salmonKey($pubkey) {
|
|
|
|
self::pemToMe($pubkey, $m, $e);
|
|
|
|
return 'RSA' . '.' . base64url_encode($m, true) . '.' . base64url_encode($e, true);
|
|
|
|
}
|
2018-12-14 01:53:09 +00:00
|
|
|
|
2021-03-28 22:37:49 +00:00
|
|
|
/**
|
|
|
|
* @param string $key
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public static function convertSalmonKey($key) {
|
|
|
|
if (strstr($key, ','))
|
|
|
|
$rawkey = substr($key, strpos($key, ',') + 1);
|
2018-12-14 01:53:09 +00:00
|
|
|
else
|
2021-03-28 22:37:49 +00:00
|
|
|
$rawkey = substr($key, 5);
|
2018-12-14 01:53:09 +00:00
|
|
|
|
2021-03-28 22:37:49 +00:00
|
|
|
$key_info = explode('.', $rawkey);
|
2018-12-14 01:53:09 +00:00
|
|
|
|
|
|
|
$m = base64url_decode($key_info[1]);
|
|
|
|
$e = base64url_decode($key_info[2]);
|
|
|
|
|
2021-03-28 22:37:49 +00:00
|
|
|
return self::meToPem($m, $e);
|
2018-12-14 01:53:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|