summaryrefslogtreecommitdiffstats
path: root/lib/SAML2/SOAPClient.php
blob: 219d03f9d5b3e8e8edeac10607ab2ab54f9b7516 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
<?php
/**
 * Implementation of the SAML 2.0 SOAP binding.
 *
 * @author Shoaib Ali
 * @package simpleSAMLphp
 * @version $Id$
 */
class SAML2_SOAPClient {

	const START_SOAP_ENVELOPE = '<soap-env:Envelope xmlns:soap-env="http://schemas.xmlsoap.org/soap/envelope/"><soap-env:Header/><soap-env:Body>';
	const END_SOAP_ENVELOPE = '</soap-env:Body></soap-env:Envelope>';

	/**
	 * This function sends the SOAP message to the service location and returns SOAP response
	 *
	 * @param SAML2_Message $m  The request that should be sent.
	 * @param SimpleSAML_Configuration $srcMetadata  The metadata of the issuer of the message.
	 * @param SimpleSAML_Configuration $dstMetadata  The metadata of the destination of the message.
	 * @return SAML2_Message  The response we received.
	 */
	public function send(SAML2_Message $msg, SimpleSAML_Configuration $srcMetadata, SimpleSAML_Configuration $dstMetadata = NULL) {

		$issuer = $msg->getIssuer();

		$options = array(
			'uri' => $issuer,
			'location' => $msg->getDestination(),
		);

		// Determine if we are going to do a MutualSSL connection between the IdP and SP  - Shoaib
		if ($srcMetadata->hasValue('saml.SOAPClient.certificate')) {
			$options['local_cert'] = SimpleSAML_Utilities::resolveCert($srcMetadata->getString('saml.SOAPClient.certificate'));
			if ($srcMetadata->hasValue('saml.SOAPClient.privatekey_pass')) {
				$options['passphrase'] = $srcMetadata->getString('saml.SOAPClient.privatekey_pass');
			}
		} else {
			/* Use the SP certificate and privatekey if it is configured. */
			$privateKey = SimpleSAML_Utilities::loadPrivateKey($srcMetadata);
			$publicKey = SimpleSAML_Utilities::loadPublicKey($srcMetadata);
			if ($privateKey !== NULL && $publicKey !== NULL && isset($publicKey['PEM'])) {
				$keyCertData = $privateKey['PEM'] . $publicKey['PEM'];
				$file = SimpleSAML_Utilities::getTempDir() . '/' . sha1($keyCertData) . '.pem';
				if (!file_exists($file)) {
					SimpleSAML_Utilities::writeFile($file, $keyCertData);
				}
				$options['local_cert'] = $file;
				if (isset($privateKey['password'])) {
					$options['passphrase'] = $privateKey['password'];
				}
			}
		}

		// do peer certificate verification
		if ($dstMetadata !== NULL) {
			$peerPublicKeys = $dstMetadata->getPublicKeys('signing', TRUE);
			$certData = '';
			foreach ($peerPublicKeys as $key) {
				if ($key['type'] !== 'X509Certificate') {
					continue;
				}
				$certData .= "-----BEGIN CERTIFICATE-----\n" .
					chunk_split($key['X509Certificate'], 64) .
					"-----END CERTIFICATE-----\n";
			}
			$peerCertFile = SimpleSAML_Utilities::getTempDir() . '/' . sha1($certData) . '.pem';
			if (!file_exists($peerCertFile)) {
				SimpleSAML_Utilities::writeFile($peerCertFile, $certData);
			}
			// create ssl context
			$ctxOpts = array(
				'ssl' => array(
					'verify_peer' => TRUE,
					'verify_depth' => 1,
					'cafile' => $peerCertFile
					));
			if (isset($options['local_cert'])) {
				$ctxOpts['ssl']['local_cert'] = $options['local_cert'];
				unset($options['local_cert']);
			}
			if (isset($options['passhprase'])) {
				$ctxOpts['ssl']['passphrase'] = $options['passphrase'];
				unset($options['passphrase']);
			}
			$context = stream_context_create($ctxOpts);
			if ($context === NULL) {
				throw new Exception('Unable to create SSL stream context');
			}
			$options['stream_context'] = $context;
		}

		$x = new SoapClient(NULL, $options);

		// Add soap-envelopes
		$request = $msg->toSignedXML();
		$request = self::START_SOAP_ENVELOPE . $request->ownerDocument->saveXML($request) . self::END_SOAP_ENVELOPE;

		$action = 'http://www.oasis-open.org/committees/security';
		$version = '1.1';
		$destination = $msg->getDestination();


		/* Perform SOAP Request over HTTP */
		$soapresponsexml = $x->__doRequest($request, $destination, $action, $version);
		if ($soapresponsexml === NULL || $soapresponsexml === "") {
			throw new Exception('Empty SOAP response, check peer certificate.');
		}

		// Convert to SAML2_Message (DOMElement)
		$dom = new DOMDocument();
		if (!$dom->loadXML($soapresponsexml)) {
			throw new Exception('Not a SOAP response.');
		}

		$soapfault = $this->getSOAPFault($dom);
		if (isset($soapfault)) {
			throw new Exception($soapfault);
		}
		//Extract the message from the response
		$xml = $dom->firstChild;    /* Soap Envelope */
		$samlresponse = SAML2_Utils::xpQuery($dom->firstChild, '/soap-env:Envelope/soap-env:Body/*[1]');
		$samlresponse = SAML2_Message::fromXML($samlresponse[0]);


		SimpleSAML_Logger::debug("Valid ArtifactResponse received from IdP");

		return $samlresponse;

	}


	/*
	 * Extracts the SOAP Fault from SOAP message
	 * @param $soapmessage Soap response needs to be type DOMDocument
	 * @return $soapfaultstring string|NULL
	 */
	private function getSOAPFault($soapmessage) {

		$soapfault = SAML2_Utils::xpQuery($soapmessage->firstChild, '/soap-env:Envelope/soap-env:Body/soap-env:Fault');

		if (empty($soapfault)) {
			/* No fault. */
			return NULL;
		}
		$soapfaultelement = $soapfault[0];
		$soapfaultstring = "Unknown fault string found"; // There is a fault element but we havn't found out what the fault string is
		// find out the fault string
		$faultstringelement =   SAML2_Utils::xpQuery($soapfaultelement, './soap-env:faultstring') ;
		if (!empty($faultstringelement)) {
			return $faultstringelement[0]->textContent;
		}
		return $soapfaultstring;
	}

}