From 8fe752626726ca8d058ce437127a37d5d738a5eb Mon Sep 17 00:00:00 2001 From: Alex Clayton Date: Fri, 26 Feb 2016 11:19:56 +0000 Subject: Add missing TLS and Base classes Add missing methods and classes in the TLS and Base packages where possible. In the case of TLSContextFactory the methods could not be implemented in java so added a not saying they are not supported in java. Test-information: Unit tests still build and run ok. Change-Id: I9be2035f092875fcdc02644a3c0082739f26949a diff --git a/PortingProgress.txt b/PortingProgress.txt index fff5a43..0dcb71d 100644 --- a/PortingProgress.txt +++ b/PortingProgress.txt @@ -22,8 +22,9 @@ All files ported to 6ca201d0b48f4273e24dd7bff17c4a46eeaddf39 except for: Log, Override, Path, Paths, PathTest, Platform, Regex, sleep, StartStopper, String, StringTest, WindowsRegistry -- Doesn't Need Porting. -SafeAllocator -- Not Yet Ported! -DateTimeTest -- Not Yet Ported! +SafeAllocator -- Not Ported! Class for memory allocation which is not needed in java. +DateTimeTest -- Did not port testDateTimeToLocalStringNotThrowingException, as we are not implementing +DateTime.dateToLocalString in java. Individual Comments: StartStopper -- StartStoppable interface added. @@ -219,8 +220,8 @@ TLS: All files ported to 6ca201d0b48f4273e24dd7bff17c4a46eeaddf39 except for: -PlatformTLSFactories -- Still needs implementing a CertificateFactory. -TLSContextFactory -- Two methods unimplemented and will also affect JSSEContextFactory. +TLSContextFactory -- Two methods unimplemented and will also affect JSSEContextFactory. These methods are not +supported in java. ----- VCards: diff --git a/src/com/isode/stroke/base/DateTime.java b/src/com/isode/stroke/base/DateTime.java index 8a8e31a..4c8310a 100644 --- a/src/com/isode/stroke/base/DateTime.java +++ b/src/com/isode/stroke/base/DateTime.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2014-2015, Isode Limited, London, England. + * Copyright (c) 2014-2016, Isode Limited, London, England. * All rights reserved. */ @@ -27,6 +27,6 @@ public class DateTime { parser.setTimeZone(TimeZone.getTimeZone("UTC")); return parser.format(date); } - - static private String format = "yyyy-MM-dd'T'HH:mm:ss'Z'"; + + static private String format = "yyyy-MM-dd'T'HH:mm:ssXXX"; } diff --git a/src/com/isode/stroke/tls/JavaCertificateFactory.java b/src/com/isode/stroke/tls/JavaCertificateFactory.java new file mode 100644 index 0000000..56a183e --- /dev/null +++ b/src/com/isode/stroke/tls/JavaCertificateFactory.java @@ -0,0 +1,58 @@ +/* Copyright (c) 2016, Isode Limited, London, England. + * All rights reserved. + * + * Acquisition and use of this software and related materials for any + * purpose requires a written license agreement from Isode Limited, + * or a written license from an organisation licensed by Isode Limited + * to grant such a license. + * + */ +package com.isode.stroke.tls; + +import java.io.ByteArrayInputStream; +import java.io.InputStream; +import java.security.cert.CertificateException; +import java.security.cert.X509Certificate; +import java.util.logging.Logger; +import java.util.logging.Level; + +import com.isode.stroke.base.ByteArray; +import com.isode.stroke.tls.java.JavaCertificate; + +public class JavaCertificateFactory implements CertificateFactory { + + private final Logger logger = Logger.getLogger(this.getClass().getName()); + + private final java.security.cert.CertificateFactory jvmCertFactory; + + public JavaCertificateFactory() { + java.security.cert.CertificateFactory temp = null; + try { + temp = java.security.cert.CertificateFactory.getInstance("X.509"); + } catch (CertificateException e) { + logger.log(Level.WARNING,"Unable to generate X509 certificate factory",e); + temp = null; + } + jvmCertFactory = temp; + } + + @Override + public Certificate createCertificateFromDER(ByteArray der) { + if (jvmCertFactory == null) { + return null; + } + InputStream derInputStream = new ByteArrayInputStream(der.getData()); + try { + X509Certificate x509Cert = (X509Certificate)jvmCertFactory.generateCertificate(derInputStream); + return new JavaCertificate(x509Cert); + } catch (CertificateException e) { + logger.log(Level.WARNING,"Unable to generate certificate from byte array "+der,e); + return null; + } catch (ClassCastException e) { + // Should not get here as factory should return an x509 certificate + logger.log(Level.WARNING,"Unable to generate X509 certificate",e); + return null; + } + } + +} diff --git a/src/com/isode/stroke/tls/PlatformTLSFactories.java b/src/com/isode/stroke/tls/PlatformTLSFactories.java index cbfcfe2..19bb0a3 100644 --- a/src/com/isode/stroke/tls/PlatformTLSFactories.java +++ b/src/com/isode/stroke/tls/PlatformTLSFactories.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2012-2013 Isode Limited, London, England. + * Copyright (c) 2012-2016 Isode Limited, London, England. * All rights reserved. */ /* @@ -11,14 +11,14 @@ package com.isode.stroke.tls; import com.isode.stroke.tls.java.JSSEContextFactory; public class PlatformTLSFactories { - private JSSEContextFactory contextFactory = new JSSEContextFactory(); + private final JSSEContextFactory contextFactory = new JSSEContextFactory(); + private final CertificateFactory certificateFactory = new JavaCertificateFactory(); public TLSContextFactory getTLSContextFactory() { return contextFactory; } public CertificateFactory getCertificateFactory() { - /*FIXME: Implement*/ - return null; + return certificateFactory; } } diff --git a/src/com/isode/stroke/tls/TLSContextFactory.java b/src/com/isode/stroke/tls/TLSContextFactory.java index f33539b..491893a 100644 --- a/src/com/isode/stroke/tls/TLSContextFactory.java +++ b/src/com/isode/stroke/tls/TLSContextFactory.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2011, Isode Limited, London, England. + * Copyright (c) 2011-2016, Isode Limited, London, England. * All rights reserved. */ /* @@ -12,6 +12,7 @@ package com.isode.stroke.tls; public interface TLSContextFactory { boolean canCreate(); TLSContext createTLSContext(TLSOptions tlsOptions); + // These aren't supported in Java //void setCheckCertificateRevocation(boolean b); //void setDisconnectOnCardRemoval(boolean b); } diff --git a/test/com/isode/stroke/base/DateTimeTest.java b/test/com/isode/stroke/base/DateTimeTest.java new file mode 100644 index 0000000..e6fc787 --- /dev/null +++ b/test/com/isode/stroke/base/DateTimeTest.java @@ -0,0 +1,56 @@ +/* Copyright (c) 2016, Isode Limited, London, England. + * All rights reserved. + * + * Acquisition and use of this software and related materials for any + * purpose requires a written license agreement from Isode Limited, + * or a written license from an organisation licensed by Isode Limited + * to grant such a license. + * + */ +package com.isode.stroke.base; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.TimeZone; + +import org.junit.Before; +import org.junit.Test; + +/** + * @author ac + * @since 16.5 + * + */ +public class DateTimeTest { + + private final SimpleDateFormat isoFormatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss"); + + @Before + public void setUp() { + isoFormatter.setTimeZone(TimeZone.getTimeZone("UTC")); + } + + @Test + public void testStringToDateTime_UTC() { + Date time = DateTime.stringToDate("1969-07-21T02:56:15Z"); + assertNotNull(time); + assertEquals("1969-07-21T02:56:15", isoFormatter.format(time)); + } + + @Test + public void testStringToDateTime_WithTimezone() { + Date time = DateTime.stringToDate("1969-07-20T21:56:15-05:00"); + assertNotNull(time); + assertEquals("1969-07-21T02:56:15", isoFormatter.format(time)); + } + + @Test + public void testDateTimeToString() { + Date time = DateTime.stringToDate("1969-07-20T21:56:15-05:00"); + assertEquals("1969-07-21T02:56:15Z", DateTime.dateToString(time)); + } + +} -- cgit v0.10.2-6-g49f6