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;
018
019import java.io.IOException;
020import java.util.Collections;
021
022import org.jivesoftware.smackx.ox.store.definition.OpenPgpStore;
023
024import org.bouncycastle.openpgp.PGPException;
025import org.bouncycastle.openpgp.PGPPublicKeyRing;
026import org.bouncycastle.openpgp.PGPPublicKeyRingCollection;
027import org.bouncycastle.openpgp.PGPSecretKeyRing;
028import org.bouncycastle.openpgp.PGPSecretKeyRingCollection;
029import org.jxmpp.jid.BareJid;
030import org.pgpainless.key.OpenPgpV4Fingerprint;
031
032/**
033 * This class acts as our own OpenPGP identity. It can be seen as a special view on the {@link OpenPgpStore}, giving
034 * access to our own encryption keys etc.
035 */
036public class OpenPgpSelf extends OpenPgpContact {
037
038    /**
039     * Constructor.
040     *
041     * @param jid our own {@link BareJid}. This is needed to access our keys in the store.
042     * @param store the store.
043     */
044    OpenPgpSelf(BareJid jid, OpenPgpStore store) {
045        super(jid, store);
046    }
047
048    /**
049     * Return true, if we have a usable secret key available.
050     * @return true if we have secret key, otherwise false.
051     * @throws IOException IO is dangerous
052     * @throws PGPException PGP is brittle
053     */
054    public boolean hasSecretKeyAvailable() throws IOException, PGPException {
055        return getSecretKeys() != null;
056    }
057
058    /**
059     * Return a {@link PGPSecretKeyRingCollection} which contains all of our {@link PGPSecretKeyRing}s.
060     * @return collection of our secret keys
061     * @throws IOException IO is dangerous
062     * @throws PGPException PGP is brittle
063     */
064    public PGPSecretKeyRingCollection getSecretKeys() throws IOException, PGPException {
065        return store.getSecretKeysOf(jid);
066    }
067
068    /**
069     * Return the {@link PGPSecretKeyRing} which we will use to sign our messages.
070     * @return signing key
071     * @throws IOException IO is dangerous
072     * @throws PGPException PGP is brittle
073     */
074    @SuppressWarnings("JavaUtilDate")
075    public PGPSecretKeyRing getSigningKeyRing() throws IOException, PGPException {
076        PGPSecretKeyRingCollection secretKeyRings = getSecretKeys();
077        if (secretKeyRings == null) {
078            return null;
079        }
080
081        PGPSecretKeyRing signingKeyRing = null;
082        for (PGPSecretKeyRing ring : secretKeyRings) {
083            if (signingKeyRing == null) {
084                signingKeyRing = ring;
085                continue;
086            }
087
088            if (ring.getPublicKey().getCreationTime().after(signingKeyRing.getPublicKey().getCreationTime())) {
089                signingKeyRing = ring;
090            }
091        }
092
093        return signingKeyRing;
094    }
095
096    /**
097     * Return the {@link OpenPgpV4Fingerprint} of our signing key.
098     * @return fingerprint of signing key
099     * @throws IOException IO is dangerous
100     * @throws PGPException PGP is brittle
101     */
102    public OpenPgpV4Fingerprint getSigningKeyFingerprint() throws IOException, PGPException {
103        PGPSecretKeyRing signingKeyRing = getSigningKeyRing();
104        return signingKeyRing != null ? new OpenPgpV4Fingerprint(signingKeyRing.getPublicKey()) : null;
105    }
106
107    /**
108     * Return a {@link PGPPublicKeyRingCollection} containing only the public keys belonging to our signing key ring.
109     * TODO: Add support for public keys of other devices of the owner.
110     *
111     * @return public keys
112     *
113     * @throws IOException IO is dangerous.
114     * @throws PGPException PGP is brittle.
115     */
116    @Override
117    public PGPPublicKeyRingCollection getAnnouncedPublicKeys() throws IOException, PGPException {
118        PGPSecretKeyRing secretKeys = getSigningKeyRing();
119        PGPPublicKeyRing publicKeys = getAnyPublicKeys().getPublicKeyRing(secretKeys.getPublicKey().getKeyID());
120        return new PGPPublicKeyRingCollection(Collections.singleton(publicKeys));
121    }
122}