Use Java's own base64 decoder and encoder

pull/81/head
Richard Körber 2019-03-17 17:09:06 +01:00
parent 5863eac821
commit e689f7373d
No known key found for this signature in database
GPG Key ID: AAB9FD19C78AA3E0
3 changed files with 30 additions and 24 deletions

View File

@ -36,7 +36,6 @@ import javax.annotation.WillNotClose;
import javax.annotation.concurrent.Immutable;
import javax.crypto.SecretKey;
import org.jose4j.base64url.Base64Url;
import org.jose4j.jwk.EllipticCurveJsonWebKey;
import org.jose4j.jwk.JsonWebKey;
import org.jose4j.jwk.RsaJsonWebKey;
@ -73,6 +72,8 @@ public final class AcmeUtils {
private static final Base64.Encoder PEM_ENCODER = Base64.getMimeEncoder(64,
"\n".getBytes(StandardCharsets.US_ASCII));
private static final Base64.Encoder URL_ENCODER = Base64.getUrlEncoder().withoutPadding();
private static final Base64.Decoder URL_DECODER = Base64.getUrlDecoder();
/**
* Enumeration of PEM labels.
@ -144,7 +145,7 @@ public final class AcmeUtils {
* @return base64 encoded string
*/
public static String base64UrlEncode(byte[] data) {
return Base64Url.encode(data);
return URL_ENCODER.encodeToString(data);
}
/**
@ -155,7 +156,7 @@ public final class AcmeUtils {
* @return decoded data
*/
public static byte[] base64UrlDecode(String base64) {
return Base64Url.decode(base64);
return URL_DECODER.decode(base64);
}
/**

View File

@ -13,8 +13,10 @@
*/
package org.shredzone.acme4j.connector;
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;
import static org.mockito.Mockito.*;
import static org.shredzone.acme4j.toolbox.TestUtils.url;
import static uk.co.datumedge.hamcrest.json.SameJSONAs.sameJSONAs;
@ -33,13 +35,13 @@ import java.time.Duration;
import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.util.Arrays;
import java.util.Base64;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import org.jose4j.base64url.Base64Url;
import org.jose4j.jws.JsonWebSignature;
import org.jose4j.jwx.CompactSerializer;
import org.junit.Before;
@ -65,6 +67,9 @@ import org.shredzone.acme4j.toolbox.TestUtils;
*/
public class DefaultConnectionTest {
private static final Base64.Encoder URL_ENCODER = Base64.getUrlEncoder().withoutPadding();
private static final Base64.Decoder URL_DECODER = Base64.getUrlDecoder();
private URL requestUrl = TestUtils.url("http://example.com/acme/");
private URL accountUrl = TestUtils.url(TestUtils.ACCOUNT_URL);
private HttpURLConnection mockUrlConnection;
@ -673,8 +678,8 @@ public class DefaultConnectionTest {
*/
@Test
public void testSendSignedRequest() throws Exception {
final String nonce1 = Base64Url.encode("foo-nonce-1-foo".getBytes());
final String nonce2 = Base64Url.encode("foo-nonce-2-foo".getBytes());
final String nonce1 = URL_ENCODER.encodeToString("foo-nonce-1-foo".getBytes());
final String nonce2 = URL_ENCODER.encodeToString("foo-nonce-2-foo".getBytes());
final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
when(mockUrlConnection.getResponseCode()).thenReturn(HttpURLConnection.HTTP_OK);
@ -733,8 +738,8 @@ public class DefaultConnectionTest {
expectedHeader.append("\"kid\":\"").append(accountUrl).append('"');
expectedHeader.append('}');
assertThat(Base64Url.decodeToUtf8String(encodedHeader), sameJSONAs(expectedHeader.toString()));
assertThat(Base64Url.decodeToUtf8String(encodedPayload), sameJSONAs("{\"foo\":123,\"bar\":\"a-string\"}"));
assertThat(new String(URL_DECODER.decode(encodedHeader), UTF_8), sameJSONAs(expectedHeader.toString()));
assertThat(new String(URL_DECODER.decode(encodedPayload), UTF_8), sameJSONAs("{\"foo\":123,\"bar\":\"a-string\"}"));
assertThat(encodedSignature, not(emptyOrNullString()));
JsonWebSignature jws = new JsonWebSignature();
@ -748,8 +753,8 @@ public class DefaultConnectionTest {
*/
@Test
public void testSendSignedPostAsGetRequest() throws Exception {
final String nonce1 = Base64Url.encode("foo-nonce-1-foo".getBytes());
final String nonce2 = Base64Url.encode("foo-nonce-2-foo".getBytes());
final String nonce1 = URL_ENCODER.encodeToString("foo-nonce-1-foo".getBytes());
final String nonce2 = URL_ENCODER.encodeToString("foo-nonce-2-foo".getBytes());
final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
when(mockUrlConnection.getResponseCode()).thenReturn(HttpURLConnection.HTTP_OK);
@ -806,8 +811,8 @@ public class DefaultConnectionTest {
expectedHeader.append("\"kid\":\"").append(accountUrl).append('"');
expectedHeader.append('}');
assertThat(Base64Url.decodeToUtf8String(encodedHeader), sameJSONAs(expectedHeader.toString()));
assertThat(Base64Url.decodeToUtf8String(encodedPayload), is(""));
assertThat(new String(URL_DECODER.decode(encodedHeader), UTF_8), sameJSONAs(expectedHeader.toString()));
assertThat(new String(URL_DECODER.decode(encodedPayload), UTF_8), is(""));
assertThat(encodedSignature, not(emptyOrNullString()));
JsonWebSignature jws = new JsonWebSignature();
@ -821,8 +826,8 @@ public class DefaultConnectionTest {
*/
@Test
public void testSendCertificateRequest() throws Exception {
final String nonce1 = Base64Url.encode("foo-nonce-1-foo".getBytes());
final String nonce2 = Base64Url.encode("foo-nonce-2-foo".getBytes());
final String nonce1 = URL_ENCODER.encodeToString("foo-nonce-1-foo".getBytes());
final String nonce2 = URL_ENCODER.encodeToString("foo-nonce-2-foo".getBytes());
final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
when(mockUrlConnection.getResponseCode()).thenReturn(HttpURLConnection.HTTP_OK);
@ -872,8 +877,8 @@ public class DefaultConnectionTest {
*/
@Test
public void testSendSignedRequestNoKid() throws Exception {
final String nonce1 = Base64Url.encode("foo-nonce-1-foo".getBytes());
final String nonce2 = Base64Url.encode("foo-nonce-2-foo".getBytes());
final String nonce1 = URL_ENCODER.encodeToString("foo-nonce-1-foo".getBytes());
final String nonce2 = URL_ENCODER.encodeToString("foo-nonce-2-foo".getBytes());
final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
when(mockUrlConnection.getResponseCode()).thenReturn(HttpURLConnection.HTTP_OK);
@ -935,8 +940,8 @@ public class DefaultConnectionTest {
expectedHeader.append("\"n\":\"").append(TestUtils.N).append("\"");
expectedHeader.append("}}");
assertThat(Base64Url.decodeToUtf8String(encodedHeader), sameJSONAs(expectedHeader.toString()));
assertThat(Base64Url.decodeToUtf8String(encodedPayload), sameJSONAs("{\"foo\":123,\"bar\":\"a-string\"}"));
assertThat(new String(URL_DECODER.decode(encodedHeader), UTF_8), sameJSONAs(expectedHeader.toString()));
assertThat(new String(URL_DECODER.decode(encodedPayload), UTF_8), sameJSONAs("{\"foo\":123,\"bar\":\"a-string\"}"));
assertThat(encodedSignature, not(emptyOrNullString()));
JsonWebSignature jws = new JsonWebSignature();
@ -1010,7 +1015,7 @@ public class DefaultConnectionTest {
assertThat(downloaded.size(), is(original.size()));
for (int ix = 0; ix < downloaded.size(); ix++) {
assertThat(downloaded.get(ix).getEncoded(), is(original.get(ix).getEncoded()));
};
}
verify(mockUrlConnection).getHeaderField("Content-Type");
verify(mockUrlConnection).getInputStream();

View File

@ -40,13 +40,13 @@ import java.security.spec.ECGenParameterSpec;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.Base64;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import javax.crypto.SecretKey;
import org.jose4j.base64url.Base64Url;
import org.jose4j.json.JsonUtil;
import org.jose4j.jwk.JsonWebKey;
import org.jose4j.jwk.JsonWebKey.OutputControlLevel;
@ -74,7 +74,7 @@ public final class TestUtils {
public static final String ACME_SERVER_URI = "https://example.com/acme";
public static final String ACCOUNT_URL = "https://example.com/acme/account/1";
public static final String DUMMY_NONCE = Base64Url.encode("foo-nonce-foo".getBytes());
public static final String DUMMY_NONCE = Base64.getEncoder().withoutPadding().encodeToString("foo-nonce-foo".getBytes());
private TestUtils() {
@ -161,7 +161,7 @@ public final class TestUtils {
@Override
public AcmeProvider provider() {
return provider;
};
}
@Override
public Connection connect() {
@ -325,7 +325,7 @@ public final class TestUtils {
System.out.println("N = " + params.get("n"));
System.out.println("E = " + params.get("e"));
System.out.println("KTY = " + params.get("kty"));
System.out.println("THUMBPRINT = " + Base64Url.encode(thumbprint));
System.out.println("THUMBPRINT = " + Base64.getEncoder().withoutPadding().encodeToString(thumbprint));
}
}