@@ -158,6 +158,70 @@ def test_sign_generated_template_pem_with_x509():
158158 compare ('sign3-res.xml' , template )
159159
160160
161+ def test_sign_generated_template_pem_with_x509_with_custom_ns ():
162+ """
163+ Should sign a file using a dynamicaly created template, key from PEM
164+ file and an X509 certificate with custom ns.
165+ """
166+
167+ # Load document file.
168+ template = parse_xml ('sign4-doc.xml' )
169+ xmlsec .tree .add_ids (template , ["ID" ])
170+ elem_id = template .get ('ID' , None )
171+ if elem_id :
172+ elem_id = '#' + elem_id
173+ # Create a signature template for RSA-SHA1 enveloped signature.
174+ signature_node = xmlsec .template .create (
175+ template ,
176+ xmlsec .Transform .EXCL_C14N ,
177+ xmlsec .Transform .RSA_SHA1 , ns = 'ds' )
178+
179+ assert signature_node is not None
180+
181+ # Add the <ds:Signature/> node to the document.
182+ template .append (signature_node )
183+
184+ # Add the <ds:Reference/> node to the signature template.
185+ ref = xmlsec .template .add_reference (signature_node , xmlsec .Transform .SHA1 , uri = elem_id )
186+
187+ # Add the enveloped transform descriptor.
188+ xmlsec .template .add_transform (ref , xmlsec .Transform .ENVELOPED )
189+ # Add the excl_c14n transform descriptor.
190+ xmlsec .template .add_transform (ref , xmlsec .Transform .EXCL_C14N )
191+
192+ # Add the <ds:KeyInfo/> and <ds:KeyName/> nodes.
193+ key_info = xmlsec .template .ensure_key_info (signature_node )
194+ xmlsec .template .add_x509_data (key_info )
195+
196+ # Create a digital signature context (no key manager is needed).
197+ ctx = xmlsec .SignatureContext ()
198+
199+ # Load private key (assuming that there is no password).
200+ filename = path .join (BASE_DIR , 'rsakey.pem' )
201+ key = xmlsec .Key .from_file (filename , xmlsec .KeyFormat .PEM )
202+
203+ assert key is not None
204+
205+ # Load the certificate and add it to the key.
206+ filename = path .join (BASE_DIR , 'rsacert.pem' )
207+ key .load_cert_from_file (filename , xmlsec .KeyFormat .PEM )
208+
209+ # Set key name to the file name (note: this is just a test).
210+ key .name = path .basename (filename )
211+
212+ # Set the key on the context.
213+ ctx .key = key
214+
215+ assert ctx .key is not None
216+ assert ctx .key .name == path .basename (filename )
217+
218+ # Sign the template.
219+ ctx .sign (signature_node )
220+
221+ # Assert the contents of the XML document against the expected result.
222+ compare ('sign4-res.xml' , template )
223+
224+
161225def test_sign_binary ():
162226 ctx = xmlsec .SignatureContext ()
163227 filename = path .join (BASE_DIR , 'rsakey.pem' )
0 commit comments