aboutsummaryrefslogtreecommitdiffstats
path: root/include/crypto.php
diff options
context:
space:
mode:
authorMario Vavti <mario@mariovavti.com>2021-02-04 21:31:37 +0100
committerMario Vavti <mario@mariovavti.com>2021-02-04 21:31:37 +0100
commit6bb73e14b6521ea7c2d4f77d2954e4f8744bb736 (patch)
treea0c7c08652d1ff96224733efe13b3fbba89ad551 /include/crypto.php
parent41f84dabcc14415a47f24ed25a90495bbfcda52a (diff)
downloadvolse-hubzilla-6bb73e14b6521ea7c2d4f77d2954e4f8744bb736.tar.gz
volse-hubzilla-6bb73e14b6521ea7c2d4f77d2954e4f8744bb736.tar.bz2
volse-hubzilla-6bb73e14b6521ea7c2d4f77d2954e4f8744bb736.zip
key conversion functions cleanup
Diffstat (limited to 'include/crypto.php')
-rw-r--r--include/crypto.php146
1 files changed, 19 insertions, 127 deletions
diff --git a/include/crypto.php b/include/crypto.php
index f86d1153c..66bd113bc 100644
--- a/include/crypto.php
+++ b/include/crypto.php
@@ -3,9 +3,6 @@
use phpseclib\Crypt\RSA;
use phpseclib\Math\BigInteger;
-require_once('library/ASNValue.class.php');
-require_once('library/asn1.php');
-
function rsa_sign($data,$key,$alg = 'sha256') {
if(! $key)
return 'no key';
@@ -311,81 +308,6 @@ function new_keypair($bits) {
}
-function DerToPem($Der, $Private=false)
-{
- //Encode:
- $Der = base64_encode($Der);
- //Split lines:
- $lines = str_split($Der, 65);
- $body = implode("\n", $lines);
- //Get title:
- $title = $Private? 'RSA PRIVATE KEY' : 'PUBLIC KEY';
- //Add wrapping:
- $result = "-----BEGIN {$title}-----\n";
- $result .= $body . "\n";
- $result .= "-----END {$title}-----\n";
-
- return $result;
-}
-
-function DerToRsa($Der)
-{
- //Encode:
- $Der = base64_encode($Der);
- //Split lines:
- $lines = str_split($Der, 64);
- $body = implode("\n", $lines);
- //Get title:
- $title = 'RSA PUBLIC KEY';
- //Add wrapping:
- $result = "-----BEGIN {$title}-----\n";
- $result .= $body . "\n";
- $result .= "-----END {$title}-----\n";
-
- return $result;
-}
-
-
-function pkcs8_encode($Modulus,$PublicExponent) {
- //Encode key sequence
- $modulus = new ASNValue(ASNValue::TAG_INTEGER);
- $modulus->SetIntBuffer($Modulus);
- $publicExponent = new ASNValue(ASNValue::TAG_INTEGER);
- $publicExponent->SetIntBuffer($PublicExponent);
- $keySequenceItems = array($modulus, $publicExponent);
- $keySequence = new ASNValue(ASNValue::TAG_SEQUENCE);
- $keySequence->SetSequence($keySequenceItems);
- //Encode bit string
- $bitStringValue = $keySequence->Encode();
- $bitStringValue = chr(0x00) . $bitStringValue; //Add unused bits byte
- $bitString = new ASNValue(ASNValue::TAG_BITSTRING);
- $bitString->Value = $bitStringValue;
- //Encode body
- $bodyValue = "\x30\x0d\x06\x09\x2a\x86\x48\x86\xf7\x0d\x01\x01\x01\x05\x00" . $bitString->Encode();
- $body = new ASNValue(ASNValue::TAG_SEQUENCE);
- $body->Value = $bodyValue;
- //Get DER encoded public key:
- $PublicDER = $body->Encode();
- return $PublicDER;
-}
-
-
-function pkcs1_encode($Modulus,$PublicExponent) {
- //Encode key sequence
- $modulus = new ASNValue(ASNValue::TAG_INTEGER);
- $modulus->SetIntBuffer($Modulus);
- $publicExponent = new ASNValue(ASNValue::TAG_INTEGER);
- $publicExponent->SetIntBuffer($PublicExponent);
- $keySequenceItems = array($modulus, $publicExponent);
- $keySequence = new ASNValue(ASNValue::TAG_SEQUENCE);
- $keySequence->SetSequence($keySequenceItems);
- //Encode bit string
- $bitStringValue = $keySequence->Encode();
- return $bitStringValue;
-}
-
-
-// http://stackoverflow.com/questions/27568570/how-to-convert-raw-modulus-exponent-to-rsa-public-key-pem-format
/**
* @param string $m modulo
* @param string $e exponent
@@ -400,27 +322,12 @@ function metopem($m, $e) {
]);
return $rsa->getPublicKey();
-/* $der = pkcs8_encode($m,$e);
- $key = DerToPem($der,false);
- return $key;*/
}
-
-function pubrsatome($key,&$m,&$e) {
- require_once('library/asn1.php');
-
- $lines = explode("\n",$key);
- unset($lines[0]);
- unset($lines[count($lines)]);
- $x = base64_decode(implode('',$lines));
-
- $r = ASN_BASE::parseASNString($x);
-
- $m = base64url_decode($r[0]->asnData[0]->asnData);
- $e = base64url_decode($r[0]->asnData[1]->asnData);
-}
-
-
+/**
+ * @param string key
+ * @return string
+ */
function rsatopem($key) {
$rsa = new RSA();
@@ -428,22 +335,26 @@ function rsatopem($key) {
return $rsa->getPublicKey(RSA::PUBLIC_FORMAT_PKCS8);
-
-/* pubrsatome($key,$m,$e);
- return(metopem($m,$e));*/
}
+/**
+ * @param string key
+ * @return string
+ */
function pemtorsa($key) {
+
$rsa = new RSA();
$rsa->setPublicKey($key);
return $rsa->getPublicKey(RSA::PUBLIC_FORMAT_PKCS1);
-/* pemtome($key,$m,$e);
- return(metorsa($m,$e));*/
-
}
+/**
+ * @param string $key key
+ * @param string $m reference modulo
+ * @param string $e reference exponent
+ */
function pemtome($key,&$m,&$e) {
$rsa = new RSA();
@@ -453,27 +364,13 @@ function pemtome($key,&$m,&$e) {
$e = $rsa->modulus->toBytes();
$m = $rsa->exponent->toBytes();
-
-/* $rsa = new RSA();
- $rsa->loadKey($key);
- $rsa->setPublicKey();
-
- $modulus = $rsa->modulus->toBytes();
- $exponent = $rsa->exponent->toBytes();
-
-
-
- $lines = explode("\n",$key);
- unset($lines[0]);
- unset($lines[count($lines)]);
- $x = base64_decode(implode('',$lines));
-
- $r = ASN_BASE::parseASNString($x);
-
- $m = base64url_decode($r[0]->asnData[1]->asnData[0]->asnData[0]->asnData);
- $e = base64url_decode($r[0]->asnData[1]->asnData[0]->asnData[1]->asnData);*/
}
+/**
+ * @param string $m modulo
+ * @param string $e exponent
+ * @return string
+ */
function metorsa($m,$e) {
$rsa = new RSA();
@@ -483,19 +380,14 @@ function metorsa($m,$e) {
]);
return $rsa->getPublicKey(RSA::PUBLIC_FORMAT_PKCS1);
-/* $der = pkcs1_encode($m,$e);
- $key = DerToRsa($der);
- return $key;*/
}
-
function salmon_key($pubkey) {
pemtome($pubkey,$m,$e);
return 'RSA' . '.' . base64url_encode($m,true) . '.' . base64url_encode($e,true) ;
}
-
function convert_salmon_key($key) {
if(strstr($key,','))