Skip to content

Commit

Permalink
Mavenize the project (#16)
Browse files Browse the repository at this point in the history
* Mavenize the project

Includes:
 - initial pom.xml that supports clean, compile, test & package
 - the libjssl.so has been made a "native resource"
 - changed the logic to load libjssl.so

* make, mvn should fail on test failures
  • Loading branch information
pushkarnk authored Jul 23, 2024
1 parent 6aa810f commit 433376a
Show file tree
Hide file tree
Showing 13 changed files with 138 additions and 9 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
build
target
*.class
**/AES*.java
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ $(BUILD)/bin:

$(BUILD)/classes:
@mkdir -p $@
@mkdir -p $@/resources/native

$(BUILD)/test/bin:
@mkdir -p $@
Expand All @@ -62,7 +63,6 @@ $(BUILD)/bin/%.o: $(NATDIR)/%.c
$(BUILD)/test/bin/%.o: $(TEST_JAVA_DIR)/native/%.c
@cc $(CCFLAGS) -o $@ $<


$(SOLIB): $(OBJS)
@cc ${LDFLAGS} -o $@ $^ -L/usr/local/lib64 -lcrypto -lssl

Expand All @@ -79,6 +79,7 @@ java-build: $(BUILD)/classes $(JAVA_FILES)
@${JAVA_HOME}/bin/javac -d $^

build: $(BUILD)/bin $(SOLIB) gen-code java-build
@cp ${SOLIB} $(BUILD)/classes/resources/native

build-test: $(TEST_JAVA_SRCS) $(BUILD)/test/bin $(TESTLIB) $(TEST_C_OBJS)
@${JAVA_HOME}/bin/javac -cp build/classes -d build/test/classes $(TEST_JAVA_SRCS)
Expand Down
81 changes: 81 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.canonical.openssl</groupId>
<artifactId>openssl-fips-java</artifactId>
<version>0.0.1</version>

<properties>
<maven.compiler.source>21</maven.compiler.source>
<maven.compiler.target>21</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<build>
<plugins>
<!-- Jar Plugin -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.2.0</version>
<configuration>
<classesDirectory>${project.basedir}/build/classes</classesDirectory>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.6.0</version>
<executions>
<execution>
<id>compile-c</id>
<phase>compile</phase>
<goals>
<goal>exec</goal>
</goals>
<configuration>
<executable>make</executable>
<arguments>
<argument>clean</argument>
<argument>build</argument>
</arguments>
<workingDirectory>${project.basedir}</workingDirectory>
</configuration>
</execution>
<execution>
<id>clean</id>
<phase>clean</phase>
<goals>
<goal>exec</goal>
</goals>
<configuration>
<executable>make</executable>
<arguments>
<argument>clean</argument>
</arguments>
<workingDirectory>${project.basedir}</workingDirectory>
</configuration>
</execution>
<execution>
<id>test</id>
<phase>test</phase>
<goals>
<goal>exec</goal>
</goals>
<configuration>
<executable>make</executable>
<arguments>
<argument>clean</argument>
<argument>test</argument>
</arguments>
<workingDirectory>${project.basedir}</workingDirectory>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
3 changes: 2 additions & 1 deletion src/java/com/canonical/openssl/cipher/OpenSSLCipher.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.canonical.openssl.cipher;

import com.canonical.openssl.util.NativeMemoryCleaner;
import com.canonical.openssl.util.NativeLibraryLoader;
import java.lang.ref.Cleaner;
import javax.crypto.CipherSpi;
import javax.crypto.Cipher;
Expand All @@ -16,7 +17,7 @@
abstract public class OpenSSLCipher extends CipherSpi {

static {
System.loadLibrary("jssl");
NativeLibraryLoader.load();
}

private static int UNDECIDED = -1;
Expand Down
3 changes: 2 additions & 1 deletion src/java/com/canonical/openssl/drbg/OpenSSLDrbg.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.canonical.openssl.drbg;

import com.canonical.openssl.util.NativeMemoryCleaner;
import com.canonical.openssl.util.NativeLibraryLoader;
import java.lang.ref.Cleaner;
import java.security.SecureRandomSpi;
import java.security.SecureRandomParameters;
Expand All @@ -11,7 +12,7 @@ public class OpenSSLDrbg extends SecureRandomSpi {

public static int DEFAULT_STRENGTH = 128;
static {
System.loadLibrary("jssl");
NativeLibraryLoader.load();
}

long drbgContext;
Expand Down
3 changes: 2 additions & 1 deletion src/java/com/canonical/openssl/kdf/OpenSSLPBKDF2.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.canonical.openssl.kdf;

import com.canonical.openssl.util.NativeLibraryLoader;
import java.security.spec.KeySpec;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
Expand Down Expand Up @@ -27,7 +28,7 @@
public class OpenSSLPBKDF2 extends SecretKeyFactorySpi {

static {
System.loadLibrary("jssl");
NativeLibraryLoader.load();
}

public class PBKDF2SecretKey implements PBEKey {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.canonical.openssl.keyagreement;

import com.canonical.openssl.util.NativeMemoryCleaner;
import com.canonical.openssl.util.NativeLibraryLoader;
import java.lang.ref.Cleaner;
import java.security.Key;
import java.security.SecureRandom;
Expand All @@ -11,7 +12,7 @@

abstract public class OpenSSLKeyAgreement extends KeyAgreementSpi {
static {
System.loadLibrary("jssl");
NativeLibraryLoader.load();
}

public static int AGREEMENT_DH = 0;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.canonical.openssl.keyencapsulation;

import com.canonical.openssl.util.NativeMemoryCleaner;
import com.canonical.openssl.util.NativeLibraryLoader;
import java.lang.ref.Cleaner;
import javax.crypto.DecapsulateException;
import javax.crypto.KEM;
Expand All @@ -22,7 +23,7 @@
final public class OpenSSLKEMRSA implements KEMSpi {

static {
System.loadLibrary("jssl");
NativeLibraryLoader.load();
}

@Override
Expand Down
3 changes: 2 additions & 1 deletion src/java/com/canonical/openssl/mac/OpenSSLMAC.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.canonical.openssl.mac;

import com.canonical.openssl.util.NativeMemoryCleaner;
import com.canonical.openssl.util.NativeLibraryLoader;
import java.lang.ref.Cleaner;
import java.nio.ByteBuffer;
import java.security.Key;
Expand All @@ -12,7 +13,7 @@
public abstract class OpenSSLMAC extends MacSpi {

static {
System.loadLibrary("jssl");
NativeLibraryLoader.load();
}


Expand Down
3 changes: 2 additions & 1 deletion src/java/com/canonical/openssl/md/OpenSSLMD.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.canonical.openssl.md;

import com.canonical.openssl.util.NativeMemoryCleaner;
import com.canonical.openssl.util.NativeLibraryLoader;

import java.lang.ref.Cleaner;
import java.nio.ByteBuffer;
Expand All @@ -13,7 +14,7 @@
public abstract class OpenSSLMD extends MessageDigestSpi {

static {
System.loadLibrary("jssl");
NativeLibraryLoader.load();
}

private static class MDState implements Runnable {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.canonical.openssl.key.*;
import com.canonical.openssl.util.NativeMemoryCleaner;
import com.canonical.openssl.util.NativeLibraryLoader;

import java.lang.ref.Cleaner;
import java.nio.ByteBuffer;
Expand All @@ -17,7 +18,7 @@
public abstract class OpenSSLSignature extends SignatureSpi {

static {
System.loadLibrary("jssl");
NativeLibraryLoader.load();
}

private static class SignatureState implements Runnable {
Expand Down
37 changes: 37 additions & 0 deletions src/java/com/canonical/openssl/util/NativeLibraryLoader.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.canonical.openssl.util;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;

public class NativeLibraryLoader {
static String libFileName = "libjssl.so";
static String location = "/resources/native/";
static boolean loaded = false;

public static synchronized void load() {
if (loaded)
return;

try {
InputStream in = NativeLibraryLoader.class.getResourceAsStream(location + libFileName);

File tempFile = Files.createTempFile(libFileName, "").toFile();
tempFile.deleteOnExit();

try (FileOutputStream out = new FileOutputStream(tempFile)) {
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = in.read(buffer)) != -1) {
out.write(buffer, 0, bytesRead);
}
}
System.load(tempFile.getAbsolutePath());
loaded = true;
} catch (Exception e) {
throw new RuntimeException("Failed to load native libary " + libFileName + ": " + e);
}
}
}
1 change: 1 addition & 0 deletions test/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,4 @@ def run_native_test(name):
for test in failing_tests:
print(test)
print("Please look into build/test/test.out for details")
exit(1)

0 comments on commit 433376a

Please sign in to comment.