Commit Graph

820 Commits

Author SHA1 Message Date
cketti 19b180a3b6 Avoid NullPointerException reported via Google Play 2014-06-20 22:37:43 +02:00
Dominik Heidler dcd1e63812 Fix sendCommand line splitup for some imap proxys
When sending a command it would be sent like this:
PKG1: 1 STARTTLS
PKG2: \r\n

Some imap proxys (maybe from Fortinet?) don't accept commands across packets:
PKG1: 1 STARTTLS\r\n
2014-06-20 22:36:47 +02:00
cketti e8f7f6ef38 Fix 'endless' loop in ImapFolderPusher
Under certain circumstances it's possible that the 'push state' isn't
updated to contain the most recent 'UIDNEXT' value. In that case
ImapFolderPusher.start() would execute the same code path through its
main loop over and over again, preventing the device from going to
sleep.
Rather than changing the code to update the 'push state' in the corner
case that triggers the behavior described above, this commit introduces
another mechanism to track the 'UIDNEXT' value. This should also catch
as of yet unknown cases where the 'push state' isn't properly updated.

At some point in the future I hope we get to a point where we only
persist the 'push state' when we manually stop/restart the service.
During normal operation there's no need to read from/write to storage
all the time.

Fixes issue 4907
2014-06-20 22:36:13 +02:00
Joe Steele d1fc587772 Fix POP3 STLS command
The server response needed to be retrieved.

Thanks to Paul Durrant:
https://groups.google.com/d/msg/k-9-mail/0XHNNMR1TQ4/yExsr7nvJQwJ
2014-01-14 05:46:01 +01:00
cketti cfaa36403c Revert "Make IMAP autoconfig recognize "Draft" as drafts folder"
This reverts commit df0f6f3a8e.
See https://github.com/k9mail/k-9/pull/429
2013-12-17 18:47:38 +01:00
erlendorf df0f6f3a8e Make IMAP autoconfig recognize "Draft" as drafts folder
Yahoo names it "Draft" instead of the more common "Drafts".
2013-12-17 02:37:41 +01:00
Joe Steele fd9eb78aa3 Move some classes out of com.fsck.k9.mail.store
The classes are just as much related to com.fsck.k9.mail.transport
as com.fsck.k9.mail.store, so having them in
com.fsck.k9.mail.store doesn't seem appropriate.

Move LocalKeyStore to com.fsck.k9.security

Move TrustManagerFactory and TrustedSocketFactory to com.fsck.k9.net.ssl
2013-12-17 02:35:26 +01:00
Joe Steele a2162e4c37 Extract code into new LocalKeyStore class
Also, implement the ability to configure an alternate key store
file location. This permits the running of unit tests without
clobbering the live key store file.

Also, add a test to confirm that the key store file is being written
out and reread correctly.
2013-12-17 02:35:26 +01:00
cketti 0c96c799eb Only check against the certificate stored for a server, not all of them 2013-12-17 02:35:26 +01:00
cketti 9cc98bcd33 Add unit tests for TrustManagerFactory 2013-12-17 02:35:26 +01:00
Joe Steele 583d1d403f Fix inadequate certificate validation
Proper host name validation was not being performed for certificates
kept in the local keystore.  If an attacker could convince a user to
accept and store an attacker's certificate, then that certificate
could be used for MITM attacks, giving the attacker access to all
connections to all servers in all accounts in K-9.

This commit changes how the certificates are stored.  Previously, an
entire certificate chain was stored for a server (and any of those
certificates in the chain were available for validating signatures on
certificates received when connecting).  Now just the single
certificate for the server is stored.

This commit changes how locally stored certificates are retrieved.
They can only be retrieved using the host:port that the user
configured for the server.

This also fixes issue 1326.  Users can now use different certificates
for different servers on the same host (listening to different ports).

The above changes mean that users might have to re-accept certificates
that they had previously accepted and are still using (but only if the
certificate's Subject doesn't match the host that they are connecting
to).

This commit modifies AccountSetupBasics so that it now calls
AccountSetupCheckSettings twice -- once for checking the incoming
settings and once for the outgoing settings.  Otherwise, an exception
could occur while checking incoming settings, the user could say
continue (or the user could accept a certificate key), and the
outgoing settings would not be checked.  This also helps with
determining if a certificate exception was for the incoming or
outgoing server, which is needed if the user decides to add the
certificate to the keystore.
2013-12-17 02:35:25 +01:00
brian m. carlson 266a715138 Use TrustedSocketFactory for STARTTLS.
The TrustedSocketFactory, which provides goodies like better cipher suites and
TLSv1.2, was only being used for tunnelled connections.  Use it for STARTTLS
connections as well.
2013-12-16 18:59:37 +01:00
cketti a036e4d2f9 Merge branch 'tls-hardening' 2013-10-29 04:40:06 +01:00
cketti a97705ffa9 Refactor TrustedSocketFactory 2013-10-29 04:37:52 +01:00
cketti 8f45d76b5c Extract WebDavSocketFactory 2013-10-29 03:42:37 +01:00
András Veres-Szentkirályi 105d3b3c4e filter TLS protocol versions by support as well 2013-10-20 10:42:39 +02:00
András Veres-Szentkirályi 77d43fb7bd extracted method filterBySupport 2013-10-20 10:30:33 +02:00
András Veres-Szentkirályi dbc47c7297 filter enabled cipher suites by availability 2013-10-20 10:22:53 +02:00
András Veres-Szentkirályi d84ce6ddb9 Hardened TLS cipher suites and versions
As Georg Lukas wrote in his blog post about how Android handles TLS
handshake (http://op-co.de/blog/posts/android_ssl_downgrade/), an
explicit order of cipher suites and TLS versions must be supplied to
avoid having the weak (presumably broken) RC4 cipher at the top of the
preference list.

This commit adds the list included in the blog post to every TLS socket
creation, including IMAP, POP3 and SMTP, see Wireshark screenshots done
during testing at http://vsza.hu/k9mail-tls-hardening/
2013-10-15 10:16:42 +02:00
Joe Steele 5a46575dc2 Generally replace \n with \r\n when part of a message
This builds upon the efforts started 2 commits back where \r\n is used for
all message text and \n is only used when the text is inside an
EolConvertingEditText widget.
2013-10-11 11:39:46 -04:00
Joe Steele bfb0316583 Fix fatal IndexOutOfBoundsException
The problem:

Configure the account (just an example -- problems can occur in other
configurations as well):
Message Format: HTML
Reply quoting style: Prefix
Quote message when replying: yes
Reply after quoted text: yes

Reply to a message that has a large quantity (20+) of \r\n scattered in
the body of its HTML version (not an unusual scenario).

Add a reply.  Save the message as a draft.  Go back & open the draft
again.  A fatal IndexOutOfBoundsException occurs.

The cause:

When the draft was saved, the X-K9mail-Identity header was computed and
added to the message, then the text of the message was processed with
MimeUtility.fixDraftTextBody, replacing all occurrences of \r\n with \n in
the quoted message before being saved in LocalStore, thus invalidating the
X-K9mail-Identity header.

The fix:

Remove MimeUtility.fixDraftTextBody and implement
MessageCompose$EolConvertingEditText instead.  Any message text placed in
an EolConvertingEditText widget is assured to have \n line endings.  Any
message text extracted from an EolConvertingEditText widget is assured to
have \r\n line endings.  The X-K9mail-Identity header will always be
computed correctly.

Issues thought to be related:  4782, 5010, 5634, 5725

As noted in some of the referenced issues, errors didn't always result in
a fatal exception, but instead with mixed up text.

Ref:  commit f9a35aeaee
2013-10-11 11:39:15 -04:00
Joe Steele 33678ea022 Eliminate unnecessary override in LocalAttachmentBody.
The overriding code is the same as the overridden code.
2013-10-11 11:24:56 -04:00
Joe Steele 1afff1e38f Delete related files when deleting a database.
The journal file was not being deleted when an account was deleted.
Over time, one can end up with a collection of dead journal files.
2013-10-11 11:24:51 -04:00
Danny Baumann 8777441d92 Use address object as contact picture instead of email address only.
Fixes assignment problems for emails sent by some issue tracking
systems, which send out mails with a fixed mail address on behalf of
different people.
2013-09-27 08:53:14 +02:00
cketti 2b7f5e7b70 Merge branch 'pick_attachment_fix'
Update LocalStore code to handle the newly introduced temporary files
for attachments

Conflicts:
	res/values/strings.xml
	src/com/fsck/k9/activity/MessageCompose.java
2013-09-25 05:22:00 +02:00
cketti 62aa1b87d0 Fetch attachments while MessageCompose activity is running
Android allows other apps to access protected content of an app without requesting the
necessary permission when the app returns an Intent with FLAG_GRANT_READ_URI_PERMISSION.
This regularly happens as a result of ACTION_GET_CONTENT, i.e. what we use to pick content
to be attached to a message. Accessing that content only works while the receiving activity
is running. Afterwards accessing the content throws a SecurityException because of the
missing permission.
This commit changes K-9 Mail's behavior to copy the content to a temporary file in K-9's
cache directory while the activity is still running.

Fixes issue 4847, 5821

This also fixes bugs related to the fact that K-9 Mail didn't save a copy of attached content
in the message database.

Fixes issue 1187, 3330, 4930
2013-09-25 03:12:34 +02:00
Koji Arai 0be299fbd0 Avoid StringIndexOutOfBoundsException when the header is broken like "=?us-ascii?q?abc?= =?" 2013-09-20 13:15:57 +09:00
Joe Steele 1ff128890d Fix MimeMessage(InputStream in, boolean recurse)
No actual impact, because all existing calls were
with recurse = true.
2013-09-07 12:52:14 -04:00
cketti aaa0de4dbc Don't append "null" to the "MAIL FROM" line in case the server doesn't support 8BITMIME 2013-09-06 20:28:46 +02:00
cketti a7c9804995 Make sure the InputStream is always closed
Moving the code to create the OutputStream wrappers into the try-block ensures
that the InputStream is closed in case something goes wrong.
2013-09-06 19:42:10 +02:00
cketti 6cd52c123c Remove unnecessary type cast 2013-09-06 19:38:15 +02:00
Joe Steele 1ca1ef5c84 Content-type case-conversion changes.
Don't convert the content-type to lower case in
MimeMessage.getContentType.  The content-type may have optional parameters
that are case sensitive (boundary, name).

In removing the lower-case conversion from getContentType, a review was
made for inappropriate case-sensitive comparisons which use data obtained
with getContentType.  The only ones found were in isMimeType in both
Message and MimeBodyPart.

Case-sensitive instances of isMimeType were made case-insensitive.  Also,
isMimeType was moved from Message to MimeMessage for symmetry with
MimeBodyPart (MimeMessage & MimeBodyPart are similar and contain a good
bit of duplication such as this).

The unit test required fixing now that the case of the boundary text is
preserved.

References:

Commits 2c5186 and dc4002 added the toLowerCase to getContentType in
MimeMessage & MimeBodyPart (Issue 94).

Later, commit 50cd60 removed the toLowerCase addition from MimeBodyPart
(Issue 1289).
2013-09-03 19:54:18 -04:00
Joe Steele 63f68328ff Standardize line breaks within headers.
Fix the unit test to match.

All line endings in the unit test are now the same.

(Just for consistency.  Not a big deal, since such problems are fixed when
the messages are run through EOLConvertingOutputStream.)
2013-09-03 19:54:17 -04:00
Joe Steele 45e3d8459e Recursively convert attachments of type message/rfc822 to 7bit if necessary.
The preceding commit resulted in attachments of type message/rfc822 being
sent with 8bit encoding even when the SMTP server did not support
8BITMIME.  This commit assures that messages will be converted to 7bit
when necessary.

A new interface CompositeBody was created that extends Body, and classes
Message and Multipart were changed from implementing Body to
CompositeBody.  Additional classes BinaryTempFileMessageBody and
LocalAttachmentMessageBody were created (by extending BinaryTempFileBody
and LocalAttachmentBody, respectively), and they too implement
CompositeBody.

A CompositeBody is a Body containing a composite-type that can contain
subparts that may require recursive processing when converting from 8bit
to 7bit.  The Part to which a CompositeBody belongs is only permitted to
use 8bit or 7bit encoding for the CompositeBody.

Previously, a Message was created so that it was 7bit clean by default
(even though that meant base64 encoding all attachments, including
messages).  Then, if the SMTP server supported 8BITMIME,
Message.setEncoding("8bit") was called so that bodies of type TextBody
would been transmitted using 8bit encoding rather than quoted-printable.

Now, messages are created with 8bit encoding by default.  Then, if the
SMTP server does not support 8BITMIME, Message.setUsing7bitTransport is
called to recursively convert the message and its subparts to 7bit.  The
method setUsing7bitTransport was added to the interfaces Part and
CompositeBody.

setEncoding no longer iterates over parts in Multipart.  That task belongs
to setUsing7bitTransport, which may in turn call setEncoding on the parts.

MimeUtility.getEncodingforType was created as a helper function for
choosing a default encoding that should be used for a given MIME type when
an attachment is added to a message (either while composing or when
retrieving from LocalStore).

setEncoding was implemented in MimeBodyPart to assure that the encoding
set in the Part's headers was the same as set for the Part's Body.  (The
method already existed in MimeMessage, which has similarities with
MimeBodyPart.)

MimeMessage.parse(InputStream in, boolean recurse) was implemented so that
the parser could be told to recursively process nested messages read from
the InputStream, thus giving access to all subparts at any level that may
need to be converted from 8bit to 7bit.
2013-09-03 19:53:13 -04:00
Joe Steele 77407eb5b7 Don't base64 encode attachments of type message/rfc822.
The problem:  Receive a message with an attachment of type message/rfc822
and forward it.  When the message is sent, K-9 Mail uses base64 encoding
for the attachment.  (Alternatively, you could compose a new message and
add such an attachment from a file using a filing-picking app, but that is
not 100% effective because the app may not choose the correct
message/rfc822 MIME type for the attachment.)

Such encoding is prohibited per RFC 2046 (5.2.1) and RFC 2045 (6.4).  Only
8bit or 7bit encoding is permitted for attachments of type message/rfc822.

Thunderbird refuses to decode such attachments.  All that is shown is the
base64 encoded body.

This commit implements LocalAttachmentBody.setEncoding.  If an attachment
to a newly composed message is itself a message, then setEncoding("8bit")
is called, otherwise setEncoding("base64")  is called for the attachment.
Similar behavior occurs when an attachment is retrieved from LocalStore.

The setEncoding method was added to the Body interface, since all
implementations of Body now declare the method.

The problem here differs from that in the preceding commit:  Here, the
encoding problem occurs on sending, not on receipt.  Here, the entire
message (headers and body) is base64 encoded, not just the body.  Here,
the headers correctly identify the encoding used;  it's just that the RFC
does not permit such encoding of attached messages.  The problem here
could in fact occur in combination with the preceding problem.
2013-09-03 19:52:11 -04:00
Joe Steele 1d1db50a9f Don't always base64 encode in BinaryTempFileBody.writeTo
Issue 5734 exemplifies the problem:  receive a message with an attachment
of type message/rfc822 that doesn't use base64 encoding for the body of
the attached message.  K-9 Mail incorrectly stores the attached message
locally with its original headers but using base64 encoding for the body.
A discrepancy thus exists between what the headers say about the encoding
of the body versus the actual encoding used.  This is obvious when
attempting to view the attachment (either by using a compatible message
viewer available on the device or by saving the attachment to a file and
viewing the file contents).

The process: When a message with an attached sub-message is received,
Message.parse puts the attachment in a new MimeMessage with the
attachment's body in a BinaryTempFileBody.  LocalFolder.saveAttachment
then calls Message.writeTo (which later calls BinaryTempFileBody.writeTo)
to place the entire attachment (headers and body) in a new file that will
become a LocalAttachmentBody.  Until now,  BinaryTempFileBody.writeTo
could only save the message body using base64 encoding.

This commit implements BinaryTempFileBody.setEncoding and assures that the
body is written out with the same encoding that was found in its  headers.
2013-09-03 19:51:26 -04:00
Joe Steele de23a0e3e1 SMTP 8BITMIME compliance
Currently, K-9 Mail detects if an SMTP server supports 8BITMIME (RFC
6152), and if so, TextBody parts are sent with content-transfer-ecoding =
8bit.  Otherwise, they are sent using quoted-printable.

This adds the required "BODY=8BITMIME" parameter to the MAIL command when
sending messages to servers that support 8BITMIME.
2013-09-03 16:47:57 -04:00
cketti 8fb2c5a490 Merge branch 'pr/349'
Remove remote/local store references when deleting accounts
2013-08-30 03:03:30 +02:00
cketti 9b370d0620 Add code to remove references to LocalStore when deleting accounts 2013-08-30 03:02:50 +02:00
cketti 01336944ec Refactor code to remove remote store references when deleting accounts 2013-08-30 02:28:57 +02:00
Joe Steele ac42bce799 Clean-up related to certificate chains
Per comments in pull request #365
2013-08-27 18:48:07 -04:00
Joe Steele 604aa87ccf Fix erroneous SSL certificate warnings
If you attempted to use SSL to connect to a server that speaks
STARTTLS, you should get an SSL protocol error.  Instead, you
were likely to get an "Unrecognized Certificate" error that shows
you an unrelated certificate chain and asks you to accept it or
reject it.  Neither action would work because the actual problem
had nothing to do with certificates.  The unrelated certificate
chain that popped up had been statically stored when validating
a prior connection to a different server.

With this patch, certificate chains are no longer stored statically
when validating server connections.

Issue 5886 is an example of a user experiencing this problem.
2013-08-25 15:43:36 -04:00
Jesse Vincent 7aeaa46fe6 Koji Arai pointed out that I screwed up the constants for folder open modes 2013-08-25 11:36:25 -04:00
cketti ce56475a4f Avoid race condition that can lead to a NullPointerException 2013-08-21 12:33:27 +02:00
cketti f11f0fcc9b Skip incorrectly formatted/parsed LSUB/LIST replies
Example: * LIST (\HasChildren) "." [Folder]
"[Folder]" will be parsed as list containing the element "Folder".
2013-08-20 18:39:20 +02:00
Joe Steele b7c67019af SMTP dot stuffing should be done *after* line wrapping. 2013-08-13 19:27:40 -04:00
Joe Steele 564195bad9 Close stream when done (StrictMode error) 2013-08-09 19:17:07 -04:00
Joe Steele fcabb7395d bug fix: use "1" not "true" for SQL boolean
Ref:  Pull request #355 from @edemaine
2013-08-02 13:01:22 -04:00
Jesse Vincent bbad298bf6 Convert OpenMode from an Enum to static ints for perf improvement.
(Based on profiling of long folder list opens)

This should be backported to 4.4
2013-07-30 21:32:35 -04:00
Jesse Vincent e9a0cbb269 Ask SQLite for a simpler calculation when counting unread messages. The docs imply this is a decent perf win. I'm seeing at least a bit of one 2013-07-30 21:32:35 -04:00