001/**
002 *
003 * Copyright 2018 Paul Schaub.
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 *     http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.jivesoftware.smackx.ox.crypto;
018
019import java.io.IOException;
020import java.util.Collection;
021
022import org.jivesoftware.smack.XMPPConnection;
023import org.jivesoftware.smackx.ox.OpenPgpContact;
024import org.jivesoftware.smackx.ox.OpenPgpMessage;
025import org.jivesoftware.smackx.ox.OpenPgpSelf;
026import org.jivesoftware.smackx.ox.element.CryptElement;
027import org.jivesoftware.smackx.ox.element.OpenPgpContentElement;
028import org.jivesoftware.smackx.ox.element.OpenPgpElement;
029import org.jivesoftware.smackx.ox.element.SignElement;
030import org.jivesoftware.smackx.ox.element.SigncryptElement;
031import org.jivesoftware.smackx.ox.store.definition.OpenPgpStore;
032
033import org.bouncycastle.openpgp.PGPException;
034import org.pgpainless.decryption_verification.OpenPgpMetadata;
035
036public interface OpenPgpProvider {
037
038    /**
039     * Return the {@link OpenPgpStore} instance of this provider.
040     * This MUST NOT return null.
041     *
042     * @return store TODO javadoc me please
043     */
044    OpenPgpStore getStore();
045
046    /**
047     * Sign a {@link SigncryptElement} using our signing key and encrypt it for all {@code recipients} and ourselves.
048     *
049     * @param element {@link SigncryptElement} which contains a payload which will be transmitted.
050     * @param self our own OpenPGP identity.
051     * @param recipients recipients identities.
052     *
053     * @return signed and encrypted {@link SigncryptElement} as a {@link OpenPgpElement}, along with
054     * {@link OpenPgpMetadata} about the encryption/signatures.
055     *
056     * @throws IOException IO is dangerous
057     * @throws PGPException PGP is brittle
058     */
059    OpenPgpElementAndMetadata signAndEncrypt(SigncryptElement element, OpenPgpSelf self, Collection<OpenPgpContact> recipients)
060            throws IOException, PGPException;
061
062    /**
063     * Sign a {@link SignElement} using our signing key.
064     * @param element {@link SignElement} which contains a payload.
065     * @param self our OpenPGP identity.
066     *
067     * @return signed {@link SignElement} as {@link OpenPgpElement}, along with {@link OpenPgpMetadata} about the
068     * signatures.
069     *
070     * @throws IOException IO is dangerous
071     * @throws PGPException PGP is brittle
072     */
073    OpenPgpElementAndMetadata sign(SignElement element, OpenPgpSelf self)
074            throws IOException, PGPException;
075
076    /**
077     * Encrypt a {@link CryptElement} for all {@code recipients} and ourselves.
078     * @param element {@link CryptElement} which contains a payload which will be transmitted.
079     * @param self our own OpenPGP identity.
080     * @param recipients recipient identities.
081     *
082     * @return encrypted {@link CryptElement} as an {@link OpenPgpElement}, along with {@link OpenPgpMetadata} about
083     * the encryption.
084     *
085     * @throws IOException IO is dangerous
086     * @throws PGPException PGP is brittle
087     */
088    OpenPgpElementAndMetadata encrypt(CryptElement element, OpenPgpSelf self, Collection<OpenPgpContact> recipients)
089            throws IOException, PGPException;
090
091    /**
092     * Decrypt and/or verify signatures on an incoming {@link OpenPgpElement}.
093     * If the message is encrypted, this method decrypts it. If it is (also) signed, the signature will be checked.
094     * The resulting {@link OpenPgpMessage} contains the original {@link OpenPgpContentElement}, as well as information
095     * about the encryption/signing.
096     *
097     * @param element signed and or encrypted {@link OpenPgpElement}.
098     * @param self our OpenPGP identity.
099     * @param sender OpenPGP identity of the sender.
100     * @param connection XMPP connection used to fetch any missing keys.
101     *
102     * @return decrypted message as {@link OpenPgpMessage}.
103     *
104     * @throws IOException IO is dangerous
105     * @throws PGPException PGP is brittle
106     */
107    OpenPgpMessage decryptAndOrVerify(XMPPConnection connection, OpenPgpElement element, OpenPgpSelf self, OpenPgpContact sender)
108            throws IOException, PGPException;
109}