1/*
2 key.h - wraps a gpgme key
3 Copyright (C) 2003, 2005 Klarälvdalens Datakonsult AB
4
5 This file is part of GPGME++.
6
7 GPGME++ is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Library General Public
9 License as published by the Free Software Foundation; either
10 version 2 of the License, or (at your option) any later version.
11
12 GPGME++ is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU Library General Public License for more details.
16
17 You should have received a copy of the GNU Library General Public License
18 along with GPGME++; see the file COPYING.LIB. If not, write to the
19 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 Boston, MA 02110-1301, USA.
21*/
22
23// -*- c++ -*-
24#ifndef __GPGMEPP_KEY_H__
25#define __GPGMEPP_KEY_H__
26
27#include <gpgme++/global.h>
28#include <gpgme++/notation.h>
29
30#include <gpgme++/gpgmefw.h>
31
32#include <boost/shared_ptr.hpp>
33#include <boost/type_traits/remove_pointer.hpp>
34
35#include <sys/time.h>
36
37#include <vector>
38#include <algorithm>
39#include <string>
40
41namespace GpgME {
42
43 class Context;
44
45 class Subkey;
46 class UserID;
47
48 typedef boost::shared_ptr< boost::remove_pointer<gpgme_key_t>::type > shared_gpgme_key_t;
49
50 //
51 // class Key
52 //
53
54 class GPGMEPP_EXPORT Key {
55 friend class ::GpgME::Context;
56 struct Null {};
57 public:
58 Key();
59 /* implicit */ Key( const Null & );
60 Key( const shared_gpgme_key_t & key );
61 Key( gpgme_key_t key, bool acquireRef );
62
63 static Null null;
64
65 const Key & operator=( Key other ) {
66 swap( other );
67 return *this;
68 }
69
70 const Key & mergeWith( const Key & other );
71
72 void swap( Key & other ) {
73 using std::swap;
74 swap( this->key, other.key );
75 }
76
77 bool isNull() const { return !key; }
78
79 UserID userID( unsigned int index ) const;
80 Subkey subkey( unsigned int index ) const;
81
82 unsigned int numUserIDs() const;
83 unsigned int numSubkeys() const;
84
85 std::vector<UserID> userIDs() const;
86 std::vector<Subkey> subkeys() const;
87
88 bool isRevoked() const;
89 bool isExpired() const;
90 bool isDisabled() const;
91 bool isInvalid() const;
92
93 bool canEncrypt() const;
94 /*!
95 This function contains a workaround for old gpgme's: all secret
96 OpenPGP keys canSign() == true, which canReallySign() doesn't
97 have. I don't have time to find what breaks when I remove this
98 workaround, but since Kleopatra merges secret into public keys,
99 the workaround is not necessary there (and actively harms), I've
100 added a new function instead.
101 */
102 bool canSign() const;
103 bool canReallySign() const;
104 bool canCertify() const;
105 bool canAuthenticate() const;
106 bool isQualified() const;
107
108 bool hasSecret() const;
109 GPGMEPP_DEPRECATED bool isSecret() const { return hasSecret(); }
110
111 /*!
112 @return true if this is a X.509 root certificate (currently
113 equivalent to something like
114 strcmp( chainID(), subkey(0).fingerprint() ) == 0 )
115 */
116 bool isRoot() const;
117
118 enum OwnerTrust { Unknown=0, Undefined=1, Never=2,
119 Marginal=3, Full=4, Ultimate=5 };
120
121 OwnerTrust ownerTrust() const;
122 char ownerTrustAsString() const;
123
124 Protocol protocol() const;
125 const char * protocolAsString() const;
126
127 const char * issuerSerial() const;
128 const char * issuerName() const;
129 const char * chainID() const;
130
131 const char * keyID() const;
132 const char * shortKeyID() const;
133 const char * primaryFingerprint() const;
134
135 unsigned int keyListMode() const;
136
137 private:
138 gpgme_key_t impl() const { return key.get(); }
139 shared_gpgme_key_t key;
140 };
141
142 //
143 // class Subkey
144 //
145
146 class GPGMEPP_EXPORT Subkey {
147 public:
148 Subkey();
149 Subkey( const shared_gpgme_key_t & key, gpgme_sub_key_t subkey );
150 Subkey( const shared_gpgme_key_t & key, unsigned int idx );
151
152 const Subkey & operator=( Subkey other ) {
153 swap( other );
154 return *this;
155 }
156
157 void swap( Subkey & other ) {
158 using std::swap;
159 swap( this->key, other.key );
160 swap( this->subkey, other.subkey );
161 }
162
163 bool isNull() const { return !key || !subkey; }
164
165 Key parent() const;
166
167 const char * keyID() const;
168 const char * fingerprint() const;
169
170 time_t creationTime() const;
171 time_t expirationTime() const;
172 bool neverExpires() const;
173
174 bool isRevoked() const;
175 bool isExpired() const;
176 bool isInvalid() const;
177 bool isDisabled() const;
178
179 bool canEncrypt() const;
180 bool canSign() const;
181 bool canCertify() const;
182 bool canAuthenticate() const;
183 bool isQualified() const;
184 bool isCardKey() const;
185
186 bool isSecret() const;
187
188 unsigned int publicKeyAlgorithm() const;
189 const char * publicKeyAlgorithmAsString() const;
190
191 unsigned int length() const;
192
193 const char * cardSerialNumber() const;
194
195 private:
196 shared_gpgme_key_t key;
197 gpgme_sub_key_t subkey;
198 };
199
200 //
201 // class UserID
202 //
203
204 class GPGMEPP_EXPORT UserID {
205 public:
206 class Signature;
207
208 UserID();
209 UserID( const shared_gpgme_key_t & key, gpgme_user_id_t uid );
210 UserID( const shared_gpgme_key_t & key, unsigned int idx );
211
212 const UserID & operator=( UserID other ) {
213 swap( other );
214 return *this;
215 }
216
217 void swap( UserID & other ) {
218 using std::swap;
219 swap( this->key, other.key );
220 swap( this->uid, other.uid );
221 }
222
223 bool isNull() const { return !key || !uid; }
224
225 Key parent() const;
226
227 unsigned int numSignatures() const;
228 Signature signature( unsigned int index ) const;
229 std::vector<Signature> signatures() const;
230
231 const char * id() const;
232 const char * name() const;
233 const char * email() const;
234 const char * comment() const;
235
236 enum Validity { Unknown=0, Undefined=1, Never=2,
237 Marginal=3, Full=4, Ultimate=5 };
238
239 Validity validity() const;
240 char validityAsString() const;
241
242 bool isRevoked() const;
243 bool isInvalid() const;
244
245 private:
246 shared_gpgme_key_t key;
247 gpgme_user_id_t uid;
248 };
249
250 //
251 // class UserID::Signature
252 //
253
254 class GPGMEPP_EXPORT UserID::Signature {
255 public:
256 typedef GPGMEPP_DEPRECATED GpgME::Notation Notation;
257
258 Signature();
259 Signature( const shared_gpgme_key_t & key, gpgme_user_id_t uid, gpgme_key_sig_t sig );
260 Signature( const shared_gpgme_key_t & key, gpgme_user_id_t uid, unsigned int idx );
261
262 const Signature & operator=( Signature other ) {
263 swap( other );
264 return *this;
265 }
266
267 void swap( Signature & other ) {
268 using std::swap;
269 swap( this->key, other.key );
270 swap( this->uid, other.uid );
271 swap( this->sig, other.sig );
272 }
273
274 bool isNull() const { return !sig || !uid || !key ; }
275
276 UserID parent() const;
277
278 const char * signerKeyID() const;
279
280 const char * algorithmAsString() const;
281 unsigned int algorithm() const;
282 time_t creationTime() const;
283 time_t expirationTime() const;
284 bool neverExpires() const;
285
286 bool isRevokation() const;
287 bool isInvalid() const;
288 bool isExpired() const;
289 bool isExportable() const;
290
291 const char * signerUserID() const;
292 const char * signerName() const;
293 const char * signerEmail() const;
294 const char * signerComment() const;
295
296 unsigned int certClass() const;
297
298 enum Status { NoError = 0, SigExpired, KeyExpired,
299 BadSignature, NoPublicKey, GeneralError };
300 Status status() const;
301 std::string statusAsString() const;
302
303 const char * policyURL() const;
304
305 unsigned int numNotations() const;
306 GpgME::Notation notation( unsigned int idx ) const;
307 std::vector<GpgME::Notation> notations() const;
308
309 private:
310 shared_gpgme_key_t key;
311 gpgme_user_id_t uid;
312 gpgme_key_sig_t sig;
313 };
314
315} // namespace GpgME
316
317GPGMEPP_MAKE_STD_SWAP_SPECIALIZATION( Key )
318GPGMEPP_MAKE_STD_SWAP_SPECIALIZATION( Subkey )
319GPGMEPP_MAKE_STD_SWAP_SPECIALIZATION( UserID )
320GPGMEPP_MAKE_STD_SWAP_SPECIALIZATION( UserID::Signature )
321
322#endif // __GPGMEPP_KEY_H__
323