| 1 |
""" |
| 2 |
SignXML utility functions |
| 3 |
|
| 4 |
bytes_to_long, long_to_bytes copied from https://github.com/dlitz/pycrypto/blob/master/lib/Crypto/Util/number.py |
| 5 |
""" |
| 6 |
|
| 7 |
import math |
| 8 |
import re |
| 9 |
import struct |
| 10 |
import textwrap |
| 11 |
from base64 import b64decode, b64encode |
| 12 |
from dataclasses import dataclass |
| 13 |
from typing import Any, List, Optional |
| 14 |
|
| 15 |
import certifi |
| 16 |
from cryptography import x509 |
| 17 |
from cryptography.hazmat.primitives import hashes, hmac |
| 18 |
from lxml.etree import QName |
| 19 |
|
| 20 |
from ..exceptions import InvalidCertificate |
| 21 |
|
| 22 |
PEM_HEADER = "-----BEGIN CERTIFICATE-----" |
| 23 |
PEM_FOOTER = "-----END CERTIFICATE-----" |
| 24 |
|
| 25 |
|
| 26 |
class Namespace(dict): |
| 27 |
def __getattr__(self, a): |
| 28 |
try: |
| 29 |
return dict.__getitem__(self, a) |
| 30 |
except KeyError: |
| 31 |
raise AttributeError(a) from None |
| 32 |
|
| 33 |
|
| 34 |
namespaces = Namespace( |
| 35 |
ds="http://www.w3.org/2000/09/xmldsig#", |
| 36 |
dsig11="http://www.w3.org/2009/xmldsig11#", |
| 37 |
dsig2="http://www.w3.org/2010/xmldsig2#", |
| 38 |
ec="http://www.w3.org/2001/10/xml-exc-c14n#", |
| 39 |
dsig_more="http://www.w3.org/2001/04/xmldsig-more#", |
| 40 |
xenc="http://www.w3.org/2001/04/xmlenc#", |
| 41 |
xenc11="http://www.w3.org/2009/xmlenc11#", |
| 42 |
xades="http://uri.etsi.org/01903/v1.3.2#", |
| 43 |
xades141="http://uri.etsi.org/01903/v1.4.1#", |
| 44 |
) |
| 45 |
|
| 46 |
|
| 47 |
def ds_tag(tag): |
| 48 |
return QName(namespaces.ds, tag) |
| 49 |
|
| 50 |
|
| 51 |
def dsig11_tag(tag): |
| 52 |
return QName(namespaces.dsig11, tag) |
| 53 |
|
| 54 |
|
| 55 |
def ec_tag(tag): |
| 56 |
return QName(namespaces.ec, tag) |
| 57 |
|
| 58 |
|
| 59 |
def xades_tag(tag): |
| 60 |
return QName(namespaces.xades, tag) |
| 61 |
|
| 62 |
|
| 63 |
def xades141_tag(tag): |
| 64 |
return QName(namespaces.xades141, tag) |
| 65 |
|
| 66 |
|
| 67 |
@dataclass |
| 68 |
class SigningSettings: |
| 69 |
key: Any |
| 70 |
key_name: Optional[str] |
| 71 |
key_info: Any |
| 72 |
always_add_key_value: bool |
| 73 |
cert_chain: Optional[List] |
| 74 |
|
| 75 |
|
| 76 |
def ensure_bytes(x, encoding="utf-8", none_ok=False): |
| 77 |
if none_ok is True and x is None: |
| 78 |
return x |
| 79 |
if not isinstance(x, bytes): |
| 80 |
x = x.encode(encoding) |
| 81 |
return x |
| 82 |
|
| 83 |
|
| 84 |
def ensure_str(x, encoding="utf-8", none_ok=False): |
| 85 |
if none_ok is True and x is None: |
| 86 |
return x |
| 87 |
if not isinstance(x, str): |
| 88 |
x = x.decode(encoding) |
| 89 |
return x |
| 90 |
|
| 91 |
|
| 92 |
def bytes_to_long(s): |
| 93 |
"""bytes_to_long(string) : long |
| 94 |
Convert a byte string to a long integer. |
| 95 |
|
| 96 |
This is (essentially) the inverse of long_to_bytes(). |
| 97 |
""" |
| 98 |
if isinstance(s, int): |
| 99 |
# On Python 2, indexing into a bytearray returns a byte string; on Python 3, an int. |
| 100 |
return s |
| 101 |
acc = 0 |
| 102 |
unpack = struct.unpack |
| 103 |
length = len(s) |
| 104 |
if length % 4: |
| 105 |
extra = 4 - length % 4 |
| 106 |
s = b"\000" * extra + s |
| 107 |
length = length + extra |
| 108 |
for i in range(0, length, 4): |
| 109 |
acc = (acc << 32) + unpack(b">I", s[i : i + 4])[0] |
| 110 |
return acc |
| 111 |
|
| 112 |
|
| 113 |
def long_to_bytes(n, blocksize=0): |
| 114 |
"""long_to_bytes(n:long, blocksize:int) : string |
| 115 |
Convert a long integer to a byte string. |
| 116 |
|
| 117 |
If optional blocksize is given and greater than zero, pad the front of the |
| 118 |
byte string with binary zeros so that the length is a multiple of |
| 119 |
blocksize. |
| 120 |
""" |
| 121 |
# after much testing, this algorithm was deemed to be the fastest |
| 122 |
s = b"" |
| 123 |
pack = struct.pack |
| 124 |
while n > 0: |
| 125 |
s = pack(b">I", n & 0xFFFFFFFF) + s |
| 126 |
n = n >> 32 |
| 127 |
# strip off leading zeros |
| 128 |
for i in range(len(s)): |
| 129 |
if s[i] != b"\000"[0]: |
| 130 |
break |
| 131 |
else: |
| 132 |
# only happens when n == 0 |
| 133 |
s = b"\000" |
| 134 |
i = 0 |
| 135 |
s = s[i:] |
| 136 |
# add back some pad bytes. this could be done more efficiently w.r.t. the |
| 137 |
# de-padding being done above, but sigh... |
| 138 |
if blocksize > 0 and len(s) % blocksize: |
| 139 |
s = (blocksize - len(s) % blocksize) * b"\000" + s |
| 140 |
return s |
| 141 |
|
| 142 |
|
| 143 |
def bits_to_bytes_unit(num_of_bits): |
| 144 |
"""bits_to_bytes_unit(num_of_bits:int) : int |
| 145 |
Convert the unit of measurement for the argument from bits to bytes. |
| 146 |
|
| 147 |
Rounds up to the nearest whole byte. |
| 148 |
""" |
| 149 |
return int(math.ceil(num_of_bits / 8)) |
| 150 |
|
| 151 |
|
| 152 |
pem_regexp = re.compile( |
| 153 |
"{header}{nl}(.+?){footer}".format(header=PEM_HEADER, nl="\r{0,1}\n", footer=PEM_FOOTER), flags=re.S |
| 154 |
) |
| 155 |
|
| 156 |
|
| 157 |
def strip_pem_header(cert): |
| 158 |
search_res = re.search(pem_regexp, ensure_str(cert)) |
| 159 |
if search_res: |
| 160 |
return search_res.group(1).replace("\r", "") |
| 161 |
return ensure_str(cert).replace("\r", "") |
| 162 |
|
| 163 |
|
| 164 |
def add_pem_header(bare_base64_cert): |
| 165 |
bare_base64_cert = ensure_str(bare_base64_cert) |
| 166 |
if bare_base64_cert.startswith(PEM_HEADER): |
| 167 |
return bare_base64_cert.encode() |
| 168 |
cert_with_header = PEM_HEADER + "\n" + textwrap.fill(bare_base64_cert, 64) + "\n" + PEM_FOOTER |
| 169 |
return cert_with_header.encode() |
| 170 |
|
| 171 |
|
| 172 |
def iterate_pem(certs): |
| 173 |
for match in re.findall(pem_regexp, ensure_str(certs)): |
| 174 |
yield match |
| 175 |
|
| 176 |
|
| 177 |
def hmac_sha1(key, message): |
| 178 |
hasher = hmac.HMAC(key, hashes.SHA1()) |
| 179 |
hasher.update(message) |
| 180 |
return hasher.finalize() |
| 181 |
|
| 182 |
|
| 183 |
def raw_p_sha1(secret, seed, sizes=()): |
| 184 |
""" |
| 185 |
Derive one or more keys from secret and seed. |
| 186 |
(See specs part 6, 6.7.5 and RFC 2246 - TLS v1.0) |
| 187 |
Lengths of keys will match sizes argument |
| 188 |
|
| 189 |
Source: https://github.com/FreeOpcUa/python-opcua |
| 190 |
key_sizes = (signature_key_size, symmetric_key_size, 16) |
| 191 |
(sigkey, key, init_vec) = p_sha1(nonce2, nonce1, key_sizes) |
| 192 |
""" |
| 193 |
full_size = 0 |
| 194 |
for size in sizes: |
| 195 |
full_size += size |
| 196 |
|
| 197 |
result = b"" |
| 198 |
accum = seed |
| 199 |
while len(result) < full_size: |
| 200 |
accum = hmac_sha1(secret, accum) |
| 201 |
result += hmac_sha1(secret, accum + seed) |
| 202 |
|
| 203 |
parts = [] |
| 204 |
for size in sizes: |
| 205 |
parts.append(result[:size]) |
| 206 |
result = result[size:] |
| 207 |
return tuple(parts) |
| 208 |
|
| 209 |
|
| 210 |
def p_sha1(client_b64_bytes, server_b64_bytes): |
| 211 |
client_bytes, server_bytes = b64decode(client_b64_bytes), b64decode(server_b64_bytes) |
| 212 |
return b64encode(raw_p_sha1(client_bytes, server_bytes, (len(client_bytes), len(server_bytes)))[0]).decode() |
| 213 |
|
| 214 |
|
| 215 |
class X509CertChainVerifier: |
| 216 |
""" |
| 217 |
Look at certs in the cert chain and add them to the store one by one. |
| 218 |
Return the cert at the end of the chain. That is the cert to be used by the caller for verifying. |
| 219 |
From https://www.w3.org/TR/xmldsig-core2/#sec-X509Data: |
| 220 |
"All certificates appearing in an X509Data element must relate to the validation key by either containing it |
| 221 |
or being part of a certification chain that terminates in a certificate containing the validation key. |
| 222 |
No ordering is implied by the above constraints" |
| 223 |
|
| 224 |
Note: SignXML no longer uses OpenSSL for certificate chain verificaiton. The CApath parameter supported by OpenSSL |
| 225 |
is not supported by cryptography. The CApath parameter is used to specify a directory containing CA certificates in |
| 226 |
PEM format. The files each contain one CA certificate. The files are looked up by the CA subject name hash value. |
| 227 |
See https://docs.openssl.org/master/man3/SSL_CTX_load_verify_locations/#notes. If you need CApath support, please |
| 228 |
contact SignXML maintainers. |
| 229 |
""" |
| 230 |
|
| 231 |
def __init__(self, ca_pem_file=None, verification_time=None): |
| 232 |
if ca_pem_file is None: |
| 233 |
ca_pem_file = certifi.where() |
| 234 |
self.ca_pem_file = ca_pem_file |
| 235 |
self.verification_time = verification_time |
| 236 |
|
| 237 |
@property |
| 238 |
def store(self): |
| 239 |
with open(self.ca_pem_file, "rb") as pems: |
| 240 |
certs = x509.load_pem_x509_certificates(pems.read()) |
| 241 |
return x509.verification.Store(certs) |
| 242 |
|
| 243 |
@property |
| 244 |
def builder(self): |
| 245 |
builder = x509.verification.PolicyBuilder() |
| 246 |
builder = builder.store(self.store) |
| 247 |
if self.verification_time is not None: |
| 248 |
builder = builder.time(self.verification_time) |
| 249 |
return builder |
| 250 |
|
| 251 |
@property |
| 252 |
def verifier(self): |
| 253 |
return self.builder.build_client_verifier() |
| 254 |
|
| 255 |
def _do_verify(self, cert_chain): |
| 256 |
leaf, intermediates = cert_chain[0], cert_chain[1:] |
| 257 |
result = self.verifier.verify(leaf=leaf, intermediates=intermediates) |
| 258 |
return result.chain[0] |
| 259 |
|
| 260 |
def verify(self, cert_chain): |
| 261 |
try: |
| 262 |
return self._do_verify(cert_chain) |
| 263 |
except x509.verification.VerificationError: |
| 264 |
try: |
| 265 |
return self._do_verify(list(reversed(cert_chain))) |
| 266 |
except x509.verification.VerificationError as e: |
| 267 |
raise InvalidCertificate(e) |
| 268 |
|
| 269 |
|
| 270 |
def _remove_sig(signature, idempotent=False): |
| 271 |
""" |
| 272 |
Remove the signature node from its parent, keeping any tail element. |
| 273 |
This is needed for eneveloped signatures. |
| 274 |
|
| 275 |
:param signature: Signature to remove from payload |
| 276 |
:type signature: XML ElementTree Element |
| 277 |
:param idempotent: |
| 278 |
If True, don't raise an error if signature is already detached from parent. |
| 279 |
:type idempotent: boolean |
| 280 |
""" |
| 281 |
try: |
| 282 |
signaturep = next(signature.iterancestors()) |
| 283 |
except StopIteration: |
| 284 |
if idempotent: |
| 285 |
return |
| 286 |
raise ValueError("Can't remove the root signature node") |
| 287 |
if signature.tail is not None: |
| 288 |
try: |
| 289 |
signatures = next(signature.itersiblings(preceding=True)) |
| 290 |
except StopIteration: |
| 291 |
if signaturep.text is not None: |
| 292 |
signaturep.text = signaturep.text + signature.tail |
| 293 |
else: |
| 294 |
signaturep.text = signature.tail |
| 295 |
else: |
| 296 |
if signatures.tail is not None: |
| 297 |
signatures.tail = signatures.tail + signature.tail |
| 298 |
else: |
| 299 |
signatures.tail = signature.tail |
| 300 |
signaturep.remove(signature) |