From 1ab0f1d4d991464b906c34befefe2ecaf485d485 Mon Sep 17 00:00:00 2001 From: Thomas Lenz Date: Tue, 19 Aug 2014 15:03:42 +0200 Subject: add interfederation without attributequery request which use encrypted bPKs (this functionality is required for federation with USP) --- .../at/gv/egovernment/moa/util/Base64Utils.java | 36 +++++++++++++++++----- .../at/gv/egovernment/moa/util/KeyStoreUtils.java | 27 +++++++++++----- 2 files changed, 47 insertions(+), 16 deletions(-) (limited to 'common/src/main/java/at/gv') diff --git a/common/src/main/java/at/gv/egovernment/moa/util/Base64Utils.java b/common/src/main/java/at/gv/egovernment/moa/util/Base64Utils.java index 27f12ab0f..66bf50316 100644 --- a/common/src/main/java/at/gv/egovernment/moa/util/Base64Utils.java +++ b/common/src/main/java/at/gv/egovernment/moa/util/Base64Utils.java @@ -50,12 +50,12 @@ public class Base64Utils { * @return byte[] The raw bytes contained in the base64String. * @throws IOException Failed to read the Base64 data. */ - public static byte[] decode(String base64String, boolean ignoreInvalidChars) + public static byte[] decode(String base64String, boolean ignoreInvalidChars, String encoding) throws IOException { Base64InputStream in = new Base64InputStream( - new ByteArrayInputStream(base64String.getBytes("UTF-8")), + new ByteArrayInputStream(base64String.getBytes(encoding)), ignoreInvalidChars); ByteArrayOutputStream out = new ByteArrayOutputStream(); byte[] bytes = new byte[256]; @@ -64,10 +64,15 @@ public class Base64Utils { while ((bytesRead = in.read(bytes)) > 0) { out.write(bytes, 0, bytesRead); } - + in.close(); + return out.toByteArray(); } + public static byte[] decode(String base64String, boolean ignoreInvalidChars) throws IOException { + return decode(base64String, ignoreInvalidChars, "UTF-8"); + } + /** * Read the bytes encoded in a Base64 encoded String and provide * them via an InputStream. @@ -80,11 +85,12 @@ public class Base64Utils { */ public static InputStream decodeToStream( String base64String, - boolean ignoreInvalidChars) { + boolean ignoreInvalidChars, + String encoding) { try { ByteArrayInputStream bin = - new ByteArrayInputStream(base64String.getBytes("UTF-8")); + new ByteArrayInputStream(base64String.getBytes(encoding)); Base64InputStream in = new Base64InputStream(bin, ignoreInvalidChars); return in; @@ -94,6 +100,13 @@ public class Base64Utils { } } + public static InputStream decodeToStream( + String base64String, + boolean ignoreInvalidChars) { + return decodeToStream(base64String, ignoreInvalidChars, "UTF-8"); + + } + /** * Convert a byte array to a Base64 encoded String. * @@ -102,9 +115,16 @@ public class Base64Utils { * @throws IOException Failed to write the bytes as Base64 data. */ public static String encode(byte[] bytes) throws IOException { - return encode(new ByteArrayInputStream(bytes)); + return encode(new ByteArrayInputStream(bytes), "UTF-8"); } + public static String encode(byte[] bytes, String encoding) throws IOException { + return encode(new ByteArrayInputStream(bytes), encoding); + } + + public static String encode(InputStream inputStream) throws IOException { + return encode(inputStream, "UTF-8"); + } /** * Convert the data contained in the given stream to a Base64 encoded * String. @@ -114,7 +134,7 @@ public class Base64Utils { * String. * @throws IOException Failed to convert the data in the stream. */ - public static String encode(InputStream inputStream) throws IOException { + public static String encode(InputStream inputStream, String encoding) throws IOException { ByteArrayOutputStream byteStream = new ByteArrayOutputStream(); Base64OutputStream base64Stream = new Base64OutputStream(byteStream, "\n".getBytes()); byte[] bytes = new byte[256]; @@ -127,7 +147,7 @@ public class Base64Utils { base64Stream.close(); inputStream.close(); - return byteStream.toString("UTF-8"); + return byteStream.toString(encoding); } } diff --git a/common/src/main/java/at/gv/egovernment/moa/util/KeyStoreUtils.java b/common/src/main/java/at/gv/egovernment/moa/util/KeyStoreUtils.java index 9db3ca6e3..3d28f4f2b 100644 --- a/common/src/main/java/at/gv/egovernment/moa/util/KeyStoreUtils.java +++ b/common/src/main/java/at/gv/egovernment/moa/util/KeyStoreUtils.java @@ -36,9 +36,7 @@ import java.net.URL; import java.security.GeneralSecurityException; import java.security.KeyStore; import java.security.KeyStoreException; -import java.security.NoSuchAlgorithmException; import java.security.cert.Certificate; -import java.security.cert.CertificateException; /** * Utility for creating and loading key stores. @@ -187,16 +185,29 @@ public class KeyStoreUtils { //InputStream is = new FileInputStream(keyStorePath); URL keystoreURL = new URL(keyStorePath); InputStream in = keystoreURL.openStream(); - InputStream isBuffered = new BufferedInputStream(in); + InputStream isBuffered = new BufferedInputStream(in); + return loadKeyStore(isBuffered, password); - isBuffered.mark(1024*1024); + } + + /** + * Loads a keyStore without knowing the keyStore type + * @param in input stream + * @param password Password protecting the keyStore + * @return keyStore loaded + * @throws KeyStoreException thrown if keyStore cannot be loaded + * @throws FileNotFoundException + * @throws IOException + */ +public static KeyStore loadKeyStore(InputStream is, String password) throws KeyStoreException, IOException{ + is.mark(1024*1024); KeyStore ks = null; try { try { - ks = loadKeyStore(KEYSTORE_TYPE_PKCS12, isBuffered, password); + ks = loadKeyStore(KEYSTORE_TYPE_PKCS12, is, password); } catch (IOException e2) { - isBuffered.reset(); - ks = loadKeyStore(KEYSTORE_TYPE_JKS, isBuffered, password); + is.reset(); + ks = loadKeyStore(KEYSTORE_TYPE_JKS, is, password); } } catch(Exception e) { e.printStackTrace(); @@ -205,7 +216,7 @@ public class KeyStoreUtils { return ks; } - + -- cgit v1.2.3