From 6e5f5405a2dd457aa2d46df2c22978229bbf536a Mon Sep 17 00:00:00 2001 From: Christian Hagau Date: Sun, 26 Nov 2017 00:00:00 +0000 Subject: [PATCH 1/5] Return encoded signature blob instead of a raw signature in SshAuthenticationService --- .../keychain/pgp/SshPublicKey.java | 38 +---- .../remote/SshAuthenticationService.java | 31 +++- .../ssh/signature/SshSignatureConverter.java | 138 ++++++++++++++++++ .../keychain/ssh/utils/SshUtils.java | 57 ++++++++ 4 files changed, 226 insertions(+), 38 deletions(-) create mode 100644 OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ssh/signature/SshSignatureConverter.java create mode 100644 OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ssh/utils/SshUtils.java diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/SshPublicKey.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/SshPublicKey.java index 94a20ba21..1bdc2774d 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/SshPublicKey.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/SshPublicKey.java @@ -27,6 +27,7 @@ import org.sufficientlysecure.keychain.ssh.key.SshDSAPublicKey; import org.sufficientlysecure.keychain.ssh.key.SshECDSAPublicKey; import org.sufficientlysecure.keychain.ssh.key.SshEd25519PublicKey; import org.sufficientlysecure.keychain.ssh.key.SshRSAPublicKey; +import org.sufficientlysecure.keychain.ssh.utils.SshUtils; public class SshPublicKey { private final static String TAG = "SshPublicKey"; @@ -66,48 +67,13 @@ public class SshPublicKey { private String encodeECKey(PGPPublicKey publicKey) { ECPublicBCPGKey publicBCPGKey = (ECPublicBCPGKey) publicKey.getPublicKeyPacket().getKey(); - String curveName = getCurveName(publicBCPGKey); + String curveName = SshUtils.getCurveName(mPublicKey.getCurveOid()); SshECDSAPublicKey sshECDSAPublicKey = new SshECDSAPublicKey(curveName, publicBCPGKey.getEncodedPoint()); return sshECDSAPublicKey.getPublicKeyBlob(); } - private String getCurveName(ECPublicBCPGKey publicBCPGKey) { - String curveOid = publicBCPGKey.getCurveOID().getId(); - // see RFC5656 section 10.{1,2} - switch (curveOid) { - // REQUIRED curves - case "1.2.840.10045.3.1.7": - return "nistp256"; - case "1.3.132.0.34": - return "nistp384"; - case "1.3.132.0.35": - return "nistp521"; - // RECOMMENDED curves - case "1.3.132.0.1": - return "1.3.132.0.1"; - case "1.2.840.10045.3.1.1": - return "1.2.840.10045.3.1.1"; - case "1.3.132.0.33": - return "1.3.132.0.33"; - case "1.3.132.0.26": - return "1.3.132.0.26"; - case "1.3.132.0.27": - return "1.3.132.0.27"; - case "1.3.132.0.16": - return "1.3.132.0.16"; - case "1.3.132.0.36": - return "1.3.132.0.36"; - case "1.3.132.0.37": - return "1.3.132.0.37"; - case "1.3.132.0.38": - return "1.3.132.0.38"; - - default: - return null; - } - } private String encodeEdDSAKey(PGPPublicKey publicKey) { EdDSAPublicBCPGKey publicBCPGKey = (EdDSAPublicBCPGKey) publicKey.getPublicKeyPacket().getKey(); diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/remote/SshAuthenticationService.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/remote/SshAuthenticationService.java index 4ef16eb72..8933b9f73 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/remote/SshAuthenticationService.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/remote/SshAuthenticationService.java @@ -23,6 +23,7 @@ import android.content.Intent; import android.os.IBinder; import android.util.Log; import org.bouncycastle.bcpg.HashAlgorithmTags; +import org.bouncycastle.bcpg.PublicKeyAlgorithmTags; import org.openintents.ssh.authentication.ISshAuthenticationService; import org.openintents.ssh.authentication.SshAuthenticationApi; import org.openintents.ssh.authentication.SshAuthenticationApiError; @@ -46,10 +47,14 @@ import org.sufficientlysecure.keychain.ssh.AuthenticationData; import org.sufficientlysecure.keychain.ssh.AuthenticationOperation; import org.sufficientlysecure.keychain.ssh.AuthenticationParcel; import org.sufficientlysecure.keychain.ssh.AuthenticationResult; +import org.sufficientlysecure.keychain.ssh.signature.SshSignatureConverter; import java.security.NoSuchAlgorithmException; import java.security.PublicKey; -import java.util.*; +import java.util.Collections; +import java.util.Date; +import java.util.HashSet; +import java.util.List; public class SshAuthenticationService extends Service { @@ -141,12 +146,22 @@ public class SshAuthenticationService extends Service { CachedPublicKeyRing cachedPublicKeyRing = mKeyRepository.getCachedPublicKeyRing(masterKeyId); long authSubKeyId; + int authSubKeyAlgorithm; + String authSubKeyCurveOid = null; try { // get first usable subkey capable of authentication authSubKeyId = cachedPublicKeyRing.getSecretAuthenticationId(); + // needed for encoding the resulting signature + authSubKeyAlgorithm = getPublicKey(masterKeyId).getAlgorithm(); + if (authSubKeyAlgorithm == PublicKeyAlgorithmTags.ECDSA) { + authSubKeyCurveOid = getPublicKey(masterKeyId).getCurveOid(); + } } catch (PgpKeyNotFoundException e) { return createExceptionErrorResult(SshAuthenticationApiError.NO_AUTH_KEY, "authentication key for master key id not found in keychain", e); + } catch (KeyRepository.NotFoundException e) { + return createExceptionErrorResult(SshAuthenticationApiError.NO_SUCH_KEY, + "Key for master key id not found", e); } authData.setAuthenticationSubKeyId(authSubKeyId); @@ -175,7 +190,19 @@ public class SshAuthenticationService extends Service { // return PendingIntent to be executed by client return packagePendingIntent(pi); } else if (authResult.success()) { - return new SigningResponse(authResult.getSignature()).toIntent(); + byte[] rawSignature = authResult.getSignature(); + byte[] sshSignature; + if (authSubKeyAlgorithm == PublicKeyAlgorithmTags.ECDSA) { + sshSignature = SshSignatureConverter.getSshSignatureEcDsa(rawSignature, authSubKeyCurveOid); + } else { + try { + sshSignature = SshSignatureConverter.getSshSignature(rawSignature, authSubKeyAlgorithm); + } catch (NoSuchAlgorithmException e) { + return createExceptionErrorResult(SshAuthenticationApiError.INTERNAL_ERROR, + "Error converting signature", e); + } + } + return new SigningResponse(sshSignature).toIntent(); } else { LogEntryParcel errorMsg = authResult.getLog().getLast(); return createErrorResult(SshAuthenticationApiError.INTERNAL_ERROR, getString(errorMsg.mType.getMsgId())); diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ssh/signature/SshSignatureConverter.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ssh/signature/SshSignatureConverter.java new file mode 100644 index 000000000..f08a0da31 --- /dev/null +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ssh/signature/SshSignatureConverter.java @@ -0,0 +1,138 @@ +/* + * Copyright (C) 2017 Christian Hagau + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package org.sufficientlysecure.keychain.ssh.signature; + +import org.bouncycastle.asn1.ASN1Integer; +import org.bouncycastle.asn1.ASN1Primitive; +import org.bouncycastle.asn1.ASN1Sequence; +import org.bouncycastle.bcpg.PublicKeyAlgorithmTags; +import org.bouncycastle.util.BigIntegers; +import org.sufficientlysecure.keychain.ssh.key.SshEncodedData; +import org.sufficientlysecure.keychain.ssh.utils.SshUtils; + +import java.io.IOException; +import java.math.BigInteger; +import java.security.NoSuchAlgorithmException; + +public class SshSignatureConverter { + + private static String getSignatureType(int algorithm) throws NoSuchAlgorithmException { + switch (algorithm) { + case PublicKeyAlgorithmTags.RSA_SIGN: + case PublicKeyAlgorithmTags.RSA_GENERAL: + return "ssh-rsa"; + + case PublicKeyAlgorithmTags.EDDSA: + return "ssh-ed25519"; + + case PublicKeyAlgorithmTags.DSA: + return "ssh-dss"; + + default: + throw new NoSuchAlgorithmException("Unknown algorithm"); + } + } + + private static byte[] getSignatureBlob(byte[] rawSignature, int algorithm) throws NoSuchAlgorithmException { + switch (algorithm) { + case PublicKeyAlgorithmTags.RSA_SIGN: + case PublicKeyAlgorithmTags.RSA_GENERAL: + return rawSignature; + + case PublicKeyAlgorithmTags.EDDSA: + return rawSignature; + + case PublicKeyAlgorithmTags.DSA: + return getDsaSignatureBlob(rawSignature); + + default: + throw new NoSuchAlgorithmException("Unknown algorithm"); + } + } + + private static byte[] getEcDsaSignatureBlob(byte[] rawSignature) { + BigInteger r = getR(rawSignature); + BigInteger s = getS(rawSignature); + + SshEncodedData rsBlob = new SshEncodedData(); + rsBlob.putMPInt(r); + rsBlob.putMPInt(s); + + return rsBlob.getBytes(); + } + + private static BigInteger getR(byte[] rawSignature) { + ASN1Sequence asn1Sequence = getASN1Sequence(rawSignature); + return ASN1Integer.getInstance(asn1Sequence.getObjectAt(0)).getValue(); + } + + private static BigInteger getS(byte[] rawSignature) { + ASN1Sequence asn1Sequence = getASN1Sequence(rawSignature); + return ASN1Integer.getInstance(asn1Sequence.getObjectAt(1)).getValue(); + } + + private static ASN1Sequence getASN1Sequence(byte[] rawSignature) { + try { + return (ASN1Sequence) ASN1Primitive.fromByteArray(rawSignature); + } catch (IOException e) { + throw new IllegalArgumentException("Could not read ASN.1 object", e); + } + } + + private static byte[] getDsaSignatureBlob(byte[] rawSignature) { + BigInteger r = getR(rawSignature); + BigInteger s = getS(rawSignature); + + byte[] rByte = BigIntegers.asUnsignedByteArray(r); + byte[] sByte = BigIntegers.asUnsignedByteArray(s); + + int integerLength = getDsaSignatureLength(rByte.length > sByte.length ? rByte.length : sByte.length); + int rPaddingLength = integerLength - rByte.length; + int sPaddingLength = integerLength - sByte.length; + + byte[] rsBlob = new byte[2 * integerLength]; + System.arraycopy(rByte, 0, rsBlob, rPaddingLength, rByte.length); + System.arraycopy(sByte, 0, rsBlob, integerLength + sPaddingLength, sByte.length); + + return rsBlob; + } + + private static int getDsaSignatureLength(int inLength) { + if (inLength <= 20) { + return 20; + } else { + return 32; + } + } + + public static byte[] getSshSignature(byte[] rawSignature, int algorithm) throws NoSuchAlgorithmException { + SshEncodedData signature = new SshEncodedData(); + signature.putString(getSignatureType(algorithm)); + signature.putString(getSignatureBlob(rawSignature, algorithm)); + + return signature.getBytes(); + } + + public static byte[] getSshSignatureEcDsa(byte[] rawSignature, String curveOid) { + SshEncodedData signature = new SshEncodedData(); + signature.putString("ecdsa-sha2-" + SshUtils.getCurveName(curveOid)); + signature.putString(getEcDsaSignatureBlob(rawSignature)); + + return signature.getBytes(); + } +} diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ssh/utils/SshUtils.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ssh/utils/SshUtils.java new file mode 100644 index 000000000..5869a2902 --- /dev/null +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ssh/utils/SshUtils.java @@ -0,0 +1,57 @@ +/* + * Copyright (C) 2017 Christian Hagau + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package org.sufficientlysecure.keychain.ssh.utils; + +public class SshUtils { + + public static String getCurveName(String curveOid) { + // see RFC5656 section 10.{1,2} + switch (curveOid) { + // REQUIRED curves + case "1.2.840.10045.3.1.7": + return "nistp256"; + case "1.3.132.0.34": + return "nistp384"; + case "1.3.132.0.35": + return "nistp521"; + + // RECOMMENDED curves + case "1.3.132.0.1": + return "1.3.132.0.1"; + case "1.2.840.10045.3.1.1": + return "1.2.840.10045.3.1.1"; + case "1.3.132.0.33": + return "1.3.132.0.33"; + case "1.3.132.0.26": + return "1.3.132.0.26"; + case "1.3.132.0.27": + return "1.3.132.0.27"; + case "1.3.132.0.16": + return "1.3.132.0.16"; + case "1.3.132.0.36": + return "1.3.132.0.36"; + case "1.3.132.0.37": + return "1.3.132.0.37"; + case "1.3.132.0.38": + return "1.3.132.0.38"; + + default: + return null; + } + } +} From d4c9f69696344f452f1d9786a2a1eb3f73dbb34b Mon Sep 17 00:00:00 2001 From: Christian Hagau Date: Mon, 27 Nov 2017 00:00:00 +0000 Subject: [PATCH 2/5] Update sshauthentication-api javadoc with regard to signature encoding --- .../openintents/ssh/authentication/SshAuthenticationApi.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/sshauthentication-api/src/main/java/org/openintents/ssh/authentication/SshAuthenticationApi.java b/sshauthentication-api/src/main/java/org/openintents/ssh/authentication/SshAuthenticationApi.java index 700be7934..d63193bfa 100644 --- a/sshauthentication-api/src/main/java/org/openintents/ssh/authentication/SshAuthenticationApi.java +++ b/sshauthentication-api/src/main/java/org/openintents/ssh/authentication/SshAuthenticationApi.java @@ -42,6 +42,10 @@ public class SshAuthenticationApi { * * Sign a given challenge * + * returns the encoded signature as described in + * RFC4253, RFC5656 & draft-ietf-curdle-ssh-ed25519 + * and their respective updates + * * required extras: * String EXTRA_KEY_ID * byte[] EXTRA_CHALLENGE From de695fa2b05b4bab5b1f104f20f43b53a8f292fa Mon Sep 17 00:00:00 2001 From: Christian Hagau Date: Tue, 28 Nov 2017 00:00:00 +0000 Subject: [PATCH 3/5] Improve error handling for curve OID to SSH curve identifier translation --- .../keychain/pgp/SshPublicKey.java | 9 ++- .../remote/SshAuthenticationService.java | 16 ++-- .../ssh/signature/SshSignatureConverter.java | 2 +- .../keychain/ssh/utils/SshUtils.java | 24 +++--- .../keychain/ui/ViewKeyAdvShareFragment.java | 6 +- .../signature/SshSignatureConverterTest.java | 74 +++++++++++++++++++ 6 files changed, 105 insertions(+), 26 deletions(-) create mode 100644 OpenKeychain/src/test/java/org/sufficientlysecure/keychain/ssh/signature/SshSignatureConverterTest.java diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/SshPublicKey.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/SshPublicKey.java index 1bdc2774d..313c93e5b 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/SshPublicKey.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/SshPublicKey.java @@ -29,6 +29,8 @@ import org.sufficientlysecure.keychain.ssh.key.SshEd25519PublicKey; import org.sufficientlysecure.keychain.ssh.key.SshRSAPublicKey; import org.sufficientlysecure.keychain.ssh.utils.SshUtils; +import java.security.NoSuchAlgorithmException; + public class SshPublicKey { private final static String TAG = "SshPublicKey"; @@ -38,7 +40,7 @@ public class SshPublicKey { mPublicKey = publicKey; } - public String getEncodedKey() throws PgpGeneralException { + public String getEncodedKey() throws PgpGeneralException, NoSuchAlgorithmException { PGPPublicKey key = mPublicKey.getPublicKey(); switch (key.getAlgorithm()) { @@ -51,9 +53,8 @@ public class SshPublicKey { case PGPPublicKey.DSA: return encodeDSAKey(key); default: - break; + throw new PgpGeneralException("Unknown key algorithm"); } - throw new PgpGeneralException("Unknown algorithm"); } private String encodeRSAKey(PGPPublicKey publicKey) { @@ -64,7 +65,7 @@ public class SshPublicKey { return pubkey.getPublicKeyBlob(); } - private String encodeECKey(PGPPublicKey publicKey) { + private String encodeECKey(PGPPublicKey publicKey) throws NoSuchAlgorithmException { ECPublicBCPGKey publicBCPGKey = (ECPublicBCPGKey) publicKey.getPublicKeyPacket().getKey(); String curveName = SshUtils.getCurveName(mPublicKey.getCurveOid()); diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/remote/SshAuthenticationService.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/remote/SshAuthenticationService.java index 8933b9f73..623e938ef 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/remote/SshAuthenticationService.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/remote/SshAuthenticationService.java @@ -192,15 +192,15 @@ public class SshAuthenticationService extends Service { } else if (authResult.success()) { byte[] rawSignature = authResult.getSignature(); byte[] sshSignature; - if (authSubKeyAlgorithm == PublicKeyAlgorithmTags.ECDSA) { - sshSignature = SshSignatureConverter.getSshSignatureEcDsa(rawSignature, authSubKeyCurveOid); - } else { - try { + try { + if (authSubKeyAlgorithm == PublicKeyAlgorithmTags.ECDSA) { + sshSignature = SshSignatureConverter.getSshSignatureEcDsa(rawSignature, authSubKeyCurveOid); + } else { sshSignature = SshSignatureConverter.getSshSignature(rawSignature, authSubKeyAlgorithm); - } catch (NoSuchAlgorithmException e) { - return createExceptionErrorResult(SshAuthenticationApiError.INTERNAL_ERROR, - "Error converting signature", e); } + } catch (NoSuchAlgorithmException e) { + return createExceptionErrorResult(SshAuthenticationApiError.INTERNAL_ERROR, + "Error converting signature", e); } return new SigningResponse(sshSignature).toIntent(); } else { @@ -351,7 +351,7 @@ public class SshAuthenticationService extends Service { SshPublicKey sshPublicKey = new SshPublicKey(publicKey); try { sshPublicKeyBlob = sshPublicKey.getEncodedKey(); - } catch (PgpGeneralException e) { + } catch (PgpGeneralException | NoSuchAlgorithmException e) { return createExceptionErrorResult(SshAuthenticationApiError.GENERIC_ERROR, "Error converting public key to SSH format", e); } diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ssh/signature/SshSignatureConverter.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ssh/signature/SshSignatureConverter.java index f08a0da31..1be09aa54 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ssh/signature/SshSignatureConverter.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ssh/signature/SshSignatureConverter.java @@ -128,7 +128,7 @@ public class SshSignatureConverter { return signature.getBytes(); } - public static byte[] getSshSignatureEcDsa(byte[] rawSignature, String curveOid) { + public static byte[] getSshSignatureEcDsa(byte[] rawSignature, String curveOid) throws NoSuchAlgorithmException { SshEncodedData signature = new SshEncodedData(); signature.putString("ecdsa-sha2-" + SshUtils.getCurveName(curveOid)); signature.putString(getEcDsaSignatureBlob(rawSignature)); diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ssh/utils/SshUtils.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ssh/utils/SshUtils.java index 5869a2902..589b6810e 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ssh/utils/SshUtils.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ssh/utils/SshUtils.java @@ -17,9 +17,11 @@ package org.sufficientlysecure.keychain.ssh.utils; +import java.security.NoSuchAlgorithmException; + public class SshUtils { - public static String getCurveName(String curveOid) { + public static String getCurveName(String curveOid) throws NoSuchAlgorithmException { // see RFC5656 section 10.{1,2} switch (curveOid) { // REQUIRED curves @@ -32,26 +34,26 @@ public class SshUtils { // RECOMMENDED curves case "1.3.132.0.1": - return "1.3.132.0.1"; + return "1.3.132.0.1"; case "1.2.840.10045.3.1.1": - return "1.2.840.10045.3.1.1"; + return "1.2.840.10045.3.1.1"; case "1.3.132.0.33": - return "1.3.132.0.33"; + return "1.3.132.0.33"; case "1.3.132.0.26": - return "1.3.132.0.26"; + return "1.3.132.0.26"; case "1.3.132.0.27": - return "1.3.132.0.27"; + return "1.3.132.0.27"; case "1.3.132.0.16": - return "1.3.132.0.16"; + return "1.3.132.0.16"; case "1.3.132.0.36": - return "1.3.132.0.36"; + return "1.3.132.0.36"; case "1.3.132.0.37": - return "1.3.132.0.37"; + return "1.3.132.0.37"; case "1.3.132.0.38": - return "1.3.132.0.38"; + return "1.3.132.0.38"; default: - return null; + throw new NoSuchAlgorithmException("Can't translate curve OID to SSH curve identifier"); } } } diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewKeyAdvShareFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewKeyAdvShareFragment.java index 3bd95eb5a..54f523d83 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewKeyAdvShareFragment.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewKeyAdvShareFragment.java @@ -69,6 +69,7 @@ import java.io.BufferedWriter; import java.io.FileNotFoundException; import java.io.IOException; import java.io.OutputStreamWriter; +import java.security.NoSuchAlgorithmException; public class ViewKeyAdvShareFragment extends LoaderFragment implements LoaderManager.LoaderCallbacks { @@ -223,7 +224,8 @@ public class ViewKeyAdvShareFragment extends LoaderFragment implements } private String getShareKeyContent(boolean asSshKey) - throws PgpKeyNotFoundException, KeyRepository.NotFoundException, IOException, PgpGeneralException { + throws PgpKeyNotFoundException, KeyRepository.NotFoundException, IOException, PgpGeneralException, + NoSuchAlgorithmException { KeyRepository keyRepository = KeyRepository.create(getContext()); @@ -303,7 +305,7 @@ public class ViewKeyAdvShareFragment extends LoaderFragment implements Intent shareChooser = Intent.createChooser(sendIntent, title); startActivity(shareChooser); - } catch (PgpGeneralException | IOException e) { + } catch (PgpGeneralException | IOException | NoSuchAlgorithmException e) { Log.e(Constants.TAG, "error processing key!", e); Notify.create(activity, R.string.error_key_processing, Notify.Style.ERROR).show(); } catch (PgpKeyNotFoundException | KeyRepository.NotFoundException e) { diff --git a/OpenKeychain/src/test/java/org/sufficientlysecure/keychain/ssh/signature/SshSignatureConverterTest.java b/OpenKeychain/src/test/java/org/sufficientlysecure/keychain/ssh/signature/SshSignatureConverterTest.java new file mode 100644 index 000000000..603507ea2 --- /dev/null +++ b/OpenKeychain/src/test/java/org/sufficientlysecure/keychain/ssh/signature/SshSignatureConverterTest.java @@ -0,0 +1,74 @@ +package org.sufficientlysecure.keychain.ssh.signature; + +import org.bouncycastle.jce.provider.BouncyCastleProvider; +import org.junit.Assert; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.robolectric.RuntimeEnvironment; +import org.robolectric.shadows.ShadowLog; +import org.sufficientlysecure.keychain.KeychainTestRunner; +import org.sufficientlysecure.keychain.pgp.CanonicalizedPublicKey; +import org.sufficientlysecure.keychain.pgp.SshPublicKey; +import org.sufficientlysecure.keychain.pgp.UncachedKeyRing; +import org.sufficientlysecure.keychain.provider.KeyRepository; +import org.sufficientlysecure.keychain.provider.KeyWritableRepository; +import org.sufficientlysecure.keychain.support.KeyringTestingHelper; +import org.sufficientlysecure.keychain.util.Passphrase; + +import java.io.PrintStream; +import java.security.Security; + + +@RunWith(KeychainTestRunner.class) +public class SshSignatureConverterTest { + + private static UncachedKeyRing mStaticRingEcDsa; + private static Passphrase mKeyPhrase; + + private static PrintStream oldShadowStream; + + @BeforeClass + public static void setUpOnce() throws Exception { + Security.insertProviderAt(new BouncyCastleProvider(), 1); + oldShadowStream = ShadowLog.stream; + // ShadowLog.stream = System.out; + + mKeyPhrase = new Passphrase("x"); + mStaticRingEcDsa = KeyringTestingHelper.readRingFromResource("/test-keys/authenticate_ecdsa.sec"); + } + + @Before + public void setUp() { + KeyWritableRepository databaseInteractor = + KeyWritableRepository.create(RuntimeEnvironment.application); + + // don't log verbosely here, we're not here to test imports + ShadowLog.stream = oldShadowStream; + + databaseInteractor.saveSecretKeyRing(mStaticRingEcDsa); + + // ok NOW log verbosely! + ShadowLog.stream = System.out; + } + + @Test + public void testECDSA() throws Exception { + KeyRepository keyRepository = KeyRepository.create(RuntimeEnvironment.application); + + long masterKeyId = mStaticRingEcDsa.getMasterKeyId(); + long authSubKeyId = keyRepository.getCachedPublicKeyRing(masterKeyId).getSecretAuthenticationId(); + CanonicalizedPublicKey canonicalizedPublicKey = keyRepository.getCanonicalizedPublicKeyRing(masterKeyId) + .getPublicKey(authSubKeyId); + + SshPublicKey publicKeyUtils = new SshPublicKey(canonicalizedPublicKey); + String publicKeyBlob = publicKeyUtils.getEncodedKey(); + + String publicKeyBlobExpected = "ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTY" + + "AAABBBJm2rlv9/8dgVm6VbN9OJDK1pA1Cb7HjJZv+zyiZGbpUrNWN81L1z45mnOfYafAzZMZ9SBy4J954wjp4d/pICIg="; + + Assert.assertEquals("Public key blobs must be equal", publicKeyBlobExpected, publicKeyBlob); + + } +} From 8afc43d19281b41ea7b5eb62a696750daaa7ad4c Mon Sep 17 00:00:00 2001 From: Christian Hagau Date: Tue, 28 Nov 2017 00:00:00 +0000 Subject: [PATCH 4/5] Add tests for SshSignatureConverter --- .../signature/SshSignatureConverterTest.java | 198 ++++++++++++++---- 1 file changed, 152 insertions(+), 46 deletions(-) diff --git a/OpenKeychain/src/test/java/org/sufficientlysecure/keychain/ssh/signature/SshSignatureConverterTest.java b/OpenKeychain/src/test/java/org/sufficientlysecure/keychain/ssh/signature/SshSignatureConverterTest.java index 603507ea2..3886d658a 100644 --- a/OpenKeychain/src/test/java/org/sufficientlysecure/keychain/ssh/signature/SshSignatureConverterTest.java +++ b/OpenKeychain/src/test/java/org/sufficientlysecure/keychain/ssh/signature/SshSignatureConverterTest.java @@ -1,74 +1,180 @@ +/* + * Copyright (C) 2017 Christian Hagau + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + package org.sufficientlysecure.keychain.ssh.signature; -import org.bouncycastle.jce.provider.BouncyCastleProvider; +import org.bouncycastle.bcpg.PublicKeyAlgorithmTags; import org.junit.Assert; -import org.junit.Before; -import org.junit.BeforeClass; import org.junit.Test; import org.junit.runner.RunWith; -import org.robolectric.RuntimeEnvironment; -import org.robolectric.shadows.ShadowLog; import org.sufficientlysecure.keychain.KeychainTestRunner; -import org.sufficientlysecure.keychain.pgp.CanonicalizedPublicKey; -import org.sufficientlysecure.keychain.pgp.SshPublicKey; -import org.sufficientlysecure.keychain.pgp.UncachedKeyRing; -import org.sufficientlysecure.keychain.provider.KeyRepository; -import org.sufficientlysecure.keychain.provider.KeyWritableRepository; -import org.sufficientlysecure.keychain.support.KeyringTestingHelper; -import org.sufficientlysecure.keychain.util.Passphrase; - -import java.io.PrintStream; -import java.security.Security; @RunWith(KeychainTestRunner.class) public class SshSignatureConverterTest { - private static UncachedKeyRing mStaticRingEcDsa; - private static Passphrase mKeyPhrase; + private final static String sCurveOidNistP256 = "1.2.840.10045.3.1.7"; - private static PrintStream oldShadowStream; + private final static int[] sRawEcDsaSignatureInt = { + 0x30, 0x46, + 0x02, 0x21, + 0x00, 0x94, 0x9f, 0xa9, 0x15, 0x1d, 0x71, 0x49, 0x5d, 0x9c, 0x02, 0x06, 0x35, 0xdc, 0xed, 0xac, 0x6a, + 0x86, 0x65, 0xd0, 0x79, 0xb4, 0xf7, 0x21, 0xb0, 0x5a, 0x94, 0x08, 0x77, 0x1e, 0x45, 0x5f, 0xe2, + 0x02, 0x21, + 0x00, 0xdf, 0x9c, 0x7a, 0x5a, 0xe5, 0x9e, 0x5d, 0x2e, 0x42, 0xd3, 0x76, 0x7e, 0x15, 0x25, 0xc8, 0x25, + 0x7c, 0xff, 0x82, 0xac, 0x26, 0x64, 0xb6, 0x41, 0x9f, 0xf6, 0x6f, 0x6e, 0x8b, 0x96, 0x69, 0xa0 + }; - @BeforeClass - public static void setUpOnce() throws Exception { - Security.insertProviderAt(new BouncyCastleProvider(), 1); - oldShadowStream = ShadowLog.stream; - // ShadowLog.stream = System.out; + private final static int[] sSshEcDsaSignatureInt = { + 0x00, 0x00, 0x00, 0x13, + 0x65, 0x63, 0x64, 0x73, 0x61, 0x2d, 0x73, 0x68, 0x61, 0x32, 0x2d, 0x6e, 0x69, 0x73, 0x74, 0x70, 0x32, + 0x35, 0x36, + 0x00, 0x00, 0x00, 0x4a, + 0x00, 0x00, 0x00, 0x21, + 0x00, 0x94, 0x9f, 0xa9, 0x15, 0x1d, 0x71, 0x49, 0x5d, 0x9c, 0x02, 0x06, 0x35, 0xdc, 0xed, 0xac, 0x6a, + 0x86, 0x65, 0xd0, 0x79, 0xb4, 0xf7, 0x21, 0xb0, 0x5a, 0x94, 0x08, 0x77, 0x1e, 0x45, 0x5f, 0xe2, + 0x00, 0x00, 0x00, 0x21, + 0x00, 0xdf, 0x9c, 0x7a, 0x5a, 0xe5, 0x9e, 0x5d, 0x2e, 0x42, 0xd3, 0x76, 0x7e, 0x15, 0x25, 0xc8, 0x25, + 0x7c, 0xff, 0x82, 0xac, 0x26, 0x64, 0xb6, 0x41, 0x9f, 0xf6, 0x6f, 0x6e, 0x8b, 0x96, 0x69, 0xa0 + }; - mKeyPhrase = new Passphrase("x"); - mStaticRingEcDsa = KeyringTestingHelper.readRingFromResource("/test-keys/authenticate_ecdsa.sec"); - } + private final static int[] sRawRsaSignatureInt = { + 0x1c, 0x97, 0x5c, 0x37, 0xa4, 0x13, 0x7e, 0x9c, 0x86, 0x1d, 0x20, 0xd9, 0xd4, 0x0b, 0x6d, 0xb1, 0x6d, + 0x1d, 0xa8, 0xb1, 0x7e, 0x36, 0x03, 0x11, 0xb6, 0xa4, 0xeb, 0xcb, 0x3f, 0x1f, 0xf5, 0x1d, 0x49, 0x06, + 0x28, 0xb8, 0x0d, 0xe0, 0xde, 0xce, 0x08, 0xa1, 0xb5, 0xeb, 0xe8, 0xa5, 0x89, 0x4e, 0xa2, 0xfe, 0xa7, + 0x40, 0x74, 0x1e, 0x7c, 0x83, 0xc2, 0x41, 0xa0, 0xd2, 0xbd, 0x9b, 0xdb, 0x3a, 0x2f, 0x39, 0x42, 0xca, + 0xe8, 0xcc, 0xc3, 0xbd, 0xa7, 0xa1, 0x7b, 0x40, 0xb0, 0x0a, 0x0e, 0x21, 0x4a, 0x5d, 0xa7, 0x65, 0x42, + 0x11, 0xf5, 0xfc, 0x49, 0xb4, 0x5d, 0x16, 0xb1, 0xe4, 0x6f, 0xa8, 0x0c, 0xe7, 0x77, 0x96, 0x9c, 0x51, + 0x9f, 0x09, 0xbb, 0x45, 0xe3, 0x12, 0xe4, 0x10, 0x9b, 0x3a, 0xf0, 0xc3, 0x13, 0x3f, 0xfa, 0x22, 0x1d, + 0xa9, 0xe3, 0xc9, 0xe0, 0x3f, 0xa2, 0xfd, 0xb7, 0x0d, 0xf0, 0x3e, 0x6c, 0x83, 0xee, 0x71, 0xf1, 0x06, + 0xb8, 0xf2, 0x4f, 0xd7, 0x2b, 0xad, 0x5e, 0x4e, 0x68, 0x12, 0x3d, 0xda, 0x65, 0x6d, 0xdb, 0xa8, 0xee, + 0x11, 0xf9, 0x10, 0x61, 0x54, 0xd1, 0xe1, 0x37, 0x0b, 0xff, 0x3b, 0xa2, 0x2e, 0x3c, 0x25, 0xb7, 0xd9, + 0x33, 0x4d, 0x90, 0x3e, 0x4d, 0xd7, 0x9a, 0x73, 0x89, 0xda, 0x41, 0xe9, 0x43, 0x7e, 0x79, 0xdd, 0xd8, + 0xa3, 0x33, 0x5d, 0x2c, 0x21, 0x7f, 0x01, 0x05, 0x9b, 0xde, 0x2f, 0x34, 0x50, 0xf8, 0x93, 0x3f, 0x38, + 0xbe, 0x10, 0xcd, 0x59, 0x46, 0x7e, 0x9c, 0x93, 0x32, 0xc7, 0x79, 0x4c, 0xcb, 0x9d, 0x19, 0xcb, 0x65, + 0xa1, 0x79, 0xb0, 0x16, 0x6c, 0xd0, 0xe5, 0x83, 0xe1, 0x7f, 0x8f, 0x31, 0x22, 0x22, 0x25, 0x9a, 0xe3, + 0x1b, 0x13, 0xe6, 0x1f, 0xca, 0xe4, 0xda, 0x5c, 0x55, 0x54, 0xe2, 0x35, 0x52, 0x18, 0xa0, 0xeb, 0x07, + 0x19 + }; - @Before - public void setUp() { - KeyWritableRepository databaseInteractor = - KeyWritableRepository.create(RuntimeEnvironment.application); + private final static int[] sSshRsaSignatureInt = { + 0x00, 0x00, 0x00, 0x07, + 0x73, 0x73, 0x68, 0x2d, 0x72, 0x73, 0x61, + 0x00, 0x00, 0x01, 0x00, + 0x1c, 0x97, 0x5c, 0x37, 0xa4, 0x13, 0x7e, 0x9c, 0x86, 0x1d, 0x20, 0xd9, 0xd4, 0x0b, 0x6d, 0xb1, 0x6d, + 0x1d, 0xa8, 0xb1, 0x7e, 0x36, 0x03, 0x11, 0xb6, 0xa4, 0xeb, 0xcb, 0x3f, 0x1f, 0xf5, 0x1d, 0x49, 0x06, + 0x28, 0xb8, 0x0d, 0xe0, 0xde, 0xce, 0x08, 0xa1, 0xb5, 0xeb, 0xe8, 0xa5, 0x89, 0x4e, 0xa2, 0xfe, 0xa7, + 0x40, 0x74, 0x1e, 0x7c, 0x83, 0xc2, 0x41, 0xa0, 0xd2, 0xbd, 0x9b, 0xdb, 0x3a, 0x2f, 0x39, 0x42, 0xca, + 0xe8, 0xcc, 0xc3, 0xbd, 0xa7, 0xa1, 0x7b, 0x40, 0xb0, 0x0a, 0x0e, 0x21, 0x4a, 0x5d, 0xa7, 0x65, 0x42, + 0x11, 0xf5, 0xfc, 0x49, 0xb4, 0x5d, 0x16, 0xb1, 0xe4, 0x6f, 0xa8, 0x0c, 0xe7, 0x77, 0x96, 0x9c, 0x51, + 0x9f, 0x09, 0xbb, 0x45, 0xe3, 0x12, 0xe4, 0x10, 0x9b, 0x3a, 0xf0, 0xc3, 0x13, 0x3f, 0xfa, 0x22, 0x1d, + 0xa9, 0xe3, 0xc9, 0xe0, 0x3f, 0xa2, 0xfd, 0xb7, 0x0d, 0xf0, 0x3e, 0x6c, 0x83, 0xee, 0x71, 0xf1, 0x06, + 0xb8, 0xf2, 0x4f, 0xd7, 0x2b, 0xad, 0x5e, 0x4e, 0x68, 0x12, 0x3d, 0xda, 0x65, 0x6d, 0xdb, 0xa8, 0xee, + 0x11, 0xf9, 0x10, 0x61, 0x54, 0xd1, 0xe1, 0x37, 0x0b, 0xff, 0x3b, 0xa2, 0x2e, 0x3c, 0x25, 0xb7, 0xd9, + 0x33, 0x4d, 0x90, 0x3e, 0x4d, 0xd7, 0x9a, 0x73, 0x89, 0xda, 0x41, 0xe9, 0x43, 0x7e, 0x79, 0xdd, 0xd8, + 0xa3, 0x33, 0x5d, 0x2c, 0x21, 0x7f, 0x01, 0x05, 0x9b, 0xde, 0x2f, 0x34, 0x50, 0xf8, 0x93, 0x3f, 0x38, + 0xbe, 0x10, 0xcd, 0x59, 0x46, 0x7e, 0x9c, 0x93, 0x32, 0xc7, 0x79, 0x4c, 0xcb, 0x9d, 0x19, 0xcb, 0x65, + 0xa1, 0x79, 0xb0, 0x16, 0x6c, 0xd0, 0xe5, 0x83, 0xe1, 0x7f, 0x8f, 0x31, 0x22, 0x22, 0x25, 0x9a, 0xe3, + 0x1b, 0x13, 0xe6, 0x1f, 0xca, 0xe4, 0xda, 0x5c, 0x55, 0x54, 0xe2, 0x35, 0x52, 0x18, 0xa0, 0xeb, 0x07, + 0x19 + }; - // don't log verbosely here, we're not here to test imports - ShadowLog.stream = oldShadowStream; + private final static int[] sRawEdDsaSignatureInt = { + 0x55, 0x49, 0x46, 0xe8, 0x27, 0xc6, 0xfd, 0x4b, 0x21, 0xb7, 0xa8, 0x1a, 0x97, 0x7a, 0x74, 0x53, 0x31, + 0x0e, 0x18, 0xc0, 0x05, 0x40, 0x3b, 0xfa, 0x4d, 0xdd, 0x87, 0x15, 0x8b, 0x56, 0xb1, 0x40, 0xfd, 0x61, + 0x0b, 0xf1, 0x5d, 0x7f, 0x38, 0xa3, 0x2b, 0x55, 0x71, 0x3f, 0xd3, 0x80, 0x87, 0xac, 0x86, 0x12, 0xdc, + 0x14, 0x56, 0xce, 0xc3, 0x15, 0xe4, 0x64, 0x3b, 0x6d, 0x24, 0x89, 0x07, 0x0a + }; - databaseInteractor.saveSecretKeyRing(mStaticRingEcDsa); + private final static int[] sSshEdDsaSignatureInt = { + 0x00, 0x00, 0x00, 0x0b, + 0x73, 0x73, 0x68, 0x2d, 0x65, 0x64, 0x32, 0x35, 0x35, 0x31, 0x39, + 0x00, 0x00, 0x00, 0x40, + 0x55, 0x49, 0x46, 0xe8, 0x27, 0xc6, 0xfd, 0x4b, 0x21, 0xb7, 0xa8, 0x1a, 0x97, 0x7a, 0x74, 0x53, 0x31, + 0x0e, 0x18, 0xc0, 0x05, 0x40, 0x3b, 0xfa, 0x4d, 0xdd, 0x87, 0x15, 0x8b, 0x56, 0xb1, 0x40, 0xfd, 0x61, + 0x0b, 0xf1, 0x5d, 0x7f, 0x38, 0xa3, 0x2b, 0x55, 0x71, 0x3f, 0xd3, 0x80, 0x87, 0xac, 0x86, 0x12, 0xdc, + 0x14, 0x56, 0xce, 0xc3, 0x15, 0xe4, 0x64, 0x3b, 0x6d, 0x24, 0x89, 0x07, 0x0a + }; - // ok NOW log verbosely! - ShadowLog.stream = System.out; + private final static int[] sRawDsaSignatureInt = { + 0x30, 0x46, + 0x02, 0x21, + 0x00, 0xde, 0xfd, 0xb8, 0xa2, 0x5f, 0xb8, 0x66, 0x0a, 0x3c, 0xab, 0x24, 0x51, 0x0a, 0x20, 0x0a, 0x01, + 0x3e, 0xb9, 0xc6, 0x77, 0xe4, 0xca, 0xed, 0x19, 0xa3, 0x49, 0xa9, 0xaf, 0x3b, 0x8c, 0x97, 0x1a, + 0x02, 0x21, + 0x00, 0xe7, 0xe4, 0xd5, 0xb8, 0xf0, 0x8a, 0xb5, 0xcb, 0x4d, 0x44, 0x5f, 0x03, 0xc4, 0x58, 0xcc, 0xce, + 0x3d, 0xff, 0x26, 0xa2, 0x0f, 0xb3, 0x14, 0x50, 0x86, 0x04, 0xd1, 0xca, 0x3e, 0x2c, 0x91, 0x25 + }; + + private final static int[] sSshDsaSignatureInt = { + 0x00, 0x00, 0x00, 0x07, + 0x73, 0x73, 0x68, 0x2d, 0x64, 0x73, 0x73, + 0x00, 0x00, 0x00, 0x40, + 0xde, 0xfd, 0xb8, 0xa2, 0x5f, 0xb8, 0x66, 0x0a, 0x3c, 0xab, 0x24, 0x51, 0x0a, 0x20, 0x0a, 0x01, 0x3e, + 0xb9, 0xc6, 0x77, 0xe4, 0xca, 0xed, 0x19, 0xa3, 0x49, 0xa9, 0xaf, 0x3b, 0x8c, 0x97, 0x1a, 0xe7, 0xe4, + 0xd5, 0xb8, 0xf0, 0x8a, 0xb5, 0xcb, 0x4d, 0x44, 0x5f, 0x03, 0xc4, 0x58, 0xcc, 0xce, 0x3d, 0xff, 0x26, + 0xa2, 0x0f, 0xb3, 0x14, 0x50, 0x86, 0x04, 0xd1, 0xca, 0x3e, 0x2c, 0x91, 0x25 + }; + + @Test + public void testEcDsa() throws Exception { + byte[] rawEcDsaSignature = convertArray(sRawEcDsaSignatureInt); + byte[] sshEcDsaSignature = convertArray(sSshEcDsaSignatureInt); + + byte[] out = SshSignatureConverter.getSshSignatureEcDsa(rawEcDsaSignature, sCurveOidNistP256); + + Assert.assertArrayEquals("Signature blobs must be equal", sshEcDsaSignature, out); } @Test - public void testECDSA() throws Exception { - KeyRepository keyRepository = KeyRepository.create(RuntimeEnvironment.application); + public void testRsa() throws Exception { + byte[] rawRsaSignature = convertArray(sRawRsaSignatureInt); + byte[] sshRsaSignature = convertArray(sSshRsaSignatureInt); - long masterKeyId = mStaticRingEcDsa.getMasterKeyId(); - long authSubKeyId = keyRepository.getCachedPublicKeyRing(masterKeyId).getSecretAuthenticationId(); - CanonicalizedPublicKey canonicalizedPublicKey = keyRepository.getCanonicalizedPublicKeyRing(masterKeyId) - .getPublicKey(authSubKeyId); + byte[] out = SshSignatureConverter.getSshSignature(rawRsaSignature, PublicKeyAlgorithmTags.RSA_SIGN); - SshPublicKey publicKeyUtils = new SshPublicKey(canonicalizedPublicKey); - String publicKeyBlob = publicKeyUtils.getEncodedKey(); + Assert.assertArrayEquals("Signature blobs must be equal", sshRsaSignature, out); + } - String publicKeyBlobExpected = "ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTY" - + "AAABBBJm2rlv9/8dgVm6VbN9OJDK1pA1Cb7HjJZv+zyiZGbpUrNWN81L1z45mnOfYafAzZMZ9SBy4J954wjp4d/pICIg="; + @Test + public void testEdDsa() throws Exception { + byte[] rawEdDsaSignature = convertArray(sRawEdDsaSignatureInt); + byte[] sshEdDsaSignature = convertArray(sSshEdDsaSignatureInt); - Assert.assertEquals("Public key blobs must be equal", publicKeyBlobExpected, publicKeyBlob); + byte[] out = SshSignatureConverter.getSshSignature(rawEdDsaSignature, PublicKeyAlgorithmTags.EDDSA); + Assert.assertArrayEquals("Signature blobs must be equal", sshEdDsaSignature, out); + } + + @Test + public void testDsa() throws Exception { + byte[] rawDsaSignature = convertArray(sRawDsaSignatureInt); + byte[] sshDsaSignature = convertArray(sSshDsaSignatureInt); + + byte[] out = SshSignatureConverter.getSshSignature(rawDsaSignature, PublicKeyAlgorithmTags.DSA); + + Assert.assertArrayEquals("Signature blobs must be equal", sshDsaSignature, out); + } + + private byte[] convertArray(int[] array) { + byte[] out = new byte[array.length]; + for (int i = 0; i < array.length; i++) { + out[i] = (byte) array[i]; + } + return out; } } From e981a1444c62c26663f5bf70317f62c0df672732 Mon Sep 17 00:00:00 2001 From: Vincent Breitmoser Date: Tue, 28 Nov 2017 16:40:06 +0100 Subject: [PATCH 5/5] use Hex.decode in unit tests --- .../signature/SshSignatureConverterTest.java | 225 ++++++++---------- 1 file changed, 103 insertions(+), 122 deletions(-) diff --git a/OpenKeychain/src/test/java/org/sufficientlysecure/keychain/ssh/signature/SshSignatureConverterTest.java b/OpenKeychain/src/test/java/org/sufficientlysecure/keychain/ssh/signature/SshSignatureConverterTest.java index 3886d658a..c0e3d7fed 100644 --- a/OpenKeychain/src/test/java/org/sufficientlysecure/keychain/ssh/signature/SshSignatureConverterTest.java +++ b/OpenKeychain/src/test/java/org/sufficientlysecure/keychain/ssh/signature/SshSignatureConverterTest.java @@ -17,7 +17,9 @@ package org.sufficientlysecure.keychain.ssh.signature; + import org.bouncycastle.bcpg.PublicKeyAlgorithmTags; +import org.bouncycastle.util.encoders.Hex; import org.junit.Assert; import org.junit.Test; import org.junit.runner.RunWith; @@ -27,154 +29,133 @@ import org.sufficientlysecure.keychain.KeychainTestRunner; @RunWith(KeychainTestRunner.class) public class SshSignatureConverterTest { - private final static String sCurveOidNistP256 = "1.2.840.10045.3.1.7"; + private final static String CURVE_OID_NIST_P_256 = "1.2.840.10045.3.1.7"; - private final static int[] sRawEcDsaSignatureInt = { - 0x30, 0x46, - 0x02, 0x21, - 0x00, 0x94, 0x9f, 0xa9, 0x15, 0x1d, 0x71, 0x49, 0x5d, 0x9c, 0x02, 0x06, 0x35, 0xdc, 0xed, 0xac, 0x6a, - 0x86, 0x65, 0xd0, 0x79, 0xb4, 0xf7, 0x21, 0xb0, 0x5a, 0x94, 0x08, 0x77, 0x1e, 0x45, 0x5f, 0xe2, - 0x02, 0x21, - 0x00, 0xdf, 0x9c, 0x7a, 0x5a, 0xe5, 0x9e, 0x5d, 0x2e, 0x42, 0xd3, 0x76, 0x7e, 0x15, 0x25, 0xc8, 0x25, - 0x7c, 0xff, 0x82, 0xac, 0x26, 0x64, 0xb6, 0x41, 0x9f, 0xf6, 0x6f, 0x6e, 0x8b, 0x96, 0x69, 0xa0 - }; + private final static byte[] RAW_ECDSA_SIGNATURE = Hex.decode( + "3046" + + "0221" + + "00949fa9151d71495d9c020635dcedac6a" + + "8665d079b4f721b05a9408771e455fe2" + + "0221" + + "00df9c7a5ae59e5d2e42d3767e1525c825" + + "7cff82ac2664b6419ff66f6e8b9669a0"); - private final static int[] sSshEcDsaSignatureInt = { - 0x00, 0x00, 0x00, 0x13, - 0x65, 0x63, 0x64, 0x73, 0x61, 0x2d, 0x73, 0x68, 0x61, 0x32, 0x2d, 0x6e, 0x69, 0x73, 0x74, 0x70, 0x32, - 0x35, 0x36, - 0x00, 0x00, 0x00, 0x4a, - 0x00, 0x00, 0x00, 0x21, - 0x00, 0x94, 0x9f, 0xa9, 0x15, 0x1d, 0x71, 0x49, 0x5d, 0x9c, 0x02, 0x06, 0x35, 0xdc, 0xed, 0xac, 0x6a, - 0x86, 0x65, 0xd0, 0x79, 0xb4, 0xf7, 0x21, 0xb0, 0x5a, 0x94, 0x08, 0x77, 0x1e, 0x45, 0x5f, 0xe2, - 0x00, 0x00, 0x00, 0x21, - 0x00, 0xdf, 0x9c, 0x7a, 0x5a, 0xe5, 0x9e, 0x5d, 0x2e, 0x42, 0xd3, 0x76, 0x7e, 0x15, 0x25, 0xc8, 0x25, - 0x7c, 0xff, 0x82, 0xac, 0x26, 0x64, 0xb6, 0x41, 0x9f, 0xf6, 0x6f, 0x6e, 0x8b, 0x96, 0x69, 0xa0 - }; + private final static byte[] SSH_ECDSA_SIGNATURE = Hex.decode( + "00000013" + + "65636473612d736861322d6e6973747032" + + "3536" + + "0000004a" + + "00000021" + + "00949fa9151d71495d9c020635dcedac6a" + + "8665d079b4f721b05a9408771e455fe2" + + "00000021" + + "00df9c7a5ae59e5d2e42d3767e1525c825" + + "7cff82ac2664b6419ff66f6e8b9669a0" + ); - private final static int[] sRawRsaSignatureInt = { - 0x1c, 0x97, 0x5c, 0x37, 0xa4, 0x13, 0x7e, 0x9c, 0x86, 0x1d, 0x20, 0xd9, 0xd4, 0x0b, 0x6d, 0xb1, 0x6d, - 0x1d, 0xa8, 0xb1, 0x7e, 0x36, 0x03, 0x11, 0xb6, 0xa4, 0xeb, 0xcb, 0x3f, 0x1f, 0xf5, 0x1d, 0x49, 0x06, - 0x28, 0xb8, 0x0d, 0xe0, 0xde, 0xce, 0x08, 0xa1, 0xb5, 0xeb, 0xe8, 0xa5, 0x89, 0x4e, 0xa2, 0xfe, 0xa7, - 0x40, 0x74, 0x1e, 0x7c, 0x83, 0xc2, 0x41, 0xa0, 0xd2, 0xbd, 0x9b, 0xdb, 0x3a, 0x2f, 0x39, 0x42, 0xca, - 0xe8, 0xcc, 0xc3, 0xbd, 0xa7, 0xa1, 0x7b, 0x40, 0xb0, 0x0a, 0x0e, 0x21, 0x4a, 0x5d, 0xa7, 0x65, 0x42, - 0x11, 0xf5, 0xfc, 0x49, 0xb4, 0x5d, 0x16, 0xb1, 0xe4, 0x6f, 0xa8, 0x0c, 0xe7, 0x77, 0x96, 0x9c, 0x51, - 0x9f, 0x09, 0xbb, 0x45, 0xe3, 0x12, 0xe4, 0x10, 0x9b, 0x3a, 0xf0, 0xc3, 0x13, 0x3f, 0xfa, 0x22, 0x1d, - 0xa9, 0xe3, 0xc9, 0xe0, 0x3f, 0xa2, 0xfd, 0xb7, 0x0d, 0xf0, 0x3e, 0x6c, 0x83, 0xee, 0x71, 0xf1, 0x06, - 0xb8, 0xf2, 0x4f, 0xd7, 0x2b, 0xad, 0x5e, 0x4e, 0x68, 0x12, 0x3d, 0xda, 0x65, 0x6d, 0xdb, 0xa8, 0xee, - 0x11, 0xf9, 0x10, 0x61, 0x54, 0xd1, 0xe1, 0x37, 0x0b, 0xff, 0x3b, 0xa2, 0x2e, 0x3c, 0x25, 0xb7, 0xd9, - 0x33, 0x4d, 0x90, 0x3e, 0x4d, 0xd7, 0x9a, 0x73, 0x89, 0xda, 0x41, 0xe9, 0x43, 0x7e, 0x79, 0xdd, 0xd8, - 0xa3, 0x33, 0x5d, 0x2c, 0x21, 0x7f, 0x01, 0x05, 0x9b, 0xde, 0x2f, 0x34, 0x50, 0xf8, 0x93, 0x3f, 0x38, - 0xbe, 0x10, 0xcd, 0x59, 0x46, 0x7e, 0x9c, 0x93, 0x32, 0xc7, 0x79, 0x4c, 0xcb, 0x9d, 0x19, 0xcb, 0x65, - 0xa1, 0x79, 0xb0, 0x16, 0x6c, 0xd0, 0xe5, 0x83, 0xe1, 0x7f, 0x8f, 0x31, 0x22, 0x22, 0x25, 0x9a, 0xe3, - 0x1b, 0x13, 0xe6, 0x1f, 0xca, 0xe4, 0xda, 0x5c, 0x55, 0x54, 0xe2, 0x35, 0x52, 0x18, 0xa0, 0xeb, 0x07, - 0x19 - }; + private final static byte[] RAW_RSA_SIGNATURE = Hex.decode( + "1c975c37a4137e9c861d20d9d40b6db16d" + + "1da8b17e360311b6a4ebcb3f1ff51d4906" + + "28b80de0dece08a1b5ebe8a5894ea2fea7" + + "40741e7c83c241a0d2bd9bdb3a2f3942ca" + + "e8ccc3bda7a17b40b00a0e214a5da76542" + + "11f5fc49b45d16b1e46fa80ce777969c51" + + "9f09bb45e312e4109b3af0c3133ffa221d" + + "a9e3c9e03fa2fdb70df03e6c83ee71f106" + + "b8f24fd72bad5e4e68123dda656ddba8ee" + + "11f9106154d1e1370bff3ba22e3c25b7d9" + + "334d903e4dd79a7389da41e9437e79ddd8" + + "a3335d2c217f01059bde2f3450f8933f38" + + "be10cd59467e9c9332c7794ccb9d19cb65" + + "a179b0166cd0e583e17f8f312222259ae3" + + "1b13e61fcae4da5c5554e2355218a0eb07" + + "19" + ); - private final static int[] sSshRsaSignatureInt = { - 0x00, 0x00, 0x00, 0x07, - 0x73, 0x73, 0x68, 0x2d, 0x72, 0x73, 0x61, - 0x00, 0x00, 0x01, 0x00, - 0x1c, 0x97, 0x5c, 0x37, 0xa4, 0x13, 0x7e, 0x9c, 0x86, 0x1d, 0x20, 0xd9, 0xd4, 0x0b, 0x6d, 0xb1, 0x6d, - 0x1d, 0xa8, 0xb1, 0x7e, 0x36, 0x03, 0x11, 0xb6, 0xa4, 0xeb, 0xcb, 0x3f, 0x1f, 0xf5, 0x1d, 0x49, 0x06, - 0x28, 0xb8, 0x0d, 0xe0, 0xde, 0xce, 0x08, 0xa1, 0xb5, 0xeb, 0xe8, 0xa5, 0x89, 0x4e, 0xa2, 0xfe, 0xa7, - 0x40, 0x74, 0x1e, 0x7c, 0x83, 0xc2, 0x41, 0xa0, 0xd2, 0xbd, 0x9b, 0xdb, 0x3a, 0x2f, 0x39, 0x42, 0xca, - 0xe8, 0xcc, 0xc3, 0xbd, 0xa7, 0xa1, 0x7b, 0x40, 0xb0, 0x0a, 0x0e, 0x21, 0x4a, 0x5d, 0xa7, 0x65, 0x42, - 0x11, 0xf5, 0xfc, 0x49, 0xb4, 0x5d, 0x16, 0xb1, 0xe4, 0x6f, 0xa8, 0x0c, 0xe7, 0x77, 0x96, 0x9c, 0x51, - 0x9f, 0x09, 0xbb, 0x45, 0xe3, 0x12, 0xe4, 0x10, 0x9b, 0x3a, 0xf0, 0xc3, 0x13, 0x3f, 0xfa, 0x22, 0x1d, - 0xa9, 0xe3, 0xc9, 0xe0, 0x3f, 0xa2, 0xfd, 0xb7, 0x0d, 0xf0, 0x3e, 0x6c, 0x83, 0xee, 0x71, 0xf1, 0x06, - 0xb8, 0xf2, 0x4f, 0xd7, 0x2b, 0xad, 0x5e, 0x4e, 0x68, 0x12, 0x3d, 0xda, 0x65, 0x6d, 0xdb, 0xa8, 0xee, - 0x11, 0xf9, 0x10, 0x61, 0x54, 0xd1, 0xe1, 0x37, 0x0b, 0xff, 0x3b, 0xa2, 0x2e, 0x3c, 0x25, 0xb7, 0xd9, - 0x33, 0x4d, 0x90, 0x3e, 0x4d, 0xd7, 0x9a, 0x73, 0x89, 0xda, 0x41, 0xe9, 0x43, 0x7e, 0x79, 0xdd, 0xd8, - 0xa3, 0x33, 0x5d, 0x2c, 0x21, 0x7f, 0x01, 0x05, 0x9b, 0xde, 0x2f, 0x34, 0x50, 0xf8, 0x93, 0x3f, 0x38, - 0xbe, 0x10, 0xcd, 0x59, 0x46, 0x7e, 0x9c, 0x93, 0x32, 0xc7, 0x79, 0x4c, 0xcb, 0x9d, 0x19, 0xcb, 0x65, - 0xa1, 0x79, 0xb0, 0x16, 0x6c, 0xd0, 0xe5, 0x83, 0xe1, 0x7f, 0x8f, 0x31, 0x22, 0x22, 0x25, 0x9a, 0xe3, - 0x1b, 0x13, 0xe6, 0x1f, 0xca, 0xe4, 0xda, 0x5c, 0x55, 0x54, 0xe2, 0x35, 0x52, 0x18, 0xa0, 0xeb, 0x07, - 0x19 - }; + private final static byte[] SSH_RSA_SIGNATURE = Hex.decode( + "00000007" + + "7373682d727361" + + "00000100" + + "1c975c37a4137e9c861d20d9d40b6db16d" + + "1da8b17e360311b6a4ebcb3f1ff51d4906" + + "28b80de0dece08a1b5ebe8a5894ea2fea7" + + "40741e7c83c241a0d2bd9bdb3a2f3942ca" + + "e8ccc3bda7a17b40b00a0e214a5da76542" + + "11f5fc49b45d16b1e46fa80ce777969c51" + + "9f09bb45e312e4109b3af0c3133ffa221d" + + "a9e3c9e03fa2fdb70df03e6c83ee71f106" + + "b8f24fd72bad5e4e68123dda656ddba8ee" + + "11f9106154d1e1370bff3ba22e3c25b7d9" + + "334d903e4dd79a7389da41e9437e79ddd8" + + "a3335d2c217f01059bde2f3450f8933f38" + + "be10cd59467e9c9332c7794ccb9d19cb65" + + "a179b0166cd0e583e17f8f312222259ae3" + + "1b13e61fcae4da5c5554e2355218a0eb07" + + "19" + ); - private final static int[] sRawEdDsaSignatureInt = { - 0x55, 0x49, 0x46, 0xe8, 0x27, 0xc6, 0xfd, 0x4b, 0x21, 0xb7, 0xa8, 0x1a, 0x97, 0x7a, 0x74, 0x53, 0x31, - 0x0e, 0x18, 0xc0, 0x05, 0x40, 0x3b, 0xfa, 0x4d, 0xdd, 0x87, 0x15, 0x8b, 0x56, 0xb1, 0x40, 0xfd, 0x61, - 0x0b, 0xf1, 0x5d, 0x7f, 0x38, 0xa3, 0x2b, 0x55, 0x71, 0x3f, 0xd3, 0x80, 0x87, 0xac, 0x86, 0x12, 0xdc, - 0x14, 0x56, 0xce, 0xc3, 0x15, 0xe4, 0x64, 0x3b, 0x6d, 0x24, 0x89, 0x07, 0x0a - }; + private final static byte[] RAW_EDDSA_SIGNATURE = Hex.decode( + "554946e827c6fd4b21b7a81a977a745331" + + "0e18c005403bfa4ddd87158b56b140fd61" + + "0bf15d7f38a32b55713fd38087ac8612dc" + + "1456cec315e4643b6d2489070a" + ); - private final static int[] sSshEdDsaSignatureInt = { - 0x00, 0x00, 0x00, 0x0b, - 0x73, 0x73, 0x68, 0x2d, 0x65, 0x64, 0x32, 0x35, 0x35, 0x31, 0x39, - 0x00, 0x00, 0x00, 0x40, - 0x55, 0x49, 0x46, 0xe8, 0x27, 0xc6, 0xfd, 0x4b, 0x21, 0xb7, 0xa8, 0x1a, 0x97, 0x7a, 0x74, 0x53, 0x31, - 0x0e, 0x18, 0xc0, 0x05, 0x40, 0x3b, 0xfa, 0x4d, 0xdd, 0x87, 0x15, 0x8b, 0x56, 0xb1, 0x40, 0xfd, 0x61, - 0x0b, 0xf1, 0x5d, 0x7f, 0x38, 0xa3, 0x2b, 0x55, 0x71, 0x3f, 0xd3, 0x80, 0x87, 0xac, 0x86, 0x12, 0xdc, - 0x14, 0x56, 0xce, 0xc3, 0x15, 0xe4, 0x64, 0x3b, 0x6d, 0x24, 0x89, 0x07, 0x0a - }; + private final static byte[] SSH_EDDSA_SIGNATURE = Hex.decode( + "0000000b" + + "7373682d65643235353139" + + "00000040" + + "554946e827c6fd4b21b7a81a977a745331" + + "0e18c005403bfa4ddd87158b56b140fd61" + + "0bf15d7f38a32b55713fd38087ac8612dc" + + "1456cec315e4643b6d2489070a" + ); - private final static int[] sRawDsaSignatureInt = { - 0x30, 0x46, - 0x02, 0x21, - 0x00, 0xde, 0xfd, 0xb8, 0xa2, 0x5f, 0xb8, 0x66, 0x0a, 0x3c, 0xab, 0x24, 0x51, 0x0a, 0x20, 0x0a, 0x01, - 0x3e, 0xb9, 0xc6, 0x77, 0xe4, 0xca, 0xed, 0x19, 0xa3, 0x49, 0xa9, 0xaf, 0x3b, 0x8c, 0x97, 0x1a, - 0x02, 0x21, - 0x00, 0xe7, 0xe4, 0xd5, 0xb8, 0xf0, 0x8a, 0xb5, 0xcb, 0x4d, 0x44, 0x5f, 0x03, 0xc4, 0x58, 0xcc, 0xce, - 0x3d, 0xff, 0x26, 0xa2, 0x0f, 0xb3, 0x14, 0x50, 0x86, 0x04, 0xd1, 0xca, 0x3e, 0x2c, 0x91, 0x25 - }; + private final static byte[] RAW_DSA_SIGNATURE = Hex.decode( + "3046" + + "0221" + + "00defdb8a25fb8660a3cab24510a200a01" + + "3eb9c677e4caed19a349a9af3b8c971a" + + "0221" + + "00e7e4d5b8f08ab5cb4d445f03c458ccce" + + "3dff26a20fb314508604d1ca3e2c9125" + ); - private final static int[] sSshDsaSignatureInt = { - 0x00, 0x00, 0x00, 0x07, - 0x73, 0x73, 0x68, 0x2d, 0x64, 0x73, 0x73, - 0x00, 0x00, 0x00, 0x40, - 0xde, 0xfd, 0xb8, 0xa2, 0x5f, 0xb8, 0x66, 0x0a, 0x3c, 0xab, 0x24, 0x51, 0x0a, 0x20, 0x0a, 0x01, 0x3e, - 0xb9, 0xc6, 0x77, 0xe4, 0xca, 0xed, 0x19, 0xa3, 0x49, 0xa9, 0xaf, 0x3b, 0x8c, 0x97, 0x1a, 0xe7, 0xe4, - 0xd5, 0xb8, 0xf0, 0x8a, 0xb5, 0xcb, 0x4d, 0x44, 0x5f, 0x03, 0xc4, 0x58, 0xcc, 0xce, 0x3d, 0xff, 0x26, - 0xa2, 0x0f, 0xb3, 0x14, 0x50, 0x86, 0x04, 0xd1, 0xca, 0x3e, 0x2c, 0x91, 0x25 - }; + private final static byte[] SSH_DSA_SIGNATURE = Hex.decode( + "00000007" + + "7373682d647373" + + "00000040" + + "defdb8a25fb8660a3cab24510a200a013e" + + "b9c677e4caed19a349a9af3b8c971ae7e4" + + "d5b8f08ab5cb4d445f03c458ccce3dff26" + + "a20fb314508604d1ca3e2c9125" + ); @Test public void testEcDsa() throws Exception { - byte[] rawEcDsaSignature = convertArray(sRawEcDsaSignatureInt); - byte[] sshEcDsaSignature = convertArray(sSshEcDsaSignatureInt); + byte[] out = SshSignatureConverter.getSshSignatureEcDsa(RAW_ECDSA_SIGNATURE, CURVE_OID_NIST_P_256); - byte[] out = SshSignatureConverter.getSshSignatureEcDsa(rawEcDsaSignature, sCurveOidNistP256); - - Assert.assertArrayEquals("Signature blobs must be equal", sshEcDsaSignature, out); + Assert.assertArrayEquals(SSH_ECDSA_SIGNATURE, out); } @Test public void testRsa() throws Exception { - byte[] rawRsaSignature = convertArray(sRawRsaSignatureInt); - byte[] sshRsaSignature = convertArray(sSshRsaSignatureInt); + byte[] out = SshSignatureConverter.getSshSignature(RAW_RSA_SIGNATURE, PublicKeyAlgorithmTags.RSA_SIGN); - byte[] out = SshSignatureConverter.getSshSignature(rawRsaSignature, PublicKeyAlgorithmTags.RSA_SIGN); - - Assert.assertArrayEquals("Signature blobs must be equal", sshRsaSignature, out); + Assert.assertArrayEquals(SSH_RSA_SIGNATURE, out); } @Test public void testEdDsa() throws Exception { - byte[] rawEdDsaSignature = convertArray(sRawEdDsaSignatureInt); - byte[] sshEdDsaSignature = convertArray(sSshEdDsaSignatureInt); + byte[] out = SshSignatureConverter.getSshSignature(RAW_EDDSA_SIGNATURE, PublicKeyAlgorithmTags.EDDSA); - byte[] out = SshSignatureConverter.getSshSignature(rawEdDsaSignature, PublicKeyAlgorithmTags.EDDSA); - - Assert.assertArrayEquals("Signature blobs must be equal", sshEdDsaSignature, out); + Assert.assertArrayEquals(SSH_EDDSA_SIGNATURE, out); } @Test public void testDsa() throws Exception { - byte[] rawDsaSignature = convertArray(sRawDsaSignatureInt); - byte[] sshDsaSignature = convertArray(sSshDsaSignatureInt); + byte[] out = SshSignatureConverter.getSshSignature(RAW_DSA_SIGNATURE, PublicKeyAlgorithmTags.DSA); - byte[] out = SshSignatureConverter.getSshSignature(rawDsaSignature, PublicKeyAlgorithmTags.DSA); - - Assert.assertArrayEquals("Signature blobs must be equal", sshDsaSignature, out); - } - - private byte[] convertArray(int[] array) { - byte[] out = new byte[array.length]; - for (int i = 0; i < array.length; i++) { - out[i] = (byte) array[i]; - } - return out; + Assert.assertArrayEquals(SSH_DSA_SIGNATURE, out); } }