Welcome to mirror list, hosted at ThFree Co, Russian Federation.

CertUtils.kt « cert4android « bitfire « at « java « main « src - github.com/bitfireAT/cert4android.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 35b7e0114bd84be3ece969d050a708dafee77419 (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
/*
 * 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.MessageDigest
import java.security.cert.X509Certificate
import java.util.*
import java.util.logging.Level
import javax.net.ssl.TrustManagerFactory
import javax.net.ssl.X509TrustManager

object CertUtils {

    fun fingerprint(cert: X509Certificate, algorithm: String): String {
        val md = MessageDigest.getInstance(algorithm)
        return hexString(md.digest(cert.encoded))
    }

    fun getTrustManager(keyStore: KeyStore?): X509TrustManager? {
        try {
            val tmf = TrustManagerFactory.getInstance("X509")
            tmf.init(keyStore)
            tmf.trustManagers
                    .filterIsInstance<X509TrustManager>()
                    .forEach { return it }
        } catch(e: GeneralSecurityException) {
            Constants.log.log(Level.SEVERE, "Couldn't initialize trust manager", e)
        }
        return null
    }

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

    fun hexString(data: ByteArray): String {
        val str = StringBuilder()
        for ((idx, b) in data.withIndex()) {
            if (idx != 0)
                str.append(':')
            str.append(String.format("%02x", b).toUpperCase())
        }
        return str.toString()
    }

}