Skip to content

Commit d68c7b0

Browse files
authored
Add files via upload
1 parent 6edaddb commit d68c7b0

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+3979
-1
lines changed

LICENSE.md

Lines changed: 674 additions & 0 deletions
Large diffs are not rendered by default.

README.md

Lines changed: 513 additions & 1 deletion
Large diffs are not rendered by default.

pom.xml

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>pl.mlodawski.security</groupId>
8+
<artifactId>credentials-support</artifactId>
9+
<version>1.0</version>
10+
11+
<properties>
12+
<maven.compiler.source>21</maven.compiler.source>
13+
<maven.compiler.target>21</maven.compiler.target>
14+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
15+
</properties>
16+
17+
18+
<licenses>
19+
<license>
20+
<name>GNU Lesser General Public License, version 3.0</name>
21+
<url>https://www.gnu.org/licenses/gpl-3.0.txt</url>
22+
<distribution>repo</distribution>
23+
</license>
24+
</licenses>
25+
26+
<developers>
27+
<developer>
28+
<id>michal.mlodawski</id>
29+
<name>Michał Młodawski</name>
30+
<email>michal [ at ] mlodaw.ski</email>
31+
</developer>
32+
</developers>
33+
34+
<scm>
35+
<connection>scm:git:https://github.com/SimpleMethod/PKCS11-Java-Wrapper.git</connection>
36+
<developerConnection>scm:git:ssh://git@github.com/SimpleMethod/PKCS11-Java-Wrapper.git</developerConnection>
37+
<url>https://github.com/SimpleMethod/PKCS11-Java-Wrapper</url>
38+
</scm>
39+
40+
41+
<dependencies>
42+
<dependency>
43+
<groupId>eu.europa.ec.joinup.sd-dss</groupId>
44+
<artifactId>dss-token</artifactId>
45+
<version>6.1.RC1</version>
46+
</dependency>
47+
<dependency>
48+
<groupId>org.bouncycastle</groupId>
49+
<artifactId>bcpkix-jdk18on</artifactId>
50+
<version>1.78.1</version>
51+
</dependency>
52+
<dependency>
53+
<groupId>ru.rutoken</groupId>
54+
<artifactId>pkcs11jna</artifactId>
55+
<version>4.1.0</version>
56+
</dependency>
57+
<dependency>
58+
<groupId>org.slf4j</groupId>
59+
<artifactId>slf4j-api</artifactId>
60+
<version>2.0.13</version>
61+
</dependency>
62+
<dependency>
63+
<groupId>ch.qos.logback</groupId>
64+
<artifactId>logback-classic</artifactId>
65+
<version>1.5.6</version>
66+
</dependency>
67+
<dependency>
68+
<groupId>org.projectlombok</groupId>
69+
<artifactId>lombok</artifactId>
70+
<version>1.18.34</version>
71+
<scope>provided</scope>
72+
</dependency>
73+
<dependency>
74+
<groupId>org.junit.jupiter</groupId>
75+
<artifactId>junit-jupiter-engine</artifactId>
76+
<version>5.10.3</version>
77+
<scope>test</scope>
78+
</dependency>
79+
<dependency>
80+
<groupId>org.junit.jupiter</groupId>
81+
<artifactId>junit-jupiter-api</artifactId>
82+
<version>5.10.3</version>
83+
<scope>test</scope>
84+
</dependency>
85+
<dependency>
86+
<groupId>org.mockito</groupId>
87+
<artifactId>mockito-core</artifactId>
88+
<version>5.12.0</version>
89+
<scope>test</scope>
90+
</dependency>
91+
<dependency>
92+
<groupId>org.mockito</groupId>
93+
<artifactId>mockito-junit-jupiter</artifactId>
94+
<version>5.12.0</version>
95+
<scope>test</scope>
96+
</dependency>
97+
</dependencies>
98+
99+
<build>
100+
<plugins>
101+
<plugin>
102+
<groupId>org.apache.maven.plugins</groupId>
103+
<artifactId>maven-source-plugin</artifactId>
104+
<version>3.3.1</version>
105+
<executions>
106+
<execution>
107+
<id>attach-sources</id>
108+
<goals>
109+
<goal>jar</goal>
110+
</goals>
111+
</execution>
112+
</executions>
113+
</plugin>
114+
<plugin>
115+
<groupId>org.apache.maven.plugins</groupId>
116+
<artifactId>maven-javadoc-plugin</artifactId>
117+
<version>3.8.0</version>
118+
<executions>
119+
<execution>
120+
<id>attach-javadocs</id>
121+
<goals>
122+
<goal>jar</goal>
123+
</goals>
124+
<configuration>
125+
<source>22</source>
126+
<encoding>UTF-8</encoding>
127+
<doclint>none</doclint>
128+
<failOnError>false</failOnError>
129+
</configuration>
130+
</execution>
131+
</executions>
132+
</plugin>
133+
</plugins>
134+
</build>
135+
</project>
Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
1+
package pl.mlodawski.security.example;
2+
3+
import pl.mlodawski.security.pkcs11.*;
4+
import pl.mlodawski.security.pkcs11.model.SupportedAlgorithm;
5+
import pl.mlodawski.security.pkcs11.model.KeyCertificatePair;
6+
import pl.mlodawski.security.pkcs11.model.CertificateInfo;
7+
8+
import java.nio.file.Path;
9+
import java.nio.file.Paths;
10+
import java.util.List;
11+
import java.util.Base64;
12+
import java.util.Scanner;
13+
14+
class PKCS11 {
15+
16+
private final Path PKCS11_WRAPPER_PATH;
17+
private final String PIN;
18+
PKCS11Utils utils = new PKCS11Utils();
19+
20+
public PKCS11(Path pkcs11WrapperPath, String pin) {
21+
this.PKCS11_WRAPPER_PATH = pkcs11WrapperPath;
22+
this.PIN = pin;
23+
}
24+
25+
public void run() {
26+
PKCS11Manager manager = new PKCS11Manager(PKCS11_WRAPPER_PATH, PIN);
27+
28+
try (PKCS11Session session = manager.openSession(0)) {
29+
while (true) {
30+
try {
31+
displayMenu();
32+
int choice = getUserChoice();
33+
34+
switch (choice) {
35+
case 1:
36+
listCertificates(manager, session);
37+
break;
38+
case 2:
39+
signMessage(manager, session);
40+
break;
41+
case 3:
42+
encryptDecryptData(manager, session);
43+
break;
44+
case 4:
45+
listSupportedAlgorithms(manager, session);
46+
break;
47+
case 5:
48+
System.out.println("Exiting...");
49+
return;
50+
default:
51+
System.out.println("Invalid choice. Please try again.");
52+
}
53+
} catch (Exception e) {
54+
System.out.println("An error occurred: " + e.getMessage());
55+
} finally {
56+
session.resetSession();
57+
}
58+
}
59+
} catch (Exception e) {
60+
throw new RuntimeException(e);
61+
}
62+
}
63+
64+
private void displayMenu() {
65+
System.out.println("\n--- PKCS#11 Operations Menu ---");
66+
System.out.println("1. List Available Certificates");
67+
System.out.println("2. Sign a Message");
68+
System.out.println("3. Encrypt and Decrypt Data");
69+
System.out.println("4. List Supported Algorithms");
70+
System.out.println("5. Exit");
71+
System.out.print("Enter your choice: ");
72+
}
73+
74+
private int getUserChoice() {
75+
Scanner scanner = new Scanner(System.in);
76+
return scanner.nextInt();
77+
}
78+
79+
private void listCertificates(PKCS11Manager manager, PKCS11Session session) {
80+
List<KeyCertificatePair> pairs = utils.findPrivateKeysAndCertificates(manager.getPkcs11(), session.getSession());
81+
System.out.println("\nAvailable certificate-key pairs:");
82+
for (int i = 0; i < pairs.size(); i++) {
83+
KeyCertificatePair pair = pairs.get(i);
84+
CertificateInfo certInfo = pair.getCertificateInfo();
85+
System.out.printf("%d: Subject: %s, Issuer: %s, Serial: %s, Not Before: %s, Not After: %s, CKA_ID: %s\n",
86+
i + 1,
87+
certInfo.getSubject(),
88+
certInfo.getIssuer(),
89+
certInfo.getSerialNumber(),
90+
certInfo.getNotBefore(),
91+
certInfo.getNotAfter(),
92+
pair.getCkaId());
93+
}
94+
}
95+
96+
private KeyCertificatePair selectCertificateKeyPair(PKCS11Manager manager, PKCS11Session session) {
97+
List<KeyCertificatePair> pairs = utils.findPrivateKeysAndCertificates(manager.getPkcs11(), session.getSession());
98+
listCertificates(manager, session);
99+
System.out.print("Select a certificate-key pair index: ");
100+
int pairIndex = getUserChoice() - 1;
101+
102+
if (pairIndex < 0 || pairIndex >= pairs.size()) {
103+
throw new IllegalArgumentException("Invalid certificate-key pair index selected.");
104+
}
105+
106+
return pairs.get(pairIndex);
107+
}
108+
109+
private void signMessage(PKCS11Manager manager, PKCS11Session session) throws Exception {
110+
try {
111+
KeyCertificatePair selectedPair = selectCertificateKeyPair(manager, session);
112+
113+
System.out.print("Enter a message to sign: ");
114+
Scanner scanner = new Scanner(System.in);
115+
String messageToSign = scanner.nextLine();
116+
117+
PKCS11Signer signer = new PKCS11Signer();
118+
byte[] signature = signer.signMessage(manager.getPkcs11(), session.getSession(), selectedPair.getKeyHandle(), messageToSign.getBytes());
119+
System.out.println("Signature: " + Base64.getEncoder().encodeToString(signature));
120+
121+
boolean isSignatureValid = signer.verifySignature(messageToSign.getBytes(), signature, selectedPair.getCertificate());
122+
System.out.println("Signature status: " + (isSignatureValid ? "Valid" : "Invalid"));
123+
} catch (Exception e) {
124+
System.out.println("Error during signing: " + e.getMessage());
125+
throw e;
126+
}
127+
}
128+
129+
private void encryptDecryptData(PKCS11Manager manager, PKCS11Session session) {
130+
try {
131+
KeyCertificatePair selectedPair = selectCertificateKeyPair(manager, session);
132+
133+
System.out.print("Enter data to encrypt: ");
134+
Scanner scanner = new Scanner(System.in);
135+
String dataToEncrypt = scanner.nextLine();
136+
137+
PKCS11Crypto decryptor = new PKCS11Crypto();
138+
139+
byte[] encryptedData = decryptor.encryptData(dataToEncrypt.getBytes(), selectedPair.getCertificate());
140+
System.out.println("Data encrypted successfully.");
141+
142+
byte[] decryptedData = decryptor.decryptData(manager.getPkcs11(), session.getSession(), selectedPair.getKeyHandle(), encryptedData);
143+
System.out.println("Decrypted data: " + new String(decryptedData));
144+
145+
if (dataToEncrypt.equals(new String(decryptedData))) {
146+
System.out.println("Encryption and decryption successful: data integrity verified.");
147+
} else {
148+
System.out.println("Warning: Decrypted data does not match original input.");
149+
}
150+
} catch (IllegalArgumentException e) {
151+
System.out.println("Invalid input: " + e.getMessage());
152+
throw e;
153+
} catch (Exception e) {
154+
System.out.println("Error during encryption/decryption: " + e.getMessage());
155+
throw e;
156+
}
157+
}
158+
159+
private void listSupportedAlgorithms(PKCS11Manager manager, PKCS11Session session) {
160+
List<SupportedAlgorithm> algorithms = utils.listSupportedAlgorithms(manager.getPkcs11(), session.getSession(), 0);
161+
System.out.println("\nSupported algorithms:");
162+
for (SupportedAlgorithm algo : algorithms) {
163+
System.out.println(algo);
164+
}
165+
}
166+
}
167+
168+
public class PKCS11Example {
169+
public static void main(String[] args) {
170+
String userDir = System.getProperty("user.dir");
171+
PKCS11 example = new PKCS11(
172+
Paths.get(userDir, "lib", "opensc-pkcs11.dll"),
173+
"123456"
174+
);
175+
example.run();
176+
}
177+
}

0 commit comments

Comments
 (0)