Skip to content

Instantly share code, notes, and snippets.

@drawcode
Last active June 26, 2019 07:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save drawcode/6f593e3cc4c13ec2d7ef056e02f1fc06 to your computer and use it in GitHub Desktop.
Save drawcode/6f593e3cc4c13ec2d7ef056e02f1fc06 to your computer and use it in GitHub Desktop.
xmldsig.md
<?xml version="1.0" encoding="UTF-8"?>
<Signature xmlns="http://www.w3.org/2000/09/xmldsig#">
<SignedInfo>
<CanonicalizationMethod Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315" />
<SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1" />
<Reference URI="#object">
<DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1" />
<DigestValue>OPnpF/ZNLDxJ/I+1F3iHhlmSwgo=</DigestValue>
</Reference>
</SignedInfo>
<SignatureValue>nihUFQg4mDhLgecvhIcKb9Gz8VRTOlw+adiZOBBXgK4JodEe5aFfCqm8WcRIT8GL
LXSk8PsUP4//SsKqUBQkpotcAqQAhtz2v9kCWdoUDnAOtFZkd/CnsZ1sge0ndha4
0wWDV+nOWyJxkYgicvB8POYtSmldLLepPGMz+J7/Uws=</SignatureValue>
<KeyInfo>
<KeyValue>
<RSAKeyValue>
<Modulus>4IlzOY3Y9fXoh3Y5f06wBbtTg94Pt6vcfcd1KQ0FLm0S36aGJtTSb6pYKfyX7PqC
UQ8wgL6xUJ5GRPEsu9gyz8ZobwfZsGCsvu40CWoT9fcFBZPfXro1Vtlh/xl/yYHm
+Gzqh0Bw76xtLHSfLfpVOrmZdwKmSFKMTvNXOFd0V18=</Modulus>
<Exponent>AQAB</Exponent>
</RSAKeyValue>
</KeyValue>
</KeyInfo>
<Object Id="object">some text
with spaces and CR-LF.</Object>
</Signature>

Procedure

Algorithm: XMLDSIG of simple text string.

INPUT:

T, text-to-be-signed, a byte string;

Ks, RSA private key;

OUTPUT: XML file, xml

  • Canonicalize* the text-to-be-signed, C = C14n(T).

  • Compute the message digest of the canonicalized text, m = Hash(C).

  • Encapsulate the message digest in an XML element, SI, in canonicalized form.

  • Compute the RSA signatureValue of the canonicalized element, SV = RsaSign(Ks, SI).

  • Compose the final XML document including the signatureValue, this time in non-canonicalized form.

  • Strictly, what we are doing here is encapsulating the text string T inside an element, then canonicalizing that element.

    CANONICALIZATION

    This is the official (2001) outline of the procedure for c14n, taken from [XML-C14N]:

    • The document is encoded in UTF-8
    • Line breaks normalized to #xA on input, before parsing
    • Attribute values are normalized, as if by a validating processor
    • Character and parsed entity references are replaced
    • CDATA sections are replaced with their character content
    • The XML declaration and document type declaration (DTD) are removed
    • Empty elements are converted to start-end tag pairs
    • Whitespace outside of the document element and within start and end tags is normalized
    • All whitespace in character content is retained (excluding characters removed during line feed normalization)
    • Attribute value delimiters are set to quotation marks (double quotes)
    • Special characters in attribute values and character content are replaced by character references
    • Superfluous namespace declarations are removed from each element
    • Default attributes are added to each element
    • Lexicographic order is imposed on the namespace declarations and attributes of each element

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment