50 const AES128_CBC =
'http://www.w3.org/2001/04/xmlenc#aes128-cbc';
51 const AES192_CBC =
'http://www.w3.org/2001/04/xmlenc#aes192-cbc';
52 const AES256_CBC =
'http://www.w3.org/2001/04/xmlenc#aes256-cbc';
53 const RSA_1_5 =
'http://www.w3.org/2001/04/xmlenc#rsa-1_5';
55 const DSA_SHA1 =
'http://www.w3.org/2000/09/xmldsig#dsa-sha1';
56 const RSA_SHA1 =
'http://www.w3.org/2000/09/xmldsig#rsa-sha1';
57 const RSA_SHA256 =
'http://www.w3.org/2001/04/xmldsig-more#rsa-sha256';
58 const RSA_SHA384 =
'http://www.w3.org/2001/04/xmldsig-more#rsa-sha384';
59 const RSA_SHA512 =
'http://www.w3.org/2001/04/xmldsig-more#rsa-sha512';
60 const HMAC_SHA1 =
'http://www.w3.org/2000/09/xmldsig#hmac-sha1';
113 case (self::TRIPLEDES_CBC):
114 $this->cryptParams[
'library'] =
'openssl';
115 $this->cryptParams[
'cipher'] =
'des-ede3-cbc';
116 $this->cryptParams[
'type'] =
'symmetric';
117 $this->cryptParams[
'method'] =
'http://www.w3.org/2001/04/xmlenc#tripledes-cbc';
118 $this->cryptParams[
'keysize'] = 24;
119 $this->cryptParams[
'blocksize'] = 8;
121 case (self::AES128_CBC):
122 $this->cryptParams[
'library'] =
'openssl';
123 $this->cryptParams[
'cipher'] =
'aes-128-cbc';
124 $this->cryptParams[
'type'] =
'symmetric';
125 $this->cryptParams[
'method'] =
'http://www.w3.org/2001/04/xmlenc#aes128-cbc';
126 $this->cryptParams[
'keysize'] = 16;
127 $this->cryptParams[
'blocksize'] = 16;
129 case (self::AES192_CBC):
130 $this->cryptParams[
'library'] =
'openssl';
131 $this->cryptParams[
'cipher'] =
'aes-192-cbc';
132 $this->cryptParams[
'type'] =
'symmetric';
133 $this->cryptParams[
'method'] =
'http://www.w3.org/2001/04/xmlenc#aes192-cbc';
134 $this->cryptParams[
'keysize'] = 24;
135 $this->cryptParams[
'blocksize'] = 16;
137 case (self::AES256_CBC):
138 $this->cryptParams[
'library'] =
'openssl';
139 $this->cryptParams[
'cipher'] =
'aes-256-cbc';
140 $this->cryptParams[
'type'] =
'symmetric';
141 $this->cryptParams[
'method'] =
'http://www.w3.org/2001/04/xmlenc#aes256-cbc';
142 $this->cryptParams[
'keysize'] = 32;
143 $this->cryptParams[
'blocksize'] = 16;
145 case (self::RSA_1_5):
146 $this->cryptParams[
'library'] =
'openssl';
147 $this->cryptParams[
'padding'] = OPENSSL_PKCS1_PADDING;
148 $this->cryptParams[
'method'] =
'http://www.w3.org/2001/04/xmlenc#rsa-1_5';
150 if (
$params[
'type'] ==
'public' ||
$params[
'type'] ==
'private') {
151 $this->cryptParams[
'type'] =
$params[
'type'];
155 throw new Exception(
'Certificate "type" (private/public) must be passed via parameters');
156 case (self::RSA_OAEP_MGF1P):
157 $this->cryptParams[
'library'] =
'openssl';
158 $this->cryptParams[
'padding'] = OPENSSL_PKCS1_OAEP_PADDING;
159 $this->cryptParams[
'method'] =
'http://www.w3.org/2001/04/xmlenc#rsa-oaep-mgf1p';
160 $this->cryptParams[
'hash'] = null;
162 if (
$params[
'type'] ==
'public' ||
$params[
'type'] ==
'private') {
163 $this->cryptParams[
'type'] =
$params[
'type'];
167 throw new Exception(
'Certificate "type" (private/public) must be passed via parameters');
168 case (self::RSA_SHA1):
169 $this->cryptParams[
'library'] =
'openssl';
170 $this->cryptParams[
'method'] =
'http://www.w3.org/2000/09/xmldsig#rsa-sha1';
171 $this->cryptParams[
'padding'] = OPENSSL_PKCS1_PADDING;
173 if (
$params[
'type'] ==
'public' ||
$params[
'type'] ==
'private') {
174 $this->cryptParams[
'type'] =
$params[
'type'];
178 throw new Exception(
'Certificate "type" (private/public) must be passed via parameters');
179 case (self::RSA_SHA256):
180 $this->cryptParams[
'library'] =
'openssl';
181 $this->cryptParams[
'method'] =
'http://www.w3.org/2001/04/xmldsig-more#rsa-sha256';
182 $this->cryptParams[
'padding'] = OPENSSL_PKCS1_PADDING;
183 $this->cryptParams[
'digest'] =
'SHA256';
185 if (
$params[
'type'] ==
'public' ||
$params[
'type'] ==
'private') {
186 $this->cryptParams[
'type'] =
$params[
'type'];
190 throw new Exception(
'Certificate "type" (private/public) must be passed via parameters');
191 case (self::RSA_SHA384):
192 $this->cryptParams[
'library'] =
'openssl';
193 $this->cryptParams[
'method'] =
'http://www.w3.org/2001/04/xmldsig-more#rsa-sha384';
194 $this->cryptParams[
'padding'] = OPENSSL_PKCS1_PADDING;
195 $this->cryptParams[
'digest'] =
'SHA384';
197 if (
$params[
'type'] ==
'public' ||
$params[
'type'] ==
'private') {
198 $this->cryptParams[
'type'] =
$params[
'type'];
202 throw new Exception(
'Certificate "type" (private/public) must be passed via parameters');
203 case (self::RSA_SHA512):
204 $this->cryptParams[
'library'] =
'openssl';
205 $this->cryptParams[
'method'] =
'http://www.w3.org/2001/04/xmldsig-more#rsa-sha512';
206 $this->cryptParams[
'padding'] = OPENSSL_PKCS1_PADDING;
207 $this->cryptParams[
'digest'] =
'SHA512';
209 if (
$params[
'type'] ==
'public' ||
$params[
'type'] ==
'private') {
210 $this->cryptParams[
'type'] =
$params[
'type'];
214 throw new Exception(
'Certificate "type" (private/public) must be passed via parameters');
215 case (self::HMAC_SHA1):
216 $this->cryptParams[
'library'] =
$type;
217 $this->cryptParams[
'method'] =
'http://www.w3.org/2000/09/xmldsig#hmac-sha1';
235 if (! isset($this->cryptParams[
'keysize'])) {
238 return $this->cryptParams[
'keysize'];
249 if (!isset($this->cryptParams[
'keysize'])) {
250 throw new Exception(
'Unknown key size for type "' . $this->type .
'".');
252 $keysize = $this->cryptParams[
'keysize'];
254 $key = openssl_random_pseudo_bytes($keysize);
256 if ($this->type === self::TRIPLEDES_CBC) {
261 $byte = ord(
$key[
$i]) & 0xfe;
263 for ($j = 1; $j < 8; $j++) {
264 $parity ^= ($byte >> $j) & 1;
284 $arCert = explode(
"\n", $cert);
288 foreach ($arCert AS $curData) {
290 if (strncmp($curData,
'-----BEGIN CERTIFICATE', 22) == 0) {
294 if (strncmp($curData,
'-----END CERTIFICATE', 20) == 0) {
297 $data .= trim($curData);
301 if (! empty(
$data)) {
302 return strtolower(sha1(base64_decode(
$data)));
319 $this->key = file_get_contents(
$key);
324 $this->key = openssl_x509_read($this->key);
325 openssl_x509_export($this->key, $str_cert);
326 $this->x509Certificate = $str_cert;
327 $this->key = $str_cert;
329 $this->x509Certificate = null;
331 if ($this->cryptParams[
'library'] ==
'openssl') {
332 switch ($this->cryptParams[
'type']) {
336 $this->X509Thumbprint = self::getRawThumbprint($this->key);
338 $this->key = openssl_get_publickey($this->key);
340 throw new Exception(
'Unable to extract public key');
345 $this->key = openssl_get_privatekey($this->key, $this->passphrase);
349 if (strlen($this->key) < $this->cryptParams[
'keysize']) {
350 throw new Exception(
'Key must contain at least 25 characters for this cipher');
370 if ($blockSize > 256) {
371 throw new Exception(
'Block size higher than 256 not allowed');
373 $padChr = $blockSize - (strlen(
$data) % $blockSize);
374 $pattern = chr($padChr);
375 return $data . str_repeat($pattern, $padChr);
386 $padChr = substr(
$data, -1);
387 $padLen = ord($padChr);
388 return substr(
$data, 0, -$padLen);
399 $this->iv = openssl_random_pseudo_bytes(openssl_cipher_iv_length($this->cryptParams[
'cipher']));
401 $encrypted = openssl_encrypt(
$data, $this->cryptParams[
'cipher'], $this->key, OPENSSL_RAW_DATA | OPENSSL_ZERO_PADDING, $this->iv);
402 if (
false === $encrypted) {
403 throw new Exception(
'Failure encrypting Data (openssl symmetric) - ' . openssl_error_string());
405 return $this->iv . $encrypted;
416 $iv_length = openssl_cipher_iv_length($this->cryptParams[
'cipher']);
417 $this->iv = substr(
$data, 0, $iv_length);
419 $decrypted = openssl_decrypt(
$data, $this->cryptParams[
'cipher'], $this->key, OPENSSL_RAW_DATA | OPENSSL_ZERO_PADDING, $this->iv);
420 if (
false === $decrypted) {
421 throw new Exception(
'Failure decrypting Data (openssl symmetric) - ' . openssl_error_string());
435 if (! openssl_public_encrypt(
$data, $encrypted, $this->key, $this->cryptParams[
'padding'])) {
436 throw new Exception(
'Failure encrypting Data (openssl public) - ' . openssl_error_string());
450 if (! openssl_public_decrypt(
$data, $decrypted, $this->key, $this->cryptParams[
'padding'])) {
451 throw new Exception(
'Failure decrypting Data (openssl public) - ' . openssl_error_string());
465 if (! openssl_private_encrypt(
$data, $encrypted, $this->key, $this->cryptParams[
'padding'])) {
466 throw new Exception(
'Failure encrypting Data (openssl private) - ' . openssl_error_string());
480 if (! openssl_private_decrypt(
$data, $decrypted, $this->key, $this->cryptParams[
'padding'])) {
481 throw new Exception(
'Failure decrypting Data (openssl private) - ' . openssl_error_string());
495 $algo = OPENSSL_ALGO_SHA1;
496 if (! empty($this->cryptParams[
'digest'])) {
497 $algo = $this->cryptParams[
'digest'];
499 if (! openssl_sign(
$data, $signature, $this->key,
$algo)) {
500 throw new Exception(
'Failure Signing Data: ' . openssl_error_string() .
' - ' .
$algo);
523 $algo = OPENSSL_ALGO_SHA1;
524 if (! empty($this->cryptParams[
'digest'])) {
525 $algo = $this->cryptParams[
'digest'];
527 return openssl_verify(
$data, $signature, $this->key,
$algo);
538 if ($this->cryptParams[
'library'] ===
'openssl') {
539 switch ($this->cryptParams[
'type']) {
558 if ($this->cryptParams[
'library'] ===
'openssl') {
559 switch ($this->cryptParams[
'type']) {
578 switch ($this->cryptParams[
'library']) {
581 case (self::HMAC_SHA1):
582 return hash_hmac(
"sha1",
$data, $this->key,
true);
604 switch ($this->cryptParams[
'library']) {
607 case (self::HMAC_SHA1):
608 $expectedSignature = hash_hmac(
"sha1",
$data, $this->key,
true);
609 return strcmp($signature, $expectedSignature) == 0;
628 return $this->cryptParams[
'method'];
641 if (ord($string) > 0x7f)
642 $string = chr(0).$string;
645 $string = chr(0).$string;
649 $length = strlen($string);
653 }
else if ($length < 0x0100) {
654 $output = sprintf(
"%c%c%c%s",
$type, 0x81, $length, $string);
655 }
else if ($length < 0x010000) {
656 $output = sprintf(
"%c%c%c%c%s",
$type, 0x82, $length / 0x0100, $length % 0x0100, $string);
673 $exponentEncoding = self::makeAsnSegment(0x02, $exponent);
674 $modulusEncoding = self::makeAsnSegment(0x02, $modulus);
675 $sequenceEncoding = self::makeAsnSegment(0x30, $modulusEncoding.$exponentEncoding);
676 $bitstringEncoding = self::makeAsnSegment(0x03, $sequenceEncoding);
677 $rsaAlgorithmIdentifier = pack(
"H*",
"300D06092A864886F70D0101010500");
678 $publicKeyInfo = self::makeAsnSegment(0x30, $rsaAlgorithmIdentifier.$bitstringEncoding);
681 $publicKeyInfoBase64 = base64_encode($publicKeyInfo);
682 $encoding =
"-----BEGIN PUBLIC KEY-----\n";
684 while ($segment = substr($publicKeyInfoBase64, $offset, 64)) {
685 $encoding = $encoding.$segment.
"\n";
688 return $encoding.
"-----END PUBLIC KEY-----\n";
739 $objenc->setNode($element);
740 if (! $objKey = $objenc->locateKey()) {
741 throw new Exception(
"Unable to locate algorithm for this Encrypted Key");
743 $objKey->isEncrypted =
true;
744 $objKey->encryptedCtx = $objenc;
getSymmetricKeySize()
Retrieve the key size for the symmetric encryption algorithm.
static getRawThumbprint($cert)
Get the raw thumbprint of a certificate.
static makeAsnSegment($type, $string)
static fromEncryptedKeyElement(DOMElement $element)
Create key from an EncryptedKey-element.
signOpenSSL($data)
Signs the given data (string) using the openssl-extension.
encryptPublic($data)
Encrypts the given public data (string) using the openssl-extension.
signData($data)
Signs the data (string) using the extension assigned to the type in the constructor.
encryptSymmetric($data)
Encrypts the given data (string) using the openssl-extension.
static convertRSA($modulus, $exponent)
Hint: Modulus and Exponent must already be base64 decoded.
__construct($type, $params=null)
encryptPrivate($data)
Encrypts the given private data (string) using the openssl-extension.
decryptSymmetric($data)
Decrypts the given data (string) using the openssl-extension.
loadKey($key, $isFile=false, $isCert=false)
Loads the given key, or - with isFile set true - the key from the keyfile.
verifyOpenSSL($data, $signature)
Verifies the given data (string) belonging to the given signature using the openssl-extension.
static staticLocateKeyInfo($objBaseKey=null, $node=null)
unpadISO10126($data)
Remove ISO 10126 Padding.
verifySignature($data, $signature)
Verifies the data (string) against the given signature using the extension assigned to the type in th...
getX509Thumbprint()
Get the thumbprint of this X509 certificate.
generateSessionKey()
Generates a session key using the openssl-extension.
encryptData($data)
Encrypts the given data (string) using the regarding php-extension, depending on the library assigned...
decryptPublic($data)
Decrypts the given public data (string) using the openssl-extension.
padISO10126($data, $blockSize)
ISO 10126 Padding.
decryptPrivate($data)
Decrypts the given private data (string) using the openssl-extension.
getX509Certificate()
Retrieve the X509 certificate this key represents.
decryptData($data)
Decrypts the given data (string) using the regarding php-extension, depending on the library assigned...