]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - modules/Users/authentication/SAMLAuthenticate/lib/onelogin/saml/xmlsec.php
Release 6.5.0
[Github/sugarcrm.git] / modules / Users / authentication / SAMLAuthenticate / lib / onelogin / saml / xmlsec.php
1 <?php
2 /*********************************************************************************
3 Copyright (c) 2010, OneLogin, Inc.
4 All rights reserved.
5
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions are met:
8     * Redistributions of source code must retain the above copyright
9       notice, this list of conditions and the following disclaimer.
10     * Redistributions in binary form must reproduce the above copyright
11       notice, this list of conditions and the following disclaimer in the
12       documentation and/or other materials provided with the distribution.
13     * Neither the name of the <organization> nor the
14       names of its contributors may be used to endorse or promote products
15       derived from this software without specific prior written permission.
16
17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 DISCLAIMED. IN NO EVENT SHALL ONELOGIN, INC. BE LIABLE FOR ANY
21 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  ********************************************************************************/
28  
29   require(dirname(__FILE__) . '/../../xmlseclibs/xmlseclibs.php');
30
31   /**
32    * Determine if the SAML response is valid using a provided x509 certificate.
33    */
34   class SamlXmlSec {
35     /**
36      * A SamlResponse class provided to the constructor.
37      */
38     private $settings;
39
40     /**
41      * The documentument to be tested.
42      */
43     private $document;
44
45     /**
46      * Construct the SamlXmlSec object.
47      *
48      * @param SamlResponse $settings
49      *   A SamlResponse settings object containing the necessary
50      *   x509 certicate to test the document.
51      * @param string $document
52      *   The document to test.
53      */
54     function __construct($settings, $document) {
55       $this->settings = $settings;
56       $this->document = $document;
57     }
58
59
60     /**
61      * Determine if the document passes the security test.
62      *
63      * @return
64      *   TRUE if the document passes. This could throw a generic Exception
65      *   if the document or key cannot be found.
66      */
67     
68     function validateNumAssertions(){
69       $rootNode = $this->document; //->documentElement->ownerDocument;
70       $assertionNodes = $rootNode->getElementsByTagName('Assertion');
71       return ($assertionNodes->length == 1);
72     }
73
74     function validateTimestamps(){
75       $rootNode = $this->document;
76       $timestampNodes = $rootNode->getElementsByTagName('Conditions');
77       for($i=0;$i<$timestampNodes->length;$i++){
78         $nbAttribute = $timestampNodes->item($i)->attributes->getNamedItem("NotBefore");
79         $naAttribute = $timestampNodes->item($i)->attributes->getNamedItem("NotOnOrAfter");
80         if($nbAttribute && strtotime($nbAttribute->textContent) > time()){
81             return false;
82         }
83         if($naAttribute && strtotime($naAttribute->textContent) <= time()){
84             return false;
85         }
86       }
87       return true;
88     }
89  
90     function is_valid() {
91         $objXMLSecDSig = new XMLSecurityDSig();
92
93         $objDSig = $objXMLSecDSig->locateSignature($this->document);
94         if (! $objDSig) {
95                 throw new Exception("Cannot locate Signature Node");
96         }
97         $objXMLSecDSig->canonicalizeSignedInfo();
98         $objXMLSecDSig->idKeys = array('ID');
99
100         $retVal = $objXMLSecDSig->validateReference();
101         if (! $retVal) {
102                 throw new Exception("Reference Validation Failed");
103         }
104
105         $objKey = $objXMLSecDSig->locateKey();
106         if (! $objKey ) {
107                 throw new Exception("We have no idea about the key");
108         }
109         $key = NULL;
110
111         $singleAssertion = $this->validateNumAssertions();
112       if (!$singleAssertion){
113         throw new Exception("Only one SAMLAssertion allowed");
114       }
115
116       $validTimestamps = $this->validateTimestamps();
117       if (!$validTimestamps){
118         throw new Exception("SAMLAssertion conditions not met");
119       }
120
121         $objKeyInfo = XMLSecEnc::staticLocateKeyInfo($objKey, $objDSig);
122
123       $objKey->loadKey($this->settings->x509certificate, FALSE, true);
124
125         $result = $objXMLSecDSig->verify($objKey);
126         return $result;
127     }
128
129  }
130
131 ?>