1/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.net.wifi.passpoint;
18
19import android.net.wifi.WifiEnterpriseConfig;
20import android.os.Parcelable;
21import android.os.Parcel;
22
23import java.util.Collection;
24import java.util.Iterator;
25import java.util.Map;
26import java.util.Set;
27
28
29/**
30 * A class representing a Wi-Fi Passpoint credential.
31 * @hide
32 */
33public class WifiPasspointCredential implements Parcelable {
34
35    private final static String TAG = "PasspointCredential";
36    private final static boolean DBG = true;
37
38    /** Wi-Fi nodes**/
39    private String mWifiSpFqdn;
40
41    /** PerProviderSubscription nodes **/
42    private String mCredentialName;
43
44    /** SubscriptionUpdate nodes **/
45    private String mSubscriptionUpdateInterval;
46    private String mSubscriptionUpdateMethod;
47    private String mSubscriptionUpdateRestriction;
48    private String mSubscriptionUpdateURI;
49    private String mSubscriptionUpdateUsername;
50    private String mSubscriptionUpdatePassword;
51
52    /** HomeSP nodes **/
53    private String mHomeSpFqdn;
54    private String mFriendlyName;
55    private Collection<WifiPasspointDmTree.HomeOIList> mHomeOIList;
56    private Collection<WifiPasspointDmTree.OtherHomePartners> mOtherHomePartnerList;
57
58    /** SubscriptionParameters nodes**/
59    private String mCreationDate;
60    private String mExpirationDate;
61
62    /** Credential nodes **/
63    private String mType;
64    private String mInnerMethod;
65    private String mCertType;
66    private String mCertSha256Fingerprint;
67    private String mUpdateIdentifier;
68    private String mUsername;
69    private String mPasswd;
70    private String mRealm;
71    private String mImsi;
72    private String mMcc;
73    private String mMnc;
74    private String mCaRootCert;
75    private String mClientCert;
76    private boolean mCheckAaaServerCertStatus;
77
78    /** Policy nodes **/
79    private String mPolicyUpdateUri;
80    private String mPolicyUpdateInterval;
81    private String mPolicyUpdateUsername;
82    private String mPolicyUpdatePassword;
83    private String mPolicyUpdateRestriction;
84    private String mPolicyUpdateMethod;
85    private Collection<WifiPasspointDmTree.PreferredRoamingPartnerList> mPreferredRoamingPartnerList;
86    private Collection<WifiPasspointDmTree.MinBackhaulThresholdNetwork> mMinBackhaulThresholdNetwork;
87    private Collection<WifiPasspointDmTree.SPExclusionList> mSpExclusionList;
88    private Collection<WifiPasspointDmTree.RequiredProtoPortTuple> mRequiredProtoPortTuple;
89    private String mMaxBssLoad;
90
91    /** CrednetialPriority node **/
92    private int mCrednetialPriority;
93
94    /** AAAServerTrustRoot nodes **/
95    private String mAaaCertUrl;
96    private String mAaaSha256Fingerprint;
97
98    /** Others **/
99    private boolean mIsMachineRemediation;
100    private boolean mUserPreferred = false;
101    private String mWifiTreePath;
102    private WifiEnterpriseConfig mEnterpriseConfig;
103
104    /** @hide */
105    public WifiPasspointCredential() {}
106
107    /**
108     * Constructor
109     * @param realm Realm of the passpoint credential
110     * @param fqdn Fully qualified domain name (FQDN) of the credential
111     * @param config Enterprise config, must be either EAP-TLS or EAP-TTLS
112     * @see WifiEnterpriseConfig
113     */
114    public WifiPasspointCredential(String realm, String fqdn, WifiEnterpriseConfig config) {
115        mRealm = realm;
116        switch (config.getEapMethod()) {
117            case WifiEnterpriseConfig.Eap.TLS:
118            case WifiEnterpriseConfig.Eap.TTLS:
119                mEnterpriseConfig = new WifiEnterpriseConfig(config);
120                break;
121            default:
122                // ignore
123        }
124    }
125
126    /** @hide */
127    public WifiPasspointCredential(String type,
128            String caroot,
129            String clientcert,
130            String mcc,
131            String mnc,
132            WifiPasspointDmTree.SpFqdn sp,
133            WifiPasspointDmTree.CredentialInfo credinfo) {
134
135        if (credinfo == null) {
136            return;
137        }
138
139        mType = type;
140        mCaRootCert = caroot;
141        mClientCert = clientcert;
142
143        mWifiSpFqdn = sp.nodeName;
144        mUpdateIdentifier = sp.perProviderSubscription.UpdateIdentifier;
145
146        mCredentialName = credinfo.nodeName;
147        mOtherHomePartnerList = credinfo.homeSP.otherHomePartners.values();
148
149        Set set = credinfo.aAAServerTrustRoot.entrySet();
150        Iterator i = set.iterator();
151        if (i.hasNext()) {
152            Map.Entry entry3 = (Map.Entry) i.next();
153            WifiPasspointDmTree.AAAServerTrustRoot aaa = (WifiPasspointDmTree.AAAServerTrustRoot) entry3.getValue();
154            mAaaCertUrl = aaa.CertURL;
155            mAaaSha256Fingerprint = aaa.CertSHA256Fingerprint;
156        }
157
158        mCertType = credinfo.credential.digitalCertificate.CertificateType;
159        mCertSha256Fingerprint = credinfo.credential.digitalCertificate.CertSHA256Fingerprint;
160        mUsername = credinfo.credential.usernamePassword.Username;
161        mPasswd = credinfo.credential.usernamePassword.Password;
162        mIsMachineRemediation = credinfo.credential.usernamePassword.MachineManaged;
163        mInnerMethod = credinfo.credential.usernamePassword.eAPMethod.InnerMethod;
164        mImsi = credinfo.credential.sim.IMSI;
165        mMcc = mcc;
166        mMnc = mnc;
167        mCreationDate = credinfo.credential.CreationDate;
168        mExpirationDate = credinfo.credential.ExpirationDate;
169        mRealm = credinfo.credential.Realm;
170
171        if (credinfo.credentialPriority == null) {
172            mCrednetialPriority = 128;
173        } else {
174            mCrednetialPriority = Integer.parseInt(credinfo.credentialPriority);
175        }
176
177        mHomeSpFqdn = credinfo.homeSP.FQDN;
178
179        mSubscriptionUpdateInterval = credinfo.subscriptionUpdate.UpdateInterval;
180        mSubscriptionUpdateMethod = credinfo.subscriptionUpdate.UpdateMethod;
181        mSubscriptionUpdateRestriction = credinfo.subscriptionUpdate.Restriction;
182        mSubscriptionUpdateURI = credinfo.subscriptionUpdate.URI;
183        mSubscriptionUpdateUsername = credinfo.subscriptionUpdate.usernamePassword.Username;
184        mSubscriptionUpdatePassword = credinfo.subscriptionUpdate.usernamePassword.Password;
185
186        mPolicyUpdateUri = credinfo.policy.policyUpdate.URI;
187        mPolicyUpdateInterval = credinfo.policy.policyUpdate.UpdateInterval;
188        mPolicyUpdateUsername = credinfo.policy.policyUpdate.usernamePassword.Username;
189        mPolicyUpdatePassword = credinfo.policy.policyUpdate.usernamePassword.Password;
190        mPolicyUpdateRestriction = credinfo.policy.policyUpdate.Restriction;
191        mPolicyUpdateMethod = credinfo.policy.policyUpdate.UpdateMethod;
192        mPreferredRoamingPartnerList = credinfo.policy.preferredRoamingPartnerList.values();
193        mMinBackhaulThresholdNetwork = credinfo.policy.minBackhaulThreshold.values();
194        mRequiredProtoPortTuple = credinfo.policy.requiredProtoPortTuple.values();
195        mMaxBssLoad = credinfo.policy.maximumBSSLoadValue;
196        mSpExclusionList = credinfo.policy.sPExclusionList.values();
197
198        mHomeOIList = credinfo.homeSP.homeOIList.values();
199        mFriendlyName = credinfo.homeSP.FriendlyName;
200        mCheckAaaServerCertStatus = credinfo.credential.CheckAAAServerCertStatus;
201    }
202
203    /** @hide */
204    public String getUpdateIdentifier() {
205        return mUpdateIdentifier;
206    }
207
208    /** @hide */
209    public String getUpdateMethod() {
210        return mSubscriptionUpdateMethod;
211    }
212
213    /** @hide */
214    public void setUpdateMethod(String method) {
215        mSubscriptionUpdateMethod = method;
216    }
217
218    /** @hide */
219    public String getWifiSpFqdn() {
220        return mWifiSpFqdn;
221    }
222
223    /** @hide */
224    public String getCredName() {
225        return mCredentialName;
226    }
227
228    /** @hide */
229    public String getType() {
230        return mType;
231    }
232
233    /**
234     * Get enterprise config of this Passpoint credential.
235     * @return Enterprise config
236     * @see WifiEnterpriseConfig
237     */
238    public WifiEnterpriseConfig getEnterpriseConfig() {
239        return new WifiEnterpriseConfig(mEnterpriseConfig);
240    }
241
242    /**
243     * Set enterprise config of this Passpoint credential.
244     * @param config Enterprise config, must be either EAP-TLS or EAP-TTLS
245     * @see WifiEnterpriseConfig
246     */
247    public void setEnterpriseConfig(WifiEnterpriseConfig config) {
248        // TODO
249    }
250
251    /** @hide */
252    public String getCertType() {
253        return mCertType;
254    }
255
256    /** @hide */
257    public String getCertSha256Fingerprint() {
258        return mCertSha256Fingerprint;
259    }
260
261    /** @hide */
262    public String getUserName() {
263        return mUsername;
264    }
265
266    /** @hide */
267    public String getPassword() {
268        // TODO: guarded by connectivity internal
269        return mPasswd;
270    }
271
272    /** @hide */
273    public String getImsi() {
274        return mImsi;
275    }
276
277    /** @hide */
278    public String getMcc() {
279        return mMcc;
280    }
281
282    /** @hide */
283    public String getMnc() {
284        return mMnc;
285    }
286
287    /** @hide */
288    public String getCaRootCertPath() {
289        return mCaRootCert;
290    }
291
292    /** @hide */
293    public String getClientCertPath() {
294        return mClientCert;
295    }
296
297    /**
298     * Get the realm of this Passpoint credential.
299     * @return Realm
300     */
301    public String getRealm() {
302        return mRealm;
303    }
304
305    /**
306     * Set the ream of this Passpoint credential.
307     * @param realm Realm
308     */
309    public void setRealm(String realm) {
310        mRealm = realm;
311    }
312
313    /** @hide */
314    public int getPriority() {
315        if (mUserPreferred) {
316            return 0;
317        }
318
319        return mCrednetialPriority;
320    }
321
322    /**
323     * Get the fully qualified domain name (FQDN) of this Passpoint credential.
324     * @return FQDN
325     */
326    public String getHomeSpFqdn() {
327        return mHomeSpFqdn;
328    }
329
330    /**
331     * Set the fully qualified domain name (FQDN) of this Passpoint credential.
332     * @param fqdn FQDN
333     */
334    public void setHomeFqdn(String fqdn) {
335        mHomeSpFqdn = fqdn;
336    }
337
338
339    /** @hide */
340    public Collection<WifiPasspointDmTree.OtherHomePartners> getOtherHomePartnerList() {
341        return mOtherHomePartnerList;
342    }
343
344    /** @hide */
345    public String getSubscriptionUpdateUsername() {
346        return mSubscriptionUpdateUsername;
347    }
348
349    /** @hide */
350    public String getSubscriptionUpdatePassword() {
351        return mSubscriptionUpdatePassword;
352    }
353
354    /** @hide */
355    public String getPolicyUpdateUri() {
356        return mPolicyUpdateUri;
357    }
358
359    /** @hide */
360    public String getPolicyUpdateInterval() {
361        return mPolicyUpdateInterval;
362    }
363
364    /** @hide */
365    public String getPolicyUpdateUsername() {
366        return mPolicyUpdateUsername;
367    }
368
369    /** @hide */
370    public String getPolicyUpdatePassword() {
371        return mPolicyUpdatePassword;
372    }
373
374    /** @hide */
375    public String getPolicyUpdateRestriction() {
376        return mPolicyUpdateRestriction;
377    }
378
379    /** @hide */
380    public String getPolicyUpdateMethod() {
381        return mPolicyUpdateMethod;
382    }
383
384    /** @hide */
385    public String getCreationDate() {
386        return mCreationDate;
387    }
388
389    /** @hide */
390    public String getExpirationDate() {
391        return mExpirationDate;
392    }
393
394    /** @hide */
395    public void setExpirationDate(String expirationdate) {
396        mExpirationDate = expirationdate;
397    }
398
399    /** @hide */
400    public Collection<WifiPasspointDmTree.PreferredRoamingPartnerList> getPreferredRoamingPartnerList() {
401        return mPreferredRoamingPartnerList;
402    }
403
404    /** @hide */
405    public Collection<WifiPasspointDmTree.HomeOIList> getHomeOiList() {
406        return mHomeOIList;
407    }
408
409    /** @hide */
410    public Collection<WifiPasspointDmTree.MinBackhaulThresholdNetwork> getBackhaulThresholdList() {
411        return mMinBackhaulThresholdNetwork;
412    }
413
414    /** @hide */
415    public Collection<WifiPasspointDmTree.RequiredProtoPortTuple> getRequiredProtoPortList() {
416        return mRequiredProtoPortTuple;
417    }
418
419    /** @hide */
420    public Collection<WifiPasspointDmTree.SPExclusionList> getSPExclusionList() {
421        return mSpExclusionList;
422    }
423
424    /** @hide */
425    public boolean getIsMachineRemediation() {
426        return mIsMachineRemediation;
427    }
428
429    /** @hide */
430    public String getAaaCertUrl() {
431        return mAaaCertUrl;
432    }
433
434    /** @hide */
435    public String getAaaSha256Fingerprint() {
436        return mAaaSha256Fingerprint;
437    }
438
439    /** @hide */
440    public String getSubscriptionUpdateRestriction() {
441        return mSubscriptionUpdateRestriction;
442    }
443
444    /** @hide */
445    public String getSubscriptionUpdateURI() {
446        return mSubscriptionUpdateURI;
447    }
448
449    /** @hide */
450    public String getSubscriptionUpdateInterval() {
451        return mSubscriptionUpdateInterval;
452    }
453
454    /** @hide */
455    public String getFriendlyName() {
456        return mFriendlyName;
457    }
458
459    /** @hide */
460    public String getMaxBssLoad() {
461        return mMaxBssLoad;
462    }
463
464    /** @hide */
465    public boolean getUserPreference() {
466        return mUserPreferred;
467    }
468
469    /** @hide */
470    public boolean getCheckAaaServerCertStatus() {
471        return mCheckAaaServerCertStatus;
472    }
473
474    /** @hide */
475    public void setUserPreference(boolean value) {
476        mUserPreferred = value;
477    }
478
479    @Override
480    /** @hide */
481    public boolean equals(Object obj) {
482        boolean result = false;
483        if (obj instanceof WifiPasspointCredential) {
484            final WifiPasspointCredential other = (WifiPasspointCredential) obj;
485            if (this.mType.equals(other.mType)) {
486                if (this.mType.equals("TTLS")) {
487                    result = this.mUsername.equals(other.mUsername) &&
488                            this.mPasswd.equals(other.mPasswd) &&
489                            this.mRealm.equals(other.mRealm) &&
490                            this.mHomeSpFqdn.equals(other.mHomeSpFqdn);
491                }
492                if (this.mType.equals("TLS")) {
493                    result = this.mRealm.equals(other.mRealm) &&
494                            this.mHomeSpFqdn.equals(other.mHomeSpFqdn) &&
495                            this.mClientCert.equals(other.mClientCert);
496                }
497                if (this.mType.equals("SIM")) {
498                    result = this.mMcc.equals(other.mMcc) &&
499                            this.mMnc.equals(other.mMnc) &&
500                            this.mImsi.equals(other.mImsi) &&
501                            this.mHomeSpFqdn.equals(other.mHomeSpFqdn);
502                }
503            }
504        }
505        return result;
506    }
507
508    @Override
509    /** @hide */
510    public String toString() {
511        StringBuffer sb = new StringBuffer();
512        String none = "<none>";
513
514        if (!DBG) {
515            sb.append(none);
516        } else {
517            sb.append(", UpdateIdentifier: ")
518            .append(mUpdateIdentifier == null ? none : mUpdateIdentifier)
519            .append(", SubscriptionUpdateMethod: ")
520            .append(mSubscriptionUpdateMethod == null ? none : mSubscriptionUpdateMethod)
521            .append(", Type: ").append(mType == null ? none : mType)
522            .append(", Username: ").append(mUsername == null ? none : mUsername)
523            .append(", Passwd: ").append(mPasswd == null ? none : mPasswd)
524            .append(", SubDMAccUsername: ")
525            .append(mSubscriptionUpdateUsername == null ? none : mSubscriptionUpdateUsername)
526            .append(", SubDMAccPassword: ")
527            .append(mSubscriptionUpdatePassword == null ? none : mSubscriptionUpdatePassword)
528            .append(", PolDMAccUsername: ")
529            .append(mPolicyUpdateUsername == null ? none : mPolicyUpdateUsername)
530            .append(", PolDMAccPassword: ")
531            .append(mPolicyUpdatePassword == null ? none : mPolicyUpdatePassword)
532            .append(", Imsi: ").append(mImsi == null ? none : mImsi)
533            .append(", Mcc: ").append(mMcc == null ? none : mMcc)
534            .append(", Mnc: ").append(mMnc == null ? none : mMnc)
535            .append(", CaRootCert: ").append(mCaRootCert == null ? none : mCaRootCert)
536            .append(", Realm: ").append(mRealm == null ? none : mRealm)
537            .append(", Priority: ").append(mCrednetialPriority)
538            .append(", Fqdn: ").append(mHomeSpFqdn == null ? none : mHomeSpFqdn)
539            .append(", Otherhomepartners: ")
540            .append(mOtherHomePartnerList == null ? none : mOtherHomePartnerList)
541            .append(", ExpirationDate: ")
542            .append(mExpirationDate == null ? none : mExpirationDate)
543            .append(", MaxBssLoad: ").append(mMaxBssLoad == null ? none : mMaxBssLoad)
544            .append(", SPExclusionList: ").append(mSpExclusionList);
545
546            if (mPreferredRoamingPartnerList != null) {
547                sb.append("PreferredRoamingPartnerList:");
548                for (WifiPasspointDmTree.PreferredRoamingPartnerList prpListItem : mPreferredRoamingPartnerList) {
549                    sb.append("[fqdnmatch:").append(prpListItem.FQDN_Match).
550                            append(", priority:").append(prpListItem.Priority).
551                            append(", country:").append(prpListItem.Country).append("]");
552                }
553            }
554
555            if (mHomeOIList != null) {
556                sb.append("HomeOIList:");
557                for (WifiPasspointDmTree.HomeOIList HomeOIListItem : mHomeOIList) {
558                    sb.append("[HomeOI:").append(HomeOIListItem.HomeOI).
559                            append(", HomeOIRequired:").append(HomeOIListItem.HomeOIRequired).
560                            append("]");
561                }
562            }
563
564            if (mMinBackhaulThresholdNetwork != null) {
565                sb.append("BackHaulThreshold:");
566                for (WifiPasspointDmTree.MinBackhaulThresholdNetwork BhtListItem : mMinBackhaulThresholdNetwork) {
567                    sb.append("[networkType:").append(BhtListItem.NetworkType).
568                            append(", dlBandwidth:").append(BhtListItem.DLBandwidth).
569                            append(", ulBandwidth:").append(BhtListItem.ULBandwidth).
570                            append("]");
571                }
572            }
573
574            if (mRequiredProtoPortTuple != null) {
575                sb.append("WifiMORequiredProtoPortTupleList:");
576                for (WifiPasspointDmTree.RequiredProtoPortTuple RpptListItem : mRequiredProtoPortTuple) {
577                    sb.append("[IPProtocol:").append(RpptListItem.IPProtocol).
578                            append(", PortNumber:").append(RpptListItem.PortNumber).
579                            append("]");
580                }
581            }
582        }
583        return sb.toString();
584    }
585
586    /** Implement the Parcelable interface {@hide} */
587    public int describeContents() {
588        return 0;
589    }
590
591    /** Implement the Parcelable interface {@hide} */
592    public void writeToParcel(Parcel dest, int flags) {
593        dest.writeString(mWifiSpFqdn);
594        dest.writeString(mCredentialName);
595        dest.writeString(mType);
596        dest.writeInt(mCrednetialPriority);
597        dest.writeString(mHomeSpFqdn);
598        dest.writeString(mRealm);
599    }
600
601    /** Implement the Parcelable interface {@hide} */
602    public void readFromParcel(Parcel in) {
603        mWifiSpFqdn = in.readString();
604        mCredentialName = in.readString();
605        mType = in.readString();
606        mCrednetialPriority = in.readInt();
607        mHomeSpFqdn = in.readString();
608        mRealm = in.readString();
609    }
610
611    /** Implement the Parcelable interface {@hide} */
612    public static final Creator<WifiPasspointCredential> CREATOR =
613            new Creator<WifiPasspointCredential>() {
614                public WifiPasspointCredential createFromParcel(Parcel in) {
615                    WifiPasspointCredential pc = new WifiPasspointCredential();
616                    pc.mWifiSpFqdn = in.readString();
617                    pc.mCredentialName = in.readString();
618                    pc.mType = in.readString();
619                    pc.mCrednetialPriority = in.readInt();
620                    pc.mHomeSpFqdn = in.readString();
621                    pc.mRealm = in.readString();
622                    return pc;
623                }
624
625                public WifiPasspointCredential[] newArray(int size) {
626                    return new WifiPasspointCredential[size];
627                }
628            };
629
630    /** @hide */
631    public int compareTo(WifiPasspointCredential another) {
632
633        //The smaller the higher
634        if (mCrednetialPriority < another.mCrednetialPriority) {
635            return -1;
636        } else if (mCrednetialPriority == another.mCrednetialPriority) {
637            return this.mType.compareTo(another.mType);
638        } else {
639            return 1;
640        }
641    }
642
643    @Override
644    /** @hide */
645    public int hashCode() {
646        int hash = 208;
647        if (mType != null) {
648            hash += mType.hashCode();
649        }
650        if (mRealm != null) {
651            hash += mRealm.hashCode();
652        }
653        if (mHomeSpFqdn != null) {
654            hash += mHomeSpFqdn.hashCode();
655        }
656        if (mUsername != null) {
657            hash += mUsername.hashCode();
658        }
659        if (mPasswd != null) {
660            hash += mPasswd.hashCode();
661        }
662
663        return hash;
664    }
665}
666