PGP verification (file, string, bytes)
function PGPVerifyFile(inFile: string, sigFile: string, keys: PGPKey | string | Array): boolean;
function PGPVerifyString(text: string, signature: string | null, keys: PGPKey | string | Array): PGPMessageResult;
function PGPVerifyBytes(data: Uint8Array | number[] | string, signature: Uint8Array | string | null, keys: PGPKey | string | Array): PGPMessageResult;NOTE
PGPVerifyFile(inFile, sigFile, publicKeyFile) is available as of Syncplify Server! v7.1.1. Key objects, several keys, and the string and byte array functions are available as of Syncplify Server! v8.12.0.
Checks a signature against the public keys you trust. keys is a key object, the path of a public key file, or an array of either: pass every key the signature may legitimately come from.
Verification succeeds only when all of this is true:
- one of
keysmade the signature, - the data is exactly what was signed, and
- the signing key is neither expired nor revoked at the time of the check.
IMPORTANT
Because of the last rule, a file that verified while the partner key was valid no longer verifies once that key has expired. Verify incoming files when they arrive, not when you take them out of an archive.
Which function to use
| What you received | How to verify it |
|---|---|
A file and a separate signature file (.sig or .asc) | PGPVerifyFile(file, sigFile, keys) |
| A file that is signed and encrypted | PGPDecryptFile with { VerifyWith: keys } |
One file that holds data and signature, not encrypted (gpg --sign) | PGPDecryptFile(file, outFile, null, { VerifyWith: keys }) |
A text that starts with -----BEGIN PGP SIGNED MESSAGE----- | PGPVerifyString(text, null, keys) |
PGPVerifyFile
Checks the detached signature in sigFile, armored or binary, over inFile.
| Return value | Explanation |
|---|---|
true | The signature is valid. PGPLastResult().SignerFingerprint() tells which key made it. |
false | The signature is not valid, or could not be checked; PGPLastResult().ErrorMsg() says why. Do not process the file. |
var partner = PGPKey.FromFile("/data/keys/partner.asc");
if (PGPVerifyFile("/data/in/prices.csv", "/data/in/prices.csv.sig", partner)) {
Log.Info("prices.csv is authentic");
} else {
Log.Error("prices.csv rejected: " + PGPLastResult().ErrorMsg());
}PGPVerifyString
Works in two ways, depending on signature:
- A string:
signatureis a detached signature overtext. null:textis itself a signed message, cleartext or inline.Data()of the result is the content that was signed.
var partner = PGPKey.FromFile("/data/keys/partner.asc");
var res = PGPVerifyString(ReadTextFile("/data/in/notice.txt.asc"), null, partner);
if (res.Ok()) {
Log.Info("notice from the partner: " + res.Data());
} else {
Log.Error("notice rejected: " + res.ErrorMsg());
}WARNING
- Use
Data()as the verified content, never the text you passed in. - A signed message must stand alone. Text before or after the signed block makes verification fail, because the signature does not cover it.
- An empty string is not
null. It is a detached signature that is missing, and verification fails. This matters becauseReadTextFilereturns""for a signature file that is not there.
PGPVerifyBytes
The same for binary data. signature is a detached signature, as bytes or as an armored string, or null when data is itself an inline signed message. Data() of the result is a byte array.
var partner = PGPKey.FromFile("/data/keys/partner.asc");
// A download and its detached signature, published side by side
var file = new HttpCli().Url("https://downloads.partner.example.com/catalog.zip").Timeout(60).Get();
var sig = new HttpCli().Url("https://downloads.partner.example.com/catalog.zip.sig").Timeout(60).Get();
if (!file.Ok() || !sig.Ok()) {
Log.Error("download failed");
Exit(1);
}
var data = file.BodyAsBytes(); // the body of a response can be read only once
var res = PGPVerifyBytes(data, sig.BodyAsBytes(), partner);
if (res.Ok()) {
Log.Info("catalog verified: " + data.length + " bytes, signed on " + FormatDateTime("YYYY-MM-DD", res.SignedAt()));
} else {
Log.Error("catalog rejected: " + res.ErrorMsg());
}Several keys
While a partner replaces an old key with a new one, files may arrive signed by either. Pass both, and read which one signed:
var oldKey = PGPKey.FromFile("/data/keys/partner.asc");
var newKey = PGPKey.FromFile("/data/keys/partner-new.asc");
if (PGPVerifyFile("/data/in/prices.csv", "/data/in/prices.csv.sig", [oldKey, newKey])) {
if (PGPLastResult().SignerFingerprint() === oldKey.Fingerprint) {
Log.Warn("the partner still signs with the old key");
}
}