aboutsummaryrefslogtreecommitdiffstats
path: root/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/CachedPublicKeyRing.java
blob: 8970d18ec11ac0344d603126dd7e2a33ad5a82dc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package org.sufficientlysecure.keychain.pgp;

import org.spongycastle.bcpg.ArmoredOutputStream;
import org.spongycastle.bcpg.SignatureSubpacketTags;
import org.spongycastle.openpgp.PGPException;
import org.spongycastle.openpgp.PGPPublicKey;
import org.spongycastle.openpgp.PGPPublicKeyRing;
import org.spongycastle.openpgp.PGPSignature;
import org.spongycastle.openpgp.PGPSignatureList;
import org.spongycastle.openpgp.PGPSignatureSubpacketVector;
import org.spongycastle.openpgp.operator.jcajce.JcaPGPContentVerifierBuilderProvider;
import org.sufficientlysecure.keychain.Constants;
import org.sufficientlysecure.keychain.pgp.exception.PgpGeneralException;
import org.sufficientlysecure.keychain.util.IterableIterator;

import java.io.IOException;
import java.security.SignatureException;
import java.util.Arrays;
import java.util.Iterator;

public class CachedPublicKeyRing extends CachedKeyRing {

    private PGPPublicKeyRing mRing;
    private final byte[] mPubKey;

    public CachedPublicKeyRing(long masterKeyId, int keySize, boolean isRevoked,
                                boolean canCertify, long creation, long expiry, int algorithm,
                                byte[] fingerprint, String userId, int verified, boolean hasSecret,
                                byte[] pubkey)
    {
        super(masterKeyId, canCertify, fingerprint, userId, verified, hasSecret);

        mPubKey = pubkey;
    }

    PGPPublicKeyRing getRing() {
        if(mRing == null) {
            mRing = (PGPPublicKeyRing) PgpConversionHelper.BytesToPGPKeyRing(mPubKey);
        }
        return mRing;
    }

    public void encode(ArmoredOutputStream stream) throws IOException {
        getRing().encode(stream);
    }

    public CachedPublicKey getSubkey() {
        return new CachedPublicKey(this, getRing().getPublicKey());
    }

    public CachedPublicKey getSubkey(long id) {
        return new CachedPublicKey(this, getRing().getPublicKey(id));
    }

    public CachedPublicKey getFirstSignSubkey() throws PgpGeneralException {
        // only return master key if no other signing key is available
        CachedPublicKey masterKey = null;
        for (PGPPublicKey k : new IterableIterator<PGPPublicKey>(getRing().getPublicKeys())) {
            CachedPublicKey key = new CachedPublicKey(this, k);
            if (key.isRevoked() || key.canSign() || key.isExpired()) {
                continue;
            }
            if (key.isMasterKey()) {
                masterKey = key;
            } else {
                return key;
            }
        }
        if(masterKey == null) {
            // TODO proper exception
            throw new PgpGeneralException("key not found");
        }
        return masterKey;
    }

    public CachedPublicKey getFirstEncryptSubkey() throws PgpGeneralException {
        // only return master key if no other encryption key is available
        CachedPublicKey masterKey = null;
        for (PGPPublicKey k : new IterableIterator<PGPPublicKey>(getRing().getPublicKeys())) {
            CachedPublicKey key = new CachedPublicKey(this, k);
            if (key.isRevoked() || key.canEncrypt() || key.isExpired()) {
                continue;
            }
            if (key.isMasterKey()) {
                masterKey = key;
            } else {
                return key;
            }
        }
        if(masterKey == null) {
            // TODO proper exception
            throw new PgpGeneralException("key not found");
        }
        return masterKey;
    }

    public boolean verifySubkeyBinding(CachedPublicKey cachedSubkey) {
        boolean validSubkeyBinding = false;
        boolean validTempSubkeyBinding = false;
        boolean validPrimaryKeyBinding = false;

        PGPPublicKey masterKey = getRing().getPublicKey();
        PGPPublicKey subKey = cachedSubkey.getKey();

        // Is this the master key? Match automatically, then.
        if(Arrays.equals(masterKey.getFingerprint(), subKey.getFingerprint())) {
            return true;
        }

        JcaPGPContentVerifierBuilderProvider contentVerifierBuilderProvider =
                new JcaPGPContentVerifierBuilderProvider()
                        .setProvider(Constants.BOUNCY_CASTLE_PROVIDER_NAME);

        Iterator<PGPSignature> itr = subKey.getSignatures();

        while (itr.hasNext()) { //what does gpg do if the subkey binding is wrong?
            //gpg has an invalid subkey binding error on key import I think, but doesn't shout
            //about keys without subkey signing. Can't get it to import a slightly broken one
            //either, so we will err on bad subkey binding here.
            PGPSignature sig = itr.next();
            if (sig.getKeyID() == masterKey.getKeyID() &&
                    sig.getSignatureType() == PGPSignature.SUBKEY_BINDING) {
                //check and if ok, check primary key binding.
                try {
                    sig.init(contentVerifierBuilderProvider, masterKey);
                    validTempSubkeyBinding = sig.verifyCertification(masterKey, subKey);
                } catch (PGPException e) {
                    continue;
                } catch (SignatureException e) {
                    continue;
                }

                if (validTempSubkeyBinding) {
                    validSubkeyBinding = true;
                }
                if (validTempSubkeyBinding) {
                    validPrimaryKeyBinding = verifyPrimaryKeyBinding(sig.getUnhashedSubPackets(),
                            masterKey, subKey);
                    if (validPrimaryKeyBinding) {
                        break;
                    }
                    validPrimaryKeyBinding = verifyPrimaryKeyBinding(sig.getHashedSubPackets(),
                            masterKey, subKey);
                    if (validPrimaryKeyBinding) {
                        break;
                    }
                }
            }
        }
        return validSubkeyBinding && validPrimaryKeyBinding;

    }

    static boolean verifyPrimaryKeyBinding(PGPSignatureSubpacketVector pkts,
                                            PGPPublicKey masterPublicKey,
                                            PGPPublicKey signingPublicKey) {
        boolean validPrimaryKeyBinding = false;
        JcaPGPContentVerifierBuilderProvider contentVerifierBuilderProvider =
                new JcaPGPContentVerifierBuilderProvider()
                        .setProvider(Constants.BOUNCY_CASTLE_PROVIDER_NAME);
        PGPSignatureList eSigList;

        if (pkts.hasSubpacket(SignatureSubpacketTags.EMBEDDED_SIGNATURE)) {
            try {
                eSigList = pkts.getEmbeddedSignatures();
            } catch (IOException e) {
                return false;
            } catch (PGPException e) {
                return false;
            }
            for (int j = 0; j < eSigList.size(); ++j) {
                PGPSignature emSig = eSigList.get(j);
                if (emSig.getSignatureType() == PGPSignature.PRIMARYKEY_BINDING) {
                    try {
                        emSig.init(contentVerifierBuilderProvider, signingPublicKey);
                        validPrimaryKeyBinding = emSig.verifyCertification(masterPublicKey, signingPublicKey);
                        if (validPrimaryKeyBinding) {
                            break;
                        }
                    } catch (PGPException e) {
                        continue;
                    } catch (SignatureException e) {
                        continue;
                    }
                }
            }
        }

        return validPrimaryKeyBinding;
    }

}