Git init
[external/xmlsec1.git] / docs / api / xmlsec-notes-simple-keys-store.html
1 <html>
2 <head>
3 <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
4 <title>Simple keys store.</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="Keys manager." href="xmlsec-notes-keysmngr.html">
9 <link rel="NEXT" title="Using keys manager for signatures/encryption." href="xmlsec-notes-keys-manager-sign-enc.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-keysmngr.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-manager-sign-enc.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-SIMPLE-KEYS-STORE">Simple keys store.</a></h1>
89 <p>     XML Security Library has a built-in simple keys store 
90         implemented using a keys list. You can use it in your application
91         if you have a small number of keys. However, this might be not a 
92         best option from performance point of view if you have a lot of keys.
93         In this case, you probably should implement your own keys store
94         using an SQL database or some other keys storage.
95         </p>
96 <p>          </p>
97 <div class="EXAMPLE">
98 <a name="AEN363"></a><p><b>Example 1. Initializing keys manager and loading keys from PEM files.</b></p>
99 <pre class="PROGRAMLISTING">/**
100  * load_keys:
101  * @files:              the list of filenames.
102  * @files_size:         the number of filenames in #files.
103  *
104  * Creates default keys manager and load PEM keys from #files in it.
105  * The caller is responsible for destroing returned keys manager using
106  * @xmlSecKeysMngrDestroy.
107  *
108  * Returns the pointer to newly created keys manager or NULL if an error
109  * occurs.
110  */
111 xmlSecKeysMngrPtr 
112 load_keys(char** files, int files_size) {
113     xmlSecKeysMngrPtr mngr;
114     xmlSecKeyPtr key;
115     int i;
116     
117     assert(files);
118     assert(files_size &gt; 0);
119     
120     /* create and initialize keys manager, we use a default list based
121      * keys manager, implement your own xmlSecKeysStore klass if you need
122      * something more sophisticated 
123      */
124     mngr = xmlSecKeysMngrCreate();
125     if(mngr == NULL) {
126         fprintf(stderr, "Error: failed to create keys manager.\n");
127         return(NULL);
128     }
129     if(xmlSecCryptoAppDefaultKeysMngrInit(mngr) &lt; 0) {
130         fprintf(stderr, "Error: failed to initialize keys manager.\n");
131         xmlSecKeysMngrDestroy(mngr);
132         return(NULL);
133     }    
134     
135     for(i = 0; i &lt; files_size; ++i) {
136         assert(files[i]);
137
138         /* load key */
139         key = xmlSecCryptoAppKeyLoad(files[i], xmlSecKeyDataFormatPem, NULL, NULL, NULL);
140         if(key == NULL) {
141             fprintf(stderr,"Error: failed to load pem key from \"%s\"\n", files[i]);
142             xmlSecKeysMngrDestroy(mngr);
143             return(NULL);
144         }
145
146         /* set key name to the file name, this is just an example! */
147         if(xmlSecKeySetName(key, BAD_CAST files[i]) &lt; 0) {
148             fprintf(stderr,"Error: failed to set key name for key from \"%s\"\n", files[i]);
149             xmlSecKeyDestroy(key);
150             xmlSecKeysMngrDestroy(mngr);
151             return(NULL);
152         }
153         
154         /* add key to keys manager, from now on keys manager is responsible 
155          * for destroying key 
156          */
157         if(xmlSecCryptoAppDefaultKeysMngrAdoptKey(mngr, key) &lt; 0) {
158             fprintf(stderr,"Error: failed to add key from \"%s\" to keys manager\n", files[i]);
159             xmlSecKeyDestroy(key);
160             xmlSecKeysMngrDestroy(mngr);
161             return(NULL);
162         }
163     }
164
165     return(mngr);
166 }
167                 </pre>
168 <p><a href="xmlsec-verify-with-keys-mngr.html#XMLSEC-EXAMPLE-VERIFY2">Full program listing</a></p>
169 </div>
170         </div>
171 <table class="navigation" width="100%" summary="Navigation footer" cellpadding="2" cellspacing="2"><tr valign="middle">
172 <td align="left"><a accesskey="p" href="xmlsec-notes-keysmngr.html"><b>&lt;&lt;&lt; Keys manager.</b></a></td>
173 <td align="right"><a accesskey="n" href="xmlsec-notes-keys-manager-sign-enc.html"><b>Using keys manager for signatures/encryption. &gt;&gt;&gt;</b></a></td>
174 </tr></table>
175 </td></tr></table></td>
176 </tr></table></body>
177 </html>