Skip to content

PGPKey (load and generate keys) ​

ts
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.

ParameterTypeRequirementExplanation
pathstringrequiredPath of the key file
passphrasestringoptionalUnlocks 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.
ts
var partner = PGPKey.FromFile("/data/keys/partner.asc");
var ours    = PGPKey.FromFile("/data/keys/ours.key", GetSecret("pgp-passphrase"));
ts
// 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.

ts
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.

ParameterTypeRequirementExplanation
keySecretNamestringrequiredName of the secret that holds the key
passphraseSecretNamestringoptionalName of the secret that holds the passphrase of the key
ts
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.

sh
# Linux
base64 -w0 /data/keys/ours.key
powershell
# 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.

OptionTypeExplanation
NamestringName in the user ID of the key. Name or Email is required. The characters ( ) < > are not allowed in Name, Email and Comment.
EmailstringEmail address in the user ID
CommentstringOptional comment in the user ID
Algorithmstring"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
BitsnumberRSA key size, from 2048 to 16384. Default 4096. Ignored for Curve25519.
ExpiresDaysnumberDays until the key expires. Leave it out for a key that never expires.
ts
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 ​

PropertyTypeExplanation
FingerprintstringFingerprint of the primary key: 40 upper case hex digits, no spaces
KeyIDstringID of the primary key: 16 upper case hex digits
UserIDsarrayUser IDs, primary first, such as "Partner Inc <pgp@partner.example>"
Algorithmstring"RSA", "EdDSA" (a Curve25519 key), "ECDSA" and so on
BitsnumberKey size in bits for an RSA key. An elliptic curve key reports the size of its encoded public key instead: 263 for Curve25519.
CreatedDateCreation time
ExpiresDate or nullExpiry time; null when the key never expires
IsExpiredbooleanThe key has expired
IsRevokedbooleanThe key has been revoked
CanEncryptbooleanMessages can be encrypted to this key now
CanSignbooleanThe key is valid for signing. Whether this copy of it can sign depends on HasPrivate and IsLocked.
HasPrivatebooleanThe private half is present
IsLockedbooleanThe private half is still protected by a passphrase, so it cannot decrypt or sign yet
IsLegacybooleanThe 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.

MethodReturnsExplanation
MatchesFingerprint(fpr)booleantrue 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()stringThe ASCII armored public key
ArmorPrivate(passphrase?)stringThe 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)booleanWrites the armored public key to path. Returns false on failure; the reason is in the log.
SavePrivate(path, passphrase?)booleanWrites 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 GetSecret returns "" for a secret that does not exist. Check the passphrase before you give it to SavePrivate or ArmorPrivate.
  • On Linux, a file written by SavePrivate is 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.

ts
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:

ts
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.

sh
# 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