Skip to content

Commit 2f8c755

Browse files
authored
Support for ECDSA-SHA256 XML signatures (#275)
* Added support for ECDSA-SHA256 XML signatures * ECDSA-SHA256 refactor to use phpseclib instead of openssl
1 parent 3b027cd commit 2f8c755

1 file changed

Lines changed: 25 additions & 0 deletions

File tree

src/XMLSecurityKey.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ class XMLSecurityKey
6363
const RSA_SHA256 = 'http://www.w3.org/2001/04/xmldsig-more#rsa-sha256';
6464
const RSA_SHA384 = 'http://www.w3.org/2001/04/xmldsig-more#rsa-sha384';
6565
const RSA_SHA512 = 'http://www.w3.org/2001/04/xmldsig-more#rsa-sha512';
66+
const ECDSA_SHA256 = 'http://www.w3.org/2001/04/xmldsig-more#ecdsa-sha256';
6667
const HMAC_SHA1 = 'http://www.w3.org/2000/09/xmldsig#hmac-sha1';
6768
const RSA_SHA256_MGF1 = 'http://www.w3.org/2007/05/xmldsig-more#sha256-rsa-MGF1';
6869
const AUTHTAG_LENGTH = 16;
@@ -232,6 +233,17 @@ public function __construct($type, $params=null)
232233
}
233234
}
234235
throw new Exception('Certificate "type" (private/public) must be passed via parameters');
236+
case (self::ECDSA_SHA256):
237+
$this->cryptParams['library'] = 'phpseclib';
238+
$this->cryptParams['method'] = 'http://www.w3.org/2001/04/xmldsig-more#ecdsa-sha256';
239+
$this->cryptParams['digest'] = 'SHA256';
240+
if (is_array($params) && ! empty($params['type'])) {
241+
if ($params['type'] == 'public' || $params['type'] == 'private') {
242+
$this->cryptParams['type'] = $params['type'];
243+
break;
244+
}
245+
}
246+
throw new Exception('Certificate "type" (private/public) must be passed via parameters');
235247
case (self::RSA_SHA384):
236248
$this->cryptParams['library'] = 'openssl';
237249
$this->cryptParams['method'] = 'http://www.w3.org/2001/04/xmldsig-more#rsa-sha384';
@@ -675,6 +687,12 @@ public function signData($data)
675687
return $this->signOpenSSL($data);
676688
case 'phpseclib':
677689
$private = PublicKeyLoader::load($this->key);
690+
if ($this->type === self::ECDSA_SHA256) {
691+
return $private
692+
->withHash($this->cryptParams['digest'])
693+
->withSignatureFormat('IEEE')
694+
->sign($data);
695+
}
678696
return $private
679697
->withPadding($this->cryptParams['padding'])
680698
->withHash($this->cryptParams['digest'])
@@ -708,6 +726,13 @@ public function verifySignature($data, $signature)
708726
return $this->verifyOpenSSL($data, $signature);
709727
case 'phpseclib':
710728
$public = PublicKeyLoader::load($this->key);
729+
if ($this->type === self::ECDSA_SHA256) {
730+
$result = $public
731+
->withHash($this->cryptParams['digest'])
732+
->withSignatureFormat('IEEE')
733+
->verify($data, $signature);
734+
return $result === true ? 1 : 0;
735+
}
711736
$result = $public
712737
->withPadding($this->cryptParams['padding'])
713738
->withHash($this->cryptParams['digest'])

0 commit comments

Comments
 (0)