001// SPDX-FileCopyrightText: 2021 Paul Schaub <vanitasvitae@fsfe.org>
002//
003// SPDX-License-Identifier: Apache-2.0
004
005package sop.operation;
006
007import java.io.ByteArrayInputStream;
008import java.io.IOException;
009import java.io.InputStream;
010import java.util.Date;
011
012import sop.DecryptionResult;
013import sop.ReadyWithResult;
014import sop.SessionKey;
015import sop.exception.SOPGPException;
016
017public interface Decrypt {
018
019    /**
020     * Makes the SOP consider signatures before this date invalid.
021     *
022     * @param timestamp timestamp
023     * @return builder instance
024     */
025    Decrypt verifyNotBefore(Date timestamp)
026            throws SOPGPException.UnsupportedOption;
027
028    /**
029     * Makes the SOP consider signatures after this date invalid.
030     *
031     * @param timestamp timestamp
032     * @return builder instance
033     */
034    Decrypt verifyNotAfter(Date timestamp)
035            throws SOPGPException.UnsupportedOption;
036
037    /**
038     * Adds the verification cert.
039     *
040     * @param cert input stream containing the cert
041     * @return builder instance
042     */
043    Decrypt verifyWithCert(InputStream cert)
044            throws SOPGPException.BadData,
045            IOException;
046
047    /**
048     * Adds the verification cert.
049     *
050     * @param cert byte array containing the cert
051     * @return builder instance
052     */
053    default Decrypt verifyWithCert(byte[] cert)
054            throws SOPGPException.BadData, IOException {
055        return verifyWithCert(new ByteArrayInputStream(cert));
056    }
057
058    /**
059     * Tries to decrypt with the given session key.
060     *
061     * @param sessionKey session key
062     * @return builder instance
063     */
064    Decrypt withSessionKey(SessionKey sessionKey)
065            throws SOPGPException.UnsupportedOption;
066
067    /**
068     * Tries to decrypt with the given password.
069     *
070     * @param password password
071     * @return builder instance
072     */
073    Decrypt withPassword(String password)
074            throws SOPGPException.PasswordNotHumanReadable,
075            SOPGPException.UnsupportedOption;
076
077    /**
078     * Adds the decryption key.
079     *
080     * @param key input stream containing the key
081     * @return builder instance
082     */
083    Decrypt withKey(InputStream key)
084            throws SOPGPException.KeyIsProtected,
085            SOPGPException.BadData,
086            SOPGPException.UnsupportedAsymmetricAlgo;
087
088    /**
089     * Adds the decryption key.
090     *
091     * @param key byte array containing the key
092     * @return builder instance
093     */
094    default Decrypt withKey(byte[] key)
095            throws SOPGPException.KeyIsProtected,
096            SOPGPException.BadData,
097            SOPGPException.UnsupportedAsymmetricAlgo {
098        return withKey(new ByteArrayInputStream(key));
099    }
100
101    /**
102     * Decrypts the given ciphertext, returning verification results and plaintext.
103     * @param ciphertext ciphertext
104     * @return ready with result
105     */
106    ReadyWithResult<DecryptionResult> ciphertext(InputStream ciphertext)
107            throws SOPGPException.BadData, SOPGPException.MissingArg, SOPGPException.CannotDecrypt;
108
109    /**
110     * Decrypts the given ciphertext, returning verification results and plaintext.
111     * @param ciphertext ciphertext
112     * @return ready with result
113     */
114    default ReadyWithResult<DecryptionResult> ciphertext(byte[] ciphertext)
115        throws SOPGPException.BadData, SOPGPException.MissingArg, SOPGPException.CannotDecrypt {
116        return ciphertext(new ByteArrayInputStream(ciphertext));
117    }
118}