Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enable to use PEM feature without nanojson nor tomitribe-util #6

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 81 additions & 0 deletions .github/workflows/maven.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
#
# Copyright (c) 2021, 2022 - Yupiik SAS - https://www.yupiik.com
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
#

name: Github CI

on: [push, pull_request]

env:
MAVEN_OPTS: -Dmaven.repo.local=/home/runner/work/churchkey/churchkey/.m2 -Dmaven.artifact.threads=256

jobs:
build:
name: Main Build
runs-on: ubuntu-20.04
steps:
- name: Clone
uses: actions/checkout@v3
- name: Cache Maven Repository
uses: actions/cache@v3
with:
path: /home/runner/work/churchkey/churchkey/.m2
key: m2_repository
- name: Set up JDK 17
uses: actions/setup-java@v3
with:
distribution: 'zulu'
java-version: '17'
- run: echo "JAVA_HOME_17=$JAVA_HOME" >> $GITHUB_ENV
- name: Set up JDK 8
uses: actions/setup-java@v3
with:
distribution: 'zulu'
java-version: '8'
- run: echo "JAVA_HOME_8=$JAVA_HOME" >> $GITHUB_ENV
- name: Set up Toolchain
shell: bash
run: |
mkdir -p $HOME/.m2 \
&& cat << EOF > $HOME/.m2/toolchains.xml
<?xml version="1.0" encoding="UTF8"?>
<toolchains>
<toolchain>
<type>jdk</type>
<provides>
<vendor>zulu</vendor>
<version>8</version>
</provides>
<configuration>
<jdkHome>${{ env.JAVA_HOME_8 }}</jdkHome>
</configuration>
</toolchain>
<toolchain>
<type>jdk</type>
<provides>
<vendor>zulu</vendor>
<version>17</version>
</provides>
<configuration>
<jdkHome>${{ env.JAVA_HOME_17 }}</jdkHome>
</configuration>
</toolchain>
</toolchains>
EOF
- name: Build
run: mvn package surefire:test@pem-dep-free -Dm2.location=/home/runner/work/churchkey/churchkey/.m2
- name: Remove Snapshots Before Caching
run: find /home/runner/work/churchkey/churchkey/.m2 -name '*SNAPSHOT' | xargs rm -Rf

9 changes: 9 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,12 @@

# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*

# idea
.idea
*.iml
*.ipr
*.iws

# Maven
target
50 changes: 36 additions & 14 deletions README.adoc
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
# Churchkey
= Churchkey
:churchkey-version: 0.14

image::https://img.shields.io/maven-central/v/org.tomitribe/churchkey?color=e77224&label=Last%20Release&logo=data%3Aimage%2Fpng%3Bbase64%2CiVBORw0KGgoAAAANSUhEUgAAAEAAAAA/CAMAAABnwz74AAAAilBMVEUAAADqcyTqcyTqcyTqcyTqcyTqcyTqcyTqcyTqcyXqcyTqcyTqcyTqcyTqcyTqcyTqcyTqcyTqcyTqcyTqcyTqcyXqcyTqcyTqcyTqcyTqcyTqcyTqcyTqcyTqcyTqcyTqcyTqcyTqcyTqcyTqcyTqcyTqcyTqcyTqcyTqcyTqcyTqcyTqcyTqcyRL2uskAAAALXRSTlMA7+SJTZT9OyIG2Z71dt/MRScZDQgE+bOXcC0U8j/FwbyljFlQ0o9lVIJrijavM7/TAAAB4ElEQVRIx+2W2ZaiMBRFE9oOCTMog2iBinPV/f/fa5QG7o3doDyz3zw5a8vKtMKGCVIO4J0lm4bKBDzhx2mC39DiBGwCXwI6rNUEwR4QiwkCAwu+h7srKeVSDx0ssNkAi50jhEj8i5okUHbXKqMpAhvVCjzZ2/cEAWBcNGLhgRvDxHlVBX8nLSUCT+F9hDAZ4scXdcTd+OHaAiHsayGahJLssHYgq39sBBV8oeK9GzNCFC+9rn0cFrALb8JTjtOqb+9GBCxyU9/fV4qE577NY6Ze5mCUrG8L9boKHwsW2j74SJAo7dT67xx7F10T8eMk9oYi0rqR+eCHHtQwgZZfTXItElFzWmv/b5ai6W2zzT+XwevijZQyYhrXBH1rRM6vaMKcDSHJAu/odWkXfnpfskHOQDD/34xDsyGPcXyCNxe4skS7LQxX6XeqPuGv3EgtVe8Iouu65vKcw0BoXzouiG9OE4lSMlYCxViNCr4B0EYQoJGPCcIEpQcGOosxwRqn1ucCetfyWTALZsEsmAWzYFgQfCjY6gLZFgua39v8QAU2UHzWsiZ50omv9OkkOemJoH/VW3jgQF77+L0fYINzwQ9KZNijJ6TJqTY6nHiDZdOH39L1n7mRHkkuM++Z7+r4D2hVbEujx/8RAAAAAElFTkSuQmCC[]
// TODO once CI is setup image::https://github.com/tomitribe/churchkey/workflows/Github%20CI/badge.svg[Github Action Build]

Churchkey is a Java library that can parse and export public and private key files in several formats including:

- JSON Web Key (JWK)
- JSON Web Key (JWK)
- PEM
- OpenSSH
- SSH2

## Reading keys
== Reading keys

*Step 1* Pass the bytes of the key file to Churchkey. No need to tell Churchkey what kind of key it is.

Expand Down Expand Up @@ -42,7 +46,7 @@ Assert.assertTrue(key.getKey() instanceof RSAPublicKey);

See the complete link:https://github.com/tomitribe/churchkey/blob/master/src/test/java/org/supertribe/ExampleTest.java#L39[source]

## Writing/Converting Keys
== Writing/Converting Keys

The following will read (decode) a PEM file and then convert (encode) it to a JWK format

Expand Down Expand Up @@ -70,7 +74,7 @@ JsonAsserts.assertJson(expected, jwk);

See the complete link:https://github.com/tomitribe/churchkey/blob/master/src/test/java/org/supertribe/Pem2JwkTest.java#L48[source]

## Get the Public key from a Private key
== Get the Public key from a Private key

The following will read (decode) a private key PEM file and then obtain and write out the public PEM.

Expand Down Expand Up @@ -102,10 +106,10 @@ assertEquals("" +
See the complete link:https://github.com/tomitribe/churchkey/blob/master/src/test/java/org/supertribe/PublicFromPrivateTest.java#L41[source]


## Supported Key Formats
== Supported Key Formats
Churchkey is a Java library that can read RSA and DSA that look like any of the following:

### JSON Web Key (JWK)
=== JSON Web Key (JWK)

[source,json]
----
Expand All @@ -122,7 +126,9 @@ Churchkey is a Java library that can read RSA and DSA that look like any of the
}
----

### Various PEM Files
NOTE: requires `nanojson` library.

=== Various PEM Files
----
-----BEGIN RSA PRIVATE KEY-----
MIICXQIBAAKBgQCyzNurU19lqnYhx5QI72sIX1lh8cTehTmboC+DLG7UuaUHqs09
Expand Down Expand Up @@ -169,12 +175,12 @@ eDVsA5nc7qTnsSgULXTxwHSF286IJdco5kasaJm4Xurlm3V+2oiTugraBsi1J0Ht
-----END PUBLIC KEY-----
----

### OpenSSH
=== OpenSSH

Common locations for these would be in:

- `~/.ssh/id_rsa` (PEM format shown above)
- `~/.ssh/id_rsa.pub` (`ssh-` format shown here)
- `~/.ssh/id_rsa` (PEM format shown above)
- `~/.ssh/id_rsa.pub` (`ssh-` format shown here)

----
ssh-dss AAAAB3NzaC1kc3MAAACBAN9w84QLHWmzl/gY2Xh/CnM7hfTsUl6Z89NUmhOFfs/wzO54Pl84qKjWmlhJO9VGFwsMRbw0EqGgS1eBngv+DR/eMAN+0KnLTPTNEajKP/ibTRf3sI3Rf7UTYhSp7W5r5FB8TN39chg9JQUR7c0ALOdbyDL8d+yhB5SzLEAWQ4QTAAAAFQCcu9GKMJJyX8go6w1gn93Xi1/EDwAAAIBJYC9VGyg80b7DF8+fHKfezGEjjRgJOVMJQA946vA3A+cntFUU+Y1LayXJ2y... dblevins@mingus.lan
Expand All @@ -184,7 +190,7 @@ ssh-dss AAAAB3NzaC1kc3MAAACBAN9w84QLHWmzl/gY2Xh/CnM7hfTsUl6Z89NUmhOFfs/wzO54Pl84
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAAAgQCyzNurU19lqnYhx5QI72sIX1lh8cTehTmboC+DLG7UuaUHqs096M754HtP2IiHFcIQqwYNzHgKmjmfGdbk9JBkz/DNeDVsA5nc7qTnsSgULXTxwHSF286IJdco5kasaJm4Xurlm3V+2oiTugraBsi1J0Ht0OtHgJIlIaGxK7mY/Q== dblevins@mingus.lan
----

### SSH2
=== SSH2

Commonly mistaken for PEM, but different.

Expand All @@ -197,14 +203,30 @@ wHSF286IJdco5kasaJm4Xurlm3V+2oiTugraBsi1J0Ht0OtHgJIlIaGxK7mY/Q==
---- END SSH2 PUBLIC KEY ----
----

## Maven Coordinates
== Maven Coordinates

[source,xml]
----
<dependency>
<groupId>org.tomitribe</groupId>
<artifactId>churchkey</artifactId>
<version>0.14</version>
<version>${churchkey.version}</version>
</dependency>
----

Note that on Java 17, you can exclude all transitive dependencies if you only use PEM format:

[source,xml]
----
<dependency>
<groupId>org.tomitribe</groupId>
<artifactId>churchkey</artifactId>
<version>${churchkey.version}</version>
<exclusions>
<exclusion>
<groupId>*</groupId>
<artifactId>*</artifactId>
</exclusion>
</exclusions>
</dependency>
----
30 changes: 30 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -174,10 +174,39 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M7</version>
<executions>
<execution>
<id>pem-dep-free</id>
<phase>none</phase>
<goals>
<goal>test</goal>
</goals>
<configuration>
<forkCount>1</forkCount>
<jdkToolchain>
<version>17</version>
</jdkToolchain>
<failIfNoTests>true</failIfNoTests>
<includes>
<include>**/pem/*Test.java</include>
</includes>
<systemPropertyVariables>
<test.json>false</test.json>
<test.tomitribe-util>false</test.tomitribe-util>
</systemPropertyVariables>
<classpathDependencyExcludes>
<classpathDependencyExclude>com.grack:nanojson</classpathDependencyExclude>
<classpathDependencyExclude>org.tomitribe:tomitribe-util</classpathDependencyExclude>
</classpathDependencyExcludes>
</configuration>
</execution>
</executions>
<configuration>
<forkCount>4</forkCount>
<reuseForks>true</reuseForks>
<argLine>-Xmx128m</argLine>
<trimStackTrace>false</trimStackTrace>
</configuration>
</plugin>
<plugin>
Expand All @@ -192,6 +221,7 @@
</goals>
<configuration>
<minimizeJar>true</minimizeJar>
<dependencyReducedPomLocation>${project.build.directory}/reduced-pom.xml</dependencyReducedPomLocation>
<relocations>
<relocation>
<pattern>com.grack.nanojson</pattern>
Expand Down
7 changes: 3 additions & 4 deletions src/main/java/io/churchkey/Keys.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,10 @@
*/
package io.churchkey;

import org.tomitribe.util.IO;

import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.security.KeyPair;
import java.security.PrivateKey;
import java.security.interfaces.DSAPrivateKey;
Expand Down Expand Up @@ -69,7 +68,7 @@ public static Key decode(final String contents) throws IOException {
* @return a {@link Key} instance that has metadata and wraps the parsed {@link java.security.Key}
*/
public static Key decode(final File file) throws IOException {
return decode(IO.readBytes(file));
return decode(Files.readAllBytes(file.toPath()));
}

/**
Expand Down Expand Up @@ -119,7 +118,7 @@ public static List<Key> decodeSet(final String contents) throws IOException {
* @return a {@link Key} instance that has metadata and wraps the parsed {@link java.security.Key}
*/
public static List<Key> decodeSet(final File file) throws IOException {
return decodeSet(IO.readBytes(file));
return decodeSet(Files.readAllBytes(file.toPath()));
}

/**
Expand Down
16 changes: 9 additions & 7 deletions src/main/java/io/churchkey/asn1/Asn1Dump.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,25 @@
package io.churchkey.asn1;

import io.churchkey.util.Pem;
import org.tomitribe.util.IO;
import org.tomitribe.util.Pipe;
import org.tomitribe.util.PrintString;

import java.io.File;
import java.io.IOException;
import java.io.PrintStream;
import java.nio.file.Files;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

import static io.churchkey.util.Printers.printer;

public class Asn1Dump {

private Asn1Dump() {
}

public static void print(final byte[] bytes) throws IOException {
final File der = File.createTempFile("der", ".dump");
IO.copy(bytes, der);
Files.write(der.toPath(), bytes);

final ProcessBuilder builder = new ProcessBuilder("openssl", "asn1parse", "-i", "-inform", "DER", "-in", der.getAbsolutePath(), "-dump");
final Process process = builder.start();
Expand All @@ -53,13 +55,13 @@ public static String dump(final byte[] bytes) throws IOException {

if (bytes[0] == '-' && bytes[1] == '-'){
final Pem pem = Pem.parse(bytes);
IO.copy(pem.getData(), der);
Files.write(der.toPath(), pem.getData());
} else {
IO.copy(bytes, der);
Files.write(der.toPath(), bytes);
}

final PrintString err = new PrintString();
final PrintString out = new PrintString();
final PrintStream out = printer();
final PrintStream err = printer();
final ProcessBuilder builder = new ProcessBuilder("openssl", "asn1parse", "-i", "-inform", "DER", "-in", der.getAbsolutePath(), "-dump");
final Process process = builder.start();
final Future<Pipe> o = Pipe.pipe(process.getInputStream(), out);
Expand Down
13 changes: 7 additions & 6 deletions src/main/java/io/churchkey/asn1/Oid.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@
*/
package io.churchkey.asn1;

import org.tomitribe.util.Hex;
import org.tomitribe.util.Join;

import java.io.ByteArrayOutputStream;
import java.io.EOFException;
import java.io.IOException;
Expand All @@ -29,6 +26,10 @@
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static io.churchkey.util.Utils.fromHexString;
import static io.churchkey.util.Utils.toHexString;
import static java.util.stream.Collectors.joining;

/**
* OIDs are encoded using Variable-Length Quantity.
*
Expand Down Expand Up @@ -65,11 +66,11 @@ public List<Integer> getComponents() {

@Override
public String toString() {
return Join.join(".", oid);
return oid.stream().map(Object::toString).collect(joining("."));
}

public String toHex() {
return Hex.toString(toBytes());
return toHexString(toBytes());
}

public byte[] toBytes() {
Expand Down Expand Up @@ -115,7 +116,7 @@ public static Oid fromString(final String dottedIntegers) {
}

public static Oid fromHex(final String hex) throws IOException {
final byte[] bytes = Hex.fromString(hex);
final byte[] bytes = fromHexString(hex);
return fromBytes(bytes);
}

Expand Down
Loading