1 #ifndef	gpglib_h
2 #define	gpglib_h
3 /*
4 ** Copyright 2001-2016 Double Precision, Inc.  See COPYING for
5 ** distribution information.
6 */
7 
8 
9 #ifdef  __cplusplus
10 extern "C" {
11 #endif
12 
13 #include	"config.h"
14 #include	<sys/types.h>
15 #include	<stdlib.h>
16 
17 
18 #define LIBMAIL_GPG_INDIVIDUAL	1
19 #define LIBMAIL_GPG_ENCAPSULATE	2
20 
21 #define LIBMAIL_GPG_CHECKSIGN	1
22 #define LIBMAIL_GPG_UNENCRYPT	2
23 
24 struct libmail_gpg_info {
25 
26 	const char *gnupghome; /* May be NULL, sets GNUPGHOME */
27 
28 	const char *passphrase_fd; /* NULL, or string giving */
29 
30 	/*
31 	** input_func gets called repeatedly to obtain the message to
32 	** encrypt/sign/decrypt/check.  input_func() receives the same
33 	** arguments as fgets(), with its third argument being input_func_arg.
34 	** input_func should read up to cnt-1 bytes, or a newline, whichever
35 	** comes first, and save read data in buf, appending a single null
36 	** byte.  input_func should return 0, or -1 on EOF condition.
37 	*/
38 	int (*input_func)(char *buf, size_t cnt, void *vp);
39 	void *input_func_arg;
40 
41 	/*
42 	** Output_func gets repeatedly invoked with the contents of the
43 	** encrypted/signed/decrypted/verified message.
44 	*/
45 
46 	void (*output_func)(const char *output, size_t nbytes,
47 			    void *output_arg);
48 	void *output_func_arg; /* Passthru arg to output_func */
49 
50 	/*
51 	** In the event of an error, the error handler will be invoked with
52 	** the error message text.  The error handler will be invoked
53 	** just before libmail_gpg_*() exits.  Note that the memory used
54 	** by the error message text will be destroyed by the time
55 	** libmail_gpg_*() exits, so the application needs to make a copy of
56 	** it, if it intends to use it later.
57 	*/
58 
59 	void (*errhandler_func)(const char *errmsg, void *errmsg_arg);
60 	void *errhandler_arg; /* Passthru arg to errhandler_func */
61 
62 	/* Additional, arbitrary, arguments to GnuPG */
63 
64 	int argc;
65 	char **argv;
66 
67 	/* On exit, the following bits may be set: */
68 
69 	int errstatus;
70 
71 #define LIBMAIL_ERR_VERIFYSIG 1
72 #define LIBMAIL_ERR_DECRYPT 2
73 
74 };
75 
76 int libmail_gpg_signencode(int dosign,
77 			   int doencode,
78 			   /*
79 			   ** One of LIBMAIL_GPG_INDIVIDUAL or
80 			   ** LIBMAIL_GPG_ENCAPSULATE
81 			   */
82 			   struct libmail_gpg_info *options);
83 
84 int libmail_gpg_decode(int mode,
85 		       /*
86 		       ** LIBMAIL_GPG_UNENCRYPT OR LIBMAIL_GPG_CHECKSIGN
87 		       */
88 		       struct libmail_gpg_info *options);
89 
90 
91 	/* A convenient input_func, where vp is FILE * */
92 
93 int libmail_gpg_inputfunc_readfp(char *buf, size_t cnt, void *vp);
94 
95 	/* Other functions: */
96 
97 int libmail_gpg_cleanup();
98 int libmail_gpg_has_gpg(const char *gpgdir);
99 
100 int libmail_gpg_genkey(const char *gpgdir,
101 		       const char *charset,
102 		       const char *name,
103 		       const char *addr,
104 		       const char *comment,
105 		       int skeylen,
106 		       int ekeylen,
107 		       unsigned expire,
108 		       char expire_unit,
109 		       const char *passphrase,
110 
111 		       int (*dump_func)(const char *, size_t, void *),
112 		       int (*timeout_func)(void *),
113 		       void *voidarg);
114 
115 struct gpg_list_info {
116 	const char *charset;
117 	const char *disabled_msg;
118 	const char *revoked_msg;
119 	const char *expired_msg;
120 	const char *group_msg;
121 	void *voidarg;
122 } ;
123 
124 int libmail_gpg_listkeys(const char *gpgdir,
125 		 int secret,
126 		 int (*callback_func)(const char *, const char *,
127 				      const char *, int,
128 				      struct gpg_list_info *),
129 		 int (*err_func)(const char *, size_t, void *),
130 		 struct gpg_list_info *);
131 
132 int libmail_gpg_listgroups(const char *gpgdir,
133 			   int (*callback_func)(const char *, const char *,
134 						const char *,
135 						int,
136 						struct gpg_list_info *),
137 			   struct gpg_list_info *voidarg);
138 
139 int libmail_gpg_exportkey(const char *gpgdir,
140 		  int secret,
141 		  const char *fingerprint,
142 		  int (*out_func)(const char *, size_t, void *),
143 		  int (*err_func)(const char *, size_t, void *),
144 		  void *voidarg);
145 
146 int libmail_gpg_deletekey(const char *gpgdir, int secret, const char *fingerprint,
147 		  int (*dump_func)(const char *, size_t, void *),
148 		  void *voidarg);
149 
150 int libmail_gpg_signkey(const char *gpgdir, const char *signthis, const char *signwith,
151 		int passphrase_fd,
152 		int (*dump_func)(const char *, size_t, void *),
153 		void *voidarg);
154 
155 int libmail_gpg_makepassphrasepipe(const char *passphrase,
156 				   size_t passphrase_size);
157 	/*
158 	** Create a pipe and fork, the child process writes the passphrase
159 	** to the pipe and exits.
160 	**
161 	** Returns the read end of the pipe.
162 	*/
163 
164 int libmail_gpg_checksign(const char *gpgdir,
165 		  const char *content,	/* Filename, for now */
166 		  const char *signature, /* Filename, for now */
167 		  int (*dump_func)(const char *, size_t, void *),
168 		  void *voidarg);
169 
170 	/* IMPORT A KEY */
171 
172 int libmail_gpg_import_start(const char *gpgdir, int issecret);
173 
174 int libmail_gpg_import_do(const char *p, size_t n,	/* Part of the key */
175 		  int (*dump_func)(const char *, size_t, void *),
176 		  /* gpg output callback */
177 
178 		  void *voidarg);
179 
180 int libmail_gpg_import_finish(int (*dump_func)(const char *, size_t, void *),
181 		      void *voidarg);
182 
183 
184 
185 	     /* INTERNAL: */
186 
187 pid_t libmail_gpg_fork(int *, int *, int *, const char *, char **);
188 
189 #define GPGARGV_PASSPHRASE_FD(argv,i,fd,buf) \
190 	((argv)[(i)++]="--passphrase-fd", \
191 	 (argv)[(i)++]=libmail_str_size_t((fd),(buf)))
192 
193 int libmail_gpg_write(const char *, size_t,
194 	      int (*)(const char *, size_t, void *),
195 	      int (*)(const char *, size_t, void *),
196 	      int (*)(void *),
197 	      unsigned,
198 	      void *);
199 
200 int libmail_gpg_read(int (*)(const char *, size_t, void *),
201 	     int (*)(const char *, size_t, void *),
202 	     int (*)(void *),
203 	     unsigned,
204 	     void *);
205 
206 char *libmail_gpg_options(const char *gpgdir);
207 	/* Filename of the options file.  If gpgdir is NULL try
208 	** the environment variables. */
209 
210 
211 struct rfc2045 *libmail_gpgmime_is_multipart_signed(const struct rfc2045 *);
212 	/*
213 	** Return ptr to signed content if ptr is a multipart/signed.
214 	*/
215 
216 struct rfc2045 *libmail_gpgmime_is_multipart_encrypted(const struct rfc2045 *);
217 	/*
218 	** Return ptr to encrypted content if ptr is a multipart/encrypted.
219 	*/
220 
221 int libmail_gpgmime_has_mimegpg(const struct rfc2045 *);
222 	/*
223 	** Return non-zero if MIME content has any signed or encrypted
224 	** content.
225 	*/
226 
227 int libmail_gpgmime_is_decoded(const struct rfc2045 *, int *);
228 	/*
229 	** Return non-zero if this is a multipart/mixed section generated
230 	** by mimegpg, and return the GnuPG return code.
231 	*/
232 
233 struct rfc2045 *libmail_gpgmime_decoded_content(const struct rfc2045 *);
234 	/*
235 	** If is_decoded, then return the ptr to the decoded content.
236 	** (note - if decryption failed, NULL is returned).
237 	*/
238 
239 struct rfc2045 *libmail_gpgmime_signed_content(const struct rfc2045 *);
240 	/*
241 	** If is_multipart_signed, return ptr to the signed content.
242 	*/
243 
244 #ifdef  __cplusplus
245 }
246 #endif
247 #endif
248