@@ -604,33 +604,7 @@ public function getLastRequestID()
604604 */
605605 public function buildRequestSignature ($ samlRequest , $ relayState , $ signAlgorithm = XMLSecurityKey::RSA_SHA1 )
606606 {
607- $ key = $ this ->_settings ->getSPkey ();
608- if (empty ($ key )) {
609- throw new OneLogin_Saml2_Error (
610- "Trying to sign the SAML Request but can't load the SP private key " ,
611- OneLogin_Saml2_Error::PRIVATE_KEY_NOT_FOUND
612- );
613- }
614-
615- $ objKey = new XMLSecurityKey ($ signAlgorithm , array ('type ' => 'private ' ));
616- $ objKey ->loadKey ($ key , false );
617-
618- $ security = $ this ->_settings ->getSecurityData ();
619- if ($ security ['lowercaseUrlencoding ' ]) {
620- $ msg = 'SAMLRequest= ' .rawurlencode ($ samlRequest );
621- if (isset ($ relayState )) {
622- $ msg .= '&RelayState= ' .rawurlencode ($ relayState );
623- }
624- $ msg .= '&SigAlg= ' . rawurlencode ($ signAlgorithm );
625- } else {
626- $ msg = 'SAMLRequest= ' .urlencode ($ samlRequest );
627- if (isset ($ relayState )) {
628- $ msg .= '&RelayState= ' .urlencode ($ relayState );
629- }
630- $ msg .= '&SigAlg= ' . urlencode ($ signAlgorithm );
631- }
632- $ signature = $ objKey ->signData ($ msg );
633- return base64_encode ($ signature );
607+ return $ this ->buildMessageSignature ($ samlRequest , $ relayState , $ signAlgorithm , "SAMLRequest " );
634608 }
635609
636610 /**
@@ -645,27 +619,47 @@ public function buildRequestSignature($samlRequest, $relayState, $signAlgorithm
645619 * @throws OneLogin_Saml2_Error
646620 */
647621 public function buildResponseSignature ($ samlResponse , $ relayState , $ signAlgorithm = XMLSecurityKey::RSA_SHA1 )
622+ {
623+ return $ this ->buildMessageSignature ($ samlResponse , $ relayState , $ signAlgorithm , "SAMLResponse " );
624+ }
625+
626+ /**
627+ * Generates the Signature for a SAML Response
628+ *
629+ * @param string $samlMessage The SAML Response
630+ * @param string $relayState The RelayState
631+ * @param string $signAlgorithm Signature algorithm method
632+ * @param string $type "SAMLRequest" or "SAMLResponse"
633+ *
634+ * @return string A base64 encoded signature
635+ *
636+ * @throws OneLogin_Saml2_Error
637+ */
638+ private function buildMessageSignature ($ samlMessage , $ relayState , $ signAlgorithm = XMLSecurityKey::RSA_SHA256 , $ type ="SAMLRequest " )
648639 {
649640 $ key = $ this ->_settings ->getSPkey ();
650641 if (empty ($ key )) {
651- throw new OneLogin_Saml2_Error (
652- "Trying to sign the SAML Response but can't load the SP private key " ,
653- OneLogin_Saml2_Error::PRIVATE_KEY_NOT_FOUND
654- );
642+ if ($ type == "SAMLRequest " ) {
643+ $ errorMsg = "Trying to sign the SAML Request but can't load the SP private key " ;
644+ } else {
645+ $ errorMsg = "Trying to sign the SAML Response but can't load the SP private key " ;
646+ }
647+
648+ throw new OneLogin_Saml2_Error ($ errorMsg , OneLogin_Saml2_Error::PRIVATE_KEY_NOT_FOUND );
655649 }
656650
657651 $ objKey = new XMLSecurityKey ($ signAlgorithm , array ('type ' => 'private ' ));
658652 $ objKey ->loadKey ($ key , false );
659653
660654 $ security = $ this ->_settings ->getSecurityData ();
661655 if ($ security ['lowercaseUrlencoding ' ]) {
662- $ msg = ' SAMLResponse = ' .rawurlencode ($ samlResponse );
656+ $ msg = $ type . ' = ' .rawurlencode ($ samlMessage );
663657 if (isset ($ relayState )) {
664658 $ msg .= '&RelayState= ' .rawurlencode ($ relayState );
665659 }
666660 $ msg .= '&SigAlg= ' . rawurlencode ($ signAlgorithm );
667661 } else {
668- $ msg = ' SAMLResponse = ' .urlencode ($ samlResponse );
662+ $ msg = $ type . ' = ' .urlencode ($ samlMessage );
669663 if (isset ($ relayState )) {
670664 $ msg .= '&RelayState= ' .urlencode ($ relayState );
671665 }
0 commit comments