PGPKey (load and generate keys)
PGPKey.FromFile(path: string, passphrase?: string): PGPKey;
PGPKey.FromString(text: string, passphrase?: string): PGPKey;
PGPKey.FromSecret(keySecretName: string, passphraseSecretName?: string): PGPKey;
PGPKey.Generate(options: object): PGPKey;NOTE
PGPKey is available as of Syncplify Server! v8.12.0.
PGPKey is a global object. Each of its four functions returns a key object, which every other PGP function accepts wherever it takes a key.
IMPORTANT
All four functions throw when the key cannot be loaded or created, so a script never carries on without the key it needs. To handle the failure yourself, wrap the call in try and catch; the reason is in the message of the error.
PGPKey.FromFile
Loads one key from a file, ASCII armored or binary, public or private.
| Parameter | Type | Requirement | Explanation |
|---|---|---|---|
path | string | required | Path of the key file |
passphrase | string | optional | Unlocks a private key that is protected by a passphrase |
- A protected private key loaded without its passphrase can still encrypt and verify, but cannot decrypt or sign.
- A file that holds several different keys is refused: give each key its own file. A file with the public and the private block of the same key is fine.
var partner = PGPKey.FromFile("/data/keys/partner.asc");
var ours = PGPKey.FromFile("/data/keys/ours.key", GetSecret("pgp-passphrase"));// Handling a key that cannot be loaded
var partner;
try {
partner = PGPKey.FromFile("/data/keys/partner.asc");
} catch (e) {
Log.Error("cannot load the partner key: " + e.message);
Exit(1);
}PGPKey.FromString
Loads one key from text, usually an armored block that starts with -----BEGIN PGP PUBLIC KEY BLOCK-----. A key encoded as Base64 is accepted too. passphrase works as in PGPKey.FromFile.
var armored = ReadTextFile("/data/keys/partner.asc"); // or text from a database or an HTTP response
var partner = PGPKey.FromString(armored);PGPKey.FromSecret
Loads one key straight from the secret store. Both secrets are read inside the engine, so neither the private key nor its passphrase ever becomes a string in your script. This is the recommended way to use a private key.
| Parameter | Type | Requirement | Explanation |
|---|---|---|---|
keySecretName | string | required | Name of the secret that holds the key |
passphraseSecretName | string | optional | Name of the secret that holds the passphrase of the key |
var ours = PGPKey.FromSecret("pgp-private-key", "pgp-passphrase");In Syncplify Server! the secret store belongs to the virtual site, so every node of a cluster has it. A key file would have to be copied to each node by hand.
Storing a key as a secret
The Value field of the Secrets page holds a single line, and an armored key has many. Store the key file as one line of standard Base64 instead: SyncJS decodes it when it loads the key.
# Linux
base64 -w0 /data/keys/ours.key# Windows PowerShell
[Convert]::ToBase64String([IO.File]::ReadAllBytes("C:\Keys\ours.key"))Paste the output as the value of the secret. Keep a backup of the private key in a safe place: a secret value cannot be read back from the Admin UI.
PGPKey.Generate
Creates a new key with its private half. The key exists only in memory until you save it.
| Option | Type | Explanation |
|---|---|---|
Name | string | Name in the user ID of the key. Name or Email is required. The characters ( ) < > are not allowed in Name, Email and Comment. |
Email | string | Email address in the user ID |
Comment | string | Optional comment in the user ID |
Algorithm | string | "rsa" (default), which every OpenPGP product accepts, or "curve25519", which is smaller and faster and is what GnuPG creates by default, but which some older partner software cannot use |
Bits | number | RSA key size, from 2048 to 16384. Default 4096. Ignored for Curve25519. |
ExpiresDays | number | Days until the key expires. Leave it out for a key that never expires. |
var key = PGPKey.Generate({ Name: "ACME Exports", Email: "pgp@acme.example", ExpiresDays: 730 });
key.SavePublic("/data/keys/acme.asc"); // give this file to your partners
key.SavePrivate("/data/keys/acme.key", GetSecret("pgp-passphrase")); // keep this one to yourself
Log.Info("fingerprint: " + key.Fingerprint);TIP
Generate a key once, in a script of its own, and not on every run. An RSA key takes a few seconds to create, and your partners need the same public key every time. See Create your key for the whole procedure.
The key object
| Property | Type | Explanation |
|---|---|---|
Fingerprint | string | Fingerprint of the primary key: 40 upper case hex digits, no spaces |
KeyID | string | ID of the primary key: 16 upper case hex digits |
UserIDs | array | User IDs, primary first, such as "Partner Inc <pgp@partner.example>" |
Algorithm | string | "RSA", "EdDSA" (a Curve25519 key), "ECDSA" and so on |
Bits | number | Key size in bits for an RSA key. An elliptic curve key reports the size of its encoded public key instead: 263 for Curve25519. |
Created | Date | Creation time |
Expires | Date or null | Expiry time; null when the key never expires |
IsExpired | boolean | The key has expired |
IsRevoked | boolean | The key has been revoked |
CanEncrypt | boolean | Messages can be encrypted to this key now |
CanSign | boolean | The key is valid for signing. Whether this copy of it can sign depends on HasPrivate and IsLocked. |
HasPrivate | boolean | The private half is present |
IsLocked | boolean | The private half is still protected by a passphrase, so it cannot decrypt or sign yet |
IsLegacy | boolean | The key was made by GeneratePGPKeys before v8.12. It works in SyncJS, but GnuPG cannot import it. |
The properties describe the key as it was when the script loaded it.
| Method | Returns | Explanation |
|---|---|---|
MatchesFingerprint(fpr) | boolean | true when fpr is the full fingerprint of this key. Spaces, colons, a leading 0x and letter case are ignored, so a fingerprint copied from gpg output matches. A short key ID never matches. |
Armor() | string | The ASCII armored public key |
ArmorPrivate(passphrase?) | string | The ASCII armored private key, protected by passphrase when one is given and unprotected otherwise. A key that is still locked keeps the protection it has. Throws when the key has no private half. |
SavePublic(path) | boolean | Writes the armored public key to path. Returns false on failure; the reason is in the log. |
SavePrivate(path, passphrase?) | boolean | Writes the armored private key to path, with the passphrase rules of ArmorPrivate. Logs a warning when no passphrase protects the file. Returns false on failure; the reason is in the log. |
WARNING
- An empty passphrase means no protection, and
GetSecretreturns""for a secret that does not exist. Check the passphrase before you give it toSavePrivateorArmorPrivate. - On Linux, a file written by
SavePrivateis readable by its owner only. On Windows it gets the permissions of the folder it is written to, so choose a folder that only the service account and the administrators can read.
Pin partner keys by fingerprint
A key file can be replaced on disk. The fingerprint, confirmed with your partner over another channel such as a phone call, cannot be faked.
var partner = PGPKey.FromFile("/data/keys/partner.asc");
if (!partner.MatchesFingerprint(GetSecret("partner-fingerprint"))) {
Log.Error("the partner key file does not match the pinned fingerprint");
Exit(1);
}Watch the expiry date
Encrypting to an expired key fails. Warn yourself while there is still time to ask the partner for a new one:
var partner = PGPKey.FromFile("/data/keys/partner.asc");
if (partner.Expires !== null && partner.Expires - new Date() < 30 * 86400000) {
Log.Warn("the partner key expires on " + FormatDateTime("YYYY-MM-DD", partner.Expires));
}Keys and GnuPG
Keys move between SyncJS and GnuPG as ordinary key files.
# Your partner exports their public key for you
gpg --armor --export pgp@partner.example > partner.asc
# Print the fingerprint of a key file without importing it
gpg --show-keys --with-fingerprint partner.asc
# Export one of your own private keys; it keeps its passphrase
gpg --armor --export-secret-keys pgp@acme.example > acme.key
# Your partner imports the public key you wrote with SavePublic
gpg --import acme.asc