/*
 * Copyright (C) 2012 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 org.apache.harmony.xnet.provider.jsse;

import java.security.cert.X509Certificate;
import java.util.List;
import libcore.io.EventLogger;

public class PinFailureLogger {

    private static final long LOG_INTERVAL_NANOS = 1000 * 1000 * 1000 * 60 * 60;

    private static long lastLoggedNanos = 0;

    public static synchronized void log(String cn, boolean chainContainsUserCert,
                                        boolean pinIsEnforcing,
                                        List<X509Certificate> chain) {
        // if we've logged recently, don't do it again
        if (!timeToLog()) {
            return;
        }
        // otherwise, log the event
        writeToLog(cn, chainContainsUserCert, pinIsEnforcing, chain);
        // update the last logged time
        lastLoggedNanos = System.nanoTime();
    }

    protected static synchronized void writeToLog(String cn, boolean chainContainsUserCert,
                                                  boolean pinIsEnforcing,
                                                  List<X509Certificate> chain) {
        Object[] values = new Object[chain.size() + 3];
        values[0] = cn;
        values[1] = chainContainsUserCert;
        values[2] = pinIsEnforcing;
        for (int i=0; i < chain.size(); i++) {
            values[i+3] = chain.get(i).toString();
        }
        EventLogger.writeEvent(90100, values);
    }

    protected static boolean timeToLog() {
        long currentTimeNanos = System.nanoTime();
        return ((currentTimeNanos - lastLoggedNanos) > LOG_INTERVAL_NANOS);
    }
}


@@ -662,16 +662,72 @@
 .jsse;%0A%0A
+import java.security.cert.CertificateEncodingException;%0A
 import j
@@ -802,27 +802,49 @@
 core.io.
-EventLogger
+Base64;%0Aimport libcore.io.DropBox
 ;%0A%0Apubli
@@ -1785,78 +1785,69 @@
     
-Object%5B%5D values = new Object%5Bchain.size() + 3%5D;%0A        values%5B0%5D = 
+StringBuilder sb = new StringBuilder();%0A        sb.append(
 cn
+)
 ;%0A  
@@ -1856,20 +1856,42 @@
     
-values%5B1%5D = 
+sb.append(%22%7C%22);%0A        sb.append(
 chai
@@ -1907,16 +1907,17 @@
 UserCert
+)
 ;%0A      
@@ -1922,188 +1922,437 @@
     
-values%5B2%5D = pinIsEnforcing;%0A        for (int i=0; i %3C chain.size(); i++) %7B%0A            values%5Bi+3%5D = chain.get(i).toString();%0A        %7D%0A        EventLogger.writeEvent(90100, values
+sb.append(%22%7C%22);%0A        sb.append(pinIsEnforcing);%0A        sb.append(%22%7C%22);%0A        for (X509Certificate cert : chain) %7B%0A            try %7B%0A                sb.append(Base64.encode(cert.getEncoded()));%0A            %7D catch (CertificateEncodingException e) %7B%0A                sb.append(%22Error: could not encode certificate%22);%0A            %7D%0A            sb.append(%22%7C%22);%0A        %7D%0A        DropBox.addText(%22cert_pin_failure%22, sb.toString()
 );%0A 

Migrate PinFailureLogger to use the DropBox.