1
0
Fork 0
mirror of https://github.com/vanitasvitae/Smack.git synced 2024-06-13 15:14:54 +02:00
Smack/smack-openpgp/src/main/java/org/jivesoftware/smackx/ox/OpenPgpStore.java

168 lines
7.9 KiB
Java
Raw Normal View History

2018-05-30 22:06:09 +02:00
/**
*
* Copyright 2018 Paul Schaub.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
2018-05-28 00:58:13 +02:00
package org.jivesoftware.smackx.ox;
2018-05-28 20:07:06 +02:00
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
2018-06-02 14:27:18 +02:00
import java.util.Date;
import java.util.Map;
2018-05-28 00:58:13 +02:00
import java.util.Set;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smackx.ox.callback.SecretKeyRestoreSelectionCallback;
import org.jivesoftware.smackx.ox.element.PubkeyElement;
import org.jivesoftware.smackx.ox.element.PublicKeysListElement;
import org.jivesoftware.smackx.ox.element.SecretkeyElement;
import org.jivesoftware.smackx.ox.element.SigncryptElement;
import org.jivesoftware.smackx.ox.exception.InvalidBackupCodeException;
import org.jivesoftware.smackx.ox.exception.MissingOpenPgpKeyPairException;
import org.jivesoftware.smackx.ox.exception.MissingOpenPgpPublicKeyException;
2018-05-30 22:06:09 +02:00
import org.jivesoftware.smackx.ox.exception.SmackOpenPgpException;
2018-05-28 00:58:13 +02:00
import org.jxmpp.jid.BareJid;
public interface OpenPgpStore {
/**
* Return the {@link OpenPgpV4Fingerprint} of the primary OpenPGP key pair.
* If multiple key pairs are available, only the primary key pair is used for signing.
* <br>
* Note: This method returns {@code null} if no key pair is available.
*
* @return fingerprint of the primary OpenPGP key pair.
*/
OpenPgpV4Fingerprint primaryOpenPgpKeyPairFingerprint();
/**
* Return a {@link Set} containing the {@link OpenPgpV4Fingerprint} of all available OpenPGP key pairs.
*
* @return set of fingerprints of available OpenPGP key pairs.
*/
Set<OpenPgpV4Fingerprint> availableOpenPgpKeyPairFingerprints();
/**
2018-06-02 14:27:18 +02:00
* Return a {@link Map} containing the {@link OpenPgpV4Fingerprint}s of all currently announced OpenPGP
* public keys of a contact along with the dates of their latest update.
2018-05-28 00:58:13 +02:00
* <br>
* Note: Those are the keys announced in the latest received metadata update.
2018-06-02 14:27:18 +02:00
* This returns a {@link Map} which might contain different {@link OpenPgpV4Fingerprint}s than the result of
2018-05-28 20:07:06 +02:00
* {@link #availableOpenPgpPublicKeysFingerprints(BareJid)}.
2018-05-28 00:58:13 +02:00
* Messages should be encrypted to the intersection of both sets.
*
* @param contact contact.
2018-06-02 14:27:18 +02:00
* @return map of contacts last announced public keys and their update dates.
2018-05-28 00:58:13 +02:00
*/
2018-06-02 14:27:18 +02:00
Map<OpenPgpV4Fingerprint, Date> announcedOpenPgpKeyFingerprints(BareJid contact);
2018-05-28 00:58:13 +02:00
/**
* Return a {@link Set} containing the {@link OpenPgpV4Fingerprint}s of all OpenPGP public keys of a
* contact, which we have locally available.
* <br>
* Note: This returns a {@link Set} that might be different from the result of
2018-05-28 20:07:06 +02:00
* {@link #availableOpenPgpPublicKeysFingerprints(BareJid)}.
2018-05-28 00:58:13 +02:00
* Messages should be encrypted to the intersection of both sets.
*
* @param contact contact.
* @return list of contacts locally available public keys.
2018-05-30 22:06:09 +02:00
* @throws SmackOpenPgpException if something goes wrong
2018-05-28 00:58:13 +02:00
*/
2018-05-28 20:07:06 +02:00
Set<OpenPgpV4Fingerprint> availableOpenPgpPublicKeysFingerprints(BareJid contact)
2018-05-30 22:06:09 +02:00
throws SmackOpenPgpException;
2018-05-28 00:58:13 +02:00
/**
* Store incoming update to the OpenPGP metadata node in persistent storage.
*
* @param connection authenticated {@link XMPPConnection} of the user.
* @param listElement {@link PublicKeysListElement} which contains a list of the keys of {@code owner}.
* @param owner {@link BareJid} of the owner of the announced public keys.
*/
void storePublicKeysList(XMPPConnection connection, PublicKeysListElement listElement, BareJid owner);
2018-05-28 20:07:06 +02:00
/**
* Create a fresh OpenPGP key pair with the {@link BareJid} of the user prefixed by "xmpp:" as user-id
* (example: {@code "xmpp:juliet@capulet.lit"}).
* Store the key pair in persistent storage and return the public keys {@link OpenPgpV4Fingerprint}.
*
2018-06-02 14:27:18 +02:00
* @return {@link OpenPgpV4Fingerprint} of the generated key pair.
2018-05-28 20:07:06 +02:00
* @throws NoSuchAlgorithmException if a Hash algorithm is not available
* @throws NoSuchProviderException id no suitable cryptographic provider (for example BouncyCastleProvider)
* is registered.
2018-05-30 22:06:09 +02:00
* @throws SmackOpenPgpException if the generated key cannot be added to the keyring for some reason.
2018-05-28 20:07:06 +02:00
*/
OpenPgpV4Fingerprint createOpenPgpKeyPair()
2018-05-30 22:06:09 +02:00
throws NoSuchAlgorithmException, NoSuchProviderException, SmackOpenPgpException;
2018-05-28 20:07:06 +02:00
2018-05-28 00:58:13 +02:00
/**
* Create a {@link PubkeyElement} which contains our exported OpenPGP public key.
* The element can for example be published.
*
* @return {@link PubkeyElement} containing our public key.
2018-05-30 22:06:09 +02:00
* @throws MissingOpenPgpPublicKeyException if we have no OpenPGP key pair.
* @throws SmackOpenPgpException if something goes wrong.
2018-05-28 00:58:13 +02:00
*/
PubkeyElement createPubkeyElement(OpenPgpV4Fingerprint fingerprint)
2018-05-30 22:06:09 +02:00
throws SmackOpenPgpException, MissingOpenPgpPublicKeyException;
2018-05-28 00:58:13 +02:00
/**
* Process an incoming {@link PubkeyElement} of a contact or ourselves.
* That typically includes importing/updating the key.
*
* @param owner owner of the OpenPGP public key contained in the {@link PubkeyElement}.
* @param fingerprint {@link OpenPgpV4Fingerprint} of the key.
* @param element {@link PubkeyElement} which presumably contains the public key of the {@code owner}.
2018-06-02 14:27:18 +02:00
* @param currentMetadataDate {@link Date} which is currently found in the metadata node for this key.
2018-05-30 22:06:09 +02:00
* @throws SmackOpenPgpException if the key found in the {@link PubkeyElement}
2018-05-28 00:58:13 +02:00
* can not be deserialized or imported.
*/
2018-06-02 14:27:18 +02:00
void storePublicKey(BareJid owner, OpenPgpV4Fingerprint fingerprint, PubkeyElement element, Date currentMetadataDate)
2018-05-30 22:06:09 +02:00
throws SmackOpenPgpException;
2018-05-28 00:58:13 +02:00
2018-06-02 14:27:18 +02:00
/**
* Return the {@link Date} of the last time on which the key has been fetched from PubSub.
*
* @param owner owner of the key
* @param fingerprint fingerprint of the key.
* @return {@link Date} or {@code null} if no record found.
*/
Date getPubkeysLatestUpdateDate(BareJid owner, OpenPgpV4Fingerprint fingerprint);
2018-05-28 00:58:13 +02:00
/**
* Create an encrypted backup of our secret keys.
*
* @param fingerprints {@link Set} of IDs of the keys that will be included in the backup.
* @param password password that is used to symmetrically encrypt the backup.
2018-05-30 22:06:09 +02:00
* @return {@link SigncryptElement} containing the selected encrypted secret keys.
2018-05-28 00:58:13 +02:00
* @throws MissingOpenPgpKeyPairException if we don't have an OpenPGP key available.
2018-05-30 22:06:09 +02:00
* @throws SmackOpenPgpException if for some reason the key pair cannot be serialized.
2018-05-28 00:58:13 +02:00
*/
SecretkeyElement createSecretkeyElement(Set<OpenPgpV4Fingerprint> fingerprints, String password)
2018-05-30 22:06:09 +02:00
throws MissingOpenPgpKeyPairException, SmackOpenPgpException;
2018-05-28 00:58:13 +02:00
/**
* Decrypt a secret key backup and restore the key from it.
*
* @param secretkeyElement {@link SecretkeyElement} containing the backup.
* @param password password to decrypt the backup.
* @param callback {@link SecretKeyRestoreSelectionCallback} to let the user decide which key to restore.
2018-05-30 22:06:09 +02:00
* @throws SmackOpenPgpException if the selected key is corrupted and cannot be restored or our key ring
* is corrupted.
2018-05-28 00:58:13 +02:00
* @throws InvalidBackupCodeException if the user provided backup code is invalid.
*/
void restoreSecretKeyBackup(SecretkeyElement secretkeyElement, String password, SecretKeyRestoreSelectionCallback callback)
2018-05-30 22:06:09 +02:00
throws SmackOpenPgpException, InvalidBackupCodeException;
2018-05-28 00:58:13 +02:00
}