Skip to content

Commit

Permalink
Remove un-needed line return at the end of the PEM when encoding.
Browse files Browse the repository at this point in the history
  • Loading branch information
robotdan committed Jan 15, 2019
1 parent 47b1d39 commit bddbdcd
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 17 deletions.
3 changes: 3 additions & 0 deletions CHANGES
@@ -1,5 +1,8 @@
FusionAuth JWT Changes

Changes in 3.0.1
* Remove un-needed line return at the end of the PEM when encoding a PEM string.

Changes in 3.0.0

This major version of fusionauth-jwt contains breaking changes.
Expand Down
2 changes: 1 addition & 1 deletion build.savant
Expand Up @@ -18,7 +18,7 @@ savantVersion = "1.0.0"
jacksonVersion = "2.9.8"
jacksonAnnotationVersion = "2.9.6"

project(group: "io.fusionauth", name: "fusionauth-jwt", version: "3.0.0", licenses: ["ApacheV2_0"]) {
project(group: "io.fusionauth", name: "fusionauth-jwt", version: "3.0.1", licenses: ["ApacheV2_0"]) {

workflow {
standard()
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Expand Up @@ -6,7 +6,7 @@

<groupId>io.fusionauth</groupId>
<artifactId>fusionauth-jwt</artifactId>
<version>3.0.0</version>
<version>3.0.1</version>
<packaging>jar</packaging>

<name>FusionAuth JWT</name>
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/io/fusionauth/pem/PEMEncoder.java
Expand Up @@ -160,12 +160,12 @@ private void addClosingTag(Key key, StringBuilder sb) {
sb.append("\n");
if (key instanceof PrivateKey) {
if (key.getFormat().equals("PKCS#1")) {
sb.append(PEM.PKCS_1_PRIVATE_KEY_SUFFIX).append("\n");
sb.append(PEM.PKCS_1_PRIVATE_KEY_SUFFIX);
} else if (key.getFormat().equals("PKCS#8")) {
sb.append(PEM.PKCS_8_PRIVATE_KEY_SUFFIX).append("\n");
sb.append(PEM.PKCS_8_PRIVATE_KEY_SUFFIX);
}
} else {
sb.append(PEM.X509_PUBLIC_KEY_SUFFIX).append("\n");
sb.append(PEM.X509_PUBLIC_KEY_SUFFIX);
}
}

Expand Down
24 changes: 12 additions & 12 deletions src/test/java/io/fusionauth/pem/PEMEncoderTest.java
Expand Up @@ -46,12 +46,12 @@ public void ec() throws Exception {
String encodedPublicKey = PEM.encode(keyPair.getPublic());
assertNotNull(encodedPublicKey);
assertTrue(encodedPublicKey.startsWith(PEM.X509_PUBLIC_KEY_PREFIX));
assertTrue(encodedPublicKey.endsWith(PEM.X509_PUBLIC_KEY_SUFFIX + "\n"));
assertTrue(encodedPublicKey.endsWith(PEM.X509_PUBLIC_KEY_SUFFIX));

String encodedPrivateKey = PEM.encode(keyPair.getPrivate());
assertNotNull(encodedPrivateKey);
assertTrue(encodedPrivateKey.startsWith(PEM.PKCS_8_PRIVATE_KEY_PREFIX));
assertTrue(encodedPrivateKey.endsWith(PEM.PKCS_8_PRIVATE_KEY_SUFFIX + "\n"));
assertTrue(encodedPrivateKey.endsWith(PEM.PKCS_8_PRIVATE_KEY_SUFFIX));

// Since we built our own key pair, the private key will not contain the public key
PEM pem = PEM.decode(encodedPrivateKey);
Expand All @@ -69,8 +69,8 @@ public void ec() throws Exception {
@Test
public void ec_backAndForth() throws Exception {
// Start with openSSL PKCS#8 private key and X.509 public key
String expectedPrivate = new String(Files.readAllBytes(Paths.get("src/test/resources/ec_private_prime256v1_p_256_openssl_pkcs8.pem")));
String expectedPublic = new String(Files.readAllBytes(Paths.get("src/test/resources/ec_public_prime256v1_p_256_openssl.pem")));
String expectedPrivate = new String(Files.readAllBytes(Paths.get("src/test/resources/ec_private_prime256v1_p_256_openssl_pkcs8.pem"))).trim();
String expectedPublic = new String(Files.readAllBytes(Paths.get("src/test/resources/ec_public_prime256v1_p_256_openssl.pem"))).trim();

// Decode the private key to ensure we get both private and public keys out of the private PEM
PEM pem = PEM.decode(expectedPrivate);
Expand Down Expand Up @@ -103,12 +103,12 @@ public void rsa() throws Exception {
String encodedPublicKey = PEM.encode(keyPair.getPublic());
assertNotNull(encodedPublicKey);
assertTrue(encodedPublicKey.startsWith(PEM.X509_PUBLIC_KEY_PREFIX));
assertTrue(encodedPublicKey.endsWith(PEM.X509_PUBLIC_KEY_SUFFIX + "\n"));
assertTrue(encodedPublicKey.endsWith(PEM.X509_PUBLIC_KEY_SUFFIX));

String encodedPrivateKey = PEM.encode(keyPair.getPrivate());
assertNotNull(encodedPrivateKey);
assertTrue(encodedPrivateKey.startsWith(PEM.PKCS_8_PRIVATE_KEY_PREFIX));
assertTrue(encodedPrivateKey.endsWith(PEM.PKCS_8_PRIVATE_KEY_SUFFIX + "\n"));
assertTrue(encodedPrivateKey.endsWith(PEM.PKCS_8_PRIVATE_KEY_SUFFIX));

// Since the public RSA modulus and public exponent are always included in the private key, they should
// be contained in the generated PEM
Expand All @@ -120,8 +120,8 @@ public void rsa() throws Exception {
@Test
public void rsa_backAndForth_pkcs_1() throws Exception {
// Start externally created PKCS#1 private key and X.509 public key
String expectedPrivate_pkcs_1 = new String(Files.readAllBytes(Paths.get("src/test/resources/rsa_private_key_2048_pkcs_1_control.pem")));
String expectedPrivate_pkcs_8 = new String(Files.readAllBytes(Paths.get("src/test/resources/rsa_private_key_2048_pkcs_8_control.pem")));
String expectedPrivate_pkcs_1 = new String(Files.readAllBytes(Paths.get("src/test/resources/rsa_private_key_2048_pkcs_1_control.pem"))).trim();
String expectedPrivate_pkcs_8 = new String(Files.readAllBytes(Paths.get("src/test/resources/rsa_private_key_2048_pkcs_8_control.pem"))).trim();
String expectedPublic = new String(Files.readAllBytes(Paths.get("src/test/resources/rsa_public_key_2048_x509_control.pem")));

// Decode the private key to ensure we get both private and public keys out of the private PEM
Expand All @@ -146,14 +146,14 @@ public void rsa_backAndForth_pkcs_1() throws Exception {

// Re-encode the public key to PEM X.509 format and ensure it equals the original
String encodedPublicKey = PEM.encode(pem.getPublicKey());
assertEquals(encodedPublicKey, expectedPublic + "\n");
assertEquals(encodedPublicKey, expectedPublic);
}

@Test
public void rsa_backAndForth_pkcs_8() throws Exception {
// Start externally created PKCS#1 private key and X.509 public key
String expectedPrivate = new String(Files.readAllBytes(Paths.get("src/test/resources/rsa_private_key_2048_pkcs_8_control.pem")));
String expectedPublic = new String(Files.readAllBytes(Paths.get("src/test/resources/rsa_public_key_2048_x509_control.pem")));
String expectedPrivate = new String(Files.readAllBytes(Paths.get("src/test/resources/rsa_private_key_2048_pkcs_8_control.pem"))).trim();
String expectedPublic = new String(Files.readAllBytes(Paths.get("src/test/resources/rsa_public_key_2048_x509_control.pem"))).trim();

// Decode the private key to ensure we get both private and public keys out of the private PEM
PEM pem = PEM.decode(expectedPrivate);
Expand All @@ -175,6 +175,6 @@ public void rsa_backAndForth_pkcs_8() throws Exception {

// Re-encode the public key to PEM X.509 format and ensure it equals the original
String encodedPublicKey = PEM.encode(pem.getPublicKey());
assertEquals(encodedPublicKey, expectedPublic + "\n");
assertEquals(encodedPublicKey, expectedPublic);
}
}

0 comments on commit bddbdcd

Please sign in to comment.