Skip to content

Commit ee709ed

Browse files
authored
Merge pull request #108 from cconlon/convertKeyStoreToWKS
Add utility method to convert JKS/PKCS12 KeyStore streams to WKS type
2 parents 783c927 + bda62a8 commit ee709ed

File tree

7 files changed

+909
-6
lines changed

7 files changed

+909
-6
lines changed

README_JCE.md

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -234,8 +234,7 @@ SecretKey objects.
234234
#### Converting Other KeyStore Formats to WKS
235235

236236
The Java `keytool` application can be used to convert between KeyStore formats.
237-
This can be easily used for example to convert a JKS KeyStore into a WKS
238-
format KeyStore.
237+
This can be easily used to convert a JKS KeyStore into a WKS format KeyStore.
239238

240239
The following example command would convert a KeyStore in JKS format named
241240
`server.jks` to a KeyStore in WKS format named `server.wks`:
@@ -248,6 +247,50 @@ keytool -importkeystore -srckeystore server.jks -destkeystore server.wks \
248247
--providerpath /path/to/wolfcrypt-jni.jar
249248
```
250249

250+
Additionally, wolfJCE provides a utility method `WolfCryptUtil.convertKeyStoreToWKS()`
251+
that can be used programmatically to convert KeyStore formats. This method
252+
supports converting from JKS, PKCS12, and WKS formats to WKS format. When
253+
converting from WKS to WKS, the method efficiently returns the same input
254+
stream without performing any conversion.
255+
256+
The method automatically detects the input KeyStore format and handles the
257+
conversion appropriately. It supports the following features:
258+
259+
- Automatic format detection (WKS, JKS, PKCS12)
260+
- Preservation of all certificates and keys from the source KeyStore
261+
- Support for both key entries (with certificate chains) and certificate-only entries
262+
- Efficient handling of WKS input (returns same stream)
263+
- Proper stream handling with mark/reset support for large KeyStores
264+
265+
**FIPS NOTE:** This utility method will call Sun provider code for JKS
266+
and PKCS12. This means that if using wolfCrypt FIPS, these calls will make
267+
calls into non-FIPS compliant cryptography for the conversion. Please take
268+
this into consideration when being used in a FIPS compliant environment.
269+
270+
Example usage:
271+
272+
```java
273+
import com.wolfssl.provider.jce.WolfCryptUtil;
274+
import java.io.InputStream;
275+
import java.security.KeyStore;
276+
277+
/* Load your source KeyStore (JKS, PKCS12, or WKS) */
278+
InputStream sourceStream = ...;
279+
char[] password = "your_password".toCharArray();
280+
281+
/* Convert to WKS format, fail on insert errors */
282+
InputStream wksStream = WolfCryptUtil.convertKeyStoreToWKS(sourceStream, password, true);
283+
284+
/* Load the converted WKS KeyStore */
285+
KeyStore wksStore = KeyStore.getInstance("WKS", "wolfJCE");
286+
wksStore.load(wksStream, password);
287+
```
288+
289+
The method respects the Security properties `wolfjce.mapJKStoWKS` and
290+
`wolfjce.mapPKCS12toWKS` when performing conversions. If these properties are
291+
set to "true", the method will use reflection to find the Sun provider
292+
implementations for JKS and PKCS12 to use for conversion.
293+
251294
To list entries inside a WKS keystore using the `keytool`, a command
252295
similar to the following can be used (with the `-list` option):
253296

@@ -388,7 +431,7 @@ ant build system, please see the main README.md included in this package.
388431
wolfSSL (company) has it's own set of code signing certificates from Oracle
389432
that allow wolfJCE to be authenticated in the Oracle JDK. With each release
390433
of wolfJCE, wolfSSL ships a couple pre-signed versions of the
391-
wolfcrypt-jni.jar, located at:
434+
'wolfcrypt-jni.jar", located at:
392435

393436
wolfcrypt-jni-X.X.X/lib/signed/debug/wolfcrypt-jni.jar
394437
wolfcrypt-jni-X.X.X/lib/signed/release/wolfcrypt-jni.jar

scripts/infer.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ infer --fail-on-issue run -- javac \
8282
src/main/java/com/wolfssl/provider/jce/WolfCryptRandom.java \
8383
src/main/java/com/wolfssl/provider/jce/WolfCryptSecretKeyFactory.java \
8484
src/main/java/com/wolfssl/provider/jce/WolfCryptSignature.java \
85+
src/main/java/com/wolfssl/provider/jce/WolfCryptUtil.java \
8586
src/main/java/com/wolfssl/provider/jce/WolfSSLKeyStore.java
8687

8788
RETVAL=$?

0 commit comments

Comments
 (0)