3 <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
4 <title>Using keys manager for signatures/encryption.</title>
5 <meta name="GENERATOR" content="Modular DocBook HTML Stylesheet Version 1.79">
6 <link rel="HOME" title="XML Security Library Reference Manual" href="index.html">
7 <link rel="UP" title="Keys manager." href="xmlsec-notes-keysmngr.html">
8 <link rel="PREVIOUS" title="Simple keys store." href="xmlsec-notes-simple-keys-store.html">
9 <link rel="NEXT" title="Using keys manager for verification/decryption." href="xmlsec-notes-keys-mngr-verify-decrypt.html">
10 <style type="text/css">.synopsis, .classsynopsis {
12 border: solid 1px #aaaaaa;
17 border: solid 1px #aaaaff;
26 border: solid 1px #ffaaaa;
33 .navigation a:visited {
40 <body><table witdh="100%" valign="top"><tr valign="top">
41 <td valign="top" align="left" width="210">
42 <img src="../images/logo.gif" alt="XML Security Library" border="0"><p></p>
44 <li><a href="../index.html">Home</a></li>
45 <li><a href="../download.html">Download</a></li>
46 <li><a href="../news.html">News</a></li>
47 <li><a href="../documentation.html">Documentation</a></li>
49 <li><a href="../faq.html">FAQ</a></li>
50 <li><a href="../api/xmlsec-notes.html">Tutorial</a></li>
51 <li><a href="../api/xmlsec-reference.html">API reference</a></li>
52 <li><a href="../api/xmlsec-examples.html">Examples</a></li>
54 <li><a href="../xmldsig.html">XML Digital Signature</a></li>
55 <ul><li><a href="http://www.aleksey.com/xmlsec/xmldsig-verifier.html">Online Verifier</a></li></ul>
56 <li><a href="../xmlenc.html">XML Encryption</a></li>
57 <li><a href="../c14n.html">XML Canonicalization</a></li>
58 <li><a href="../bugs.html">Reporting Bugs</a></li>
59 <li><a href="http://www.aleksey.com/pipermail/xmlsec">Mailing list</a></li>
60 <li><a href="../related.html">Related</a></li>
61 <li><a href="../authors.html">Authors</a></li>
66 <td><a href="http://xmlsoft.org/"><img src="../images/libxml2-logo.png" alt="LibXML2" border="0"></a></td>
70 <td><a href="http://xmlsoft.org/XSLT"><img src="../images/libxslt-logo.png" alt="LibXSLT" border="0"></a></td>
74 <td><a href="http://www.openssl.org/"><img src="../images/openssl-logo.png" alt="OpenSSL" border="0"></a></td>
76 <!--Links - start--><!--Links - end-->
79 <td valign="top"><table width="100%" valign="top"><tr><td valign="top" align="left" id="xmlsecContent">
80 <table width="100%" class="navigation" summary="Navigation header" cellpadding="2" cellspacing="2"><tr valign="middle">
81 <td><a accesskey="p" href="xmlsec-notes-simple-keys-store.html"><img src="left.png" width="24" height="24" border="0" alt="Prev"></a></td>
82 <td><a accesskey="u" href="xmlsec-notes-keysmngr.html"><img src="up.png" width="24" height="24" border="0" alt="Up"></a></td>
83 <td><a accesskey="h" href="index.html"><img src="home.png" width="24" height="24" border="0" alt="Home"></a></td>
84 <th width="100%" align="center">XML Security Library Reference Manual</th>
85 <td><a accesskey="n" href="xmlsec-notes-keys-mngr-verify-decrypt.html"><img src="right.png" width="24" height="24" border="0" alt="Next"></a></td>
87 <br clear="all"><div class="SECT1">
88 <h1 class="SECT1"><a name="XMLSEC-NOTES-KEYS-MANAGER-SIGN-ENC">Using keys manager for signatures/encryption.</a></h1>
89 <p>Instead of specifiying signature or encryption key in the
90 corresponding context object (<code class="STRUCTFIELD">signKey</code>
91 member of <a href="xmlsec-xmldsig.html#XMLSECDSIGCTX">xmlSecDSigCtx</a>
92 structure or <code class="STRUCTFIELD">encKey</code> member of
93 <a href="xmlsec-xmlenc.html#XMLSECENCCTX">xmlSecEncCtx</a> structure),
94 the application can use keys manager to select the
95 signature or encryption key. This is especialy useful
96 when you are encrypting or signing something with a session key
97 which is by itself should be encrypted. The key for the
98 session key encryption in the
99 <a href="http://www.w3.org/TR/xmlenc-core/#sec-EncryptedKey" target="_top"><EncryptedKey/></a>
100 node could be selected using
101 <a href="http://www.w3.org/TR/xmldsig-core/#sec-KeyName" target="_top"><dsig:KeyName/></a>
102 node in the template.
105 <div class="EXAMPLE">
106 <a name="AEN378"></a><p><b>Example 2. Encrypting file using a session key and a permanent key from keys manager.</b></p>
107 <pre class="PROGRAMLISTING">/**
109 * @key_file: the key filename.
111 * Creates default keys manager and load RSA key from #key_file in it.
112 * The caller is responsible for destroing returned keys manager using
113 * @xmlSecKeysMngrDestroy.
115 * Returns the pointer to newly created keys manager or NULL if an error
119 load_rsa_keys(char* key_file) {
120 xmlSecKeysMngrPtr mngr;
125 /* create and initialize keys manager, we use a default list based
126 * keys manager, implement your own xmlSecKeysStore klass if you need
127 * something more sophisticated
129 mngr = xmlSecKeysMngrCreate();
131 fprintf(stderr, "Error: failed to create keys manager.\n");
134 if(xmlSecCryptoAppDefaultKeysMngrInit(mngr) < 0) {
135 fprintf(stderr, "Error: failed to initialize keys manager.\n");
136 xmlSecKeysMngrDestroy(mngr);
140 /* load private RSA key */
141 key = xmlSecCryptoAppKeyLoad(key_file, xmlSecKeyDataFormatPem, NULL, NULL, NULL);
143 fprintf(stderr,"Error: failed to load rsa key from file \"%s\"\n", key_file);
144 xmlSecKeysMngrDestroy(mngr);
148 /* set key name to the file name, this is just an example! */
149 if(xmlSecKeySetName(key, BAD_CAST key_file) < 0) {
150 fprintf(stderr,"Error: failed to set key name for key from \"%s\"\n", key_file);
151 xmlSecKeyDestroy(key);
152 xmlSecKeysMngrDestroy(mngr);
156 /* add key to keys manager, from now on keys manager is responsible
159 if(xmlSecCryptoAppDefaultKeysMngrAdoptKey(mngr, key) < 0) {
160 fprintf(stderr,"Error: failed to add key from \"%s\" to keys manager\n", key_file);
161 xmlSecKeyDestroy(key);
162 xmlSecKeysMngrDestroy(mngr);
171 * @mngr: the pointer to keys manager.
172 * @xml_file: the encryption template file name.
173 * @key_name: the RSA key name.
175 * Encrypts #xml_file using a dynamicaly created template, a session DES key
176 * and an RSA key from keys manager.
178 * Returns 0 on success or a negative value if an error occurs.
181 encrypt_file(xmlSecKeysMngrPtr mngr, const char* xml_file, const char* key_name) {
182 xmlDocPtr doc = NULL;
183 xmlNodePtr encDataNode = NULL;
184 xmlNodePtr keyInfoNode = NULL;
185 xmlNodePtr encKeyNode = NULL;
186 xmlNodePtr keyInfoNode2 = NULL;
187 xmlSecEncCtxPtr encCtx = NULL;
195 doc = xmlParseFile(xml_file);
196 if ((doc == NULL) || (xmlDocGetRootElement(doc) == NULL)){
197 fprintf(stderr, "Error: unable to parse file \"%s\"\n", xml_file);
201 /* create encryption template to encrypt XML file and replace
202 * its content with encryption result */
203 encDataNode = xmlSecTmplEncDataCreate(doc, xmlSecTransformDes3CbcId,
204 NULL, xmlSecTypeEncElement, NULL, NULL);
205 if(encDataNode == NULL) {
206 fprintf(stderr, "Error: failed to create encryption template\n");
210 /* we want to put encrypted data in the <enc:CipherValue/> node */
211 if(xmlSecTmplEncDataEnsureCipherValue(encDataNode) == NULL) {
212 fprintf(stderr, "Error: failed to add CipherValue node\n");
216 /* add <dsig:KeyInfo/> */
217 keyInfoNode = xmlSecTmplEncDataEnsureKeyInfo(encDataNode, NULL);
218 if(keyInfoNode == NULL) {
219 fprintf(stderr, "Error: failed to add key info\n");
223 /* add <enc:EncryptedKey/> to store the encrypted session key */
224 encKeyNode = xmlSecTmplKeyInfoAddEncryptedKey(keyInfoNode,
225 xmlSecTransformRsaOaepId,
227 if(encKeyNode == NULL) {
228 fprintf(stderr, "Error: failed to add key info\n");
232 /* we want to put encrypted key in the <enc:CipherValue/> node */
233 if(xmlSecTmplEncDataEnsureCipherValue(encKeyNode) == NULL) {
234 fprintf(stderr, "Error: failed to add CipherValue node\n");
238 /* add <dsig:KeyInfo/> and <dsig:KeyName/> nodes to <enc:EncryptedKey/> */
239 keyInfoNode2 = xmlSecTmplEncDataEnsureKeyInfo(encKeyNode, NULL);
240 if(keyInfoNode2 == NULL) {
241 fprintf(stderr, "Error: failed to add key info\n");
245 /* set key name so we can lookup key when needed */
246 if(xmlSecTmplKeyInfoAddKeyName(keyInfoNode2, key_name) == NULL) {
247 fprintf(stderr, "Error: failed to add key name\n");
251 /* create encryption context */
252 encCtx = xmlSecEncCtxCreate(mngr);
254 fprintf(stderr,"Error: failed to create encryption context\n");
258 /* generate a Triple DES key */
259 encCtx->encKey = xmlSecKeyGenerate(xmlSecKeyDataDesId, 192, xmlSecKeyDataTypeSession);
260 if(encCtx->encKey == NULL) {
261 fprintf(stderr,"Error: failed to generate session des key\n");
265 /* encrypt the data */
266 if(xmlSecEncCtxXmlEncrypt(encCtx, encDataNode, xmlDocGetRootElement(doc)) < 0) {
267 fprintf(stderr,"Error: encryption failed\n");
271 /* we template is inserted in the doc */
274 /* print encrypted data with document to stdout */
275 xmlDocDump(stdout, doc);
284 xmlSecEncCtxDestroy(encCtx);
287 if(encDataNode != NULL) {
288 xmlFreeNode(encDataNode);
298 <p><a href="xmlsec-encrypt-with-session-key.html#XMLSEC-EXAMPLE-ENCRYPT3">Full program listing</a></p>
301 <table class="navigation" width="100%" summary="Navigation footer" cellpadding="2" cellspacing="2"><tr valign="middle">
302 <td align="left"><a accesskey="p" href="xmlsec-notes-simple-keys-store.html"><b><<< Simple keys store.</b></a></td>
303 <td align="right"><a accesskey="n" href="xmlsec-notes-keys-mngr-verify-decrypt.html"><b>Using keys manager for verification/decryption. >>></b></a></td>
305 </td></tr></table></td>