2021-10-07 15:48:52 +02:00
|
|
|
// SPDX-FileCopyrightText: 2018 Paul Schaub <vanitasvitae@fsfe.org>
|
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
2018-07-18 18:23:06 +02:00
|
|
|
package org.pgpainless;
|
2018-06-02 21:21:35 +02:00
|
|
|
|
2021-07-01 17:15:25 +02:00
|
|
|
import java.io.IOException;
|
2021-05-31 15:33:42 +02:00
|
|
|
import java.util.Date;
|
2021-07-01 17:15:25 +02:00
|
|
|
import javax.annotation.Nonnull;
|
|
|
|
|
2020-11-22 20:48:09 +01:00
|
|
|
import org.bouncycastle.openpgp.PGPKeyRing;
|
2021-07-01 17:15:25 +02:00
|
|
|
import org.bouncycastle.openpgp.PGPPublicKeyRing;
|
2020-10-23 16:44:21 +02:00
|
|
|
import org.bouncycastle.openpgp.PGPSecretKeyRing;
|
2018-07-18 18:23:06 +02:00
|
|
|
import org.pgpainless.decryption_verification.DecryptionBuilder;
|
|
|
|
import org.pgpainless.decryption_verification.DecryptionStream;
|
|
|
|
import org.pgpainless.encryption_signing.EncryptionBuilder;
|
|
|
|
import org.pgpainless.encryption_signing.EncryptionStream;
|
|
|
|
import org.pgpainless.key.generation.KeyRingBuilder;
|
2021-11-02 12:23:05 +01:00
|
|
|
import org.pgpainless.key.generation.KeyRingTemplates;
|
2020-11-22 20:48:09 +01:00
|
|
|
import org.pgpainless.key.info.KeyRingInfo;
|
2020-11-19 17:51:57 +01:00
|
|
|
import org.pgpainless.key.modification.secretkeyring.SecretKeyRingEditor;
|
|
|
|
import org.pgpainless.key.modification.secretkeyring.SecretKeyRingEditorInterface;
|
2018-07-18 18:23:06 +02:00
|
|
|
import org.pgpainless.key.parsing.KeyRingReader;
|
2021-07-01 17:15:25 +02:00
|
|
|
import org.pgpainless.key.util.KeyRingUtils;
|
2021-02-19 19:51:44 +01:00
|
|
|
import org.pgpainless.policy.Policy;
|
2021-07-01 17:15:25 +02:00
|
|
|
import org.pgpainless.util.ArmorUtils;
|
2018-06-02 21:21:35 +02:00
|
|
|
|
2021-08-15 15:24:19 +02:00
|
|
|
public final class PGPainless {
|
|
|
|
|
|
|
|
private PGPainless() {
|
|
|
|
|
|
|
|
}
|
2018-06-02 21:21:35 +02:00
|
|
|
|
2018-07-02 20:14:57 +02:00
|
|
|
/**
|
2021-11-02 12:23:05 +01:00
|
|
|
* Generate a fresh OpenPGP key ring from predefined templates.
|
|
|
|
* @return templates
|
|
|
|
*/
|
|
|
|
public static KeyRingTemplates generateKeyRing() {
|
|
|
|
return new KeyRingTemplates();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Build a custom OpenPGP key ring.
|
|
|
|
*
|
2018-07-02 20:14:57 +02:00
|
|
|
* @return builder
|
|
|
|
*/
|
2021-11-02 12:23:05 +01:00
|
|
|
public static KeyRingBuilder buildKeyRing() {
|
2018-06-02 21:21:35 +02:00
|
|
|
return new KeyRingBuilder();
|
|
|
|
}
|
2018-06-04 14:50:09 +02:00
|
|
|
|
2018-07-02 20:14:57 +02:00
|
|
|
/**
|
|
|
|
* Read an existing OpenPGP key ring.
|
|
|
|
* @return builder
|
|
|
|
*/
|
|
|
|
public static KeyRingReader readKeyRing() {
|
|
|
|
return new KeyRingReader();
|
2018-06-04 14:50:09 +02:00
|
|
|
}
|
|
|
|
|
2021-07-01 17:15:25 +02:00
|
|
|
/**
|
|
|
|
* Extract a public key certificate from a secret key.
|
|
|
|
*
|
|
|
|
* @param secretKey secret key
|
|
|
|
* @return public key certificate
|
|
|
|
*/
|
|
|
|
public static PGPPublicKeyRing extractCertificate(@Nonnull PGPSecretKeyRing secretKey) {
|
|
|
|
return KeyRingUtils.publicKeyRingFrom(secretKey);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Wrap a key or certificate in ASCII armor.
|
|
|
|
*
|
|
|
|
* @param key key or certificate
|
|
|
|
* @return ascii armored string
|
2022-04-02 18:56:05 +02:00
|
|
|
*
|
|
|
|
* @throws IOException in case of an error in the {@link org.bouncycastle.bcpg.ArmoredOutputStream}
|
2021-07-01 17:15:25 +02:00
|
|
|
*/
|
|
|
|
public static String asciiArmor(@Nonnull PGPKeyRing key) throws IOException {
|
|
|
|
if (key instanceof PGPSecretKeyRing) {
|
|
|
|
return ArmorUtils.toAsciiArmoredString((PGPSecretKeyRing) key);
|
|
|
|
} else {
|
|
|
|
return ArmorUtils.toAsciiArmoredString((PGPPublicKeyRing) key);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-29 15:33:54 +01:00
|
|
|
/**
|
|
|
|
* Create an {@link EncryptionStream}, which can be used to encrypt and/or sign data using OpenPGP.
|
2021-01-09 20:30:34 +01:00
|
|
|
*
|
2020-11-29 15:33:54 +01:00
|
|
|
* @return builder
|
|
|
|
*/
|
|
|
|
public static EncryptionBuilder encryptAndOrSign() {
|
2018-07-02 20:14:57 +02:00
|
|
|
return new EncryptionBuilder();
|
2018-06-04 14:50:09 +02:00
|
|
|
}
|
|
|
|
|
2020-11-29 15:33:54 +01:00
|
|
|
/**
|
|
|
|
* Create a {@link DecryptionStream}, which can be used to decrypt and/or verify data using OpenPGP.
|
2021-05-15 18:44:03 +02:00
|
|
|
*
|
2020-11-29 15:33:54 +01:00
|
|
|
* @return builder
|
|
|
|
*/
|
|
|
|
public static DecryptionBuilder decryptAndOrVerify() {
|
2018-07-02 20:14:57 +02:00
|
|
|
return new DecryptionBuilder();
|
2018-06-05 01:30:58 +02:00
|
|
|
}
|
2018-06-13 17:26:48 +02:00
|
|
|
|
2021-05-31 15:33:42 +02:00
|
|
|
/**
|
|
|
|
* Make changes to a key ring.
|
|
|
|
* This method can be used to change key expiration dates and passphrases, or add/remove/revoke subkeys.
|
|
|
|
*
|
|
|
|
* After making the desired changes in the builder, the modified key ring can be extracted using {@link SecretKeyRingEditorInterface#done()}.
|
|
|
|
*
|
|
|
|
* @param secretKeys secret key ring
|
|
|
|
* @return builder
|
|
|
|
*/
|
2020-11-19 17:51:57 +01:00
|
|
|
public static SecretKeyRingEditorInterface modifyKeyRing(PGPSecretKeyRing secretKeys) {
|
|
|
|
return new SecretKeyRingEditor(secretKeys);
|
2020-10-23 16:44:21 +02:00
|
|
|
}
|
|
|
|
|
2020-11-22 20:48:09 +01:00
|
|
|
/**
|
|
|
|
* Quickly access information about a {@link org.bouncycastle.openpgp.PGPPublicKeyRing} / {@link PGPSecretKeyRing}.
|
2021-05-31 15:33:42 +02:00
|
|
|
* This method can be used to determine expiration dates, key flags and other information about a key.
|
|
|
|
*
|
|
|
|
* To evaluate a key at a given date (e.g. to determine if the key was allowed to create a certain signature)
|
2022-03-07 10:36:20 +01:00
|
|
|
* use {@link #inspectKeyRing(PGPKeyRing, Date)} instead.
|
2020-11-22 20:48:09 +01:00
|
|
|
*
|
|
|
|
* @param keyRing key ring
|
|
|
|
* @return access object
|
|
|
|
*/
|
|
|
|
public static KeyRingInfo inspectKeyRing(PGPKeyRing keyRing) {
|
|
|
|
return new KeyRingInfo(keyRing);
|
|
|
|
}
|
|
|
|
|
2022-03-07 10:36:20 +01:00
|
|
|
/**
|
|
|
|
* Quickly access information about a {@link org.bouncycastle.openpgp.PGPPublicKeyRing} / {@link PGPSecretKeyRing}.
|
|
|
|
* This method can be used to determine expiration dates, key flags and other information about a key at a specific time.
|
|
|
|
*
|
|
|
|
* @param keyRing key ring
|
|
|
|
* @param inspectionDate date of inspection
|
|
|
|
* @return access object
|
|
|
|
*/
|
|
|
|
public static KeyRingInfo inspectKeyRing(PGPKeyRing keyRing, Date inspectionDate) {
|
|
|
|
return new KeyRingInfo(keyRing, inspectionDate);
|
|
|
|
}
|
|
|
|
|
2021-05-31 15:33:42 +02:00
|
|
|
/**
|
|
|
|
* Access, and make changes to PGPainless policy on acceptable/default algorithms etc.
|
|
|
|
*
|
|
|
|
* @return policy
|
|
|
|
*/
|
2021-01-22 16:50:08 +01:00
|
|
|
public static Policy getPolicy() {
|
|
|
|
return Policy.getInstance();
|
|
|
|
}
|
2018-06-02 21:21:35 +02:00
|
|
|
}
|