Skip to content

[ADD] issuer info #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 86 additions & 1 deletion signxml/xades/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
from lxml.builder import ElementMaker
from defusedxml.lxml import fromstring

from OpenSSL.crypto import X509Name

from cryptography.hazmat import _der
from cryptography.hazmat.primitives.asymmetric import rsa, dsa, ec
from cryptography.hazmat.primitives.asymmetric.padding import PKCS1v15
from cryptography.hazmat.primitives.hashes import Hash, SHA1, SHA224, SHA256, SHA384, SHA512
Expand Down Expand Up @@ -57,6 +60,15 @@ def namedtuple_with_defaults(typename, field_names, default_values=()):

# helper functions

class X509NamePKCS12(X509Name):
def _issuer_string(self):
x509name = self.get_components()
x509name.reverse()
return ",".join([f"{var.decode()}={alias.decode()}" for var, alias in x509name])

def issuer_string(certificate):
return X509NamePKCS12(certificate.get_issuer())._issuer_string()

def _gen_id(prefix, suffix=None):
"""
Generates the id
Expand All @@ -75,7 +87,6 @@ def resolve_uri(uri):
except:
raise InvalidInput(f"Unable to resolve reference URI: {uri}")


class XAdESProcessor(XMLSignatureProcessor):
schema_file = "v1.4.1/XAdES01903v141-201601.xsd"

Expand Down Expand Up @@ -588,6 +599,37 @@ def _generate_xades(self, options_struct):
up := UnsignedProperties, c. 4.3.3
usp := UnignedSignatureProperties, c. 4.3.6
udop := UnignedDataObjectProperties, c. 4.3.7

Electronic signature forms elements

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"Electronic signature grammar (I guess that's what it is) by mode"

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm ok with that, just those ? look a little grammar-ish. But yep, in xml, that is probably not a common usage of a word...

Mention differences between nodes

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nodes -> modes

XaDES-BES
ssp:
(SigningTime)?
(SigningCertificate)?
(SignatureProductionPlace)?
(SignerRole)?

XaDES-EPES:
ssp:
(SigningTime)?
(SigningCertificate)?
(SignaturePolicyIdentifier)
(SignatureProductionPlace)?
(SignerRole)?

XaDES-T:
usp:
(CounterSignature)*
(SignatureTimeStamp)+

XaDES-C:
usp:
(CounterSignature)
(SignatureTimeStamp)
(CompleteCertificateRefs)
(CompleteRevocationRefs)
(AttributeCertificateRefs)?
(AttributeRevocationRefs)?
"""

ssp_elements = self._generate_xades_ssp_elements(options_struct)
Expand Down Expand Up @@ -712,3 +754,46 @@ def _generate_xades(self, options_struct):
qp = XADES.QualifyingProperties(*qp_elements, **qp_attributes)
self._add_xades_reference(qp)
return qp


class XAdESEPESSigner(XAdESSigner):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be an excellent idea, I have to think around it a bit.. Thanks 👍


def _generate_xades_ssp_elements(self, options_struct):
# Item 4: SignaturePolicyIdentifier
"""
The SignaturePolicyIdentifier qualifying property shall be a signed
qualifying property qualifying the signature.
The SignaturePolicyIdentifier qualifying property shall contain either
an explicit identifier of a signature policy or an indication that
there is an implied signature policy that the relying party should
be aware of.

ETSI TS 119 172-1 specifies a framework for signature policies.
"""
elements = super(XAdESEPESSigner, self)._generate_xades_ssp_elements(options_struct)
spid_elements = []
sp = options_struct.SignaturePolicy
# digest method
pdm = self.known_digest_tags.get(self.digest_alg)
# digest value
pv = resolve_uri(sp.Identifier)

spid_elements.append(
XADES132.SigPolicyId(
XADES132.Identifier(sp.Identifier),
XADES132.Description(sp.Description)
)
)
spid_elements.append(
XADES132.SigPolicyHash(
DS.DigestMethod(Algorithm=pdm),
DS.DigestValue(
self._get_digest(pv,self._get_digest_method_by_tag(self.digest_alg))
)
)
)
spid = XADES132.SignaturePolicyId(*spid_elements)
spi_elements = []
spi_elements.append(spid)
elements.append(XADES132.SignaturePolicyIdentifier(*spi_elements))
return elements