Git init
[external/xmlsec1.git] / docs / api / xmlsec-notes-keys-manager-sign-enc.html
1 <html>
2 <head>
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 {
11     background: #eeeeee;
12     border: solid 1px #aaaaaa;
13     padding: 0.5em;
14 }
15 .programlisting {
16     background: #eeeeff;
17     border: solid 1px #aaaaff;
18     padding: 0.5em;
19 }
20 .variablelist {
21     padding: 4px;
22     margin-left: 3em;
23 }
24 .navigation {
25     background: #ffeeee;
26     border: solid 1px #ffaaaa;
27     margin-top: 0.5em;
28     margin-bottom: 0.5em;
29 }
30 .navigation a {
31     color: #770000;
32 }
33 .navigation a:visited {
34     color: #550000;
35 }
36 .navigation .title {
37     font-size: 200%;
38 }</style>
39 </head>
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>
43 <ul>
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>
48 <ul>
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>
53 </ul>
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>
62 </ul>
63 <table width="100%">
64 <tr>
65 <td width="15"></td>
66 <td><a href="http://xmlsoft.org/"><img src="../images/libxml2-logo.png" alt="LibXML2" border="0"></a></td>
67 </tr>
68 <tr>
69 <td width="15"></td>
70 <td><a href="http://xmlsoft.org/XSLT"><img src="../images/libxslt-logo.png" alt="LibXSLT" border="0"></a></td>
71 </tr>
72 <tr>
73 <td width="15"></td>
74 <td><a href="http://www.openssl.org/"><img src="../images/openssl-logo.png" alt="OpenSSL" border="0"></a></td>
75 </tr>
76 <!--Links - start--><!--Links - end-->
77 </table>
78 </td>
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>
86 </tr></table>
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">&lt;EncryptedKey/&gt;</a>
100         node could be selected using
101         <a href="http://www.w3.org/TR/xmldsig-core/#sec-KeyName" target="_top">&lt;dsig:KeyName/&gt;</a> 
102         node in the template.
103         </p>
104 <p>          </p>
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">/**
108  * load_rsa_keys:
109  * @key_file:           the key filename.
110  *
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.
114  *
115  * Returns the pointer to newly created keys manager or NULL if an error
116  * occurs.
117  */
118 xmlSecKeysMngrPtr 
119 load_rsa_keys(char* key_file) {
120     xmlSecKeysMngrPtr mngr;
121     xmlSecKeyPtr key;
122     
123     assert(key_file);
124     
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 
128      */
129     mngr = xmlSecKeysMngrCreate();
130     if(mngr == NULL) {
131         fprintf(stderr, "Error: failed to create keys manager.\n");
132         return(NULL);
133     }
134     if(xmlSecCryptoAppDefaultKeysMngrInit(mngr) &lt; 0) {
135         fprintf(stderr, "Error: failed to initialize keys manager.\n");
136         xmlSecKeysMngrDestroy(mngr);
137         return(NULL);
138     }    
139     
140     /* load private RSA key */
141     key = xmlSecCryptoAppKeyLoad(key_file, xmlSecKeyDataFormatPem, NULL, NULL, NULL);
142     if(key == NULL) {
143         fprintf(stderr,"Error: failed to load rsa key from file \"%s\"\n", key_file);
144         xmlSecKeysMngrDestroy(mngr);
145         return(NULL);
146     }
147
148     /* set key name to the file name, this is just an example! */
149     if(xmlSecKeySetName(key, BAD_CAST key_file) &lt; 0) {
150         fprintf(stderr,"Error: failed to set key name for key from \"%s\"\n", key_file);
151         xmlSecKeyDestroy(key);  
152         xmlSecKeysMngrDestroy(mngr);
153         return(NULL);
154     }
155         
156     /* add key to keys manager, from now on keys manager is responsible 
157      * for destroying key 
158      */
159     if(xmlSecCryptoAppDefaultKeysMngrAdoptKey(mngr, key) &lt; 0) {
160         fprintf(stderr,"Error: failed to add key from \"%s\" to keys manager\n", key_file);
161         xmlSecKeyDestroy(key);
162         xmlSecKeysMngrDestroy(mngr);
163         return(NULL);
164     }
165
166     return(mngr);
167 }
168
169 /**
170  * encrypt_file:
171  * @mngr:               the pointer to keys manager.
172  * @xml_file:           the encryption template file name.
173  * @key_name:           the RSA key name.
174  *
175  * Encrypts #xml_file using a dynamicaly created template, a session DES key 
176  * and an RSA key from keys manager.
177  *
178  * Returns 0 on success or a negative value if an error occurs.
179  */
180 int 
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;
188     int res = -1;
189     
190     assert(mngr);
191     assert(xml_file);
192     assert(key_name);
193
194     /* load template */
195     doc = xmlParseFile(xml_file);
196     if ((doc == NULL) || (xmlDocGetRootElement(doc) == NULL)){
197         fprintf(stderr, "Error: unable to parse file \"%s\"\n", xml_file);
198         goto done;      
199     }
200     
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");
207         goto done;   
208     }
209
210     /* we want to put encrypted data in the &lt;enc:CipherValue/&gt; node */
211     if(xmlSecTmplEncDataEnsureCipherValue(encDataNode) == NULL) {
212         fprintf(stderr, "Error: failed to add CipherValue node\n");
213         goto done;   
214     }
215
216     /* add &lt;dsig:KeyInfo/&gt; */
217     keyInfoNode = xmlSecTmplEncDataEnsureKeyInfo(encDataNode, NULL);
218     if(keyInfoNode == NULL) {
219         fprintf(stderr, "Error: failed to add key info\n");
220         goto done;              
221     }
222
223     /* add &lt;enc:EncryptedKey/&gt; to store the encrypted session key */
224     encKeyNode = xmlSecTmplKeyInfoAddEncryptedKey(keyInfoNode, 
225                                     xmlSecTransformRsaOaepId, 
226                                     NULL, NULL, NULL);
227     if(encKeyNode == NULL) {
228         fprintf(stderr, "Error: failed to add key info\n");
229         goto done;              
230     }
231
232     /* we want to put encrypted key in the &lt;enc:CipherValue/&gt; node */
233     if(xmlSecTmplEncDataEnsureCipherValue(encKeyNode) == NULL) {
234         fprintf(stderr, "Error: failed to add CipherValue node\n");
235         goto done;   
236     }
237
238     /* add &lt;dsig:KeyInfo/&gt; and &lt;dsig:KeyName/&gt; nodes to &lt;enc:EncryptedKey/&gt; */
239     keyInfoNode2 = xmlSecTmplEncDataEnsureKeyInfo(encKeyNode, NULL);
240     if(keyInfoNode2 == NULL) {
241         fprintf(stderr, "Error: failed to add key info\n");
242         goto done;              
243     }
244     
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");
248         goto done;              
249     }
250
251     /* create encryption context */
252     encCtx = xmlSecEncCtxCreate(mngr);
253     if(encCtx == NULL) {
254         fprintf(stderr,"Error: failed to create encryption context\n");
255         goto done;
256     }
257
258     /* generate a Triple DES key */
259     encCtx-&gt;encKey = xmlSecKeyGenerate(xmlSecKeyDataDesId, 192, xmlSecKeyDataTypeSession);
260     if(encCtx-&gt;encKey == NULL) {
261         fprintf(stderr,"Error: failed to generate session des key\n");
262         goto done;
263     }
264
265     /* encrypt the data */
266     if(xmlSecEncCtxXmlEncrypt(encCtx, encDataNode, xmlDocGetRootElement(doc)) &lt; 0) {
267         fprintf(stderr,"Error: encryption failed\n");
268         goto done;
269     }
270     
271     /* we template is inserted in the doc */
272     encDataNode = NULL;
273         
274     /* print encrypted data with document to stdout */
275     xmlDocDump(stdout, doc);
276     
277     /* success */
278     res = 0;
279
280 done:    
281
282     /* cleanup */
283     if(encCtx != NULL) {
284         xmlSecEncCtxDestroy(encCtx);
285     }
286
287     if(encDataNode != NULL) {
288         xmlFreeNode(encDataNode);
289     }
290         
291     if(doc != NULL) {
292         xmlFreeDoc(doc); 
293     }
294     return(res);
295 }
296
297                 </pre>
298 <p><a href="xmlsec-encrypt-with-session-key.html#XMLSEC-EXAMPLE-ENCRYPT3">Full program listing</a></p>
299 </div>
300         </div>
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>&lt;&lt;&lt; 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. &gt;&gt;&gt;</b></a></td>
304 </tr></table>
305 </td></tr></table></td>
306 </tr></table></body>
307 </html>