Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Unreleased

- **A mod can move to a new author key** without owners removing it: the old key signs
`key-handover.json` (`Signing handover`), and an update by the new key goes in only when
those steps lead from the installed key to it. The catalogue takes the new key the same way
(`"key_from"`), always through a maintainer. So each mod can have its own key.
- **Dependencies are installed with a mod.** A mod's `requires` (copied into its catalogue
entry by `catalog.py add`, and read again from the downloaded zip's `mod.json`) are met
by what's installed, or by the catalogue's version, installed first; a required mod that's
Expand Down
14 changes: 13 additions & 1 deletion core/runtime/java/hmgapi/engine/Catalog.java
Original file line number Diff line number Diff line change
Expand Up @@ -1532,6 +1532,18 @@ static String pure(Plan p, String id) {
return null;
}

/**
* The keys an entry's mod was handed over from ("key_from", catalog.py add writes it from the zip's
* key-handover.json): a hint only, since the catalogue isn't signed. The install checks the handover itself.
*/
@SuppressWarnings("unchecked")
static Set<String> keyFrom(Map<String, Object> en) {
Set<String> out = new HashSet<String>();
Object k = en == null ? null : en.get("key_from");
if (k instanceof List) for (Object o : (List<Object>) k) out.add(String.valueOf(o).toLowerCase());
return out;
}

/**
* The automatic check's staging of one mod: only a pure update, signed with the installed key, into
* /data/hmgapi/pending/<id> (engine apply puts it in place). null when staged, else why not.
Expand All @@ -1542,7 +1554,7 @@ String stage(String id) throws Exception {
if (why != null) return why;
Step s = p.steps.get(0);
String have = Packages.installedUuid(new File(e.data, "mods/" + id));
if (have == null || !have.equalsIgnoreCase(String.valueOf(s.entry.get("uuid")))) {
if (have == null || !(have.equalsIgnoreCase(String.valueOf(s.entry.get("uuid"))) || keyFrom(s.entry).contains(have.toLowerCase()))) {
return "the catalogue names another author key than the installed " + id + "'s: update it yourself";
}
String off = offline();
Expand Down
24 changes: 21 additions & 3 deletions core/runtime/java/hmgapi/engine/Packages.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@
* the owner's settings and whether the mod is on. Files change only at the
* next `engine apply`. --force: an older version, or (with
* hmgapi.json overrides.allow_unsigned) an unsigned package, at the owner's risk.
*
* A new author key: an update signed with another key than the installed mod's goes
* in only when the mod's key-handover.json leads from the installed key to it, each
* step signed by the key it leaves (Signing.handoverChain). The installed key itself
* says what comes next, so nobody else can take a mod over; afterwards the new key
* is the installed one, and only it updates the mod.
*/
final class Packages {
private Packages() {}
Expand Down Expand Up @@ -120,8 +126,20 @@ static Verdict check(Engine e, File dir, String uuid, boolean force, boolean any
File old = new File(installed, "mod.json");
v.installedVersion = old.isFile() ? String.valueOf(Json.object(Engine.read(old)).get("version")) : null;

// signature and author
Signing.Result sig = Signing.verify(dir, haveUuid);
// signature and author: the installed key, or a key it handed the mod over to
Signing.Result sig = Signing.verify(dir, null);
boolean moved = false;
if (sig.ok && haveUuid != null && !haveUuid.equalsIgnoreCase(sig.uuid)) {
String why = Signing.handoverChain(dir, sig.id, haveUuid, sig.uuid);
if (why == null) {
moved = true;
String who = trustedAuthor(e, sig.uuid);
v.notes.add("key " + v.id + " moves to author key " + sig.uuid + (who != null ? " (" + who + ")" : "")
+ ", handed over by the installed key " + haveUuid + " (" + Signing.HANDOVER + "); its updates now come from the new key");
} else {
Signing.fail(sig, "signed by a different author (uuid " + sig.uuid + ", installed mod has " + haveUuid + "); " + why);
}
}
if (sig.ok) v.uuid = sig.uuid;
if (!sig.ok) {
boolean unsigned = !new File(dir, Signing.DIR + "/signature").isFile();
Expand Down Expand Up @@ -175,7 +193,7 @@ else if (!Version.satisfies(dv, d.range)) {
} else {
v.status = "new";
}
if (sig.ok && uuid != null && !uuid.equalsIgnoreCase(sig.uuid)) {
if (sig.ok && uuid != null && !uuid.equalsIgnoreCase(sig.uuid) && !(moved && uuid.equalsIgnoreCase(haveUuid))) {
return refuse(v, v.id + " is signed by author key " + sig.uuid + ", not the expected " + uuid);
}
if (sig.ok && haveUuid == null) { // a first install: its key is recorded, and said
Expand Down
100 changes: 99 additions & 1 deletion core/runtime/java/hmgapi/engine/Signing.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@
* sign <mod folder> <key file> writes META-HMGAPI/manifest.json and META-HMGAPI/signature
* sign-hmgapi <payload folder> <key> the same for a HMG-API release (id "hmgapi", version from VERSION)
* verify <mod folder> [<expected uuid>] checks every file, the signature, and the UUID
* handover <mod folder> <old key file> <new key file or .pub>
* adds a step to <mod folder>/key-handover.json: the old key
* says the mod's updates now come from the new key
* handover-check <mod folder> <installed uuid>
* verify, and that key-handover.json leads from that key to
* the one the folder is signed with
*
* A mod's UUID is derived from its author's public key (SHA-256 of the encoded
* key, first 16 bytes, as a version-8 UUID), so only the key holder can sign a
Expand All @@ -37,6 +43,15 @@
* manifest.json: {"format": 1, "id", "version", "uuid", "key": <base64 X.509
* public key>, "files": {"<path>": "<sha256>", ...}}. signature: base64 ECDSA
* (DER) over the manifest's exact bytes.
*
* Moving a mod to a new key: an update signed with another key than the installed
* mod's is refused, unless the mod carries key-handover.json, where the installed
* key itself says which key comes next: {"format": 1, "steps": [{"id", "from",
* "from_key": <base64 X.509 public key>, "to", "signature"}, ...]}, each signature
* the from key's ECDSA over handoverText(id, from, to). The steps chain (old to
* newer to newest), so a car that skipped a release still follows. The file is an
* ordinary file of the mod (in the manifest, so the new key signs over it too) and
* stays in every later release.
*/
public final class Signing {
private Signing() {}
Expand All @@ -61,12 +76,20 @@ public static void main(String[] a) throws Exception {
} else if (a.length >= 3 && "sign-hmgapi".equals(a[0])) {
File d = new File(a[1]);
System.out.println(signAs(d, new File(a[2]), "hmgapi", Engine.read(new File(d, "VERSION")).trim()));
} else if (a.length >= 4 && "handover".equals(a[0])) {
System.out.println(handover(new File(a[1]), new File(a[2]), new File(a[3])));
} else if (a.length >= 3 && "handover-check".equals(a[0])) {
Result r = verify(new File(a[1]), null);
String why = !r.ok ? r.error : handoverChain(new File(a[1]), r.id, a[2], r.uuid);
System.out.println(why == null ? "ok: " + r.id + " " + r.version + " key " + a[2] + " to " + r.uuid : "FAILED: " + why);
System.exit(why == null ? 0 : 1);
} else if (a.length >= 2 && "verify".equals(a[0])) {
Result r = verify(new File(a[1]), a.length > 2 ? a[2] : null);
System.out.println(r.ok ? "ok: " + r.id + " " + r.version + " uuid " + r.uuid : "FAILED: " + r.error);
System.exit(r.ok ? 0 : 1);
} else {
System.err.println("usage: keygen <key> | uuid <key|pub> | sign <mod dir> <key> | verify <mod dir> [uuid]");
System.err.println("usage: keygen <key> | uuid <key|pub> | sign <mod dir> <key> | verify <mod dir> [uuid]"
+ " | handover <mod dir> <old key> <new key|pub> | handover-check <mod dir> <installed uuid>");
System.exit(2);
}
}
Expand Down Expand Up @@ -127,6 +150,81 @@ public static String signAs(File mod, File keyFile, String id, String version) t
return "signed " + id + " " + version + ": " + files.size() + " files, uuid " + man.get("uuid");
}

// ---- key handover

static final String HANDOVER = "key-handover.json";

/** what the old key signs: the mod, the old key and the next one */
static String handoverText(String id, String from, String to) {
return "HMG-API key handover\nid " + id + "\nfrom " + from + "\nto " + to + "\n";
}

/** Adds a step from oldKey to newKey (a key file or its .pub) to mod's key-handover.json. */
@SuppressWarnings("unchecked")
public static String handover(File mod, File oldKey, File newKey) throws Exception {
String id = String.valueOf(Json.object(Engine.read(new File(mod, "mod.json"))).get("id"));
PublicKey oldPub = publicOf(oldKey);
String from = uuid(oldPub.getEncoded()), to = uuid(publicOf(newKey).getEncoded());
if (from.equals(to)) throw new IOException("the old and new keys are the same key");
PrivateKey priv = KeyFactory.getInstance("EC").generatePrivate(new PKCS8EncodedKeySpec(unb64(Engine.read(oldKey).trim())));
Signature s = Signature.getInstance("SHA256withECDSA");
s.initSign(priv);
s.update(handoverText(id, from, to).getBytes("UTF-8"));
File f = new File(mod, HANDOVER);
Map<String, Object> doc = f.isFile() ? Json.object(Engine.read(f)) : new LinkedHashMap<String, Object>();
List<Object> steps = doc.get("steps") instanceof List ? (List<Object>) doc.get("steps") : new ArrayList<Object>();
for (Object o : steps) {
if (o instanceof Map && from.equals(((Map<String, Object>) o).get("from"))) throw new IOException(HANDOVER + " already hands " + from + " over");
}
Map<String, Object> step = new LinkedHashMap<String, Object>();
step.put("id", id);
step.put("from", from);
step.put("from_key", b64(oldPub.getEncoded()));
step.put("to", to);
step.put("signature", b64(s.sign()));
steps.add(step);
Map<String, Object> out = new LinkedHashMap<String, Object>();
out.put("format", 1L);
out.put("steps", steps);
Engine.write(f, Json.write(out) + "\n");
return "key-handover " + id + ": " + from + " to " + to + " (" + steps.size() + (steps.size() == 1 ? " step)" : " steps)")
+ "; sign the mod with the new key, and keep " + HANDOVER + " in every release";
}

/**
* null when mod's key-handover.json leads from key {@code from} to key {@code to} for mod {@code id},
* each step signed by the key it leaves; else why not.
*/
@SuppressWarnings("unchecked")
public static String handoverChain(File mod, String id, String from, String to) {
try {
File f = new File(mod, HANDOVER);
if (!f.isFile()) return "no " + HANDOVER;
Object st = Json.object(Engine.read(f)).get("steps");
List<Object> steps = st instanceof List ? (List<Object>) st : new ArrayList<Object>();
String cur = from.toLowerCase();
for (int hops = 0; hops < 16 && !cur.equalsIgnoreCase(to); hops++) {
Map<String, Object> next = null;
for (Object o : steps) {
if (o instanceof Map && cur.equals(((Map<String, Object>) o).get("from"))) next = (Map<String, Object>) o;
}
if (next == null) return HANDOVER + " has no step from key " + cur;
if (!id.equals(next.get("id"))) return HANDOVER + ": the step from " + cur + " is for " + next.get("id") + ", not " + id;
byte[] key = unb64(String.valueOf(next.get("from_key")));
if (!cur.equals(uuid(key))) return HANDOVER + ": the step from " + cur + " carries another key";
String nx = String.valueOf(next.get("to"));
Signature s = Signature.getInstance("SHA256withECDSA");
s.initVerify(KeyFactory.getInstance("EC").generatePublic(new X509EncodedKeySpec(key)));
s.update(handoverText(id, cur, nx).getBytes("UTF-8"));
if (!s.verify(unb64(String.valueOf(next.get("signature"))))) return HANDOVER + ": the step from " + cur + " isn't signed by that key";
cur = nx.toLowerCase();
}
return cur.equalsIgnoreCase(to) ? null : HANDOVER + " doesn't lead to key " + to;
} catch (Exception x) {
return HANDOVER + ": " + x;
}
}

// ---- verify

public static final class Result {
Expand Down
19 changes: 19 additions & 0 deletions core/runtime/packages-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -135,4 +135,23 @@ ln -s $T/victim/top $R/data/hmgapi/mods/demo/filelink
eng remove demo; check "remove a mod with symlinks in its folder" 0 $rc
check "... the mod's folder is gone" no "$([ -e $R/data/hmgapi/mods/demo ] && echo yes || echo no)"
check "... what the links pointed at is all still there" "keep keep" "$(cat $T/victim/sub/file $T/victim/top 2>/dev/null | tr '\n' ' ' | sed 's/ $//')"

# a new author key: the installed key hands the mod over (key-handover.json, Signing handover)
mk 1.0.0; sig sign $T/m $T/author.key > /dev/null; pkg $T/m $T/k1.zip
eng install $T/k1.zip; check "key move: installed by the old key" 0 $rc
sig keygen $T/new.key > /dev/null; N=$(sig uuid $T/new.key)
mk 1.1.0; sig sign $T/m $T/new.key > /dev/null; pkg $T/m $T/k2.zip
eng install $T/k2.zip; check "a new key without a handover: refused" 3 $rc; has "... says why" "signed by a different author (uuid $N, installed mod has $U); no key-handover.json" "$out"
mk 1.1.0; sig handover $T/m $T/other.key $T/new.key.pub > /dev/null; sig sign $T/m $T/new.key > /dev/null; pkg $T/m $T/k3.zip
eng install $T/k3.zip; check "a handover from a key that isn't the installed one: refused" 3 $rc
mk 1.1.0; sig handover $T/m $T/author.key $T/new.key.pub > /dev/null; sig sign $T/m $T/new.key > /dev/null; pkg $T/m $T/k4.zip
eng install $T/k4.zip --uuid $U; check "handed over by the installed key (expected: the installed key, as release updates pass)" 0 $rc
has "... says the key moves" "key demo moves to author key $N, handed over by the installed key $U" "$out"
check "... the new key is the installed one" "$N" "$(python3 -c 'import json,sys; print(json.load(open(sys.argv[1]))["uuid"])' $R/data/hmgapi/mods/demo/META-HMGAPI/manifest.json)"
mk 1.2.0; sig sign $T/m $T/author.key > /dev/null; pkg $T/m $T/k5.zip
eng install $T/k5.zip; check "after the move, the old key: refused" 3 $rc
mk 1.2.0; sig handover $T/m $T/author.key $T/new.key.pub > /dev/null; sig sign $T/m $T/new.key > /dev/null; pkg $T/m $T/k6.zip
eng install $T/k6.zip --uuid $N; check "the new key's next release (expected: the catalogue's new key)" 0 $rc
hasnt "... no move this time" "moves to author key" "$out"
eng remove demo > /dev/null
exit $fail
30 changes: 30 additions & 0 deletions core/runtime/signing-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,34 @@ cp -r "$T/hello" "$T/t6"; S sign "$T/t6" "$T/forger.key" > /dev/null
v "$T/t6" "$U"; check "update by another key refused" 1 $rc
cp -r "$T/hello" "$T/t7"; sed 's/"version": "1.0.0"/"version": "9.9.9"/' "$T/t7/mod.json" > "$T/t7/mod.json.new" && mv "$T/t7/mod.json.new" "$T/t7/mod.json"
v "$T/t7"; check "edited mod.json refused" 1 $rc

# key handover: the installed key says which key the mod moves to (key-handover.json)
S keygen "$T/new.key" > /dev/null; S keygen "$T/newer.key" > /dev/null; N=$(S uuid "$T/new.key")
hc() { set +e; out=$(S handover-check "$@" 2>&1); rc=$?; set -e; }
cp -r mods/examples/hello "$T/h"; rm -rf "$T/h/META-HMGAPI"
has "handover" "key-handover hello: $U to $N (1 step)" "$(S handover "$T/h" "$T/author.key" "$T/new.key.pub")"
S sign "$T/h" "$T/new.key" > /dev/null
v "$T/h"; check "... the handover file is signed with the mod" 0 $rc
hc "$T/h" "$U"; check "handover-check: the old key to the new one" 0 $rc; has "says so" "ok: hello 1.0.0 key $U to $N" "$out"
hc "$T/h" "00000000-0000-8000-8000-000000000000"; check "... from a key it doesn't name: refused" 1 $rc
has "... says why" "has no step from key 00000000-0000-8000-8000-000000000000" "$out"
set +e; S handover "$T/h" "$T/author.key" "$T/newer.key" > /dev/null 2>&1; rc=$?; set -e
check "... the same old key can't hand over twice" 1 $rc
has "a second step" "(2 steps)" "$(S handover "$T/h" "$T/new.key" "$T/newer.key")"
S sign "$T/h" "$T/newer.key" > /dev/null
hc "$T/h" "$U"; check "a chain: old to new to newer" 0 $rc
hc "$T/h" "$N"; check "... and from the middle" 0 $rc
# a forger's step: their own signature, claiming to leave the real key
cp -r mods/examples/hello "$T/f"; rm -rf "$T/f/META-HMGAPI"; S handover "$T/f" "$T/forger.key" "$T/new.key.pub" > /dev/null
python3 - "$T/f/key-handover.json" "$U" "$(cat "$T/author.key.pub")" <<'P'
import json, sys
d = json.load(open(sys.argv[1])); d['steps'][0]['from'] = sys.argv[2]; d['steps'][0]['from_key'] = sys.argv[3].strip()
json.dump(d, open(sys.argv[1], 'w'))
P
S sign "$T/f" "$T/new.key" > /dev/null
hc "$T/f" "$U"; check "a step not signed by the key it leaves: refused" 1 $rc; has "... says why" "isn't signed by that key" "$out"
# another mod's handover doesn't move this one
cp -r "$T/h" "$T/o"; sed 's/"id": "hello"/"id": "other"/' "$T/o/mod.json" > "$T/o/mod.json.new" && mv "$T/o/mod.json.new" "$T/o/mod.json"
S sign "$T/o" "$T/newer.key" > /dev/null
hc "$T/o" "$U"; check "another mod's handover: refused" 1 $rc; has "... says why" "is for hello, not other" "$out"
exit $fail
32 changes: 32 additions & 0 deletions docs/mod-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -540,6 +540,38 @@ your source. Keep your key private and backed up: a lost key
means a new UUID, and owners would have to remove the old mod before installing
yours again.

### Moving to a new key

A mod can move to a new author key without owners removing it, as long as you still
have the old key (a lost key can't be handed over). The old key signs a short
statement, `key-handover.json` at the top of your mod folder, naming the mod and the
key that comes next:

```
java -cp <sdk>/lib/hmgapi-engine.jar hmgapi.engine.Signing keygen new.key
java -cp <sdk>/lib/hmgapi-engine.jar hmgapi.engine.Signing handover <mod folder> old.key new.key.pub
```

Commit `key-handover.json`, sign the next release with the new key, and set
`"hmgapi"` to the HMG-API release that first reads handovers or newer (an older head
unit refuses an update by another key, whatever the file says). Then:

- An installed copy signed by the old key takes the update: the install checks that
each step of `key-handover.json` is signed by the key it leaves, and that the steps
lead from the installed key to the one the release is signed with. It says the key
moved. From then on only the new key updates the mod; the old key no longer does.
- Keep `key-handover.json` in every later release, so a car that skipped the first
one still follows. A second move adds a step (`handover <mod folder> new.key
newer.key.pub`); the file chains them.
- In the catalogue, `tools/catalog.py add` takes the new key when the zip hands the
old one over, lists it in `maps/authors.json`, and records the old one in the
entry's `"key_from"`. CI checks the handover in the download. A key change is never
a plain version bump, so a maintainer merges it.
- A new key isn't on older HMG-API releases' trusted author list, but that only
matters for a first install: updates follow the handover.
- Nobody else can move your mod: a handover only counts when the installed key signed
it, and it names one mod id.

### Updates from your own releases

A mod the catalogue doesn't list (installed from USB or adb, private, or waiting for
Expand Down
Loading
Loading