248eebe30a0b102fe9ba98cb30ab65baa7aab3d8
[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 #ifdef HAVE_CONFIG_H
46 #include <config.h>
47 #endif
48 #include <stdio.h>
49 #include <stdlib.h>
50 #include <string.h>
51 #include <assert.h>
52 #include <errno.h>
53
54 #include "gpgme.h"
55
56 #include "cryptplug.h"
57
58
59 typedef struct {
60   const char*             signatureKeyCertificate;
61   SignatureAlgorithm      signatureAlgorithm;
62   SendCertificates        sendCertificates;
63   SignEmail               signEmail;
64   bool                    saveSentSignatures;
65   bool                    warnNoCertificate;
66   PinRequests             numPINRequests;
67   bool                    checkSignatureCertificatePathToRoot;
68   bool                    signatureUseCRLs;
69   EncryptionAlgorithm     encryptionAlgorithm;
70   EncryptEmail            encryptEmail;
71   bool                    saveMessagesEncrypted;
72   bool                    checkEncryptionCertificatePathToRoot;
73   bool                    encryptionUseCRLs;
74   bool                    encryptionCRLExpiryNearWarning;
75   int                     encryptionCRLNearExpiryInterval;
76   struct DirectoryServer *directoryServers;
77   unsigned int            numDirectoryServers;
78   CertificateSource       certificateSource;
79   CertificateSource       cRLSource;
80   bool                    warnSendUnsigned;
81   int                     numPINRequestsInterval;
82   bool                    signatureCertificateExpiryNearWarning;
83   int                     signatureCertificateExpiryNearInterval;
84   bool                    cACertificateExpiryNearWarning;
85   int                     cACertificateExpiryNearInterval;
86   bool                    rootCertificateExpiryNearWarning;
87   int                     rootCertificateExpiryNearInterval;
88   bool                    warnSendUnencrypted;
89   bool                    checkCertificatePath;
90   bool                    receiverCertificateExpiryNearWarning;
91   int                     receiverCertificateExpiryNearWarningInterval;
92   bool                    certificateInChainExpiryNearWarning;
93   int                     certificateInChainExpiryNearWarningInterval;
94   bool                    receiverEmailAddressNotInCertificateWarning;
95 } Config;
96
97
98 Config config;
99
100
101 #define NEAR_EXPIRY 14
102
103 bool initialize()
104 {
105   config.signatureKeyCertificate              = "";
106   config.signatureAlgorithm                   = SignAlg_SHA1;
107   config.sendCertificates                     = SendCert_SendChainWithRoot;
108   config.signEmail                            = SignEmail_SignAll;
109   config.saveSentSignatures                   = true;
110   config.warnNoCertificate                    = true;
111   config.numPINRequests                       = PinRequest_Always;
112   config.checkSignatureCertificatePathToRoot  = true;
113   config.signatureUseCRLs                     = true;
114   config.encryptionAlgorithm                  = EncryptAlg_RSA;
115   config.encryptEmail                         = EncryptEmail_Ask;
116   config.saveMessagesEncrypted                = true;
117   config.checkEncryptionCertificatePathToRoot = true;
118   config.encryptionUseCRLs                    = true;
119   config.encryptionCRLExpiryNearWarning       = true;
120   config.encryptionCRLNearExpiryInterval      = NEAR_EXPIRY;
121   config.directoryServers                     = NULL;
122   config.numDirectoryServers                  = 0;
123   config.certificateSource                    = CertSrc_Server;
124   config.cRLSource                            = CertSrc_Server;
125   config.warnSendUnsigned                             = true;
126   config.numPINRequestsInterval                       = NEAR_EXPIRY;
127   config.signatureCertificateExpiryNearWarning        = true;
128   config.signatureCertificateExpiryNearInterval       = NEAR_EXPIRY;
129   config.cACertificateExpiryNearWarning               = true;
130   config.cACertificateExpiryNearInterval              = NEAR_EXPIRY;
131   config.rootCertificateExpiryNearWarning             = true;
132   config.rootCertificateExpiryNearInterval            = NEAR_EXPIRY;
133   config.warnSendUnencrypted                          = false;
134   config.checkCertificatePath                         = true;
135   config.receiverCertificateExpiryNearWarning         = true;
136   config.receiverCertificateExpiryNearWarningInterval = NEAR_EXPIRY;
137   config.certificateInChainExpiryNearWarning          = true;
138   config.certificateInChainExpiryNearWarningInterval  = NEAR_EXPIRY;
139   config.receiverEmailAddressNotInCertificateWarning  = true;
140
141   return true;
142 };
143
144
145 void deinitialize()
146 {
147   unsigned int i;
148   for( i = 0; i < config.numDirectoryServers; ++i ) {
149     free( (char *)config.directoryServers[i].servername );
150     free( (char *)config.directoryServers[i].description );
151   }
152   free( config.directoryServers );
153 }
154
155
156 bool hasFeature( Feature flag )
157 {
158   switch ( flag ) {
159     case CryptPlugFeat_SignMessages:              return true;
160     case CryptPlugFeat_VerifySignatures:          return true;
161     case CryptPlugFeat_EncryptMessages:           return true;
162     case CryptPlugFeat_DecryptMessages:           return true;
163     // undefined or not yet implemented:
164     case CryptPlugFeat_undef:                     return false;
165     default:                                      return false;
166   }
167 }
168
169
170 void unsafeStationery( void** pixmap, const char** menutext, char* accel,
171           const char** tooltip, const char** statusbartext ){}
172
173 void signedStationery( void** pixmap, const char** menutext, char* accel,
174           const char** tooltip, const char** statusbartext ){}
175
176 void encryptedStationery( void** pixmap, const char**
177           menutext, char* accel,
178           const char** tooltip, const char** statusbartext ){}
179
180 void signedEncryptedStationery( void** pixmap, const char**
181           menutext, char* accel,
182           const char** tooltip, const char** statusbartext ){}
183
184 const char* signatureConfigurationDialog(){ return 0; }
185
186 const char* signatureKeySelectionDialog(){ return 0; }
187
188 const char* signatureAlgorithmDialog(){ return 0; }
189
190 const char* signatureHandlingDialog(){ return 0; }
191
192 void setSignatureKeyCertificate( const char* certificate )
193 {
194   config.signatureKeyCertificate = certificate;
195 }
196
197 const char* signatureKeyCertificate()
198 {
199   return config.signatureKeyCertificate;
200 }
201
202 void setSignatureAlgorithm( SignatureAlgorithm sigAlg )
203 {
204   config.signatureAlgorithm = sigAlg;
205 }
206
207 SignatureAlgorithm signatureAlgorithm()
208 {
209   return config.signatureAlgorithm;
210 }
211
212 void setSendCertificates( SendCertificates sendCert )
213 {
214   config.sendCertificates = sendCert;
215 }
216
217 SendCertificates sendCertificates()
218 {
219   return config.sendCertificates;
220 }
221
222 void setSignEmail( SignEmail signMail )
223 {
224   config.signEmail = signMail;
225 }
226
227 SignEmail signEmail()
228 {
229   return config.signEmail;
230 }
231
232
233
234
235
236 void setWarnSendUnsigned( bool flag )
237 {
238   config.warnSendUnsigned = flag;
239 }
240
241 bool warnSendUnsigned()
242 {
243   return config.warnSendUnsigned;
244 }
245
246
247
248
249
250
251 void setSaveSentSignatures( bool flag )
252 {
253   config.saveSentSignatures = flag;
254 }
255
256 bool saveSentSignatures()
257 {
258   return config.saveSentSignatures;
259 }
260
261 void setWarnNoCertificate( bool flag )
262 {
263   config.warnNoCertificate = flag;
264 }
265
266 bool warnNoCertificate()
267 {
268   return config.warnNoCertificate;
269 }
270
271 void setNumPINRequests( PinRequests reqMode )
272 {
273   config.numPINRequests = reqMode;
274 }
275
276 PinRequests numPINRequests()
277 {
278   return config.numPINRequests;
279 }
280
281
282
283
284
285 void setNumPINRequestsInterval( int interval )
286 {
287   config.numPINRequestsInterval = interval;
288 }
289
290 int numPINRequestsInterval()
291 {
292   return config.numPINRequestsInterval;
293 }
294
295
296
297
298
299
300
301 void setCheckSignatureCertificatePathToRoot( bool flag )
302 {
303   config.checkSignatureCertificatePathToRoot = flag;
304 }
305
306 bool checkSignatureCertificatePathToRoot()
307 {
308   return config.checkSignatureCertificatePathToRoot;
309 }
310
311 void setSignatureUseCRLs( bool flag )
312 {
313   config.signatureUseCRLs = flag;
314 }
315
316 bool signatureUseCRLs()
317 {
318   return config.signatureUseCRLs;
319 }
320
321
322
323
324
325
326 void setSignatureCertificateExpiryNearWarning( bool flag )
327 {
328   config.signatureCertificateExpiryNearWarning = flag;
329 }
330
331 bool signatureCertificateExpiryNearWarning( void )
332 {
333   return config.signatureCertificateExpiryNearWarning;
334 }
335
336 void setSignatureCertificateExpiryNearInterval( int interval )
337 {
338   config.signatureCertificateExpiryNearInterval = interval;
339 }
340
341 int signatureCertificateExpiryNearInterval( void )
342 {
343   return config.signatureCertificateExpiryNearInterval;
344 }
345
346 void setCACertificateExpiryNearWarning( bool flag )
347 {
348   config.cACertificateExpiryNearWarning = flag;
349 }
350
351 bool caCertificateExpiryNearWarning( void )
352 {
353   return config.cACertificateExpiryNearWarning;
354 }
355
356 void setCACertificateExpiryNearInterval( int interval )
357 {
358   config.cACertificateExpiryNearInterval = interval;
359 }
360
361 int caCertificateExpiryNearInterval( void )
362 {
363   return config.cACertificateExpiryNearInterval;
364 }
365
366 void setRootCertificateExpiryNearWarning( bool flag )
367 {
368   config.rootCertificateExpiryNearWarning = flag;
369 }
370
371 bool rootCertificateExpiryNearWarning( void )
372 {
373   return config.rootCertificateExpiryNearWarning;
374 }
375
376 void setRootCertificateExpiryNearInterval( int interval )
377 {
378   config.rootCertificateExpiryNearInterval = interval;
379 }
380
381 int rootCertificateExpiryNearInterval( void )
382 {
383   return config.rootCertificateExpiryNearInterval;
384 }
385
386
387
388
389
390
391
392
393 const char* encryptionConfigurationDialog(){ return 0; }
394
395 const char* encryptionAlgorithmDialog(){ return 0; }
396
397 const char* encryptionHandlingDialog(){ return 0; }
398
399 const char* encryptionReceiverDialog(){ return 0; }
400
401 void setEncryptionAlgorithm( EncryptionAlgorithm cryptAlg )
402 {
403   config.encryptionAlgorithm = cryptAlg;
404 }
405
406 EncryptionAlgorithm encryptionAlgorithm()
407 {
408   return config.encryptionAlgorithm;
409 }
410
411 void setEncryptEmail( EncryptEmail cryptMode )
412 {
413   config.encryptEmail = cryptMode;
414 }
415
416 EncryptEmail encryptEmail()
417 {
418   return config.encryptEmail;
419 }
420
421
422
423
424
425
426 void setWarnSendUnencrypted( bool flag )
427 {
428   config.warnSendUnencrypted = flag;
429 }
430
431 bool warnSendUnencrypted()
432 {
433   return config.warnSendUnencrypted;
434 }
435
436
437
438
439
440
441
442
443
444 void setSaveMessagesEncrypted( bool flag )
445 {
446   config.saveMessagesEncrypted = flag;
447 }
448
449 bool saveMessagesEncrypted()
450 {
451   return config.saveMessagesEncrypted;
452 }
453
454
455
456
457
458
459
460 void setCheckCertificatePath( bool flag )
461 {
462   config.checkCertificatePath = flag;
463 }
464
465 bool checkCertificatePath()
466 {
467   return config.checkCertificatePath;
468 }
469
470
471
472
473
474
475
476
477 void setCheckEncryptionCertificatePathToRoot( bool flag )
478 {
479   config.checkEncryptionCertificatePathToRoot = flag;
480 }
481
482 bool checkEncryptionCertificatePathToRoot()
483 {
484   return config.checkEncryptionCertificatePathToRoot;
485 }
486
487
488
489
490
491
492
493 void setReceiverCertificateExpiryNearWarning( bool flag )
494 {
495   config.receiverCertificateExpiryNearWarning = flag;
496 }
497
498 bool receiverCertificateExpiryNearWarning()
499 {
500   return config.receiverCertificateExpiryNearWarning;
501 }
502
503 void setReceiverCertificateExpiryNearWarningInterval( int interval )
504 {
505   config.receiverCertificateExpiryNearWarningInterval = interval;
506 }
507
508 int receiverCertificateExpiryNearWarningInterval()
509 {
510   return config.receiverCertificateExpiryNearWarningInterval;
511 }
512
513 void setCertificateInChainExpiryNearWarning( bool flag )
514 {
515   config.certificateInChainExpiryNearWarning = flag;
516 }
517
518 bool certificateInChainExpiryNearWarning()
519 {
520   return config.certificateInChainExpiryNearWarning;
521 }
522
523 void setCertificateInChainExpiryNearWarningInterval( int interval )
524 {
525   config.certificateInChainExpiryNearWarningInterval = interval;
526 }
527
528 int certificateInChainExpiryNearWarningInterval()
529 {
530   return config.certificateInChainExpiryNearWarningInterval;
531 }
532
533 void setReceiverEmailAddressNotInCertificateWarning( bool flag )
534 {
535   config.receiverEmailAddressNotInCertificateWarning = flag;
536 }
537
538 bool receiverEmailAddressNotInCertificateWarning()
539 {
540   return config.receiverEmailAddressNotInCertificateWarning;
541 }
542
543
544
545
546
547
548
549
550 void setEncryptionUseCRLs( bool flag )
551 {
552   config.encryptionUseCRLs = flag;
553 }
554
555 bool encryptionUseCRLs()
556 {
557   return config.encryptionUseCRLs;
558 }
559
560 void setEncryptionCRLExpiryNearWarning( bool flag )
561 {
562   config.encryptionCRLExpiryNearWarning = flag;
563 }
564
565 bool encryptionCRLExpiryNearWarning()
566 {
567   return config.encryptionCRLExpiryNearWarning;
568 }
569
570 void setEncryptionCRLNearExpiryInterval( int interval )
571 {
572   config.encryptionCRLNearExpiryInterval = interval;
573 }
574
575 int encryptionCRLNearExpiryInterval()
576 {
577   return config.encryptionCRLNearExpiryInterval;
578 }
579
580
581 const char* directoryServiceConfigurationDialog(){ return 0; }
582
583 void appendDirectoryServer( const char* servername,
584                             int         port,
585                             const char* description )
586 {
587   struct DirectoryServer *newServers = NULL;
588   newServers = realloc( config.directoryServers,
589                         (1+config.numDirectoryServers) * sizeof *newServers );
590   if( newServers ) {
591     config.directoryServers = newServers;
592     newServers[ config.numDirectoryServers ].servername =
593       malloc( strlen( servername ) );
594     if( newServers[ config.numDirectoryServers ].servername ) {
595       strcpy( (char *)newServers[ config.numDirectoryServers ].servername,
596         servername );
597       newServers[ config.numDirectoryServers ].description =
598         malloc( strlen(  description ) );
599       if( newServers[ config.numDirectoryServers ].description ) {
600         strcpy( (char *)newServers[ config.numDirectoryServers ].description,
601           description );
602         newServers[ config.numDirectoryServers ].port = port;
603         config.numDirectoryServers += 1;
604       }
605     }
606   }
607 }
608
609 void setDirectoryServers( struct DirectoryServer server[], unsigned int size )
610 {
611   unsigned int i;
612   int oldSize = config.numDirectoryServers;
613   struct DirectoryServer *newServers = NULL;
614   newServers = calloc ( size, sizeof *newServers );
615   if( newServers ) {
616     for( i=0; i < oldSize; ++i ) {
617       free( (char *)config.directoryServers[i].servername );
618       free( (char *)config.directoryServers[i].description );
619     }
620     free( config.directoryServers );
621     for( i=0; i < size; ++i ) {
622       newServers[ i ].servername = malloc( strlen( server[i].servername ) );
623       if( newServers[ i ].servername ) {
624         strcpy( (char *)newServers[ i ].servername, server[i].servername );
625         newServers[ i ].description = malloc( strlen( server[i].description ) );
626         if( newServers[ i ].description ) {
627           strcpy( (char *)newServers[ i ].description, server[i].description );
628           newServers[ i ].port = server[i].port;
629         }
630       }
631     }
632     config.directoryServers = newServers;
633     config.numDirectoryServers = size;
634   }
635 }
636
637 struct DirectoryServer * directoryServers( int* numServers )
638 {
639   if( numServers )
640     *numServers = config.numDirectoryServers;
641   return config.directoryServers;
642 };
643
644 void setCertificateSource( CertificateSource source )
645 {
646   config.certificateSource = source;
647 }
648
649 CertificateSource certificateSource()
650 {
651   return config.certificateSource;
652 }
653
654 void setCRLSource( CertificateSource source )
655 {
656   config.cRLSource = source;
657 }
658
659 CertificateSource crlSource()
660 {
661   return config.cRLSource;
662 }
663
664
665 bool certificateValidity( const char* certificate,
666                           int* level ){ return true; }
667
668
669 bool signMessage( const char*  cleartext,
670                   const char** ciphertext,
671                   const char*  certificate )
672 {
673   GpgmeCtx ctx;
674   GpgmeData data, sig;
675
676   char buf[1024];
677   size_t nread;
678
679
680   gpgme_new (&ctx);
681   gpgme_set_armor (ctx, 1);
682   gpgme_set_textmode (ctx, 1);
683
684   gpgme_data_new_from_mem (&data, cleartext,
685                             strlen( cleartext ), 1 );
686   gpgme_data_new ( &sig );
687   gpgme_op_sign (ctx, data, sig, GPGME_SIG_MODE_DETACH );
688
689   fputs ( "Content-Type: multipart/signed;\r\n"
690           "              protocol=\"application/pgp-signature\";\r\n"
691           "              boundary=\"42=.42=.42=.42\"\r\n"
692           "\r\n--42=.42=.42=.42\r\n",
693           stdout );
694
695   gpgme_data_rewind (data);
696   while ( !gpgme_data_read (data, buf, sizeof buf, &nread ) ) {
697         fwrite (buf, nread, 1, stdout );
698   }
699   fputs ( "\r\n--42=.42=.42=.42\r\n"
700           "Content-Type: application/pgp-signature\r\n\r\n", stdout);
701
702   gpgme_data_rewind (sig);
703   while ( !gpgme_data_read (sig, buf, sizeof buf, &nread ) ) {
704         fwrite (buf, nread, 1, stdout );
705   }
706   fputs ( "\r\n--42=.42=.42=.42--\r\n", stdout );
707
708   gpgme_release (ctx);
709   gpgme_data_release(data);
710   gpgme_data_release(sig);
711
712   return true;
713 }
714
715 bool checkMessageSignature( const char* ciphertext, const char**
716         cleartext, struct SignatureMetaData* sigmeta ){ return true; }
717
718 bool storeCertificatesFromMessage(
719         const char* ciphertext ){ return true; }
720
721
722 bool encryptMessage( const char* cleartext,
723                      const char** ciphertext ){ return true; }
724
725 bool encryptAndSignMessage( const char* cleartext,
726           const char** ciphertext, const char* certificate,
727           struct SignatureMetaData* sigmeta ){ return true; }
728
729 bool decryptMessage( const char* ciphertext, const
730           char** cleartext, const char* certificate ){ return true; }
731
732 bool decryptAndCheckMessage( const char* ciphertext,
733           const char** cleartext, const char* certificate,
734           struct SignatureMetaData* sigmeta ){ return true; }
735
736
737 const char* requestCertificateDialog(){ return 0; }
738
739 bool requestDecentralCertificate( const char* name, const char*
740           email, const char* organization, const char* department,
741           const char* ca_address ){ return true; }
742
743 bool requestCentralCertificateAndPSE( const char* name,
744           const char* email, const char* organization, const char* department,
745           const char* ca_address ){ return true; }
746
747 bool createPSE(){ return true; }
748
749 bool registerCertificate( const char* certificate ){ return true; }
750
751 bool requestCertificateProlongation( const char* certificate,
752                                      const char* ca_address ){ return true; }
753
754 const char* certificateChain(){ return 0; }
755
756 bool deleteCertificate( const char* certificate ){ return true; }
757
758 bool archiveCertificate( const char* certificate ){ return true; }
759
760
761 const char* displayCRL(){ return 0; }
762
763 void updateCRL(){}