aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/java/org/pacien/tincapp/utils/TincKeyring.kt
blob: 422763f219be8e4171fea0117f70cc8addc57e3c (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
package org.pacien.tincapp.utils

import android.os.ParcelFileDescriptor
import org.pacien.tincapp.commands.TincApp
import java.io.File
import java.io.FileNotFoundException

/**
 * @author pacien
 */
object TincKeyring {
  fun needsPassphrase(netName: String) = try {
    TincApp.listPrivateKeys(netName).filter { it.exists() }.any { PemUtils.isEncrypted(PemUtils.read(it)) }
  } catch (e: FileNotFoundException) {
    false
  }

  fun openPrivateKey(f: File?, passphrase: String?): ParcelFileDescriptor? {
    if (f == null || !f.exists() || passphrase == null) return null
    val pipe = ParcelFileDescriptor.createPipe()
    val decryptedKey = PemUtils.decrypt(PemUtils.read(f), passphrase)
    val outputStream = ParcelFileDescriptor.AutoCloseOutputStream(pipe[1])
    PemUtils.write(decryptedKey, outputStream.writer())
    return pipe[0]
  }
}