TYPO3 CMS  TYPO3_6-2
PhpBackend.php
Go to the documentation of this file.
1 <?php
3 
33  public function createNewKeyPair() {
35  $keyPair = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Rsaauth\\Keypair');
36  if ($keyPair->isReady()) {
37  return $keyPair;
38  }
39 
40  $privateKey = @openssl_pkey_new();
41  if ($privateKey !== FALSE) {
42  // Create private key as string
43  $privateKeyStr = '';
44  openssl_pkey_export($privateKey, $privateKeyStr);
45  // Prepare public key information
46  $exportedData = '';
47  $csr = openssl_csr_new(array(
48  'localityName' => 'foo',
49  'organizationName' => 'bar',
50  ), $privateKey);
51  openssl_csr_export($csr, $exportedData, FALSE);
52  // Get public key (in fact modulus) and exponent
53  $publicKey = $this->extractPublicKeyModulus($exportedData);
54  $exponent = $this->extractExponent($exportedData);
55 
56  $keyPair->setExponent($exponent);
57  $keyPair->setPrivateKey($privateKeyStr);
58  $keyPair->setPublicKey($publicKey);
59  // Clean up all resources
60  openssl_free_key($privateKey);
61  } else {
62  $keyPair = NULL;
63  }
64 
65  return $keyPair;
66  }
67 
77  public function decrypt($privateKey, $data) {
78  $result = '';
79  if (!@openssl_private_decrypt(base64_decode($data), $result, $privateKey)) {
80  $result = NULL;
81  }
82  return $result;
83  }
84 
92  public function isAvailable() {
93  $result = FALSE;
94  if (is_callable('openssl_pkey_new')) {
95  // PHP extension has to be configured properly. It
96  // can be installed and available but will not work unless
97  // properly configured. So we check if it works.
98  $testKey = @openssl_pkey_new();
99  if (is_resource($testKey)) {
100  openssl_free_key($testKey);
101  $result = TRUE;
102  }
103  }
104  return $result;
105  }
106 
113  protected function extractExponent($data) {
114  $index = strpos($data, 'Exponent: ');
115  // We do not check for '$index === FALSE' because the exponent is
116  // always there!
117  return (int)substr($data, $index + 10);
118  }
119 
126  protected function extractPublicKeyModulus($data) {
127  $fragment = preg_replace('/.*Modulus.*?\\n(.*)Exponent:.*/ms', '\\1', $data);
128  $fragment = preg_replace('/[\\s\\n\\r:]/', '', $fragment);
129  $result = trim(strtoupper(substr($fragment, 2)));
130  return $result;
131  }
132 
133 }
if($list_of_literals) if(!empty($literals)) if(!empty($literals)) $result
Analyse literals to prepend the N char to them if their contents aren&#39;t numeric.