/*
 * CustomTrustManager
 * Connect SDK
 *
 * Copyright (c) 2014 LG Electronics.
 * Created by Sudeep Mukherjee on 13 Dec 2018
 *
 * 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.reactlibrary;

import android.util.Log;

import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.Arrays;

import javax.net.ssl.X509TrustManager;


public class CustomTrustManager implements X509TrustManager {
    X509Certificate expectedCert;
    X509Certificate lastCheckedCert;

    public void setExpectedCertificate(X509Certificate cert) {
        //Log.d(Util.T, "WebOSTVTrustManager X509Certificate setExpectedCertificate: "+ cert);
        this.expectedCert = cert;
    }

    public X509Certificate getLastCheckedCertificate () {
        //Log.d(Util.T, "WebOSTVTrustManager X509Certificate getLastCheckedCertificate: "+ lastCheckedCert);
        return lastCheckedCert;
    }

    @Override
    public void checkClientTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {
        // TODO Auto-generated method stub
        Log.d("SPD", "Expecting checkClientTrusted device cert " + (expectedCert != null ? expectedCert.getSubjectDN() : "(any)"));
    }

    @Override
    public void checkServerTrusted(X509Certificate[] chain, String arg1) throws CertificateException {
        // TODO Auto-generated method stub
//        Log.d("SPD", "finally certificate "+ chain[0].getPublicKey().toString());
//        Log.d("SPD", "finally getting certificate "+ chain[0].toString());
//        Log.d("SPD", "Expecting checkServerTrusted device cert " + (expectedCert != null ? expectedCert.getSubjectDN() : "(any)"));
        if (chain != null && chain.length > 0) {
            X509Certificate cert = chain[0];

            lastCheckedCert = cert;

            if (expectedCert != null) {
                byte [] certBytes = cert.getEncoded();
                byte [] expectedCertBytes = expectedCert.getEncoded();

                Log.d("SPD", "Device presented cert " + cert.getSubjectDN());

                if (!Arrays.equals(certBytes, expectedCertBytes)) {
                    throw new CertificateException("certificate does not match");
                }
            }
        } else {
            lastCheckedCert = null;
            throw new CertificateException("no server certificate");
        }
    }

    @Override
    public X509Certificate[] getAcceptedIssuers() {
        // TODO Auto-generated method stub
        Log.d("SPD", "getAcceptedIssuers ");
        return new X509Certificate[0];
    }
}
