/** * Verifies an id token and returns the authenticated apiLoginTicket. * Throws an exception if the id token is not valid. * The audience parameter can be used to control which id tokens are * accepted. By default, the id token must have been issued to this OAuth2 client. * * @param $audience * @return array the token payload, if successful */ public function verifyIdToken($idToken, $audience = null) { if (empty($idToken)) { throw new LogicException('id_token cannot be null'); } // Check signature $certs = $this->getFederatedSignOnCerts(); foreach ($certs as $cert) { $modulus = new BigInteger($this->jwt->urlsafeB64Decode($cert['n']), 256); $exponent = new BigInteger($this->jwt->urlsafeB64Decode($cert['e']), 256); $rsa = new RSA(); $rsa->loadKey(array('n' => $modulus, 'e' => $exponent)); try { $payload = $this->jwt->decode($idToken, $rsa->getPublicKey(), array('RS256')); if (property_exists($payload, 'aud')) { if ($audience && $payload->aud != $audience) { return false; } } // support HTTP and HTTPS issuers // @see https://developers.google.com/identity/sign-in/web/backend-auth $issuers = array(self::OAUTH2_ISSUER, self::OAUTH2_ISSUER_HTTPS); if (!isset($payload->iss) || !in_array($payload->iss, $issuers)) { return false; } return (array) $payload; } catch (ExpiredException $e) { return false; } catch (DomainException $e) { // continue } } return false; }
public function testEncryptionModeNone() { $plaintext = 'a'; $rsa = new RSA(); $privatekey = '-----BEGIN RSA PRIVATE KEY----- MIICXAIBAAKBgQCqGKukO1De7zhZj6+H0qtjTkVxwTCpvKe4eCZ0FPqri0cb2JZfXJ/DgYSF6vUp wmJG8wVQZKjeGcjDOL5UlsuusFncCzWBQ7RKNUSesmQRMSGkVb1/3j+skZ6UtW+5u09lHNsj6tQ5 1s1SPrCBkedbNf0Tp0GbMJDyR4e9T04ZZwIDAQABAoGAFijko56+qGyN8M0RVyaRAXz++xTqHBLh 3tx4VgMtrQ+WEgCjhoTwo23KMBAuJGSYnRmoBZM3lMfTKevIkAidPExvYCdm5dYq3XToLkkLv5L2 pIIVOFMDG+KESnAFV7l2c+cnzRMW0+b6f8mR1CJzZuxVLL6Q02fvLi55/mbSYxECQQDeAw6fiIQX GukBI4eMZZt4nscy2o12KyYner3VpoeE+Np2q+Z3pvAMd/aNzQ/W9WaI+NRfcxUJrmfPwIGm63il AkEAxCL5HQb2bQr4ByorcMWm/hEP2MZzROV73yF41hPsRC9m66KrheO9HPTJuo3/9s5p+sqGxOlF L0NDt4SkosjgGwJAFklyR1uZ/wPJjj611cdBcztlPdqoxssQGnh85BzCj/u3WqBpE2vjvyyvyI5k X6zk7S0ljKtt2jny2+00VsBerQJBAJGC1Mg5Oydo5NwD6BiROrPxGo2bpTbu/fhrT8ebHkTz2epl U9VQQSQzY1oZMVX8i1m5WUTLPz2yLJIBQVdXqhMCQBGoiuSoSjafUhV7i1cEGpb88h5NBYZzWXGZ 37sJ5QsW+sJyoNde3xH8vdXhzU7eT82D6X/scw9RZz+/6rCJ4p0= -----END RSA PRIVATE KEY-----'; $rsa->loadKey($privatekey); $rsa->loadKey($rsa->getPublicKey()); $rsa->setEncryptionMode(RSA::ENCRYPTION_NONE); $expected = '105b92f59a87a8ad4da52c128b8c99491790ef5a54770119e0819060032fb9e772ed6772828329567f3d7e9472154c1530f8156ba7fd732f52ca1c06' . '5a3f5ed8a96c442e4662e0464c97f133aed31262170201993085a589565d67cc9e727e0d087e3b225c8965203b271e38a499c92fc0d6502297eca712' . '4d04bd467f6f1e7c'; $expected = pack('H*', $expected); $result = $rsa->encrypt($plaintext); $this->assertEquals($result, $expected); $rsa->loadKey($privatekey); $this->assertEquals(trim($rsa->decrypt($result), ""), $plaintext); }
/** * @Route("/asd", name="homepage") */ public function indexAction(Request $request) { $request = $this->get('request'); $defaultData = array('name' => 'Type your file name here'); $form = $this->createFormBuilder($defaultData)->add('name', 'text')->add('file', 'file', array('mapped' => false))->add('submit', 'submit')->getForm(); if ($request->getMethod() == 'POST') { $form->handleRequest($this->get('request')); if ($form->isValid()) { // perform some action, such as saving the task to the database $data = $form->getData(); if ($form['file']->getData()) { $filename = $form['file']->getData()->getClientOriginalName(); $uploadDir = dirname($this->container->getParameter('kernel.root_dir')) . '/web/bundles/framework/upload'; $form['file']->getData()->move($uploadDir, $filename); $link = '/web/bundles/framework/upload' . '/' . $filename; } } $inputFile = $request->files->get('cache.xml'); return $this->render('default/index.html.twig', array('cipher' => "", 'plain' => "", 'rsacipher' => "", 'rsaplain' => "", 'rsapk' => "", 'form' => $form->createView(), 'link' => $link)); } else { $des = new DES(); echo gettype($des); $des->setKey('This is my secret key'); $plaintext = 'asda sda sdas dasd asdasdada sd'; $cipher = $des->encrypt($plaintext); $plain = $des->decrypt($cipher); $rsa = new RSA(); $rsa->createKey(1024); $rsaplain = "encrypt using RSA"; $key = $rsa->createKey(1024); $rsa->loadKey($key['publickey']); $rsacipher = $rsa->encrypt($rsaplain); $rsa->loadKey($key['privatekey']); $rsadec = $rsa->decrypt($rsacipher); // replace this example code with whatever you need return $this->render('default/index.html.twig', array('base_dir' => realpath($this->container->getParameter('kernel.root_dir') . '/..'), 'cipher' => $cipher, 'plain' => $plain, 'rsacipher' => $key['publickey'], 'rsaplain' => $key['privatekey'], 'rsapk' => $rsa->getPublicKey(), 'form' => $form->createView())); } }
/** * @group github705 */ public function testSaveNullRSAParam() { $privKey = new RSA(); $privKey->loadKey('-----BEGIN RSA PRIVATE KEY----- MIICXQIBAAKBgQDMswfEpAgnUDWA74zZw5XcPsWh1ly1Vk99tsqwoFDkLF7jvXy1 dDLHYfuquvfxCgcp8k/4fQhx4ubR8bbGgEq9B05YRnViK0R0iBB5Ui4IaxWYYhKE 8xqAEH2fL+/7nsqqNFKkEN9KeFwc7WbMY49U2adlMrpBdRjk1DqIEW3QTwIDAQAB AoGBAJ+83cT/1DUJjJcPWLTeweVbPtJp+3Ku5d1OdaGbmURVs764scbP5Ihe2AuF V9LLZoe/RdS9jYeB72nJ3D3PA4JVYYgqMOnJ8nlUMNQ+p0yGl5TqQk6EKLI8MbX5 kQEazNqFXsiWVQXubAd5wjtb6g0n0KD3zoT/pWLES7dtUFexAkEA89h5+vbIIl2P H/NnkPie2NWYDZ1YiMGHFYxPDwsd9KCZMSbrLwAhPg9bPgqIeVNfpwxrzeksS6D9 P98tJt335QJBANbnCe+LhDSrkpHMy9aOG2IdbLGG63MSRUCPz8v2gKPq3kYXDxq6 Y1iqF8N5g0k5iirHD2qlWV5Q+nuGvFTafCMCQQC1wQiC0IkyXEw/Q31RqI82Dlcs 5rhEDwQyQof3LZEhcsdcxKaOPOmKSYX4A3/f9w4YBIEiVQfoQ1Ig1qfgDZklAkAT TQDJcOBY0qgBTEFqbazr7PScJR/0X8m0eLYS/XqkPi3kYaHLpr3RcsVbmwg9hVtx aBtsWpliLSex/HHhtRW9AkBGcq67zKmEpJ9kXcYLEjJii3flFS+Ct/rNm+Hhm1l7 4vca9v/F2hGVJuHIMJ8mguwYlNYzh2NqoIDJTtgOkBmt -----END RSA PRIVATE KEY-----'); $pubKey = new RSA(); $pubKey->loadKey($privKey->getPublicKey()); $pubKey->setPublicKey(); $subject = new X509(); $subject->setDNProp('id-at-organizationName', 'phpseclib demo cert'); $subject->setPublicKey($pubKey); $issuer = new X509(); $issuer->setPrivateKey($privKey); $issuer->setDN($subject->getDN()); $x509 = new X509(); $result = $x509->sign($issuer, $subject); $cert = $x509->saveX509($result); $cert = $x509->loadX509($cert); $this->assertArrayHasKey('parameters', $cert['tbsCertificate']['subjectPublicKeyInfo']['algorithm']); $this->assertArrayHasKey('parameters', $cert['signatureAlgorithm']); $this->assertArrayHasKey('parameters', $cert['tbsCertificate']['signature']); }
/** * Get Public Key * * Wrapper for $this->key->getPublicKey() * * @param int $format optional * @return mixed * @access public */ function getPublicKey($format = null) { return !isset($format) ? $this->key->getPublicKey() : $this->key->getPublicKey($format); }
/** * Get Public Key * * Wrapper for $this->key->getPublicKey() * * @param int $type optional * @return mixed * @access public */ function getPublicKey($type = 'PKCS8') { return $this->key->getPublicKey($type); }
/** * @group github468 */ public function testSignedPKCS1() { $rsa = new RSA(); $key = '-----BEGIN PUBLIC KEY----- MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC/k7FwSDE9R9rvTU2nGdJwKaVG RvBIYGJNahseQhZkQH4CVFMdpWhmD8PyXpjNHtV1CJ0bqAX6e5QyNjvl0FeBj9dz JWrQdxx/WNN+ABG426rgYYbeGcIlWLZCw6Bx/1HtN5ef6nVEoiGNChYKIRB4QFOi 01smFxps1w8ZIQnD6wIDAQAB -----END PUBLIC KEY-----'; $rsa->loadKey($key); $rsa->setPublicKey(); $newkey = $rsa->getPublicKey(); $this->assertSame(preg_replace('#\\s#', '', $key), preg_replace('#\\s#', '', $newkey)); }
function createRsaKey($id, $pw, $sessionKey, $keyName, $eValue, $nValue) { $rsa = new RSA(); $n = $eValue; // naver~trick $e = $nValue; // switch~them $rsa->modulus = new BigInteger($n, 16); $rsa->publicExponent = new BigInteger($e, 16); $key = $rsa->getPublicKey(); $rsa->loadKey($key); $rsa->setEncryptionMode(CRYPT_RSA_ENCRYPTION_PKCS1); $comVal = $this->getLenChar($sessionKey) + $sessionKey + $this->getLenChar($id) + $id; return bin2hex($rsa->encrypt($comVal + $this->getLenChar($pw) + $pw)); }
public function testPrivateMSBlob() { $key = 'BwIAAACkAABSU0EyAAQAAAEAAQAnh6FFs6kYe/gmb9dzqsQKmtjFE9mxNAe9mEU3OwOEEfyI' . 'wkAx0/8dwh12fuP4wzNbdZAq4mmqCE6Lo8wTNNIJVNYEhKq5chHg1+hPDgfETFgtEO54JZSg' . '3cBZWEV/Tq3LHEX8CaLvHZxMEfFXbTfliFYMLoJ+YK1mpg9GYcmbrVmMAKSoOgETkkiJJzYm' . 'XftO3KOveBtvkAzjHxxSS1yP/Ba10BzeIleH96SbTuQtQRLXwRykdX9uazK+YsiSud9/PyLb' . 'gy5TI+o28OHq5P+0y5+a9IaAQ/92UwlrkHUYfhN/xTVlUIxKlTEdUQTIf+iHif8d4ABb3OdY' . 'JXZOW6fGeUP10jMyvbnrEoPDsYy9qfNk++0/8UP2NeO1IATszuZYg1nEXOW/5jmUxMCdiFyd' . 'p9ES211kpEZ4XcvjGaDlaQ+bLWj05i2m/9aHYcBrfcxxvlMa/9ZvrX4DfPWeydUDDDQ4+ntp' . 'T50BunSvmyf7cUk76Bf2sPgLXUQFoufEQ5g1Qo/v1uyhWBJzh6OSUO/DDXN/s8ec/tN05RQQ' . 'FZQ0na+v0hOCrV9IuRqtBuj4WAj1I/A1JjwyyP9Y/6yWFPM6EcS/6lyPy30lJPoULh7G29zk' . 'n7NVdTEkDtthdDjtX7Qhgd9qWvm5ADlmnvsS9A5m7ToOgQyOxtJoSlLitLbf/09LRycl/cdI' . 'zoMOCEdPe3DQcyEKqUPsghAq+DKw3uZpXwHzwTdfqlHSWAnHDggFKV1HZuWc1c4rV4k4b513TqE='; $plaintext = 'zzz'; $privKey = new RSA(); $privKey->load($key); $this->assertSame($privKey->getLoadedFormat(), 'MSBLOB'); $this->assertGreaterThanOrEqual(1, strlen("{$privKey}")); $pubKey = new RSA(); $pubKey->load($privKey->getPublicKey('msblob')); $this->assertGreaterThanOrEqual(1, strlen("{$pubKey}")); $ciphertext = $pubKey->encrypt($plaintext); $this->assertSame($privKey->decrypt($ciphertext), $plaintext); }