Diffstat (limited to 'noncore/apps/zsafe/zsafe.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r-- | noncore/apps/zsafe/zsafe.cpp | 57 |
1 files changed, 29 insertions, 28 deletions
diff --git a/noncore/apps/zsafe/zsafe.cpp b/noncore/apps/zsafe/zsafe.cpp index f70f863..9c0c6ce 100644 --- a/noncore/apps/zsafe/zsafe.cpp +++ b/noncore/apps/zsafe/zsafe.cpp | |||
@@ -1747,109 +1747,115 @@ bool ZSafe::openDocument(const char* _filename, const char* ) | |||
1747 | exitZs (1); | 1747 | exitZs (1); |
1748 | break; | 1748 | break; |
1749 | case 1: // Yes | 1749 | case 1: // Yes |
1750 | break; | 1750 | break; |
1751 | } | 1751 | } |
1752 | } | 1752 | } |
1753 | 1753 | ||
1754 | retval = loadFinalize(); | 1754 | retval = loadFinalize(); |
1755 | 1755 | ||
1756 | return true; | 1756 | return true; |
1757 | } | 1757 | } |
1758 | 1758 | ||
1759 | int ZSafe::loadInit(const char* _filename, const char *password) | 1759 | int ZSafe::loadInit(const char* _filename, const char *password) |
1760 | { | 1760 | { |
1761 | unsigned int j = 0; | 1761 | unsigned int j = 0; |
1762 | unsigned int keylength=0; | 1762 | unsigned int keylength=0; |
1763 | int count=0, count2=0, count3=0; | 1763 | int count=0, count2=0, count3=0; |
1764 | unsigned char charbuf[8]; | 1764 | unsigned char charbuf[8]; |
1765 | unsigned short ciphertext[4]; | 1765 | unsigned short ciphertext[4]; |
1766 | char key[128]; | 1766 | char key[128]; |
1767 | Krc2* krc2 = new Krc2(); | 1767 | Krc2* krc2 = new Krc2(); |
1768 | 1768 | ||
1769 | fd = fopen (_filename, "rb"); | 1769 | fd = fopen (_filename, "rb"); |
1770 | 1770 | ||
1771 | QFileInfo f (_filename); | 1771 | QFileInfo f (_filename); |
1772 | load_buffer_length = f.size(); | 1772 | load_buffer_length = f.size(); |
1773 | load_buffer_length = ((load_buffer_length / 1024)+1) * 1024 * 2; | 1773 | load_buffer_length = ((load_buffer_length / 1024)+1) * 1024 * 2; |
1774 | 1774 | ||
1775 | if (fd == NULL) | 1775 | if (fd == NULL) { |
1776 | delete krc2; | ||
1776 | return PWERR_OPEN; | 1777 | return PWERR_OPEN; |
1778 | } | ||
1777 | 1779 | ||
1778 | buffer = (char *)malloc(load_buffer_length); | 1780 | buffer = (char *)malloc(load_buffer_length); |
1779 | for (j = 0; password[j] != '\0'; j++) { | 1781 | for (j = 0; password[j] != '\0'; j++) { |
1780 | key[j] = password[j]; | 1782 | key[j] = password[j]; |
1781 | } | 1783 | } |
1782 | keylength = j; | 1784 | keylength = j; |
1783 | krc2->rc2_expandkey (key, keylength, 128); | 1785 | krc2->rc2_expandkey (key, keylength, 128); |
1784 | 1786 | ||
1785 | #ifndef Q_WS_WIN | 1787 | #ifndef Q_WS_WIN |
1786 | size = read(fileno (fd), (unsigned char *) (charbuf + count), 8); | 1788 | size = read(fileno (fd), (unsigned char *) (charbuf + count), 8); |
1787 | #else | 1789 | #else |
1788 | size = fread ((unsigned char *) (charbuf + count), sizeof(unsigned char), 8, fd); | 1790 | size = fread ((unsigned char *) (charbuf + count), sizeof(unsigned char), 8, fd); |
1789 | #endif | 1791 | #endif |
1790 | 1792 | ||
1791 | if (size < 8) | 1793 | if (size < 8) { |
1794 | delete krc2; | ||
1792 | return PWERR_DATA; | 1795 | return PWERR_DATA; |
1796 | } | ||
1793 | 1797 | ||
1794 | for (count = 0; count < 4; count++) { | 1798 | for (count = 0; count < 4; count++) { |
1795 | count2 = count << 1; | 1799 | count2 = count << 1; |
1796 | iv[count] = charbuf[count2] << 8; | 1800 | iv[count] = charbuf[count2] << 8; |
1797 | iv[count] += charbuf[count2 + 1]; | 1801 | iv[count] += charbuf[count2 + 1]; |
1798 | } | 1802 | } |
1799 | 1803 | ||
1800 | size = 0; | 1804 | size = 0; |
1801 | bufferIndex = 0; | 1805 | bufferIndex = 0; |
1802 | #ifndef Q_WS_WIN | 1806 | #ifndef Q_WS_WIN |
1803 | while ((count = read (fileno (fd), (unsigned char *) charbuf, 8)) > 0) { | 1807 | while ((count = read (fileno (fd), (unsigned char *) charbuf, 8)) > 0) { |
1804 | while (count < 8) { | 1808 | while (count < 8) { |
1805 | count2 = read (fileno (fd), (unsigned char *) (charbuf + count), 8); | 1809 | count2 = read (fileno (fd), (unsigned char *) (charbuf + count), 8); |
1806 | #else | 1810 | #else |
1807 | while ((count = fread ((unsigned char *) (charbuf), sizeof(unsigned char), 8, fd)) > 0) { | 1811 | while ((count = fread ((unsigned char *) (charbuf), sizeof(unsigned char), 8, fd)) > 0) { |
1808 | while (count < 8) { | 1812 | while (count < 8) { |
1809 | count2 = fread ((unsigned char *) (charbuf + count), sizeof(unsigned char), 8, fd); | 1813 | count2 = fread ((unsigned char *) (charbuf + count), sizeof(unsigned char), 8, fd); |
1810 | #endif | 1814 | #endif |
1811 | if (count2 == 0) { | 1815 | if (count2 == 0) { |
1816 | delete krc2; | ||
1812 | return PWERR_DATA; | 1817 | return PWERR_DATA; |
1813 | } | 1818 | } |
1814 | count += count2; | 1819 | count += count2; |
1815 | } /* while (count < 8) */ | 1820 | } /* while (count < 8) */ |
1816 | 1821 | ||
1817 | size += 8; | 1822 | size += 8; |
1818 | for (count2 = 0; count2 < 8; count2 += 2) { | 1823 | for (count2 = 0; count2 < 8; count2 += 2) { |
1819 | count3 = count2 >> 1; | 1824 | count3 = count2 >> 1; |
1820 | ciphertext[count3] = charbuf[count2] << 8; | 1825 | ciphertext[count3] = charbuf[count2] << 8; |
1821 | ciphertext[count3] += charbuf[count2 + 1]; | 1826 | ciphertext[count3] += charbuf[count2 + 1]; |
1822 | 1827 | ||
1823 | plaintext[count3] = ciphertext[count3] ^ iv[count3]; | 1828 | plaintext[count3] = ciphertext[count3] ^ iv[count3]; |
1824 | iv[count3] = plaintext[count3]; | 1829 | iv[count3] = plaintext[count3]; |
1825 | } /* for (count2) */ | 1830 | } /* for (count2) */ |
1826 | 1831 | ||
1827 | krc2->rc2_decrypt (plaintext); | 1832 | krc2->rc2_decrypt (plaintext); |
1828 | memcpy ((unsigned char *) (buffer + bufferIndex), plaintext, 8); | 1833 | memcpy ((unsigned char *) (buffer + bufferIndex), plaintext, 8); |
1829 | bufferIndex += 8; | 1834 | bufferIndex += 8; |
1830 | buffer[bufferIndex + 1] = '\0'; | 1835 | buffer[bufferIndex + 1] = '\0'; |
1831 | } /* while ((count = read (fileno (fd), (unsigned char *) charbuf, 8)) > 0) */ | 1836 | } /* while ((count = read (fileno (fd), (unsigned char *) charbuf, 8)) > 0) */ |
1837 | delete krc2; | ||
1832 | size -= buffer[size - 1]; | 1838 | size -= buffer[size - 1]; |
1833 | lastcount = 0; | 1839 | lastcount = 0; |
1834 | 1840 | ||
1835 | /* This will point to the starting index */ | 1841 | /* This will point to the starting index */ |
1836 | bufferIndex = 0; | 1842 | bufferIndex = 0; |
1837 | return PWERR_GOOD; | 1843 | return PWERR_GOOD; |
1838 | } | 1844 | } |
1839 | 1845 | ||
1840 | int ZSafe::loadEntry(char *entry[FIELD_SIZE]) | 1846 | int ZSafe::loadEntry(char *entry[FIELD_SIZE]) |
1841 | { | 1847 | { |
1842 | /* Strip off PKCS 5 padding | 1848 | /* Strip off PKCS 5 padding |
1843 | * Should check to make sure it's good here | 1849 | * Should check to make sure it's good here |
1844 | */ | 1850 | */ |
1845 | int count, count1=0; | 1851 | int count, count1=0; |
1846 | 1852 | ||
1847 | for (count = lastcount; count < size; count++) { | 1853 | for (count = lastcount; count < size; count++) { |
1848 | if ((unsigned char) (buffer[count]) == 255) { | 1854 | if ((unsigned char) (buffer[count]) == 255) { |
1849 | if (buffer[bufferIndex] == '\0') { | 1855 | if (buffer[bufferIndex] == '\0') { |
1850 | bufferIndex++; | 1856 | bufferIndex++; |
1851 | } | 1857 | } |
1852 | entry[count1] = (char *) malloc (count - bufferIndex + 1); | 1858 | entry[count1] = (char *) malloc (count - bufferIndex + 1); |
1853 | memcpy (entry[count1], (unsigned char *) (buffer + bufferIndex), count - bufferIndex); | 1859 | memcpy (entry[count1], (unsigned char *) (buffer + bufferIndex), count - bufferIndex); |
1854 | entry[count1][count - bufferIndex] = '\0'; | 1860 | entry[count1][count - bufferIndex] = '\0'; |
1855 | count++; | 1861 | count++; |
@@ -2078,79 +2084,67 @@ void ZSafe::getDocPassword(QString title) | |||
2078 | 2084 | ||
2079 | dialog->PasswordField->setFocus(); | 2085 | dialog->PasswordField->setFocus(); |
2080 | QDialog::DialogCode result = (QDialog::DialogCode) dialog->exec(); | 2086 | QDialog::DialogCode result = (QDialog::DialogCode) dialog->exec(); |
2081 | 2087 | ||
2082 | QString password; | 2088 | QString password; |
2083 | if (result == QDialog::Accepted || newPwdDialogResult) | 2089 | if (result == QDialog::Accepted || newPwdDialogResult) |
2084 | { | 2090 | { |
2085 | m_password = dialog->PasswordField->text(); | 2091 | m_password = dialog->PasswordField->text(); |
2086 | } | 2092 | } |
2087 | else | 2093 | else |
2088 | { | 2094 | { |
2089 | exitZs (1); | 2095 | exitZs (1); |
2090 | } | 2096 | } |
2091 | } | 2097 | } |
2092 | 2098 | ||
2093 | int ZSafe::saveInit(const char *_filename, const char *password) | 2099 | int ZSafe::saveInit(const char *_filename, const char *password) |
2094 | { | 2100 | { |
2095 | char key[128]; | 2101 | char key[128]; |
2096 | unsigned int j = 0; | 2102 | unsigned int j = 0; |
2097 | unsigned int keylength; | 2103 | unsigned int keylength; |
2098 | // int val; | 2104 | // int val; |
2099 | int count2; | 2105 | int count2; |
2100 | Krc2* krc2 = new Krc2(); | 2106 | Krc2* krc2 = new Krc2(); |
2101 | 2107 | ||
2102 | /* first we should check the permissions of the filename */ | 2108 | QFileInfo f (_filename); |
2103 | /* | 2109 | save_buffer_length = f.size(); |
2104 | if (QFile::exists(_filename)) { | 2110 | save_buffer_length = ((save_buffer_length / 1024)+1) * 1024; |
2105 | val = checkFile(_filename); | ||
2106 | if (val != PWERR_GOOD) | ||
2107 | return val; | ||
2108 | } else | ||
2109 | { | ||
2110 | val = creat (_filename, (S_IRUSR | S_IWUSR)); | ||
2111 | if (val == -1) | ||
2112 | return PWERR_OPEN; | ||
2113 | else | ||
2114 | close(val); | ||
2115 | } | ||
2116 | */ | ||
2117 | QFileInfo f (_filename); | ||
2118 | save_buffer_length = f.size(); | ||
2119 | save_buffer_length = ((save_buffer_length / 1024)+1) * 1024; | ||
2120 | 2111 | ||
2121 | fd = fopen (_filename, "wb"); | 2112 | fd = fopen (_filename, "wb"); |
2122 | if (fd == NULL) | 2113 | if (fd == NULL) { |
2114 | delete krc2; | ||
2123 | return PWERR_OPEN; | 2115 | return PWERR_OPEN; |
2116 | } | ||
2124 | 2117 | ||
2125 | buffer = (char*)malloc(save_buffer_length); | 2118 | buffer = (char*)malloc(save_buffer_length); |
2126 | 2119 | ||
2127 | /* make the key ready */ | 2120 | /* make the key ready */ |
2128 | for (j = 0; password[j] != '\0'; j++) { | 2121 | for (j = 0; password[j] != '\0'; j++) { |
2129 | key[j] = password[j]; | 2122 | key[j] = password[j]; |
2130 | } | 2123 | } |
2131 | keylength = j; | 2124 | keylength = j; |
2132 | krc2->rc2_expandkey (key, keylength, 128); | 2125 | krc2->rc2_expandkey (key, keylength, 128); |
2126 | delete krc2; | ||
2133 | 2127 | ||
2134 | /* First, we make the IV */ | 2128 | /* First, we make the IV */ |
2135 | for (count2 = 0; count2 < 4; count2++) { | 2129 | for (count2 = 0; count2 < 4; count2++) { |
2136 | iv[count2] = rand (); | 2130 | iv[count2] = rand (); |
2137 | putc ((unsigned char) (iv[count2] >> 8), fd); | 2131 | putc ((unsigned char) (iv[count2] >> 8), fd); |
2138 | putc ((unsigned char) (iv[count2] & 0xff), fd); | 2132 | putc ((unsigned char) (iv[count2] & 0xff), fd); |
2139 | } | 2133 | } |
2140 | 2134 | ||
2141 | bufferIndex = 0; | 2135 | bufferIndex = 0; |
2142 | return PWERR_GOOD; | 2136 | return PWERR_GOOD; |
2143 | } | 2137 | } |
2144 | 2138 | ||
2145 | 2139 | ||
2146 | int ZSafe::saveEntry(char *entry[FIELD_SIZE]) | 2140 | int ZSafe::saveEntry(char *entry[FIELD_SIZE]) |
2147 | { | 2141 | { |
2148 | char *text1; | 2142 | char *text1; |
2149 | int count2, count3; | 2143 | int count2, count3; |
2150 | unsigned short ciphertext[4]; | 2144 | unsigned short ciphertext[4]; |
2151 | Krc2* krc2 = new Krc2(); | 2145 | Krc2* krc2 = new Krc2(); |
2152 | 2146 | ||
2153 | buffer = (char*)memset(buffer, '\0', save_buffer_length); | 2147 | buffer = (char*)memset(buffer, '\0', save_buffer_length); |
2154 | 2148 | ||
2155 | for (count2 = 0; count2 < FIELD_SIZE; count2++) { | 2149 | for (count2 = 0; count2 < FIELD_SIZE; count2++) { |
2156 | text1 = entry[count2]; | 2150 | text1 = entry[count2]; |
@@ -2167,88 +2161,95 @@ int ZSafe::saveEntry(char *entry[FIELD_SIZE]) | |||
2167 | * At the bottom, encrypted, I will append an MD5 hash of the file, eventually. | 2161 | * At the bottom, encrypted, I will append an MD5 hash of the file, eventually. |
2168 | * PKCS 5 padding (explained at the code section | 2162 | * PKCS 5 padding (explained at the code section |
2169 | */ | 2163 | */ |
2170 | while (count2 < (int)strlen (buffer)) { | 2164 | while (count2 < (int)strlen (buffer)) { |
2171 | #ifndef WORDS_BIGENDIAN | 2165 | #ifndef WORDS_BIGENDIAN |
2172 | plaintext[bufferIndex] = buffer[count2 + 1] << 8; | 2166 | plaintext[bufferIndex] = buffer[count2 + 1] << 8; |
2173 | plaintext[bufferIndex] += buffer[count2] & 0xff; | 2167 | plaintext[bufferIndex] += buffer[count2] & 0xff; |
2174 | #endif | 2168 | #endif |
2175 | #ifdef WORDS_BIGENDIAN | 2169 | #ifdef WORDS_BIGENDIAN |
2176 | plaintext[bufferIndex] = buffer[count2] << 8; | 2170 | plaintext[bufferIndex] = buffer[count2] << 8; |
2177 | plaintext[bufferIndex] += buffer[count2 + 1] & 0xff; | 2171 | plaintext[bufferIndex] += buffer[count2 + 1] & 0xff; |
2178 | #endif | 2172 | #endif |
2179 | bufferIndex++; | 2173 | bufferIndex++; |
2180 | if (bufferIndex == 4) { | 2174 | if (bufferIndex == 4) { |
2181 | krc2->rc2_encrypt (plaintext); | 2175 | krc2->rc2_encrypt (plaintext); |
2182 | 2176 | ||
2183 | for (count3 = 0; count3 < 4; count3++) { | 2177 | for (count3 = 0; count3 < 4; count3++) { |
2184 | ciphertext[count3] = iv[count3] ^ plaintext[count3]; | 2178 | ciphertext[count3] = iv[count3] ^ plaintext[count3]; |
2185 | 2179 | ||
2186 | /* Now store the ciphertext as the iv */ | 2180 | /* Now store the ciphertext as the iv */ |
2187 | iv[count3] = plaintext[count3]; | 2181 | iv[count3] = plaintext[count3]; |
2188 | 2182 | ||
2189 | /* reset the buffer index */ | 2183 | /* reset the buffer index */ |
2190 | bufferIndex = 0; | 2184 | bufferIndex = 0; |
2191 | if (putc ((unsigned char) (ciphertext[count3] >> 8), fd) == EOF) return PWERR_DATA; | 2185 | if (putc ((unsigned char) (ciphertext[count3] >> 8), fd) == EOF) { |
2192 | if (putc ((unsigned char) (ciphertext[count3] & 0xff), fd) == EOF) return PWERR_DATA; | 2186 | delete krc2; |
2187 | return PWERR_DATA; | ||
2188 | } | ||
2189 | if (putc ((unsigned char) (ciphertext[count3] & 0xff), fd) == EOF) { | ||
2190 | delete krc2; | ||
2191 | return PWERR_DATA; | ||
2192 | } | ||
2193 | } /*for (count3 = 0; count3 < 5; count3++)*/ | 2193 | } /*for (count3 = 0; count3 < 5; count3++)*/ |
2194 | } /*if (bufferIndex == 5)*/ | 2194 | } /*if (bufferIndex == 5)*/ |
2195 | /* increment a short, not a byte */ | 2195 | /* increment a short, not a byte */ |
2196 | count2 += 2; | 2196 | count2 += 2; |
2197 | } /*while (count2 < strlen (buffer))*/ | 2197 | } /*while (count2 < strlen (buffer))*/ |
2198 | int ret = PWERR_GOOD; | 2198 | delete krc2; |
2199 | return ret; | 2199 | return PWERR_GOOD; |
2200 | } | 2200 | } |
2201 | 2201 | ||
2202 | int ZSafe::saveFinalize(void) | 2202 | int ZSafe::saveFinalize(void) |
2203 | { | 2203 | { |
2204 | int count1, retval = PWERR_GOOD; | 2204 | int count1, retval = PWERR_GOOD; |
2205 | unsigned short ciphertext[4]; | 2205 | unsigned short ciphertext[4]; |
2206 | Krc2* krc2 = new Krc2(); | 2206 | Krc2* krc2 = new Krc2(); |
2207 | 2207 | ||
2208 | /* Tack on the PKCS 5 padding | 2208 | /* Tack on the PKCS 5 padding |
2209 | * How it works is we fill up the last n bytes with the value n | 2209 | * How it works is we fill up the last n bytes with the value n |
2210 | * | 2210 | * |
2211 | * So, if we have, say, 13 bytes, 8 of which are used, we have 5 left | 2211 | * So, if we have, say, 13 bytes, 8 of which are used, we have 5 left |
2212 | * over, leaving us 3 short, so we fill it in with 3's. | 2212 | * over, leaving us 3 short, so we fill it in with 3's. |
2213 | * | 2213 | * |
2214 | * If we come out even, we fill it with 8 8s | 2214 | * If we come out even, we fill it with 8 8s |
2215 | * | 2215 | * |
2216 | * um, except that in this instance we are using 4 shorts instead of 8 bytes. | 2216 | * um, except that in this instance we are using 4 shorts instead of 8 bytes. |
2217 | * so, half everything | 2217 | * so, half everything |
2218 | */ | 2218 | */ |
2219 | for (count1 = bufferIndex; count1 < 4; count1++) { | 2219 | for (count1 = bufferIndex; count1 < 4; count1++) { |
2220 | plaintext[count1] = (4 - bufferIndex); | 2220 | plaintext[count1] = (4 - bufferIndex); |
2221 | } | 2221 | } |
2222 | krc2->rc2_encrypt (plaintext); | 2222 | krc2->rc2_encrypt (plaintext); |
2223 | for (count1 = 0; count1 < 4; count1++) { | 2223 | for (count1 = 0; count1 < 4; count1++) { |
2224 | ciphertext[count1] = iv[count1] ^ plaintext[count1]; | 2224 | ciphertext[count1] = iv[count1] ^ plaintext[count1]; |
2225 | if (putc ((unsigned char) (ciphertext[count1] >> 8), fd) == EOF) retval = PWERR_DATA; | 2225 | if (putc ((unsigned char) (ciphertext[count1] >> 8), fd) == EOF) retval = PWERR_DATA; |
2226 | if (putc ((unsigned char) (ciphertext[count1] & 0xff), fd) == EOF) retval = PWERR_DATA; | 2226 | if (putc ((unsigned char) (ciphertext[count1] & 0xff), fd) == EOF) retval = PWERR_DATA; |
2227 | } | 2227 | } |
2228 | 2228 | ||
2229 | fclose (fd); | 2229 | fclose (fd); |
2230 | free(buffer); | 2230 | free(buffer); |
2231 | delete krc2; | ||
2231 | return retval; | 2232 | return retval; |
2232 | } | 2233 | } |
2233 | 2234 | ||
2234 | void ZSafe::quitMe () | 2235 | void ZSafe::quitMe () |
2235 | { | 2236 | { |
2236 | if (modified) | 2237 | if (modified) |
2237 | { | 2238 | { |
2238 | switch( QMessageBox::information( this, tr("ZSafe"), | 2239 | switch( QMessageBox::information( this, tr("ZSafe"), |
2239 | tr("Do you want to save\nbefore exiting?"), | 2240 | tr("Do you want to save\nbefore exiting?"), |
2240 | tr("&Save"), | 2241 | tr("&Save"), |
2241 | tr("S&ave with\nnew\npassword"), | 2242 | tr("S&ave with\nnew\npassword"), |
2242 | tr("&Don't Save"), | 2243 | tr("&Don't Save"), |
2243 | 0 // Enter == button 0 | 2244 | 0 // Enter == button 0 |
2244 | ) ) | 2245 | ) ) |
2245 | { // Escape == button 2 | 2246 | { // Escape == button 2 |
2246 | case 0: // Save clicked, Alt-S or Enter pressed. | 2247 | case 0: // Save clicked, Alt-S or Enter pressed. |
2247 | // save | 2248 | // save |
2248 | modified = false; | 2249 | modified = false; |
2249 | saveDocument(filename, FALSE); | 2250 | saveDocument(filename, FALSE); |
2250 | exitZs (1); | 2251 | exitZs (1); |
2251 | break; | 2252 | break; |
2252 | case 1: // | 2253 | case 1: // |
2253 | // Save with new password | 2254 | // Save with new password |
2254 | modified = false; | 2255 | modified = false; |