~ubuntu-branches/ubuntu/wily/qca2/wily-proposed

« back to all changes in this revision

Viewing changes to qca/apidocs/html/certtest_8cpp-example.html

  • Committer: Bazaar Package Importer
  • Author(s): Jan Niehusmann
  • Date: 2007-10-27 18:51:54 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20071027185154-4ir9ys3h2q9fofrw
Tags: 2.0.0-2
Upload to unstable

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
2
 
<html><head><meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1">
3
 
<title>Qt Cryptographic Architecture: certtest.cpp</title>
4
 
<link href="doxygen.css" rel="stylesheet" type="text/css">
5
 
<link href="tabs.css" rel="stylesheet" type="text/css">
6
 
</head><body>
7
 
<!-- Generated by Doxygen 1.4.6 -->
8
 
<div class="tabs">
9
 
  <ul>
10
 
    <li><a href="index.html"><span>Main&nbsp;Page</span></a></li>
11
 
    <li><a href="namespaces.html"><span>Namespaces</span></a></li>
12
 
    <li><a href="annotated.html"><span>Classes</span></a></li>
13
 
    <li><a href="files.html"><span>Files</span></a></li>
14
 
    <li><a href="dirs.html"><span>Directories</span></a></li>
15
 
    <li><a href="pages.html"><span>Related&nbsp;Pages</span></a></li>
16
 
    <li><a href="examples.html"><span>Examples</span></a></li>
17
 
  </ul></div>
18
 
<h1>certtest.cpp</h1>This example shows how <a class="el" href="classQCA_1_1Certificate.html">QCA::Certificate</a> and <a class="el" href="classQCA_1_1CertificateCollection.html">QCA::CertificateCollection</a> can be used. Note that the argument, if you provide it, must be a PEM encoded file collection.<p>
19
 
<div class="fragment"><pre class="fragment"><a name="l00001"></a>00001 <span class="comment">/*</span>
20
 
<a name="l00002"></a>00002 <span class="comment"> Copyright (C) 2003 Justin Karneges &lt;justin@affinix.com&gt;</span>
21
 
<a name="l00003"></a>00003 <span class="comment"> Copyright (C) 2005 Brad Hards &lt;bradh@frogmouth.net&gt;</span>
22
 
<a name="l00004"></a>00004 <span class="comment"></span>
23
 
<a name="l00005"></a>00005 <span class="comment"> Permission is hereby granted, free of charge, to any person obtaining a copy</span>
24
 
<a name="l00006"></a>00006 <span class="comment"> of this software and associated documentation files (the "Software"), to deal</span>
25
 
<a name="l00007"></a>00007 <span class="comment"> in the Software without restriction, including without limitation the rights</span>
26
 
<a name="l00008"></a>00008 <span class="comment"> to use, copy, modify, merge, publish, distribute, sublicense, and/or sell</span>
27
 
<a name="l00009"></a>00009 <span class="comment"> copies of the Software, and to permit persons to whom the Software is</span>
28
 
<a name="l00010"></a>00010 <span class="comment"> furnished to do so, subject to the following conditions:</span>
29
 
<a name="l00011"></a>00011 <span class="comment"></span>
30
 
<a name="l00012"></a>00012 <span class="comment"> The above copyright notice and this permission notice shall be included in</span>
31
 
<a name="l00013"></a>00013 <span class="comment"> all copies or substantial portions of the Software.</span>
32
 
<a name="l00014"></a>00014 <span class="comment"></span>
33
 
<a name="l00015"></a>00015 <span class="comment"> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR</span>
34
 
<a name="l00016"></a>00016 <span class="comment"> IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,</span>
35
 
<a name="l00017"></a>00017 <span class="comment"> FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE</span>
36
 
<a name="l00018"></a>00018 <span class="comment"> AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN</span>
37
 
<a name="l00019"></a>00019 <span class="comment"> AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN</span>
38
 
<a name="l00020"></a>00020 <span class="comment"> CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.</span>
39
 
<a name="l00021"></a>00021 <span class="comment">*/</span>
40
 
<a name="l00022"></a>00022 
41
 
<a name="l00023"></a>00023 
42
 
<a name="l00024"></a>00024 <span class="preprocessor">#include &lt;QtCrypto&gt;</span>
43
 
<a name="l00025"></a>00025 
44
 
<a name="l00026"></a>00026 <span class="preprocessor">#include &lt;QCoreApplication&gt;</span>
45
 
<a name="l00027"></a>00027 
46
 
<a name="l00028"></a>00028 <span class="preprocessor">#include &lt;iostream&gt;</span>
47
 
<a name="l00029"></a>00029 
48
 
<a name="l00030"></a>00030 <span class="comment">// dump out information about some part of the certificate</span>
49
 
<a name="l00031"></a>00031 <span class="comment">// we use this same approach for information about the subject</span>
50
 
<a name="l00032"></a>00032 <span class="comment">// of the certificate, and also about the issuer of the certificate</span>
51
 
<a name="l00033"></a>00033 <span class="keyword">static</span> <span class="keywordtype">void</span> dumpCertificateInfo( <a class="codeRef" doxygen="qt.tag:" href="qmultimap.html">QCA::CertificateInfo</a> info)
52
 
<a name="l00034"></a>00034 {
53
 
<a name="l00035"></a>00035     std::cout &lt;&lt; <span class="stringliteral">"  Organization: "</span> &lt;&lt; std::endl;
54
 
<a name="l00036"></a>00036 
55
 
<a name="l00037"></a>00037     <span class="comment">// Note that a single certificate can apply to more than one</span>
56
 
<a name="l00038"></a>00038     <span class="comment">// organisation. QCA::Certificate is a multimap, so when you</span>
57
 
<a name="l00039"></a>00039     <span class="comment">// ask for the values associated with a parameter, it returns</span>
58
 
<a name="l00040"></a>00040     <span class="comment">// a list.</span>
59
 
<a name="l00041"></a>00041     <a class="codeRef" doxygen="qt.tag:" href="qlist.html">QList&lt;QString&gt;</a> orgInfoList = info.values(<a name="a27"></a><a class="code" href="namespaceQCA.html#aec56a74cc9f34c93cbde6eabbbe37e8710604bbbb5c5513e2fd83fe27ea986f">QCA::Organization</a>);
60
 
<a name="l00042"></a>00042 
61
 
<a name="l00043"></a>00043     <span class="comment">// foreach() interates over each value in the list, and we dump</span>
62
 
<a name="l00044"></a>00044     <span class="comment">// out each value. Note that is uncommon for a certificate to</span>
63
 
<a name="l00045"></a>00045     <span class="comment">// actually contain multiple values for a single parameter.</span>
64
 
<a name="l00046"></a>00046     <a class="codeRef" doxygen="qt.tag:" href="qstring.html">QString</a> organization;
65
 
<a name="l00047"></a>00047     foreach( organization, orgInfoList ) {
66
 
<a name="l00048"></a>00048         std::cout &lt;&lt; <span class="stringliteral">"    "</span> &lt;&lt; qPrintable(organization) &lt;&lt; std::endl;
67
 
<a name="l00049"></a>00049     }
68
 
<a name="l00050"></a>00050 
69
 
<a name="l00051"></a>00051     std::cout &lt;&lt; <span class="stringliteral">"  Country: "</span> &lt;&lt; std::endl;
70
 
<a name="l00052"></a>00052     <span class="comment">// As above, however this shows a more compact way to represent</span>
71
 
<a name="l00053"></a>00053     <span class="comment">// the iteration and output.</span>
72
 
<a name="l00054"></a>00054     foreach( <a class="codeRef" doxygen="qt.tag:" href="qstring.html">QString</a> country, info.values(<a name="a28"></a><a class="code" href="namespaceQCA.html#aec56a74cc9f34c93cbde6eabbbe37e80f5fbd9076d3230f439f153e77cfb4dd">QCA::Country</a>) ) {
73
 
<a name="l00055"></a>00055         std::cout &lt;&lt; <span class="stringliteral">"    "</span> &lt;&lt; qPrintable(country) &lt;&lt; std::endl;
74
 
<a name="l00056"></a>00056     }
75
 
<a name="l00057"></a>00057 }
76
 
<a name="l00058"></a>00058 
77
 
<a name="l00059"></a>00059 <span class="comment">// This is just a convenience routine</span>
78
 
<a name="l00060"></a>00060 <span class="keyword">static</span> <span class="keywordtype">void</span> dumpSubjectInfo( <a class="codeRef" doxygen="qt.tag:" href="qmultimap.html">QCA::CertificateInfo</a> subject)
79
 
<a name="l00061"></a>00061 {
80
 
<a name="l00062"></a>00062     std::cout &lt;&lt; <span class="stringliteral">"Subject: "</span> &lt;&lt; std::endl;
81
 
<a name="l00063"></a>00063 
82
 
<a name="l00064"></a>00064     dumpCertificateInfo( subject );
83
 
<a name="l00065"></a>00065 }
84
 
<a name="l00066"></a>00066 
85
 
<a name="l00067"></a>00067 
86
 
<a name="l00068"></a>00068 <span class="comment">// This is just a convenience routine</span>
87
 
<a name="l00069"></a>00069 <span class="keyword">static</span> <span class="keywordtype">void</span> dumpIssuerInfo( <a class="codeRef" doxygen="qt.tag:" href="qmultimap.html">QCA::CertificateInfo</a> issuer)
88
 
<a name="l00070"></a>00070 {
89
 
<a name="l00071"></a>00071     std::cout &lt;&lt; <span class="stringliteral">"Issuer: "</span> &lt;&lt; std::endl;
90
 
<a name="l00072"></a>00072 
91
 
<a name="l00073"></a>00073     dumpCertificateInfo( issuer );
92
 
<a name="l00074"></a>00074 }
93
 
<a name="l00075"></a>00075 
94
 
<a name="l00076"></a>00076 
95
 
<a name="l00077"></a>00077 <span class="keywordtype">int</span> main(<span class="keywordtype">int</span> argc, <span class="keywordtype">char</span>** argv)
96
 
<a name="l00078"></a>00078 {
97
 
<a name="l00079"></a>00079     <span class="comment">// the Initializer object sets things up, and</span>
98
 
<a name="l00080"></a>00080     <span class="comment">// also does cleanup when it goes out of scope</span>
99
 
<a name="l00081"></a>00081     <a name="_a29"></a><a class="code" href="classQCA_1_1Initializer.html">QCA::Initializer</a> <a name="a30"></a><a class="code" href="namespaceQCA.html#1de90bf113c54c9e4ffdc5ad784ce629">init</a>;
100
 
<a name="l00082"></a>00082 
101
 
<a name="l00083"></a>00083     <a class="codeRef" doxygen="qt.tag:" href="qcoreapplication.html">QCoreApplication</a> app(argc, argv);
102
 
<a name="l00084"></a>00084 
103
 
<a name="l00085"></a>00085     <span class="comment">// We need to ensure that we have certificate handling support</span>
104
 
<a name="l00086"></a>00086     <span class="keywordflow">if</span> ( !<a name="a31"></a><a class="code" href="namespaceQCA.html#833c9f215544113d52a3a52eedc58620">QCA::isSupported</a>( <span class="stringliteral">"cert"</span> ) ) {
105
 
<a name="l00087"></a>00087         std::cout &lt;&lt; <span class="stringliteral">"Sorry, no PKI certificate support"</span> &lt;&lt; std::endl;
106
 
<a name="l00088"></a>00088         <span class="keywordflow">return</span> 1;
107
 
<a name="l00089"></a>00089     }
108
 
<a name="l00090"></a>00090 
109
 
<a name="l00091"></a>00091     <span class="comment">// We are going to work with a number of certificates, and a</span>
110
 
<a name="l00092"></a>00092     <span class="comment">// QList is a great template class for that</span>
111
 
<a name="l00093"></a>00093     <a class="codeRef" doxygen="qt.tag:" href="qlist.html">QList&lt;QCA::Certificate&gt;</a> certlist;
112
 
<a name="l00094"></a>00094 
113
 
<a name="l00095"></a>00095     <span class="comment">// We do two different cases - if we provide an argument, it is taken</span>
114
 
<a name="l00096"></a>00096     <span class="comment">// as a filename to read the keys from. If there is no argument, we just</span>
115
 
<a name="l00097"></a>00097     <span class="comment">// read from the system store certificates.</span>
116
 
<a name="l00098"></a>00098     <span class="keywordflow">if</span> (argc &gt;= 2) {
117
 
<a name="l00099"></a>00099         <span class="comment">// we are going to read the certificates in using a single call</span>
118
 
<a name="l00100"></a>00100         <span class="comment">// which requires a CertificateCollection.</span>
119
 
<a name="l00101"></a>00101         <a name="_a32"></a><a class="code" href="classQCA_1_1CertificateCollection.html">QCA::CertificateCollection</a> filecerts;
120
 
<a name="l00102"></a>00102         <span class="comment">// The conversion can be tested (although you don't have to) to find out if it</span>
121
 
<a name="l00103"></a>00103         <span class="comment">// worked.</span>
122
 
<a name="l00104"></a>00104         <a class="code" href="namespaceQCA.html#b00c91d4b676b998115216ef0562161d">QCA::ConvertResult</a> importResult;
123
 
<a name="l00105"></a>00105         <span class="comment">// This imports all the PEM encoded certificates from the file specified as the argument</span>
124
 
<a name="l00106"></a>00106         <span class="comment">// Note that you pass in a pointer to the result argument.</span>
125
 
<a name="l00107"></a>00107         filecerts = <a name="a33"></a><a class="code" href="classQCA_1_1CertificateCollection.html#5c761410c4fb60c56f542a122e226bfb">QCA::CertificateCollection::fromFlatTextFile</a>( argv[1], &amp;importResult );
126
 
<a name="l00108"></a>00108         <span class="keywordflow">if</span> ( <a name="a34"></a><a class="code" href="namespaceQCA.html#b00c91d4b676b998115216ef0562161d4ab65f11eb035b245b32dd2ed7ee9207">QCA::ConvertGood</a> == importResult) {
127
 
<a name="l00109"></a>00109             std::cout &lt;&lt; <span class="stringliteral">"Import succeeded"</span> &lt;&lt; std::endl;
128
 
<a name="l00110"></a>00110             <span class="comment">// this turns the CertificateCollection into a QList of Certificate objects</span>
129
 
<a name="l00111"></a>00111             certlist = filecerts.<a name="a35"></a><a class="code" href="classQCA_1_1CertificateCollection.html#1648d513823baef8efdfdee3a8b34798">certificates</a>();
130
 
<a name="l00112"></a>00112         }
131
 
<a name="l00113"></a>00113     } <span class="keywordflow">else</span> {
132
 
<a name="l00114"></a>00114         <span class="comment">// we have no arguments, so just use the system certificates</span>
133
 
<a name="l00115"></a>00115         <span class="keywordflow">if</span> ( !<a name="a36"></a><a class="code" href="namespaceQCA.html#77f5e20c99d07400a92a0f1a38aed721">QCA::haveSystemStore</a>() ) {
134
 
<a name="l00116"></a>00116             std::cout &lt;&lt; <span class="stringliteral">"System certificates not available"</span> &lt;&lt; std::endl;
135
 
<a name="l00117"></a>00117             <span class="keywordflow">return</span> 2;
136
 
<a name="l00118"></a>00118         }
137
 
<a name="l00119"></a>00119 
138
 
<a name="l00120"></a>00120         <span class="comment">// Similar to above, except we just want the system certificates</span>
139
 
<a name="l00121"></a>00121         <a class="code" href="classQCA_1_1CertificateCollection.html">QCA::CertificateCollection</a> systemcerts = <a name="a37"></a><a class="code" href="namespaceQCA.html#ff42b3899718425b11da15efc1019084">QCA::systemStore</a>();
140
 
<a name="l00122"></a>00122 
141
 
<a name="l00123"></a>00123         <span class="comment">// this turns the CertificateCollection into a QList of Certificate objects</span>
142
 
<a name="l00124"></a>00124         certlist = systemcerts.<a class="code" href="classQCA_1_1CertificateCollection.html#1648d513823baef8efdfdee3a8b34798">certificates</a>();
143
 
<a name="l00125"></a>00125     }
144
 
<a name="l00126"></a>00126 
145
 
<a name="l00127"></a>00127     std::cout &lt;&lt; <span class="stringliteral">"Number of certificates: "</span> &lt;&lt; certlist.<a name="a38"></a><a class="codeRef" doxygen="qt.tag:" href="qlist.html#265aa6a012215bff40a5eea6ab2e18be">count</a>() &lt;&lt; std::endl;
146
 
<a name="l00128"></a>00128 
147
 
<a name="l00129"></a>00129     <a name="_a39"></a><a class="code" href="classQCA_1_1Certificate.html">QCA::Certificate</a> cert;
148
 
<a name="l00130"></a>00130     foreach (cert, certlist) {
149
 
<a name="l00131"></a>00131         std::cout &lt;&lt; <span class="stringliteral">"Serial Number:"</span>;
150
 
<a name="l00132"></a>00132         <span class="comment">// the serial number of the certificate is a QCA::BigInteger, but we can</span>
151
 
<a name="l00133"></a>00133         <span class="comment">// just convert it to a string, and then output it.</span>
152
 
<a name="l00134"></a>00134         std::cout &lt;&lt; qPrintable(cert.<a name="a40"></a><a class="code" href="classQCA_1_1Certificate.html#cdc4857713803551416870bb1502c644">serialNumber</a>().toString()) &lt;&lt; std::endl;
153
 
<a name="l00135"></a>00135 
154
 
<a name="l00136"></a>00136         <span class="comment">// The subject information shows properties of who the certificate</span>
155
 
<a name="l00137"></a>00137         <span class="comment">// applies to. See the convenience routines above.</span>
156
 
<a name="l00138"></a>00138         dumpSubjectInfo( cert.<a name="a41"></a><a class="code" href="classQCA_1_1Certificate.html#6a39492fa804b3d2f26e37175fd4312d">subjectInfo</a>() );
157
 
<a name="l00139"></a>00139 
158
 
<a name="l00140"></a>00140         <span class="comment">// The issuer information shows properties of who the certificate</span>
159
 
<a name="l00141"></a>00141         <span class="comment">// was signed by. See the convenience routines above.</span>
160
 
<a name="l00142"></a>00142         dumpIssuerInfo( cert.<a name="a42"></a><a class="code" href="classQCA_1_1Certificate.html#2b2fc0991b0cc3967d34866609ff624d">issuerInfo</a>() );
161
 
<a name="l00143"></a>00143 
162
 
<a name="l00144"></a>00144         <span class="comment">// Test if the certificate can be used as a certificate authority</span>
163
 
<a name="l00145"></a>00145         <span class="keywordflow">if</span> ( cert.<a name="a43"></a><a class="code" href="classQCA_1_1Certificate.html#ef5d394c4ab959be5b31cd3b79702121">isCA</a>() ) {
164
 
<a name="l00146"></a>00146             std::cout &lt;&lt; <span class="stringliteral">"Is certificate authority"</span> &lt;&lt; std::endl;
165
 
<a name="l00147"></a>00147         } <span class="keywordflow">else</span> {
166
 
<a name="l00148"></a>00148             std::cout &lt;&lt; <span class="stringliteral">"Is not a certificate authority"</span> &lt;&lt; std::endl;
167
 
<a name="l00149"></a>00149         }
168
 
<a name="l00150"></a>00150 
169
 
<a name="l00151"></a>00151         <span class="comment">// Test if the certificate is self-signed.</span>
170
 
<a name="l00152"></a>00152         <span class="keywordflow">if</span> (cert.<a name="a44"></a><a class="code" href="classQCA_1_1Certificate.html#998fc4ad643bb12c0bba0341a6430897">isSelfSigned</a>() ) {
171
 
<a name="l00153"></a>00153             std::cout &lt;&lt; <span class="stringliteral">"Self signed"</span> &lt;&lt; std::endl;
172
 
<a name="l00154"></a>00154         } <span class="keywordflow">else</span> {
173
 
<a name="l00155"></a>00155             std::cout &lt;&lt; <span class="stringliteral">"Is not self-signed!!!"</span> &lt;&lt; std::endl;
174
 
<a name="l00156"></a>00156         }
175
 
<a name="l00157"></a>00157 
176
 
<a name="l00158"></a>00158         <span class="comment">// Certificate are only valid between specific dates. We can get the dates</span>
177
 
<a name="l00159"></a>00159         <span class="comment">// (as a QDateTime) using a couple of calls</span>
178
 
<a name="l00160"></a>00160         std::cout &lt;&lt; <span class="stringliteral">"Valid from "</span> &lt;&lt; qPrintable(cert.<a name="a45"></a><a class="code" href="classQCA_1_1Certificate.html#13e4a24dc8db02cfef632a4cf0d5f630">notValidBefore</a>().<a name="a46"></a><a class="codeRef" doxygen="qt.tag:" href="qdatetime.html#9a4a6acae0c62b5a772d54d3dad17db7">toString</a>());
179
 
<a name="l00161"></a>00161         std::cout &lt;&lt; <span class="stringliteral">", until "</span> &lt;&lt; qPrintable(cert.<a name="a47"></a><a class="code" href="classQCA_1_1Certificate.html#73e8cc34b294017f32b0467a8303c69a">notValidAfter</a>().<a class="codeRef" doxygen="qt.tag:" href="qdatetime.html#9a4a6acae0c62b5a772d54d3dad17db7">toString</a>());
180
 
<a name="l00162"></a>00162         std::cout &lt;&lt; std::endl;
181
 
<a name="l00163"></a>00163 
182
 
<a name="l00164"></a>00164         <span class="comment">// You can get the certificate in PEM encoding with a simple toPEM() call</span>
183
 
<a name="l00165"></a>00165         std::cout &lt;&lt; <span class="stringliteral">"PEM:"</span> &lt;&lt; std::endl;
184
 
<a name="l00166"></a>00166         std::cout &lt;&lt; qPrintable(cert.<a name="a48"></a><a class="code" href="classQCA_1_1Certificate.html#87df3f8d39f078a50be296a10488de1b">toPEM</a>());
185
 
<a name="l00167"></a>00167         std::cout &lt;&lt; std::endl &lt;&lt; std::endl;
186
 
<a name="l00168"></a>00168    }
187
 
<a name="l00169"></a>00169 
188
 
<a name="l00170"></a>00170     <span class="keywordflow">return</span> 0;
189
 
<a name="l00171"></a>00171 }
190
 
<a name="l00172"></a>00172 
191
 
</pre></div> <hr size="1"><address style="align: right;"><small>Generated on Fri Jul 6 13:22:42 2007 for Qt Cryptographic Architecture by&nbsp;
192
 
<a href="http://www.doxygen.org/index.html">
193
 
<img src="doxygen.png" alt="doxygen" align="middle" border="0"></a> 1.4.6 </small></address>
194
 
</body>
195
 
</html>