5 * (c) 2018 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of the Trivial IP Encryption (TrIPE) Android app.
12 * TrIPE is free software: you can redistribute it and/or modify it under
13 * the terms of the GNU General Public License as published by the Free
14 * Software Foundation; either version 3 of the License, or (at your
15 * option) any later version.
17 * TrIPE is distributed in the hope that it will be useful, but WITHOUT
18 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
19 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22 * You should have received a copy of the GNU General Public License
23 * along with TrIPE. If not, see <https://www.gnu.org/licenses/>.
26 package uk.org.distorted.tripe; package object keys {
28 /*----- Imports -----------------------------------------------------------*/
30 import scala.collection.mutable.HashMap;
32 import java.io.{Closeable, File};
33 import java.net.{URL, URLConnection};
34 import java.util.zip.GZIPInputStream;
36 import sys.{SystemError, hashsz, runCommand};
37 import sys.Errno.EEXIST;
38 import sys.FileImplicits._;
40 /*----- Useful regular expressions ----------------------------------------*/
42 private val RX_COMMENT = """(?x) ^ \s* (?: \# .* )? $""".r;
43 private val RX_KEYVAL = """(?x) ^ \s*
48 private val RX_DOLLARSUBST = """(?x) \$ \{ ([-\w]+) \}""".r;
50 /*----- Things that go wrong ----------------------------------------------*/
52 class ConfigSyntaxError(val file: String, val lno: Int, val msg: String)
54 override def getMessage(): String = s"$file:$lno: $msg";
57 class ConfigDefaultFailed(val file: String, val dfltkey: String,
58 val badkey: String, val badval: String)
60 override def getMessage(): String =
61 s"$file: can't default `$dfltkey' because " +
62 s"`$badval' is not a recognized value for `$badkey'";
65 class DefaultFailed(val key: String) extends Exception;
67 /*----- Parsing a configuration -------------------------------------------*/
69 type Config = scala.collection.Map[String, String];
71 private val DEFAULTS: Seq[(String, Config => String)] =
72 Seq("repos-base" -> { _ => "tripe-keys.tar.gz" },
73 "sig-base" -> { _ => "tripe-keys.sig-<SEQ>" },
74 "repos-url" -> { conf => conf("base-url") + conf("repos-base") },
75 "sig-url" -> { conf => conf("base-url") + conf("sig-base") },
76 "kx" -> { _ => "dh" },
77 "kx-genalg" -> { conf => conf("kx") match {
78 case alg@("dh" | "ec" | "x25519" | "x448") => alg
79 case _ => throw new DefaultFailed("kx")
81 "kx-expire" -> { _ => "now + 1 year" },
82 "kx-warn-days" -> { _ => "28" },
83 "bulk" -> { _ => "iiv" },
84 "cipher" -> { conf => conf("bulk") match {
85 case "naclbox" => "salsa20"
86 case _ => "rijndael-cbc"
88 "hash" -> { _ => "sha256" },
89 "mgf" -> { conf => conf("hash") + "-mgf" },
90 "mac" -> { conf => conf("bulk") match {
91 case "naclbox" => "poly1305/128"
95 case -1 => throw new DefaultFailed("hash")
96 case hsz => s"${h}-hmac/${4*hsz}"
99 "sig" -> { conf => conf("kx") match {
102 case "x25519" => "ed25519"
103 case "x448" => "ed448"
104 case _ => throw new DefaultFailed("kx")
106 "sig-fresh" -> { _ => "always" },
107 "fingerprint-hash" -> { _("hash") });
109 /*----- Managing a key repository -----------------------------------------*/
111 def downloadToFile(file: File, url: URL, maxlen: Long = Long.MaxValue) {
112 fetchURL(url, new URLFetchCallbacks {
113 val out = file.openForOutput();
114 private def toobig() {
115 throw new KeyConfigException(s"remote file `$url' is " +
116 "suspiciously large");
118 var totlen: Long = 0;
119 override def preflight(conn: URLConnection) {
120 totlen = conn.getContentLength;
121 if (totlen > maxlen) toobig();
123 override def done(win: Boolean) { out.close(); }
124 def write(buf: Array[Byte], n: Int, len: Long) {
125 if (len + n > maxlen) toobig();
126 out.write(buf, 0, n);
135 * insert config file via URL or something
137 * -> pending (pending/tripe-keys.conf)
139 * verify master key fingerprint (against barcode?)
141 * -> confirmed (live/tripe-keys.conf; no live/repos)
151 * -> updating (live/...; new/...)
153 * rename old repository aside
155 * -> committing (old/...; new/...)
157 * rename verified repository
165 object State extends Enumeration {
166 val Empty, Pending, Confirmed, Updating, Committing, Live = Value;
170 class RepositoryStateException(val state: Repository.State.Value,
172 extends Exception(msg);
174 class KeyConfigException(msg: String) extends Exception(msg);
176 class Repository(val root: File) extends Closeable {
177 import Repository.State.{Value => State, _};
179 /* Important directories and files. */
180 private[this] val livedir = root + "live";
181 private[this] val livereposdir = livedir + "repos";
182 private[this] val newdir = root + "new";
183 private[this] val olddir = root + "old";
184 private[this] val pendingdir = root + "pending";
185 private[this] val tmpdir = root + "tmp";
187 /* Take out a lock in case of other instances. */
188 private[this] val lock = {
189 try { root.mkdir_!(); }
190 catch { case SystemError(EEXIST, _) => ok; }
191 (root + "lk").lock_!()
193 def close() { lock.close(); }
195 /* Maintain a cache of some repository state. */
196 private var _state: State = null;
197 private var _config: Config = null;
198 private def invalidate() {
204 /* Determine the current repository state. */
207 _state = if (livedir.isdir_!) {
208 if (!livereposdir.isdir_!) Confirmed
209 else if (newdir.isdir_!) Updating
212 if (newdir.isdir_!) Committing
213 else if (pendingdir.isdir_!) Pending
220 def checkState(wanted: State*) {
221 /* Ensure we're in a particular state. */
223 if (wanted.forall(_ != st)) {
224 throw new RepositoryStateException(st, s"Repository is $st, not " +
226 wanted.map(_.toString)));
232 /* If we're part-way through an update then back out or press forward. */
236 /* We have a new tree allegedly ready, but the current one is still
237 * in place. It seems safer to zap the new one here, but we could go
242 invalidate(); // should move back to `Live' or `Confirmed'
245 /* We have a new tree ready, and an old one moved aside. We're going
246 * to have to move one of them. Let's try committing the new tree.
249 newdir.rename_!(livedir); // should move on to `Live'
253 /* Other states are stable. */
257 /* Now work through the things in our area of the filesystem and zap the
258 * ones which don't belong. In particular, this will always erase
262 root.foreachFile { f => (f.getName, st) match {
263 case ("lk", _) => ok;
264 case ("live", Live | Confirmed) => ok;
265 case ("pending", Pending) => ok;
266 case (_, Updating | Committing) =>
267 unreachable(s"unexpectedly still in `$st' state");
268 case _ => f.rmTree();
273 /* Clear out the entire repository. Everything. It's all gone. */
274 root.foreachFile { f => if (f.getName != "lk") f.rmTree(); }
278 /* Arrange to have an empty `tmpdir'. */
283 def config: Config = {
284 /* Return the repository configuration. */
286 if (_config == null) {
288 /* Firstly, decide where to find the configuration file. */
290 val dir = state match {
291 case Live | Confirmed => livedir
292 case Pending => pendingdir
294 throw new RepositoryStateException(Empty, "repository is Empty");
296 val file = dir + "tripe-keys.conf";
298 /* Build the new configuration in a temporary place. */
299 var m = HashMap[String, String]();
301 /* Read the config file into our map. */
302 file.withReader { in =>
304 for (line <- lines(in)) {
306 case RX_COMMENT() => ok;
307 case RX_KEYVAL(key, value) => m += key -> value;
309 throw new ConfigSyntaxError(file.getPath, lno,
310 "failed to parse line");
316 /* Fill in defaults where things have been missed out. */
317 for ((key, dflt) <- DEFAULTS) {
318 if (!(m contains key)) {
319 try { m += key -> dflt(m); }
321 case e: DefaultFailed =>
322 throw new ConfigDefaultFailed(file.getPath, key,
335 def fetchConfig(url: URL) {
336 /* Fetch an initial configuration file from a given URL. */
340 downloadToFile(tmpdir + "tripe-keys.conf", url);
341 tmpdir.rename_!(pendingdir);
342 invalidate(); // should move to `Pending'
346 /* The user has approved the master key fingerprint in the `Pending'
347 * configuration. Advance to `Confirmed'.
351 pendingdir.rename_!(livedir);
352 invalidate(); // should move to `Confirmed'
356 /* Update the repository from the master.
358 * Fetch a (possibly new) archive; unpack it; verify the master key
359 * against the known fingerprint; and check the signature on the bundle.
362 checkState(Confirmed, Live);
366 /* First thing is to download the tarball and signature. */
367 val tarfile = tmpdir + "tripe-keys.tar.gz";
368 downloadToFile(tarfile, new URL(conf("repos-url")));
369 val sigfile = tmpdir + "tripe-keys.sig";
370 val seq = conf("master-sequence");
371 downloadToFile(sigfile,
372 new URL(conf("sig-url").replaceAllLiterally("<SEQ>",
375 /* Unpack the tarball. Carefully. */
376 val unpkdir = tmpdir + "unpk";
378 withCleaner { clean =>
379 val tar = new TarFile(new GZIPInputStream(tarfile.open()));
380 clean { tar.close(); }
383 /* Check the filename to make sure it's not evil. */
384 if (e.name.split('/').exists { _ == ".." })
385 throw new KeyConfigException("invalid path in tarball");
387 /* Find out where this file points. */
388 val f = unpkdir + e.name;
392 /* A directory. Create it if it doesn't exist already. */
395 catch { case SystemError(EEXIST, _) => ok; }
396 } else if (e.isreg) {
397 /* A regular file. Write stuff to it. */
400 f.withOutput { out =>
401 for ((b, n) <- blocks(in)) out.write(b, 0, n);
405 /* Something else. Be paranoid and reject it. */
407 throw new KeyConfigException("unexpected object type in tarball");
412 /* There ought to be a file in here called `repos/master.pub'. */
413 val reposdir = unpkdir + "repos";
414 if (!reposdir.isdir_!)
415 throw new KeyConfigException("missing `repos/' directory");
416 val masterfile = reposdir + "master.pub";
417 if (!masterfile.isreg_!)
418 throw new KeyConfigException("missing `repos/master.pub' file");
420 /* Fetch the master key's fingerprint. */
421 val (out, _) = runCommand("key", "-k", masterfile.getPath,
424 "-a", conf("fingerprint-hash"),
430 /*----- That's all, folks -------------------------------------------------*/