aboutsummaryrefslogtreecommitdiffstats
path: root/OpenKeychain/src/main/java/org/bouncycastle/openpgp/operator/jcajce/NfcSyncPGPContentSignerBuilder.java
diff options
context:
space:
mode:
authorVincent Breitmoser <valodim@mugenguild.com>2016-02-10 17:36:02 +0100
committerVincent Breitmoser <valodim@mugenguild.com>2016-02-10 17:36:02 +0100
commitda6dfb57a0aa816919cd23752e1707ba6a2e8cae (patch)
treed6ede03785491d4c56b10cad9062b8bcac3ea78b /OpenKeychain/src/main/java/org/bouncycastle/openpgp/operator/jcajce/NfcSyncPGPContentSignerBuilder.java
parent01b165ea88a032f31b8c2ff07351d3f893f6413d (diff)
parent751298a4d832f316244fd6345c46ba806dcfc860 (diff)
downloadopen-keychain-da6dfb57a0aa816919cd23752e1707ba6a2e8cae.tar.gz
open-keychain-da6dfb57a0aa816919cd23752e1707ba6a2e8cae.tar.bz2
open-keychain-da6dfb57a0aa816919cd23752e1707ba6a2e8cae.zip
Merge branch 'master' into performance
Diffstat (limited to 'OpenKeychain/src/main/java/org/bouncycastle/openpgp/operator/jcajce/NfcSyncPGPContentSignerBuilder.java')
-rw-r--r--OpenKeychain/src/main/java/org/bouncycastle/openpgp/operator/jcajce/NfcSyncPGPContentSignerBuilder.java144
1 files changed, 144 insertions, 0 deletions
diff --git a/OpenKeychain/src/main/java/org/bouncycastle/openpgp/operator/jcajce/NfcSyncPGPContentSignerBuilder.java b/OpenKeychain/src/main/java/org/bouncycastle/openpgp/operator/jcajce/NfcSyncPGPContentSignerBuilder.java
new file mode 100644
index 000000000..584d86891
--- /dev/null
+++ b/OpenKeychain/src/main/java/org/bouncycastle/openpgp/operator/jcajce/NfcSyncPGPContentSignerBuilder.java
@@ -0,0 +1,144 @@
+/**
+ * Copyright (c) 2013-2014 Philipp Jakubeit, Signe Rüsch, Dominik Schürmann
+ * Copyright (c) 2000-2013 The Legion of the Bouncy Castle Inc. (http://www.bouncycastle.org)
+ *
+ * Licensed under the Bouncy Castle License (MIT license). See LICENSE file for details.
+ */
+
+package org.bouncycastle.openpgp.operator.jcajce;
+
+import org.bouncycastle.openpgp.PGPException;
+import org.bouncycastle.openpgp.PGPPrivateKey;
+import org.bouncycastle.openpgp.operator.PGPContentSigner;
+import org.bouncycastle.openpgp.operator.PGPContentSignerBuilder;
+import org.bouncycastle.openpgp.operator.PGPDigestCalculator;
+
+import java.io.OutputStream;
+import java.nio.ByteBuffer;
+import java.security.Provider;
+import java.util.Date;
+import java.util.HashMap;
+import java.util.Map;
+
+
+/**
+ * This class is based on JcaPGPContentSignerBuilder.
+ *
+ * Instead of using a Signature object based on a privateKey, this class only calculates the digest
+ * of the output stream and gives the result back using a RuntimeException.
+ */
+public class NfcSyncPGPContentSignerBuilder
+ implements PGPContentSignerBuilder
+{
+ private JcaPGPDigestCalculatorProviderBuilder digestCalculatorProviderBuilder = new JcaPGPDigestCalculatorProviderBuilder();
+ private int hashAlgorithm;
+ private int keyAlgorithm;
+ private long keyID;
+
+ private Map signedHashes;
+
+ public static class NfcInteractionNeeded extends RuntimeException
+ {
+ public byte[] hashToSign;
+ public int hashAlgo;
+
+ public NfcInteractionNeeded(byte[] hashToSign, int hashAlgo)
+ {
+ super("NFC interaction required!");
+ this.hashToSign = hashToSign;
+ this.hashAlgo = hashAlgo;
+ }
+ }
+
+ public NfcSyncPGPContentSignerBuilder(int keyAlgorithm, int hashAlgorithm, long keyID, Map signedHashes)
+ {
+ this.keyAlgorithm = keyAlgorithm;
+ this.hashAlgorithm = hashAlgorithm;
+ this.keyID = keyID;
+ this.signedHashes = signedHashes;
+ }
+
+ public NfcSyncPGPContentSignerBuilder setProvider(Provider provider)
+ {
+ digestCalculatorProviderBuilder.setProvider(provider);
+
+ return this;
+ }
+
+ public NfcSyncPGPContentSignerBuilder setProvider(String providerName)
+ {
+ digestCalculatorProviderBuilder.setProvider(providerName);
+
+ return this;
+ }
+
+ public NfcSyncPGPContentSignerBuilder setDigestProvider(Provider provider)
+ {
+ digestCalculatorProviderBuilder.setProvider(provider);
+
+ return this;
+ }
+
+ public NfcSyncPGPContentSignerBuilder setDigestProvider(String providerName)
+ {
+ digestCalculatorProviderBuilder.setProvider(providerName);
+
+ return this;
+ }
+
+ public PGPContentSigner build(final int signatureType, PGPPrivateKey privateKey)
+ throws PGPException {
+ // NOTE: privateKey is null in this case!
+ return build(signatureType, keyID);
+ }
+
+ public PGPContentSigner build(final int signatureType, final long keyID)
+ throws PGPException
+ {
+ final PGPDigestCalculator digestCalculator = digestCalculatorProviderBuilder.build().get(hashAlgorithm);
+
+ return new PGPContentSigner()
+ {
+ public int getType()
+ {
+ return signatureType;
+ }
+
+ public int getHashAlgorithm()
+ {
+ return hashAlgorithm;
+ }
+
+ public int getKeyAlgorithm()
+ {
+ return keyAlgorithm;
+ }
+
+ public long getKeyID()
+ {
+ return keyID;
+ }
+
+ public OutputStream getOutputStream()
+ {
+ return digestCalculator.getOutputStream();
+ }
+
+ public byte[] getSignature() {
+ byte[] digest = digestCalculator.getDigest();
+ ByteBuffer buf = ByteBuffer.wrap(digest);
+ if (signedHashes.containsKey(buf)) {
+ return (byte[]) signedHashes.get(buf);
+ }
+ // catch this when signatureGenerator.generate() is executed and divert digest to card,
+ // when doing the operation again reuse creationTimestamp (this will be hashed)
+ throw new NfcInteractionNeeded(digest, getHashAlgorithm());
+ }
+
+ public byte[] getDigest()
+ {
+ return digestCalculator.getDigest();
+ }
+ };
+ }
+}