aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/java/org/pacien/tincapp/utils/TincKeyring.kt
diff options
context:
space:
mode:
Diffstat (limited to 'app/src/main/java/org/pacien/tincapp/utils/TincKeyring.kt')
-rw-r--r--app/src/main/java/org/pacien/tincapp/utils/TincKeyring.kt32
1 files changed, 23 insertions, 9 deletions
diff --git a/app/src/main/java/org/pacien/tincapp/utils/TincKeyring.kt b/app/src/main/java/org/pacien/tincapp/utils/TincKeyring.kt
index bae38ac..89bb246 100644
--- a/app/src/main/java/org/pacien/tincapp/utils/TincKeyring.kt
+++ b/app/src/main/java/org/pacien/tincapp/utils/TincKeyring.kt
@@ -1,6 +1,6 @@
1/* 1/*
2 * Tinc App, an Android binding and user interface for the tinc mesh VPN daemon 2 * Tinc App, an Android binding and user interface for the tinc mesh VPN daemon
3 * Copyright (C) 2017-2018 Pacien TRAN-GIRARD 3 * Copyright (C) 2017-2020 Pacien TRAN-GIRARD
4 * 4 *
5 * This program is free software: you can redistribute it and/or modify 5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by 6 * it under the terms of the GNU General Public License as published by
@@ -18,10 +18,11 @@
18 18
19package org.pacien.tincapp.utils 19package org.pacien.tincapp.utils
20 20
21import android.os.ParcelFileDescriptor
22import org.pacien.tincapp.commands.TincApp 21import org.pacien.tincapp.commands.TincApp
22import org.pacien.tincapp.context.AppPaths
23import java.io.File 23import java.io.File
24import java.io.FileNotFoundException 24import java.io.FileNotFoundException
25import java.io.FileWriter
25 26
26/** 27/**
27 * @author pacien 28 * @author pacien
@@ -33,12 +34,25 @@ object TincKeyring {
33 false 34 false
34 } 35 }
35 36
36 fun openPrivateKey(f: File?, passphrase: String?): ParcelFileDescriptor? { 37 fun unlockKey(target: String, input: File?, passphrase: String?): File? {
37 if (f == null || !f.exists() || passphrase == null) return null 38 if (input == null || !input.exists() || passphrase == null) return null
38 val pipe = ParcelFileDescriptor.createPipe() 39 val decryptedKey = PemUtils.decrypt(PemUtils.read(input), passphrase)
39 val decryptedKey = PemUtils.decrypt(PemUtils.read(f), passphrase) 40 val decryptedFile = tempKey(target)
40 val outputStream = ParcelFileDescriptor.AutoCloseOutputStream(pipe[1]) 41 PemUtils.write(decryptedKey, FileWriter(decryptedFile, false))
41 PemUtils.write(decryptedKey, outputStream.writer()) 42 return decryptedFile
42 return pipe[0] 43 }
44
45 private fun tempKey(name: String): File {
46 val file = File(AppPaths.internalCacheDir(), name)
47 file.createNewFile()
48 file.deleteOnExit()
49 file.makePrivate()
50 return file
51 }
52
53 private fun File.makePrivate() {
54 this.setExecutable(false, false)
55 this.setReadable(true, true)
56 this.setWritable(true, true)
43 } 57 }
44} 58}