[vault] Android: the android key kind (RSA-4096, self-signed, 100 years) and the apk sign kind (1-4 SHA-512 digests, PKCS#1 v1.5), never unattended; v0.14.0

This commit is contained in:
2026-11-08 00:27:47 +01:00
parent 82698bd9e5
commit 045f1f5d00
12 changed files with 564 additions and 27 deletions
+107 -5
View File
@@ -49,6 +49,8 @@ pub enum KeyKind {
Ed25519,
RootCa,
CodeSigning,
/// An Android app's signing key (core 0.14.0): RSA-4096 and a self-signed certificate; signs `apk` only.
Android,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
@@ -59,6 +61,9 @@ pub enum SignKind {
Authenticode,
/// A deploy approval's canonical JSON, signed as minisign by `domi-approve` only (core 0.12.0).
Approval,
/// APK Signature Scheme v2/v3: 1 to 4 SHA-512 digests, signed with RSA PKCS#1 v1.5, by an Android key only
/// (core 0.14.0).
Apk,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
@@ -92,7 +97,8 @@ pub struct Context {
pub struct Request {
pub key: String,
pub kind: SignKind,
/// BLAKE2b-512 of the file (minisign) or signtool's 32-byte digest (authenticode). Empty for a manifest.
/// BLAKE2b-512 of the file (minisign), signtool's 32-byte digest (authenticode), or 1 to 4 SHA-512 digests of
/// an APK's signed data, concatenated (apk). Empty for a manifest or an approval.
pub digest: Vec<u8>,
/// The `SHA256SUMS` text, for a manifest.
pub manifest: Option<String>,
@@ -148,7 +154,8 @@ pub struct AuditEntry {
/// A signature handed back to the build.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Signed {
/// The `.minisig` text (minisign, manifest) or the raw RSA signature (authenticode).
/// The `.minisig` text (minisign, manifest, approval), the raw RSA signature (authenticode) or the raw RSA
/// signatures, concatenated in the order of the digests (apk).
pub bytes: Vec<u8>,
pub sha256: String,
}
@@ -170,7 +177,7 @@ pub struct KeyView {
pub key_id: Option<String>,
/// The `minisign.pub` text, for Ed25519 keys.
pub minisign_public: Option<String>,
/// The certificate (DER), for root and code-signing keys.
/// The certificate (DER), for root, code-signing and Android keys.
pub cert: Option<Vec<u8>>,
pub issuer: Option<String>,
}
@@ -275,7 +282,7 @@ impl Store {
let seed: [u8; 32] = k.secret.expose().try_into().ok()?;
Secret::Ed25519(Ed25519Secret::from_bytes(seed))
}
KeyKind::RootCa | KeyKind::CodeSigning => {
KeyKind::RootCa | KeyKind::CodeSigning | KeyKind::Android => {
Secret::Rsa(RsaSecret::from_pkcs8_der(k.secret.expose().to_vec()).ok()?)
}
};
@@ -297,6 +304,10 @@ impl Store {
Some(c) => Some(unb64(c)?),
None => None,
};
// An Android key is nothing without its certificate: the app is identified by it forever.
if k.kind == KeyKind::Android && cert.is_none() {
return None;
}
keys.push(Key {
id: k.id,
kind: k.kind,
@@ -372,6 +383,22 @@ fn approve_key(pipelines: &[String], repos: &[String], now: i64) -> Key {
}
}
/// What [`Vault::signing_create_android`] makes: the key's id and name, the certificate's subject, its role and
/// who may ask. The key is RSA-4096 and the certificate is valid [`custos_sign::ANDROID_YEARS`] years.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct AndroidKey {
/// `[a-z0-9-]{1,64}`, e.g. `postea-android`; not the id of a key the vault has.
pub id: String,
pub name: String,
/// The certificate's subject: common name, organisation, two-letter country code.
pub cn: String,
pub o: String,
pub c: String,
pub role: Role,
pub pipelines: Vec<String>,
pub repos: Vec<String>,
}
fn in_scope(list: &[String], what: &str) -> bool {
list.iter().any(|x| x == "*" || x == what)
}
@@ -484,6 +511,49 @@ impl Vault {
Ok(APPROVE_KEY.into())
}
/// Creates an Android app signing key (core 0.14.0): RSA-4096 and a self-signed certificate. A release key
/// always asks; a test key signs without asking, never for a `tag`. The caller saves. Slow: one RSA-4096 key.
pub fn signing_create_android(&mut self, spec: &AndroidKey, now: i64) -> Result<String, Error> {
let id = spec.id.as_str();
if id.is_empty()
|| id.len() > 64
|| !id.bytes().all(|b| b.is_ascii_lowercase() || b.is_ascii_digit() || b == b'-')
{
return Err(Error::Signing("a key id is 1 to 64 of a-z, 0-9 and -".into()));
}
if self.signing.get(id).is_some() {
return Err(Error::Signing(format!("there is a key `{id}` already")));
}
let name = spec.name.trim();
if name.is_empty() || name.len() > 100 || name.chars().any(char::is_control) {
return Err(Error::Signing("the key's name is not usable".into()));
}
let (pipelines, repos) = (clean_list(&spec.pipelines)?, clean_list(&spec.repos)?);
let made = custos_sign::new_android(&spec.cn, &spec.o, &spec.c).map_err(|e| match e {
custos_sign::Error::Certificate => Error::Signing(
"the certificate's name is not usable (1-64 characters without , + = \" \\ < > ; # and no space at \
either end, and a two-letter country code)"
.into(),
),
e => Error::Signing(e.to_string()),
})?;
self.signing.keys.push(Key {
id: id.into(),
kind: KeyKind::Android,
role: spec.role,
name: name.into(),
secret: Secret::Rsa(made.secret),
public: None,
key_id: None,
cert: Some(made.cert_der),
issuer: None,
created: now,
pipelines,
repos,
});
Ok(id.into())
}
/// Every signing key, public parts only.
pub fn signing_keys(&self) -> Vec<KeyView> {
self.signing
@@ -553,6 +623,7 @@ impl Vault {
(KeyKind::Ed25519, SignKind::Minisign | SignKind::Manifest) => k.id != APPROVE_KEY,
(KeyKind::Ed25519, SignKind::Approval) => approver,
(KeyKind::CodeSigning, SignKind::Authenticode) => true,
(KeyKind::Android, SignKind::Apk) => true,
_ => false,
};
if k.kind == KeyKind::RootCa {
@@ -579,6 +650,13 @@ impl Vault {
SignKind::Authenticode if req.digest.len() != 32 => {
return Err(Refusal::Invalid("an Authenticode digest is SHA-256: 32 bytes".into()));
}
SignKind::Apk
if req.digest.is_empty()
|| req.digest.len() % 64 != 0
|| req.digest.len() > 64 * custos_sign::MAX_APK_DIGESTS =>
{
return Err(Refusal::Invalid("an APK request is 1 to 4 SHA-512 digests: 64 bytes each".into()));
}
SignKind::Manifest => {
let text = req.manifest.as_deref().ok_or_else(|| Refusal::Invalid("no manifest text".into()))?;
custos_sign::manifest_check(text).map_err(|e| Refusal::Invalid(e.to_string()))?;
@@ -670,6 +748,7 @@ impl Vault {
)
.map(String::into_bytes),
(Secret::Rsa(s), SignKind::Authenticode) => custos_sign::authenticode(s, &req.digest),
(Secret::Rsa(s), SignKind::Apk) => custos_sign::apk(s, &req.digest),
_ => unreachable!("signing_check matched the key to the kind"),
};
let bytes = match bytes {
@@ -805,6 +884,19 @@ mod tests {
Vault::create(&SecretString::from("pw"), KdfParams { m_cost_kib: 64, t_cost: 1, p_cost: 1 }).unwrap();
v.signing_create(Role::Release, &s(&["*"]), &s(&["*"]), 1).unwrap();
v.signing_create(Role::Test, &s(&["*"]), &s(&["*"]), 1).unwrap();
for (id, role) in [("postea-android", Role::Release), ("domi-test-android", Role::Test)] {
let spec = AndroidKey {
id: id.into(),
name: id.into(),
cn: "Postea".into(),
o: "Marek Novak".into(),
c: "CZ".into(),
role,
pipelines: s(&["*"]),
repos: s(&["*"]),
};
v.signing_create_android(&spec, 1).unwrap();
}
let secrets: Vec<Vec<u8>> = v
.signing
.keys
@@ -814,7 +906,7 @@ mod tests {
Secret::Rsa(s) => s.expose().to_vec(),
})
.collect();
assert_eq!(secrets.len(), 7, "the release set with domi-approve, and the test set");
assert_eq!(secrets.len(), 9, "the release set with domi-approve, the test set, two Android keys");
// Every public way out: views (serialised and printed), certificates, minisign public keys, checks,
// signatures of every kind, refusals, the audit log, errors, and the Debug of the vault's own store.
@@ -839,6 +931,10 @@ mod tests {
));
reqs.push(approve);
reqs.push(req("domi-approve", SignKind::Minisign, 64));
reqs.push(req("postea-android", SignKind::Apk, 128));
reqs.push(req("domi-test-android", SignKind::Apk, 64));
reqs.push(req("postea-android", SignKind::Authenticode, 32));
reqs.push(req("domi-code-signing", SignKind::Apk, 64));
for r in &reqs {
out += &format!("{:?}", v.signing_check(r));
for approval in [Approval::Mark, Approval::Auto] {
@@ -852,6 +948,12 @@ mod tests {
v.signing_audit().iter().any(|e| e.kind == SignKind::Approval && e.outcome == Outcome::Approved),
"an approval was signed too"
);
assert!(
v.signing_audit()
.iter()
.any(|e| e.kind == SignKind::Apk && e.key == "postea-android" && e.outcome == Outcome::Approved),
"and an APK"
);
out += &serde_json::to_string(v.signing_audit()).unwrap();
out += &format!("{:?}", v.signing_audit());
out += &format!("{:?}", v.signing_create(Role::Release, &[], &[], 3));