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;
023
024import org.jivesoftware.smackx.ox.OpenPgpContact;
025import org.jivesoftware.smackx.ox.OpenPgpMessage;
026import org.jivesoftware.smackx.ox.OpenPgpSelf;
027import org.jivesoftware.smackx.ox.element.CryptElement;
028import org.jivesoftware.smackx.ox.element.OpenPgpContentElement;
029import org.jivesoftware.smackx.ox.element.OpenPgpElement;
030import org.jivesoftware.smackx.ox.element.SignElement;
031import org.jivesoftware.smackx.ox.element.SigncryptElement;
032import org.jivesoftware.smackx.ox.store.definition.OpenPgpStore;
033
034import org.bouncycastle.openpgp.PGPException;
035import org.pgpainless.decryption_verification.OpenPgpMetadata;
036
037public interface OpenPgpProvider {
038
039    /**
040     * Return the {@link OpenPgpStore} instance of this provider.
041     * This MUST NOT return null.
042     *
043     * @return store TODO javadoc me please
044     */
045    OpenPgpStore getStore();
046
047    /**
048     * Sign a {@link SigncryptElement} using our signing key and encrypt it for all {@code recipients} and ourselves.
049     *
050     * @param element {@link SigncryptElement} which contains a payload which will be transmitted.
051     * @param self our own OpenPGP identity.
052     * @param recipients recipients identities.
053     *
054     * @return signed and encrypted {@link SigncryptElement} as a {@link OpenPgpElement}, along with
055     * {@link OpenPgpMetadata} about the encryption/signatures.
056     *
057     * @throws IOException IO is dangerous
058     * @throws PGPException PGP is brittle
059     */
060    OpenPgpElementAndMetadata signAndEncrypt(SigncryptElement element, OpenPgpSelf self, Collection<OpenPgpContact> recipients)
061            throws IOException, PGPException;
062
063    /**
064     * Sign a {@link SignElement} using our signing key.
065     * @param element {@link SignElement} which contains a payload.
066     * @param self our OpenPGP identity.
067     *
068     * @return signed {@link SignElement} as {@link OpenPgpElement}, along with {@link OpenPgpMetadata} about the
069     * signatures.
070     *
071     * @throws IOException IO is dangerous
072     * @throws PGPException PGP is brittle
073     */
074    OpenPgpElementAndMetadata sign(SignElement element, OpenPgpSelf self)
075            throws IOException, PGPException;
076
077    /**
078     * Encrypt a {@link CryptElement} for all {@code recipients} and ourselves.
079     * @param element {@link CryptElement} which contains a payload which will be transmitted.
080     * @param self our own OpenPGP identity.
081     * @param recipients recipient identities.
082     *
083     * @return encrypted {@link CryptElement} as an {@link OpenPgpElement}, along with {@link OpenPgpMetadata} about
084     * the encryption.
085     *
086     * @throws IOException IO is dangerous
087     * @throws PGPException PGP is brittle
088     */
089    OpenPgpElementAndMetadata encrypt(CryptElement element, OpenPgpSelf self, Collection<OpenPgpContact> recipients)
090            throws IOException, PGPException;
091
092    /**
093     * Decrypt and/or verify signatures on an incoming {@link OpenPgpElement}.
094     * If the message is encrypted, this method decrypts it. If it is (also) signed, the signature will be checked.
095     * The resulting {@link OpenPgpMessage} contains the original {@link OpenPgpContentElement}, as well as information
096     * about the encryption/signing.
097     *
098     * @param element signed and or encrypted {@link OpenPgpElement}.
099     * @param self our OpenPGP identity.
100     * @param sender OpenPGP identity of the sender.
101     * @param connection XMPP connection used to fetch any missing keys.
102     *
103     * @return decrypted message as {@link OpenPgpMessage}.
104     *
105     * @throws IOException IO is dangerous
106     * @throws PGPException PGP is brittle
107     */
108    OpenPgpMessage decryptAndOrVerify(XMPPConnection connection, OpenPgpElement element, OpenPgpSelf self, OpenPgpContact sender)
109            throws IOException, PGPException;
110}