configure struct updated according to modified cryptplug.h
[gpgme.git] / gpgmeplug / gpgmeplug.c
1 /* -*- Mode: C -*-
2
3   $Id$
4
5   GPGMEPLUG - an GPGME based cryptography plug-in following
6               the common CRYPTPLUG specification.
7
8   Copyright (C) 2001 by Klarälvdalens Datakonsult AB
9
10   GPGMEPLUG is free software; you can redistribute it and/or modify
11   it under the terms of GNU General Public License as published by
12   the Free Software Foundation; version 2 of the License.
13
14   GPGMEPLUG is distributed in the hope that it will be useful,
15   it under the terms of GNU General Public License as published by
16   the Free Software Foundation; version 2 of the License
17   but WITHOUT ANY WARRANTY; without even the implied warranty of
18   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19   GNU General Public License for more details.
20
21   You should have received a copy of the GNU General Public License
22   along with this program; if not, write to the Free Software
23   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
24 */
25
26
27
28 /*! \file gpgmeplug.c
29     \brief GPGME implementation of CRYPTPLUG following the
30     specification located in common API header cryptplug.h.
31
32     CRYPTPLUG is an independent cryptography plug-in API
33     developed for Sphinx-enabeling KMail and Mutt.
34
35     CRYPTPLUG was designed for the Aegypten project, but it may
36     be used by 3rd party developers as well to design pluggable
37     crypto backends for the above mentioned MUAs.
38
39     \note All string parameters appearing in this API are to be
40     interpreted as UTF-8 encoded.
41
42     \see cryptplug.h
43 */
44
45 #include <config.h>
46 #include <stdio.h>
47 #include <stdlib.h>
48 #include <string.h>
49 #include <assert.h>
50 #include <errno.h>
51
52 #include <gpgme.h>
53 #include <util.h>
54
55 #include "cryptplug.h"
56
57
58 typedef struct {
59   const char*             signatureKeyCertificate;
60   SignatureAlgorithm      signatureAlgorithm;
61   SendCertificates        sendCertificates;
62   SignEmail               signEmail;
63   bool                    saveSentSignatures;
64   bool                    warnNoCertificate;
65   PinRequests             numPINRequests;
66   bool                    checkSignatureCertificatePathToRoot;
67   bool                    signatureUseCRLs;
68   EncryptionAlgorithm     encryptionAlgorithm;
69   EncryptEmail            encryptEmail;
70   bool                    saveMessagesEncrypted;
71   bool                    checkEncryptionCertificatePathToRoot;
72   bool                    encryptionUseCRLs;
73   bool                    encryptionCRLExpiryNearWarning;
74   int                     encryptionCRLNearExpiryInterval;
75   struct DirectoryServer *directoryServers;
76   unsigned int            numDirectoryServers;
77   CertificateSource       certificateSource;
78   CertificateSource       cRLSource;
79   bool                    warnSendUnsigned;
80   int                     numPINRequestsInterval;
81   bool                    signatureCertificateExpiryNearWarning;
82   int                     signatureCertificateExpiryNearInterval;
83   bool                    cACertificateExpiryNearWarning;
84   int                     cACertificateExpiryNearInterval;
85   bool                    rootCertificateExpiryNearWarning;
86   int                     rootCertificateExpiryNearInterval;
87   bool                    warnSendUnencrypted;
88   bool                    checkCertificatePath;
89   bool                    receiverCertificateExpiryNearWarning;
90   int                     receiverCertificateExpiryNearWarningInterval;
91   bool                    certificateInChainExpiryNearWarning;
92   int                     certificateInChainExpiryNearWarningInterval;
93   bool                    receiverEmailAddressNotInCertificateWarning;
94 } Config;
95
96
97 Config config;
98
99
100 #define NEAR_EXPIRY 14
101
102 bool initialize()
103 {
104   config.signatureKeyCertificate              = "";
105   config.signatureAlgorithm                   = SignAlg_SHA1;
106   config.sendCertificates                     = SendCert_SendChainWithRoot;
107   config.signEmail                            = SignEmail_SignAll;
108   config.saveSentSignatures                   = true;
109   config.warnNoCertificate                    = true;
110   config.numPINRequests                       = PinRequest_Always;
111   config.checkSignatureCertificatePathToRoot  = true;
112   config.signatureUseCRLs                     = true;
113   config.encryptionAlgorithm                  = EncryptAlg_RSA;
114   config.encryptEmail                         = EncryptEmail_Ask;
115   config.saveMessagesEncrypted                = true;
116   config.checkEncryptionCertificatePathToRoot = true;
117   config.encryptionUseCRLs                    = true;
118   config.encryptionCRLExpiryNearWarning       = true;
119   config.encryptionCRLNearExpiryInterval      = NEAR_EXPIRY;
120   config.directoryServers                     = NULL;
121   config.numDirectoryServers                  = 0;
122   config.certificateSource                    = CertSrc_Server;
123   config.cRLSource                            = CertSrc_Server;
124   config.warnSendUnsigned                             = true;
125   config.numPINRequestsInterval                       = NEAR_EXPIRY;
126   config.signatureCertificateExpiryNearWarning        = true;
127   config.signatureCertificateExpiryNearInterval       = NEAR_EXPIRY;
128   config.cACertificateExpiryNearWarning               = true;
129   config.cACertificateExpiryNearInterval              = NEAR_EXPIRY;
130   config.rootCertificateExpiryNearWarning             = true;
131   config.rootCertificateExpiryNearInterval            = NEAR_EXPIRY;
132   config.warnSendUnencrypted                          = false;
133   config.checkCertificatePath                         = true;
134   config.receiverCertificateExpiryNearWarning         = true;
135   config.receiverCertificateExpiryNearWarningInterval = NEAR_EXPIRY;
136   config.certificateInChainExpiryNearWarning          = true;
137   config.certificateInChainExpiryNearWarningInterval  = NEAR_EXPIRY;
138   config.receiverEmailAddressNotInCertificateWarning  = true;
139
140   return true;
141 };
142
143
144 void deinitialize()
145 {
146   _gpgme_free( config.directoryServers );
147 }
148
149
150 bool hasFeature( Feature flag )
151 {
152   switch ( flag ) {
153     case CryptPlugFeat_SignMessages:              return true;
154     case CryptPlugFeat_VerifySignatures:          return true;
155     case CryptPlugFeat_EncryptMessages:           return true;
156     case CryptPlugFeat_DecryptMessages:           return true;
157     // undefined or not yet implemented:
158     case CryptPlugFeat_undef:                     return false;
159     default:                                      return false;
160   }
161 }
162
163
164 void unsafeStationery( void** pixmap, const char** menutext, char* accel,
165           const char** tooltip, const char** statusbartext ){}
166
167 void signedStationery( void** pixmap, const char** menutext, char* accel,
168           const char** tooltip, const char** statusbartext ){}
169
170 void encryptedStationery( void** pixmap, const char**
171           menutext, char* accel,
172           const char** tooltip, const char** statusbartext ){}
173
174 void signedEncryptedStationery( void** pixmap, const char**
175           menutext, char* accel,
176           const char** tooltip, const char** statusbartext ){}
177
178 const char* signatureConfigurationDialog(){ return 0; }
179
180 const char* signatureKeySelectionDialog(){ return 0; }
181
182 const char* signatureAlgorithmDialog(){ return 0; }
183
184 const char* signatureHandlingDialog(){ return 0; }
185
186 void setSignatureKeyCertificate( const char* certificate )
187 {
188   config.signatureKeyCertificate = certificate;
189 }
190
191 const char* signatureKeyCertificate()
192 {
193   return config.signatureKeyCertificate;
194 }
195
196 void setSignatureAlgorithm( SignatureAlgorithm sigAlg )
197 {
198   config.signatureAlgorithm = sigAlg;
199 }
200
201 SignatureAlgorithm signatureAlgorithm()
202 {
203   return config.signatureAlgorithm;
204 }
205
206 void setSendCertificates( SendCertificates sendCert )
207 {
208   config.sendCertificates = sendCert;
209 }
210
211 SendCertificates sendCertificates()
212 {
213   return config.sendCertificates;
214 }
215
216 void setSignEmail( SignEmail signMail )
217 {
218   config.signEmail = signMail;
219 }
220
221 SignEmail signEmail()
222 {
223   return config.signEmail;
224 }
225
226
227
228
229
230 void setWarnSendUnsigned( bool flag )
231 {
232   config.warnSendUnsigned = flag;
233 }
234
235 bool warnSendUnsigned()
236 {
237   return config.warnSendUnsigned;
238 }
239
240
241
242
243
244
245 void setSaveSentSignatures( bool flag )
246 {
247   config.saveSentSignatures = flag;
248 }
249
250 bool saveSentSignatures()
251 {
252   return config.saveSentSignatures;
253 }
254
255 void setWarnNoCertificate( bool flag )
256 {
257   config.warnNoCertificate = flag;
258 }
259
260 bool warnNoCertificate()
261 {
262   return config.warnNoCertificate;
263 }
264
265 void setNumPINRequests( PinRequests reqMode )
266 {
267   config.numPINRequests = reqMode;
268 }
269
270 PinRequests numPINRequests()
271 {
272   return config.numPINRequests;
273 }
274
275
276
277
278
279 void setNumPINRequestsInterval( int interval )
280 {
281   config.numPINRequestsInterval = interval;
282 }
283
284 int numPINRequestsInterval()
285 {
286   return config.numPINRequestsInterval;
287 }
288
289
290
291
292
293
294
295 void setCheckSignatureCertificatePathToRoot( bool flag )
296 {
297   config.checkSignatureCertificatePathToRoot = flag;
298 }
299
300 bool checkSignatureCertificatePathToRoot()
301 {
302   return config.checkSignatureCertificatePathToRoot;
303 }
304
305 void setSignatureUseCRLs( bool flag )
306 {
307   config.signatureUseCRLs = flag;
308 }
309
310 bool signatureUseCRLs()
311 {
312   return config.signatureUseCRLs;
313 }
314
315
316
317
318
319
320 void setSignatureCertificateExpiryNearWarning( bool flag )
321 {
322   config.signatureCertificateExpiryNearWarning = flag;
323 }
324
325 bool signatureCertificateExpiryNearWarning( void )
326 {
327   return config.signatureCertificateExpiryNearWarning;
328 }
329
330 void setSignatureCertificateExpiryNearInterval( int interval )
331 {
332   config.signatureCertificateExpiryNearInterval = interval;
333 }
334
335 int signatureCertificateExpiryNearInterval( void )
336 {
337   return config.signatureCertificateExpiryNearInterval;
338 }
339
340 void setCACertificateExpiryNearWarning( bool flag )
341 {
342   config.cACertificateExpiryNearWarning = flag;
343 }
344
345 bool caCertificateExpiryNearWarning( void )
346 {
347   return config.cACertificateExpiryNearWarning;
348 }
349
350 void setCACertificateExpiryNearInterval( int interval )
351 {
352   config.cACertificateExpiryNearInterval = interval;
353 }
354
355 int caCertificateExpiryNearInterval( void )
356 {
357   return config.cACertificateExpiryNearInterval;
358 }
359
360 void setRootCertificateExpiryNearWarning( bool flag )
361 {
362   config.rootCertificateExpiryNearWarning = flag;
363 }
364
365 bool rootCertificateExpiryNearWarning( void )
366 {
367   return config.rootCertificateExpiryNearWarning;
368 }
369
370 void setRootCertificateExpiryNearInterval( int interval )
371 {
372   config.rootCertificateExpiryNearInterval = interval;
373 }
374
375 int rootCertificateExpiryNearInterval( void )
376 {
377   return config.rootCertificateExpiryNearInterval;
378 }
379
380
381
382
383
384
385
386
387 const char* encryptionConfigurationDialog(){ return 0; }
388
389 const char* encryptionAlgorithmDialog(){ return 0; }
390
391 const char* encryptionHandlingDialog(){ return 0; }
392
393 const char* encryptionReceiverDialog(){ return 0; }
394
395 void setEncryptionAlgorithm( EncryptionAlgorithm cryptAlg )
396 {
397   config.encryptionAlgorithm = cryptAlg;
398 }
399
400 EncryptionAlgorithm encryptionAlgorithm()
401 {
402   return config.encryptionAlgorithm;
403 }
404
405 void setEncryptEmail( EncryptEmail cryptMode )
406 {
407   config.encryptEmail = cryptMode;
408 }
409
410 EncryptEmail encryptEmail()
411 {
412   return config.encryptEmail;
413 }
414
415
416
417
418
419
420 void setWarnSendUnencrypted( bool flag )
421 {
422   config.warnSendUnencrypted = flag;
423 }
424
425 bool warnSendUnencrypted()
426 {
427   return config.warnSendUnencrypted;
428 }
429
430
431
432
433
434
435
436
437
438 void setSaveMessagesEncrypted( bool flag )
439 {
440   config.saveMessagesEncrypted = flag;
441 }
442
443 bool saveMessagesEncrypted()
444 {
445   return config.saveMessagesEncrypted;
446 }
447
448
449
450
451
452
453
454 void setCheckCertificatePath( bool flag )
455 {
456   config.checkCertificatePath = flag;
457 }
458
459 bool checkCertificatePath()
460 {
461   return config.checkCertificatePath;
462 }
463
464
465
466
467
468
469
470
471 void setCheckEncryptionCertificatePathToRoot( bool flag )
472 {
473   config.checkEncryptionCertificatePathToRoot = flag;
474 }
475
476 bool checkEncryptionCertificatePathToRoot()
477 {
478   return config.checkEncryptionCertificatePathToRoot;
479 }
480
481
482
483
484
485
486
487 void setReceiverCertificateExpiryNearWarning( bool flag )
488 {
489   config.receiverCertificateExpiryNearWarning = flag;
490 }
491
492 bool receiverCertificateExpiryNearWarning()
493 {
494   return config.receiverCertificateExpiryNearWarning;
495 }
496
497 void setReceiverCertificateExpiryNearWarningInterval( int interval )
498 {
499   config.receiverCertificateExpiryNearWarningInterval = interval;
500 }
501
502 int receiverCertificateExpiryNearWarningInterval()
503 {
504   return config.receiverCertificateExpiryNearWarningInterval;
505 }
506
507 void setCertificateInChainExpiryNearWarning( bool flag )
508 {
509   config.certificateInChainExpiryNearWarning = flag;
510 }
511
512 bool certificateInChainExpiryNearWarning()
513 {
514   return config.certificateInChainExpiryNearWarning;
515 }
516
517 void setCertificateInChainExpiryNearWarningInterval( int interval )
518 {
519   config.certificateInChainExpiryNearWarningInterval = interval;
520 }
521
522 int certificateInChainExpiryNearWarningInterval()
523 {
524   return config.certificateInChainExpiryNearWarningInterval;
525 }
526
527 void setReceiverEmailAddressNotInCertificateWarning( bool flag )
528 {
529   config.receiverEmailAddressNotInCertificateWarning = flag;
530 }
531
532 bool receiverEmailAddressNotInCertificateWarning()
533 {
534   return config.receiverEmailAddressNotInCertificateWarning;
535 }
536
537
538
539
540
541
542
543
544 void setEncryptionUseCRLs( bool flag )
545 {
546   config.encryptionUseCRLs = flag;
547 }
548
549 bool encryptionUseCRLs()
550 {
551   return config.encryptionUseCRLs;
552 }
553
554 void setEncryptionCRLExpiryNearWarning( bool flag )
555 {
556   config.encryptionCRLExpiryNearWarning = flag;
557 }
558
559 bool encryptionCRLExpiryNearWarning()
560 {
561   return config.encryptionCRLExpiryNearWarning;
562 }
563
564 void setEncryptionCRLNearExpiryInterval( int interval )
565 {
566   config.encryptionCRLNearExpiryInterval = interval;
567 }
568
569 int encryptionCRLNearExpiryInterval()
570 {
571   return config.encryptionCRLNearExpiryInterval;
572 }
573
574
575 const char* directoryServiceConfigurationDialog(){ return 0; }
576
577 void appendDirectoryServer( const char* servername, int port,
578                             const char* description )
579 {
580   struct DirectoryServer *servers = NULL;
581   servers = xtryrealloc( config.directoryServers,
582                          (1+config.numDirectoryServers) * sizeof *servers );
583   if( servers ) {
584     config.directoryServers = servers;
585     servers[ config.numDirectoryServers ].servername  = servername;
586     servers[ config.numDirectoryServers ].port        = port;
587     servers[ config.numDirectoryServers ].description = description;
588     config.numDirectoryServers += 1;
589   }
590 }
591
592 void setDirectoryServers( struct DirectoryServer server[], unsigned int size )
593 {
594   struct DirectoryServer *servers = NULL;
595   servers = xtrycalloc ( size, sizeof *servers );
596   if( servers ) {
597     _gpgme_free( config.directoryServers );
598     config.directoryServers = servers;
599     config.numDirectoryServers = size;
600   }
601 }
602
603 struct DirectoryServer * directoryServers( int* numServers )
604 {
605   if( numServers )
606     *numServers = config.numDirectoryServers;
607   return config.directoryServers;
608 };
609
610 void setCertificateSource( CertificateSource source )
611 {
612   config.certificateSource = source;
613 }
614
615 CertificateSource certificateSource()
616 {
617   return config.certificateSource;
618 }
619
620 void setCRLSource( CertificateSource source )
621 {
622   config.cRLSource = source;
623 }
624
625 CertificateSource crlSource()
626 {
627   return config.cRLSource;
628 }
629
630
631 bool certificateValidity( const char* certificate,
632                           int* level ){ return true; }
633
634
635 bool signMessage( const char*  cleartext,
636                   const char** ciphertext,
637                   const char*  certificate )
638 {
639 /*
640   GpgmeCtx ctx;
641   GpgmeData data, sig;
642
643   gpgme_new (&ctx);
644   gpgme_set_armor (ctx, 1);
645   gpgme_set_textmode (ctx, 1);
646
647   gpgme_data_new_from_mem (&data, mime_object,
648                             mime_object_len, TRUE );
649   gpgme_data_new ( &sig );
650   gpgme_op_sign (ctx, data, sig, GPGME_SIG_MODE_DETACH );
651
652   fputs ( "Content-Type: multipart/signed;\r\n"
653           "              protocol=\"application/pgp-signature\";\r\n"
654           "              boundary=\"42=.42=.42=.42\"\r\n"
655           "\r\n--42=.42=.42=.42\r\n", stdout );
656
657   gpgme_data_rewind (data);
658   while ( !gpgme_data_read (data, buf, sizeof buf, &nread ) ) {
659         fwrite (buf, nread, 1, stdout );
660   }
661   fputs ( "\r\n--42=.42=.42=.42--\r\n"
662           "Content-Type: application/pgp-signature\r\n\r\n", stdout);
663
664   gpgme_data_rewind (sig);
665   while ( !gpgme_data_read (sig, buf, sizeof buf, &nread ) ) {
666         fwrite (buf, nread, 1, stdout );
667   }
668   fputs ( "\r\n--42=.42=.42=.42--\r\n", stdout );
669
670   gpgme_release (ctx);
671   gpgme_data_release(data);
672   gpgme_data_release(sig);
673 */
674   return true;
675 }
676
677 bool checkMessageSignature( const char* ciphertext, const char**
678         cleartext, struct SignatureMetaData* sigmeta ){ return true; }
679
680 bool storeCertificatesFromMessage(
681         const char* ciphertext ){ return true; }
682
683
684 bool encryptMessage( const char* cleartext,
685                      const char** ciphertext ){ return true; }
686
687 bool encryptAndSignMessage( const char* cleartext,
688           const char** ciphertext, const char* certificate,
689           struct SignatureMetaData* sigmeta ){ return true; }
690
691 bool decryptMessage( const char* ciphertext, const
692           char** cleartext, const char* certificate ){ return true; }
693
694 bool decryptAndCheckMessage( const char* ciphertext,
695           const char** cleartext, const char* certificate,
696           struct SignatureMetaData* sigmeta ){ return true; }
697
698
699 const char* requestCertificateDialog(){ return 0; }
700
701 bool requestDecentralCertificate( const char* name, const char*
702           email, const char* organization, const char* department,
703           const char* ca_address ){ return true; }
704
705 bool requestCentralCertificateAndPSE( const char* name,
706           const char* email, const char* organization, const char* department,
707           const char* ca_address ){ return true; }
708
709 bool createPSE(){ return true; }
710
711 bool registerCertificate( const char* certificate ){ return true; }
712
713 bool requestCertificateProlongation( const char* certificate,
714                                      const char* ca_address ){ return true; }
715
716 const char* certificateChain(){ return 0; }
717
718 bool deleteCertificate( const char* certificate ){ return true; }
719
720 bool archiveCertificate( const char* certificate ){ return true; }
721
722
723 const char* displayCRL(){ return 0; }
724
725 void updateCRL(){}