001// SPDX-FileCopyrightText: 2018 Paul Schaub <vanitasvitae@fsfe.org>
002//
003// SPDX-License-Identifier: Apache-2.0
004
005package org.pgpainless;
006
007import java.io.IOException;
008import java.util.Date;
009import javax.annotation.Nonnull;
010
011import org.bouncycastle.openpgp.PGPKeyRing;
012import org.bouncycastle.openpgp.PGPPublicKeyRing;
013import org.bouncycastle.openpgp.PGPSecretKeyRing;
014import org.pgpainless.decryption_verification.DecryptionBuilder;
015import org.pgpainless.decryption_verification.DecryptionStream;
016import org.pgpainless.encryption_signing.EncryptionBuilder;
017import org.pgpainless.encryption_signing.EncryptionStream;
018import org.pgpainless.key.generation.KeyRingBuilder;
019import org.pgpainless.key.generation.KeyRingTemplates;
020import org.pgpainless.key.info.KeyRingInfo;
021import org.pgpainless.key.modification.secretkeyring.SecretKeyRingEditor;
022import org.pgpainless.key.modification.secretkeyring.SecretKeyRingEditorInterface;
023import org.pgpainless.key.parsing.KeyRingReader;
024import org.pgpainless.key.util.KeyRingUtils;
025import org.pgpainless.policy.Policy;
026import org.pgpainless.util.ArmorUtils;
027
028public final class PGPainless {
029
030    private PGPainless() {
031
032    }
033
034    /**
035     * Generate a fresh OpenPGP key ring from predefined templates.
036     * @return templates
037     */
038    public static KeyRingTemplates generateKeyRing() {
039        return new KeyRingTemplates();
040    }
041
042    /**
043     * Build a custom OpenPGP key ring.
044     *
045     * @return builder
046     */
047    public static KeyRingBuilder buildKeyRing() {
048        return new KeyRingBuilder();
049    }
050
051    /**
052     * Read an existing OpenPGP key ring.
053     * @return builder
054     */
055    public static KeyRingReader readKeyRing() {
056        return new KeyRingReader();
057    }
058
059    /**
060     * Extract a public key certificate from a secret key.
061     *
062     * @param secretKey secret key
063     * @return public key certificate
064     */
065    public static PGPPublicKeyRing extractCertificate(@Nonnull PGPSecretKeyRing secretKey) {
066        return KeyRingUtils.publicKeyRingFrom(secretKey);
067    }
068
069    /**
070     * Wrap a key or certificate in ASCII armor.
071     *
072     * @param key key or certificate
073     * @return ascii armored string
074     */
075    public static String asciiArmor(@Nonnull PGPKeyRing key) throws IOException {
076        if (key instanceof PGPSecretKeyRing) {
077            return ArmorUtils.toAsciiArmoredString((PGPSecretKeyRing) key);
078        } else {
079            return ArmorUtils.toAsciiArmoredString((PGPPublicKeyRing) key);
080        }
081    }
082
083    /**
084     * Create an {@link EncryptionStream}, which can be used to encrypt and/or sign data using OpenPGP.
085     *
086     * @return builder
087     */
088    public static EncryptionBuilder encryptAndOrSign() {
089        return new EncryptionBuilder();
090    }
091
092    /**
093     * Create a {@link DecryptionStream}, which can be used to decrypt and/or verify data using OpenPGP.
094     *
095     * @return builder
096     */
097    public static DecryptionBuilder decryptAndOrVerify() {
098        return new DecryptionBuilder();
099    }
100
101    /**
102     * Make changes to a key ring.
103     * This method can be used to change key expiration dates and passphrases, or add/remove/revoke subkeys.
104     *
105     * After making the desired changes in the builder, the modified key ring can be extracted using {@link SecretKeyRingEditorInterface#done()}.
106     *
107     * @param secretKeys secret key ring
108     * @return builder
109     */
110    public static SecretKeyRingEditorInterface modifyKeyRing(PGPSecretKeyRing secretKeys) {
111        return new SecretKeyRingEditor(secretKeys);
112    }
113
114    /**
115     * Quickly access information about a {@link org.bouncycastle.openpgp.PGPPublicKeyRing} / {@link PGPSecretKeyRing}.
116     * This method can be used to determine expiration dates, key flags and other information about a key.
117     *
118     * To evaluate a key at a given date (e.g. to determine if the key was allowed to create a certain signature)
119     * use {@link KeyRingInfo#KeyRingInfo(PGPKeyRing, Date)} instead.
120     *
121     * @param keyRing key ring
122     * @return access object
123     */
124    public static KeyRingInfo inspectKeyRing(PGPKeyRing keyRing) {
125        return new KeyRingInfo(keyRing);
126    }
127
128    /**
129     * Access, and make changes to PGPainless policy on acceptable/default algorithms etc.
130     *
131     * @return policy
132     */
133    public static Policy getPolicy() {
134        return Policy.getInstance();
135    }
136}