summaryrefslogtreecommitdiffstats
path: root/emailcommon/src/com/android/emailcommon/provider/HostAuth.java
blob: c23f276a83912eb6dfc05c8df112515170b6eab8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
/*
 * Copyright (C) 2011 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */


package com.android.emailcommon.provider;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.os.Parcel;
import android.os.Parcelable;
import android.text.TextUtils;

import com.android.emailcommon.utility.SSLUtils;
import com.android.mail.utils.LogUtils;
import com.google.common.annotations.VisibleForTesting;

import org.json.JSONException;
import org.json.JSONObject;

import java.net.URI;
import java.net.URISyntaxException;

public class HostAuth extends EmailContent implements Parcelable {
    public static final String TABLE_NAME = "HostAuth";
    public static Uri CONTENT_URI;

    public static void initHostAuth() {
        CONTENT_URI = Uri.parse(EmailContent.CONTENT_URI + "/hostauth");
    }

    // These legacy constants should be used in code created prior to Email2
    public static final String LEGACY_SCHEME_SMTP = "smtp";

    public static final String SCHEME_TRUST_ALL_CERTS = "trustallcerts";

    public static final int PORT_UNKNOWN = -1;

    public static final int FLAG_NONE         = 0x00;    // No flags
    public static final int FLAG_SSL          = 0x01;    // Use SSL
    public static final int FLAG_TLS          = 0x02;    // Use TLS
    public static final int FLAG_AUTHENTICATE = 0x04;    // Use name/password for authentication
    public static final int FLAG_TRUST_ALL    = 0x08;    // Trust all certificates
    public static final int FLAG_OAUTH        = 0x10;    // Use OAuth for authentication
    // Mask of settings directly configurable by the user
    public static final int USER_CONFIG_MASK  = 0x1b;
    public static final int FLAG_TRANSPORTSECURITY_MASK = FLAG_SSL | FLAG_TLS | FLAG_TRUST_ALL;

    public String mProtocol;
    public String mAddress;
    public int mPort;
    public int mFlags;
    public String mLogin;
    public String mPassword;
    public String mDomain;
    public String mClientCertAlias = null;
    // NOTE: The server certificate is NEVER automatically retrieved from EmailProvider
    public byte[] mServerCert = null;
    public long mCredentialKey;

    @VisibleForTesting
    static final String JSON_TAG_CREDENTIAL = "credential";
    public transient Credential mCredential;

    public static final int CONTENT_ID_COLUMN = 0;
    public static final int CONTENT_PROTOCOL_COLUMN = 1;
    public static final int CONTENT_ADDRESS_COLUMN = 2;
    public static final int CONTENT_PORT_COLUMN = 3;
    public static final int CONTENT_FLAGS_COLUMN = 4;
    public static final int CONTENT_LOGIN_COLUMN = 5;
    public static final int CONTENT_PASSWORD_COLUMN = 6;
    public static final int CONTENT_DOMAIN_COLUMN = 7;
    public static final int CONTENT_CLIENT_CERT_ALIAS_COLUMN = 8;
    public static final int CONTENT_CREDENTIAL_KEY_COLUMN = 9;

    public static final String[] CONTENT_PROJECTION = new String[] {
            HostAuthColumns._ID, HostAuthColumns.PROTOCOL, HostAuthColumns.ADDRESS,
            HostAuthColumns.PORT, HostAuthColumns.FLAGS, HostAuthColumns.LOGIN,
            HostAuthColumns.PASSWORD, HostAuthColumns.DOMAIN, HostAuthColumns.CLIENT_CERT_ALIAS,
            HostAuthColumns.CREDENTIAL_KEY
    };

    public HostAuth() {
        mBaseUri = CONTENT_URI;
        mPort = PORT_UNKNOWN;
        mCredentialKey = -1;
    }

     /**
     * Restore a HostAuth from the database, given its unique id
     * @param context for provider loads
     * @param id corresponds to rowid
     * @return the instantiated HostAuth
     */
    public static HostAuth restoreHostAuthWithId(Context context, long id) {
        return EmailContent.restoreContentWithId(context, HostAuth.class,
                HostAuth.CONTENT_URI, HostAuth.CONTENT_PROJECTION, id);
    }

    /**
     * Returns the credential object for this HostAuth. This will load from the
     * database if the HosAuth has a valid credential key, or return null if not.
     */
    public Credential getCredential(Context context) {
        if (mCredential == null) {
            if (mCredentialKey >= 0) {
                mCredential = Credential.restoreCredentialsWithId(context, mCredentialKey);
            }
        }
        return mCredential;
    }

    /**
     * getOrCreateCredential Return the credential object for this HostAuth,
     * creating it if it does not yet exist. This should not be called on the
     * main thread.
     *
     * As a side-effect, it also ensures FLAG_OAUTH is set. Use {@link #removeCredential()} to clear
     *
     * @param context for provider loads
     * @return the credential object for this HostAuth
     */
    public Credential getOrCreateCredential(Context context) {
        mFlags |= FLAG_OAUTH;
        if (mCredential == null) {
            if (mCredentialKey >= 0) {
                mCredential = Credential.restoreCredentialsWithId(context, mCredentialKey);
            } else {
                mCredential = new Credential();
            }
        }
        return mCredential;
    }

    /**
     * Clear the credential object.
     */
    public void removeCredential() {
        mCredential = null;
        mCredentialKey = -1;
        mFlags &= ~FLAG_OAUTH;
    }

    /**
     * Builds a URI scheme name given the parameters for a {@code HostAuth}. If
     * a {@code clientAlias} is provided, this indicates that a secure
     * connection must be used.
     *
     * This is not used in live code, but is kept here for reference when creating providers.xml
     * entries
     */
    @SuppressWarnings("unused")
    public static String getSchemeString(String protocol, int flags, String clientAlias) {
        String security = "";
        switch (flags & USER_CONFIG_MASK) {
            case FLAG_SSL:
                security = "+ssl+";
                break;
            case FLAG_SSL | FLAG_TRUST_ALL:
                security = "+ssl+trustallcerts";
                break;
            case FLAG_TLS:
                security = "+tls+";
                break;
            case FLAG_TLS | FLAG_TRUST_ALL:
                security = "+tls+trustallcerts";
                break;
        }

        if (!TextUtils.isEmpty(clientAlias)) {
            if (TextUtils.isEmpty(security)) {
                throw new IllegalArgumentException(
                        "Can't specify a certificate alias for a non-secure connection");
            }
            if (!security.endsWith("+")) {
                security += "+";
            }
            security += SSLUtils.escapeForSchemeName(clientAlias);
        }

        return protocol + security;
    }

    /**
     * Returns the flags for the specified scheme.
     */
    public static int getSchemeFlags(String scheme) {
        String[] schemeParts = scheme.split("\\+");
        int flags = HostAuth.FLAG_NONE;
        if (schemeParts.length >= 2) {
            String part1 = schemeParts[1];
            if ("ssl".equals(part1)) {
                flags |= HostAuth.FLAG_SSL;
            } else if ("tls".equals(part1)) {
                flags |= HostAuth.FLAG_TLS;
            }
            if (schemeParts.length >= 3) {
                String part2 = schemeParts[2];
                if (SCHEME_TRUST_ALL_CERTS.equals(part2)) {
                    flags |= HostAuth.FLAG_TRUST_ALL;
                }
            }
        }
        return flags;
    }

    @Override
    public void restore(Cursor cursor) {
        mBaseUri = CONTENT_URI;
        mId = cursor.getLong(CONTENT_ID_COLUMN);
        mProtocol = cursor.getString(CONTENT_PROTOCOL_COLUMN);
        mAddress = cursor.getString(CONTENT_ADDRESS_COLUMN);
        mPort = cursor.getInt(CONTENT_PORT_COLUMN);
        mFlags = cursor.getInt(CONTENT_FLAGS_COLUMN);
        mLogin = cursor.getString(CONTENT_LOGIN_COLUMN);
        mPassword = cursor.getString(CONTENT_PASSWORD_COLUMN);
        mDomain = cursor.getString(CONTENT_DOMAIN_COLUMN);
        mClientCertAlias = cursor.getString(CONTENT_CLIENT_CERT_ALIAS_COLUMN);
        mCredentialKey = cursor.getLong(CONTENT_CREDENTIAL_KEY_COLUMN);
        if (mCredentialKey != -1) {
            mFlags |= FLAG_OAUTH;
        }
    }

    @Override
    public ContentValues toContentValues() {
        ContentValues values = new ContentValues();
        values.put(HostAuthColumns.PROTOCOL, mProtocol);
        values.put(HostAuthColumns.ADDRESS, mAddress);
        values.put(HostAuthColumns.PORT, mPort);
        values.put(HostAuthColumns.FLAGS, mFlags);
        values.put(HostAuthColumns.LOGIN, mLogin);
        values.put(HostAuthColumns.PASSWORD, mPassword);
        values.put(HostAuthColumns.DOMAIN, mDomain);
        values.put(HostAuthColumns.CLIENT_CERT_ALIAS, mClientCertAlias);
        values.put(HostAuthColumns.CREDENTIAL_KEY, mCredentialKey);
        values.put(HostAuthColumns.ACCOUNT_KEY, 0); // Need something to satisfy the DB

        return values;
    }

    protected JSONObject toJson() {
        try {
            final JSONObject json = new JSONObject();
            json.put(HostAuthColumns.PROTOCOL, mProtocol);
            json.put(HostAuthColumns.ADDRESS, mAddress);
            json.put(HostAuthColumns.PORT, mPort);
            json.put(HostAuthColumns.FLAGS, mFlags);
            json.put(HostAuthColumns.LOGIN, mLogin);
            json.putOpt(HostAuthColumns.PASSWORD, mPassword);
            json.putOpt(HostAuthColumns.DOMAIN, mDomain);
            json.putOpt(HostAuthColumns.CLIENT_CERT_ALIAS, mClientCertAlias);
            if (mCredential != null) {
                json.putOpt(JSON_TAG_CREDENTIAL, mCredential.toJson());
            }
            return json;
        } catch (final JSONException e) {
            LogUtils.d(LogUtils.TAG, e, "Exception while serializing HostAuth");
        }
        return null;
    }

    protected static HostAuth fromJson(final JSONObject json) {
        try {
            final HostAuth h = new HostAuth();
            h.mProtocol = json.getString(HostAuthColumns.PROTOCOL);
            h.mAddress = json.getString(HostAuthColumns.ADDRESS);
            h.mPort = json.getInt(HostAuthColumns.PORT);
            h.mFlags = json.getInt(HostAuthColumns.FLAGS);
            h.mLogin = json.getString(HostAuthColumns.LOGIN);
            h.mPassword = json.optString(HostAuthColumns.PASSWORD);
            h.mDomain = json.optString(HostAuthColumns.DOMAIN);
            h.mClientCertAlias = json.optString(HostAuthColumns.CLIENT_CERT_ALIAS);
            final JSONObject credJson = json.optJSONObject(JSON_TAG_CREDENTIAL);
            if (credJson != null) {
                h.mCredential = Credential.fromJson(credJson);
            }
            return h;
        } catch (final JSONException e) {
            LogUtils.d(LogUtils.TAG, e, "Exception while deserializing HostAuth");
        }
        return null;
    }

    /**
     * Ensure that all optionally-loaded fields are populated from the provider.
     * @param context for provider loads
     */
    public void ensureLoaded(final Context context) {
        getCredential(context);
    }

    /**
     * Sets the user name and password from URI user info string
     */
    public void setLogin(String userInfo) {
        String userName = null;
        String userPassword = null;
        if (!TextUtils.isEmpty(userInfo)) {
            String[] userInfoParts = userInfo.split(":", 2);
            userName = userInfoParts[0];
            if (userInfoParts.length > 1) {
                userPassword = userInfoParts[1];
            }
        }
        setLogin(userName, userPassword);
    }

    public void setUserName(final String userName) {
        mLogin = userName;
        if (TextUtils.isEmpty(mLogin)) {
            mFlags &= ~FLAG_AUTHENTICATE;
        } else {
            mFlags |= FLAG_AUTHENTICATE;
        }
    }

    /**
     * Sets the user name and password
     */
    public void setLogin(String userName, String userPassword) {
        mLogin = userName;
        mPassword = userPassword;

        if (TextUtils.isEmpty(mLogin)) {
            mFlags &= ~FLAG_AUTHENTICATE;
        } else {
            mFlags |= FLAG_AUTHENTICATE;
        }
    }

    /**
     * Returns the login information. [0] is the username and [1] is the password.
     */
    public String[] getLogin() {
        String trimUser = (mLogin != null) ? mLogin.trim() : null;
        return new String[] { trimUser, mPassword };
    }

    public void setConnection(String protocol, String address, int port, int flags) {
        setConnection(protocol, address, port, flags, null);
    }

    /**
     * Sets the internal connection parameters based on the specified parameter values.
     * @param protocol the mail protocol to use (e.g. "eas", "imap").
     * @param address the address of the server
     * @param port the port for the connection
     * @param flags flags indicating the security and type of the connection
     * @param clientCertAlias an optional alias to use if a client user certificate is to be
     *     presented during connection establishment. If this is non-empty, it must be the case
     *     that flags indicates use of a secure connection
     */
    public void setConnection(String protocol, String address,
            int port, int flags, String clientCertAlias) {
        // Set protocol, security, and additional flags based on uri scheme
        mProtocol = protocol;

        mFlags &= ~(FLAG_SSL | FLAG_TLS | FLAG_TRUST_ALL);
        mFlags |= (flags & USER_CONFIG_MASK);

        boolean useSecureConnection = (flags & (FLAG_SSL | FLAG_TLS)) != 0;
        if (!useSecureConnection && !TextUtils.isEmpty(clientCertAlias)) {
            throw new IllegalArgumentException("Can't use client alias on non-secure connections");
        }

        mAddress = address;
        mPort = port;
        if (mPort == PORT_UNKNOWN) {
            boolean useSSL = ((mFlags & FLAG_SSL) != 0);
            if (LEGACY_SCHEME_SMTP.equals(mProtocol)) {
                mPort = useSSL ? 465 : 587;
            }
        }

        mClientCertAlias = clientCertAlias;
    }


    /** Convenience method to determine if SSL is used. */
    public boolean shouldUseSsl() {
        return (mFlags & FLAG_SSL) != 0;
    }

    /** Convenience method to determine if all server certs should be used. */
    public boolean shouldTrustAllServerCerts() {
        return (mFlags & FLAG_TRUST_ALL) != 0;
    }

    /**
     * Supports Parcelable
     */
    @Override
    public int describeContents() {
        return 0;
    }

    /**
     * Supports Parcelable
     */
    public static final Parcelable.Creator<HostAuth> CREATOR
            = new Parcelable.Creator<HostAuth>() {
        @Override
        public HostAuth createFromParcel(Parcel in) {
            return new HostAuth(in);
        }

        @Override
        public HostAuth[] newArray(int size) {
            return new HostAuth[size];
        }
    };

    /**
     * Supports Parcelable
     */
    @Override
    public void writeToParcel(Parcel dest, int flags) {
        // mBaseUri is not parceled
        dest.writeLong(mId);
        dest.writeString(mProtocol);
        dest.writeString(mAddress);
        dest.writeInt(mPort);
        dest.writeInt(mFlags);
        dest.writeString(mLogin);
        dest.writeString(mPassword);
        dest.writeString(mDomain);
        dest.writeString(mClientCertAlias);
        if ((mFlags & FLAG_OAUTH) != 0) {
            // TODO: This is nasty, but to be compatible with backward Exchange, we can't make any
            // change to the parcelable format. But we need Credential objects to be here.
            // So... only parcel or unparcel Credentials if the OAUTH flag is set. This will never
            // be set on HostAuth going to or coming from Exchange.
            dest.writeLong(mCredentialKey);
            if (mCredential == null) {
                Credential.EMPTY.writeToParcel(dest, flags);
            } else {
                mCredential.writeToParcel(dest, flags);
            }
        }
    }

    /**
     * Supports Parcelable
     */
    public HostAuth(Parcel in) {
        mBaseUri = CONTENT_URI;
        mId = in.readLong();
        mProtocol = in.readString();
        mAddress = in.readString();
        mPort = in.readInt();
        mFlags = in.readInt();
        mLogin = in.readString();
        mPassword = in.readString();
        mDomain = in.readString();
        mClientCertAlias = in.readString();
        if ((mFlags & FLAG_OAUTH) != 0) {
            // TODO: This is nasty, but to be compatible with backward Exchange, we can't make any
            // change to the parcelable format. But we need Credential objects to be here.
            // So... only parcel or unparcel Credentials if the OAUTH flag is set. This will never
            // be set on HostAuth going to or coming from Exchange.
            mCredentialKey = in.readLong();
            mCredential = new Credential(in);
            if (mCredential.equals(Credential.EMPTY)) {
                mCredential = null;
            }
        } else {
            mCredentialKey = -1;
        }
    }

    @Override
    public boolean equals(Object o) {
        if (!(o instanceof HostAuth)) {
            return false;
        }
        HostAuth that = (HostAuth)o;
        return mPort == that.mPort
                && mId == that.mId
                && mFlags == that.mFlags
                && TextUtils.equals(mProtocol, that.mProtocol)
                && TextUtils.equals(mAddress, that.mAddress)
                && TextUtils.equals(mLogin, that.mLogin)
                && TextUtils.equals(mPassword, that.mPassword)
                && TextUtils.equals(mDomain, that.mDomain)
                && TextUtils.equals(mClientCertAlias, that.mClientCertAlias);
                // We don't care about the server certificate for equals
    }

    /**
     * The flag, password, and client cert alias are the only items likely to change after a
     * HostAuth is created
     */
    @Override
    public int hashCode() {
        int hashCode = 29;
        if (mPassword != null) {
            hashCode += mPassword.hashCode();
        }
        if (mClientCertAlias != null) {
            hashCode += (mClientCertAlias.hashCode() << 8);
        }
        return (hashCode << 8) + mFlags;
    }

    /**
     * Legacy URI parser. Used in parsing template from provider.xml
     * Example string:
     *   "eas+ssl+trustallcerts://user:password@server/domain:123"
     *
     * Note that the use of client certificate is specified in the URI, a secure connection type
     * must be used.
     */
    public void setHostAuthFromString(String uriString)
            throws URISyntaxException {
        URI uri = new URI(uriString);
        String path = uri.getPath();
        String domain = null;
        if (!TextUtils.isEmpty(path)) {
            // Strip off the leading slash that begins the path.
            domain = path.substring(1);
        }
        mDomain = domain;
        setLogin(uri.getUserInfo());

        String scheme = uri.getScheme();
        setConnection(scheme, uri.getHost(), uri.getPort());
    }

    /**
     * Legacy code for setting connection values from a "scheme" (see above)
     */
    public void setConnection(String scheme, String host, int port) {
        String[] schemeParts = scheme.split("\\+");
        String protocol = schemeParts[0];
        String clientCertAlias = null;
        int flags = getSchemeFlags(scheme);

        // Example scheme: "eas+ssl+trustallcerts" or "eas+tls+trustallcerts+client-cert-alias"
        if (schemeParts.length > 3) {
            clientCertAlias = schemeParts[3];
        } else if (schemeParts.length > 2) {
            if (!SCHEME_TRUST_ALL_CERTS.equals(schemeParts[2])) {
                mClientCertAlias = schemeParts[2];
            }
        }

        setConnection(protocol, host, port, flags, clientCertAlias);
    }

    @Override
    public String toString() {
        return "[protocol " + mProtocol + "]";
    }
}