summaryrefslogtreecommitdiffabout
authorulf69 <ulf69>2004-10-15 22:47:12 (UTC)
committer ulf69 <ulf69>2004-10-15 22:47:12 (UTC)
commit1821dcc08c8f6f2bc0fdb991c335ed03262d1443 (patch) (unidiff)
tree93249cb2cc3fbb57cc8cf6688afbc252629b62a4
parent71ee1be533cdbe7ce50c4f617254011fc640e88e (diff)
downloadkdepimpi-1821dcc08c8f6f2bc0fdb991c335ed03262d1443.zip
kdepimpi-1821dcc08c8f6f2bc0fdb991c335ed03262d1443.tar.gz
kdepimpi-1821dcc08c8f6f2bc0fdb991c335ed03262d1443.tar.bz2
*** empty log message ***
Diffstat (more/less context) (ignore whitespace changes)
-rw-r--r--pwmanager/pwmanager/libgcryptif.cpp420
-rw-r--r--pwmanager/pwmanager/libgcryptif.h5
-rw-r--r--pwmanager/pwmanager/pwmanagerE.pro10
-rw-r--r--pwmanager/pwmanager/pwmdoc.cpp77
4 files changed, 446 insertions, 66 deletions
diff --git a/pwmanager/pwmanager/libgcryptif.cpp b/pwmanager/pwmanager/libgcryptif.cpp
index 8e55144..6f3a994 100644
--- a/pwmanager/pwmanager/libgcryptif.cpp
+++ b/pwmanager/pwmanager/libgcryptif.cpp
@@ -430,6 +430,426 @@ void LibGCryptIf::unpadData(const unsigned char *buf,
430 --pos; 430 --pos;
431 } 431 }
432 *bufLen = pos; 432 *bufLen = pos;
433} 433}
434 434
435#endif // CONFIG_PWMANAGER_GCRY 435#endif // CONFIG_PWMANAGER_GCRY
436
437#ifdef CONFIG_PWMANAGER_CRYPTO
438
439#include "pwmdoc.h"
440#include "randomizer.h"
441
442#include <openssl/crypto.h>
443
444PwMerror LibGCryptIf::encrypt(unsigned char **outBuf,
445 size_t *outBufLen,
446 unsigned char *inBuf,
447 size_t inBufLen,
448 const unsigned char *key,
449 size_t keylen,
450 char _algo)
451{
452 PwMerror ret = e_success;
453 gcry_error_t err;
454 gcry_cipher_hd_t handle;
455 size_t blklen;
456 size_t unpaddedLen = inBufLen;
457 size_t cipherKeylen;
458 unsigned char *hashedKey;
459 unsigned char salt[STRING2KEY_SALTLEN];
460 int algo = mapCipherId(_algo);
461
462 if (!inBufLen || !keylen)
463 return e_invalidArg;
464
465 // test if algo is ready for encryption
466 err = gcry_cipher_algo_info(algo,
467 GCRYCTL_TEST_ALGO,
468 0, 0);
469 if (err != GPG_ERR_NO_ERROR) {
470 printDebug(string("LibGCryptIf::doEncrypt(): GCRYCTL_TEST_ALGO failed: ")
471 + gcry_strerror(err));
472 ret = e_cryptNotImpl;
473 goto out;
474 }
475 // get the algo block length
476 err = gcry_cipher_algo_info(algo,
477 GCRYCTL_GET_BLKLEN,
478 0,
479 &blklen);
480 if (err != GPG_ERR_NO_ERROR) {
481 printDebug(string("LibGCryptIf::doEncrypt(): GCRYCTL_GET_BLKLEN failed: ")
482 + gcry_strerror(err));
483 ret = e_cryptNotImpl;
484 goto out;
485 }
486 /* double check if we have enough space.
487 * We have only 1024 extra bytes for padding and salt.
488 */
489 BUG_ON(blklen > 1024 - STRING2KEY_SALTLEN);
490 // get the algo key length
491 err = gcry_cipher_algo_info(algo,
492 GCRYCTL_GET_KEYLEN,
493 0,
494 &cipherKeylen);
495 if (err != GPG_ERR_NO_ERROR) {
496 printDebug(string("LibGCryptIf::doEncrypt(): GCRYCTL_GET_KEYLEN failed: ")
497 + gcry_strerror(err));
498 ret = e_cryptNotImpl;
499 goto out;
500 }
501 // now open the algo and get a handle
502 err = gcry_cipher_open(&handle,
503 algo,
504 GCRY_CIPHER_MODE_CBC,
505 0);
506 if (err != GPG_ERR_NO_ERROR) {
507 printDebug(string("LibGCryptIf::doEncrypt(): gcry_cipher_open() failed: ")
508 + gcry_strerror(err));
509 ret = e_cryptNotImpl;
510 goto out;
511 }
512 // hash the "key" to a fixed size hash matching "cipherKeylen"
513 hashedKey = new unsigned char[cipherKeylen];
514 hashPassphrase(key, keylen, salt, hashedKey, cipherKeylen, true);
515 // so now set the hashed key
516 err = gcry_cipher_setkey(handle, hashedKey, cipherKeylen);
517 if (err != GPG_ERR_NO_ERROR) {
518 printDebug(string("LibGCryptIf::doEncrypt(): gcry_cipher_setkey() failed: ")
519 + gcry_strerror(err));
520 ret = e_cryptNotImpl;
521 delete [] hashedKey;
522 goto out_close;
523 }
524 delete [] hashedKey;
525 /* allocate a buffer for the encrypted data.
526 * The size of the buffer is the inBuf length, but blklen
527 * aligned and plus the length of the salt, that is appended.
528 */
529 *outBufLen = getBufLen(unpaddedLen, blklen) + STRING2KEY_SALTLEN;
530 *outBuf = new unsigned char[*outBufLen];
531 padData(inBuf, unpaddedLen, blklen);
532 // encrypt the padded data
533 err = gcry_cipher_encrypt(handle,
534 *outBuf,
535 *outBufLen - STRING2KEY_SALTLEN,
536 inBuf,
537 *outBufLen - STRING2KEY_SALTLEN);
538 if (err != GPG_ERR_NO_ERROR) {
539 printDebug(string("LibGCryptIf::doEncrypt(): gcry_cipher_encrypt() failed: ")
540 + gcry_strerror(err));
541 ret = e_cryptNotImpl;
542 goto out_delete;
543 }
544 // append the salt to the encrypted data
545 memcpy(*outBuf + *outBufLen - STRING2KEY_SALTLEN, salt, STRING2KEY_SALTLEN);
546 goto out_close;
547out_delete:
548 delete [] *outBuf;
549out_close:
550 gcry_cipher_close(handle);
551out:
552 return ret;
553}
554
555PwMerror LibGCryptIf::decrypt(unsigned char **outBuf,
556 size_t *outBufLen,
557 const unsigned char *inBuf,
558 size_t inBufLen,
559 const unsigned char *key,
560 size_t keylen,
561 char _algo)
562{
563 PwMerror ret = e_success;
564 gcry_error_t err;
565 gcry_cipher_hd_t handle;
566 size_t cipherKeylen;
567 unsigned char *hashedKey;
568 unsigned char salt[STRING2KEY_SALTLEN];
569 int algo = mapCipherId(_algo);
570
571 if (!inBufLen || !keylen)
572 return e_invalidArg;
573
574 // test if algo is ready for encryption
575 err = gcry_cipher_algo_info(algo,
576 GCRYCTL_TEST_ALGO,
577 0, 0);
578 if (err != GPG_ERR_NO_ERROR) {
579 printDebug(string("LibGCryptIf::doDecrypt(): GCRYCTL_TEST_ALGO failed: ")
580 + gcry_strerror(err));
581 ret = e_cryptNotImpl;
582 goto out;
583 }
584 // get algo key length
585 err = gcry_cipher_algo_info(algo,
586 GCRYCTL_GET_KEYLEN,
587 0,
588 &cipherKeylen);
589 if (err != GPG_ERR_NO_ERROR) {
590 printDebug(string("LibGCryptIf::doDecrypt(): GCRYCTL_GET_KEYLEN failed: ")
591 + gcry_strerror(err));
592 ret = e_cryptNotImpl;
593 goto out;
594 }
595 // extract the salt of the encrypted data buffer
596 memcpy(salt, inBuf + inBufLen - STRING2KEY_SALTLEN, STRING2KEY_SALTLEN);
597 // open the algo and get a handle
598 err = gcry_cipher_open(&handle,
599 algo,
600 GCRY_CIPHER_MODE_CBC,
601 0);
602 if (err != GPG_ERR_NO_ERROR) {
603 printDebug(string("LibGCryptIf::doDecrypt(): gcry_cipher_open() failed: ")
604 + gcry_strerror(err));
605 ret = e_cryptNotImpl;
606 goto out;
607 }
608 // hash the "key" to a fixed size hash matching "cipherKeylen"
609 hashedKey = new unsigned char[cipherKeylen];
610 hashPassphrase(key, keylen, salt, hashedKey, cipherKeylen, false);
611 // so now set the hashed key
612 err = gcry_cipher_setkey(handle, hashedKey, cipherKeylen);
613 if (err != GPG_ERR_NO_ERROR) {
614 printDebug(string("LibGCryptIf::doDecrypt(): gcry_cipher_setkey() failed: ")
615 + gcry_strerror(err));
616 ret = e_cryptNotImpl;
617 delete [] hashedKey;
618 goto out_close;
619 }
620 delete [] hashedKey;
621 *outBufLen = inBufLen - STRING2KEY_SALTLEN;
622 *outBuf = new unsigned char[*outBufLen];
623 // decrypt the data
624 err = gcry_cipher_decrypt(handle,
625 *outBuf,
626 *outBufLen,
627 inBuf,
628 *outBufLen);
629 if (err != GPG_ERR_NO_ERROR) {
630 printDebug(string("LibGCryptIf::doEncrypt(): gcry_cipher_encrypt() failed: ")
631 + gcry_strerror(err));
632 ret = e_cryptNotImpl;
633 goto out_delete;
634 }
635 // remove all random padding
636 unpadData(*outBuf, outBufLen);
637 goto out_close;
638out_delete:
639 delete [] *outBuf;
640out_close:
641 gcry_cipher_close(handle);
642out:
643 return ret;
644}
645
646PwMerror LibGCryptIf::hash(unsigned char **outBuf,
647 size_t *outBufLen,
648 const unsigned char *inBuf,
649 size_t inBufLen,
650 char _algo)
651{
652 PwMerror ret = e_success;
653 unsigned int hashLen;
654 int algo = mapHashId(_algo);
655
656 hashLen = gcry_md_get_algo_dlen(algo);
657 *outBufLen = hashLen;
658 *outBuf = new unsigned char[*outBufLen];
659 gcry_md_hash_buffer(algo,
660 *outBuf,
661 inBuf,
662 inBufLen);
663 return ret;
664}
665
666unsigned int LibGCryptIf::hashLength(char _algo)
667{
668 unsigned int ret;
669 int algo = mapHashId(_algo);
670 ret = gcry_md_get_algo_dlen(algo);
671 return ret;
672}
673
674int LibGCryptIf::mapCipherId(char algo)
675{
676 switch (algo) {
677 case PWM_CRYPT_AES128:
678 return GCRY_CIPHER_AES;
679 case PWM_CRYPT_AES192:
680 return GCRY_CIPHER_AES192;
681 case PWM_CRYPT_AES256:
682 return GCRY_CIPHER_AES256;
683 case PWM_CRYPT_3DES:
684 return GCRY_CIPHER_3DES;
685 case PWM_CRYPT_TWOFISH:
686 return GCRY_CIPHER_TWOFISH;
687 case PWM_CRYPT_TWOFISH128:
688 return GCRY_CIPHER_TWOFISH128;
689 default:
690 BUG();
691 }
692 return GCRY_CIPHER_NONE;
693}
694
695int LibGCryptIf::mapHashId(char algo)
696{
697 switch (algo) {
698 case PWM_HASH_SHA1:
699 return GCRY_MD_SHA1;
700 case PWM_HASH_SHA256:
701 return GCRY_MD_SHA256;
702 case PWM_HASH_SHA384:
703 return GCRY_MD_SHA384;
704 case PWM_HASH_SHA512:
705 return GCRY_MD_SHA512;
706 case PWM_HASH_MD5:
707 return GCRY_MD_MD5;
708 case PWM_HASH_RMD160:
709 return GCRY_MD_RMD160;
710 case PWM_HASH_TIGER:
711 return GCRY_MD_TIGER;
712 default:
713 BUG();
714 }
715 return GCRY_MD_NONE;
716}
717
718bool LibGCryptIf::hashPassphrase(const unsigned char *pw,
719 size_t pwlen,
720 unsigned char *salt,
721 unsigned char *key,
722 size_t keylen,
723 bool create)
724{
725 DEK dek;
726 STRING2KEY s2k;
727 bool ret;
728
729 dek.keylen = keylen;
730 s2k.mode = 1;
731 s2k.hash_algo = mapHashId(conf()->confGlobHashAlgo());
732 s2k.count = 0;
733 if (!create)
734 memcpy(s2k.salt, salt, STRING2KEY_SALTLEN);
735 ret = doHashPassphrase(&dek,
736 pw,
737 pwlen,
738 &s2k,
739 create);
740 if (!ret)
741 goto out;
742 memcpy(key, dek.key, dek.keylen);
743 if (create)
744 memcpy(salt, s2k.salt, STRING2KEY_SALTLEN);
745out:
746 return ret;
747}
748
749
750bool LibGCryptIf::doHashPassphrase(DEK *dek,
751 const unsigned char *pw,
752 size_t pwlen,
753 STRING2KEY *s2k,
754 bool create)
755{
756 // This function is derived from GnuPG-1.2.5-rc2
757 gcry_md_hd_t md;
758 gcry_error_t err;
759 bool ret = true;
760 size_t pass, i;
761 size_t used = 0;
762
763 PWM_ASSERT(s2k->hash_algo);
764 BUG_ON(!(dek->keylen > 0 && dek->keylen <= array_size(dek->key)));
765
766 err = gcry_md_open(&md, s2k->hash_algo, 0);
767 if (err != GPG_ERR_NO_ERROR) {
768 ret = false;
769 goto out;
770 }
771 for (pass = 0; used < dek->keylen; pass++) {
772 if (pass) {
773 gcry_md_reset(md);
774 for (i = 0; i < pass; i++) // preset the hash context
775 gcry_md_putc(md, 0);
776 }
777 if (s2k->mode == 1 || s2k->mode == 3) {
778 size_t len2 = pwlen + 8;
779 size_t count = len2;
780
781 if (create && !pass) {
782 Randomizer *rnd = Randomizer::obj();
783 const unsigned int salt_len = 8;
784 string rndBuf(rnd->genRndBuf(salt_len));
785 memcpy(s2k->salt, rndBuf.c_str(), salt_len);
786 if (s2k->mode == 3)
787 s2k->count = 96; // 65536 iterations
788 }
789 if (s2k->mode == 3) {
790 count = (16ul + (s2k->count & 15)) << ((s2k->count >> 4) + 6);
791 if (count < len2)
792 count = len2;
793 }
794 // a little bit complicated because we need a ulong for count
795 while (count > len2) { // maybe iterated+salted
796 gcry_md_write(md, s2k->salt, 8);
797 gcry_md_write(md, pw, pwlen);
798 count -= len2;
799 }
800 if (count < 8) {
801 gcry_md_write(md, s2k->salt, count);
802 } else {
803 gcry_md_write(md, s2k->salt, 8);
804 count -= 8;
805 gcry_md_write(md, pw, count);
806 }
807 } else
808 gcry_md_write(md, pw, pwlen);
809 gcry_md_final(md);
810 i = gcry_md_get_algo_dlen(s2k->hash_algo);
811 if (i > dek->keylen - used)
812 i = dek->keylen - used;
813 memcpy(dek->key+used, gcry_md_read(md, s2k->hash_algo), i);
814 used += i;
815 }
816 gcry_md_close(md);
817out:
818 return ret;
819}
820
821void LibGCryptIf::padData(unsigned char *buf,
822 size_t bufLen,
823 size_t boundary)
824{
825 size_t numPadBytes = boundary - ((bufLen + 1) % boundary);
826 buf[bufLen] = static_cast<char>(0x01);
827 size_t i = 0;
828 Randomizer *rnd = Randomizer::obj();
829 char c;
830 unsigned char *b;
831 while (i < numPadBytes) {
832 c = rnd->genRndChar();
833 if (c == static_cast<char>(0x01))
834 continue;
835 b = buf + bufLen + 1 + i;
836 *b = c;
837 ++i;
838 }
839}
840
841void LibGCryptIf::unpadData(const unsigned char *buf,
842 size_t *bufLen)
843{
844 size_t pos;
845 BUG_ON(*bufLen % 8);
846 pos = *bufLen - 1;
847 while (buf[pos] != static_cast<char>(0x01)) {
848 BUG_ON(!pos);
849 --pos;
850 }
851 *bufLen = pos;
852}
853
854#endif // CONFIG_PWMANAGER_CRYPTO
855
diff --git a/pwmanager/pwmanager/libgcryptif.h b/pwmanager/pwmanager/libgcryptif.h
index e86d638..7390827 100644
--- a/pwmanager/pwmanager/libgcryptif.h
+++ b/pwmanager/pwmanager/libgcryptif.h
@@ -15,14 +15,17 @@
15 15
16#ifndef __LIBGCRYPTIF_H 16#ifndef __LIBGCRYPTIF_H
17#define __LIBGCRYPTIF_H 17#define __LIBGCRYPTIF_H
18 18
19#include "pwmexception.h" 19#include "pwmexception.h"
20 20
21//US ENH: should we put this better into globalstuff.h?
22#define CONFIG_PWMANAGER_CRYPTO
23
21//#undef CONFIG_PWMANAGER_GCRY // for debugging only. 24//#undef CONFIG_PWMANAGER_GCRY // for debugging only.
22#ifdef CONFIG_PWMANAGER_GCRY 25#if defined CONFIG_PWMANAGER_GCRY || defined CONFIG_PWMANAGER_CRYPTO
23 26
24#include <stddef.h> 27#include <stddef.h>
25#include <sys/types.h> 28#include <sys/types.h>
26#include <stdint.h> 29#include <stdint.h>
27 30
28 #define STRING2KEY_SALTLEN8 31 #define STRING2KEY_SALTLEN8
diff --git a/pwmanager/pwmanager/pwmanagerE.pro b/pwmanager/pwmanager/pwmanagerE.pro
index 52d7586..294f549 100644
--- a/pwmanager/pwmanager/pwmanagerE.pro
+++ b/pwmanager/pwmanager/pwmanagerE.pro
@@ -7,21 +7,21 @@ OBJECTS_DIR = obj/$(PLATFORM)
7MOC_DIR = moc/$(PLATFORM) 7MOC_DIR = moc/$(PLATFORM)
8DESTDIR=$(QPEDIR)/bin 8DESTDIR=$(QPEDIR)/bin
9 9
10INCLUDEPATH += . ../../ ../../qtcompat ../../qtcompat/xml ../../libkdepim ../../microkde ../../microkde/kdecore ../../microkde/kdeui ../../microkde/kutils $(QPEDIR)/include 10INCLUDEPATH += . ../../ ../../qtcompat ../../qtcompat/xml ../../libkdepim ../../microkde ../../microkde/kdecore ../../microkde/kdeui ../../microkde/kutils $(QPEDIR)/include
11DEFINES += PWM_EMBEDDED 11DEFINES += PWM_EMBEDDED
12#enable this setting if you want debugoutput for pwmanager 12#enable this setting if you want debugoutput for pwmanager
13#DEFINES += CONFIG_DEBUG 13DEFINES += CONFIG_DEBUG
14 14
15LIBS += -lmicrokde 15LIBS += -lmicrokde
16LIBS += -lmicroqtcompat 16LIBS += -lmicroqtcompat
17LIBS += -lmicrokdepim 17LIBS += -lmicrokdepim
18LIBS += -L$(QPEDIR)/lib 18LIBS += -L$(QPEDIR)/lib
19LIBS += -lqpe 19LIBS += -lqpe
20LIBS += -lz 20LIBS += -lz
21LIBS += -lbz2 21#LIBS += -lbz2
22LIBS += -lcrypto 22LIBS += -lcrypto
23LIBS += $(QTOPIALIB) 23LIBS += $(QTOPIALIB)
24 24
25#INTERFACES = \ 25#INTERFACES = \
26#addentrywnd.ui \ 26#addentrywnd.ui \
27#configwnd.ui \ 27#configwnd.ui \
@@ -41,34 +41,35 @@ LIBS += $(QTOPIALIB)
41#configuration.h \ 41#configuration.h \
42#configwnd.h \ 42#configwnd.h \
43#configwndimpl.h \ 43#configwndimpl.h \
44#selftest.h 44#selftest.h
45#subtbledit.h \ 45#subtbledit.h \
46#subtbleditimpl.h \ 46#subtbleditimpl.h \
47#compressbzip2.h \
47 48
48HEADERS = \ 49HEADERS = \
49addentrywnd_emb.h \ 50addentrywnd_emb.h \
50addentrywndimpl.h \ 51addentrywndimpl.h \
51base64.h \ 52base64.h \
52binentrygen.h \ 53binentrygen.h \
53blowfish.h \ 54blowfish.h \
54commentbox.h \ 55commentbox.h \
55compiler.h \ 56compiler.h \
56compressbzip2.h \
57compressgzip.h \ 57compressgzip.h \
58findwnd_emb.h \ 58findwnd_emb.h \
59findwndimpl.h \ 59findwndimpl.h \
60genpasswd.h \ 60genpasswd.h \
61getkeycardwnd.h \ 61getkeycardwnd.h \
62getmasterpwwnd_emb.h \ 62getmasterpwwnd_emb.h \
63getmasterpwwndimpl.h \ 63getmasterpwwndimpl.h \
64globalstuff.h \ 64globalstuff.h \
65gpasmanfile.h \ 65gpasmanfile.h \
66htmlgen.h \ 66htmlgen.h \
67htmlparse.h \ 67htmlparse.h \
68ipc.h \ 68ipc.h \
69libgcryptif.h \
69listobjselectwnd.h \ 70listobjselectwnd.h \
70listviewpwm.h \ 71listviewpwm.h \
71printtext.h \ 72printtext.h \
72pwgenwnd_emb.h \ 73pwgenwnd_emb.h \
73pwgenwndimpl.h \ 74pwgenwndimpl.h \
74pwmdoc.h \ 75pwmdoc.h \
@@ -105,32 +106,33 @@ kcmconfigs/pwmconfigwidget.h \
105#printtext.cpp \ 106#printtext.cpp \
106#selftest.cpp \ 107#selftest.cpp \
107#pwmprint.cpp \ 108#pwmprint.cpp \
108#spinforsignal.cpp 109#spinforsignal.cpp
109#subtbledit.cpp \ 110#subtbledit.cpp \
110#subtbleditimpl.cpp \ 111#subtbleditimpl.cpp \
112#compressbzip2.cpp \
111 113
112SOURCES = \ 114SOURCES = \
113addentrywnd_emb.cpp \ 115addentrywnd_emb.cpp \
114addentrywndimpl.cpp \ 116addentrywndimpl.cpp \
115base64.cpp \ 117base64.cpp \
116binentrygen.cpp \ 118binentrygen.cpp \
117blowfish.cpp \ 119blowfish.cpp \
118commentbox.cpp \ 120commentbox.cpp \
119compressbzip2.cpp \
120compressgzip.cpp \ 121compressgzip.cpp \
121findwnd_emb.cpp \ 122findwnd_emb.cpp \
122findwndimpl.cpp \ 123findwndimpl.cpp \
123genpasswd.cpp \ 124genpasswd.cpp \
124getkeycardwnd.cpp \ 125getkeycardwnd.cpp \
125getmasterpwwnd_emb.cpp \ 126getmasterpwwnd_emb.cpp \
126getmasterpwwndimpl.cpp \ 127getmasterpwwndimpl.cpp \
127globalstuff.cpp \ 128globalstuff.cpp \
128gpasmanfile.cpp \ 129gpasmanfile.cpp \
129htmlgen.cpp \ 130htmlgen.cpp \
130ipc.cpp \ 131ipc.cpp \
132libgcryptif.cpp \
131listobjselectwnd.cpp \ 133listobjselectwnd.cpp \
132listviewpwm.cpp \ 134listviewpwm.cpp \
133main.cpp \ 135main.cpp \
134pwgenwnd_emb.cpp \ 136pwgenwnd_emb.cpp \
135pwgenwndimpl.cpp \ 137pwgenwndimpl.cpp \
136pwm.cpp \ 138pwm.cpp \
diff --git a/pwmanager/pwmanager/pwmdoc.cpp b/pwmanager/pwmanager/pwmdoc.cpp
index 4ad392e..a5df8f0 100644
--- a/pwmanager/pwmanager/pwmdoc.cpp
+++ b/pwmanager/pwmanager/pwmdoc.cpp
@@ -25,15 +25,14 @@
25#include "gpasmanfile.h" 25#include "gpasmanfile.h"
26#include "serializer.h" 26#include "serializer.h"
27#include "compressgzip.h" 27#include "compressgzip.h"
28#include "compressbzip2.h" 28#include "compressbzip2.h"
29#include "randomizer.h" 29#include "randomizer.h"
30#include "pwminit.h" 30#include "pwminit.h"
31#ifndef PWM_EMBEDDED 31#include "libgcryptif.h"
32//US #include "libgryptif.h" 32#ifdef PWM_EMBEDDED
33#else
34#include "pwmprefs.h" 33#include "pwmprefs.h"
35#include "kglobal.h" 34#include "kglobal.h"
36#endif 35#endif
37 36
38#include <kmessagebox.h> 37#include <kmessagebox.h>
39#include <libkcal/syncdefines.h> 38#include <libkcal/syncdefines.h>
@@ -56,12 +55,24 @@
56#include <algorithm> 55#include <algorithm>
57#include <sys/types.h> 56#include <sys/types.h>
58#include <sys/stat.h> 57#include <sys/stat.h>
59#include <unistd.h> 58#include <unistd.h>
60#include <stdint.h> 59#include <stdint.h>
61 60
61
62#ifdef PWM_EMBEDDED
63#ifndef Q_LONG
64#define Q_LONG long
65#endif
66
67#ifndef Q_ULONG
68#define Q_ULONG unsigned long
69#endif
70#endif //PWM_EMBEDDED
71
72
62//TODO: reset to its normal value. 73//TODO: reset to its normal value.
63 #define META_CHECK_TIMER_INTERVAL10/*300*/ /* sek */ 74 #define META_CHECK_TIMER_INTERVAL10/*300*/ /* sek */
64 75
65using namespace std; 76using namespace std;
66 77
67 78
@@ -349,19 +360,15 @@ PwMerror PwMDoc::saveDoc(char compress, const QString *file)
349 if (useChipcard) { 360 if (useChipcard) {
350 setDocStatFlag(DOC_STAT_USE_CHIPCARD); 361 setDocStatFlag(DOC_STAT_USE_CHIPCARD);
351 } else { 362 } else {
352 unsetDocStatFlag(DOC_STAT_USE_CHIPCARD); 363 unsetDocStatFlag(DOC_STAT_USE_CHIPCARD);
353 } 364 }
354 } 365 }
355#ifndef PWM_EMBEDDED 366
356 int _cryptAlgo = conf()->confGlobCryptAlgo(); 367 int _cryptAlgo = conf()->confGlobCryptAlgo();
357 int _hashAlgo = conf()->confGlobHashAlgo(); 368 int _hashAlgo = conf()->confGlobHashAlgo();
358#else
359 int _cryptAlgo = PWM_CRYPT_BLOWFISH;
360 int _hashAlgo = PWM_HASH_SHA1;
361#endif
362 369
363 // sanity check for the selected algorithms 370 // sanity check for the selected algorithms
364 if (_cryptAlgo < PWM_CRYPT_BLOWFISH || 371 if (_cryptAlgo < PWM_CRYPT_BLOWFISH ||
365 _cryptAlgo > PWM_CRYPT_TWOFISH128) { 372 _cryptAlgo > PWM_CRYPT_TWOFISH128) {
366 printWarn("Invalid Crypto-Algorithm selected! " 373 printWarn("Invalid Crypto-Algorithm selected! "
367 "Config-file seems to be corrupt. " 374 "Config-file seems to be corrupt. "
@@ -596,13 +603,12 @@ out_success:
596PwMerror PwMDoc::writeFileHeader(char keyHash, char dataHash, char crypt, char compress, 603PwMerror PwMDoc::writeFileHeader(char keyHash, char dataHash, char crypt, char compress,
597 QString *pw, QFile *f) 604 QString *pw, QFile *f)
598{ 605{
599 PWM_ASSERT(pw); 606 PWM_ASSERT(pw);
600 PWM_ASSERT(f); 607 PWM_ASSERT(f);
601 PWM_ASSERT(listView); 608 PWM_ASSERT(listView);
602#ifndef PWM_EMBEDDED
603 if (f->writeBlock(FILE_ID_HEADER, strlen(FILE_ID_HEADER)) != 609 if (f->writeBlock(FILE_ID_HEADER, strlen(FILE_ID_HEADER)) !=
604 static_cast<Q_LONG>(strlen(FILE_ID_HEADER))) { 610 static_cast<Q_LONG>(strlen(FILE_ID_HEADER))) {
605 return e_writeFile; 611 return e_writeFile;
606 } 612 }
607 if (f->putch(PWM_FILE_VER) == -1 || 613 if (f->putch(PWM_FILE_VER) == -1 ||
608 f->putch(keyHash) == -1 || 614 f->putch(keyHash) == -1 ||
@@ -611,27 +617,12 @@ PwMerror PwMDoc::writeFileHeader(char keyHash, char dataHash, char crypt, char c
611 f->putch(compress) == -1 || 617 f->putch(compress) == -1 ||
612 f->putch((getDocStatFlag(DOC_STAT_USE_CHIPCARD)) ? 618 f->putch((getDocStatFlag(DOC_STAT_USE_CHIPCARD)) ?
613 (static_cast<char>(0x01)) : (static_cast<char>(0x00))) == -1) { 619 (static_cast<char>(0x01)) : (static_cast<char>(0x00))) == -1) {
614 return e_writeFile; 620 return e_writeFile;
615 } 621 }
616 622
617#else
618 if (f->writeBlock(FILE_ID_HEADER, strlen(FILE_ID_HEADER)) !=
619 (long)(strlen(FILE_ID_HEADER))) {
620 return e_writeFile;
621 }
622 if (f->putch(PWM_FILE_VER) == -1 ||
623 f->putch(keyHash) == -1 ||
624 f->putch(dataHash) == -1 ||
625 f->putch(crypt) == -1 ||
626 f->putch(compress) == -1 ||
627 f->putch((getDocStatFlag(DOC_STAT_USE_CHIPCARD)) ?
628 ((char)(0x01)) : ((char)(0x00))) == -1) {
629 return e_writeFile;
630 }
631#endif
632 // write bytes of NUL-data. These bytes are reserved for future-use. 623 // write bytes of NUL-data. These bytes are reserved for future-use.
633 const int bufSize = 64; 624 const int bufSize = 64;
634 char tmp_buf[bufSize]; 625 char tmp_buf[bufSize];
635 memset(tmp_buf, 0x00, bufSize); 626 memset(tmp_buf, 0x00, bufSize);
636 if (f->writeBlock(tmp_buf, bufSize) != bufSize) 627 if (f->writeBlock(tmp_buf, bufSize) != bufSize)
637 return e_writeFile; 628 return e_writeFile;
@@ -643,13 +634,12 @@ PwMerror PwMDoc::writeFileHeader(char keyHash, char dataHash, char crypt, char c
643 hash.sha1_write(reinterpret_cast<const byte *>(pw->latin1()), pw->length()); 634 hash.sha1_write(reinterpret_cast<const byte *>(pw->latin1()), pw->length());
644 string ret = hash.sha1_read(); 635 string ret = hash.sha1_read();
645 if (f->writeBlock(ret.c_str(), hashlen) != hashlen) 636 if (f->writeBlock(ret.c_str(), hashlen) != hashlen)
646 return e_writeFile; 637 return e_writeFile;
647 break; 638 break;
648 } 639 }
649#ifndef PWM_EMBEDDED
650 case PWM_HASH_SHA256: 640 case PWM_HASH_SHA256:
651 /*... fall through */ 641 /*... fall through */
652 case PWM_HASH_SHA384: 642 case PWM_HASH_SHA384:
653 case PWM_HASH_SHA512: 643 case PWM_HASH_SHA512:
654 case PWM_HASH_MD5: 644 case PWM_HASH_MD5:
655 case PWM_HASH_RMD160: 645 case PWM_HASH_RMD160:
@@ -673,13 +663,12 @@ PwMerror PwMDoc::writeFileHeader(char keyHash, char dataHash, char crypt, char c
673 delete [] buf; 663 delete [] buf;
674 return e_hashNotImpl; 664 return e_hashNotImpl;
675 } 665 }
676 delete [] buf; 666 delete [] buf;
677 break; 667 break;
678 } 668 }
679#endif
680 default: { 669 default: {
681 return e_hashNotImpl; 670 return e_hashNotImpl;
682 } } 671 } }
683 return e_success; 672 return e_success;
684} 673}
685 674
@@ -759,13 +748,12 @@ PwMerror PwMDoc::checkHeader(char *cryptAlgo, QString *pw, char *compress,
759 hash.sha1_write(reinterpret_cast<const byte *>(pw->latin1()), pw->length()); 748 hash.sha1_write(reinterpret_cast<const byte *>(pw->latin1()), pw->length());
760 string ret = hash.sha1_read(); 749 string ret = hash.sha1_read();
761 if (ret != readHash) 750 if (ret != readHash)
762 return e_wrongPw;// hash doesn't match (wrong key) 751 return e_wrongPw;// hash doesn't match (wrong key)
763 break; 752 break;
764 } 753 }
765#ifndef PWM_EMBEDDED
766 case PWM_HASH_SHA256: 754 case PWM_HASH_SHA256:
767 /*... fall through */ 755 /*... fall through */
768 case PWM_HASH_SHA384: 756 case PWM_HASH_SHA384:
769 case PWM_HASH_SHA512: 757 case PWM_HASH_SHA512:
770 case PWM_HASH_MD5: 758 case PWM_HASH_MD5:
771 case PWM_HASH_RMD160: 759 case PWM_HASH_RMD160:
@@ -792,23 +780,21 @@ PwMerror PwMDoc::checkHeader(char *cryptAlgo, QString *pw, char *compress,
792 for (i = 0; i < hashLen; ++i) 780 for (i = 0; i < hashLen; ++i)
793 readHash.push_back(f->getch()); 781 readHash.push_back(f->getch());
794 if (calcHash != readHash) 782 if (calcHash != readHash)
795 return e_wrongPw;// hash doesn't match (wrong key) 783 return e_wrongPw;// hash doesn't match (wrong key)
796 break; 784 break;
797 } 785 }
798#endif
799 default: { 786 default: {
800 return e_hashNotImpl; 787 return e_hashNotImpl;
801 } } 788 } }
802 // read the data-hash from the file 789 // read the data-hash from the file
803 unsigned int hashLen, i; 790 unsigned int hashLen, i;
804 switch (*dataHashType) { 791 switch (*dataHashType) {
805 case PWM_HASH_SHA1: 792 case PWM_HASH_SHA1:
806 hashLen = SHA1_HASH_LEN_BYTE; 793 hashLen = SHA1_HASH_LEN_BYTE;
807 break; 794 break;
808#ifndef PWM_EMBEDDED
809 case PWM_HASH_SHA256: 795 case PWM_HASH_SHA256:
810 /*... fall through */ 796 /*... fall through */
811 case PWM_HASH_SHA384: 797 case PWM_HASH_SHA384:
812 case PWM_HASH_SHA512: 798 case PWM_HASH_SHA512:
813 case PWM_HASH_MD5: 799 case PWM_HASH_MD5:
814 case PWM_HASH_RMD160: 800 case PWM_HASH_RMD160:
@@ -818,13 +804,12 @@ PwMerror PwMDoc::checkHeader(char *cryptAlgo, QString *pw, char *compress,
818 LibGCryptIf gc; 804 LibGCryptIf gc;
819 hashLen = gc.hashLength(*dataHashType); 805 hashLen = gc.hashLength(*dataHashType);
820 if (hashLen == 0) 806 if (hashLen == 0)
821 return e_hashNotImpl; 807 return e_hashNotImpl;
822 break; 808 break;
823 } 809 }
824#endif
825 default: 810 default:
826 return e_hashNotImpl; 811 return e_hashNotImpl;
827 } 812 }
828 *dataHash = ""; 813 *dataHash = "";
829 for (i = 0; i < hashLen; ++i) { 814 for (i = 0; i < hashLen; ++i) {
830 tmpRet = f->getch(); 815 tmpRet = f->getch();
@@ -862,13 +847,12 @@ PwMerror PwMDoc::writeDataHash(char dataHash, string *d, QFile *f)
862 h.sha1_write(reinterpret_cast<const byte *>(d->c_str()), d->size()); 847 h.sha1_write(reinterpret_cast<const byte *>(d->c_str()), d->size());
863 string hRet = h.sha1_read(); 848 string hRet = h.sha1_read();
864 if (f->writeBlock(hRet.c_str(), hashLen) != hashLen) 849 if (f->writeBlock(hRet.c_str(), hashLen) != hashLen)
865 return e_writeFile; 850 return e_writeFile;
866 break; 851 break;
867 } 852 }
868 #ifndef PWM_EMBEDDED
869 case PWM_HASH_SHA256: 853 case PWM_HASH_SHA256:
870 /*... fall through */ 854 /*... fall through */
871 case PWM_HASH_SHA384: 855 case PWM_HASH_SHA384:
872 case PWM_HASH_SHA512: 856 case PWM_HASH_SHA512:
873 case PWM_HASH_MD5: 857 case PWM_HASH_MD5:
874 case PWM_HASH_RMD160: 858 case PWM_HASH_RMD160:
@@ -891,13 +875,12 @@ PwMerror PwMDoc::writeDataHash(char dataHash, string *d, QFile *f)
891 delete [] buf; 875 delete [] buf;
892 return e_hashNotImpl; 876 return e_hashNotImpl;
893 } 877 }
894 delete [] buf; 878 delete [] buf;
895 break; 879 break;
896 } 880 }
897#endif
898 default: { 881 default: {
899 return e_hashNotImpl; 882 return e_hashNotImpl;
900 } } 883 } }
901 884
902 return e_success; 885 return e_success;
903} 886}
@@ -932,37 +915,24 @@ bool PwMDoc::copyFile(const QString &src, const QString &dst)
932 if (!dstFd.open(IO_ReadWrite)) { 915 if (!dstFd.open(IO_ReadWrite)) {
933 srcFd.close(); 916 srcFd.close();
934 return false; 917 return false;
935 } 918 }
936 const int tmpBuf_size = 512; 919 const int tmpBuf_size = 512;
937 char tmpBuf[tmpBuf_size]; 920 char tmpBuf[tmpBuf_size];
938#ifndef PWM_EMBEDDED
939 Q_LONG bytesRead, bytesWritten; 921 Q_LONG bytesRead, bytesWritten;
940#else 922
941 long bytesRead, bytesWritten;
942#endif
943 while (!srcFd.atEnd()) { 923 while (!srcFd.atEnd()) {
944#ifndef PWM_EMBEDDED
945 bytesRead = srcFd.readBlock(tmpBuf, 924 bytesRead = srcFd.readBlock(tmpBuf,
946 static_cast<Q_ULONG>(tmpBuf_size)); 925 static_cast<Q_ULONG>(tmpBuf_size));
947#else
948 bytesRead = srcFd.readBlock(tmpBuf,
949 (unsigned long)(tmpBuf_size));
950#endif
951 if (bytesRead == -1) { 926 if (bytesRead == -1) {
952 srcFd.close(); 927 srcFd.close();
953 dstFd.close(); 928 dstFd.close();
954 return false; 929 return false;
955 } 930 }
956#ifndef PWM_EMBEDDED
957 bytesWritten = dstFd.writeBlock(tmpBuf, 931 bytesWritten = dstFd.writeBlock(tmpBuf,
958 static_cast<Q_ULONG>(bytesRead)); 932 static_cast<Q_ULONG>(bytesRead));
959#else
960 bytesWritten = dstFd.writeBlock(tmpBuf,
961 (unsigned long)(bytesRead));
962#endif
963 if (bytesWritten != bytesRead) { 933 if (bytesWritten != bytesRead) {
964 srcFd.close(); 934 srcFd.close();
965 dstFd.close(); 935 dstFd.close();
966 return false; 936 return false;
967 } 937 }
968 } 938 }
@@ -1297,13 +1267,12 @@ PwMerror PwMDoc::encrypt(string *d, const QString *pw, QFile *f, char algo)
1297 delete [] encrypted; 1267 delete [] encrypted;
1298 return e_weakPw; 1268 return e_weakPw;
1299 } 1269 }
1300 bf.bf_encrypt((byte *) encrypted, (byte *) d->c_str(), encSize); 1270 bf.bf_encrypt((byte *) encrypted, (byte *) d->c_str(), encSize);
1301 break; 1271 break;
1302 } 1272 }
1303 #ifndef PWM_EMBEDDED
1304 case PWM_CRYPT_AES128: 1273 case PWM_CRYPT_AES128:
1305 /*... fall through */ 1274 /*... fall through */
1306 case PWM_CRYPT_AES192: 1275 case PWM_CRYPT_AES192:
1307 case PWM_CRYPT_AES256: 1276 case PWM_CRYPT_AES256:
1308 case PWM_CRYPT_3DES: 1277 case PWM_CRYPT_3DES:
1309 case PWM_CRYPT_TWOFISH: 1278 case PWM_CRYPT_TWOFISH:
@@ -1323,34 +1292,24 @@ PwMerror PwMDoc::encrypt(string *d, const QString *pw, QFile *f, char algo)
1323 algo); 1292 algo);
1324 delete [] plain; 1293 delete [] plain;
1325 if (err != e_success) 1294 if (err != e_success)
1326 return e_cryptNotImpl; 1295 return e_cryptNotImpl;
1327 break; 1296 break;
1328 } 1297 }
1329#endif
1330 default: { 1298 default: {
1331 delete_ifnot_null_array(encrypted); 1299 delete_ifnot_null_array(encrypted);
1332 return e_cryptNotImpl; 1300 return e_cryptNotImpl;
1333 } } 1301 } }
1334 1302
1335 // write encrypted data to file 1303 // write encrypted data to file
1336#ifndef PWM_EMBEDDED
1337 if (f->writeBlock(reinterpret_cast<const char *>(encrypted), 1304 if (f->writeBlock(reinterpret_cast<const char *>(encrypted),
1338 static_cast<Q_ULONG>(encSize)) 1305 static_cast<Q_ULONG>(encSize))
1339 != static_cast<Q_LONG>(encSize)) { 1306 != static_cast<Q_LONG>(encSize)) {
1340 delete_ifnot_null_array(encrypted); 1307 delete_ifnot_null_array(encrypted);
1341 return e_writeFile; 1308 return e_writeFile;
1342 } 1309 }
1343#else
1344 if (f->writeBlock((const char *)(encrypted),
1345 (unsigned long)(encSize))
1346 != (long)(encSize)) {
1347 delete_ifnot_null_array(encrypted);
1348 return e_writeFile;
1349 }
1350#endif
1351 delete_ifnot_null_array(encrypted); 1310 delete_ifnot_null_array(encrypted);
1352 return e_success; 1311 return e_success;
1353} 1312}
1354 1313
1355PwMerror PwMDoc::decrypt(string *d, unsigned int pos, const QString *pw, 1314PwMerror PwMDoc::decrypt(string *d, unsigned int pos, const QString *pw,
1356 char algo, QFile *f) 1315 char algo, QFile *f)
@@ -1385,13 +1344,12 @@ PwMerror PwMDoc::decrypt(string *d, unsigned int pos, const QString *pw,
1385 case PWM_CRYPT_BLOWFISH: { 1344 case PWM_CRYPT_BLOWFISH: {
1386 Blowfish bf; 1345 Blowfish bf;
1387 bf.bf_setkey((byte *) pw->latin1(), pw->length()); 1346 bf.bf_setkey((byte *) pw->latin1(), pw->length());
1388 bf.bf_decrypt(decrypted, encrypted, cryptLen); 1347 bf.bf_decrypt(decrypted, encrypted, cryptLen);
1389 break; 1348 break;
1390 } 1349 }
1391#ifndef PWM_EMBEDDED
1392 case PWM_CRYPT_AES128: 1350 case PWM_CRYPT_AES128:
1393 /*... fall through */ 1351 /*... fall through */
1394 case PWM_CRYPT_AES192: 1352 case PWM_CRYPT_AES192:
1395 case PWM_CRYPT_AES256: 1353 case PWM_CRYPT_AES256:
1396 case PWM_CRYPT_3DES: 1354 case PWM_CRYPT_3DES:
1397 case PWM_CRYPT_TWOFISH: 1355 case PWM_CRYPT_TWOFISH:
@@ -1411,13 +1369,12 @@ PwMerror PwMDoc::decrypt(string *d, unsigned int pos, const QString *pw,
1411 delete [] encrypted; 1369 delete [] encrypted;
1412 delete [] decrypted; 1370 delete [] decrypted;
1413 return e_cryptNotImpl; 1371 return e_cryptNotImpl;
1414 } 1372 }
1415 break; 1373 break;
1416 } 1374 }
1417#endif
1418 default: { 1375 default: {
1419 delete [] encrypted; 1376 delete [] encrypted;
1420 delete [] decrypted; 1377 delete [] decrypted;
1421 return e_cryptNotImpl; 1378 return e_cryptNotImpl;
1422 } } 1379 } }
1423 delete [] encrypted; 1380 delete [] encrypted;
@@ -1449,13 +1406,12 @@ PwMerror PwMDoc::checkDataHash(char dataHashType, const string *dataHash,
1449 hash.sha1_write((byte*)dataStream->c_str(), dataStream->length()); 1406 hash.sha1_write((byte*)dataStream->c_str(), dataStream->length());
1450 string ret = hash.sha1_read(); 1407 string ret = hash.sha1_read();
1451 if (ret != *dataHash) 1408 if (ret != *dataHash)
1452 return e_fileCorrupt; 1409 return e_fileCorrupt;
1453 break; 1410 break;
1454 } 1411 }
1455#ifndef PWM_EMBEDDED
1456 case PWM_HASH_SHA256: 1412 case PWM_HASH_SHA256:
1457 /*... fall through */ 1413 /*... fall through */
1458 case PWM_HASH_SHA384: 1414 case PWM_HASH_SHA384:
1459 case PWM_HASH_SHA512: 1415 case PWM_HASH_SHA512:
1460 case PWM_HASH_MD5: 1416 case PWM_HASH_MD5:
1461 case PWM_HASH_RMD160: 1417 case PWM_HASH_RMD160:
@@ -1477,13 +1433,12 @@ PwMerror PwMDoc::checkDataHash(char dataHashType, const string *dataHash,
1477 static_cast<string::size_type>(hashLen)); 1433 static_cast<string::size_type>(hashLen));
1478 delete [] buf; 1434 delete [] buf;
1479 if (calcHash != *dataHash) 1435 if (calcHash != *dataHash)
1480 return e_fileCorrupt; 1436 return e_fileCorrupt;
1481 break; 1437 break;
1482 } 1438 }
1483#endif
1484 default: 1439 default:
1485 return e_hashNotImpl; 1440 return e_hashNotImpl;
1486 } 1441 }
1487 return e_success; 1442 return e_success;
1488} 1443}
1489 1444