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    public PGPSecretKeyRing getSigningKeyRing() throws IOException, PGPException {
075        PGPSecretKeyRingCollection secretKeyRings = getSecretKeys();
076        if (secretKeyRings == null) {
077            return null;
078        }
079
080        PGPSecretKeyRing signingKeyRing = null;
081        for (PGPSecretKeyRing ring : secretKeyRings) {
082            if (signingKeyRing == null) {
083                signingKeyRing = ring;
084                continue;
085            }
086
087            if (ring.getPublicKey().getCreationTime().after(signingKeyRing.getPublicKey().getCreationTime())) {
088                signingKeyRing = ring;
089            }
090        }
091
092        return signingKeyRing;
093    }
094
095    /**
096     * Return the {@link OpenPgpV4Fingerprint} of our signing key.
097     * @return fingerprint of signing key
098     * @throws IOException IO is dangerous
099     * @throws PGPException PGP is brittle
100     */
101    public OpenPgpV4Fingerprint getSigningKeyFingerprint() throws IOException, PGPException {
102        PGPSecretKeyRing signingKeyRing = getSigningKeyRing();
103        return signingKeyRing != null ? new OpenPgpV4Fingerprint(signingKeyRing.getPublicKey()) : null;
104    }
105
106    /**
107     * Return a {@link PGPPublicKeyRingCollection} containing only the public keys belonging to our signing key ring.
108     * TODO: Add support for public keys of other devices of the owner.
109     *
110     * @return public keys
111     *
112     * @throws IOException IO is dangerous.
113     * @throws PGPException PGP is brittle.
114     */
115    @Override
116    public PGPPublicKeyRingCollection getAnnouncedPublicKeys() throws IOException, PGPException {
117        PGPSecretKeyRing secretKeys = getSigningKeyRing();
118        PGPPublicKeyRing publicKeys = getAnyPublicKeys().getPublicKeyRing(secretKeys.getPublicKey().getKeyID());
119        return new PGPPublicKeyRingCollection(Collections.singleton(publicKeys));
120    }
121}