aboutsummaryrefslogtreecommitdiffstats
path: root/OpenKeychain/src/main/java/org/spongycastle/openpgp/operator/jcajce/NfcSyncPublicKeyDataDecryptorFactoryBuilder.java
blob: ffa1548769cc7e82372c27eda22bb6dcee8bd3f1 (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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
/**
 * Copyright (c) 2013-2014 Philipp Jakubeit, Signe Rüsch, Dominik Schürmann
 *
 * Licensed under the Bouncy Castle License (MIT license). See LICENSE file for details.
 */

package org.spongycastle.openpgp.operator.jcajce;

import org.spongycastle.bcpg.PublicKeyAlgorithmTags;
import org.spongycastle.jcajce.util.DefaultJcaJceHelper;
import org.spongycastle.jcajce.util.NamedJcaJceHelper;
import org.spongycastle.jcajce.util.ProviderJcaJceHelper;
import org.spongycastle.openpgp.PGPException;
import org.spongycastle.openpgp.PGPPublicKey;
import org.spongycastle.openpgp.operator.PGPDataDecryptor;
import org.spongycastle.openpgp.operator.PublicKeyDataDecryptorFactory;

import java.security.Provider;

/**
 * This class is based on JcePublicKeyDataDecryptorFactoryBuilder
 *
 */
public class NfcSyncPublicKeyDataDecryptorFactoryBuilder
{
    private OperatorHelper helper = new OperatorHelper(new DefaultJcaJceHelper());
    private OperatorHelper contentHelper = new OperatorHelper(new DefaultJcaJceHelper());
    private JcaPGPKeyConverter keyConverter = new JcaPGPKeyConverter();
//    private JcaPGPDigestCalculatorProviderBuilder digestCalculatorProviderBuilder = new JcaPGPDigestCalculatorProviderBuilder();
//    private JcaKeyFingerprintCalculator fingerprintCalculator = new JcaKeyFingerprintCalculator();

    public static class NfcInteractionNeeded extends RuntimeException
    {
        public byte[] encryptedSessionKey;

        public NfcInteractionNeeded(byte[] encryptedSessionKey)
        {
            super("NFC interaction required!");
            this.encryptedSessionKey = encryptedSessionKey;
        }
    }

    public NfcSyncPublicKeyDataDecryptorFactoryBuilder()
    {
    }

    /**
     * Set the provider object to use for creating cryptographic primitives in the resulting factory the builder produces.
     *
     * @param provider  provider object for cryptographic primitives.
     * @return  the current builder.
     */
    public NfcSyncPublicKeyDataDecryptorFactoryBuilder setProvider(Provider provider)
    {
        this.helper = new OperatorHelper(new ProviderJcaJceHelper(provider));
        keyConverter.setProvider(provider);
        this.contentHelper = helper;

        return this;
    }

    /**
     * Set the provider name to use for creating cryptographic primitives in the resulting factory the builder produces.
     *
     * @param providerName  the name of the provider to reference for cryptographic primitives.
     * @return  the current builder.
     */
    public NfcSyncPublicKeyDataDecryptorFactoryBuilder setProvider(String providerName)
    {
        this.helper = new OperatorHelper(new NamedJcaJceHelper(providerName));
        keyConverter.setProvider(providerName);
        this.contentHelper = helper;

        return this;
    }

    public NfcSyncPublicKeyDataDecryptorFactoryBuilder setContentProvider(Provider provider)
    {
        this.contentHelper = new OperatorHelper(new ProviderJcaJceHelper(provider));

        return this;
    }

    public NfcSyncPublicKeyDataDecryptorFactoryBuilder setContentProvider(String providerName)
    {
        this.contentHelper = new OperatorHelper(new NamedJcaJceHelper(providerName));

        return this;
    }

    public PublicKeyDataDecryptorFactory build(final byte[] nfcDecrypted) {
        return new PublicKeyDataDecryptorFactory()
        {
            public byte[] recoverSessionData(int keyAlgorithm, byte[][] secKeyData)
                    throws PGPException
            {
                if (keyAlgorithm == PublicKeyAlgorithmTags.ECDH)
                {
                    throw new PGPException("ECDH not supported!");
                }

                return decryptSessionData(keyAlgorithm, secKeyData, nfcDecrypted);
            }

            public PGPDataDecryptor createDataDecryptor(boolean withIntegrityPacket, int encAlgorithm, byte[] key)
                    throws PGPException
            {
                return contentHelper.createDataDecryptor(withIntegrityPacket, encAlgorithm, key);
            }
        };
    }

//    public PublicKeyDataDecryptorFactory build(final PrivateKey privKey)
//    {
//        return new PublicKeyDataDecryptorFactory()
//        {
//            public byte[] recoverSessionData(int keyAlgorithm, byte[][] secKeyData)
//                    throws PGPException
//            {
//                if (keyAlgorithm == PublicKeyAlgorithmTags.ECDH)
//                {
//                    throw new PGPException("ECDH requires use of PGPPrivateKey for decryption");
//                }
//                return decryptSessionData(keyAlgorithm, privKey, secKeyData);
//            }
//
//            public PGPDataDecryptor createDataDecryptor(boolean withIntegrityPacket, int encAlgorithm, byte[] key)
//                    throws PGPException
//            {
//                return contentHelper.createDataDecryptor(withIntegrityPacket, encAlgorithm, key);
//            }
//        };
//    }

//    public PublicKeyDataDecryptorFactory build(final PGPPrivateKey privKey, final byte[] nfcDecrypted)
//    {
//        return new PublicKeyDataDecryptorFactory()
//        {
//            public byte[] recoverSessionData(int keyAlgorithm, byte[][] secKeyData)
//                    throws PGPException
//            {
//                if (keyAlgorithm == PublicKeyAlgorithmTags.ECDH)
//                {
//                    return decryptSessionData(privKey.getPrivateKeyDataPacket(), privKey.getPublicKeyPacket(), secKeyData);
//                }
//
//                return decryptSessionData(keyAlgorithm, keyConverter.getPrivateKey(privKey), secKeyData, nfcDecrypted);
//            }
//
//            public PGPDataDecryptor createDataDecryptor(boolean withIntegrityPacket, int encAlgorithm, byte[] key)
//                    throws PGPException
//            {
//                return contentHelper.createDataDecryptor(withIntegrityPacket, encAlgorithm, key);
//            }
//        };
//    }

//    private byte[] decryptSessionData(BCPGKey privateKeyPacket, PublicKeyPacket pubKeyData, byte[][] secKeyData)
//            throws PGPException
//    {
//        ECDHPublicBCPGKey ecKey = (ECDHPublicBCPGKey)pubKeyData.getKey();
//        X9ECParameters x9Params = NISTNamedCurves.getByOID(ecKey.getCurveOID());
//
//        byte[] enc = secKeyData[0];
//
//        int pLen = ((((enc[0] & 0xff) << 8) + (enc[1] & 0xff)) + 7) / 8;
//        byte[] pEnc = new byte[pLen];
//
//        System.arraycopy(enc, 2, pEnc, 0, pLen);
//
//        byte[] keyEnc = new byte[enc[pLen + 2]];
//
//        System.arraycopy(enc, 2 + pLen + 1, keyEnc, 0, keyEnc.length);
//
//        Cipher c = helper.createKeyWrapper(ecKey.getSymmetricKeyAlgorithm());
//
//        ECPoint S = x9Params.getCurve().decodePoint(pEnc).multiply(((ECSecretBCPGKey)privateKeyPacket).getX()).normalize();
//
//        RFC6637KDFCalculator rfc6637KDFCalculator = new RFC6637KDFCalculator(digestCalculatorProviderBuilder.build().get(ecKey.getHashAlgorithm()), ecKey.getSymmetricKeyAlgorithm());
//        Key key = new SecretKeySpec(rfc6637KDFCalculator.createKey(ecKey.getCurveOID(), S, fingerprintCalculator.calculateFingerprint(pubKeyData)), "AESWrap");
//
//        try
//        {
//            c.init(Cipher.UNWRAP_MODE, key);
//
//            Key paddedSessionKey = c.unwrap(keyEnc, "Session", Cipher.SECRET_KEY);
//
//            return PGPPad.unpadSessionData(paddedSessionKey.getEncoded());
//        }
//        catch (InvalidKeyException e)
//        {
//            throw new PGPException("error setting asymmetric cipher", e);
//        }
//        catch (NoSuchAlgorithmException e)
//        {
//            throw new PGPException("error setting asymmetric cipher", e);
//        }
//    }

    private byte[] decryptSessionData(int keyAlgorithm, byte[][] secKeyData, byte[] nfcDecrypted)
            throws PGPException
    {
//        Cipher c1 = helper.createPublicKeyCipher(keyAlgorithm);
//
//        try
//        {
//            c1.init(Cipher.DECRYPT_MODE, privKey);
//        }
//        catch (InvalidKeyException e)
//        {
//            throw new PGPException("error setting asymmetric cipher", e);
//        }

        if (keyAlgorithm == PGPPublicKey.RSA_ENCRYPT
                || keyAlgorithm == PGPPublicKey.RSA_GENERAL)
        {
            byte[] bi = secKeyData[0];  // encoded MPI

            if (nfcDecrypted != null) {
                // we already have the decrypted bytes from a previous execution, return this!
                return nfcDecrypted;
            } else {
                // catch this when decryptSessionData() is executed and divert digest to card,
                // when doing the operation again reuse nfcDecrypted
                throw new NfcInteractionNeeded(bi);
            }

//            c1.update(bi, 2, bi.length - 2);
        }
        else
        {
            throw new PGPException("ElGamal not supported!");

//            ElGamalKey k = (ElGamalKey)privKey;
//            int size = (k.getParameters().getP().bitLength() + 7) / 8;
//            byte[] tmp = new byte[size];
//
//            byte[] bi = secKeyData[0]; // encoded MPI
//            if (bi.length - 2 > size)  // leading Zero? Shouldn't happen but...
//            {
//                c1.update(bi, 3, bi.length - 3);
//            }
//            else
//            {
//                System.arraycopy(bi, 2, tmp, tmp.length - (bi.length - 2), bi.length - 2);
//                c1.update(tmp);
//            }
//
//            bi = secKeyData[1];  // encoded MPI
//            for (int i = 0; i != tmp.length; i++)
//            {
//                tmp[i] = 0;
//            }
//
//            if (bi.length - 2 > size) // leading Zero? Shouldn't happen but...
//            {
//                c1.update(bi, 3, bi.length - 3);
//            }
//            else
//            {
//                System.arraycopy(bi, 2, tmp, tmp.length - (bi.length - 2), bi.length - 2);
//                c1.update(tmp);
//            }
        }

//        try
//        {
//            return c1.doFinal();
//        }
//        catch (Exception e)
//        {
//            throw new PGPException("exception decrypting session data", e);
//        }
    }
}