Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Encryption certificate inclusion #224

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 26 additions & 5 deletions src/XMLSecEnc.php
Original file line number Diff line number Diff line change
Expand Up @@ -300,9 +300,10 @@ public function decryptNode($objKey, $replace=true)
* @param XMLSecurityKey $srcKey
* @param XMLSecurityKey $rawKey
* @param bool $append
* @param bool $includeKeyInfo
* @throws Exception
*/
public function encryptKey($srcKey, $rawKey, $append=true)
public function encryptKey($srcKey, $rawKey, $append = true, $includeKeyInfo = false)
{
if ((! $srcKey instanceof XMLSecurityKey) || (! $rawKey instanceof XMLSecurityKey)) {
throw new Exception('Invalid Key');
Expand All @@ -317,11 +318,32 @@ public function encryptKey($srcKey, $rawKey, $append=true)
$this->encKey = $encKey;
}
$encMethod = $encKey->appendChild($this->encdoc->createElementNS(self::XMLENCNS, 'xenc:EncryptionMethod'));
$encMethod->setAttribute('Algorithm', $srcKey->getAlgorith());
if (! empty($srcKey->name)) {
$encMethod->setAttribute('Algorithm', $srcKey->getAlgorithm());

$x509Certificate = $srcKey->getX509Certificate();
if ($includeKeyInfo && (!empty($srcKey->name) || !empty($x509Certificate))) {
$keyInfo = $encKey->appendChild($this->encdoc->createElementNS('http://www.w3.org/2000/09/xmldsig#', 'dsig:KeyInfo'));
$keyInfo->appendChild($this->encdoc->createElementNS('http://www.w3.org/2000/09/xmldsig#', 'dsig:KeyName', $srcKey->name));

if (!empty($srcKey->name)) {
$keyInfo->appendChild($this->encdoc->createElementNS('http://www.w3.org/2000/09/xmldsig#', 'dsig:KeyName', $srcKey->name));
}

if (!empty($x509Certificate)) {
$x509Data = $keyInfo->appendChild($this->encdoc->createElementNS('http://www.w3.org/2000/09/xmldsig#', 'dsig:X509Data'));

$key = preg_split('~(\r?\n)+~', $x509Certificate, null, PREG_SPLIT_NO_EMPTY);

# Don't re-encode keys in PEM format
if($key[0] === '-----BEGIN CERTIFICATE-----') {
$key = implode('', array_slice($key, 1, count($key) - 2));
} else {
$key = base64_encode($x509Certificate);
}

$x509Data->appendChild($this->encdoc->createElementNS('http://www.w3.org/2000/09/xmldsig#', 'dsig:X509Certificate', $key));
}
}

$cipherData = $encKey->appendChild($this->encdoc->createElementNS(self::XMLENCNS, 'xenc:CipherData'));
$cipherData->appendChild($this->encdoc->createElementNS(self::XMLENCNS, 'xenc:CipherValue', $strEncKey));
if (is_array($this->references) && count($this->references) > 0) {
Expand All @@ -332,7 +354,6 @@ public function encryptKey($srcKey, $rawKey, $append=true)
$dataRef->setAttribute("URI", '#' . $refuri);
}
}
return;
}

/**
Expand Down
47 changes: 47 additions & 0 deletions tests/xmlsec-encrypt-include-keyinfo.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
--TEST--
Basic Encryption with included KeyInfo
--FILE--
<?php
require(dirname(__FILE__) . '/../xmlseclibs.php');
use RobRichards\XMLSecLibs\XMLSecurityKey;
use RobRichards\XMLSecLibs\XMLSecEnc;

if (file_exists(dirname(__FILE__) . '/oaep_sha1.xml')) {
unlink(dirname(__FILE__) . '/oaep_sha1.xml');
}

$dom = new DOMDocument();
$dom->load(dirname(__FILE__) . '/basic-doc.xml');

$objKey = new XMLSecurityKey(XMLSecurityKey::AES256_CBC);
$objKey->generateSessionKey();

$siteKey = new XMLSecurityKey(XMLSecurityKey::RSA_OAEP_MGF1P, array('type'=>'public'));
$siteKey->loadKey(dirname(__FILE__) . '/mycert.pem', TRUE, TRUE);

$enc = new XMLSecEnc();
$enc->setNode($dom->documentElement);
$enc->encryptKey($siteKey, $objKey, true, true);

$enc->type = XMLSecEnc::Element;
$encNode = $enc->encryptNode($objKey);

$dom->save(dirname(__FILE__) . '/oaep_sha1_keyinfo.xml');

$xPath = new DOMXPath($dom);
$xPath->registerNamespace('dsig', 'http://www.w3.org/2000/09/xmldsig#');
$xPath->registerNamespace('xenc', 'http://www.w3.org/2001/04/xmlenc#');

$queriedNode = $xPath->query(
'/xenc:EncryptedData/dsig:KeyInfo/xenc:EncryptedKey/dsig:KeyInfo/dsig:X509Data/dsig:X509Certificate'
);
if ($queriedNode->length !== 1) {
echo 'X509Certificate not found in KeyInfo';
}
echo $queriedNode->item(0)->textContent;

unlink(dirname(__FILE__) . '/oaep_sha1_keyinfo.xml');

?>
--EXPECTF--
MIIEVDCCAzygAwIBAgIJAPTrkMJbCOr1MA0GCSqGSIb3DQEBBQUAMHkxCzAJBgNVBAYTAlVTMQ4wDAYDVQQIEwVNYWluZTESMBAGA1UEBxMJTGltaW5ndG9uMR8wHQYDVQQKExZ4bWxzZWNsaWJzLnBocCBMaWJyYXJ5MSUwIwYDVQQDExx4bWxzZWNsaWJzL3d3dy5jZGF0YXpvbmUub3JnMB4XDTA4MDcwNzIwMjIzMVoXDTE4MDcwNTIwMjIzMVoweTELMAkGA1UEBhMCVVMxDjAMBgNVBAgTBU1haW5lMRIwEAYDVQQHEwlMaW1pbmd0b24xHzAdBgNVBAoTFnhtbHNlY2xpYnMucGhwIExpYnJhcnkxJTAjBgNVBAMTHHhtbHNlY2xpYnMvd3d3LmNkYXRhem9uZS5vcmcwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQDttdMyM5ISVD1Uz+BHAPrxVJ6N1eZonfg3DMvZVT0Zy64+qcXj8zuHC6lolDsfGnD8LUttraQ7qCL+bHKps+hjAhCRdx5Wcn4iDrlFpxFL7INnr6vekzsCQ45BPUrvksF9FKa7yX4iSDButmPfoT14gPnIuSe8Y5UeGe6Lk6sF0WgHyL+JmxOu377Kuhah2pXZ1+z7V4JIlNgemJtKlqrvgGeuE9TagfGHUL9BuZK5fUx/RSDUjqxUeKU3fft9fGIAZl0dduitC2Otv4dr1gxLrUmI+ZZ75FmtfKQT7SmS92QVI2B5WAPlL1bnbvhkZiyw7nFE+Q/wGJ2myE4RIFjdAgMBAAGjgd4wgdswHQYDVR0OBBYEFEC5iG0uGXLpQG/zMj/4TuDWfTpHMIGrBgNVHSMEgaMwgaCAFEC5iG0uGXLpQG/zMj/4TuDWfTpHoX2kezB5MQswCQYDVQQGEwJVUzEOMAwGA1UECBMFTWFpbmUxEjAQBgNVBAcTCUxpbWluZ3RvbjEfMB0GA1UEChMWeG1sc2VjbGlicy5waHAgTGlicmFyeTElMCMGA1UEAxMceG1sc2VjbGlicy93d3cuY2RhdGF6b25lLm9yZ4IJAPTrkMJbCOr1MAwGA1UdEwQFMAMBAf8wDQYJKoZIhvcNAQEFBQADggEBACmSKrte07VrGB8dtrN5mrt28ILickQjguu46h6vChgQ4YfIAoA1KWNsZJUuuIzTDjE5xz2hsW37CI0yrNesv2ho2hhP+fIaxCGmcwLYXL80UaPRglYk5+wPWFOt3QFAVoEgwjLX9+y+c2Gu7xLgHAFZVRjQ5hhKT0Nj3vhnt0k8LcognNl1wKuWda7VL4tODp/2IOXr5o5v/OL3UesGfeWfvr8LVmMc5f7/vLAu1+2Yk+/C9/EZyf3BDZQ4z8ae/iwqprCTUIEjhUDcq4+0YN2EIw6suGE2FtWlsIywNErmoOhdrmntU61n3nFCQBi7QDUnZrAFrl4/bmk3eRJ00nE=
11 changes: 11 additions & 0 deletions tests/xmlsec-encrypt.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,17 @@ $encNode = $enc->encryptNode($objKey);

$dom->save(dirname(__FILE__) . '/oaep_sha1.xml');

$xPath = new DOMXPath($dom);
$xPath->registerNamespace('dsig', 'http://www.w3.org/2000/09/xmldsig#');
$xPath->registerNamespace('xenc', 'http://www.w3.org/2001/04/xmlenc#');

$queriedNode = $xPath->query(
'/xenc:EncryptedData/dsig:KeyInfo/xenc:EncryptedKey/dsig:KeyInfo/dsig:X509Data/dsig:X509Certificate'
);
if ($queriedNode->length !== 0) {
echo 'X509Certificate was not expected to be found in KeyInfo';
}

$root = $dom->documentElement;
echo $root->localName."\n";

Expand Down