7
* This source file is subject to the new BSD license that is bundled
8
* with this package in the file LICENSE.txt.
9
* It is also available through the world-wide-web at this URL:
10
* http://framework.zend.com/license/new-bsd
11
* If you did not receive a copy of the license and are unable to
12
* obtain it through the world-wide-web, please send an email
13
* to license@zend.com so we can send you a copy immediately.
16
* @package Zend_InfoCard
17
* @subpackage Zend_InfoCard_Xml_Security
18
* @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
19
* @license http://framework.zend.com/license/new-bsd New BSD License
20
* @version $Id: Security.php 9094 2008-03-30 18:36:55Z thomas $
24
* Zend_InfoCard_Xml_Security_Exception
26
require_once 'Zend/InfoCard/Xml/Security/Exception.php';
29
* Zend_InfoCard_Xml_Security_Transform
31
require_once 'Zend/InfoCard/Xml/Security/Transform.php';
36
* @package Zend_InfoCard
37
* @subpackage Zend_InfoCard_Xml_Security
38
* @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
39
* @license http://framework.zend.com/license/new-bsd New BSD License
41
class Zend_InfoCard_Xml_Security
44
* ASN.1 type INTEGER class
46
const ASN_TYPE_INTEGER = 0x02;
49
* ASN.1 type BIT STRING class
51
const ASN_TYPE_BITSTRING = 0x03;
54
* ASN.1 type SEQUENCE class
56
const ASN_TYPE_SEQUENCE = 0x30;
59
* The URI for Canonical Method C14N Exclusive
61
const CANONICAL_METHOD_C14N_EXC = 'http://www.w3.org/2001/10/xml-exc-c14n#';
64
* The URI for Signature Method SHA1
66
const SIGNATURE_METHOD_SHA1 = 'http://www.w3.org/2000/09/xmldsig#rsa-sha1';
69
* The URI for Digest Method SHA1
71
const DIGEST_METHOD_SHA1 = 'http://www.w3.org/2000/09/xmldsig#sha1';
74
* The Identifier for RSA Keys
76
const RSA_KEY_IDENTIFIER = '300D06092A864886F70D0101010500';
79
* Constructor (disabled)
83
private function __construct()
88
* Validates the signature of a provided XML block
90
* @param string $strXMLInput An XML block containing a Signature
91
* @return bool True if the signature validated, false otherwise
92
* @throws Zend_InfoCard_Xml_Security_Exception
94
static public function validateXMLSignature($strXMLInput)
96
if(!extension_loaded('openssl')) {
97
throw new Zend_InfoCard_Xml_Security_Exception("You must have the openssl extension installed to use this class");
100
$sxe = simplexml_load_string($strXMLInput);
102
if(!isset($sxe->Signature)) {
103
throw new Zend_InfoCard_Xml_Security_Exception("Could not identify XML Signature element");
106
if(!isset($sxe->Signature->SignedInfo)) {
107
throw new Zend_InfoCard_Xml_Security_Exception("Signature is missing a SignedInfo block");
110
if(!isset($sxe->Signature->SignatureValue)) {
111
throw new Zend_InfoCard_Xml_Security_Exception("Signature is missing a SignatureValue block");
114
if(!isset($sxe->Signature->KeyInfo)) {
115
throw new Zend_InfoCard_Xml_Security_Exception("Signature is missing a KeyInfo block");
118
if(!isset($sxe->Signature->KeyInfo->KeyValue)) {
119
throw new Zend_InfoCard_Xml_Security_Exception("Signature is missing a KeyValue block");
122
switch((string)$sxe->Signature->SignedInfo->CanonicalizationMethod['Algorithm']) {
123
case self::CANONICAL_METHOD_C14N_EXC:
124
$cMethod = (string)$sxe->Signature->SignedInfo->CanonicalizationMethod['Algorithm'];
127
throw new Zend_InfoCard_Xml_Security_Exception("Unknown or unsupported CanonicalizationMethod Requested");
130
switch((string)$sxe->Signature->SignedInfo->SignatureMethod['Algorithm']) {
131
case self::SIGNATURE_METHOD_SHA1:
132
$sMethod = (string)$sxe->Signature->SignedInfo->SignatureMethod['Algorithm'];
135
throw new Zend_InfoCard_Xml_Security_Exception("Unknown or unsupported SignatureMethod Requested");
138
switch((string)$sxe->Signature->SignedInfo->Reference->DigestMethod['Algorithm']) {
139
case self::DIGEST_METHOD_SHA1:
140
$dMethod = (string)$sxe->Signature->SignedInfo->Reference->DigestMethod['Algorithm'];
143
throw new Zend_InfoCard_Xml_Security_Exception("Unknown or unsupported DigestMethod Requested");
146
$base64DecodeSupportsStrictParam = version_compare(PHP_VERSION, '5.2.0', '>=');
148
if ($base64DecodeSupportsStrictParam) {
149
$dValue = base64_decode((string)$sxe->Signature->SignedInfo->Reference->DigestValue, true);
151
$dValue = base64_decode((string)$sxe->Signature->SignedInfo->Reference->DigestValue);
154
if ($base64DecodeSupportsStrictParam) {
155
$signatureValue = base64_decode((string)$sxe->Signature->SignatureValue, true);
157
$signatureValue = base64_decode((string)$sxe->Signature->SignatureValue);
160
$transformer = new Zend_InfoCard_Xml_Security_Transform();
162
foreach($sxe->Signature->SignedInfo->Reference->Transforms->children() as $transform) {
163
$transformer->addTransform((string)$transform['Algorithm']);
166
$transformed_xml = $transformer->applyTransforms($strXMLInput);
168
$transformed_xml_binhash = pack("H*", sha1($transformed_xml));
170
if($transformed_xml_binhash != $dValue) {
171
throw new Zend_InfoCard_Xml_Security_Exception("Locally Transformed XML does not match XML Document. Cannot Verify Signature");
177
case isset($sxe->Signature->KeyInfo->KeyValue->X509Certificate):
179
$certificate = (string)$sxe->Signature->KeyInfo->KeyValue->X509Certificate;
182
$pem = "-----BEGIN CERTIFICATE-----\n" .
183
wordwrap($certificate, 64, "\n", true) .
184
"\n-----END CERTIFICATE-----";
186
$public_key = openssl_pkey_get_public($pem);
189
throw new Zend_InfoCard_Xml_Security_Exception("Unable to extract and prcoess X509 Certificate from KeyValue");
193
case isset($sxe->Signature->KeyInfo->KeyValue->RSAKeyValue):
195
if(!isset($sxe->Signature->KeyInfo->KeyValue->RSAKeyValue->Modulus) ||
196
!isset($sxe->Signature->KeyInfo->KeyValue->RSAKeyValue->Exponent)) {
197
throw new Zend_InfoCard_Xml_Security_Exception("RSA Key Value not in Modulus/Exponent form");
200
$modulus = base64_decode((string)$sxe->Signature->KeyInfo->KeyValue->RSAKeyValue->Modulus);
201
$exponent = base64_decode((string)$sxe->Signature->KeyInfo->KeyValue->RSAKeyValue->Exponent);
203
$pem_public_key = self::_getPublicKeyFromModExp($modulus, $exponent);
205
$public_key = openssl_pkey_get_public ($pem_public_key);
209
throw new Zend_InfoCard_Xml_Security_Exception("Unable to determine or unsupported representation of the KeyValue block");
212
$transformer = new Zend_InfoCard_Xml_Security_Transform();
213
$transformer->addTransform((string)$sxe->Signature->SignedInfo->CanonicalizationMethod['Algorithm']);
215
// The way we are doing our XML processing requires that we specifically add this
216
// (even though it's in the <Signature> parent-block).. otherwise, our canonical form
217
// fails signature verification
218
$sxe->Signature->SignedInfo->addAttribute('xmlns', 'http://www.w3.org/2000/09/xmldsig#');
220
$canonical_signedinfo = $transformer->applyTransforms($sxe->Signature->SignedInfo->asXML());
222
if(@openssl_verify($canonical_signedinfo, $signatureValue, $public_key)) {
223
return (string)$sxe->Signature->SignedInfo->Reference['URI'];
230
* Transform an RSA Key in Modulus/Exponent format into a PEM encoding and
231
* return an openssl resource for it
233
* @param string $modulus The RSA Modulus in binary format
234
* @param string $exponent The RSA exponent in binary format
235
* @return string The PEM encoded version of the key
237
static protected function _getPublicKeyFromModExp($modulus, $exponent)
239
$modulusInteger = self::_encodeValue($modulus, self::ASN_TYPE_INTEGER);
240
$exponentInteger = self::_encodeValue($exponent, self::ASN_TYPE_INTEGER);
241
$modExpSequence = self::_encodeValue($modulusInteger . $exponentInteger, self::ASN_TYPE_SEQUENCE);
242
$modExpBitString = self::_encodeValue($modExpSequence, self::ASN_TYPE_BITSTRING);
244
$binRsaKeyIdentifier = pack( "H*", self::RSA_KEY_IDENTIFIER );
246
$publicKeySequence = self::_encodeValue($binRsaKeyIdentifier . $modExpBitString, self::ASN_TYPE_SEQUENCE);
248
$publicKeyInfoBase64 = base64_encode( $publicKeySequence );
250
$publicKeyString = "-----BEGIN PUBLIC KEY-----\n";
251
$publicKeyString .= wordwrap($publicKeyInfoBase64, 64, "\n", true);
252
$publicKeyString .= "\n-----END PUBLIC KEY-----\n";
254
return $publicKeyString;
258
* Encode a limited set of data types into ASN.1 encoding format
259
* which is used in X.509 certificates
261
* @param string $data The data to encode
262
* @param const $type The encoding format constant
263
* @return string The encoded value
264
* @throws Zend_InfoCard_Xml_Security_Exception
266
static protected function _encodeValue($data, $type)
268
// Null pad some data when we get it (integer values > 128 and bitstrings)
269
if( (($type == self::ASN_TYPE_INTEGER) && (ord($data) > 0x7f)) ||
270
($type == self::ASN_TYPE_BITSTRING)) {
274
$len = strlen($data);
276
// encode the value based on length of the string
277
// I'm fairly confident that this is by no means a complete implementation
278
// but it is enough for our purposes
281
return sprintf("%c%c%s", $type, $len, $data);
282
case ($len < 0x0100):
283
return sprintf("%c%c%c%s", $type, 0x81, $len, $data);
284
case ($len < 0x010000):
285
return sprintf("%c%c%c%c%s", $type, 0x82, $len / 0x0100, $len % 0x0100, $data);
287
throw new Zend_InfoCard_Xml_Security_Exception("Could not encode value");
290
throw new Zend_InfoCard_Xml_Security_Exception("Invalid code path");
b'\\ No newline at end of file'