Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit ec33076b authored by Ricki Hirner's avatar Ricki Hirner
Browse files

Initial transformation to Kotlin

parent 9033f39d
Loading
Loading
Loading
Loading
+8 −8
Original line number Diff line number Diff line

buildscript {
    ext.kotlin_version = '1.1.2-4'

    repositories {
        jcenter()
    }

    dependencies {
        classpath 'com.android.tools.build:gradle:2.3.0'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }
}

allprojects {
repositories {
    jcenter()
        maven {
            url 'http://oss.sonatype.org/content/repositories/snapshots'
        }
    mavenCentral()
}
}


apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'

android {
    compileSdkVersion 25
@@ -41,6 +40,7 @@ android {
}

dependencies {
    compile "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
    compile 'com.android.support:appcompat-v7:25.+'
    compile 'com.android.support:cardview-v7:25.+'

+1 −2
Original line number Diff line number Diff line
@@ -101,7 +101,7 @@ public class CustomCertManagerTest {
        paranoidCertManager.checkServerTrusted(siteCerts, "RSA");
    }

    /* fails randomly (in about 1 of 3 cases) for unknown reason:
    // fails randomly (in about 1 of 3 cases) for unknown reason:
    @Test(expected = CertificateException.class)
    public void testRemoveCustomCertificate() throws CertificateException, TimeoutException, InterruptedException {
        addCustomCertificate();
@@ -115,7 +115,6 @@ public class CustomCertManagerTest {
        startService(intent, CustomCertService.class);
        paranoidCertManager.checkServerTrusted(siteCerts, "RSA");
    }
    */

    private void addCustomCertificate() throws TimeoutException, InterruptedException {
        // add certificate and check again
+0 −48
Original line number Diff line number Diff line
/*
 * Copyright © Ricki Hirner (bitfire web engineering).
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Public License v3.0
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/gpl.html
 */

package at.bitfire.cert4android;

import android.support.annotation.NonNull;
import android.support.annotation.Nullable;

import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.X509Certificate;
import java.util.logging.Level;

import javax.net.ssl.TrustManager;
import javax.net.ssl.TrustManagerFactory;
import javax.net.ssl.X509TrustManager;

public class CertUtils {

    @Nullable
    public static X509TrustManager getTrustManager(@Nullable KeyStore keyStore) {
        try {
            TrustManagerFactory tmf = TrustManagerFactory.getInstance("X509");
            tmf.init(keyStore);
            for (TrustManager trustManager : tmf.getTrustManagers())
                if (trustManager instanceof X509TrustManager)
                    return (X509TrustManager)trustManager;
        } catch(NoSuchAlgorithmException|KeyStoreException e) {
            Constants.log.log(Level.SEVERE, "Couldn't initialize trust manager", e);
        }
        return null;
    }

    @NonNull
    public static String getTag(@NonNull X509Certificate cert) {
        StringBuilder sb = new StringBuilder();
        for (byte b: cert.getSignature())
            sb.append(String.format("%02x", b & 0xFF));
        return sb.toString();
    }

}
+44 −0
Original line number Diff line number Diff line
/*
 * Copyright © Ricki Hirner (bitfire web engineering).
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Public License v3.0
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/gpl.html
 */

package at.bitfire.cert4android

import java.security.GeneralSecurityException
import java.security.KeyStore
import java.security.cert.X509Certificate
import java.util.logging.Level
import javax.net.ssl.TrustManagerFactory
import javax.net.ssl.X509TrustManager

class CertUtils {
    companion object {

        @JvmStatic
        fun getTrustManager(keyStore: KeyStore?): X509TrustManager? {
            try {
                val tmf = TrustManagerFactory.getInstance("X509")
                tmf.init(keyStore)
                for (trustManager in tmf.trustManagers)
                    if (trustManager is X509TrustManager)
                        return trustManager
            } catch(e: GeneralSecurityException) {
                Constants.log.log(Level.SEVERE, "Couldn't initialize trust manager", e)
            }
            return null;
        }

        @JvmStatic
        fun getTag(cert: X509Certificate): String {
            val str = StringBuilder()
            for (b in cert.signature)
                str.append(String.format("%02x", b))
            return str.toString()
        }

    }
}
+9 −5
Original line number Diff line number Diff line
@@ -6,14 +6,18 @@
 * http://www.gnu.org/licenses/gpl.html
 */

package at.bitfire.cert4android;
package at.bitfire.cert4android

import java.util.logging.Logger;
import java.util.logging.Logger

public class Constants {
class Constants {
    companion object {

    public static Logger log = Logger.getLogger("cert4android");
        @JvmField
        var log: Logger = Logger.getLogger("cert4android")

    public static int NOTIFICATION_CERT_DECISION = 88809;
        @JvmField
        val NOTIFICATION_CERT_DECISION = 88809

    }
}
Loading