Skip to content

Commit 7b4d4a6

Browse files
committed
Add AES128_GCM encryption on generateNameId method. New setting parameter encryption_algorithm. If you set a encryption method different than AES128_CBC then the algorithm RSA_OAEP_MGF1P will be used as well instead RSA_1_5
1 parent ee19b50 commit 7b4d4a6

6 files changed

Lines changed: 51 additions & 4 deletions

File tree

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -513,6 +513,17 @@ $advancedSettings = array(
513513
// Notice that sha1 is a deprecated algorithm and should not be used
514514
'digestAlgorithm' => 'http://www.w3.org/2001/04/xmlenc#sha256',
515515

516+
// Algorithm that the toolkit will use for encryption process. Options:
517+
// 'http://www.w3.org/2001/04/xmlenc#tripledes-cbc'
518+
// 'http://www.w3.org/2001/04/xmlenc#aes128-cbc'
519+
// 'http://www.w3.org/2001/04/xmlenc#aes192-cbc'
520+
// 'http://www.w3.org/2001/04/xmlenc#aes256-cbc'
521+
// 'http://www.w3.org/2009/xmlenc11#aes128-gcm'
522+
// 'http://www.w3.org/2009/xmlenc11#aes192-gcm'
523+
// 'http://www.w3.org/2009/xmlenc11#aes256-gcm';
524+
// Notice that aes-cbc are not consider secure anymore so should not be used
525+
'encryption_algorithm' => 'http://www.w3.org/2009/xmlenc11#aes128-gcm',
526+
516527
// ADFS URL-Encodes SAML data as lowercase, and the toolkit by default uses
517528
// uppercase. Turn it True for ADFS compatibility on signature verification
518529
'lowercaseUrlencoding' => false,

advanced_settings_example.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,17 @@
116116
// Notice that sha1 is a deprecated algorithm and should not be used
117117
'digestAlgorithm' => 'http://www.w3.org/2001/04/xmlenc#sha256',
118118

119+
// Algorithm that the toolkit will use for encryption process. Options:
120+
// 'http://www.w3.org/2001/04/xmlenc#tripledes-cbc'
121+
// 'http://www.w3.org/2001/04/xmlenc#aes128-cbc'
122+
// 'http://www.w3.org/2001/04/xmlenc#aes192-cbc'
123+
// 'http://www.w3.org/2001/04/xmlenc#aes256-cbc'
124+
// 'http://www.w3.org/2009/xmlenc11#aes128-gcm'
125+
// 'http://www.w3.org/2009/xmlenc11#aes192-gcm'
126+
// 'http://www.w3.org/2009/xmlenc11#aes256-gcm';
127+
// Notice that aes-cbc are not consider secure anymore so should not be used
128+
'encryption_algorithm' => 'http://www.w3.org/2009/xmlenc11#aes128-gcm',
129+
119130
// ADFS URL-Encodes SAML data as lowercase, and the toolkit by default uses
120131
// uppercase. Turn it True for ADFS compatibility on signature verification
121132
'lowercaseUrlencoding' => false,

src/Saml2/LogoutRequest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,8 @@ public function __construct(\OneLogin\Saml2\Settings $settings, $request = null,
122122
$nameIdSPNameQualifier,
123123
$nameIdFormat,
124124
$cert,
125-
$nameIdNameQualifier
125+
$nameIdNameQualifier,
126+
$security['encryption_algorithm']
126127
);
127128

128129
$sessionIndexStr = isset($sessionIndex) ? "<samlp:SessionIndex>{$sessionIndex}</samlp:SessionIndex>" : "";

src/Saml2/Settings.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,11 @@ private function _addDefaultValues()
430430
$this->_security['digestAlgorithm'] = XMLSecurityDSig::SHA256;
431431
}
432432

433+
// EncryptionAlgorithm
434+
if (!isset($this->_security['encryption_algorithm'])) {
435+
$this->_security['encryption_algorithm'] = XMLSecurityKey::AES128_CBC;
436+
}
437+
433438
if (!isset($this->_security['lowercaseUrlencoding'])) {
434439
$this->_security['lowercaseUrlencoding'] = false;
435440
}

src/Saml2/Utils.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1056,12 +1056,13 @@ public static function formatFingerPrint($fingerprint)
10561056
* @param string|null $format SP Format
10571057
* @param string|null $cert IdP Public cert to encrypt the nameID
10581058
* @param string|null $nq IdP Name Qualifier
1059+
* @param string|null $enc_alg Encryption algorithm
10591060
*
10601061
* @return string $nameIDElement DOMElement | XMLSec nameID
10611062
*
10621063
* @throws Exception
10631064
*/
1064-
public static function generateNameId($value, $spnq, $format = null, $cert = null, $nq = null)
1065+
public static function generateNameId($value, $spnq, $format = null, $cert = null, $nq = null, $enc_alg = XMLSecurityKey::AES128_CBC)
10651066
{
10661067

10671068
$doc = new DOMDocument();
@@ -1081,14 +1082,18 @@ public static function generateNameId($value, $spnq, $format = null, $cert = nul
10811082
$doc->appendChild($nameId);
10821083

10831084
if (!empty($cert)) {
1084-
$seckey = new XMLSecurityKey(XMLSecurityKey::RSA_1_5, array('type'=>'public'));
1085+
if ($enc_alg == XMLSecurityKey::AES128_CBC) {
1086+
$seckey = new XMLSecurityKey(XMLSecurityKey::RSA_1_5, array('type'=>'public'));
1087+
} else {
1088+
$seckey = new XMLSecurityKey(XMLSecurityKey::RSA_OAEP_MGF1P, array('type'=>'public'));
1089+
}
10851090
$seckey->loadKey($cert);
10861091

10871092
$enc = new XMLSecEnc();
10881093
$enc->setNode($nameId);
10891094
$enc->type = XMLSecEnc::Element;
10901095

1091-
$symmetricKey = new XMLSecurityKey(XMLSecurityKey::AES128_CBC);
1096+
$symmetricKey = new XMLSecurityKey($enc_alg);
10921097
$symmetricKey->generateSessionKey();
10931098
$enc->encryptKey($seckey, $symmetricKey);
10941099

tests/src/OneLogin/Saml2/UtilsTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -853,6 +853,20 @@ public function testGenerateNameIdWithSPNameQualifier()
853853

854854
$nameidExpectedEnc = '<saml:EncryptedID><xenc:EncryptedData xmlns:xenc="http://www.w3.org/2001/04/xmlenc#" xmlns:dsig="http://www.w3.org/2000/09/xmldsig#" Type="http://www.w3.org/2001/04/xmlenc#Element"><xenc:EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#aes128-cbc"/><dsig:KeyInfo xmlns:dsig="http://www.w3.org/2000/09/xmldsig#"><xenc:EncryptedKey><xenc:EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#rsa-1_5"/><xenc:CipherData><xenc:CipherValue>';
855855
$this->assertContains($nameidExpectedEnc, $nameIdEnc);
856+
857+
// Check AES128_GCM support
858+
859+
$nameidExpectedEnc = '<saml:EncryptedID><xenc:EncryptedData xmlns:xenc="http://www.w3.org/2001/04/xmlenc#" xmlns:dsig="http://www.w3.org/2000/09/xmldsig#" Type="http://www.w3.org/2001/04/xmlenc#Element"><xenc:EncryptionMethod Algorithm="http://www.w3.org/2009/xmlenc11#aes128-gcm"/><dsig:KeyInfo xmlns:dsig="http://www.w3.org/2000/09/xmldsig#"><xenc:EncryptedKey><xenc:EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#rsa-oaep-mgf1p"/><xenc:CipherData><xenc:CipherValue>';
860+
861+
$nameIdEnc = Utils::generateNameId(
862+
$nameIdValue,
863+
$entityId,
864+
$nameIDFormat,
865+
$key,
866+
null,
867+
XMLSecurityKey::AES128_GCM
868+
);
869+
$this->assertContains($nameidExpectedEnc, $nameIdEnc);
856870
}
857871

858872
/**

0 commit comments

Comments
 (0)