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') {
11 if (version_compare(PHP_VERSION, '5.3.0', '>=') || $alg === 'sha1') {
12 openssl_sign($data,$sig,$key,(($alg == 'sha1') ? OPENSSL_ALGO_SHA1 : $alg));
15 if(strlen($key) < 1024 || extension_loaded('gmp')) {
16 require_once('library/phpsec/Crypt/RSA.php');
17 $rsa = new CRYPT_RSA();
18 $rsa->signatureMode = CRYPT_RSA_SIGNATURE_PKCS1;
21 $sig = $rsa->sign($data);
24 logger('rsa_sign: insecure algorithm used. Please upgrade PHP to 5.3');
25 openssl_private_encrypt(hex2bin('3031300d060960864801650304020105000420') . hash('sha256',$data,true), $sig, $key);
31 function rsa_verify($data,$sig,$key,$alg = 'sha256') {
33 if (version_compare(PHP_VERSION, '5.3.0', '>=') || $alg === 'sha1') {
34 $verify = openssl_verify($data,$sig,$key,(($alg == 'sha1') ? OPENSSL_ALGO_SHA1 : $alg));
37 if(strlen($key) <= 300 || extension_loaded('gmp')) {
38 require_once('library/phpsec/Crypt/RSA.php');
39 $rsa = new CRYPT_RSA();
40 $rsa->signatureMode = CRYPT_RSA_SIGNATURE_PKCS1;
43 $verify = $rsa->verify($data,$sig);
46 // fallback sha256 verify for PHP < 5.3 and large key lengths
48 openssl_public_decrypt($sig,$rawsig,$key);
49 $verify = (($rawsig && substr($rawsig,-32) === hash('sha256',$data,true)) ? true : false);
56 function DerToPem($Der, $Private=false)
59 $Der = base64_encode($Der);
61 $lines = str_split($Der, 65);
62 $body = implode("\n", $lines);
64 $title = $Private? 'RSA PRIVATE KEY' : 'PUBLIC KEY';
66 $result = "-----BEGIN {$title}-----\n";
67 $result .= $body . "\n";
68 $result .= "-----END {$title}-----\n";
73 function DerToRsa($Der)
76 $Der = base64_encode($Der);
78 $lines = str_split($Der, 64);
79 $body = implode("\n", $lines);
81 $title = 'RSA PUBLIC KEY';
83 $result = "-----BEGIN {$title}-----\n";
84 $result .= $body . "\n";
85 $result .= "-----END {$title}-----\n";
91 function pkcs8_encode($Modulus,$PublicExponent) {
93 $modulus = new ASNValue(ASNValue::TAG_INTEGER);
94 $modulus->SetIntBuffer($Modulus);
95 $publicExponent = new ASNValue(ASNValue::TAG_INTEGER);
96 $publicExponent->SetIntBuffer($PublicExponent);
97 $keySequenceItems = array($modulus, $publicExponent);
98 $keySequence = new ASNValue(ASNValue::TAG_SEQUENCE);
99 $keySequence->SetSequence($keySequenceItems);
101 $bitStringValue = $keySequence->Encode();
102 $bitStringValue = chr(0x00) . $bitStringValue; //Add unused bits byte
103 $bitString = new ASNValue(ASNValue::TAG_BITSTRING);
104 $bitString->Value = $bitStringValue;
106 $bodyValue = "\x30\x0d\x06\x09\x2a\x86\x48\x86\xf7\x0d\x01\x01\x01\x05\x00" . $bitString->Encode();
107 $body = new ASNValue(ASNValue::TAG_SEQUENCE);
108 $body->Value = $bodyValue;
109 //Get DER encoded public key:
110 $PublicDER = $body->Encode();
115 function pkcs1_encode($Modulus,$PublicExponent) {
116 //Encode key sequence
117 $modulus = new ASNValue(ASNValue::TAG_INTEGER);
118 $modulus->SetIntBuffer($Modulus);
119 $publicExponent = new ASNValue(ASNValue::TAG_INTEGER);
120 $publicExponent->SetIntBuffer($PublicExponent);
121 $keySequenceItems = array($modulus, $publicExponent);
122 $keySequence = new ASNValue(ASNValue::TAG_SEQUENCE);
123 $keySequence->SetSequence($keySequenceItems);
125 $bitStringValue = $keySequence->Encode();
126 return $bitStringValue;
130 function metopem($m,$e) {
131 $der = pkcs8_encode($m,$e);
132 $key = DerToPem($der,false);
137 function pubrsatome($key,&$m,&$e) {
138 require_once('library/asn1.php');
139 require_once('include/salmon.php');
141 $lines = explode("\n",$key);
143 unset($lines[count($lines)]);
144 $x = base64_decode(implode('',$lines));
146 $r = ASN_BASE::parseASNString($x);
148 $m = base64url_decode($r[0]->asnData[0]->asnData);
149 $e = base64url_decode($r[0]->asnData[1]->asnData);
153 function rsatopem($key) {
154 pubrsatome($key,$m,$e);
155 return(metopem($m,$e));
158 function pemtorsa($key) {
160 return(metorsa($m,$e));
163 function pemtome($key,&$m,&$e) {
164 require_once('include/salmon.php');
165 $lines = explode("\n",$key);
167 unset($lines[count($lines)]);
168 $x = base64_decode(implode('',$lines));
170 $r = ASN_BASE::parseASNString($x);
172 $m = base64url_decode($r[0]->asnData[1]->asnData[0]->asnData[0]->asnData);
173 $e = base64url_decode($r[0]->asnData[1]->asnData[0]->asnData[1]->asnData);
176 function metorsa($m,$e) {
177 $der = pkcs1_encode($m,$e);
178 $key = DerToRsa($der);
182 function salmon_key($pubkey) {
183 pemtome($pubkey,$m,$e);
184 return 'RSA' . '.' . base64url_encode($m,true) . '.' . base64url_encode($e,true) ;
189 if(! function_exists('aes_decrypt')) {
190 function aes_decrypt($val,$ky)
192 $key="\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0";
193 for($a=0;$a<strlen($ky);$a++)
194 $key[$a%16]=chr(ord($key[$a%16]) ^ ord($ky[$a]));
195 $mode = MCRYPT_MODE_ECB;
196 $enc = MCRYPT_RIJNDAEL_128;
197 $dec = @mcrypt_decrypt($enc, $key, $val, $mode, @mcrypt_create_iv( @mcrypt_get_iv_size($enc, $mode), MCRYPT_DEV_URANDOM ) );
198 return rtrim($dec,(( ord(substr($dec,strlen($dec)-1,1))>=0 and ord(substr($dec, strlen($dec)-1,1))<=16)? chr(ord( substr($dec,strlen($dec)-1,1))):null));
202 if(! function_exists('aes_encrypt')) {
203 function aes_encrypt($val,$ky)
205 $key="\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0";
206 for($a=0;$a<strlen($ky);$a++)
207 $key[$a%16]=chr(ord($key[$a%16]) ^ ord($ky[$a]));
208 $mode=MCRYPT_MODE_ECB;
209 $enc=MCRYPT_RIJNDAEL_128;
210 $val=str_pad($val, (16*(floor(strlen($val) / 16)+(strlen($val) % 16==0?2:1))), chr(16-(strlen($val) % 16)));
211 return mcrypt_encrypt($enc, $key, $val, $mode, mcrypt_create_iv( mcrypt_get_iv_size($enc, $mode), MCRYPT_DEV_URANDOM));
215 function pkcs5_pad ($text, $blocksize)
217 $pad = $blocksize - (strlen($text) % $blocksize);
218 return $text . str_repeat(chr($pad), $pad);
221 function pkcs5_unpad($text)
223 $pad = ord($text{strlen($text)-1});
224 if ($pad > strlen($text)) return false;
225 if (strspn($text, chr($pad), strlen($text) - $pad) != $pad) return false;
226 return substr($text, 0, -1 * $pad);
229 function AES256CBC_encrypt($data,$key,$iv) {
230 return mcrypt_encrypt(
232 str_pad($key,32,"\0"),
235 str_pad($iv,16,"\0"));
238 function AES256CBC_decrypt($data,$key,$iv) {
239 return pkcs5_unpad(mcrypt_decrypt(
241 str_pad($key,32,"\0"),
244 str_pad($iv,16,"\0")));
247 function aes_encapsulate($data,$pubkey) {
248 $key = random_string(32,RANDOM_STRING_TEXT);
249 $iv = random_string(16,RANDOM_STRING_TEXT);
250 $result['data'] = base64url_encode(AES256CBC_encrypt($data,$key,$iv),true);
251 openssl_public_encrypt($key,$k,$pubkey);
252 $result['key'] = base64url_encode($k,true);
253 openssl_public_encrypt($iv,$i,$pubkey);
254 $result['iv'] = base64url_encode($i,true);
258 function aes_unencapsulate($data,$prvkey) {
259 openssl_private_decrypt(base64url_decode($data['key']),$k,$prvkey);
260 openssl_private_decrypt(base64url_decode($data['iv']),$i,$prvkey);
261 return AES256CBC_decrypt(base64url_decode($data['data']),$k,$i);
265 // This has been superceded.
267 function zot_encapsulate($data,$envelope,$pubkey) {
268 $res = aes_encapsulate($data,$pubkey);
271 <?xml version='1.0' encoding='UTF-8'?>
272 <zot:msg xmlns:zot='http://purl.org/zot/1.0'>
273 <zot:key>{$res['key']}</zot:key>
274 <zot:iv>{$res['iv']}</zot:iv>
275 <zot:env>$s1</zot:env>
276 <zot:sig key_id="$keyid">$sig</zot:sig>
277 <zot:alg>AES-256-CBC</zot:alg>
278 <zot:data type='application/magic-envelope+xml'>{$res['data']}</zot:data>
286 function zot_unencapsulate($data,$prvkey) {
289 $x = parse_xml_string($data);
290 $c = array('key' => $x->key,'iv' => $x->iv,'data' => $x->data);
291 openssl_private_decrypt(base64url_decode($x->sender),$s,$prvkey);
293 $ret['data'] = aes_unencapsulate($x,$prvkey);