3 * @file src/Util/Crypto.php
5 namespace Friendica\Util;
7 use Friendica\Core\Config;
11 require_once 'library/ASNValue.class.php';
12 require_once 'library/asn1.php';
19 // supported algorithms are 'sha256', 'sha1'
21 * @param string $data data
22 * @param string $key key
23 * @param string $alg algorithm
26 public static function rsaSign($data, $key, $alg = 'sha256')
28 openssl_sign($data, $sig, $key, (($alg == 'sha1') ? OPENSSL_ALGO_SHA1 : $alg));
33 * @param string $data data
34 * @param string $sig signature
35 * @param string $key key
36 * @param string $alg algorithm
39 public static function rsaVerify($data, $sig, $key, $alg = 'sha256')
41 return openssl_verify($data, $sig, $key, (($alg == 'sha1') ? OPENSSL_ALGO_SHA1 : $alg));
45 * @param string $Der der formatted string
46 * @param string $Private key type optional, default false
49 private static function DerToPem($Der, $Private = false)
52 $Der = base64_encode($Der);
54 $lines = str_split($Der, 65);
55 $body = implode("\n", $lines);
57 $title = $Private ? 'RSA PRIVATE KEY' : 'PUBLIC KEY';
59 $result = "-----BEGIN {$title}-----\n";
60 $result .= $body . "\n";
61 $result .= "-----END {$title}-----\n";
67 * @param string $Der der formatted string
70 private static function DerToRsa($Der)
73 $Der = base64_encode($Der);
75 $lines = str_split($Der, 64);
76 $body = implode("\n", $lines);
78 $title = 'RSA PUBLIC KEY';
80 $result = "-----BEGIN {$title}-----\n";
81 $result .= $body . "\n";
82 $result .= "-----END {$title}-----\n";
88 * @param string $Modulus modulo
89 * @param string $PublicExponent exponent
92 private static function pkcs8Encode($Modulus, $PublicExponent)
95 $modulus = new ASNValue(ASNValue::TAG_INTEGER);
96 $modulus->SetIntBuffer($Modulus);
97 $publicExponent = new ASNValue(ASNValue::TAG_INTEGER);
98 $publicExponent->SetIntBuffer($PublicExponent);
99 $keySequenceItems = [$modulus, $publicExponent];
100 $keySequence = new ASNValue(ASNValue::TAG_SEQUENCE);
101 $keySequence->SetSequence($keySequenceItems);
103 $bitStringValue = $keySequence->Encode();
104 $bitStringValue = chr(0x00) . $bitStringValue; //Add unused bits byte
105 $bitString = new ASNValue(ASNValue::TAG_BITSTRING);
106 $bitString->Value = $bitStringValue;
108 $bodyValue = "\x30\x0d\x06\x09\x2a\x86\x48\x86\xf7\x0d\x01\x01\x01\x05\x00" . $bitString->Encode();
109 $body = new ASNValue(ASNValue::TAG_SEQUENCE);
110 $body->Value = $bodyValue;
111 //Get DER encoded public key:
112 $PublicDER = $body->Encode();
117 * @param string $Modulus modulo
118 * @param string $PublicExponent exponent
121 private static function pkcs1Encode($Modulus, $PublicExponent)
123 //Encode key sequence
124 $modulus = new ASNValue(ASNValue::TAG_INTEGER);
125 $modulus->SetIntBuffer($Modulus);
126 $publicExponent = new ASNValue(ASNValue::TAG_INTEGER);
127 $publicExponent->SetIntBuffer($PublicExponent);
128 $keySequenceItems = [$modulus, $publicExponent];
129 $keySequence = new ASNValue(ASNValue::TAG_SEQUENCE);
130 $keySequence->SetSequence($keySequenceItems);
132 $bitStringValue = $keySequence->Encode();
133 return $bitStringValue;
137 * @param string $m modulo
138 * @param string $e exponent
141 public static function meToPem($m, $e)
143 $der = self::pkcs8Encode($m, $e);
144 $key = self::DerToPem($der, false);
149 * @param string $key key
150 * @param string $m modulo reference
151 * @param object $e exponent reference
154 private static function pubRsaToMe($key, &$m, &$e)
156 $lines = explode("\n", $key);
158 unset($lines[count($lines)]);
159 $x = base64_decode(implode('', $lines));
161 $r = ASN_BASE::parseASNString($x);
163 $m = base64url_decode($r[0]->asnData[0]->asnData);
164 $e = base64url_decode($r[0]->asnData[1]->asnData);
168 * @param string $key key
171 public static function rsaToPem($key)
173 self::pubRsaToMe($key, $m, $e);
174 return self::meToPem($m, $e);
178 * @param string $key key
181 private static function pemToRsa($key)
183 self::pemToMe($key, $m, $e);
184 return self::meToRsa($m, $e);
188 * @param string $key key
189 * @param string $m modulo reference
190 * @param string $e exponent reference
193 public static function pemToMe($key, &$m, &$e)
195 $lines = explode("\n", $key);
197 unset($lines[count($lines)]);
198 $x = base64_decode(implode('', $lines));
200 $r = ASN_BASE::parseASNString($x);
202 $m = base64url_decode($r[0]->asnData[1]->asnData[0]->asnData[0]->asnData);
203 $e = base64url_decode($r[0]->asnData[1]->asnData[0]->asnData[1]->asnData);
207 * @param string $m modulo
208 * @param string $e exponent
211 private static function meToRsa($m, $e)
213 $der = self::pkcs1Encode($m, $e);
214 $key = self::DerToRsa($der);
219 * @param integer $bits number of bits
222 public static function newKeypair($bits)
225 'digest_alg' => 'sha1',
226 'private_key_bits' => $bits,
227 'encrypt_key' => false
230 $conf = Config::get('system', 'openssl_conf_file');
232 $openssl_options['config'] = $conf;
234 $result = openssl_pkey_new($openssl_options);
236 if (empty($result)) {
237 logger('new_keypair: failed');
242 $response = ['prvkey' => '', 'pubkey' => ''];
244 openssl_pkey_export($result, $response['prvkey']);
247 $pkey = openssl_pkey_get_details($result);
248 $response['pubkey'] = $pkey["key"];