3 require_once 'library/ASNValue.class.php';
4 require_once 'library/asn1.php';
6 // supported algorithms are 'sha256', 'sha1'
8 function rsa_sign($data, $key, $alg = 'sha256') {
9 openssl_sign($data, $sig, $key, (($alg == 'sha1') ? OPENSSL_ALGO_SHA1 : $alg));
13 function rsa_verify($data, $sig, $key, $alg = 'sha256') {
14 return openssl_verify($data, $sig, $key, (($alg == 'sha1') ? OPENSSL_ALGO_SHA1 : $alg));
17 function DerToPem($Der, $Private = false) {
19 $Der = base64_encode($Der);
21 $lines = str_split($Der, 65);
22 $body = implode("\n", $lines);
24 $title = $Private ? 'RSA PRIVATE KEY' : 'PUBLIC KEY';
26 $result = "-----BEGIN {$title}-----\n";
27 $result .= $body . "\n";
28 $result .= "-----END {$title}-----\n";
33 function DerToRsa($Der) {
35 $Der = base64_encode($Der);
37 $lines = str_split($Der, 64);
38 $body = implode("\n", $lines);
40 $title = 'RSA PUBLIC KEY';
42 $result = "-----BEGIN {$title}-----\n";
43 $result .= $body . "\n";
44 $result .= "-----END {$title}-----\n";
49 function pkcs8_encode($Modulus, $PublicExponent) {
51 $modulus = new ASNValue(ASNValue::TAG_INTEGER);
52 $modulus->SetIntBuffer($Modulus);
53 $publicExponent = new ASNValue(ASNValue::TAG_INTEGER);
54 $publicExponent->SetIntBuffer($PublicExponent);
55 $keySequenceItems = array($modulus, $publicExponent);
56 $keySequence = new ASNValue(ASNValue::TAG_SEQUENCE);
57 $keySequence->SetSequence($keySequenceItems);
59 $bitStringValue = $keySequence->Encode();
60 $bitStringValue = chr(0x00) . $bitStringValue; //Add unused bits byte
61 $bitString = new ASNValue(ASNValue::TAG_BITSTRING);
62 $bitString->Value = $bitStringValue;
64 $bodyValue = "\x30\x0d\x06\x09\x2a\x86\x48\x86\xf7\x0d\x01\x01\x01\x05\x00" . $bitString->Encode();
65 $body = new ASNValue(ASNValue::TAG_SEQUENCE);
66 $body->Value = $bodyValue;
67 //Get DER encoded public key:
68 $PublicDER = $body->Encode();
72 function pkcs1_encode($Modulus, $PublicExponent) {
74 $modulus = new ASNValue(ASNValue::TAG_INTEGER);
75 $modulus->SetIntBuffer($Modulus);
76 $publicExponent = new ASNValue(ASNValue::TAG_INTEGER);
77 $publicExponent->SetIntBuffer($PublicExponent);
78 $keySequenceItems = array($modulus, $publicExponent);
79 $keySequence = new ASNValue(ASNValue::TAG_SEQUENCE);
80 $keySequence->SetSequence($keySequenceItems);
82 $bitStringValue = $keySequence->Encode();
83 return $bitStringValue;
86 function metopem($m, $e) {
87 $der = pkcs8_encode($m, $e);
88 $key = DerToPem($der, false);
92 function pubrsatome($key,&$m,&$e) {
93 require_once('library/asn1.php');
94 require_once('include/salmon.php');
96 $lines = explode("\n", $key);
98 unset($lines[count($lines)]);
99 $x = base64_decode(implode('', $lines));
101 $r = ASN_BASE::parseASNString($x);
103 $m = base64url_decode($r[0]->asnData[0]->asnData);
104 $e = base64url_decode($r[0]->asnData[1]->asnData);
108 function rsatopem($key) {
109 pubrsatome($key, $m, $e);
110 return metopem($m, $e);
113 function pemtorsa($key) {
114 pemtome($key, $m, $e);
115 return metorsa($m, $e);
118 function pemtome($key, &$m, &$e) {
119 require_once('include/salmon.php');
120 $lines = explode("\n", $key);
122 unset($lines[count($lines)]);
123 $x = base64_decode(implode('', $lines));
125 $r = ASN_BASE::parseASNString($x);
127 $m = base64url_decode($r[0]->asnData[1]->asnData[0]->asnData[0]->asnData);
128 $e = base64url_decode($r[0]->asnData[1]->asnData[0]->asnData[1]->asnData);
131 function metorsa($m, $e) {
132 $der = pkcs1_encode($m, $e);
133 $key = DerToRsa($der);
137 function salmon_key($pubkey) {
138 pemtome($pubkey, $m, $e);
139 return 'RSA' . '.' . base64url_encode($m, true) . '.' . base64url_encode($e, true) ;
142 function new_keypair($bits) {
143 $openssl_options = array(
144 'digest_alg' => 'sha1',
145 'private_key_bits' => $bits,
146 'encrypt_key' => false
149 $conf = get_config('system', 'openssl_conf_file');
151 $openssl_options['config'] = $conf;
153 $result = openssl_pkey_new($openssl_options);
155 if (empty($result)) {
156 logger('new_keypair: failed');
161 $response = array('prvkey' => '', 'pubkey' => '');
163 openssl_pkey_export($result, $response['prvkey']);
166 $pkey = openssl_pkey_get_details($result);
167 $response['pubkey'] = $pkey["key"];