update dev300-m58
[ooovba.git] / extensions / workben / testpgp.cxx
blob7a3b29bb2a04bc92a286e5e42a87f3ff6edd8fd8
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: testpgp.cxx,v $
10 * $Revision: 1.4 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_extensions.hxx"
33 #include <sal/types.h>
34 #include <rtl/memory.h>
35 #ifndef _RTL_WSTRING_
36 #include <rtl/wstring>
37 #endif
38 #include <vos/macros.hxx>
40 #ifndef _USR_SMARTSERVICES_HXX_
41 #include <usr/smartservices.hxx>
42 #endif
43 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
44 #include <com/sun/star/io/XInputStream.hpp>
45 #include <com/sun/star/io/XOutputStream.hpp>
46 #include <com/sun/star/pgp/RecipientsEvent.hpp>
47 #include <com/sun/star/pgp/SignatureEvent.hpp>
48 #include <com/sun/star/pgp/XPGPDecoder.hpp>
49 #include <com/sun/star/pgp/XPGPDecoderListener.hpp>
50 #include <com/sun/star/pgp/XPGPEncoder.hpp>
51 #include <com/sun/star/pgp/XPGPPreferences.hpp>
52 #include <com/sun/star/uno/XInterface.hpp>
53 #include <com/sun/star/uno/Any.h>
54 #include <com/sun/star/uno/Reference.h>
55 #include <com/sun/star/uno/Sequence.hxx>
56 #include <cppuhelper/weak.hxx>
58 #include <stdio.h>
59 #include <stdlib.h>
60 #include <string.h>
62 #include <fcntl.h>
63 #include <io.h>
65 using namespace com::sun::star::lang;
66 using namespace com::sun::star::io;
67 using namespace com::sun::star::pgp;
68 using namespace com::sun::star::uno;
70 /*========================================================================
72 * DataSource_Impl interface.
74 *======================================================================*/
75 class DataSource_Impl :
76 public OWeakObject,
77 public XInputStream
79 Sequence<sal_Int8> m_buffer;
80 sal_Int32 m_position;
81 int m_fd;
83 public:
84 DataSource_Impl (int fd = 0);
85 virtual ~DataSource_Impl (void);
87 void setBuffer (const Sequence<sal_Int8> &rBuffer);
89 /** XInterface.
91 virtual sal_Bool SAL_CALL queryInterface (
92 const Uik &rUik, Any &rIfc) throw(RuntimeException);
94 virtual void SAL_CALL acquire (void) throw(RuntimeException);
96 virtual void SAL_CALL release (void) throw(RuntimeException);
98 /** XInputStream.
100 virtual sal_Int32 SAL_CALL readBytes (
101 Sequence<sal_Int8> &rData, sal_Int32 nBytesToRead)
102 throw (NotConnectedException,
103 BufferSizeExceededException,
104 IOException);
106 virtual sal_Int32 SAL_CALL readSomeBytes (
107 Sequence<sal_Int8> &rData, sal_Int32 nMaxBytesToRead)
108 throw (NotConnectedException,
109 BufferSizeExceededException,
110 IOException);
112 virtual void SAL_CALL skipBytes (sal_Int32 nBytesToSkip)
113 throw (NotConnectedException,
114 BufferSizeExceededException,
115 IOException);
117 virtual sal_Int32 SAL_CALL available (void)
118 throw (NotConnectedException, IOException);
120 virtual void SAL_CALL closeInput (void)
121 throw (NotConnectedException, IOException);
124 /*========================================================================
126 * DataSink_Impl interface.
128 *======================================================================*/
129 class DataSink_Impl :
130 public OWeakObject,
131 public XOutputStream
133 Sequence<sal_Int8> m_buffer;
135 public:
136 DataSink_Impl (void);
137 virtual ~DataSink_Impl (void);
139 const Sequence<sal_Int8>& getBuffer (void) const { return m_buffer; }
141 /** XInterface.
143 virtual sal_Bool SAL_CALL queryInterface (
144 const Uik &rUik, Any &rIfc) throw(RuntimeException);
146 virtual void SAL_CALL acquire (void) throw(RuntimeException);
147 virtual void SAL_CALL release (void) throw(RuntimeException);
149 /** XOutputStream.
151 virtual void SAL_CALL writeBytes (
152 const Sequence<sal_Int8> &rBuffer)
153 throw (NotConnectedException,
154 BufferSizeExceededException,
155 IOException);
157 virtual void SAL_CALL flush (void)
158 throw (NotConnectedException,
159 BufferSizeExceededException,
160 IOException);
162 virtual void SAL_CALL closeOutput (void)
163 throw (NotConnectedException,
164 BufferSizeExceededException,
165 IOException);
168 /*========================================================================
170 * DecoderListener_Impl interface.
172 *======================================================================*/
173 class DecoderListener_Impl :
174 public OWeakObject,
175 public XPGPDecoderListener
177 public:
178 DecoderListener_Impl (void);
179 virtual ~DecoderListener_Impl (void);
181 /** XInterface.
183 virtual sal_Bool SAL_CALL queryInterface (
184 const Uik &rUik, Any &rIfc) throw(RuntimeException);
186 virtual void SAL_CALL acquire (void) throw(RuntimeException);
188 virtual void SAL_CALL release (void) throw(RuntimeException);
190 /** XEventListener.
192 virtual void SAL_CALL disposing (const EventObject &rSource);
194 /** XPGPDecoderListener.
196 virtual void SAL_CALL decrypted (const RecipientsEvent &rEvent);
197 virtual void SAL_CALL verified (const SignatureEvent &rEvent);
200 /*========================================================================
202 * DataSource_Impl implementation.
204 *======================================================================*/
206 * DataSource_Impl.
208 DataSource_Impl::DataSource_Impl (int fd)
209 : m_fd (fd), m_position (0)
214 * ~DataSource_Impl.
216 DataSource_Impl::~DataSource_Impl (void)
221 * DataSource_Impl: setBuffer.
223 void DataSource_Impl::setBuffer (const Sequence<sal_Int8> &rBuffer)
225 m_buffer = rBuffer;
226 if (!m_buffer.getLength())
228 // Fill buffer from file descriptor.
229 char buffer[1024];
230 rtl_zeroMemory (buffer, sizeof(buffer));
232 int k = read (m_fd, buffer, sizeof(buffer));
233 while (k > 0)
235 sal_Int32 n = m_buffer.getLength();
236 m_buffer.realloc (n + k);
238 rtl_copyMemory (m_buffer.getArray() + n, buffer, k);
239 n += k;
241 rtl_zeroMemory (buffer, k);
242 k = read (m_fd, buffer, sizeof(buffer));
245 m_position = 0;
249 * XInterface: queryInterface.
251 sal_Bool SAL_CALL DataSource_Impl::queryInterface (
252 const Uik &rUik, Any &rIfc) throw(RuntimeException)
254 if (com::sun::star::uno::queryInterface (
255 rUik, rIfc,
256 SAL_STATIC_CAST (XInputStream*, this)))
257 return sal_True;
258 else
259 return OWeakObject::queryInterface (rUik, rIfc);
263 * XInterface: acquire.
265 void SAL_CALL DataSource_Impl::acquire (void) throw(RuntimeException)
267 OWeakObject::acquire();
271 * XInterface: release.
273 void SAL_CALL DataSource_Impl::release (void) throw(RuntimeException)
275 OWeakObject::release();
279 * XInputStream: readBytes.
281 sal_Int32 SAL_CALL DataSource_Impl::readBytes (
282 Sequence<sal_Int8> &rData, sal_Int32 nBytesToRead)
283 throw (NotConnectedException, BufferSizeExceededException, IOException)
285 if (nBytesToRead < 0)
286 throw IOException();
288 sal_Int32 k = m_buffer.getLength() - m_position;
289 k = VOS_BOUND(k, 0, nBytesToRead);
290 if (k > 0)
292 rData.realloc(k);
293 rtl_copyMemory (
294 rData.getArray(), m_buffer.getConstArray() + m_position, k);
295 m_position += k;
297 return k;
301 * XInputStream: readSomeBytes.
303 sal_Int32 SAL_CALL DataSource_Impl::readSomeBytes (
304 Sequence<sal_Int8> &rData, sal_Int32 nMaxBytesToRead)
305 throw (NotConnectedException, BufferSizeExceededException, IOException)
307 return readBytes (rData, nMaxBytesToRead);
311 * XInputStream: skipBytes.
313 void SAL_CALL DataSource_Impl::skipBytes (sal_Int32 nBytesToSkip)
314 throw (NotConnectedException, BufferSizeExceededException, IOException)
316 if (nBytesToSkip < 0)
317 throw IOException();
319 m_position += nBytesToSkip;
323 * XInputStream: available.
325 sal_Int32 SAL_CALL DataSource_Impl::available (void)
326 throw (NotConnectedException, IOException)
328 sal_Int32 k = m_buffer.getLength() - m_position;
329 return ((k > 0) ? k : 0);
333 * XInputStream: closeInput.
335 void SAL_CALL DataSource_Impl::closeInput (void)
336 throw (NotConnectedException, IOException)
340 /*========================================================================
342 * DataSink_Impl implementation.
344 *======================================================================*/
346 * DataSink_Impl.
348 DataSink_Impl::DataSink_Impl (void)
353 * ~DataSink_Impl.
355 DataSink_Impl::~DataSink_Impl (void)
360 * XInterface: queryInterface.
362 sal_Bool SAL_CALL DataSink_Impl::queryInterface (
363 const Uik &rUik, Any &rIfc) throw(RuntimeException)
365 if (com::sun::star::uno::queryInterface (
366 rUik, rIfc,
367 SAL_STATIC_CAST (XOutputStream*, this)))
368 return sal_True;
369 else
370 return OWeakObject::queryInterface (rUik, rIfc);
374 * XInterface: acquire.
376 void SAL_CALL DataSink_Impl::acquire (void) throw(RuntimeException)
378 OWeakObject::acquire();
382 * XInterface: release.
384 void SAL_CALL DataSink_Impl::release (void) throw(RuntimeException)
386 OWeakObject::release();
390 * XOutputStream: writeBytes.
392 void SAL_CALL DataSink_Impl::writeBytes (const Sequence<sal_Int8> &rBuffer)
393 throw (NotConnectedException, BufferSizeExceededException, IOException)
395 if (rBuffer.getLength())
397 sal_Int32 n = m_buffer.getLength();
398 m_buffer.realloc (n + rBuffer.getLength());
400 rtl_copyMemory (
401 m_buffer.getArray() + n,
402 rBuffer.getConstArray(),
403 rBuffer.getLength());
408 * XOutputStream: flush.
410 void SAL_CALL DataSink_Impl::flush (void)
411 throw (NotConnectedException, BufferSizeExceededException, IOException)
413 if (m_buffer.getLength())
415 // Write data to stdout.
416 const sal_Int8 *pData = m_buffer.getConstArray();
417 sal_Int32 nData = m_buffer.getLength();
419 int prev = ::setmode (1, O_BINARY);
420 if (!(prev < 0))
422 int k = ::write (1, pData, nData);
423 if (k > 0)
424 ::write (1, "\n", 1);
425 ::setmode (1, prev);
431 * XOutputStream: closeOutput.
433 void SAL_CALL DataSink_Impl::closeOutput (void)
434 throw (NotConnectedException, BufferSizeExceededException, IOException)
436 flush();
439 /*========================================================================
441 * DecoderListener_Impl implementation.
443 *======================================================================*/
445 * DecoderListener_Impl.
447 DecoderListener_Impl::DecoderListener_Impl (void)
452 * ~DecoderListener_Impl.
454 DecoderListener_Impl::~DecoderListener_Impl (void)
459 * XInterface: queryInterface.
461 sal_Bool SAL_CALL DecoderListener_Impl::queryInterface (
462 const Uik &rUik, Any &rIfc) throw(RuntimeException)
464 if (com::sun::star::uno::queryInterface (
465 rUik, rIfc,
466 SAL_STATIC_CAST (XEventListener*, this),
467 SAL_STATIC_CAST (XPGPDecoderListener*, this)))
468 return sal_True;
469 else
470 return OWeakObject::queryInterface (rUik, rIfc);
474 * XInterface: acquire.
476 void SAL_CALL DecoderListener_Impl::acquire (void) throw(RuntimeException)
478 OWeakObject::acquire();
482 * XInterface: release.
484 void SAL_CALL DecoderListener_Impl::release (void) throw(RuntimeException)
486 OWeakObject::release();
490 * XEventListener: disposing.
492 void SAL_CALL DecoderListener_Impl::disposing (const EventObject &rSource)
497 * XPGPDecoderListener: decrypted.
499 void SAL_CALL DecoderListener_Impl::decrypted (const RecipientsEvent &rEvent)
504 * XPGPDecoderListener: verified.
506 void SAL_CALL DecoderListener_Impl::verified (const SignatureEvent &rEvent)
510 /*========================================================================
512 * Main.
514 *======================================================================*/
515 inline rtl::OWString S2U (const sal_Char *ascii)
517 return rtl::OWString::createFromAscii (ascii);
520 #if 0 /* OLD */
523 * queryModuleActivator.
525 BOOL queryModuleActivator (
526 const XServiceManagerRef &rxManager,
527 XServiceActivatorRef &rxActivator)
529 XServiceProviderRef xProv;
530 XInterfaceRef xProvInst;
532 xProv = rxManager->queryServiceProvider (
533 L"stardiv.uno.ServiceActivator.module");
534 if (!xProv.is())
536 printf ("Error: no ServiceActivator service.\n");
537 return FALSE;
540 xProvInst = xProv->createInstance();
541 if (!xProvInst.is())
543 printf ("Error: no ServiceActivator instance.\n");
544 return FALSE;
547 return xProvInst->queryInterface (
548 XServiceActivator::getSmartUik(), rxActivator);
552 * install.
554 BOOL install (
555 const XServiceActivatorRef &rxActivator,
556 const char *prefix)
558 String aModule ("module://");
559 char pBuffer[1024];
561 NAMESPACE_VOS(ORealDynamicLoader)::computeModuleName (
562 prefix, pBuffer, sizeof(pBuffer));
563 aModule += pBuffer;
565 return rxActivator->install (
566 StringToUString (aModule, CHARSET_SYSTEM));
570 * uninstall.
572 BOOL uninstall (
573 const XServiceActivatorRef &rxActivator,
574 const char *prefix)
576 String aModule ("module://");
577 char pBuffer[1024];
579 NAMESPACE_VOS(ORealDynamicLoader)::computeModuleName (
580 prefix, pBuffer, sizeof(pBuffer));
581 aModule += pBuffer;
583 return rxActivator->deinstall (
584 StringToUString (aModule, CHARSET_SYSTEM));
587 #endif /* OLD */
590 * main.
592 int SAL_CALL main (int argc, char **argv)
594 enum Option
596 OPTION_INSTALL = 0x01,
597 OPTION_UNINSTALL = 0x02,
599 OPTION_DECRYPT = 0x04,
600 OPTION_ENCRYPT = 0x08,
601 OPTION_SIGN = 0x10,
603 OPTION_FILE = 0x20,
604 OPTION_HELP = 0x40
607 int fd = 0;
608 unsigned long nOptions = 0;
610 for (int i = 1; i < argc; i++)
612 const char *opt = argv[i];
613 if (opt[0] == '-')
615 switch (opt[1])
617 case 'i':
618 nOptions |= OPTION_INSTALL;
619 break;
621 case 'u':
622 nOptions |= OPTION_UNINSTALL;
623 break;
625 case 'd':
626 nOptions |= OPTION_DECRYPT;
627 break;
629 case 'e':
630 nOptions |= OPTION_ENCRYPT;
631 break;
633 case 's':
634 nOptions |= OPTION_SIGN;
635 break;
637 case 'f':
638 nOptions |= OPTION_FILE;
639 break;
641 case 'h':
642 default:
643 nOptions |= OPTION_HELP;
644 break;
647 else
649 if (nOptions & OPTION_FILE)
651 if ((fd = open (argv[i], O_RDONLY | O_BINARY)) < 0)
653 printf ("Error: can't open file: %s\n", argv[i]);
654 exit (0);
660 if ((nOptions == 0) || (nOptions & OPTION_HELP))
662 printf ("Options:\n");
663 printf ("-i\tinstall module\n");
664 printf ("-u\tuninstall module\n");
665 printf ("-d\tdecrypt and verify\n");
666 printf ("-e\tencrypt test pattern\n");
667 printf ("-s\tsign test pattern\n");
668 printf ("-h\thelp\n");
669 exit (0);
672 Reference<XMultiServiceFactory> xManager (
673 usr::createDefaultSmartRegistryServiceFactory());
674 if (!xManager.is())
676 printf ("Error: no ProcessServiceManager.\n");
677 exit (1);
679 usr::setProcessServiceFactory (xManager);
681 if (nOptions & OPTION_INSTALL)
683 #if 0 /* OLD */
684 XServiceActivatorRef xActivator;
685 if (queryModuleActivator (xManager, xActivator))
687 if (install (xActivator, "pgp"))
688 printf ("Module PGP installed.\n");
689 else
690 printf ("Error: module PGP not installed.\n");
692 nOptions &= ~OPTION_INSTALL;
693 #endif /* OLD */
696 if (nOptions & (OPTION_DECRYPT | OPTION_ENCRYPT | OPTION_SIGN))
698 Reference<XMultiServiceFactory> xProv (
699 xManager->createInstance (
700 S2U("com.sun.star.pgp.PGPFactory")),
701 UNO_QUERY);
702 if (!xProv.is())
704 printf ("Error: no PGPFactory service.\n");
705 exit (1);
708 Reference<XInterface> xProvInst (
709 xProv->createInstance (
710 S2U("com.sun.star.pgp.SimplePGPMailer")));
711 if (!xProvInst.is())
713 printf ("Error: no SimplePGPMailer service.\n");
714 exit (2);
717 Reference<XPGPPreferences> xPrefs (xProvInst, UNO_QUERY);
718 if (xPrefs.is())
720 unsigned long nDefaults = 0;
722 if (xPrefs->getEncryptByDefault())
723 nDefaults |= OPTION_ENCRYPT;
724 if (xPrefs->getSignByDefault())
725 nDefaults |= OPTION_SIGN;
726 if (xPrefs->getAutoDecrypt())
727 nDefaults |= OPTION_DECRYPT;
729 if (nDefaults)
734 static const sal_Int8 pData[] = "" /* "Hello PGP World." */;
735 Sequence<sal_Int8> buffer (pData, sizeof (pData) - 1);
737 if (nOptions & (OPTION_ENCRYPT | OPTION_SIGN))
739 Reference<XPGPEncoder> xEncoder (xProvInst, UNO_QUERY);
740 if (!xEncoder.is())
742 printf ("Error: no PGPEncoder interface.\n");
743 exit (4);
746 DataSource_Impl *source = new DataSource_Impl (fd);
747 source->setBuffer (buffer);
749 DataSink_Impl *sink = new DataSink_Impl;
751 Reference<XInputStream> xPlainText (source);
752 Reference<XOutputStream> xCipherText (sink);
754 if (nOptions & OPTION_ENCRYPT)
756 rtl::OWString aRecipients[] =
758 S2U("er@stardiv.de"),
759 // L"mhu@stardivision.de",
760 S2U("mhu@rabbit")
763 sal_Int32 nRecipients =
764 sizeof(aRecipients) / sizeof(aRecipients[0]);
766 if (nOptions & OPTION_SIGN)
768 xEncoder->encryptAndSign (
769 Sequence<rtl::OWString>(aRecipients, nRecipients),
770 xPlainText,
771 xCipherText);
772 nOptions &= ~OPTION_SIGN;
774 else
776 xEncoder->encrypt (
777 Sequence<rtl::OWString>(aRecipients, nRecipients),
778 xPlainText,
779 xCipherText);
781 nOptions &= ~OPTION_ENCRYPT;
784 if (nOptions & OPTION_SIGN)
786 sal_Bool bDataIsAscii = (fd == 0); // stdin.
788 xEncoder->sign (
789 bDataIsAscii,
790 xPlainText,
791 xCipherText);
792 nOptions &= ~OPTION_SIGN;
795 buffer = sink->getBuffer();
798 if (nOptions & OPTION_DECRYPT)
800 Reference<XPGPDecoder> xDecoder (xProvInst, UNO_QUERY);
801 if (!xDecoder.is())
803 printf ("Error: no PGPDecoder interface.\n");
804 exit (5);
807 DataSource_Impl *source = new DataSource_Impl;
808 source->setBuffer (buffer);
810 DataSink_Impl *sink = new DataSink_Impl;
812 Reference<XInputStream> xCipherText (source);
813 Reference<XOutputStream> xPlainText (sink);
815 Reference<XPGPDecoderListener> xListener (
816 new DecoderListener_Impl);
817 xDecoder->addDecoderListener (xListener);
819 xDecoder->decryptAndVerify (
820 xCipherText,
821 xPlainText);
822 nOptions &= ~OPTION_DECRYPT;
824 xDecoder->removeDecoderListener (xListener);
826 buffer = sink->getBuffer();
830 if (nOptions & OPTION_UNINSTALL)
832 #if 0 /* OLD */
833 XServiceActivatorRef xActivator;
834 if (queryModuleActivator (xManager, xActivator))
836 if (uninstall (xActivator, "pgp"))
837 printf ("Module PGP uninstalled.\n");
838 else
839 printf ("Error: module PGP not uninstalled.\n");
841 nOptions &= ~OPTION_UNINSTALL;
842 #endif /* OLD */
845 return 0;