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;
031import org.pgpainless.util.BCUtil;
032
033/**
034 * This class acts as our own OpenPGP identity. It can be seen as a special view on the {@link OpenPgpStore}, giving
035 * access to our own encryption keys etc.
036 */
037public class OpenPgpSelf extends OpenPgpContact {
038
039    /**
040     * Constructor.
041     *
042     * @param jid our own {@link BareJid}. This is needed to access our keys in the store.
043     * @param store the store.
044     */
045    OpenPgpSelf(BareJid jid, OpenPgpStore store) {
046        super(jid, store);
047    }
048
049    /**
050     * Return true, if we have a usable secret key available.
051     * @return true if we have secret key, otherwise false.
052     * @throws IOException IO is dangerous
053     * @throws PGPException PGP is brittle
054     */
055    public boolean hasSecretKeyAvailable() throws IOException, PGPException {
056        return getSecretKeys() != null;
057    }
058
059    /**
060     * Return a {@link PGPSecretKeyRingCollection} which contains all of our {@link PGPSecretKeyRing}s.
061     * @return collection of our secret keys
062     * @throws IOException IO is dangerous
063     * @throws PGPException PGP is brittle
064     */
065    public PGPSecretKeyRingCollection getSecretKeys() throws IOException, PGPException {
066        return store.getSecretKeysOf(jid);
067    }
068
069    /**
070     * Return the {@link PGPSecretKeyRing} which we will use to sign our messages.
071     * @return signing key
072     * @throws IOException IO is dangerous
073     * @throws PGPException PGP is brittle
074     */
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        publicKeys = BCUtil.removeUnassociatedKeysFromKeyRing(publicKeys, secretKeys.getPublicKey());
121        return new PGPPublicKeyRingCollection(Collections.singleton(publicKeys));
122    }
123}