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
033public class OpenPgpSelf extends OpenPgpContact {
034
035    OpenPgpSelf(BareJid jid, OpenPgpStore store) {
036        super(jid, store);
037    }
038
039    /**
040     * Return true, if we have a usable secret key available.
041     * @return true if we have secret key, otherwise false.
042     * @throws IOException IO is dangerous
043     * @throws PGPException PGP is brittle
044     */
045    public boolean hasSecretKeyAvailable() throws IOException, PGPException {
046        return getSecretKeys() != null;
047    }
048
049    /**
050     * Return a {@link PGPSecretKeyRingCollection} which contains all of our {@link PGPSecretKeyRing}s.
051     * @return collection of our secret keys
052     * @throws IOException IO is dangerous
053     * @throws PGPException PGP is brittle
054     */
055    public PGPSecretKeyRingCollection getSecretKeys() throws IOException, PGPException {
056        return store.getSecretKeysOf(jid);
057    }
058
059    /**
060     * Return the {@link PGPSecretKeyRing} which we will use to sign our messages.
061     * @return signing key
062     * @throws IOException IO is dangerous
063     * @throws PGPException PGP is brittle
064     */
065    public PGPSecretKeyRing getSigningKeyRing() throws IOException, PGPException {
066        PGPSecretKeyRingCollection secretKeyRings = getSecretKeys();
067        if (secretKeyRings == null) {
068            return null;
069        }
070
071        PGPSecretKeyRing signingKeyRing = null;
072        for (PGPSecretKeyRing ring : secretKeyRings) {
073            if (signingKeyRing == null) {
074                signingKeyRing = ring;
075                continue;
076            }
077
078            if (ring.getPublicKey().getCreationTime().after(signingKeyRing.getPublicKey().getCreationTime())) {
079                signingKeyRing = ring;
080            }
081        }
082
083        return signingKeyRing;
084    }
085
086    /**
087     * Return the {@link OpenPgpV4Fingerprint} of our signing key.
088     * @return fingerprint of signing key
089     * @throws IOException IO is dangerous
090     * @throws PGPException PGP is brittle
091     */
092    public OpenPgpV4Fingerprint getSigningKeyFingerprint() throws IOException, PGPException {
093        PGPSecretKeyRing signingKeyRing = getSigningKeyRing();
094        return signingKeyRing != null ? new OpenPgpV4Fingerprint(signingKeyRing.getPublicKey()) : null;
095    }
096
097    /**
098     * Return a {@link PGPPublicKeyRingCollection} containing only the public keys belonging to our signing key ring.
099     * TODO: Add support for public keys of other devices of the owner.
100     *
101     * @return public keys
102     *
103     * @throws IOException IO is dangerous.
104     * @throws PGPException PGP is brittle.
105     */
106    @Override
107    public PGPPublicKeyRingCollection getAnnouncedPublicKeys() throws IOException, PGPException {
108        PGPSecretKeyRing secretKeys = getSigningKeyRing();
109        PGPPublicKeyRing publicKeys = getAnyPublicKeys().getPublicKeyRing(secretKeys.getPublicKey().getKeyID());
110        publicKeys = BCUtil.removeUnassociatedKeysFromKeyRing(publicKeys, secretKeys.getPublicKey());
111        return new PGPPublicKeyRingCollection(Collections.singleton(publicKeys));
112    }
113}