9 parent::SetKeys($arKeys);
10 $this->_PRIV = $arKeys[
"PRIV"];
15 $arKeys = unserialize(COption::GetOptionString(
"main",
"~rsa_keys_openssl",
""), [
'allowed_classes' =>
false]);
16 if(!is_array($arKeys))
18 $arKeys[
"PRIV"] = COption::GetOptionString(
"main",
"~rsa_key_pem",
"");
24 $privKey = $arKeys[
"PRIV"];
25 unset($arKeys[
"PRIV"]);
26 COption::SetOptionString(
"main",
"~rsa_keys_openssl", serialize($arKeys));
27 COption::SetOptionString(
"main",
"~rsa_key_pem", $privKey);
32 $key = openssl_pkey_get_private($this->_PRIV);
35 $blocks = explode(
' ',
$data);
36 foreach($blocks as $block)
39 openssl_private_decrypt(strrev(base64_decode($block)), $out1,
$key, OPENSSL_NO_PADDING);
40 $out1 = strrev($out1);
48 public function Keygen($keylen=
false)
53 $keylen = intval($keylen);
55 $fname =
$_SERVER[
"DOCUMENT_ROOT"].
"/bitrix/tmp/openssl.cnf";
56 if(!file_exists($fname))
59 file_put_contents($fname,
'');
62 $keys = openssl_pkey_new(
array(
63 "private_key_type"=>OPENSSL_KEYTYPE_RSA,
64 "private_key_bits"=>$keylen,
70 openssl_pkey_export($keys, $privkey,
null,
array(
"config" => $fname));
71 $k = self::get_openssl_key_details($privkey);
76 "M" => base64_encode(strrev(
$k[
'n'])),
77 "E" => base64_encode(strrev(
$k[
'e'])),
78 "D" => base64_encode(strrev(
$k[
'd'])),
87 private static function get_openssl_key_details(
$key)
90 $lines = explode(
"\n", trim(
$key));
91 unset($lines[
count($lines)-1]);
93 $der = implode(
'', $lines);
94 $der = base64_decode($der);
99 $bodyItems = $body->GetSequence();
101 if(!empty($bodyItems))
103 if(is_object($bodyItems[1]) && is_object($bodyItems[2]) && is_object($bodyItems[3] ??
null))
105 $n = $bodyItems[1]->GetValue();
106 $e = $bodyItems[2]->GetValue();
107 $d = $bodyItems[3]->GetValue();
109 return array(
"n"=>
$n,
"e"=>$e,
"d"=>$d);
111 elseif(is_object($bodyItems[2]))
113 $body =
new CASNReader();
114 $body->Read($bodyItems[2]->GetValue());
115 $bodyItems = $body->GetSequence();
117 if(is_object($bodyItems[1]) && is_object($bodyItems[2]) && is_object($bodyItems[3]))
119 $n = $bodyItems[1]->GetValue();
120 $e = $bodyItems[2]->GetValue();
121 $d = $bodyItems[3]->GetValue();
123 return array(
"n"=>
$n,
"e"=>$e,
"d"=>$d);
if( $daysToExpire >=0 &&$daysToExpire< 60 elseif)( $daysToExpire< 0)