source: TI12-security/trunk/python/Tests/xmlsec/decrypt1.py @ 1415

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/TI12-security/trunk/python/Tests/xmlsec/decrypt1.py@1415
Revision 1415, 3.7 KB checked in by pjkersha, 13 years ago (diff)

Include XML Security test programs.

  • Property svn:executable set to *
Line 
1#!/usr/bin/env python
2#
3# $Id: decrypt1.py,v 1.3 2004/01/25 00:31:12 valos Exp $
4#
5# PyXMLSec example: Decrypting an encrypted file using a single key.
6
7# Decrypts encrypted XML file using a single DES key from a binary file
8#
9# Usage:
10#       ./decrypt1.py <xml-enc> <des-key-file>
11#
12# Example:
13#       ./decrypt1.py encrypt1-res.xml deskey.bin
14#       ./decrypt1.py encrypt2-res.xml deskey.bin
15#
16# This is free software; see COPYING file in the source
17# distribution for preciese wording.
18#
19# Copyright (C) 2003-2004 Valery Febvre <vfebvre@easter-eggs.com>
20#
21
22import sys
23sys.path.insert(0, '../')
24
25import libxml2
26import xmlsec
27
28def main():
29    assert(sys.argv)
30    if len(sys.argv) < 3:
31        print "Error: wrong number of arguments."
32        print "Usage: %s <xml-tmpl> <des-key-file>" % sys.argv[0]
33        return sys.exit(1)
34   
35    # Init libxml library
36    libxml2.initParser()
37    libxml2.substituteEntitiesDefault(1)
38
39    # Init xmlsec library
40    if xmlsec.init() < 0:
41        print "Error: xmlsec initialization failed."
42        return sys.exit(-1)
43   
44    # Check loaded library version
45    if xmlsec.checkVersion() != 1:
46        print "Error: loaded xmlsec library version is not compatible.\n"
47        sys.exit(-1)
48
49    # Init crypto library
50    if xmlsec.cryptoAppInit(None) < 0:
51        print "Error: crypto initialization failed."
52   
53    # Init xmlsec-crypto library
54    if xmlsec.cryptoInit() < 0:
55        print "Error: xmlsec-crypto initialization failed."
56
57    res = decrypt_file(sys.argv[1], sys.argv[2])
58
59    # Shutdown xmlsec-crypto library
60    xmlsec.cryptoShutdown()
61
62    # Shutdown crypto library
63    xmlsec.cryptoAppShutdown()
64
65    # Shutdown xmlsec library
66    xmlsec.shutdown()
67
68    # Shutdown LibXML2
69    libxml2.cleanupParser()
70
71    sys.exit(res)
72
73
74# Decrypts the XML file enc_file using DES key from key_file and
75# prints results to stdout.
76# Returns 0 on success or a negative value if an error occurs.
77def decrypt_file(enc_file, key_file):
78    assert(enc_file)
79    assert(key_file)
80
81    # Load template
82    doc = libxml2.parseFile(enc_file)
83    if doc is None or doc.getRootElement() is None:
84        print "Error: unable to parse file \"%s\"" % enc_file
85        return cleanup(doc)
86   
87    # Find start node
88    node = xmlsec.findNode(doc.getRootElement(), xmlsec.NodeEncryptedData,
89                           xmlsec.EncNs)
90    if node is None:
91        print "Error: start node not found in \"%s\"" % tmpl_file
92        return cleanup(doc)
93
94    # Create encryption context, we don't need keys manager in this example
95    enc_ctx = xmlsec.EncCtx(None)
96    if enc_ctx is None:
97        print "Error: failed to create encryption context"
98        return cleanup(doc)
99       
100    # Load DES key
101    key = xmlsec.keyReadBinaryFile(xmlsec.keyDataDesId(), key_file)
102    if key is None:
103        print "Error failed to load DES key from binary file \"%s\"" % key_file
104        return cleanup(doc, enc_ctx)
105
106    # Set key name to the file name, this is just an example!
107    if key.setName(key_file) < 0:
108        print "Error: failed to set key name for key from \"%s\"" % key_file
109        return cleanup(doc, enc_ctx)
110
111    enc_ctx.encKey = key
112
113    # Decrypt the data
114    if enc_ctx.decrypt(node) < 0 or enc_ctx.result is None:
115        print "Error: decryption failed"
116        return cleanup(doc, enc_ctx)
117
118    # Print decrypted data to stdout
119    if enc_ctx.resultReplaced != 0:
120        print "Decrypted XML data:"
121        doc.dump("-")
122    else:
123        print "Decrypted binary data (%d bytes):" % enc_ctx.result.getSize()
124        print enc_ctx.result.getData()
125
126    # Success
127    return cleanup(doc, enc_ctx, 1)
128
129
130def cleanup(doc=None, enc_ctx=None, res=-1):
131    if enc_ctx is not None:
132        enc_ctx.destroy()
133    if doc is not None:
134        doc.freeDoc()
135    return res
136
137
138if __name__ == "__main__":
139    main()
Note: See TracBrowser for help on using the repository browser.