1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3  * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
4  *
5  * This library is free software: you can redistribute it and/or modify it
6  * under the terms of the GNU Lesser General Public License as published by
7  * the Free Software Foundation.
8  *
9  * This library is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
12  * for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public License
15  * along with this library. If not, see <http://www.gnu.org/licenses/>.
16  *
17  * Authors: Jeffrey Stedfast <fejj@ximian.com>
18  */
19 
20 #if !defined (__CAMEL_H_INSIDE__) && !defined (CAMEL_COMPILATION)
21 #error "Only <camel/camel.h> can be included directly."
22 #endif
23 
24 #ifndef CAMEL_CIPHER_CONTEXT_H
25 #define CAMEL_CIPHER_CONTEXT_H
26 
27 #include <camel/camel-mime-part.h>
28 #include <camel/camel-session.h>
29 
30 /* Standard GObject macros */
31 #define CAMEL_TYPE_CIPHER_CONTEXT \
32 	(camel_cipher_context_get_type ())
33 #define CAMEL_CIPHER_CONTEXT(obj) \
34 	(G_TYPE_CHECK_INSTANCE_CAST \
35 	((obj), CAMEL_TYPE_CIPHER_CONTEXT, CamelCipherContext))
36 #define CAMEL_CIPHER_CONTEXT_CLASS(cls) \
37 	(G_TYPE_CHECK_CLASS_CAST \
38 	((cls), CAMEL_TYPE_CIPHER_CONTEXT, CamelCipherContextClass))
39 #define CAMEL_IS_CIPHER_CONTEXT(obj) \
40 	(G_TYPE_CHECK_INSTANCE_TYPE \
41 	((obj), CAMEL_TYPE_CIPHER_CONTEXT))
42 #define CAMEL_IS_CIPHER_CONTEXT_CLASS(cls) \
43 	(G_TYPE_CHECK_CLASS_TYPE \
44 	((cls), CAMEL_TYPE_CIPHER_CONTEXT))
45 #define CAMEL_CIPHER_CONTEXT_GET_CLASS(obj) \
46 	(G_TYPE_INSTANCE_GET_CLASS \
47 	((obj), CAMEL_TYPE_CIPHER_CONTEXT, CamelCipherContextClass))
48 
49 /**
50  * CAMEL_CIPHER_CERT_INFO_PROPERTY_PHOTO_FILENAME:
51  *
52  * Name of the photo-filename property which can be stored
53  * on a #CamelCipherCertInfo.
54  *
55  * Since: 3.22
56  **/
57 #define CAMEL_CIPHER_CERT_INFO_PROPERTY_PHOTO_FILENAME "photo-filename"
58 
59 /**
60  * CAMEL_CIPHER_CERT_INFO_PROPERTY_SIGNERS_ALT_EMAILS:
61  *
62  * A string containing a list of email addresses of all signers
63  * including their alternative emails. Use camel_address_unformat()
64  * to break them back into separate addresses. This can be set
65  * only on the first signer of the first validity, even the addresses
66  * can belong to a different signer.
67  *
68  * Since: 3.28
69  **/
70 #define CAMEL_CIPHER_CERT_INFO_PROPERTY_SIGNERS_ALT_EMAILS "signers-alt-emails"
71 
72 G_BEGIN_DECLS
73 
74 typedef gpointer (* CamelCipherCloneFunc) (gpointer value);
75 
76 typedef struct _CamelCipherValidity CamelCipherValidity;
77 typedef struct _CamelCipherCertInfo CamelCipherCertInfo;
78 typedef struct _CamelCipherCertInfoProperty CamelCipherCertInfoProperty;
79 
80 typedef struct _CamelCipherContext CamelCipherContext;
81 typedef struct _CamelCipherContextClass CamelCipherContextClass;
82 typedef struct _CamelCipherContextPrivate CamelCipherContextPrivate;
83 
84 typedef enum {
85 	CAMEL_CIPHER_HASH_DEFAULT,
86 	CAMEL_CIPHER_HASH_MD2,
87 	CAMEL_CIPHER_HASH_MD5,
88 	CAMEL_CIPHER_HASH_SHA1,
89 	CAMEL_CIPHER_HASH_SHA256,
90 	CAMEL_CIPHER_HASH_SHA384,
91 	CAMEL_CIPHER_HASH_SHA512,
92 	CAMEL_CIPHER_HASH_RIPEMD160,
93 	CAMEL_CIPHER_HASH_TIGER192,
94 	CAMEL_CIPHER_HASH_HAVAL5160
95 } CamelCipherHash;
96 
97 typedef enum _camel_cipher_validity_sign_t {
98 	CAMEL_CIPHER_VALIDITY_SIGN_NONE,
99 	CAMEL_CIPHER_VALIDITY_SIGN_GOOD,
100 	CAMEL_CIPHER_VALIDITY_SIGN_BAD,
101 	CAMEL_CIPHER_VALIDITY_SIGN_UNKNOWN,
102 	CAMEL_CIPHER_VALIDITY_SIGN_NEED_PUBLIC_KEY
103 } CamelCipherValiditySign;
104 
105 typedef enum _camel_cipher_validity_encrypt_t {
106 	CAMEL_CIPHER_VALIDITY_ENCRYPT_NONE,
107 	CAMEL_CIPHER_VALIDITY_ENCRYPT_WEAK,
108 	CAMEL_CIPHER_VALIDITY_ENCRYPT_ENCRYPTED, /* encrypted, unknown strenght */
109 	CAMEL_CIPHER_VALIDITY_ENCRYPT_STRONG
110 } CamelCipherValidityEncrypt;
111 
112 typedef enum _camel_cipher_validity_mode_t {
113 	CAMEL_CIPHER_VALIDITY_SIGN,
114 	CAMEL_CIPHER_VALIDITY_ENCRYPT
115 } CamelCipherValidityMode;
116 
117 struct _CamelCipherCertInfoProperty {
118 	gchar *name;
119 	gpointer value;
120 
121 	GDestroyNotify value_free;
122 	CamelCipherCloneFunc value_clone;
123 };
124 
125 struct _CamelCipherCertInfo {
126 	gchar *name;		/* common name */
127 	gchar *email;
128 
129 	gpointer cert_data;  /* custom certificate data; can be NULL */
130 	GDestroyNotify cert_data_free; /* called to free cert_data; can be NULL only if cert_data is NULL */
131 	CamelCipherCloneFunc cert_data_clone; /* called to clone cert_data; can be NULL only if cert_data is NULL */
132 
133 	GSList *properties; /* CamelCipherCertInfoProperty * */
134 };
135 
136 struct _CamelCipherValidity {
137 	GQueue children;
138 
139 	struct _sign {
140 		CamelCipherValiditySign status;
141 		gchar *description;
142 		GQueue signers;	/* CamelCipherCertInfo's */
143 	} sign;
144 
145 	struct _encrypt {
146 		CamelCipherValidityEncrypt status;
147 		gchar *description;
148 		GQueue encrypters;	/* CamelCipherCertInfo's */
149 	} encrypt;
150 };
151 
152 struct _CamelCipherContext {
153 	GObject parent;
154 	CamelCipherContextPrivate *priv;
155 };
156 
157 struct _CamelCipherContextClass {
158 	GObjectClass parent_class;
159 
160 	/* these MUST be set by implementors */
161 	const gchar *sign_protocol;
162 	const gchar *encrypt_protocol;
163 	const gchar *key_protocol;
164 
165 	/* Non-Blocking Methods */
166 	CamelCipherHash	(*id_to_hash)		(CamelCipherContext *context,
167 						 const gchar *id);
168 	const gchar *	(*hash_to_id)		(CamelCipherContext *context,
169 						 CamelCipherHash hash);
170 
171 	/* Synchronous I/O Methods */
172 	gboolean	(*sign_sync)		(CamelCipherContext *context,
173 						 const gchar *userid,
174 						 CamelCipherHash hash,
175 						 CamelMimePart *ipart,
176 						 CamelMimePart *opart,
177 						 GCancellable *cancellable,
178 						 GError **error);
179 	CamelCipherValidity *
180 			(*verify_sync)		(CamelCipherContext *context,
181 						 CamelMimePart *ipart,
182 						 GCancellable *cancellable,
183 						 GError **error);
184 	gboolean	(*encrypt_sync)		(CamelCipherContext *context,
185 						 const gchar *userid,
186 						 GPtrArray *recipients,
187 						 CamelMimePart *ipart,
188 						 CamelMimePart *opart,
189 						 GCancellable *cancellable,
190 						 GError **error);
191 	CamelCipherValidity *
192 			(*decrypt_sync)		(CamelCipherContext *context,
193 						 CamelMimePart *ipart,
194 						 CamelMimePart *opart,
195 						 GCancellable *cancellable,
196 						 GError **error);
197 
198 	/* Padding for future expansion */
199 	gpointer reserved[20];
200 };
201 
202 GType		camel_cipher_context_get_type	(void);
203 CamelCipherContext *
204 		camel_cipher_context_new	(CamelSession *session);
205 CamelSession *	camel_cipher_context_get_session
206 						(CamelCipherContext *context);
207 
208 /* cipher context util routines */
209 CamelCipherHash	camel_cipher_context_id_to_hash	(CamelCipherContext *context,
210 						 const gchar *id);
211 const gchar *	camel_cipher_context_hash_to_id	(CamelCipherContext *context,
212 						 CamelCipherHash hash);
213 
214 /* FIXME:
215  * There are some inconsistencies here, the api's should probably handle CamelMimePart's as input/outputs,
216  * Something that might generate a multipart/signed should do it as part of that processing, internally
217  * to the cipher, etc etc. */
218 
219 /* cipher routines */
220 gboolean	camel_cipher_context_sign_sync	(CamelCipherContext *context,
221 						 const gchar *userid,
222 						 CamelCipherHash hash,
223 						 CamelMimePart *ipart,
224 						 CamelMimePart *opart,
225 						 GCancellable *cancellable,
226 						 GError **error);
227 void		camel_cipher_context_sign	(CamelCipherContext *context,
228 						 const gchar *userid,
229 						 CamelCipherHash hash,
230 						 CamelMimePart *ipart,
231 						 CamelMimePart *opart,
232 						 gint io_priority,
233 						 GCancellable *cancellable,
234 						 GAsyncReadyCallback callback,
235 						 gpointer user_data);
236 gboolean	camel_cipher_context_sign_finish
237 						(CamelCipherContext *context,
238 						 GAsyncResult *result,
239 						 GError **error);
240 CamelCipherValidity *
241 		camel_cipher_context_verify_sync
242 						(CamelCipherContext *context,
243 						 CamelMimePart *ipart,
244 						 GCancellable *cancellable,
245 						 GError **error);
246 void		camel_cipher_context_verify	(CamelCipherContext *context,
247 						 CamelMimePart *ipart,
248 						 gint io_priority,
249 						 GCancellable *cancellable,
250 						 GAsyncReadyCallback callback,
251 						 gpointer user_data);
252 CamelCipherValidity *
253 		camel_cipher_context_verify_finish
254 						(CamelCipherContext *context,
255 						 GAsyncResult *result,
256 						 GError **error);
257 gboolean	camel_cipher_context_encrypt_sync
258 						(CamelCipherContext *context,
259 						 const gchar *userid,
260 						 GPtrArray *recipients,
261 						 CamelMimePart *ipart,
262 						 CamelMimePart *opart,
263 						 GCancellable *cancellable,
264 						 GError **error);
265 void		camel_cipher_context_encrypt	(CamelCipherContext *context,
266 						 const gchar *userid,
267 						 GPtrArray *recipients,
268 						 CamelMimePart *ipart,
269 						 CamelMimePart *opart,
270 						 gint io_priority,
271 						 GCancellable *cancellable,
272 						 GAsyncReadyCallback callback,
273 						 gpointer user_data);
274 gboolean	camel_cipher_context_encrypt_finish
275 						(CamelCipherContext *context,
276 						 GAsyncResult *result,
277 						 GError **error);
278 CamelCipherValidity *
279 		camel_cipher_context_decrypt_sync
280 						(CamelCipherContext *context,
281 						 CamelMimePart *ipart,
282 						 CamelMimePart *opart,
283 						 GCancellable *cancellable,
284 						 GError **error);
285 void		camel_cipher_context_decrypt	(CamelCipherContext *context,
286 						 CamelMimePart *ipart,
287 						 CamelMimePart *opart,
288 						 gint io_priority,
289 						 GCancellable *cancellable,
290 						 GAsyncReadyCallback callback,
291 						 gpointer user_data);
292 CamelCipherValidity *
293 		camel_cipher_context_decrypt_finish
294 						(CamelCipherContext *context,
295 						 GAsyncResult *result,
296 						 GError **error);
297 
298 /* CamelCipherValidity utility functions */
299 GType		camel_cipher_validity_get_type	(void);
300 CamelCipherValidity *
301 		camel_cipher_validity_new	(void);
302 void		camel_cipher_validity_init	(CamelCipherValidity *validity);
303 gboolean	camel_cipher_validity_get_valid	(CamelCipherValidity *validity);
304 void		camel_cipher_validity_set_valid	(CamelCipherValidity *validity,
305 						 gboolean valid);
306 gchar *		camel_cipher_validity_get_description
307 						(CamelCipherValidity *validity);
308 void		camel_cipher_validity_set_description
309 						(CamelCipherValidity *validity,
310 						 const gchar *description);
311 void		camel_cipher_validity_clear	(CamelCipherValidity *validity);
312 CamelCipherValidity *
313 		camel_cipher_validity_clone	(CamelCipherValidity *vin);
314 gint		camel_cipher_validity_add_certinfo
315 						(CamelCipherValidity *vin,
316 						 CamelCipherValidityMode mode,
317 						 const gchar *name,
318 						 const gchar *email);
319 gint		camel_cipher_validity_add_certinfo_ex (
320 						CamelCipherValidity *vin,
321 						CamelCipherValidityMode mode,
322 						const gchar *name,
323 						const gchar *email,
324 						gpointer cert_data,
325 						GDestroyNotify cert_data_free,
326 						CamelCipherCloneFunc cert_data_clone);
327 gpointer	camel_cipher_validity_get_certinfo_property
328 						(CamelCipherValidity *vin,
329 						 CamelCipherValidityMode mode,
330 						 gint info_index,
331 						 const gchar *name);
332 void		camel_cipher_validity_set_certinfo_property
333 						(CamelCipherValidity *vin,
334 						 CamelCipherValidityMode mode,
335 						 gint info_index,
336 						 const gchar *name,
337 						 gpointer value,
338 						 GDestroyNotify value_free,
339 						 CamelCipherCloneFunc value_clone);
340 void		camel_cipher_validity_envelope	(CamelCipherValidity *parent,
341 						 CamelCipherValidity *valid);
342 void		camel_cipher_validity_free	(CamelCipherValidity *validity);
343 
344 /* CamelCipherCertInfo utility functions */
345 gpointer	camel_cipher_certinfo_get_property
346 						(CamelCipherCertInfo *cert_info,
347 						 const gchar *name);
348 void		camel_cipher_certinfo_set_property
349 						(CamelCipherCertInfo *cert_info,
350 						 const gchar *name,
351 						 gpointer value,
352 						 GDestroyNotify value_free,
353 						 CamelCipherCloneFunc value_clone);
354 
355 /* utility functions */
356 gint		camel_cipher_canonical_to_stream
357 						(CamelMimePart *part,
358 						 guint32 flags,
359 						 CamelStream *ostream,
360 						 GCancellable *cancellable,
361 						 GError **error);
362 gboolean	camel_cipher_can_load_photos	(void);
363 
364 G_END_DECLS
365 
366 #endif /* CAMEL_CIPHER_CONTEXT_H */
367