diff --git a/build.gradle b/build.gradle index 021e0eb..ef690e8 100644 --- a/build.gradle +++ b/build.gradle @@ -198,12 +198,36 @@ def signingKey = SigningKeys.armoured(providers.environmentVariable('SIGNING_KEY // SIGNING_PASSWORD signs with it, printing no part of either. Gradle's signing reports every one of // these as "Could not read PGP secret key" (v1.0.0 release run 9); this says which. A key other // than the release key passes with -PsigningKeyFingerprint= (a rehearsal key). +// gpg reads only a passphrase's first line and Gradle the whole value, so the task then signs a few +// bytes with Gradle's own signatory: the verdict is the one the Sign tasks would reach. tasks.register('verifySigningKey', Exec) { group = 'verification' description = 'Checks SIGNING_KEY and SIGNING_PASSWORD without printing them (scripts/release/check-signing-key.sh).' outputs.upToDateWhen { false } + ignoreExitValue = true commandLine 'bash', file('scripts/release/check-signing-key.sh'), providers.gradleProperty('signingKeyFingerprint').getOrElse('') + def signed = !signingKey.isBlank() + doLast { + boolean scriptPassed = executionResult.get().exitValue == 0 + boolean gradlePassed = !signed + String verdict = 'skipped: no SIGNING_KEY' + if (signed) { + try { + signing.signatory.sign(new ByteArrayInputStream('signing check'.bytes), OutputStream.nullOutputStream()) + gradlePassed = true + verdict = 'ok' + } catch (Exception failure) { + // Exception text only: BouncyCastle names what failed (checksum mismatch, a tag), never the key. + def cause = failure.cause ? " (${failure.cause.class.simpleName}: ${failure.cause.message})" : '' + verdict = "failed: ${failure.message}${cause}" + } + } + logger.lifecycle("signing as Gradle's Sign tasks do (in-memory key): ${verdict}") + if (!scriptPassed || !gradlePassed) { + throw new GradleException('verifySigningKey: the signing secrets would not sign; the PROBLEM lines above say why.') + } + } } tasks.withType(Sign).configureEach { mustRunAfter tasks.named('verifySigningKey') diff --git a/scripts/release/check-signing-key.sh b/scripts/release/check-signing-key.sh index d1cb1c6..ea66519 100755 --- a/scripts/release/check-signing-key.sh +++ b/scripts/release/check-signing-key.sh @@ -23,6 +23,15 @@ if [ -z "${SIGNING_KEY:-}" ]; then exit 1 fi [ -n "${SIGNING_PASSWORD:-}" ] || echo "check-signing-key: SIGNING_PASSWORD is empty (only right for a key without a passphrase)" +# The passphrase's shape only: a pasted value often carries a line break or spaces the key was not +# protected with, and Gradle uses the value as given. +python3 - <<'PY' +import os +p = os.environ.get("SIGNING_PASSWORD", "") +print(f"password shape: {len(p)} characters, line breaks {p.count(chr(10))}, CR {p.count(chr(13))}," + f" leading whitespace {'yes' if p[:1].isspace() else 'no'}, trailing whitespace {'yes' if p[-1:].isspace() else 'no'}," + f" non-ASCII characters {sum(1 for c in p if ord(c) > 127)}") +PY home=$(mktemp -d) trap 'rm -rf "$home"' EXIT @@ -143,12 +152,16 @@ if [ -s "$home/key.asc" ] && GNUPGHOME="$home" gpg --batch --quiet --import "$ho fail=1 fi printf 'signing check\n' > "$home/message" - if printf '%s' "${SIGNING_PASSWORD:-}" | GNUPGHOME="$home" gpg --batch --pinentry-mode loopback \ - --passphrase-fd 0 --local-user "${fingerprint:-none}" --detach-sign --output "$home/message.sig" \ - "$home/message" >/dev/null 2>&1; then + # gpg reads only the first line of the file; the shape line above says whether there is more. + printf '%s' "${SIGNING_PASSWORD:-}" > "$home/passphrase" + if GNUPGHOME="$home" gpg --batch --pinentry-mode loopback --passphrase-file "$home/passphrase" \ + --status-fd 3 --local-user "${fingerprint:-none}" --detach-sign --output "$home/message.sig" \ + "$home/message" >/dev/null 2>&1 3>"$home/status"; then echo "signing with SIGNING_PASSWORD: ok" else - echo "PROBLEM: SIGNING_PASSWORD does not unlock the key for signing (wrong passphrase, or the key is not a signing key)" + # gpg's status keywords only (BAD_PASSPHRASE, MISSING_PASSPHRASE, ERROR …): no values. + status=$(awk '$1 == "[GNUPG:]" {print $2}' "$home/status" | sort -u | tr '\n' ' ') + echo "PROBLEM: SIGNING_PASSWORD does not unlock the key for signing (gpg status: ${status:-none})" fail=1 fi else