├── .gitignore ├── src ├── test │ ├── resources │ │ └── log4j.xml │ └── java │ │ └── org │ │ └── pkcs11 │ │ └── jacknji11 │ │ └── CKATest.java └── main │ ├── java │ └── org │ │ └── pkcs11 │ │ └── jacknji11 │ │ ├── CK_VERSION.java │ │ ├── CK_NOTIFY.java │ │ ├── CK.java │ │ ├── CKZ.java │ │ ├── NativePointer.java │ │ ├── jffi │ │ ├── JFFI_CK_NOTIFY.java │ │ ├── JFFI_CK_VERSION.java │ │ ├── JFFI_CK_MECHANISM_INFO.java │ │ ├── JFFI_CK_SESSION_INFO.java │ │ ├── JFFI_CKA.java │ │ ├── JFFI_CK_SLOT_INFO.java │ │ ├── JFFI_CKM.java │ │ ├── JFFI_CK_INFO.java │ │ ├── Template.java │ │ ├── JFFI_CK_TOKEN_INFO.java │ │ ├── JFFI_CK_C_INITIALIZE_ARGS.java │ │ └── JFFINative.java │ │ ├── LongRef.java │ │ ├── jna │ │ ├── JNA_CK_NOTIFY.java │ │ ├── JNA_CK_VERSION.java │ │ ├── JNA_CKM.java │ │ ├── JNA_CK_MECHANISM_INFO.java │ │ ├── JNA_CK_SESSION_INFO.java │ │ ├── JNA_CK_SLOT_INFO.java │ │ ├── JNA_CK_INFO.java │ │ ├── LongArray.java │ │ ├── JNA_CK_TOKEN_INFO.java │ │ ├── Template.java │ │ ├── JNA_CK_C_INITIALIZE_ARGS.java │ │ └── JNANativeI.java │ │ ├── NativePointerByReference.java │ │ ├── CKU.java │ │ ├── CKC.java │ │ ├── CKH.java │ │ ├── CKS.java │ │ ├── CKG.java │ │ ├── CKRException.java │ │ ├── UBigInt.java │ │ ├── CK_INFO.java │ │ ├── CK_SESSION_INFO.java │ │ ├── CKO.java │ │ ├── CK_SLOT_INFO.java │ │ ├── CKP.java │ │ ├── CKD.java │ │ ├── CK_MECHANISM_INFO.java │ │ ├── CKK.java │ │ ├── CK_C_INITIALIZE_ARGS.java │ │ ├── CK_TOKEN_INFO.java │ │ ├── ULong.java │ │ ├── Buf.java │ │ ├── AttributeLengthStrategy.java │ │ ├── Hex.java │ │ └── jni │ │ └── JNI.java │ ├── javadoc │ └── overview.html │ └── h │ └── rsa │ ├── cryptoki.h │ └── pkcs11.h ├── LICENSE.txt ├── README.md └── pom.xml /.gitignore: -------------------------------------------------------------------------------- 1 | .classpath 2 | .idea 3 | .project 4 | .settings 5 | logs 6 | softhsm2.conf 7 | target 8 | tokens 9 | /bin/ 10 | *.iml 11 | -------------------------------------------------------------------------------- /src/test/resources/log4j.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | The MIT Licence 2 | 3 | Copyright 2010-2011 Joel Hockey (joel.hockey@gmail.com). All rights reserved. 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. -------------------------------------------------------------------------------- /src/main/java/org/pkcs11/jacknji11/CK_VERSION.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2010-2011 Joel Hockey (joel.hockey@gmail.com). All rights reserved. 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy 4 | * of this software and associated documentation files (the "Software"), to deal 5 | * in the Software without restriction, including without limitation the rights 6 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | * copies of the Software, and to permit persons to whom the Software is 8 | * furnished to do so, subject to the following conditions: 9 | * 10 | * The above copyright notice and this permission notice shall be included in 11 | * all copies or substantial portions of the Software. 12 | * 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 | * THE SOFTWARE. 20 | */ 21 | 22 | package org.pkcs11.jacknji11; 23 | 24 | /** 25 | * PKCS#11 CK_VERSION wrapper. 26 | * @author Joel Hockey (joel.hockey@gmail.com) 27 | */ 28 | public class CK_VERSION { 29 | public byte major; 30 | public byte minor; 31 | } 32 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Java Interface for PKCS#11. 2 | 3 | Provides a Java PKCS#11 interface that provides low-level interface 4 | as close as possible to the cryptoki C interface and wraps with 5 | Java-styled interface providing convenience methods and using 6 | exceptions for error handling. 7 | 8 | Uses a provider architecture to allow any implementation of the 9 | native mapping. Includes JNA < https://github.com/java-native-access/jna > 10 | as default provider to bridge between Java and native cryptoki lib. 11 | 12 | # Install 13 | 14 | Build and install with: 15 | 16 | >mvn install 17 | 18 | If you want to build without running the tests, use: 19 | 20 | >mvn install -DskipTests 21 | 22 | # Run tests 23 | The tests, from src/test/java/org/pkcs11/jacknji11/CryptokiTest.java, are run on every call to mvn install. 24 | In order to run the tests on your HSMs (note that not all operations may pass) you can set these environment variables: 25 | 26 | ``` 27 | export JACKNJI11_TEST_TESTSLOT=1762252043 28 | export JACKNJI11_TEST_INITSLOT=1762252043 29 | export JACKNJI11_TEST_SO_PIN=sopin 30 | export JACKNJI11_TEST_USER_PIN=userpin 31 | ``` 32 | 33 | # Loading native cryptoki library 34 | By default, the `cryptoki` library (`cryptoki.dll` or `libcryptoki.so`) must be available (`LD_LIBRARY_PATH` for linux). 35 | You must either copy/symlink your library to have this name, or you can specify the library path using 36 | `JACKNJI11_PKCS11_LIB_PATH`. 37 | 38 | If for example you run SoftHSM2, you have could either: 39 | ``` 40 | export JACKNJI11_PKCS11_LIB_PATH=/usr/lib/softhsm/libsofthsm2.so 41 | ``` 42 | or 43 | ``` 44 | sudo ln -s /usr/lib/softhsm/libsofthsm2.so /usr/lib/softhsm/libcryptoki.so 45 | export LD_LIBRARY_PATH=/usr/lib/softhsm 46 | ``` 47 | -------------------------------------------------------------------------------- /src/main/java/org/pkcs11/jacknji11/CK_NOTIFY.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2010-2011 Joel Hockey (joel.hockey@gmail.com). All rights reserved. 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy 4 | * of this software and associated documentation files (the "Software"), to deal 5 | * in the Software without restriction, including without limitation the rights 6 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | * copies of the Software, and to permit persons to whom the Software is 8 | * furnished to do so, subject to the following conditions: 9 | * 10 | * The above copyright notice and this permission notice shall be included in 11 | * all copies or substantial portions of the Software. 12 | * 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 | * THE SOFTWARE. 20 | */ 21 | 22 | package org.pkcs11.jacknji11; 23 | 24 | /** 25 | * PKCS#11 CK_NOTIFY. 26 | * @author Joel Hockey (joel.hockey@gmail.com) 27 | */ 28 | public interface CK_NOTIFY { 29 | /** 30 | * CK_NOTIFY is an application callback that processes events. 31 | * @param hSession the session's handle 32 | * @param event event 33 | * @param pApplication passed to C_OpenSession 34 | * @return {@link CKR} return code 35 | */ 36 | long invoke(long hSession, long event, NativePointer pApplication); 37 | } 38 | -------------------------------------------------------------------------------- /src/main/java/org/pkcs11/jacknji11/CK.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2010-2011 Joel Hockey (joel.hockey@gmail.com). All rights reserved. 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy 4 | * of this software and associated documentation files (the "Software"), to deal 5 | * in the Software without restriction, including without limitation the rights 6 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | * copies of the Software, and to permit persons to whom the Software is 8 | * furnished to do so, subject to the following conditions: 9 | * 10 | * The above copyright notice and this permission notice shall be included in 11 | * all copies or substantial portions of the Software. 12 | * 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 | * THE SOFTWARE. 20 | */ 21 | package org.pkcs11.jacknji11; 22 | 23 | /** 24 | * PKCS#11 CK_? constants. 25 | * 26 | * @author Tomasz Wysocki 27 | */ 28 | public class CK { 29 | 30 | /** 31 | * Value of invalid handles. 32 | */ 33 | public static final long INVALID_HANDLE = 0x00000000L; 34 | 35 | /** 36 | * Value of infinite used for max session counts. 37 | */ 38 | public static final long EFFECTIVELY_INFINITE = 0x00000000L; 39 | 40 | /** 41 | * Value returned in ulValueLen when attribute is not available. 42 | */ 43 | public static final long UNAVAILABLE_INFORMATION = -1L; 44 | 45 | } 46 | -------------------------------------------------------------------------------- /src/main/java/org/pkcs11/jacknji11/CKZ.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2010-2011 Joel Hockey (joel.hockey@gmail.com). All rights reserved. 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy 4 | * of this software and associated documentation files (the "Software"), to deal 5 | * in the Software without restriction, including without limitation the rights 6 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | * copies of the Software, and to permit persons to whom the Software is 8 | * furnished to do so, subject to the following conditions: 9 | * 10 | * The above copyright notice and this permission notice shall be included in 11 | * all copies or substantial portions of the Software. 12 | * 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 | * THE SOFTWARE. 20 | */ 21 | 22 | package org.pkcs11.jacknji11; 23 | 24 | import java.util.Map; 25 | 26 | /** 27 | * CKZ_? constants. 28 | * @author Joel Hockey (joel.hockey@gmail.com) 29 | */ 30 | public class CKZ { 31 | public static final long DATA_SPECIFIED = 0x00000001; 32 | 33 | /** Maps from long value to String description (variable name). */ 34 | private static final Map L2S = C.createL2SMap(CKZ.class); 35 | /** 36 | * Convert long constant value to name. 37 | * @param ckz value 38 | * @return name 39 | */ 40 | public static final String L2S(long ckz) { return C.l2s(L2S, CKZ.class.getSimpleName(), ckz); } 41 | } 42 | -------------------------------------------------------------------------------- /src/main/java/org/pkcs11/jacknji11/NativePointer.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2008-2011 Joel Hockey (joel.hockey@gmail.com). All rights reserved. 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy 5 | * of this software and associated documentation files (the "Software"), to deal 6 | * in the Software without restriction, including without limitation the rights 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | * copies of the Software, and to permit persons to whom the Software is 9 | * furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in 12 | * all copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 20 | * THE SOFTWARE. 21 | */ 22 | package org.pkcs11.jacknji11; 23 | 24 | /** 25 | * Java wrapper for native pointer 26 | * 27 | * @author Joel Hockey (joel.hockey@gmail.com) 28 | */ 29 | public class NativePointer { 30 | private long address; 31 | /** 32 | * Construct NativePointer with given address 33 | * @param address address 34 | */ 35 | public NativePointer(long address) { 36 | this.address = address; 37 | } 38 | /** @param address memory address */ 39 | public void setAddress(long address) { this.address = address; } 40 | /** @return memory address */ 41 | public long getAddress() { return address; } 42 | } 43 | -------------------------------------------------------------------------------- /src/main/java/org/pkcs11/jacknji11/jffi/JFFI_CK_NOTIFY.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2010-2011 Joel Hockey (joel.hockey@gmail.com). All rights reserved. 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy 4 | * of this software and associated documentation files (the "Software"), to deal 5 | * in the Software without restriction, including without limitation the rights 6 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | * copies of the Software, and to permit persons to whom the Software is 8 | * furnished to do so, subject to the following conditions: 9 | * 10 | * The above copyright notice and this permission notice shall be included in 11 | * all copies or substantial portions of the Software. 12 | * 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 | * THE SOFTWARE. 20 | */ 21 | 22 | package org.pkcs11.jacknji11.jffi; 23 | 24 | import jnr.ffi.Pointer; 25 | import jnr.ffi.annotations.Delegate; 26 | 27 | /** 28 | * JFFI wrapper for PKCS#11 CK_NOTIFY. 29 | * @author Joel Hockey (joel.hockey@gmail.com) 30 | */ 31 | public interface JFFI_CK_NOTIFY { 32 | /** 33 | * CK_NOTIFY is an application callback that processes events. 34 | * @param hSession the session's handle 35 | * @param event event 36 | * @param pApplication passed to C_OpenSession 37 | * @return {@link CKR} return code 38 | */ 39 | @Delegate 40 | long invoke(long hSession, long event, Pointer pApplication); 41 | } 42 | -------------------------------------------------------------------------------- /src/main/java/org/pkcs11/jacknji11/LongRef.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2010-2011 Joel Hockey (joel.hockey@gmail.com). All rights reserved. 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy 4 | * of this software and associated documentation files (the "Software"), to deal 5 | * in the Software without restriction, including without limitation the rights 6 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | * copies of the Software, and to permit persons to whom the Software is 8 | * furnished to do so, subject to the following conditions: 9 | * 10 | * The above copyright notice and this permission notice shall be included in 11 | * all copies or substantial portions of the Software. 12 | * 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 | * THE SOFTWARE. 20 | */ 21 | 22 | package org.pkcs11.jacknji11; 23 | 24 | /** 25 | * Wrapper for native long that can be modified. 26 | * @author Joel Hockey (joel.hockey@gmail.com) 27 | */ 28 | public class LongRef { 29 | public long value; 30 | /** Default constructor */ 31 | public LongRef() { this(0); } 32 | /** 33 | * Constructor taking java long. 34 | * @param value value 35 | */ 36 | public LongRef(long value) { this.value = value; } 37 | 38 | /** @return current value */ 39 | public long value() { return value; } 40 | 41 | /** @return value as string */ 42 | public String toString() { return Long.toString(value); } 43 | } 44 | -------------------------------------------------------------------------------- /src/main/java/org/pkcs11/jacknji11/jna/JNA_CK_NOTIFY.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2010-2011 Joel Hockey (joel.hockey@gmail.com). All rights reserved. 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy 4 | * of this software and associated documentation files (the "Software"), to deal 5 | * in the Software without restriction, including without limitation the rights 6 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | * copies of the Software, and to permit persons to whom the Software is 8 | * furnished to do so, subject to the following conditions: 9 | * 10 | * The above copyright notice and this permission notice shall be included in 11 | * all copies or substantial portions of the Software. 12 | * 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 | * THE SOFTWARE. 20 | */ 21 | 22 | package org.pkcs11.jacknji11.jna; 23 | 24 | import com.sun.jna.Callback; 25 | import com.sun.jna.NativeLong; 26 | import com.sun.jna.Pointer; 27 | 28 | /** 29 | * JNA wrapper for PKCS#11 CK_NOTIFY. 30 | * @author Joel Hockey (joel.hockey@gmail.com) 31 | */ 32 | public interface JNA_CK_NOTIFY extends Callback { 33 | /** 34 | * CK_NOTIFY is an application callback that processes events. 35 | * @param hSession the session's handle 36 | * @param event event 37 | * @param pApplication passed to C_OpenSession 38 | * @return {@link CKR} return code 39 | */ 40 | NativeLong invoke(NativeLong hSession, NativeLong event, Pointer pApplication); 41 | } 42 | -------------------------------------------------------------------------------- /src/main/java/org/pkcs11/jacknji11/NativePointerByReference.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2008-2011 Joel Hockey (joel.hockey@gmail.com). All rights reserved. 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy 5 | * of this software and associated documentation files (the "Software"), to deal 6 | * in the Software without restriction, including without limitation the rights 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | * copies of the Software, and to permit persons to whom the Software is 9 | * furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in 12 | * all copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 20 | * THE SOFTWARE. 21 | */ 22 | package org.pkcs11.jacknji11; 23 | 24 | /** 25 | * Java wrapper for Pointer to NativePointer 26 | * 27 | * @author Joel Hockey (joel.hockey@gmail.com) 28 | */ 29 | public class NativePointerByReference { 30 | private NativePointer pointer; 31 | 32 | /** 33 | * Constructor with pointer 34 | * @param pointer pointer 35 | */ 36 | public NativePointerByReference(NativePointer pointer) { 37 | this.pointer = pointer; 38 | } 39 | /** @param pointer pointer referenced */ 40 | public void setPointer(NativePointer pointer) { this.pointer = pointer; } 41 | /** @return pointer */ 42 | public NativePointer getPointer() { return pointer; } 43 | } 44 | -------------------------------------------------------------------------------- /src/main/java/org/pkcs11/jacknji11/CKU.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2010-2011 Joel Hockey (joel.hockey@gmail.com). All rights reserved. 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy 4 | * of this software and associated documentation files (the "Software"), to deal 5 | * in the Software without restriction, including without limitation the rights 6 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | * copies of the Software, and to permit persons to whom the Software is 8 | * furnished to do so, subject to the following conditions: 9 | * 10 | * The above copyright notice and this permission notice shall be included in 11 | * all copies or substantial portions of the Software. 12 | * 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 | * THE SOFTWARE. 20 | */ 21 | 22 | package org.pkcs11.jacknji11; 23 | 24 | import java.util.Map; 25 | 26 | /** 27 | * CKU_? constants. 28 | * @author Joel Hockey (joel.hockey@gmail.com) 29 | */ 30 | public class CKU { 31 | 32 | public static final long SO = 0x00000000; 33 | public static final long USER = 0x00000001; 34 | public static final long CKU_CONTEXT_SPECIFIC = 0x00000002; 35 | 36 | /** Maps from long value to String description (variable name). */ 37 | private static final Map L2S = C.createL2SMap(CKU.class); 38 | /** 39 | * Convert long constant value to name. 40 | * @param cku value 41 | * @return name 42 | */ 43 | public static final String L2S(long cku) { return C.l2s(L2S, CKU.class.getSimpleName(), cku); } 44 | } 45 | -------------------------------------------------------------------------------- /src/main/java/org/pkcs11/jacknji11/CKC.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2010-2011 Joel Hockey (joel.hockey@gmail.com). All rights reserved. 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy 4 | * of this software and associated documentation files (the "Software"), to deal 5 | * in the Software without restriction, including without limitation the rights 6 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | * copies of the Software, and to permit persons to whom the Software is 8 | * furnished to do so, subject to the following conditions: 9 | * 10 | * The above copyright notice and this permission notice shall be included in 11 | * all copies or substantial portions of the Software. 12 | * 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 | * THE SOFTWARE. 20 | */ 21 | 22 | package org.pkcs11.jacknji11; 23 | 24 | import java.util.Map; 25 | 26 | /** 27 | * CKC_? constants. 28 | * @author Joel Hockey (joel.hockey@gmail.com) 29 | */ 30 | public class CKC { 31 | 32 | public static final long CKC_X_509 = 0x00000000; 33 | public static final long CKC_X_509_ATTR_CERT = 0x00000001; 34 | public static final long CKC_WTLS = 0x00000002; 35 | public static final long CKC_VENDOR_DEFINED = 0x80000000; 36 | 37 | /** Maps from long value to String description (variable name). */ 38 | private static final Map L2S = C.createL2SMap(CKC.class); 39 | /** 40 | * Convert long constant value to name. 41 | * @param ckc value 42 | * @return name 43 | */ 44 | public static final String L2S(long ckc) { return C.l2s(L2S, CKC.class.getSimpleName(), ckc); } 45 | } 46 | -------------------------------------------------------------------------------- /src/main/java/org/pkcs11/jacknji11/CKH.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2010-2011 Joel Hockey (joel.hockey@gmail.com). All rights reserved. 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy 4 | * of this software and associated documentation files (the "Software"), to deal 5 | * in the Software without restriction, including without limitation the rights 6 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | * copies of the Software, and to permit persons to whom the Software is 8 | * furnished to do so, subject to the following conditions: 9 | * 10 | * The above copyright notice and this permission notice shall be included in 11 | * all copies or substantial portions of the Software. 12 | * 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 | * THE SOFTWARE. 20 | */ 21 | 22 | package org.pkcs11.jacknji11; 23 | 24 | import java.util.Map; 25 | 26 | /** 27 | * CKH_? (CKH_USER_INTERFACE) constants. 28 | * @author Joel Hockey (joel.hockey@gmail.com) 29 | */ 30 | public class CKH { 31 | public static final long MONOTONIC_COUNTER = 0x00000001; 32 | public static final long CLOCK = 0x00000002; 33 | public static final long USER_INTERFACE = 0x00000003; 34 | public static final long VENDOR_DEFINED = 0x80000000; 35 | 36 | /** Maps from long value to String description (variable name). */ 37 | private static final Map L2S = C.createL2SMap(CKH.class); 38 | /** 39 | * Convert long constant value to name. 40 | * @param ckh value 41 | * @return name 42 | */ 43 | public static final String L2S(long ckh) { return C.l2s(L2S, CKH.class.getSimpleName(), ckh); } 44 | } 45 | -------------------------------------------------------------------------------- /src/main/java/org/pkcs11/jacknji11/CKS.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2010-2011 Joel Hockey (joel.hockey@gmail.com). All rights reserved. 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy 4 | * of this software and associated documentation files (the "Software"), to deal 5 | * in the Software without restriction, including without limitation the rights 6 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | * copies of the Software, and to permit persons to whom the Software is 8 | * furnished to do so, subject to the following conditions: 9 | * 10 | * The above copyright notice and this permission notice shall be included in 11 | * all copies or substantial portions of the Software. 12 | * 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 | * THE SOFTWARE. 20 | */ 21 | 22 | package org.pkcs11.jacknji11; 23 | 24 | import java.util.Map; 25 | 26 | /** 27 | * CKS_? (CK_STATE) constants. 28 | * @author Joel Hockey (joel.hockey@gmail.com) 29 | */ 30 | public class CKS { 31 | public static final long RO_PUBLIC_SESSION = 0; 32 | public static final long RO_USER_FUNCTIONS = 1; 33 | public static final long RW_PUBLIC_SESSION = 2; 34 | public static final long RW_USER_FUNCTIONS = 3; 35 | public static final long RW_SO_FUNCTIONS = 4; 36 | 37 | /** Maps from long value to String description (variable name). */ 38 | private static final Map L2S = C.createL2SMap(CKS.class); 39 | /** 40 | * Convert long constant value to name. 41 | * @param cks value 42 | * @return name 43 | */ 44 | public static final String L2S(long cks) { return C.l2s(L2S, CKS.class.getSimpleName(), cks); } 45 | } 46 | -------------------------------------------------------------------------------- /src/main/java/org/pkcs11/jacknji11/CKG.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2010-2011 Joel Hockey (joel.hockey@gmail.com). All rights reserved. 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy 4 | * of this software and associated documentation files (the "Software"), to deal 5 | * in the Software without restriction, including without limitation the rights 6 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | * copies of the Software, and to permit persons to whom the Software is 8 | * furnished to do so, subject to the following conditions: 9 | * 10 | * The above copyright notice and this permission notice shall be included in 11 | * all copies or substantial portions of the Software. 12 | * 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 | * THE SOFTWARE. 20 | */ 21 | 22 | package org.pkcs11.jacknji11; 23 | 24 | import java.util.Map; 25 | 26 | /** 27 | * CKG_? constants. 28 | * @author Joel Hockey (joel.hockey@gmail.com) 29 | */ 30 | public class CKG { 31 | public static final long MGF1_SHA1 = 0x00000001; 32 | public static final long MGF1_SHA256 = 0x00000002; 33 | public static final long MGF1_SHA384 = 0x00000003; 34 | public static final long MGF1_SHA512 = 0x00000004; 35 | public static final long MGF1_SHA224 = 0x00000005; 36 | 37 | /** Maps from long value to String description (variable name). */ 38 | private static final Map L2S = C.createL2SMap(CKG.class); 39 | /** 40 | * Convert long constant value to name. 41 | * @param ckg value 42 | * @return name 43 | */ 44 | public static final String L2S(long ckg) { return C.l2s(L2S, CKG.class.getSimpleName(), ckg); } 45 | } 46 | -------------------------------------------------------------------------------- /src/main/java/org/pkcs11/jacknji11/jffi/JFFI_CK_VERSION.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2010-2011 Joel Hockey (joel.hockey@gmail.com). All rights reserved. 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy 4 | * of this software and associated documentation files (the "Software"), to deal 5 | * in the Software without restriction, including without limitation the rights 6 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | * copies of the Software, and to permit persons to whom the Software is 8 | * furnished to do so, subject to the following conditions: 9 | * 10 | * The above copyright notice and this permission notice shall be included in 11 | * all copies or substantial portions of the Software. 12 | * 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 | * THE SOFTWARE. 20 | */ 21 | 22 | package org.pkcs11.jacknji11.jffi; 23 | 24 | import jnr.ffi.Struct; 25 | 26 | import org.pkcs11.jacknji11.CK_VERSION; 27 | 28 | /** 29 | * JFFI wrapper for PKCS#11 CK_VERSION. It hardly seems worthwhile 30 | * wrapping 2 bytes, but we have. 31 | * @author Joel Hockey (joel.hockey@gmail.com) 32 | */ 33 | public class JFFI_CK_VERSION extends Struct { 34 | public byte major; 35 | public byte minor; 36 | 37 | public JFFI_CK_VERSION() { 38 | super(jnr.ffi.Runtime.getSystemRuntime()); 39 | } 40 | 41 | public JFFI_CK_VERSION readFrom(CK_VERSION version) { 42 | major = version.major; 43 | minor = version.minor; 44 | return this; 45 | } 46 | public CK_VERSION writeTo(CK_VERSION version) { 47 | version.major = major; 48 | version.minor = minor; 49 | return version; 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /src/main/java/org/pkcs11/jacknji11/jna/JNA_CK_VERSION.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2010-2011 Joel Hockey (joel.hockey@gmail.com). All rights reserved. 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy 4 | * of this software and associated documentation files (the "Software"), to deal 5 | * in the Software without restriction, including without limitation the rights 6 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | * copies of the Software, and to permit persons to whom the Software is 8 | * furnished to do so, subject to the following conditions: 9 | * 10 | * The above copyright notice and this permission notice shall be included in 11 | * all copies or substantial portions of the Software. 12 | * 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 | * THE SOFTWARE. 20 | */ 21 | 22 | package org.pkcs11.jacknji11.jna; 23 | 24 | import org.pkcs11.jacknji11.CK_VERSION; 25 | 26 | import java.util.Arrays; 27 | import java.util.List; 28 | 29 | import com.sun.jna.Structure; 30 | 31 | /** 32 | * JNA wrapper for PKCS#11 CK_VERSION. It hardly seems worthwhile 33 | * wrapping 2 bytes, but we have. 34 | * @author Joel Hockey (joel.hockey@gmail.com) 35 | */ 36 | public class JNA_CK_VERSION extends Structure { 37 | public byte major; 38 | public byte minor; 39 | 40 | @Override 41 | protected List getFieldOrder() { 42 | return Arrays.asList("major", "minor"); 43 | } 44 | 45 | public JNA_CK_VERSION readFrom(CK_VERSION version) { 46 | major = version.major; 47 | minor = version.minor; 48 | return this; 49 | } 50 | public CK_VERSION writeTo(CK_VERSION version) { 51 | version.major = major; 52 | version.minor = minor; 53 | return version; 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /src/main/javadoc/overview.html: -------------------------------------------------------------------------------- 1 | 2 | jacknji11 provides 3 interfaces for calling cryptoki functions. 3 |
    4 |
  1. {@link org.pkcs11.jacknji11.NativeProvider} provides the lowest level 5 | direct mapping to the 'C_*' functions. There is little 6 | reason why you would ever want to invoke it directly, but you can. 7 |
  2. {@link org.pkcs11.jacknji11.C} provides the exact same functions 8 | as {@link org.pkcs11.jacknji11.NativeProvider} by calling through to the 9 | corresponding native method. The 'C_' at the start of the 10 | function name is removed since the 'C.' when you call the 11 | static methods of this class looks similar. In addition to calling 12 | the native methods, {@link org.pkcs11.jacknji11.C} provides logging 13 | through apache commons logging. You can use this if you require fine-grain 14 | control over something such as checking 15 | {@link org.pkcs11.jacknji11.CKR} return codes. 16 |
  3. {@link org.pkcs11.jacknji11.CE} (Cryptoki 17 | with Exceptions) provides the most user-friendly interface 18 | and is the preferred interface to use. It calls 19 | related function(s) in {@link org.pkcs11.jacknji11.C}, 20 | and converts any non-zero return values into a 21 | {@link org.pkcs11.jacknji11.CKRException}. It automatically resizes 22 | arrays and other helpful things. 23 |
24 | 25 |

Example usage: 26 |

27 |         int TESTSLOT = 0;
28 |         byte[] USER_PIN = "userpin".getBytes();
29 |         int session = CE.OpenSession(TESTSLOT);
30 |         CE.LoginUser(session, USER_PIN);
31 | 
32 |         int des3key = CE.GenerateKey(session, new CKM(CKM.DES3_KEY_GEN),
33 |                 new CKA(CKA.VALUE_LEN, 24),
34 |                 new CKA(CKA.LABEL, "label"),
35 |                 new CKA(CKA.SENSITIVE, false),
36 |                 new CKA(CKA.DERIVE, true));
37 | 
38 |         CE.EncryptInit(session, new CKM(CKM.DES3_CBC_PAD), des3key);
39 |         byte[] plaintext = new byte[10];
40 |         byte[] encrypted = CE.Encrypt(session, plaintext);
41 | 
42 |         CE.DecryptInit(session, new CKM(CKM.DES3_CBC_PAD), des3key);
43 |         byte[] decrypted = CE.Decrypt(session, encrypted);
44 |         assertTrue(Arrays.equals(plaintext, decrypted));
45 | 
46 | -------------------------------------------------------------------------------- /src/main/java/org/pkcs11/jacknji11/jffi/JFFI_CK_MECHANISM_INFO.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2010-2011 Joel Hockey (joel.hockey@gmail.com). All rights reserved. 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy 4 | * of this software and associated documentation files (the "Software"), to deal 5 | * in the Software without restriction, including without limitation the rights 6 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | * copies of the Software, and to permit persons to whom the Software is 8 | * furnished to do so, subject to the following conditions: 9 | * 10 | * The above copyright notice and this permission notice shall be included in 11 | * all copies or substantial portions of the Software. 12 | * 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 | * THE SOFTWARE. 20 | */ 21 | 22 | package org.pkcs11.jacknji11.jffi; 23 | 24 | import jnr.ffi.Struct; 25 | 26 | import org.pkcs11.jacknji11.CK_MECHANISM_INFO; 27 | 28 | /** 29 | * JFFI wrapper for PKCS#11 CK_MECHANSIM_INFO struct. 30 | * @author Joel Hockey (joel.hockey@gmail.com) 31 | */ 32 | public class JFFI_CK_MECHANISM_INFO extends Struct { 33 | public long ulMinKeySize; 34 | public long ulMaxKeySize; 35 | public long flags; 36 | 37 | public JFFI_CK_MECHANISM_INFO() { 38 | super(jnr.ffi.Runtime.getSystemRuntime()); 39 | } 40 | 41 | public JFFI_CK_MECHANISM_INFO readFrom(CK_MECHANISM_INFO info) { 42 | ulMinKeySize = info.ulMinKeySize; 43 | ulMaxKeySize = info.ulMaxKeySize; 44 | flags = info.flags; 45 | return this; 46 | } 47 | 48 | public CK_MECHANISM_INFO writeTo(CK_MECHANISM_INFO info) { 49 | info.ulMinKeySize = ulMinKeySize; 50 | info.ulMaxKeySize = ulMaxKeySize; 51 | info.flags = flags; 52 | return info; 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /src/main/h/rsa/cryptoki.h: -------------------------------------------------------------------------------- 1 | /* cryptoki.h include file for PKCS #11. */ 2 | /* $Revision: 1.4 $ */ 3 | 4 | /* License to copy and use this software is granted provided that it is 5 | * identified as "RSA Security Inc. PKCS #11 Cryptographic Token Interface 6 | * (Cryptoki)" in all material mentioning or referencing this software. 7 | 8 | * License is also granted to make and use derivative works provided that 9 | * such works are identified as "derived from the RSA Security Inc. PKCS #11 10 | * Cryptographic Token Interface (Cryptoki)" in all material mentioning or 11 | * referencing the derived work. 12 | 13 | * RSA Security Inc. makes no representations concerning either the 14 | * merchantability of this software or the suitability of this software for 15 | * any particular purpose. It is provided "as is" without express or implied 16 | * warranty of any kind. 17 | */ 18 | 19 | /* This is a sample file containing the top level include directives 20 | * for building Win32 Cryptoki libraries and applications. 21 | */ 22 | 23 | #ifndef ___CRYPTOKI_H_INC___ 24 | #define ___CRYPTOKI_H_INC___ 25 | 26 | #pragma pack(push, cryptoki, 1) 27 | 28 | /* Specifies that the function is a DLL entry point. */ 29 | #define CK_IMPORT_SPEC __declspec(dllimport) 30 | 31 | /* Define CRYPTOKI_EXPORTS during the build of cryptoki libraries. Do 32 | * not define it in applications. 33 | */ 34 | #ifdef CRYPTOKI_EXPORTS 35 | /* Specified that the function is an exported DLL entry point. */ 36 | #define CK_EXPORT_SPEC __declspec(dllexport) 37 | #else 38 | #define CK_EXPORT_SPEC CK_IMPORT_SPEC 39 | #endif 40 | 41 | /* Ensures the calling convention for Win32 builds */ 42 | #define CK_CALL_SPEC __cdecl 43 | 44 | #define CK_PTR * 45 | 46 | #define CK_DEFINE_FUNCTION(returnType, name) \ 47 | returnType CK_EXPORT_SPEC CK_CALL_SPEC name 48 | 49 | #define CK_DECLARE_FUNCTION(returnType, name) \ 50 | returnType CK_EXPORT_SPEC CK_CALL_SPEC name 51 | 52 | #define CK_DECLARE_FUNCTION_POINTER(returnType, name) \ 53 | returnType CK_IMPORT_SPEC (CK_CALL_SPEC CK_PTR name) 54 | 55 | #define CK_CALLBACK_FUNCTION(returnType, name) \ 56 | returnType (CK_CALL_SPEC CK_PTR name) 57 | 58 | #ifndef NULL_PTR 59 | #define NULL_PTR 0 60 | #endif 61 | 62 | #include "pkcs11.h" 63 | 64 | #pragma pack(pop, cryptoki) 65 | 66 | #endif /* ___CRYPTOKI_H_INC___ */ 67 | -------------------------------------------------------------------------------- /src/main/java/org/pkcs11/jacknji11/CKRException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2010-2011 Joel Hockey (joel.hockey@gmail.com). All rights reserved. 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy 4 | * of this software and associated documentation files (the "Software"), to deal 5 | * in the Software without restriction, including without limitation the rights 6 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | * copies of the Software, and to permit persons to whom the Software is 8 | * furnished to do so, subject to the following conditions: 9 | * 10 | * The above copyright notice and this permission notice shall be included in 11 | * all copies or substantial portions of the Software. 12 | * 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 | * THE SOFTWARE. 20 | */ 21 | 22 | package org.pkcs11.jacknji11; 23 | 24 | /** 25 | * Exception for CKR values that are non-zero (CKR.OK). 26 | * Used in {@link CE} interface as alternative to returning 27 | * CKR for every function. 28 | * @author Joel Hockey (joel.hockey@gmail.com) 29 | */ 30 | public class CKRException extends RuntimeException { 31 | private static final long serialVersionUID = 0x2841de9d258bab8bL; 32 | private long ckr; 33 | 34 | /** 35 | * Constructor with CKR value. 36 | * @param ckr CKR value. 37 | */ 38 | public CKRException(long ckr) { 39 | super(String.format("0x%08x: %s", ckr, CKR.L2S(ckr))); 40 | this.ckr = ckr; 41 | } 42 | 43 | /** 44 | * Constructor with message and CKR value. 45 | * @param msg message 46 | * @param ckr CKR value 47 | */ 48 | public CKRException(String msg, long ckr) { 49 | super(String.format("0x%08x: %s : %s", ckr, CKR.L2S(ckr), msg)); 50 | this.ckr = ckr; 51 | } 52 | 53 | /** @return CKR value */ 54 | public long getCKR() { return ckr; } 55 | } 56 | -------------------------------------------------------------------------------- /src/main/java/org/pkcs11/jacknji11/jffi/JFFI_CK_SESSION_INFO.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2010-2011 Joel Hockey (joel.hockey@gmail.com). All rights reserved. 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy 4 | * of this software and associated documentation files (the "Software"), to deal 5 | * in the Software without restriction, including without limitation the rights 6 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | * copies of the Software, and to permit persons to whom the Software is 8 | * furnished to do so, subject to the following conditions: 9 | * 10 | * The above copyright notice and this permission notice shall be included in 11 | * all copies or substantial portions of the Software. 12 | * 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 | * THE SOFTWARE. 20 | */ 21 | 22 | package org.pkcs11.jacknji11.jffi; 23 | 24 | import jnr.ffi.Struct; 25 | 26 | import org.pkcs11.jacknji11.CK_SESSION_INFO; 27 | 28 | /** 29 | * JFFI wrapper for PKCS#11 CK_SESSION_INFO struct. 30 | * @author Joel Hockey (joel.hockey@gmail.com) 31 | */ 32 | public class JFFI_CK_SESSION_INFO extends Struct { 33 | public long slotID; 34 | public long state; 35 | public long flags; 36 | public long ulDeviceError; 37 | 38 | public JFFI_CK_SESSION_INFO() { 39 | super(jnr.ffi.Runtime.getSystemRuntime()); 40 | } 41 | 42 | 43 | public JFFI_CK_SESSION_INFO readFrom(CK_SESSION_INFO info) { 44 | slotID = info.slotID; 45 | state = info.state; 46 | flags = info.flags; 47 | ulDeviceError = info.ulDeviceError; 48 | return this; 49 | } 50 | 51 | public CK_SESSION_INFO writeTo(CK_SESSION_INFO info) { 52 | info.slotID = slotID; 53 | info.state = state; 54 | info.flags = flags; 55 | info.ulDeviceError = ulDeviceError; 56 | return info; 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /src/main/java/org/pkcs11/jacknji11/jffi/JFFI_CKA.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2010-2011 Joel Hockey (joel.hockey@gmail.com). All rights reserved. 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy 4 | * of this software and associated documentation files (the "Software"), to deal 5 | * in the Software without restriction, including without limitation the rights 6 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | * copies of the Software, and to permit persons to whom the Software is 8 | * furnished to do so, subject to the following conditions: 9 | * 10 | * The above copyright notice and this permission notice shall be included in 11 | * all copies or substantial portions of the Software. 12 | * 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 | * THE SOFTWARE. 20 | */ 21 | 22 | package org.pkcs11.jacknji11.jffi; 23 | 24 | import jnr.ffi.Memory; 25 | import jnr.ffi.Struct; 26 | 27 | import org.pkcs11.jacknji11.CKA; 28 | 29 | /** 30 | * JFFI CK_ATTRIBUTE wrapper. 31 | * @author Joel Hockey (joel.hockey@gmail.com) 32 | */ 33 | public class JFFI_CKA extends Struct { 34 | public long type; 35 | public jnr.ffi.Pointer pValue; 36 | public long ulValueLen; 37 | 38 | public JFFI_CKA() { 39 | super(jnr.ffi.Runtime.getSystemRuntime()); 40 | } 41 | 42 | public JFFI_CKA readFrom(CKA cka) { 43 | type = cka.type; 44 | int len = cka.pValue != null ? cka.pValue.length : 0; 45 | if (len > 0) { 46 | pValue = Memory.allocate(jnr.ffi.Runtime.getSystemRuntime(), len); 47 | pValue.put(0, cka.pValue, 0, len); 48 | } 49 | ulValueLen = len; 50 | return this; 51 | } 52 | 53 | public CKA writeTo(CKA cka) { 54 | cka.type = (int) type; 55 | cka.ulValueLen = (int) ulValueLen; 56 | if (cka.ulValueLen > 0) { 57 | pValue.get(0, cka.pValue, 0, (int) cka.ulValueLen); 58 | } 59 | return cka; 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /src/main/java/org/pkcs11/jacknji11/jna/JNA_CKM.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2010-2011 Joel Hockey (joel.hockey@gmail.com). All rights reserved. 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy 4 | * of this software and associated documentation files (the "Software"), to deal 5 | * in the Software without restriction, including without limitation the rights 6 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | * copies of the Software, and to permit persons to whom the Software is 8 | * furnished to do so, subject to the following conditions: 9 | * 10 | * The above copyright notice and this permission notice shall be included in 11 | * all copies or substantial portions of the Software. 12 | * 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 | * THE SOFTWARE. 20 | */ 21 | 22 | package org.pkcs11.jacknji11.jna; 23 | 24 | import org.pkcs11.jacknji11.CKM; 25 | 26 | import java.util.Arrays; 27 | import java.util.List; 28 | 29 | import com.sun.jna.NativeLong; 30 | import com.sun.jna.Pointer; 31 | import com.sun.jna.Structure; 32 | 33 | 34 | /** 35 | * CKM_? constants and CK_MECHANISM struct wrapper. 36 | * @author Joel Hockey (joel.hockey@gmail.com) 37 | */ 38 | public class JNA_CKM extends Structure { 39 | public NativeLong mechanism; 40 | public Pointer pParameter; 41 | public NativeLong ulParameterLen; 42 | 43 | public JNA_CKM() { 44 | // Need to set alignment to none since 'pParameter' is not 45 | // aligned to a 8 byte boundary on Win64 (long is 4 bytes there) 46 | super(ALIGN_NONE); 47 | } 48 | 49 | @Override 50 | protected List getFieldOrder() { 51 | return Arrays.asList("mechanism", "pParameter", "ulParameterLen"); 52 | } 53 | 54 | public JNA_CKM readFrom(CKM ckm) { 55 | mechanism = new NativeLong(ckm.mechanism); 56 | pParameter = ckm.pParameter; 57 | ulParameterLen = new NativeLong(ckm.ulParameterLen); 58 | return this; 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /src/main/java/org/pkcs11/jacknji11/jna/JNA_CK_MECHANISM_INFO.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2010-2011 Joel Hockey (joel.hockey@gmail.com). All rights reserved. 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy 4 | * of this software and associated documentation files (the "Software"), to deal 5 | * in the Software without restriction, including without limitation the rights 6 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | * copies of the Software, and to permit persons to whom the Software is 8 | * furnished to do so, subject to the following conditions: 9 | * 10 | * The above copyright notice and this permission notice shall be included in 11 | * all copies or substantial portions of the Software. 12 | * 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 | * THE SOFTWARE. 20 | */ 21 | 22 | package org.pkcs11.jacknji11.jna; 23 | 24 | import org.pkcs11.jacknji11.CK_MECHANISM_INFO; 25 | 26 | import java.util.Arrays; 27 | import java.util.List; 28 | 29 | import com.sun.jna.NativeLong; 30 | import com.sun.jna.Structure; 31 | 32 | /** 33 | * JNA wrapper for PKCS#11 CK_MECHANSIM_INFO struct. 34 | * @author Joel Hockey (joel.hockey@gmail.com) 35 | */ 36 | public class JNA_CK_MECHANISM_INFO extends Structure { 37 | public NativeLong ulMinKeySize; 38 | public NativeLong ulMaxKeySize; 39 | public NativeLong flags; 40 | 41 | @Override 42 | protected List getFieldOrder() { 43 | return Arrays.asList("ulMinKeySize", "ulMaxKeySize", "flags"); 44 | } 45 | 46 | public JNA_CK_MECHANISM_INFO readFrom(CK_MECHANISM_INFO info) { 47 | ulMinKeySize = new NativeLong(info.ulMinKeySize); 48 | ulMaxKeySize = new NativeLong(info.ulMaxKeySize); 49 | flags = new NativeLong(info.flags); 50 | return this; 51 | } 52 | 53 | public CK_MECHANISM_INFO writeTo(CK_MECHANISM_INFO info) { 54 | info.ulMinKeySize = ulMinKeySize.intValue(); 55 | info.ulMaxKeySize = ulMaxKeySize.intValue(); 56 | info.flags = flags.intValue(); 57 | return info; 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /src/main/java/org/pkcs11/jacknji11/jna/JNA_CK_SESSION_INFO.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2010-2011 Joel Hockey (joel.hockey@gmail.com). All rights reserved. 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy 4 | * of this software and associated documentation files (the "Software"), to deal 5 | * in the Software without restriction, including without limitation the rights 6 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | * copies of the Software, and to permit persons to whom the Software is 8 | * furnished to do so, subject to the following conditions: 9 | * 10 | * The above copyright notice and this permission notice shall be included in 11 | * all copies or substantial portions of the Software. 12 | * 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 | * THE SOFTWARE. 20 | */ 21 | 22 | package org.pkcs11.jacknji11.jna; 23 | 24 | import org.pkcs11.jacknji11.CK_SESSION_INFO; 25 | 26 | import java.util.Arrays; 27 | import java.util.List; 28 | 29 | import com.sun.jna.NativeLong; 30 | import com.sun.jna.Structure; 31 | 32 | /** 33 | * JNA wrapper for PKCS#11 CK_SESSION_INFO struct. 34 | * @author Joel Hockey (joel.hockey@gmail.com) 35 | */ 36 | public class JNA_CK_SESSION_INFO extends Structure { 37 | public NativeLong slotID; 38 | public NativeLong state; 39 | public NativeLong flags; 40 | public NativeLong ulDeviceError; 41 | 42 | @Override 43 | protected List getFieldOrder() { 44 | return Arrays.asList("slotID", "state", "flags", "ulDeviceError"); 45 | } 46 | 47 | public JNA_CK_SESSION_INFO readFrom(CK_SESSION_INFO info) { 48 | slotID = new NativeLong(info.slotID); 49 | state = new NativeLong(info.state); 50 | flags = new NativeLong(info.flags); 51 | ulDeviceError = new NativeLong(info.ulDeviceError); 52 | return this; 53 | } 54 | 55 | public CK_SESSION_INFO writeTo(CK_SESSION_INFO info) { 56 | info.slotID = slotID.intValue(); 57 | info.state = state.intValue(); 58 | info.flags = flags.intValue(); 59 | info.ulDeviceError = ulDeviceError.intValue(); 60 | return info; 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /src/main/java/org/pkcs11/jacknji11/jffi/JFFI_CK_SLOT_INFO.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2010-2011 Joel Hockey (joel.hockey@gmail.com). All rights reserved. 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy 4 | * of this software and associated documentation files (the "Software"), to deal 5 | * in the Software without restriction, including without limitation the rights 6 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | * copies of the Software, and to permit persons to whom the Software is 8 | * furnished to do so, subject to the following conditions: 9 | * 10 | * The above copyright notice and this permission notice shall be included in 11 | * all copies or substantial portions of the Software. 12 | * 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 | * THE SOFTWARE. 20 | */ 21 | 22 | package org.pkcs11.jacknji11.jffi; 23 | 24 | import jnr.ffi.Struct; 25 | 26 | import org.pkcs11.jacknji11.CK_SLOT_INFO; 27 | 28 | /** 29 | * JFFI wrapper for PKCS#11 CK_SLOT_INFO struct. 30 | * @author Joel Hockey (joel.hockey@gmail.com) 31 | */ 32 | public class JFFI_CK_SLOT_INFO extends Struct { 33 | public byte[] slotDescription; 34 | public byte[] manufacturerID; 35 | public long flags; 36 | public JFFI_CK_VERSION hardwareVersion; 37 | public JFFI_CK_VERSION firmwareVersion; 38 | 39 | public JFFI_CK_SLOT_INFO() { 40 | super(jnr.ffi.Runtime.getSystemRuntime()); 41 | } 42 | 43 | 44 | public JFFI_CK_SLOT_INFO readFrom(CK_SLOT_INFO info) { 45 | slotDescription = info.slotDescription; 46 | manufacturerID = info.manufacturerID; 47 | flags = info.flags; 48 | hardwareVersion = new JFFI_CK_VERSION().readFrom(info.hardwareVersion); 49 | firmwareVersion = new JFFI_CK_VERSION().readFrom(info.firmwareVersion); 50 | return this; 51 | } 52 | 53 | public CK_SLOT_INFO writeTo(CK_SLOT_INFO info) { 54 | info.slotDescription = slotDescription; 55 | info.manufacturerID = manufacturerID; 56 | info.flags = flags; 57 | hardwareVersion.writeTo(info.hardwareVersion); 58 | firmwareVersion.writeTo(info.firmwareVersion); 59 | return info; 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /src/main/java/org/pkcs11/jacknji11/UBigInt.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2010-2011 Joel Hockey (joel.hockey@gmail.com). All rights reserved. 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy 4 | * of this software and associated documentation files (the "Software"), to deal 5 | * in the Software without restriction, including without limitation the rights 6 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | * copies of the Software, and to permit persons to whom the Software is 8 | * furnished to do so, subject to the following conditions: 9 | * 10 | * The above copyright notice and this permission notice shall be included in 11 | * all copies or substantial portions of the Software. 12 | * 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 | * THE SOFTWARE. 20 | */ 21 | package org.pkcs11.jacknji11; 22 | 23 | import java.math.BigInteger; 24 | import java.util.Arrays; 25 | 26 | /** 27 | * Utility class for encoding and decoding unsigned big integers in big-endian byte arrays. 28 | *

29 | * Unsigned big integers are encoded as big-endian byte arrays without leading zero bytes. 30 | * 31 | * @author Tomasz Wysocki 32 | */ 33 | public class UBigInt { 34 | 35 | private UBigInt() { 36 | // utility class 37 | } 38 | 39 | /** 40 | * Encode unsigned big integer to byte array. 41 | * 42 | * @param value unsigned big integer 43 | * @return byte array with big-endian encoding without leading zero bytes, 44 | * the size of the array is the minimum required to represent the value. 45 | */ 46 | public static byte[] ubigint2b(BigInteger value) { 47 | byte[] bytes = value.toByteArray(); 48 | // strip initial zero if present as we are operating on unsigned values 49 | if (bytes.length > 1 && bytes[0] == 0) { 50 | bytes = Arrays.copyOfRange(bytes, 1, bytes.length); 51 | } 52 | return bytes; 53 | } 54 | 55 | /** 56 | * Decode unsigned big integer from byte array. 57 | * 58 | * @param bytes byte array with big-endian encoding of unsigned big integer (highest bit is not a sign bit) 59 | * @return unsigned big integer 60 | */ 61 | public static BigInteger b2ubigint(byte[] bytes) { 62 | return new BigInteger(1, bytes); 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /src/main/java/org/pkcs11/jacknji11/jffi/JFFI_CKM.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2010-2011 Joel Hockey (joel.hockey@gmail.com). All rights reserved. 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy 4 | * of this software and associated documentation files (the "Software"), to deal 5 | * in the Software without restriction, including without limitation the rights 6 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | * copies of the Software, and to permit persons to whom the Software is 8 | * furnished to do so, subject to the following conditions: 9 | * 10 | * The above copyright notice and this permission notice shall be included in 11 | * all copies or substantial portions of the Software. 12 | * 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 | * THE SOFTWARE. 20 | */ 21 | 22 | package org.pkcs11.jacknji11.jffi; 23 | 24 | import jnr.ffi.Memory; 25 | import jnr.ffi.Struct; 26 | 27 | import com.sun.jna.Native; 28 | 29 | import org.pkcs11.jacknji11.CKM; 30 | 31 | /** 32 | * JFFI CK_MECHANISM struct wrapper. 33 | * @author Joel Hockey (joel.hockey@gmail.com) 34 | */ 35 | public class JFFI_CKM extends Struct { 36 | public long mechanism; 37 | public jnr.ffi.Pointer pParameter; 38 | public long ulParameterLen; 39 | 40 | public JFFI_CKM() { 41 | super(jnr.ffi.Runtime.getSystemRuntime()); 42 | } 43 | 44 | public JFFI_CKM readFrom(CKM ckm) { 45 | mechanism = ckm.mechanism; 46 | int len = ckm.bParameter != null ? ckm.bParameter.length : 0; 47 | if (len > 0) { 48 | pParameter = Memory.allocate(jnr.ffi.Runtime.getSystemRuntime(), len); 49 | pParameter.put(0, ckm.bParameter, 0, len); 50 | } 51 | ulParameterLen = len; 52 | return this; 53 | } 54 | 55 | public JFFI_CKM readFromPointer(CKM pMechanism) { 56 | mechanism = pMechanism.mechanism; 57 | int len = pMechanism.pParameter != null ? Native.POINTER_SIZE : 0; 58 | if (len > 0) { 59 | byte[] ckmParamBytes = pMechanism.pParameter.getByteArray(0, len); 60 | pParameter = Memory.allocate(jnr.ffi.Runtime.getSystemRuntime(), len); 61 | pParameter.put(0, ckmParamBytes, 0, len); 62 | } 63 | ulParameterLen = len; 64 | return this; 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /src/main/java/org/pkcs11/jacknji11/CK_INFO.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2010-2011 Joel Hockey (joel.hockey@gmail.com). All rights reserved. 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy 4 | * of this software and associated documentation files (the "Software"), to deal 5 | * in the Software without restriction, including without limitation the rights 6 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | * copies of the Software, and to permit persons to whom the Software is 8 | * furnished to do so, subject to the following conditions: 9 | * 10 | * The above copyright notice and this permission notice shall be included in 11 | * all copies or substantial portions of the Software. 12 | * 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 | * THE SOFTWARE. 20 | */ 21 | 22 | package org.pkcs11.jacknji11; 23 | 24 | import java.util.Map; 25 | 26 | /** 27 | * PKCS#11 CK_INFO struct. 28 | * @author Joel Hockey (joel.hockey@gmail.com) 29 | */ 30 | public class CK_INFO { 31 | 32 | /** Maps from long value to String description (variable name). */ 33 | private static final Map L2S = C.createL2SMap(CK_INFO.class); 34 | /** 35 | * Convert long constant value to name. 36 | * @param ckf value 37 | * @return name 38 | */ 39 | public static final String L2S(long ckf) { return C.l2s(L2S, "CKF", ckf); } 40 | /** 41 | * Convert flags to string. 42 | * @param flags flags 43 | * @return string format 44 | */ 45 | public static String f2s(long flags) { return C.f2s(L2S, flags); } 46 | 47 | public CK_VERSION cryptokiVersion = new CK_VERSION(); 48 | public byte[] manufacturerID = new byte[32]; 49 | public long flags; 50 | public byte[] libraryDescription = new byte[32]; 51 | public CK_VERSION libraryVersion = new CK_VERSION(); 52 | 53 | /** @return string */ 54 | public String toString() { 55 | return String.format("(\n version=%d.%d\n manufacturerID=%s\n flags=0x%08x{%s}\n libraryDescription=%s\n libraryVersion=%d.%d\n)", 56 | cryptokiVersion.major & 0xff, cryptokiVersion.minor & 0xff, Buf.escstr(manufacturerID), 57 | flags, f2s(flags), Buf.escstr(libraryDescription), 58 | libraryVersion.major & 0xff, libraryVersion.minor & 0xff); 59 | 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /src/main/java/org/pkcs11/jacknji11/jffi/JFFI_CK_INFO.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2010-2011 Joel Hockey (joel.hockey@gmail.com). All rights reserved. 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy 4 | * of this software and associated documentation files (the "Software"), to deal 5 | * in the Software without restriction, including without limitation the rights 6 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | * copies of the Software, and to permit persons to whom the Software is 8 | * furnished to do so, subject to the following conditions: 9 | * 10 | * The above copyright notice and this permission notice shall be included in 11 | * all copies or substantial portions of the Software. 12 | * 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 | * THE SOFTWARE. 20 | */ 21 | 22 | package org.pkcs11.jacknji11.jffi; 23 | 24 | import jnr.ffi.Struct; 25 | 26 | import org.pkcs11.jacknji11.CK_INFO; 27 | 28 | /** 29 | * JFFI wrapper for PKCS#11 CK_INFO struct. 30 | * @author Joel Hockey (joel.hockey@gmail.com) 31 | */ 32 | public class JFFI_CK_INFO extends Struct { 33 | 34 | public JFFI_CK_VERSION cryptokiVersion; 35 | public byte[] manufacturerID; 36 | public long flags; 37 | public byte[] libraryDescription; 38 | public JFFI_CK_VERSION libraryVersion; 39 | 40 | /** 41 | * Default constructor. 42 | * need to set alignment to none since 'flags' is not 43 | * correctly aligned to a 4 byte boundary 44 | */ 45 | public JFFI_CK_INFO() { 46 | super(jnr.ffi.Runtime.getSystemRuntime()); 47 | // setAlignType(); 48 | } 49 | 50 | public JFFI_CK_INFO readFrom(CK_INFO info) { 51 | cryptokiVersion = new JFFI_CK_VERSION().readFrom(info.cryptokiVersion); 52 | manufacturerID = info.manufacturerID; 53 | flags = info.flags; 54 | libraryDescription = info.libraryDescription; 55 | libraryVersion = new JFFI_CK_VERSION().readFrom(info.libraryVersion); 56 | return this; 57 | } 58 | 59 | public CK_INFO writeTo(CK_INFO info) { 60 | cryptokiVersion.writeTo(info.cryptokiVersion); 61 | info.manufacturerID = manufacturerID; 62 | info.flags = flags; 63 | info.libraryDescription = libraryDescription; 64 | libraryVersion.writeTo(info.libraryVersion); 65 | return info; 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /src/main/java/org/pkcs11/jacknji11/CK_SESSION_INFO.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2010-2011 Joel Hockey (joel.hockey@gmail.com). All rights reserved. 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy 4 | * of this software and associated documentation files (the "Software"), to deal 5 | * in the Software without restriction, including without limitation the rights 6 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | * copies of the Software, and to permit persons to whom the Software is 8 | * furnished to do so, subject to the following conditions: 9 | * 10 | * The above copyright notice and this permission notice shall be included in 11 | * all copies or substantial portions of the Software. 12 | * 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 | * THE SOFTWARE. 20 | */ 21 | 22 | package org.pkcs11.jacknji11; 23 | 24 | import java.util.Map; 25 | 26 | /** 27 | * PKCS#11 CK_SESSION_INFO struct. 28 | * @author Joel Hockey (joel.hockey@gmail.com) 29 | */ 30 | public class CK_SESSION_INFO { 31 | 32 | public static final long CKF_RW_SESSION = 0x00000002; 33 | public static final long CKF_SERIAL_SESSION = 0x00000004; 34 | 35 | /** Maps from long value to String description (variable name). */ 36 | private static final Map L2S = C.createL2SMap(CK_SESSION_INFO.class); 37 | /** 38 | * Convert long constant value to name. 39 | * @param ckf value 40 | * @return name 41 | */ 42 | public static final String I2S(long ckf) { return C.l2s(L2S, "CKF", ckf); } 43 | /** 44 | * Convert flags to string. 45 | * @param flags flags 46 | * @return string format 47 | */ 48 | public static String f2s(long flags) { return C.f2s(L2S, flags); } 49 | 50 | public long slotID; 51 | public long state; 52 | public long flags; 53 | public long ulDeviceError; 54 | 55 | /** @return True, if the provided flag is set */ 56 | public boolean isFlagSet(long CKF_FLAG) { 57 | return (flags & CKF_FLAG) != 0L; 58 | } 59 | 60 | /** @return string */ 61 | public String toString() { 62 | return String.format("(\n slotID=0x%08x\n state=0x%08x{%s}\n flags=0x%08x{%s}\n deviceError=%d\n)", 63 | slotID, state, CKS.L2S(state), flags, f2s(flags), ulDeviceError); 64 | } 65 | } -------------------------------------------------------------------------------- /src/main/java/org/pkcs11/jacknji11/jna/JNA_CK_SLOT_INFO.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2010-2011 Joel Hockey (joel.hockey@gmail.com). All rights reserved. 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy 4 | * of this software and associated documentation files (the "Software"), to deal 5 | * in the Software without restriction, including without limitation the rights 6 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | * copies of the Software, and to permit persons to whom the Software is 8 | * furnished to do so, subject to the following conditions: 9 | * 10 | * The above copyright notice and this permission notice shall be included in 11 | * all copies or substantial portions of the Software. 12 | * 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 | * THE SOFTWARE. 20 | */ 21 | 22 | package org.pkcs11.jacknji11.jna; 23 | 24 | import org.pkcs11.jacknji11.CK_SLOT_INFO; 25 | 26 | import java.util.Arrays; 27 | import java.util.List; 28 | 29 | import com.sun.jna.NativeLong; 30 | import com.sun.jna.Structure; 31 | 32 | /** 33 | * JNA wrapper for PKCS#11 CK_SLOT_INFO struct. 34 | * @author Joel Hockey (joel.hockey@gmail.com) 35 | */ 36 | public class JNA_CK_SLOT_INFO extends Structure { 37 | public byte[] slotDescription; 38 | public byte[] manufacturerID; 39 | public NativeLong flags; 40 | public JNA_CK_VERSION hardwareVersion; 41 | public JNA_CK_VERSION firmwareVersion; 42 | 43 | @Override 44 | protected List getFieldOrder() { 45 | return Arrays.asList("slotDescription", "manufacturerID", "flags", "hardwareVersion", "firmwareVersion"); 46 | } 47 | 48 | public JNA_CK_SLOT_INFO readFrom(CK_SLOT_INFO info) { 49 | slotDescription = info.slotDescription; 50 | manufacturerID = info.manufacturerID; 51 | flags = new NativeLong(info.flags); 52 | hardwareVersion = new JNA_CK_VERSION().readFrom(info.hardwareVersion); 53 | firmwareVersion = new JNA_CK_VERSION().readFrom(info.firmwareVersion); 54 | return this; 55 | } 56 | 57 | public CK_SLOT_INFO writeTo(CK_SLOT_INFO info) { 58 | info.slotDescription = slotDescription; 59 | info.manufacturerID = manufacturerID; 60 | info.flags = flags.intValue(); 61 | hardwareVersion.writeTo(info.hardwareVersion); 62 | firmwareVersion.writeTo(info.firmwareVersion); 63 | return info; 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /src/main/java/org/pkcs11/jacknji11/CKO.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2010-2011 Joel Hockey (joel.hockey@gmail.com). All rights reserved. 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy 4 | * of this software and associated documentation files (the "Software"), to deal 5 | * in the Software without restriction, including without limitation the rights 6 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | * copies of the Software, and to permit persons to whom the Software is 8 | * furnished to do so, subject to the following conditions: 9 | * 10 | * The above copyright notice and this permission notice shall be included in 11 | * all copies or substantial portions of the Software. 12 | * 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 | * THE SOFTWARE. 20 | */ 21 | 22 | package org.pkcs11.jacknji11; 23 | 24 | import java.util.Map; 25 | 26 | /** 27 | * CKO_? constants. 28 | * @author Joel Hockey (joel.hockey@gmail.com) 29 | */ 30 | public class CKO { 31 | 32 | public static final long DATA = 0x00000000; 33 | public static final long CERTIFICATE = 0x00000001; 34 | public static final long PUBLIC_KEY = 0x00000002; 35 | public static final long PRIVATE_KEY = 0x00000003; 36 | public static final long SECRET_KEY = 0x00000004; 37 | public static final long HW_FEATURE = 0x00000005; 38 | public static final long DOMAIN_PARAMETERS = 0x00000006; 39 | public static final long MECHANISM = 0x00000007; 40 | public static final long OTP_KEY = 0x00000008; 41 | 42 | // Vendor defined values 43 | // Eracom PTK 44 | public static final long VENDOR_PTK_CERTIFICATE_REQUEST = 0x80000201; 45 | public static final long VENDOR_PTK_CRL = 0x80000202; 46 | public static final long VENDOR_PTK_ADAPTER = 0x8000020a; 47 | public static final long VENDOR_PTK_SLOT = 0x8000020b; 48 | public static final long VENDOR_PTK_FM = 0x8000020c; 49 | 50 | /** Maps from long value to String description (variable name). */ 51 | private static final Map L2S = C.createL2SMap(CKO.class); 52 | /** 53 | * Convert long constant value to name. 54 | * @param cko value 55 | * @return name 56 | */ 57 | public static final String L2S(long cko) { return C.l2s(L2S, CKO.class.getSimpleName(), cko); } 58 | } 59 | -------------------------------------------------------------------------------- /src/main/java/org/pkcs11/jacknji11/CK_SLOT_INFO.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2010-2011 Joel Hockey (joel.hockey@gmail.com). All rights reserved. 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy 4 | * of this software and associated documentation files (the "Software"), to deal 5 | * in the Software without restriction, including without limitation the rights 6 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | * copies of the Software, and to permit persons to whom the Software is 8 | * furnished to do so, subject to the following conditions: 9 | * 10 | * The above copyright notice and this permission notice shall be included in 11 | * all copies or substantial portions of the Software. 12 | * 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 | * THE SOFTWARE. 20 | */ 21 | 22 | package org.pkcs11.jacknji11; 23 | 24 | import java.util.Map; 25 | 26 | /** 27 | * PKCS#11 CK_SLOT_INFO struct. 28 | * @author Joel Hockey (joel.hockey@gmail.com) 29 | */ 30 | public class CK_SLOT_INFO { 31 | 32 | public static final long CKF_TOKEN_PRESENT = 0x00000001; 33 | public static final long CKF_REMOVABLE_DEVICE = 0x00000002; 34 | public static final long CKF_HW_SLOT = 0x00000004; 35 | 36 | /** Maps from long value to String description (variable name). */ 37 | private static final Map L2S = C.createL2SMap(CK_SLOT_INFO.class); 38 | /** 39 | * Convert long constant value to name. 40 | * @param ckf value 41 | * @return name 42 | */ 43 | public static final String L2S(long ckf) { return C.l2s(L2S, "CKF", ckf); } 44 | /** 45 | * Convert flags to string. 46 | * @param flags flags 47 | * @return string format 48 | */ 49 | public static String f2s(long flags) { return C.f2s(L2S, flags); } 50 | 51 | public byte[] slotDescription = new byte[64]; 52 | public byte[] manufacturerID = new byte[32]; 53 | public long flags; 54 | public CK_VERSION hardwareVersion = new CK_VERSION(); 55 | public CK_VERSION firmwareVersion = new CK_VERSION(); 56 | 57 | /** @return True, if the provided flag is set */ 58 | public boolean isFlagSet(long CKF_FLAG) { 59 | return (flags & CKF_FLAG) != 0L; 60 | } 61 | 62 | /** @return string */ 63 | public String toString() { 64 | return String.format("(\n slotDescription=%s\n manufacturerID=%s\n flags=0x%08x{%s}\n hardwareVersion=%d.%d\n firmwareVersion=%d.%d\n)", 65 | Buf.escstr(slotDescription), Buf.escstr(manufacturerID), flags, f2s(flags), 66 | hardwareVersion.major & 0xff, hardwareVersion.minor & 0xff, 67 | firmwareVersion.major & 0xff, firmwareVersion.minor & 0xff); 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /src/main/java/org/pkcs11/jacknji11/CKP.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2010-2011 Joel Hockey (joel.hockey@gmail.com). All rights reserved. 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy 4 | * of this software and associated documentation files (the "Software"), to deal 5 | * in the Software without restriction, including without limitation the rights 6 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | * copies of the Software, and to permit persons to whom the Software is 8 | * furnished to do so, subject to the following conditions: 9 | * 10 | * The above copyright notice and this permission notice shall be included in 11 | * all copies or substantial portions of the Software. 12 | * 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 | * THE SOFTWARE. 20 | */ 21 | 22 | package org.pkcs11.jacknji11; 23 | 24 | import java.util.Map; 25 | 26 | /** 27 | * CKP_? constants. 28 | * https://github.com/oasis-tcs/pkcs11/blob/master/working/headers/pkcs11t.h 29 | */ 30 | public class CKP { 31 | public static final long CKP_PKCS5_PBKD2_HMAC_SHA1 = 0x00000001; 32 | 33 | public static final long ML_DSA_44 = 0x00000001; 34 | public static final long ML_DSA_65 = 0x00000002; 35 | public static final long ML_DSA_87 = 0x00000003; 36 | 37 | /* SLH-DSA values for CKA_PARAMETER_SETS */ 38 | public static final long SLH_DSA_SHA2_128S = 0x00000001; 39 | public static final long SLH_DSA_SHAKE_128S = 0x00000002; 40 | public static final long SLH_DSA_SHA2_128F = 0x00000003; 41 | public static final long SLH_DSA_SHAKE_128F = 0x00000004; 42 | public static final long SLH_DSA_SHA2_192S = 0x00000005; 43 | public static final long SLH_DSA_SHAKE_192S = 0x00000006; 44 | public static final long SLH_DSA_SHA2_192F = 0x00000007; 45 | public static final long SLH_DSA_SHAKE_192F = 0x00000008; 46 | public static final long SLH_DSA_SHA2_256S = 0x00000009; 47 | public static final long SLH_DSA_SHAKE_256S = 0x0000000a; 48 | public static final long SLH_DSA_SHA2_256F = 0x0000000b; 49 | public static final long SLH_DSA_SHAKE_256F = 0x0000000c; 50 | 51 | /* ML-KEM values for CKA_PARAMETER_SETS */ 52 | public static final long ML_KEM_512 = 0x00000001; 53 | public static final long ML_KEM_768 = 0x00000002; 54 | public static final long ML_KEM_1024 = 0x00000003; 55 | 56 | /** Maps from long value to String description (variable name). */ 57 | private static final Map L2S = C.createL2SMap(CKP.class); 58 | /** 59 | * Convert long constant value to name. 60 | * @param ckp value 61 | * @return name 62 | */ 63 | public static final String L2S(long ckp) { return C.l2s(L2S, CKP.class.getSimpleName(), ckp); } 64 | } 65 | 66 | -------------------------------------------------------------------------------- /src/main/java/org/pkcs11/jacknji11/jna/JNA_CK_INFO.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2010-2011 Joel Hockey (joel.hockey@gmail.com). All rights reserved. 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy 4 | * of this software and associated documentation files (the "Software"), to deal 5 | * in the Software without restriction, including without limitation the rights 6 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | * copies of the Software, and to permit persons to whom the Software is 8 | * furnished to do so, subject to the following conditions: 9 | * 10 | * The above copyright notice and this permission notice shall be included in 11 | * all copies or substantial portions of the Software. 12 | * 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 | * THE SOFTWARE. 20 | */ 21 | 22 | package org.pkcs11.jacknji11.jna; 23 | 24 | import org.pkcs11.jacknji11.CK_INFO; 25 | 26 | import java.util.Arrays; 27 | import java.util.List; 28 | 29 | import com.sun.jna.NativeLong; 30 | import com.sun.jna.Structure; 31 | 32 | /** 33 | * JNA wrapper for PKCS#11 CK_INFO struct. It sets align type to {@link Structure#ALIGN_NONE} 34 | * since the ULONGS (NativeLongs) don't line up on a 4 byte boundary. You wouldn't care to know 35 | * how painful that learning experience was. 36 | * @author Joel Hockey (joel.hockey@gmail.com) 37 | */ 38 | public class JNA_CK_INFO extends Structure { 39 | 40 | public JNA_CK_VERSION cryptokiVersion; 41 | public byte[] manufacturerID; 42 | public NativeLong flags; 43 | public byte[] libraryDescription; 44 | public JNA_CK_VERSION libraryVersion; 45 | 46 | /** 47 | * Default constructor. 48 | * need to set alignment to none since 'flags' is not 49 | * correctly aligned to a 4 byte boundary 50 | */ 51 | public JNA_CK_INFO() { 52 | setAlignType(ALIGN_NONE); 53 | } 54 | 55 | @Override 56 | protected List getFieldOrder() { 57 | return Arrays.asList("cryptokiVersion", "manufacturerID", "flags", "libraryDescription", "libraryVersion"); 58 | } 59 | 60 | public JNA_CK_INFO readFrom(CK_INFO info) { 61 | cryptokiVersion = new JNA_CK_VERSION().readFrom(info.cryptokiVersion); 62 | manufacturerID = info.manufacturerID; 63 | flags = new NativeLong(info.flags); 64 | libraryDescription = info.libraryDescription; 65 | libraryVersion = new JNA_CK_VERSION().readFrom(info.libraryVersion); 66 | return this; 67 | } 68 | 69 | public CK_INFO writeTo(CK_INFO info) { 70 | cryptokiVersion.writeTo(info.cryptokiVersion); 71 | info.manufacturerID = manufacturerID; 72 | info.flags = flags.intValue(); 73 | info.libraryDescription = libraryDescription; 74 | libraryVersion.writeTo(info.libraryVersion); 75 | return info; 76 | } 77 | } 78 | -------------------------------------------------------------------------------- /src/main/java/org/pkcs11/jacknji11/jna/LongArray.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2010-2011 Joel Hockey (joel.hockey@gmail.com). All rights reserved. 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy 4 | * of this software and associated documentation files (the "Software"), to deal 5 | * in the Software without restriction, including without limitation the rights 6 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | * copies of the Software, and to permit persons to whom the Software is 8 | * furnished to do so, subject to the following conditions: 9 | * 10 | * The above copyright notice and this permission notice shall be included in 11 | * all copies or substantial portions of the Software. 12 | * 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 | * THE SOFTWARE. 20 | */ 21 | 22 | package org.pkcs11.jacknji11.jna; 23 | 24 | import com.sun.jna.Memory; 25 | import com.sun.jna.NativeLong; 26 | import com.sun.jna.PointerType; 27 | 28 | /** 29 | * Implements a CK_ULONG[] type for JNA. Allows simple conversion with java long[]. 30 | * JNA direct memory mapping doesn't seem to support struct arrays, 31 | * so this class is required to map the ints into a contiguous block of memory. 32 | * @author Joel Hockey (joel.hockey@gmail.com) 33 | */ 34 | public class LongArray extends PointerType { 35 | private long[] list; 36 | private int listLen; 37 | 38 | /** Default no-arg constructor required by JNA. */ 39 | public LongArray() { 40 | this(null); 41 | } 42 | 43 | /** 44 | * Allocates JNA Memory and writes long values. 45 | * @param list longs 46 | */ 47 | public LongArray(long[] list) { 48 | this.list = list; 49 | listLen = list == null ? 0 : list.length; 50 | if (listLen == 0) { 51 | return; 52 | } 53 | setPointer(new Memory(listLen * NativeLong.SIZE)); 54 | if (NativeLong.SIZE == 8) { 55 | getPointer().write(0, list, 0, listLen); 56 | } else { 57 | for (int i = 0; i < listLen; i++) { 58 | getPointer().setInt(i, (int) list[i]); 59 | } 60 | } 61 | } 62 | 63 | /** 64 | * Reads (updated) JNA Memory and modifies values in list. 65 | * This must be called after native PKCS#11 calls in {@link NativeProvider} that modify 66 | * ULONG values such as {@link NativeProvider#C_FindObjects(NativeLong, LongArray, NativeLong, LongRef)}. 67 | * This is automatically done by the {@link C} and {@link CE} interfaces. 68 | */ 69 | public void update() { 70 | if (listLen == 0) { 71 | return; 72 | } 73 | if (NativeLong.SIZE == 8) { 74 | getPointer().read(0, list, 0, listLen); 75 | } else { 76 | for (int i = 0; i < listLen; i++) { 77 | list[i] = getPointer().getInt(i * NativeLong.SIZE); 78 | } 79 | } 80 | } 81 | } 82 | -------------------------------------------------------------------------------- /src/main/java/org/pkcs11/jacknji11/jffi/Template.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2010-2011 Joel Hockey (joel.hockey@gmail.com). All rights reserved. 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy 4 | * of this software and associated documentation files (the "Software"), to deal 5 | * in the Software without restriction, including without limitation the rights 6 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | * copies of the Software, and to permit persons to whom the Software is 8 | * furnished to do so, subject to the following conditions: 9 | * 10 | * The above copyright notice and this permission notice shall be included in 11 | * all copies or substantial portions of the Software. 12 | * 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 | * THE SOFTWARE. 20 | */ 21 | 22 | package org.pkcs11.jacknji11.jffi; 23 | 24 | import jnr.ffi.Memory; 25 | import jnr.ffi.Pointer; 26 | import jnr.ffi.Runtime; 27 | 28 | import org.pkcs11.jacknji11.CKA; 29 | 30 | /** 31 | * JFFI Wrapper for CK_ATTRIBUTE[]. 32 | * 33 | * @author Joel Hockey (joel.hockey@gmail.com) 34 | */ 35 | public class Template { 36 | 37 | public static Pointer templ(CKA[] cka) { 38 | if (cka == null) { 39 | return null; 40 | } 41 | Runtime runtime = Runtime.getSystemRuntime(); 42 | Pointer result = Memory.allocate(runtime, cka.length * (runtime.longSize() + runtime.addressSize() + runtime.longSize())); 43 | 44 | int offset = 0; 45 | for (int i = 0; i < cka.length; i++) { 46 | // type 47 | result.putLong(offset, cka[i].type); 48 | offset += runtime.longSize(); 49 | 50 | // pValue 51 | if (cka[i].pValue != null) { 52 | Pointer pValue = Memory.allocate(runtime, cka[i].pValue.length); 53 | pValue.put(0, cka[i].pValue, 0, cka[i].pValue.length); 54 | } 55 | offset += runtime.addressSize(); 56 | 57 | // ulValueLen 58 | result.putLong(offset, cka[i].ulValueLen); 59 | offset += runtime.longSize(); 60 | } 61 | 62 | return result; 63 | } 64 | 65 | public static void update(Pointer templ, CKA[] cka) { 66 | if (cka == null) { 67 | return; 68 | } 69 | Runtime runtime = Runtime.getSystemRuntime(); 70 | int offset = 0; 71 | for (int i = 0; i < cka.length; i++) { 72 | // read ulValueLen (skip type, pValue) 73 | long ulValueLen = templ.getLong(offset + runtime.longSize() + runtime.addressSize()); 74 | cka[i].ulValueLen = ulValueLen; 75 | 76 | // pValue 77 | Pointer pValue = templ.getPointer(offset + runtime.longSize()); 78 | pValue.put(0, cka[i].pValue, 0, cka[i].pValue.length); 79 | 80 | // update offset (skip type, pValue, ulValueLen) 81 | offset += runtime.longSize() + runtime.addressSize() + runtime.longSize(); 82 | } 83 | } 84 | } 85 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | org.pkcs11 8 | jacknji11 9 | 1.3-SNAPSHOT 10 | jacknji11 11 | jar 12 | 13 | 2010 14 | 15 | 16 | Java Native Interface for PKCS#11 17 | 18 | https://github.com/joelhockey/jacknji11 19 | 20 | 21 | 22 | MIT License 23 | https://github.com/joelhockey/jacknji11/blob/master/LICENSE.txt 24 | 25 | 26 | 27 | 28 | 29 | Joel Hockey 30 | joel.hockey@gmail.com 31 | https://github.com/joelhockey/jacknji11 32 | 33 | 34 | 35 | 36 | scm:git:https://github.com/joelhockey/jacknji11.git 37 | scm:git:git@github.com:joelhockey/jacknji11.git 38 | https://github.com/joelhockey/jacknji11 39 | 40 | 41 | 42 | 43 | 44 | org.apache.maven.plugins 45 | maven-compiler-plugin 46 | 3.14.0 47 | 48 | 1.8 49 | 1.8 50 | 51 | 52 | 53 | 54 | org.apache.maven.plugins 55 | maven-source-plugin 56 | 3.3.1 57 | 58 | 59 | attach-sources 60 | 61 | jar-no-fork 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | net.java.dev.jna 72 | jna 73 | 5.17.0 74 | 75 | 76 | com.github.jnr 77 | jnr-ffi 78 | 2.2.17 79 | 80 | 81 | 82 | 83 | commons-logging 84 | commons-logging 85 | 1.2 86 | provided 87 | 88 | 89 | 90 | 91 | junit 92 | junit 93 | 4.13.2 94 | test 95 | 96 | 97 | org.apache.logging.log4j 98 | log4j-core 99 | 2.25.1 100 | test 101 | 102 | 103 | org.apache.logging.log4j 104 | log4j-1.2-api 105 | 2.25.1 106 | test 107 | 108 | 109 | 110 | -------------------------------------------------------------------------------- /src/main/java/org/pkcs11/jacknji11/CKD.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2010-2011 Joel Hockey (joel.hockey@gmail.com). All rights reserved. 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy 4 | * of this software and associated documentation files (the "Software"), to deal 5 | * in the Software without restriction, including without limitation the rights 6 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | * copies of the Software, and to permit persons to whom the Software is 8 | * furnished to do so, subject to the following conditions: 9 | * 10 | * The above copyright notice and this permission notice shall be included in 11 | * all copies or substantial portions of the Software. 12 | * 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 | * THE SOFTWARE. 20 | */ 21 | 22 | package org.pkcs11.jacknji11; 23 | 24 | import java.util.Map; 25 | 26 | /** 27 | * CKD_? constants. 28 | * @author Joel Hockey (joel.hockey@gmail.com) 29 | */ 30 | public class CKD { 31 | public static final long NULL = 0x00000001; 32 | public static final long SHA1_KDF = 0x00000002; 33 | public static final long SHA1_KDF_ASN1 = 0x00000003; 34 | public static final long SHA1_KDF_CONCATENATE = 0x00000004; 35 | public static final long SHA224_KDF = 0x00000005; 36 | public static final long SHA256_KDF = 0x00000006; 37 | public static final long SHA384_KDF = 0x00000007; 38 | public static final long SHA512_KDF = 0x00000008; 39 | public static final long CPDIVERSIFY_KDF = 0x00000009; 40 | public static final long SHA3_224_KDF = 0x0000000A; 41 | public static final long SHA3_256_KDF = 0x0000000B; 42 | public static final long SHA3_384_KDF = 0x0000000C; 43 | public static final long SHA3_512_KDF = 0x0000000D; 44 | public static final long SHA1_KDF_SP800 = 0x0000000E; 45 | public static final long SHA224_KDF_SP800 = 0x0000000F; 46 | public static final long SHA256_KDF_SP800 = 0x00000010; 47 | public static final long SHA384_KDF_SP800 = 0x00000011; 48 | public static final long SHA512_KDF_SP800 = 0x00000012; 49 | public static final long SHA3_224_KDF_SP800 = 0x00000013; 50 | public static final long SHA3_256_KDF_SP800 = 0x00000014; 51 | public static final long SHA3_384_KDF_SP800 = 0x00000015; 52 | public static final long SHA3_512_KDF_SP800 = 0x00000016; 53 | public static final long BLAKE2B_160_KDF = 0x00000017; 54 | public static final long BLAKE2B_256_KDF = 0x00000018; 55 | public static final long BLAKE2B_384_KDF = 0x00000019; 56 | public static final long BLAKE2B_512_KDF = 0x0000001a; 57 | 58 | /** Maps from long value to String description (variable name). */ 59 | private static final Map L2S = C.createL2SMap(CKD.class); 60 | /** 61 | * Convert long constant value to name. 62 | * @param ckd value 63 | * @return name 64 | */ 65 | public static final String L2S(long ckd) { return C.l2s(L2S, CKD.class.getSimpleName(), ckd); } 66 | } 67 | -------------------------------------------------------------------------------- /src/main/java/org/pkcs11/jacknji11/CK_MECHANISM_INFO.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2010-2011 Joel Hockey (joel.hockey@gmail.com). All rights reserved. 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy 4 | * of this software and associated documentation files (the "Software"), to deal 5 | * in the Software without restriction, including without limitation the rights 6 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | * copies of the Software, and to permit persons to whom the Software is 8 | * furnished to do so, subject to the following conditions: 9 | * 10 | * The above copyright notice and this permission notice shall be included in 11 | * all copies or substantial portions of the Software. 12 | * 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 | * THE SOFTWARE. 20 | */ 21 | 22 | package org.pkcs11.jacknji11; 23 | 24 | import java.util.Map; 25 | 26 | /** 27 | * PKCS#11 CK_MECHANSIM_INFO struct. 28 | * @author Joel Hockey (joel.hockey@gmail.com) 29 | */ 30 | public class CK_MECHANISM_INFO { 31 | public static final long CKF_HW = 0x00000001; 32 | public static final long CKF_ENCRYPT = 0x00000100; 33 | public static final long CKF_DECRYPT = 0x00000200; 34 | public static final long CKF_DIGEST = 0x00000400; 35 | public static final long CKF_SIGN = 0x00000800; 36 | public static final long CKF_SIGN_RECOVER = 0x00001000; 37 | public static final long CKF_VERIFY = 0x00002000; 38 | public static final long CKF_VERIFY_RECOVER = 0x00004000; 39 | public static final long CKF_GENERATE = 0x00008000; 40 | public static final long CKF_GENERATE_KEY_PAIR = 0x00010000; 41 | public static final long CKF_WRAP = 0x00020000; 42 | public static final long CKF_UNWRAP = 0x00040000; 43 | public static final long CKF_DERIVE = 0x00080000; 44 | public static final long CKF_EC_F_P = 0x00100000; 45 | public static final long CKF_EC_F_2M = 0x00200000; 46 | public static final long CKF_EC_ECPARAMETERS = 0x00400000; 47 | public static final long CKF_EC_NAMEDCURVE = 0x00800000; 48 | public static final long CKF_EC_UNCOMPRESS = 0x01000000; 49 | public static final long CKF_EC_COMPRESS = 0x02000000; 50 | public static final long CKF_EXTENSION = 0x80000000; 51 | 52 | 53 | /** Maps from long value to String description (variable name). */ 54 | private static final Map L2S = C.createL2SMap(CK_MECHANISM_INFO.class); 55 | /** 56 | * Convert long constant value to name. 57 | * @param ckf value 58 | * @return name 59 | */ 60 | public static final String I2S(long ckf) { return C.l2s(L2S, "CKF", ckf); } 61 | /** 62 | * Convert flags to string. 63 | * @param flags flags 64 | * @return string format 65 | */ 66 | public static String f2s(long flags) { return C.f2s(L2S, flags); } 67 | 68 | 69 | public long ulMinKeySize; 70 | public long ulMaxKeySize; 71 | public long flags; 72 | 73 | /** @return True, if the provided flag is set */ 74 | public boolean isFlagSet(long CKF_FLAG) { 75 | return (flags & CKF_FLAG) != 0L; 76 | } 77 | 78 | /** @return string */ 79 | public String toString() { 80 | return String.format("(\n minKeySize=%d\n maxKeySize=%d\n flags=0x%08x{%s}\n)", 81 | ulMinKeySize, ulMaxKeySize, flags, f2s(flags)); 82 | 83 | } 84 | } 85 | -------------------------------------------------------------------------------- /src/main/java/org/pkcs11/jacknji11/jffi/JFFI_CK_TOKEN_INFO.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2010-2011 Joel Hockey (joel.hockey@gmail.com). All rights reserved. 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy 4 | * of this software and associated documentation files (the "Software"), to deal 5 | * in the Software without restriction, including without limitation the rights 6 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | * copies of the Software, and to permit persons to whom the Software is 8 | * furnished to do so, subject to the following conditions: 9 | * 10 | * The above copyright notice and this permission notice shall be included in 11 | * all copies or substantial portions of the Software. 12 | * 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 | * THE SOFTWARE. 20 | */ 21 | 22 | package org.pkcs11.jacknji11.jffi; 23 | 24 | import jnr.ffi.Struct; 25 | 26 | import org.pkcs11.jacknji11.CK_TOKEN_INFO; 27 | 28 | /** 29 | * JFFI wrapper for PKCS#11 CK_TOKEN_INFO struct. 30 | * @author Joel Hockey (joel.hockey@gmail.com) 31 | */ 32 | public class JFFI_CK_TOKEN_INFO extends Struct { 33 | public byte[] label; 34 | public byte[] manufacturerID; 35 | public byte[] model; 36 | public byte[] serialNumber; 37 | public long flags; 38 | public long ulMaxSessionCount; 39 | public long ulSessionCount; 40 | public long ulMaxRwSessionCount; 41 | public long ulRwSessionCount; 42 | public long ulMaxPinLen; 43 | public long ulMinPinLen; 44 | public long ulTotalPublicMemory; 45 | public long ulFreePublicMemory; 46 | public long ulTotalPrivateMemory; 47 | public long ulFreePrivateMemory; 48 | public JFFI_CK_VERSION hardwareVersion; 49 | public JFFI_CK_VERSION firmwareVersion; 50 | public byte[] utcTime; 51 | 52 | public JFFI_CK_TOKEN_INFO() { 53 | super(jnr.ffi.Runtime.getSystemRuntime()); 54 | } 55 | 56 | 57 | public JFFI_CK_TOKEN_INFO readFrom(CK_TOKEN_INFO info) { 58 | label = info.label; 59 | manufacturerID = info.manufacturerID; 60 | model = info.model; 61 | serialNumber = info.serialNumber; 62 | flags = info.flags; 63 | ulMaxSessionCount = info.ulMaxSessionCount; 64 | ulSessionCount = info.ulSessionCount; 65 | ulMaxRwSessionCount = info.ulMaxRwSessionCount; 66 | ulRwSessionCount = info.ulRwSessionCount; 67 | ulMaxPinLen = info.ulMaxPinLen; 68 | ulMinPinLen = info.ulMinPinLen; 69 | ulTotalPublicMemory = info.ulTotalPublicMemory; 70 | ulFreePublicMemory = info.ulFreePublicMemory; 71 | ulTotalPrivateMemory = info.ulTotalPrivateMemory; 72 | ulFreePrivateMemory = info.ulFreePrivateMemory; 73 | hardwareVersion = new JFFI_CK_VERSION().readFrom(info.hardwareVersion); 74 | firmwareVersion = new JFFI_CK_VERSION().readFrom(info.firmwareVersion); 75 | utcTime = info.utcTime; 76 | return this; 77 | } 78 | 79 | public CK_TOKEN_INFO writeTo(CK_TOKEN_INFO info) { 80 | info.label = label; 81 | info.manufacturerID = manufacturerID; 82 | info.model = model; 83 | info.serialNumber = serialNumber; 84 | info.flags = flags; 85 | info.ulMaxSessionCount = ulMaxSessionCount; 86 | info.ulSessionCount = ulSessionCount; 87 | info.ulMaxRwSessionCount = ulMaxRwSessionCount; 88 | info.ulRwSessionCount = ulRwSessionCount; 89 | info.ulMaxPinLen = ulMaxPinLen; 90 | info.ulMinPinLen = ulMinPinLen; 91 | info.ulTotalPublicMemory = ulTotalPublicMemory; 92 | info.ulFreePublicMemory = ulFreePublicMemory; 93 | info.ulTotalPrivateMemory = ulTotalPrivateMemory; 94 | info.ulFreePrivateMemory = ulFreePrivateMemory; 95 | hardwareVersion.writeTo(info.hardwareVersion); 96 | firmwareVersion.writeTo(info.firmwareVersion); 97 | info.utcTime = utcTime; 98 | return info; 99 | } 100 | } 101 | -------------------------------------------------------------------------------- /src/main/java/org/pkcs11/jacknji11/CKK.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2010-2011 Joel Hockey (joel.hockey@gmail.com). All rights reserved. 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy 4 | * of this software and associated documentation files (the "Software"), to deal 5 | * in the Software without restriction, including without limitation the rights 6 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | * copies of the Software, and to permit persons to whom the Software is 8 | * furnished to do so, subject to the following conditions: 9 | * 10 | * The above copyright notice and this permission notice shall be included in 11 | * all copies or substantial portions of the Software. 12 | * 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 | * THE SOFTWARE. 20 | */ 21 | 22 | package org.pkcs11.jacknji11; 23 | 24 | import java.util.Map; 25 | 26 | /** 27 | * CKK_? constants. 28 | * @author Joel Hockey (joel.hockey@gmail.com) 29 | */ 30 | public class CKK { 31 | 32 | public static final long RSA = 0x00000000; 33 | public static final long DSA = 0x00000001; 34 | public static final long DH = 0x00000002; 35 | public static final long EC = 0x00000003; 36 | public static final long X9_42_DH = 0x00000004; 37 | public static final long KEA = 0x00000005; 38 | public static final long GENERIC_SECRET = 0x00000010; 39 | public static final long RC2 = 0x00000011; 40 | public static final long RC4 = 0x00000012; 41 | public static final long DES = 0x00000013; 42 | public static final long DES2 = 0x00000014; 43 | public static final long DES3 = 0x00000015; 44 | public static final long CAST = 0x00000016; 45 | public static final long CAST3 = 0x00000017; 46 | public static final long CAST128 = 0x00000018; 47 | public static final long RC5 = 0x00000019; 48 | public static final long IDEA = 0x0000001a; 49 | public static final long SKIPJACK = 0x0000001b; 50 | public static final long BATON = 0x0000001c; 51 | public static final long JUNIPER = 0x0000001d; 52 | public static final long CDMF = 0x0000001e; 53 | public static final long AES = 0x0000001f; 54 | public static final long SECURID = 0x00000022; 55 | public static final long HOTP = 0x00000023; 56 | public static final long ACTI = 0x00000024; 57 | public static final long CAMELLIA = 0x00000025; 58 | public static final long ARIA = 0x00000026; 59 | public static final long MD5_HMAC = 0x00000027; 60 | public static final long SHA_1_HMAC = 0x00000028; 61 | public static final long RIPEMD128_HMAC = 0x00000029; 62 | public static final long RIPEMD160_HMAC = 0x0000002a; 63 | public static final long SHA256_HMAC = 0x0000002b; 64 | public static final long SHA384_HMAC = 0x0000002c; 65 | public static final long SHA512_HMAC = 0x0000002d; 66 | public static final long SHA224_HMAC = 0x0000002e; 67 | public static final long SEED = 0x0000002f; 68 | public static final long GOSTR3410 = 0x00000030; 69 | public static final long GOSTR3411 = 0x00000031; 70 | public static final long GOST28147 = 0x00000032; 71 | public static final long CKK_EC_EDWARDS = 0x00000040; 72 | 73 | // Vendor defined values 74 | // Eracom PTK 75 | public static final long VENDOR_PTK_RSA_DISCRETE = 0x80000201L; 76 | public static final long VENDOR_PTK_DSA_DISCRETE = 0x80000202L; 77 | public static final long VENDOR_PTK_SEED = 0x80000203L; 78 | 79 | /** Maps from long value to String description (variable name). */ 80 | private static final Map L2S = C.createL2SMap(CKK.class); 81 | /** 82 | * Convert long constant value to name. 83 | * @param ckk value 84 | * @return name 85 | */ 86 | public static final String L2S(long ckk) { return C.l2s(L2S, CKK.class.getSimpleName(), ckk); } 87 | } 88 | -------------------------------------------------------------------------------- /src/main/java/org/pkcs11/jacknji11/jna/JNA_CK_TOKEN_INFO.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2010-2011 Joel Hockey (joel.hockey@gmail.com). All rights reserved. 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy 4 | * of this software and associated documentation files (the "Software"), to deal 5 | * in the Software without restriction, including without limitation the rights 6 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | * copies of the Software, and to permit persons to whom the Software is 8 | * furnished to do so, subject to the following conditions: 9 | * 10 | * The above copyright notice and this permission notice shall be included in 11 | * all copies or substantial portions of the Software. 12 | * 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 | * THE SOFTWARE. 20 | */ 21 | 22 | package org.pkcs11.jacknji11.jna; 23 | 24 | import org.pkcs11.jacknji11.CK_TOKEN_INFO; 25 | 26 | import java.util.Arrays; 27 | import java.util.List; 28 | 29 | import com.sun.jna.NativeLong; 30 | import com.sun.jna.Structure; 31 | 32 | /** 33 | * JNA wrapper for PKCS#11 CK_TOKEN_INFO struct. 34 | * @author Joel Hockey (joel.hockey@gmail.com) 35 | */ 36 | public class JNA_CK_TOKEN_INFO extends Structure { 37 | public byte[] label; 38 | public byte[] manufacturerID; 39 | public byte[] model; 40 | public byte[] serialNumber; 41 | public NativeLong flags; 42 | public NativeLong ulMaxSessionCount; 43 | public NativeLong ulSessionCount; 44 | public NativeLong ulMaxRwSessionCount; 45 | public NativeLong ulRwSessionCount; 46 | public NativeLong ulMaxPinLen; 47 | public NativeLong ulMinPinLen; 48 | public NativeLong ulTotalPublicMemory; 49 | public NativeLong ulFreePublicMemory; 50 | public NativeLong ulTotalPrivateMemory; 51 | public NativeLong ulFreePrivateMemory; 52 | public JNA_CK_VERSION hardwareVersion; 53 | public JNA_CK_VERSION firmwareVersion; 54 | public byte[] utcTime; 55 | 56 | @Override 57 | protected List getFieldOrder() { 58 | return Arrays.asList("label", "manufacturerID", "model", "serialNumber", "flags", 59 | "ulMaxSessionCount", "ulSessionCount", "ulMaxRwSessionCount", "ulRwSessionCount", 60 | "ulMaxPinLen", "ulMinPinLen", "ulTotalPublicMemory", "ulFreePublicMemory", 61 | "ulTotalPrivateMemory", "ulFreePrivateMemory", "hardwareVersion", "firmwareVersion", "utcTime"); 62 | } 63 | 64 | public JNA_CK_TOKEN_INFO readFrom(CK_TOKEN_INFO info) { 65 | label = info.label; 66 | manufacturerID = info.manufacturerID; 67 | model = info.model; 68 | serialNumber = info.serialNumber; 69 | flags = new NativeLong(info.flags); 70 | ulMaxSessionCount = new NativeLong(info.ulMaxSessionCount); 71 | ulSessionCount = new NativeLong(info.ulSessionCount); 72 | ulMaxRwSessionCount = new NativeLong(info.ulMaxRwSessionCount); 73 | ulRwSessionCount = new NativeLong(info.ulRwSessionCount); 74 | ulMaxPinLen = new NativeLong(info.ulMaxPinLen); 75 | ulMinPinLen = new NativeLong(info.ulMinPinLen); 76 | ulTotalPublicMemory = new NativeLong(info.ulTotalPublicMemory); 77 | ulFreePublicMemory = new NativeLong(info.ulFreePublicMemory); 78 | ulTotalPrivateMemory = new NativeLong(info.ulTotalPrivateMemory); 79 | ulFreePrivateMemory = new NativeLong(info.ulFreePrivateMemory); 80 | hardwareVersion = new JNA_CK_VERSION().readFrom(info.hardwareVersion); 81 | firmwareVersion = new JNA_CK_VERSION().readFrom(info.firmwareVersion); 82 | utcTime = info.utcTime; 83 | return this; 84 | } 85 | 86 | public CK_TOKEN_INFO writeTo(CK_TOKEN_INFO info) { 87 | info.label = label; 88 | info.manufacturerID = manufacturerID; 89 | info.model = model; 90 | info.serialNumber = serialNumber; 91 | info.flags = flags.intValue(); 92 | info.ulMaxSessionCount = ulMaxSessionCount.intValue(); 93 | info.ulSessionCount = ulSessionCount.intValue(); 94 | info.ulMaxRwSessionCount = ulMaxRwSessionCount.intValue(); 95 | info.ulRwSessionCount = ulRwSessionCount.intValue(); 96 | info.ulMaxPinLen = ulMaxPinLen.intValue(); 97 | info.ulMinPinLen = ulMinPinLen.intValue(); 98 | info.ulTotalPublicMemory = ulTotalPublicMemory.intValue(); 99 | info.ulFreePublicMemory = ulFreePublicMemory.intValue(); 100 | info.ulTotalPrivateMemory = ulTotalPrivateMemory.intValue(); 101 | info.ulFreePrivateMemory = ulFreePrivateMemory.intValue(); 102 | hardwareVersion.writeTo(info.hardwareVersion); 103 | firmwareVersion.writeTo(info.firmwareVersion); 104 | info.utcTime = utcTime; 105 | return info; 106 | } 107 | } 108 | -------------------------------------------------------------------------------- /src/main/java/org/pkcs11/jacknji11/CK_C_INITIALIZE_ARGS.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2010-2011 Joel Hockey (joel.hockey@gmail.com). All rights reserved. 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy 4 | * of this software and associated documentation files (the "Software"), to deal 5 | * in the Software without restriction, including without limitation the rights 6 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | * copies of the Software, and to permit persons to whom the Software is 8 | * furnished to do so, subject to the following conditions: 9 | * 10 | * The above copyright notice and this permission notice shall be included in 11 | * all copies or substantial portions of the Software. 12 | * 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 | * THE SOFTWARE. 20 | */ 21 | 22 | package org.pkcs11.jacknji11; 23 | 24 | import java.util.Map; 25 | 26 | /** 27 | * PKCS#11 CK_C_INITIALIZE_ARGS struct. 28 | * @author Joel Hockey (joel.hockey@gmail.com) 29 | */ 30 | public class CK_C_INITIALIZE_ARGS { 31 | 32 | /** 33 | * True if application threads which are executing calls to the library may not use native operating system calls to 34 | * spawn new threads; false if they may. 35 | */ 36 | public static final long CKF_LIBRARY_CANT_CREATE_OS_THREADS = 0x00000001; 37 | /** True if the library can use the native operation system threading model for locking; false otherwise. */ 38 | public static final long CKF_OS_LOCKING_OK = 0x00000002; 39 | 40 | /** Maps from long value to String description (variable name). */ 41 | private static final Map L2S = C.createL2SMap(CK_C_INITIALIZE_ARGS.class); 42 | /** 43 | * Convert long constant value to name. 44 | * @param ckf value 45 | * @return name 46 | */ 47 | public static final String L2S(long ckf) { return C.l2s(L2S, "CKF", ckf); } 48 | /** 49 | * Convert flags to string. 50 | * @param flags flags 51 | * @return string format 52 | */ 53 | public static String f2s(long flags) { return C.f2s(L2S, flags); } 54 | 55 | 56 | public CK_CREATEMUTEX createMutex; 57 | public CK_DESTROYMUTEX destroyMutex; 58 | public CK_LOCKMUTEX lockMutex; 59 | public CK_UNLOCKMUTEX unlockMutex; 60 | public long flags; 61 | public NativePointer pReserved; 62 | 63 | /** 64 | * Initialise struct with supplied values. 65 | * @param createMutex create mutex 66 | * @param destroyMutex destroy mutex 67 | * @param lockMutex lock mutex 68 | * @param unlockMutex unlock mutex 69 | * @param flags locking flags constant from CKF 70 | */ 71 | public CK_C_INITIALIZE_ARGS(CK_CREATEMUTEX createMutex, CK_DESTROYMUTEX destroyMutex, CK_LOCKMUTEX lockMutex, 72 | CK_UNLOCKMUTEX unlockMutex, long flags) { 73 | 74 | this.createMutex = createMutex; 75 | this.destroyMutex = destroyMutex; 76 | this.lockMutex = lockMutex; 77 | this.unlockMutex = unlockMutex; 78 | this.flags = flags; 79 | } 80 | 81 | /** @return True, if the provided flag is set */ 82 | public boolean isFlagSet(long CKF_FLAG) { 83 | return (flags & CKF_FLAG) != 0L; 84 | } 85 | 86 | /** @return string */ 87 | public String toString() { 88 | return String.format("create=%s destroy=%s lock=%s unlock=%s flags=0x%08x{%s}", 89 | createMutex, destroyMutex, lockMutex, unlockMutex, flags, f2s(flags)); 90 | } 91 | 92 | /** 93 | * JNA wrapper for PKCS#11 CK_CREATEMUTEX. 94 | * @author Joel Hockey 95 | */ 96 | public interface CK_CREATEMUTEX { 97 | /** 98 | * Create Mutex. 99 | * @param mutex mutex 100 | * @return {@link CKR} return code 101 | */ 102 | long invoke(NativePointerByReference mutex); 103 | } 104 | 105 | /** 106 | * JNA wrapper for PKCS#11 CK_DESTROYMUTEX. 107 | * @author Joel Hockey 108 | */ 109 | public interface CK_DESTROYMUTEX { 110 | /** 111 | * Destroy Mutex. 112 | * @param mutex mutex 113 | * @return {@link CKR} return code 114 | */ 115 | long invoke(NativePointer mutex); 116 | } 117 | 118 | /** 119 | * JNA wrapper for PKCS#11 CK_LOCKMUTEX. 120 | * @author Joel Hockey 121 | */ 122 | public interface CK_LOCKMUTEX { 123 | /** 124 | * Lock Mutex. 125 | * @param mutex mutex 126 | * @return {@link CKR} return code 127 | */ 128 | long invoke(NativePointer mutex); 129 | } 130 | 131 | /** 132 | * JNA wrapper for PKCS#11 CK_UNLOCKMUTEX. 133 | * @author Joel Hockey 134 | */ 135 | public interface CK_UNLOCKMUTEX { 136 | /** 137 | * Unlock Mutex. 138 | * @param mutex mutex 139 | * @return {@link CKR} return code 140 | */ 141 | long invoke(NativePointer mutex); 142 | } 143 | } 144 | -------------------------------------------------------------------------------- /src/main/java/org/pkcs11/jacknji11/CK_TOKEN_INFO.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2010-2011 Joel Hockey (joel.hockey@gmail.com). All rights reserved. 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy 4 | * of this software and associated documentation files (the "Software"), to deal 5 | * in the Software without restriction, including without limitation the rights 6 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | * copies of the Software, and to permit persons to whom the Software is 8 | * furnished to do so, subject to the following conditions: 9 | * 10 | * The above copyright notice and this permission notice shall be included in 11 | * all copies or substantial portions of the Software. 12 | * 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 | * THE SOFTWARE. 20 | */ 21 | 22 | package org.pkcs11.jacknji11; 23 | 24 | import java.util.Map; 25 | 26 | /** 27 | * PKCS#11 CK_TOKEN_INFO struct. 28 | * @author Joel Hockey (joel.hockey@gmail.com) 29 | */ 30 | public class CK_TOKEN_INFO { 31 | public static final long CKF_RNG = 0x00000001; 32 | public static final long CKF_WRITE_PROTECTED = 0x00000002; 33 | public static final long CKF_LOGIN_REQUIRED = 0x00000004; 34 | public static final long CKF_USER_PIN_INITIALIZED = 0x00000008; 35 | public static final long CKF_RESTORE_KEY_NOT_NEEDED= 0x00000020; 36 | public static final long CKF_CLOCK_ON_TOKEN = 0x00000040; 37 | public static final long CKF_PROTECTED_AUTHENTICATION_PATH =0x00000100; 38 | public static final long CKF_DUAL_CRYPTO_OPERATIONS =0x00000200; 39 | public static final long CKF_TOKEN_INITIALIZED =0x00000400; 40 | public static final long CKF_SECONDARY_AUTHENTICATION = 0x00000800; 41 | public static final long CKF_USER_PIN_COUNT_LOW =0x00010000; 42 | public static final long CKF_USER_PIN_FINAL_TRY =0x00020000; 43 | public static final long CKF_USER_PIN_LOCKED =0x00040000; 44 | public static final long CKF_USER_PIN_TO_BE_CHANGED =0x00080000; 45 | public static final long CKF_SO_PIN_COUNT_LOW =0x00100000; 46 | public static final long CKF_SO_PIN_FINAL_TRY =0x00200000; 47 | public static final long CKF_SO_PIN_LOCKED =0x00400000; 48 | public static final long CKF_SO_PIN_TO_BE_CHANGED =0x00800000; 49 | 50 | /** Maps from long value to String description (variable name). */ 51 | private static final Map L2S = C.createL2SMap(CK_TOKEN_INFO.class); 52 | /** 53 | * Convert long constant value to name. 54 | * @param ckf value 55 | * @return name 56 | */ 57 | public static final String L2S(long ckf) { return C.l2s(L2S, "CKF", ckf); } 58 | /** 59 | * Convert flags to string. 60 | * @param flags flags 61 | * @return string format 62 | */ 63 | public static String f2s(long flags) { return C.f2s(L2S, flags); } 64 | 65 | public byte[] label = new byte[32]; 66 | public byte[] manufacturerID = new byte[32]; 67 | public byte[] model = new byte[16]; 68 | public byte[] serialNumber = new byte[16]; 69 | public long flags; 70 | public long ulMaxSessionCount; 71 | public long ulSessionCount; 72 | public long ulMaxRwSessionCount; 73 | public long ulRwSessionCount; 74 | public long ulMaxPinLen; 75 | public long ulMinPinLen; 76 | public long ulTotalPublicMemory; 77 | public long ulFreePublicMemory; 78 | public long ulTotalPrivateMemory; 79 | public long ulFreePrivateMemory; 80 | public CK_VERSION hardwareVersion = new CK_VERSION(); 81 | public CK_VERSION firmwareVersion = new CK_VERSION(); 82 | public byte[] utcTime = new byte[16]; 83 | 84 | /** @return True, if the provided flag is set */ 85 | public boolean isFlagSet(long CKF_FLAG) { 86 | return (flags & CKF_FLAG) != 0L; 87 | } 88 | 89 | /** @return string */ 90 | public String toString() { 91 | return String.format("(\n label=%s\n manufacturerID=%s\n model=%s\n serialNumber=%s\n flags=0x%08x{%s}" + 92 | "\n maxSessionCount=%d\n sessionCount=%d\n maxRwSessionCount=%d\n rwSessionCount=%d" + 93 | "\n maxPinLen=%d\n minPinLen=%d\n totalPublicMemory=%d\n freePublicMemory=%d" + 94 | "\n totalPrivateMemory=%d\n freePrivateMemory=%d" + 95 | "\n hardwareVersion=%d.%d\n firmwareVersion=%d.%d\n utcTime=%s\n)", 96 | Buf.escstr(label), Buf.escstr(manufacturerID), Buf.escstr(model), Buf.escstr(serialNumber), 97 | flags, f2s(flags), ulMaxSessionCount, ulSessionCount, 98 | ulMaxRwSessionCount, ulRwSessionCount, 99 | ulMaxPinLen, ulMinPinLen, 100 | ulTotalPublicMemory, ulFreePublicMemory, 101 | ulTotalPrivateMemory, ulFreePrivateMemory, 102 | hardwareVersion.major & 0xff, hardwareVersion.minor & 0xff, 103 | firmwareVersion.major & 0xff, firmwareVersion.minor & 0xff, 104 | Buf.escstr(utcTime)); 105 | } 106 | } 107 | -------------------------------------------------------------------------------- /src/main/java/org/pkcs11/jacknji11/jffi/JFFI_CK_C_INITIALIZE_ARGS.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2010-2011 Joel Hockey (joel.hockey@gmail.com). All rights reserved. 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy 4 | * of this software and associated documentation files (the "Software"), to deal 5 | * in the Software without restriction, including without limitation the rights 6 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | * copies of the Software, and to permit persons to whom the Software is 8 | * furnished to do so, subject to the following conditions: 9 | * 10 | * The above copyright notice and this permission notice shall be included in 11 | * all copies or substantial portions of the Software. 12 | * 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 | * THE SOFTWARE. 20 | */ 21 | 22 | package org.pkcs11.jacknji11.jffi; 23 | 24 | import jnr.ffi.Struct; 25 | import jnr.ffi.annotations.Delegate; 26 | import jnr.ffi.byref.PointerByReference; 27 | 28 | import org.pkcs11.jacknji11.CK_C_INITIALIZE_ARGS; 29 | import org.pkcs11.jacknji11.CK_C_INITIALIZE_ARGS.CK_CREATEMUTEX; 30 | import org.pkcs11.jacknji11.CK_C_INITIALIZE_ARGS.CK_DESTROYMUTEX; 31 | import org.pkcs11.jacknji11.CK_C_INITIALIZE_ARGS.CK_LOCKMUTEX; 32 | import org.pkcs11.jacknji11.CK_C_INITIALIZE_ARGS.CK_UNLOCKMUTEX; 33 | import org.pkcs11.jacknji11.NativePointer; 34 | import org.pkcs11.jacknji11.NativePointerByReference; 35 | 36 | /** 37 | * JFFI wrapper for PKCS#11 CK_C_INITIALIZE_ARGS struct. Also includes JFFI mutex interface wrappers. 38 | * @author Joel Hockey (joel.hockey@gmail.com) 39 | */ 40 | public class JFFI_CK_C_INITIALIZE_ARGS extends Struct { 41 | 42 | public JFFI_CK_CREATEMUTEX createMutex; 43 | public JFFI_CK_DESTROYMUTEX destroyMutex; 44 | public JFFI_CK_LOCKMUTEX lockMutex; 45 | public JFFI_CK_UNLOCKMUTEX unlockMutex; 46 | public long flags; 47 | public jnr.ffi.Pointer pReserved; 48 | 49 | public JFFI_CK_C_INITIALIZE_ARGS(final CK_C_INITIALIZE_ARGS args) { 50 | super(jnr.ffi.Runtime.getSystemRuntime()); 51 | this.createMutex = new JFFI_CK_CREATEMUTEX() { 52 | public long invoke(NativePointerByReference mutex) { 53 | return args.createMutex.invoke(mutex); 54 | } 55 | public long invoke(PointerByReference mutex) { 56 | return invoke(new NativePointerByReference( 57 | new NativePointer(mutex.getValue().address()))); 58 | } 59 | }; 60 | this.destroyMutex = new JFFI_CK_DESTROYMUTEX() { 61 | public long invoke(NativePointer mutex) { 62 | return args.destroyMutex.invoke(mutex); 63 | } 64 | public long invoke(jnr.ffi.Pointer mutex) { 65 | return invoke(new NativePointer(mutex.address())); 66 | } 67 | }; 68 | this.lockMutex = new JFFI_CK_LOCKMUTEX() { 69 | public long invoke(NativePointer mutex) { 70 | return args.lockMutex.invoke(mutex); 71 | } 72 | public long invoke(jnr.ffi.Pointer mutex) { 73 | return invoke(new NativePointer(mutex.address())); 74 | } 75 | }; 76 | this.unlockMutex = new JFFI_CK_UNLOCKMUTEX() { 77 | public long invoke(NativePointer mutex) { 78 | return args.unlockMutex.invoke(mutex); 79 | } 80 | public long invoke(jnr.ffi.Pointer mutex) { 81 | return invoke(new NativePointer(mutex.address())); 82 | } 83 | }; 84 | this.flags = args.flags; 85 | } 86 | 87 | /** 88 | * JNA wrapper for PKCS#11 CK_CREATEMUTEX. 89 | * @author Joel Hockey 90 | */ 91 | public interface JFFI_CK_CREATEMUTEX extends CK_CREATEMUTEX { 92 | /** 93 | * Create Mutex. 94 | * @param mutex mutex 95 | * @return {@link CKR} return code 96 | */ 97 | @Delegate 98 | long invoke(PointerByReference mutex); 99 | } 100 | 101 | /** 102 | * JNA wrapper for PKCS#11 CK_DESTROYMUTEX. 103 | * @author Joel Hockey 104 | */ 105 | public interface JFFI_CK_DESTROYMUTEX extends CK_DESTROYMUTEX { 106 | /** 107 | * Destroy Mutex. 108 | * @param mutex mutex 109 | * @return {@link CKR} return code 110 | */ 111 | @Delegate 112 | long invoke(jnr.ffi.Pointer mutex); 113 | } 114 | 115 | /** 116 | * JNA wrapper for PKCS#11 CK_LOCKMUTEX. 117 | * @author Joel Hockey 118 | */ 119 | public interface JFFI_CK_LOCKMUTEX extends CK_LOCKMUTEX { 120 | /** 121 | * Lock Mutex. 122 | * @param mutex mutex 123 | * @return {@link CKR} return code 124 | */ 125 | @Delegate 126 | long invoke(jnr.ffi.Pointer mutex); 127 | } 128 | 129 | /** 130 | * JNA wrapper for PKCS#11 CK_UNLOCKMUTEX. 131 | * @author Joel Hockey 132 | */ 133 | public interface JFFI_CK_UNLOCKMUTEX extends CK_UNLOCKMUTEX { 134 | /** 135 | * Unlock Mutex. 136 | * @param mutex mutex 137 | * @return {@link CKR} return code 138 | */ 139 | @Delegate 140 | long invoke(jnr.ffi.Pointer mutex); 141 | } 142 | } 143 | -------------------------------------------------------------------------------- /src/main/java/org/pkcs11/jacknji11/jna/Template.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2010-2011 Joel Hockey (joel.hockey@gmail.com). All rights reserved. 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy 4 | * of this software and associated documentation files (the "Software"), to deal 5 | * in the Software without restriction, including without limitation the rights 6 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | * copies of the Software, and to permit persons to whom the Software is 8 | * furnished to do so, subject to the following conditions: 9 | * 10 | * The above copyright notice and this permission notice shall be included in 11 | * all copies or substantial portions of the Software. 12 | * 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 | * THE SOFTWARE. 20 | */ 21 | 22 | package org.pkcs11.jacknji11.jna; 23 | 24 | import java.util.ArrayList; 25 | import java.util.List; 26 | 27 | import org.pkcs11.jacknji11.CKA; 28 | 29 | import com.sun.jna.Memory; 30 | import com.sun.jna.Native; 31 | import com.sun.jna.NativeLong; 32 | import com.sun.jna.Pointer; 33 | import com.sun.jna.PointerType; 34 | 35 | /** 36 | * Wrapper for CK_ATTRIBUTE[] (in this case it is CKA class that represents 37 | * PKCS#11 CK_ATTRIBUTE struct). JNA direct memory mapping doesn't seem to 38 | * support struct arrays, so this class is required to map the 39 | * list of (type, pValue, ulValueLen) into a contiguous block of memory. 40 | * 41 | * @author Joel Hockey (joel.hockey@gmail.com) 42 | */ 43 | public class Template extends PointerType { 44 | private CKA[] list; 45 | private List pValues; 46 | private int listLen; 47 | 48 | /** Default no-arg constructor required by JNA. */ 49 | public Template() { 50 | this(null); 51 | } 52 | 53 | /** 54 | * @return number of attributes in this template 55 | */ 56 | public int getListLen() { 57 | return listLen; 58 | } 59 | 60 | /** 61 | * Allocates JNA Memory and writes CKA[] values. 62 | * @param list template 63 | */ 64 | public Template(CKA[] list) { 65 | this.list = list; 66 | listLen = list == null ? 0 : list.length; 67 | if (listLen == 0) { 68 | return; 69 | } 70 | setPointer(new Memory(listLen * (NativeLong.SIZE + Native.POINTER_SIZE + NativeLong.SIZE))); 71 | // Keep java references over the life time of this Template to avoid native memory being freed before use 72 | pValues = new ArrayList<>(); 73 | int offset = 0; 74 | 75 | for (int i = 0; i < listLen; i++) { 76 | // type 77 | if (NativeLong.SIZE == 4) { 78 | getPointer().setInt(offset, (int) list[i].type); 79 | } else { 80 | getPointer().setLong(offset, list[i].type); 81 | } 82 | offset += NativeLong.SIZE; 83 | 84 | // pValue 85 | Memory pValue = null; 86 | if (list[i].ulValueLen > 0) { 87 | pValue = new Memory(list[i].ulValueLen); 88 | pValue.write(0, list[i].pValue, 0, (int) list[i].ulValueLen); 89 | } 90 | getPointer().setPointer(offset, pValue); 91 | // We saved a "long" pointer to the memory allocated by "new Memory" above, keep the java object 92 | // reference as well to avoid it being garbage collected, which will free the native memory allocated when 93 | // Memory.finalize is called 94 | pValues.add(pValue); 95 | offset += Native.POINTER_SIZE; 96 | 97 | // ulValueLen 98 | if (NativeLong.SIZE == 4) { 99 | getPointer().setInt(offset, (int) list[i].ulValueLen); 100 | } else { 101 | getPointer().setLong(offset, list[i].ulValueLen); 102 | } 103 | offset += NativeLong.SIZE; 104 | } 105 | } 106 | 107 | /** 108 | * Reads (updated) JNA Memory and modifies values in list. 109 | * This must be called after native PKCS#11 calls in {@link NativeProvider} that modify CK_ATTRIBUTE struct such as 110 | * {@link NativeProvider#C_GetAttributeValue(NativeLong, NativeLong, Template, NativeLong)}. 111 | * This is automatically done by the {@link C} and {@link CE} interfaces. 112 | */ 113 | public void update() { 114 | if (listLen == 0) { 115 | return; 116 | } 117 | int offset = 0; 118 | for (CKA cka : list) { 119 | offset += NativeLong.SIZE; // skip type 120 | 121 | // read pValue 122 | Pointer ptr = getPointer().getPointer(offset); 123 | offset += Native.POINTER_SIZE; 124 | 125 | // read ulValueLen 126 | int ulValueLen = 0; 127 | if (NativeLong.SIZE == 4) { 128 | ulValueLen = getPointer().getInt(offset); 129 | } else { 130 | ulValueLen = (int) getPointer().getLong(offset); 131 | } 132 | offset += NativeLong.SIZE; 133 | 134 | // read contents into pValue if ptr != null && ulValueLen > 0 135 | if (ptr != null && ulValueLen > 0) { 136 | ptr.read(0, cka.pValue, 0, ulValueLen); 137 | } 138 | cka.ulValueLen = ulValueLen; 139 | cka.set(); 140 | } 141 | } 142 | } 143 | -------------------------------------------------------------------------------- /src/main/java/org/pkcs11/jacknji11/jna/JNA_CK_C_INITIALIZE_ARGS.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2010-2011 Joel Hockey (joel.hockey@gmail.com). All rights reserved. 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy 4 | * of this software and associated documentation files (the "Software"), to deal 5 | * in the Software without restriction, including without limitation the rights 6 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | * copies of the Software, and to permit persons to whom the Software is 8 | * furnished to do so, subject to the following conditions: 9 | * 10 | * The above copyright notice and this permission notice shall be included in 11 | * all copies or substantial portions of the Software. 12 | * 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 | * THE SOFTWARE. 20 | */ 21 | 22 | package org.pkcs11.jacknji11.jna; 23 | 24 | import org.pkcs11.jacknji11.CK_C_INITIALIZE_ARGS; 25 | import org.pkcs11.jacknji11.CK_C_INITIALIZE_ARGS.CK_CREATEMUTEX; 26 | import org.pkcs11.jacknji11.CK_C_INITIALIZE_ARGS.CK_DESTROYMUTEX; 27 | import org.pkcs11.jacknji11.CK_C_INITIALIZE_ARGS.CK_LOCKMUTEX; 28 | import org.pkcs11.jacknji11.CK_C_INITIALIZE_ARGS.CK_UNLOCKMUTEX; 29 | import org.pkcs11.jacknji11.NativePointer; 30 | import org.pkcs11.jacknji11.NativePointerByReference; 31 | 32 | import java.util.Arrays; 33 | import java.util.List; 34 | 35 | import com.sun.jna.Callback; 36 | import com.sun.jna.NativeLong; 37 | import com.sun.jna.Pointer; 38 | import com.sun.jna.Structure; 39 | import com.sun.jna.ptr.PointerByReference; 40 | 41 | /** 42 | * JNA wrapper for PKCS#11 CK_C_INITIALIZE_ARGS struct. Also includes JNA mutex interface wrappers. 43 | * @author Joel Hockey (joel.hockey@gmail.com) 44 | */ 45 | public class JNA_CK_C_INITIALIZE_ARGS extends Structure { 46 | 47 | public JNA_CK_CREATEMUTEX createMutex; 48 | public JNA_CK_DESTROYMUTEX destroyMutex; 49 | public JNA_CK_LOCKMUTEX lockMutex; 50 | public JNA_CK_UNLOCKMUTEX unlockMutex; 51 | public NativeLong flags; 52 | public Pointer pReserved; 53 | 54 | @Override 55 | protected List getFieldOrder() { 56 | return Arrays.asList("createMutex", "destroyMutex", "lockMutex", "unlockMutex", "flags", "pReserved"); 57 | } 58 | 59 | public JNA_CK_C_INITIALIZE_ARGS(final CK_C_INITIALIZE_ARGS args) { 60 | // Need to set alignment to none. Otherwise there would be alignment 61 | // padding before 'pReserved' on Win64 (there, long is 4 bytes but 62 | // pointers have 8 byte alignment) 63 | super(ALIGN_NONE); 64 | if (args.createMutex != null) { 65 | this.createMutex = new JNA_CK_CREATEMUTEX() { 66 | public long invoke(NativePointerByReference mutex) { 67 | return args.createMutex.invoke(mutex); 68 | } 69 | public NativeLong invoke(PointerByReference mutex) { 70 | return new NativeLong(invoke(new NativePointerByReference( 71 | new NativePointer(Pointer.nativeValue(mutex.getPointer()))))); 72 | } 73 | }; 74 | } 75 | if (args.destroyMutex != null) { 76 | this.destroyMutex = new JNA_CK_DESTROYMUTEX() { 77 | public long invoke(NativePointer mutex) { 78 | return args.destroyMutex.invoke(mutex); 79 | } 80 | public NativeLong invoke(Pointer mutex) { 81 | return new NativeLong(invoke(new NativePointer(Pointer.nativeValue(mutex)))); 82 | } 83 | }; 84 | } 85 | if (args.lockMutex != null) { 86 | this.lockMutex = new JNA_CK_LOCKMUTEX() { 87 | public long invoke(NativePointer mutex) { 88 | return args.lockMutex.invoke(mutex); 89 | } 90 | public NativeLong invoke(Pointer mutex) { 91 | return new NativeLong(invoke(new NativePointer(Pointer.nativeValue(mutex)))); 92 | } 93 | }; 94 | } 95 | if (args.unlockMutex != null) { 96 | this.unlockMutex = new JNA_CK_UNLOCKMUTEX() { 97 | public long invoke(NativePointer mutex) { 98 | return args.unlockMutex.invoke(mutex); 99 | } 100 | public NativeLong invoke(Pointer mutex) { 101 | return new NativeLong(invoke(new NativePointer(Pointer.nativeValue(mutex)))); 102 | } 103 | }; 104 | } 105 | this.flags = new NativeLong(args.flags); 106 | } 107 | 108 | /** 109 | * JNA wrapper for PKCS#11 CK_CREATEMUTEX. 110 | * @author Joel Hockey 111 | */ 112 | public interface JNA_CK_CREATEMUTEX extends CK_CREATEMUTEX, Callback { 113 | /** 114 | * Create Mutex. 115 | * @param mutex mutex 116 | * @return {@link CKR} return code 117 | */ 118 | NativeLong invoke(PointerByReference mutex); 119 | } 120 | 121 | /** 122 | * JNA wrapper for PKCS#11 CK_DESTROYMUTEX. 123 | * @author Joel Hockey 124 | */ 125 | public interface JNA_CK_DESTROYMUTEX extends CK_DESTROYMUTEX, Callback { 126 | /** 127 | * Destroy Mutex. 128 | * @param mutex mutex 129 | * @return {@link CKR} return code 130 | */ 131 | NativeLong invoke(Pointer mutex); 132 | } 133 | 134 | /** 135 | * JNA wrapper for PKCS#11 CK_LOCKMUTEX. 136 | * @author Joel Hockey 137 | */ 138 | public interface JNA_CK_LOCKMUTEX extends CK_LOCKMUTEX, Callback { 139 | /** 140 | * Lock Mutex. 141 | * @param mutex mutex 142 | * @return {@link CKR} return code 143 | */ 144 | NativeLong invoke(Pointer mutex); 145 | } 146 | 147 | /** 148 | * JNA wrapper for PKCS#11 CK_UNLOCKMUTEX. 149 | * @author Joel Hockey 150 | */ 151 | public interface JNA_CK_UNLOCKMUTEX extends CK_UNLOCKMUTEX, Callback { 152 | /** 153 | * Unlock Mutex. 154 | * @param mutex mutex 155 | * @return {@link CKR} return code 156 | */ 157 | NativeLong invoke(Pointer mutex); 158 | } 159 | } 160 | -------------------------------------------------------------------------------- /src/main/java/org/pkcs11/jacknji11/ULong.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2010-2011 Joel Hockey (joel.hockey@gmail.com). All rights reserved. 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy 4 | * of this software and associated documentation files (the "Software"), to deal 5 | * in the Software without restriction, including without limitation the rights 6 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | * copies of the Software, and to permit persons to whom the Software is 8 | * furnished to do so, subject to the following conditions: 9 | * 10 | * The above copyright notice and this permission notice shall be included in 11 | * all copies or substantial portions of the Software. 12 | * 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 | * THE SOFTWARE. 20 | */ 21 | 22 | package org.pkcs11.jacknji11; 23 | 24 | import java.nio.ByteOrder; 25 | 26 | /** 27 | * Converts between Java long and 'unsigned long int' catering for size(long) and endianness. 28 | * @author Joel Hockey (joel.hockey@gmail.com) 29 | */ 30 | public class ULong { 31 | public enum ULongSize { ULONG4(4), ULONG8(8); 32 | private int size; 33 | private ULongSize(int size) { this.size = size; } 34 | public int size() { return size;} 35 | }; 36 | public static ULongSize ULONG_SIZE = ULongSize.ULONG4; 37 | 38 | /** 39 | * Convert list of Java long to byte array 40 | * of native long catering for endianness and sizeof(long) 41 | * @param la list of longs 42 | * @return native format 43 | */ 44 | public static byte[] ulong2b(long... la) { 45 | if (la == null || la.length == 0) { 46 | return new byte[0]; 47 | } 48 | 49 | if (ByteOrder.nativeOrder() == ByteOrder.BIG_ENDIAN) { 50 | if (ULONG_SIZE == ULongSize.ULONG4) { 51 | byte[] result = new byte[la.length * 4]; 52 | // start at rhs and work back for big-endian 53 | int j = result.length - 1; 54 | for (int i = la.length - 1; i >= 0; i--) { 55 | long val = la[i]; 56 | result[j--] = (byte) val; 57 | val >>= 8; 58 | result[j--] = (byte) val; 59 | val >>= 8; 60 | result[j--] = (byte) val; 61 | val >>= 8; 62 | result[j--] = (byte) val; 63 | } 64 | return result; 65 | } else { 66 | byte[] result = new byte[la.length * 8]; 67 | // start at rhs and work back for big-endian 68 | int j = result.length - 1; 69 | for (int i = la.length - 1; i >= 0; i--) { 70 | long val = la[i]; 71 | result[j--] = (byte) val; 72 | val >>= 8; 73 | result[j--] = (byte) val; 74 | val >>= 8; 75 | result[j--] = (byte) val; 76 | val >>= 8; 77 | result[j--] = (byte) val; 78 | val >>= 8; 79 | result[j--] = (byte) val; 80 | val >>= 8; 81 | result[j--] = (byte) val; 82 | val >>= 8; 83 | result[j--] = (byte) val; 84 | val >>= 8; 85 | result[j--] = (byte) val; 86 | } 87 | return result; 88 | } 89 | } else { 90 | if (ULONG_SIZE == ULongSize.ULONG4) { 91 | byte[] result = new byte[la.length * 4]; 92 | // start at lhs and work forward for little-endian 93 | int j = 0; 94 | for (int i = 0; i < la.length; i++) { 95 | long val = la[i]; 96 | result[j++] = (byte) val; 97 | val >>= 8; 98 | result[j++] = (byte) val; 99 | val >>= 8; 100 | result[j++] = (byte) val; 101 | val >>= 8; 102 | result[j++] = (byte) val; 103 | } 104 | return result; 105 | } else { 106 | byte[] result = new byte[la.length * 8]; 107 | // start at lhs and work forward for little-endian 108 | int j = 0; 109 | for (int i = 0; i < la.length; i++) { 110 | long val = la[i]; 111 | result[j++] = (byte) val; 112 | val >>= 8; 113 | result[j++] = (byte) val; 114 | val >>= 8; 115 | result[j++] = (byte) val; 116 | val >>= 8; 117 | result[j++] = (byte) val; 118 | val >>= 8; 119 | result[j++] = (byte) val; 120 | val >>= 8; 121 | result[j++] = (byte) val; 122 | val >>= 8; 123 | result[j++] = (byte) val; 124 | val >>= 8; 125 | result[j++] = (byte) val; 126 | } 127 | return result; 128 | } 129 | } 130 | } 131 | 132 | /** 133 | * Convert byte array of native long to java long 134 | * @param buf native long 135 | * @return java long 136 | */ 137 | public static long b2ulong(byte[] buf) { 138 | if (ByteOrder.nativeOrder() == ByteOrder.BIG_ENDIAN) { 139 | if (ULONG_SIZE == ULongSize.ULONG4) { 140 | return ((buf[0] & 0xff) << 24) | ((buf[1] & 0xff) << 16) | ((buf[2] & 0xff) << 8) | (buf[3] & 0xff); 141 | } else { 142 | return ((buf[0] & 0xff) << 56) | ((buf[1] & 0xff) << 48) | ((buf[2] & 0xff) << 40) | ((buf[3] & 0xff) << 32) | ((buf[4] & 0xff) << 24) | ((buf[5] & 0xff) << 16) | ((buf[6] & 0xff) << 8) | (buf[7] & 0xff); 143 | } 144 | } else { 145 | if (ULONG_SIZE == ULongSize.ULONG4) { 146 | return ((buf[3] & 0xff) << 24) | ((buf[2] & 0xff) << 16) | ((buf[1] & 0xff) << 8) | (buf[0] & 0xff); 147 | } else { 148 | return ((buf[7] & 0xff) << 56) | ((buf[6] & 0xff) << 48) | ((buf[5] & 0xff) << 40) | ((buf[4] & 0xff) << 32) | ((buf[3] & 0xff) << 24) | ((buf[2] & 0xff) << 16) | ((buf[1] & 0xff) << 8) | (buf[0] & 0xff); 149 | } 150 | } 151 | } 152 | } 153 | 154 | 155 | -------------------------------------------------------------------------------- /src/main/java/org/pkcs11/jacknji11/Buf.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2008-2011 Joel Hockey (joel.hockey@gmail.com). All rights reserved. 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy 5 | * of this software and associated documentation files (the "Software"), to deal 6 | * in the Software without restriction, including without limitation the rights 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | * copies of the Software, and to permit persons to whom the Software is 9 | * furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in 12 | * all copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 20 | * THE SOFTWARE. 21 | */ 22 | package org.pkcs11.jacknji11; 23 | 24 | /** 25 | * Byte array utils. 26 | * 27 | * @author Joel Hockey (joel.hockey@gmail.com) 28 | */ 29 | public class Buf { 30 | /** 31 | * Maps how to convert byte to printable java string. 32 | * -1 => non-printable (use '\\uxxxx'), 0 => printable as-is, else gives escape char. 33 | */ 34 | public static final int[] LKP_ESCAPES = new int[256]; 35 | 36 | static { 37 | // non-printable or escapable 38 | for (int i = 0; i < 0x20; i++) { 39 | LKP_ESCAPES[i] = -1; 40 | } 41 | for (int i = 127; i < 256; i++) { 42 | LKP_ESCAPES[i] = -1; 43 | } 44 | // special escapes 45 | LKP_ESCAPES['\b'] = 'b'; 46 | LKP_ESCAPES['\t'] = 't'; 47 | LKP_ESCAPES['\n'] = 'n'; 48 | LKP_ESCAPES['\r'] = 'r'; 49 | LKP_ESCAPES['\f'] = 'f'; 50 | LKP_ESCAPES['"'] = '"'; 51 | LKP_ESCAPES['\''] = '\''; 52 | LKP_ESCAPES['\\'] = '\\'; 53 | } 54 | /** 55 | * concatenate bufs. 56 | * @param bufs parts to concatenate 57 | * @return concatenated 58 | */ 59 | public static byte[] cat(byte[]... bufs) { 60 | if (bufs == null) { 61 | return null; 62 | } 63 | int l = 0; 64 | for (int i = 0; i < bufs.length; i++) { 65 | l += bufs[i] == null ? 0 : bufs[i].length; 66 | } 67 | 68 | byte[] result = new byte[l]; 69 | cat(result, 0, bufs); 70 | return result; 71 | } 72 | 73 | /** 74 | * cat into dest starting at start. 75 | * @param dest destination 76 | * @param start position to start writing 77 | * @param bufs parts to concatenate 78 | */ 79 | public static void cat(byte[] dest, int start, byte[]... bufs) { 80 | if (bufs == null) { 81 | return; 82 | } 83 | for (int i = 0; i < bufs.length; i++) { 84 | if (bufs[i] != null) { 85 | System.arraycopy(bufs[i], 0, dest, start, bufs[i].length); 86 | start += bufs[i].length; 87 | } 88 | } 89 | } 90 | 91 | /** 92 | * Substring of buf. Allows negative indexing as per python. 93 | * If range of substring outside range of buf, then result is zero-padded. 94 | * Result will be left justified if start is positive, 95 | * right-justified if start is negative. 96 | * @param src src to get sub buf 97 | * @param start index to start 98 | * @param len index to end 99 | * @return buf len (end - start) containing src 100 | */ 101 | public static byte[] substring(byte[] src, int start, int len) { 102 | if (len < 0) { 103 | throw new IllegalArgumentException("len cannot be negative, got: " + len); 104 | } 105 | 106 | if (src == null) { src = new byte[0]; } 107 | if (src != null && start == 0 && len == src.length) { 108 | return src; 109 | } 110 | 111 | // result is src 112 | if (len == src.length && (start == 0 || start == -src.length)) { 113 | return src; 114 | } 115 | 116 | byte[] result = new byte[len]; 117 | // left-justified 118 | if (start >= 0 && start < src.length) { 119 | int tocopy = Math.min(len, src.length - start); 120 | System.arraycopy(src, start, result, 0, tocopy); 121 | 122 | // right-justified 123 | } else if (start < 0 && start + src.length + len > 0) { 124 | start += src.length; 125 | int tocopy = start < 0 ? 126 | Math.min(len + start, src.length) 127 | : Math.min(len, src.length - start); 128 | System.arraycopy(src, Math.max(0, start), result, len - tocopy, tocopy); 129 | } 130 | 131 | return result; 132 | } 133 | 134 | /** 135 | * Return java-escaped string enclosed in double quotes. 136 | * @param buf buf 137 | * @return java-escaped string enclosed in double quotes 138 | */ 139 | public static String escstr(byte[] buf) { 140 | if (buf == null) { 141 | return null; 142 | } 143 | // size for totally printable string - should help perf 144 | StringBuilder sb = new StringBuilder(buf.length + 2); 145 | sb.append('"'); 146 | for (int i = 0; i < buf.length; i++) { 147 | int c = buf[i] & 0xff; 148 | int escape = LKP_ESCAPES[c]; 149 | // printable without escape 150 | if (escape == 0) { 151 | sb.append((char) c); 152 | } else if (escape != -1) { 153 | sb.append('\\').append((char) escape); 154 | } else { 155 | sb.append("\\u00").append(Hex.HEX_B2S[buf[i] & 0xff]); 156 | } 157 | } 158 | sb.append('"'); 159 | return sb.toString(); 160 | } 161 | 162 | /** 163 | * Convert char array to byte array using single byte encoding. 164 | * Each char is cast to byte. 165 | * @param c char array 166 | * @return byte array with each char cast to byte 167 | */ 168 | public static byte[] c2b(char[] c) { 169 | if (c == null) return null; 170 | byte[] result = new byte[c.length]; 171 | for (int i = 0; i < c.length; i++) { 172 | result[i] = (byte) c[i]; 173 | } 174 | return result; 175 | } 176 | 177 | /** 178 | * Convert string to byte array using single byte encoding. 179 | * Each char is cast to byte. 180 | * @param s string 181 | * @return byte array with each char cast to byte 182 | */ 183 | public static byte[] c2b(String s) { 184 | if (s == null) return null; 185 | return c2b(s.toCharArray()); 186 | } 187 | } 188 | -------------------------------------------------------------------------------- /src/main/java/org/pkcs11/jacknji11/AttributeLengthStrategy.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2010-2011 Joel Hockey (joel.hockey@gmail.com). All rights reserved. 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy 4 | * of this software and associated documentation files (the "Software"), to deal 5 | * in the Software without restriction, including without limitation the rights 6 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | * copies of the Software, and to permit persons to whom the Software is 8 | * furnished to do so, subject to the following conditions: 9 | * 10 | * The above copyright notice and this permission notice shall be included in 11 | * all copies or substantial portions of the Software. 12 | * 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 | * THE SOFTWARE. 20 | */ 21 | package org.pkcs11.jacknji11; 22 | 23 | /** 24 | * Strategy for determining length of attribute value in C_GetAttributeValue request. 25 | * 26 | * @author Tomasz Wysocki 27 | */ 28 | public interface AttributeLengthStrategy { 29 | 30 | /** 31 | * Get expected length of attribute value in C_GetAttributeValue request. 32 | * 33 | * @param cka attribute type 34 | * @return expected length of attribute value or 0 if length should be queried. 35 | */ 36 | int getAttributeLength(long cka); 37 | 38 | /** 39 | * Implementation of {@link AttributeLengthStrategy} that is using a list of large attributes 40 | * and their maximum length as well as a default length for regular attributes. 41 | *

42 | * Use to avoid querying length of attributes for every call to C_GetAttributeValue. 43 | *

44 | * Note that implementations need to adhere strictly to PKCS#11 for this strategy 45 | * to work robustly. One detail that this implementation relies on is that PKCS#11, 46 | * base specification section 5.7.5 C_GetAttributeValue, 47 | * specifies returning CKR_BUFFER_TOO_SMALL and setting uLValueLen to 48 | * CK_UNAVAILABLE_INFORMATION, if sent a pre-allocated buffer that is too small to hold the value. 49 | * There are multuple variations of implementation errors with either the wrong return value 50 | * or failing to set uLValueLen to the correct value. 51 | * Due to these common implementation issued, using this strategy requires careful testing 52 | * with your HSM, for your use case. 53 | */ 54 | class MaxLengthStrategy implements AttributeLengthStrategy { 55 | 56 | /** 57 | * Default of 2KB has been established by following facts: 58 | * Modulus of 15Kb RSA (maximum) is around 2KB. 59 | * For 8192 bit RSA (which is a practical limit) public keys are less than 2K while certificates 60 | * are just above about 2K (with standard set of extensions), but those should be rare. There is 61 | * a balance and limits are configurable for your own use case. 62 | *

63 | * Note: CKA_VALUE is used for certificate objects, and PQC key values, so it is large value as well 64 | * which is shame since value is typically used for symmetric keys which are relatively small size. 65 | *

66 | * If set to 0 then max length strategy is not used for large attributes. 67 | */ 68 | public static final int DEFAULT_LARGE_ATTRIBUTE_LENGTH = 2048; 69 | 70 | /** 71 | * Default 72 (divisible by 8) bytes should be sufficient for most attributes including custom labels and ids 72 | * as well as EC P-521 compressed public key ( 1B tag | 66B x ) 73 | */ 74 | public static final int DEFAULT_REGULAR_ATTRIBUTE_LENGTH = 72; 75 | 76 | /** 77 | * Set of large attributes types established to potentially contain large values. 78 | */ 79 | public static final long[] DEFAULT_LARGE_ATTRIBUTES = new long[]{ 80 | CKA.MODULUS, 81 | CKA.PRIME_1, 82 | CKA.PRIME_2, 83 | CKA.EXPONENT_1, 84 | CKA.EXPONENT_2, 85 | CKA.COEFFICIENT, 86 | CKA.PRIVATE_EXPONENT, 87 | CKA.VALUE, 88 | CKA.EC_POINT, 89 | }; 90 | 91 | /** 92 | * Large attribute types. 93 | */ 94 | private final long[] largeAttributes; 95 | 96 | /** 97 | * Length for large attributes, if 0 then max length strategy is not used for large attributes. 98 | */ 99 | private final int largeAttributeLength; 100 | 101 | /** 102 | * Default length for attributes, if 0 then max length strategy is not used for regular attributes. 103 | */ 104 | private final int regularAttributeLength; 105 | 106 | /** 107 | * Constructor with default values. 108 | */ 109 | public MaxLengthStrategy() { 110 | this(DEFAULT_REGULAR_ATTRIBUTE_LENGTH, DEFAULT_LARGE_ATTRIBUTES, DEFAULT_LARGE_ATTRIBUTE_LENGTH); 111 | } 112 | 113 | /** 114 | * Constructor with custom values. 115 | * 116 | * @param regularAttributeLength length for regular attributes 117 | * @param largeAttributes set of large attributes 118 | * @param largeAttributeLength length for large attributes 119 | */ 120 | public MaxLengthStrategy(int regularAttributeLength, long[] largeAttributes, int largeAttributeLength) { 121 | this.largeAttributes = largeAttributes; 122 | this.regularAttributeLength = regularAttributeLength; 123 | this.largeAttributeLength = largeAttributeLength; 124 | } 125 | 126 | @Override 127 | public int getAttributeLength(long cka) { 128 | if (contains(largeAttributes, cka)) { 129 | return largeAttributeLength; 130 | } else { 131 | return regularAttributeLength; 132 | } 133 | } 134 | 135 | // simple check if array contains value 136 | private static boolean contains(long[] array, long value) { 137 | for (long l : array) { 138 | if (l == value) { 139 | return true; 140 | } 141 | } 142 | return false; 143 | } 144 | } 145 | 146 | /** 147 | * Strategy for querying length of attribute value in C_GetAttributeValue request 148 | * for every attribute. 149 | */ 150 | class IndefiniteLengthStrategy implements AttributeLengthStrategy { 151 | @Override 152 | public int getAttributeLength(long cka) { 153 | return 0; 154 | } 155 | } 156 | } 157 | -------------------------------------------------------------------------------- /src/main/h/rsa/pkcs11.h: -------------------------------------------------------------------------------- 1 | /* Copyright (c) OASIS Open 2016-2019. All Rights Reserved. 2 | * Distributed under the terms of the OASIS IPR Policy, 3 | * [http://www.oasis-open.org/policies-guidelines/ipr], AS-IS, WITHOUT ANY 4 | * IMPLIED OR EXPRESS WARRANTY; there is no warranty of MERCHANTABILITY, FITNESS FOR A 5 | * PARTICULAR PURPOSE or NONINFRINGEMENT of the rights of others. 6 | */ 7 | 8 | #ifndef _PKCS11_H_ 9 | #define _PKCS11_H_ 1 10 | 11 | #ifdef __cplusplus 12 | extern "C" { 13 | #endif 14 | 15 | /* Before including this file (pkcs11.h) (or pkcs11t.h by 16 | * itself), 5 platform-specific macros must be defined. These 17 | * macros are described below, and typical definitions for them 18 | * are also given. Be advised that these definitions can depend 19 | * on both the platform and the compiler used (and possibly also 20 | * on whether a Cryptoki library is linked statically or 21 | * dynamically). 22 | * 23 | * In addition to defining these 5 macros, the packing convention 24 | * for Cryptoki structures should be set. The Cryptoki 25 | * convention on packing is that structures should be 1-byte 26 | * aligned. 27 | * 28 | * If you're using Windows this might be done by using the following 29 | * preprocessor directive before including pkcs11.h or pkcs11t.h: 30 | * 31 | * #pragma pack(push, cryptoki, 1) 32 | * 33 | * and using the following preprocessor directive after including 34 | * pkcs11.h or pkcs11t.h: 35 | * 36 | * #pragma pack(pop, cryptoki) 37 | * 38 | * In a UNIX environment, you're on your own for this. You might 39 | * not need to do (or be able to do!) anything. 40 | * 41 | * 42 | * Now for the macros: 43 | * 44 | * 45 | * 1. CK_PTR: The indirection string for making a pointer to an 46 | * object. It can be used like this: 47 | * 48 | * typedef CK_BYTE CK_PTR CK_BYTE_PTR; 49 | * 50 | * If you're using windows, it might be defined by: 51 | * 52 | * #define CK_PTR * 53 | * 54 | * In a typical UNIX environment, it might be defined by: 55 | * 56 | * #define CK_PTR * 57 | * 58 | * 59 | * 2. CK_DECLARE_FUNCTION(returnType, name): A macro which makes 60 | * an importable Cryptoki library function declaration out of a 61 | * return type and a function name. It should be used in the 62 | * following fashion: 63 | * 64 | * extern CK_DECLARE_FUNCTION(CK_RV, C_Initialize)( 65 | * CK_VOID_PTR pReserved 66 | * ); 67 | * 68 | * If you're using Windows to declare a function in a Win32 cryptoki .dll, 69 | * it might be defined by: 70 | * 71 | * #define CK_DECLARE_FUNCTION(returnType, name) \ 72 | * returnType __declspec(dllimport) name 73 | * 74 | * In a UNIX environment, it might be defined by: 75 | * 76 | * #define CK_DECLARE_FUNCTION(returnType, name) \ 77 | * returnType name 78 | * 79 | * 80 | * 3. CK_DECLARE_FUNCTION_POINTER(returnType, name): A macro 81 | * which makes a Cryptoki API function pointer declaration or 82 | * function pointer type declaration out of a return type and a 83 | * function name. It should be used in the following fashion: 84 | * 85 | * // Define funcPtr to be a pointer to a Cryptoki API function 86 | * // taking arguments args and returning CK_RV. 87 | * CK_DECLARE_FUNCTION_POINTER(CK_RV, funcPtr)(args); 88 | * 89 | * or 90 | * 91 | * // Define funcPtrType to be the type of a pointer to a 92 | * // Cryptoki API function taking arguments args and returning 93 | * // CK_RV, and then define funcPtr to be a variable of type 94 | * // funcPtrType. 95 | * typedef CK_DECLARE_FUNCTION_POINTER(CK_RV, funcPtrType)(args); 96 | * funcPtrType funcPtr; 97 | * 98 | * If you're using Windows to access 99 | * functions in a Win32 Cryptoki .dll, in might be defined by: 100 | * 101 | * #define CK_DECLARE_FUNCTION_POINTER(returnType, name) \ 102 | * returnType __declspec(dllimport) (* name) 103 | * 104 | * In a UNIX environment, it might be defined by: 105 | * 106 | * #define CK_DECLARE_FUNCTION_POINTER(returnType, name) \ 107 | * returnType (* name) 108 | * 109 | * 110 | * 4. CK_CALLBACK_FUNCTION(returnType, name): A macro which makes 111 | * a function pointer type for an application callback out of 112 | * a return type for the callback and a name for the callback. 113 | * It should be used in the following fashion: 114 | * 115 | * CK_CALLBACK_FUNCTION(CK_RV, myCallback)(args); 116 | * 117 | * to declare a function pointer, myCallback, to a callback 118 | * which takes arguments args and returns a CK_RV. It can also 119 | * be used like this: 120 | * 121 | * typedef CK_CALLBACK_FUNCTION(CK_RV, myCallbackType)(args); 122 | * myCallbackType myCallback; 123 | * 124 | * If you're using Windows, it might be defined by: 125 | * 126 | * #define CK_CALLBACK_FUNCTION(returnType, name) \ 127 | * returnType (* name) 128 | * 129 | * In a UNIX environment, it might be defined by: 130 | * 131 | * #define CK_CALLBACK_FUNCTION(returnType, name) \ 132 | * returnType (* name) 133 | * 134 | * 135 | * 5. NULL_PTR: This macro is the value of a NULL pointer. 136 | * 137 | * In any ANSI/ISO C environment (and in many others as well), 138 | * this should best be defined by 139 | * 140 | * #ifndef NULL_PTR 141 | * #define NULL_PTR 0 142 | * #endif 143 | */ 144 | 145 | 146 | /* All the various Cryptoki types and #define'd values are in the 147 | * file pkcs11t.h. 148 | */ 149 | #include "pkcs11t.h" 150 | 151 | #define __PASTE(x,y) x##y 152 | 153 | 154 | /* ============================================================== 155 | * Define the "extern" form of all the entry points. 156 | * ============================================================== 157 | */ 158 | 159 | #define CK_NEED_ARG_LIST 1 160 | #define CK_PKCS11_FUNCTION_INFO(name) \ 161 | extern CK_DECLARE_FUNCTION(CK_RV, name) 162 | 163 | /* pkcs11f.h has all the information about the Cryptoki 164 | * function prototypes. 165 | */ 166 | #include "pkcs11f.h" 167 | 168 | #undef CK_NEED_ARG_LIST 169 | #undef CK_PKCS11_FUNCTION_INFO 170 | 171 | 172 | /* ============================================================== 173 | * Define the typedef form of all the entry points. That is, for 174 | * each Cryptoki function C_XXX, define a type CK_C_XXX which is 175 | * a pointer to that kind of function. 176 | * ============================================================== 177 | */ 178 | 179 | #define CK_NEED_ARG_LIST 1 180 | #define CK_PKCS11_FUNCTION_INFO(name) \ 181 | typedef CK_DECLARE_FUNCTION_POINTER(CK_RV, __PASTE(CK_,name)) 182 | 183 | /* pkcs11f.h has all the information about the Cryptoki 184 | * function prototypes. 185 | */ 186 | #include "pkcs11f.h" 187 | 188 | #undef CK_NEED_ARG_LIST 189 | #undef CK_PKCS11_FUNCTION_INFO 190 | 191 | 192 | /* ============================================================== 193 | * Define structed vector of entry points. A CK_FUNCTION_LIST 194 | * contains a CK_VERSION indicating a library's Cryptoki version 195 | * and then a whole slew of function pointers to the routines in 196 | * the library. This type was declared, but not defined, in 197 | * pkcs11t.h. 198 | * ============================================================== 199 | */ 200 | 201 | #define CK_PKCS11_FUNCTION_INFO(name) \ 202 | __PASTE(CK_,name) name; 203 | 204 | /* Create the 3.0 Function list */ 205 | struct CK_FUNCTION_LIST_3_0 { 206 | 207 | CK_VERSION version; /* Cryptoki version */ 208 | 209 | /* Pile all the function pointers into the CK_FUNCTION_LIST. */ 210 | /* pkcs11f.h has all the information about the Cryptoki 211 | * function prototypes. 212 | */ 213 | #include "pkcs11f.h" 214 | 215 | }; 216 | 217 | #define CK_PKCS11_2_0_ONLY 1 218 | 219 | /* Continue to define the old CK_FUNCTION_LIST */ 220 | struct CK_FUNCTION_LIST { 221 | 222 | CK_VERSION version; /* Cryptoki version */ 223 | 224 | /* Pile all the function pointers into the CK_FUNCTION_LIST. */ 225 | /* pkcs11f.h has all the information about the Cryptoki 226 | * function prototypes. 227 | */ 228 | #include "pkcs11f.h" 229 | 230 | }; 231 | 232 | #undef CK_PKCS11_FUNCTION_INFO 233 | #undef CK_PKCS11_2_0_ONLY 234 | 235 | 236 | #undef __PASTE 237 | 238 | #ifdef __cplusplus 239 | } 240 | #endif 241 | 242 | #endif /* _PKCS11_H_ */ 243 | 244 | -------------------------------------------------------------------------------- /src/main/java/org/pkcs11/jacknji11/Hex.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2008-2011 Joel Hockey (joel.hockey@gmail.com). All rights reserved. 3 | * 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy 5 | * of this software and associated documentation files (the "Software"), to deal 6 | * in the Software without restriction, including without limitation the rights 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | * copies of the Software, and to permit persons to whom the Software is 9 | * furnished to do so, subject to the following conditions: 10 | * 11 | * The above copyright notice and this permission notice shall be included in 12 | * all copies or substantial portions of the Software. 13 | * 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 20 | * THE SOFTWARE. 21 | */ 22 | 23 | package org.pkcs11.jacknji11; 24 | 25 | 26 | /** 27 | * Hex encoder. 28 | * @author Joel Hockey (joel.hockey@gmail.com) 29 | */ 30 | public class Hex { 31 | /** Hex digits. 0123456789abcdef */ 32 | public static final char[] HEX_DIGITS = "0123456789abcdef".toCharArray(); 33 | 34 | /** Hex string to byte lookup. -1 if non-hex, else hex nibble value. */ 35 | public static final int[] HEX_S2B = new int[256]; 36 | 37 | /** Hex byte to string lookup. returns 2-char hex encoding */ 38 | public static final char[][] HEX_B2S = new char[256][]; 39 | 40 | static { 41 | // init lookup tables 42 | // Hex S2B 43 | for (int i = 0; i < HEX_S2B.length; i++) { 44 | HEX_S2B[i] = -1; 45 | } 46 | for (int i = '0'; i <= '9'; i++) { 47 | HEX_S2B[i] = i - '0'; 48 | } 49 | for (int i = 'A'; i <= 'F'; i++) { 50 | HEX_S2B[i] = i - 'A' + 10; 51 | } 52 | for (int i = 'a'; i <= 'f'; i++) { 53 | HEX_S2B[i] = i - 'a' + 10; 54 | } 55 | 56 | // Hex B2S 57 | for (int i = 0; i < HEX_DIGITS.length; i++) { 58 | for (int j = 0; j < HEX_DIGITS.length; j++) { 59 | HEX_B2S[i * 16 + j] = new char[] { HEX_DIGITS[i], HEX_DIGITS[j] }; 60 | } 61 | } 62 | } 63 | 64 | /** 65 | * Returns lower case hex string representation of byte[]. 66 | * @param buf byte array 67 | * @return lower case hex encoded string 68 | */ 69 | public static String b2s(byte[] buf) { 70 | if (buf == null) return null; 71 | return b2s(buf, 0, buf.length); 72 | } 73 | 74 | /** 75 | * Returns hex string representation of byte[]. 76 | * @param buf byte array 77 | * @param start pos in buf to start at 78 | * @param len number of bytes to encode 79 | * @return hex encoded string 80 | */ 81 | public static String b2s(byte[] buf, int start, int len) { 82 | if (buf == null) return null; 83 | if (start < 0 || start > buf.length) { 84 | throw new IllegalArgumentException("start index must be between 0 and buf.length [" + buf.length + "]. Got value" + start); 85 | } 86 | 87 | if (len < 0 || start + len > buf.length) { 88 | throw new IllegalArgumentException("len must be between 0 and (buf.length - start) [" 89 | + buf.length + " - " + start + " = " + (buf.length - start)+ "]. Got value " + len); 90 | } 91 | 92 | char[] cbuf = new char[len * 2]; 93 | for (int i = 0; i < len; i++) { 94 | System.arraycopy(HEX_B2S[buf[start + i] & 0xff], 0, cbuf, i * 2, 2); 95 | } 96 | return new String(cbuf); 97 | } 98 | 99 | /** 100 | * Return 8 char (lower case) hex encoded 32-bit big-endian value 101 | * @param num number to encode 102 | * @return 8 char (lower case) hex encoded 32-bit big-endian value 103 | */ 104 | public static String i2s(int num) { 105 | char[] cbuf = new char[8]; 106 | // start at rhs 107 | for (int i = 3; i >= 0; i--) { 108 | System.arraycopy(Hex.HEX_B2S[num & 0xff] , 0, cbuf, i*2, 2); 109 | num >>>= 8; 110 | } 111 | return new String(cbuf); 112 | } 113 | 114 | /** 115 | * Returns byte[] from hex string. 116 | * Ignores any non-hex chars. 117 | * Pads extra 0 on end if odd number of hex chars. 118 | * @param hex hex string e.g. "01ff" 119 | * @return byte array. e.g. byte[] {1, 255} 120 | */ 121 | public static byte[] s2b(String hex) { 122 | if (hex == null) return null; 123 | 124 | byte[] buf = new byte[(hex.length() + 1) / 2]; 125 | int tmpbuf = 0; // stores nibble 126 | int bits = 0; // num of bits in tmpbuf 127 | int i = 0; // index into hex 128 | int j = 0; // index into result buf 129 | while (i < hex.length()) { 130 | int c = Hex.HEX_S2B[hex.charAt(i++) & 0xff]; 131 | // skip non-hex chars 132 | if (c < 0) { 133 | continue; 134 | } 135 | tmpbuf = tmpbuf | c; 136 | bits += 4; 137 | if (bits == 8) { 138 | buf[j++] = (byte) tmpbuf; 139 | bits = 0; 140 | } 141 | tmpbuf <<= 4; 142 | } 143 | // add extra char if exists 144 | if (bits > 0) { 145 | buf[j++] = (byte) tmpbuf; 146 | } 147 | 148 | // return correctly sized byte[] 149 | if (j == buf.length) { 150 | return buf; 151 | } else { 152 | byte[] smallbuf = new byte[j]; 153 | System.arraycopy(buf, 0, smallbuf, 0, j); 154 | return smallbuf; 155 | } 156 | } 157 | 158 | /** 159 | * Print hex dump of buf 160 | * @param buf buf 161 | * @return hex dump 162 | */ 163 | public static String dump(byte[] buf) { 164 | if (buf == null) { 165 | return null; 166 | } 167 | StringBuilder sb = new StringBuilder(); 168 | dump(sb, buf, 0, buf.length, "", 16, false); 169 | 170 | return sb.toString(); 171 | } 172 | 173 | /** 174 | * Hex dump. 175 | * @param sb stringbuilder for result 176 | * @param buf buf to dump 177 | * @param start start index 178 | * @param len length 179 | * @param indent string for indent 180 | * @param lineLen number of bytes per line (16 or 32 are good choices) 181 | * @param lineNum if true, line numbers are shown in left col 182 | */ 183 | public static void dump(StringBuilder sb, byte[] buf, int start, int len, 184 | String indent, int lineLen, boolean lineNum) { 185 | if (buf == null) { 186 | if (lineNum) { 187 | sb.append(i2s(0)).append(" - "); 188 | } 189 | sb.append(indent).append("null"); 190 | return; 191 | } 192 | 193 | char[] ascii = new char[lineLen]; 194 | int lineOffset = 0; // resets to zero for every line 195 | 196 | int i = start; // index into buf 197 | int end = start + len; 198 | 199 | while (i < end) { 200 | // put '\n' and indent to start each line 201 | if (lineOffset == 0) { 202 | if (i > start) { 203 | sb.append('\n'); 204 | } 205 | if (lineNum) { 206 | sb.append(i2s(i - start)).append(" - "); 207 | } 208 | sb.append(indent); 209 | 210 | // put a '-' every 8 chars 211 | } else if ((lineOffset & 0x7) == 0) { 212 | sb.append("- "); 213 | } 214 | 215 | // put ascii into ascii buf 216 | ascii[lineOffset++] = (buf[i] >= 32 && buf[i] <= 126) ? (char) buf[i] : '.'; 217 | // put hex into sb 218 | sb.append(HEX_B2S[(buf[(i++)] & 0xff)]).append(' '); 219 | 220 | // put ascii at end of each line 221 | if (lineOffset == ascii.length) { 222 | sb.append(" ").append(ascii); 223 | lineOffset = 0; 224 | } 225 | } 226 | 227 | if (lineOffset == 0) { 228 | return; 229 | } 230 | 231 | // put fill to line up ascii print 232 | int missingHex = ascii.length - lineOffset; 233 | // 3 for each hex, 2 for each '- ', 2 at end 234 | int fillLen = missingHex * 3 + missingHex / 8 * 2 + 2; 235 | 236 | while (fillLen-- > 0) { 237 | sb.append(' '); 238 | } 239 | sb.append(ascii, 0, lineOffset); 240 | } 241 | } 242 | -------------------------------------------------------------------------------- /src/main/java/org/pkcs11/jacknji11/jni/JNI.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2010-2011 Joel Hockey (joel.hockey@gmail.com). All rights reserved. 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy 4 | * of this software and associated documentation files (the "Software"), to deal 5 | * in the Software without restriction, including without limitation the rights 6 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | * copies of the Software, and to permit persons to whom the Software is 8 | * furnished to do so, subject to the following conditions: 9 | * 10 | * The above copyright notice and this permission notice shall be included in 11 | * all copies or substantial portions of the Software. 12 | * 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 | * THE SOFTWARE. 20 | */ 21 | 22 | package org.pkcs11.jacknji11.jni; 23 | 24 | import org.pkcs11.jacknji11.CKA; 25 | import org.pkcs11.jacknji11.CKM; 26 | import org.pkcs11.jacknji11.CK_C_INITIALIZE_ARGS; 27 | import org.pkcs11.jacknji11.CK_INFO; 28 | import org.pkcs11.jacknji11.CK_MECHANISM_INFO; 29 | import org.pkcs11.jacknji11.CK_NOTIFY; 30 | import org.pkcs11.jacknji11.CK_SESSION_INFO; 31 | import org.pkcs11.jacknji11.CK_SLOT_INFO; 32 | import org.pkcs11.jacknji11.CK_TOKEN_INFO; 33 | import org.pkcs11.jacknji11.LongRef; 34 | import org.pkcs11.jacknji11.NativePointer; 35 | import org.pkcs11.jacknji11.NativeProvider; 36 | import org.pkcs11.jacknji11.ULong; 37 | 38 | public class JNI implements NativeProvider { 39 | static { 40 | System.loadLibrary("jacknji11"); 41 | init(); 42 | ULong.ULONG_SIZE = ULongSize() == 4 ? ULong.ULongSize.ULONG4 : ULong.ULongSize.ULONG8; 43 | } 44 | 45 | public static native void init(); 46 | public static native int ULongSize(); 47 | public native long C_Initialize(CK_C_INITIALIZE_ARGS pInitArgs); 48 | public native long C_Finalize(NativePointer pReserved); 49 | public native long C_GetInfo(CK_INFO pInfo); 50 | public native long C_GetSlotList(boolean tokenPresent, long[] pSlotList, LongRef pulCount); 51 | public native long C_GetSlotInfo(long slotID, CK_SLOT_INFO pInfo); 52 | public native long C_GetTokenInfo(long slotID, CK_TOKEN_INFO pInfo); 53 | public native long C_WaitForSlotEvent(long flags, LongRef pSlot, NativePointer pReserved); 54 | public native long C_GetMechanismList(long slotID, long[] pMechanismList, LongRef pulCount); 55 | public native long C_GetMechanismInfo(long slotID, long type, CK_MECHANISM_INFO pInfo); 56 | public native long C_InitToken(long slotID, byte[] pPin, long ulPinLen, byte[] pLabel32); 57 | public native long C_InitPIN(long hSession, byte[] pPin, long ulPinLen); 58 | public native long C_SetPIN(long hSession, byte[] pOldPin, long ulOldLen, byte[] pNewPin, long ulNewLen); 59 | public native long C_OpenSession(long slotID, long flags, NativePointer application, CK_NOTIFY notify, LongRef phSession); 60 | public native long C_CloseSession(long hSession); 61 | public native long C_CloseAllSessions(long slotID); 62 | public native long C_GetSessionInfo(long hSession, CK_SESSION_INFO pInfo); 63 | public native long C_GetOperationState(long hSession, byte[] pOperationState, LongRef pulOperationStateLen); 64 | public native long C_SetOperationState(long hSession, byte[] pOperationState, long ulOperationStateLen, long hEncryptionKey, long hAuthenticationKey); 65 | public native long C_Login(long hSession, long userType, byte[] pPin, long ulPinLen); 66 | public native long C_Logout(long hSession); 67 | public native long C_CreateObject(long hSession, CKA[] pTemplate, long ulCount, LongRef phObject); 68 | public native long C_CopyObject(long hSession, long hObject, CKA[] pTemplate, long ulCount, LongRef phNewObject); 69 | public native long C_DestroyObject(long hSession, long hObject); 70 | public native long C_GetObjectSize(long hSession, long hObject, LongRef pulSize); 71 | public native long C_GetAttributeValue(long hSession, long hObject, CKA[] pTemplate, long ulCount); 72 | public native long C_SetAttributeValue(long hSession, long hObject, CKA[] pTemplate, long ulCount); 73 | public native long C_FindObjectsInit(long hSession, CKA[] pTemplate, long ulCount); 74 | public native long C_FindObjects(long hSession, long[] phObject, long ulMaxObjectCount, LongRef pulObjectCount); 75 | public native long C_FindObjectsFinal(long hSession); 76 | public native long C_EncryptInit(long hSession, CKM pMechanism, long hKey); 77 | public native long C_Encrypt(long hSession, byte[] pData, long ulDataLen, byte[] pEncryptedData, LongRef pulEncryptedDataLen); 78 | public native long C_EncryptUpdate(long hSession, byte[] pPart, long ulPartLen, byte[] pEncryptedPart, LongRef pulEncryptedPartLen); 79 | public native long C_EncryptFinal(long hSession, byte[] pLastEncryptedPart, LongRef pulLastEncryptedPartLen); 80 | public native long C_DecryptInit(long hSession, CKM pMechanism, long hKey); 81 | public native long C_Decrypt(long hSession, byte[] pEncryptedData, long ulEncryptedDataLen, byte[] pData, LongRef pulDataLen); 82 | public native long C_DecryptUpdate(long hSession, byte[] pEncryptedPart, long ulEncryptedPartLen, byte[] pData, LongRef pulDataLen); 83 | public native long C_DecryptFinal(long hSession, byte[] pLastPart, LongRef pulLastPartLen); 84 | public native long C_DigestInit(long hSession, CKM pMechanism); 85 | public native long C_Digest(long hSession, byte[] pData, long ulDataLen, byte[] pDigest, LongRef pulDigestLen); 86 | public native long C_DigestUpdate(long hSession, byte[] pPart, long ulPartLen); 87 | public native long C_DigestKey(long hSession, long hKey); 88 | public native long C_DigestFinal(long hSession, byte[] pDigest, LongRef pulDigestLen); 89 | public native long C_SignInit(long hSession, CKM pMechanism, long hKey); 90 | public native long C_Sign(long hSession, byte[] pData, long ulDataLen, byte[] pSignature, LongRef pulSignatureLen); 91 | public native long C_SignUpdate(long hSession, byte[] pPart, long ulPartLen); 92 | public native long C_SignFinal(long hSession, byte[] pSignature, LongRef pulSignatureLen); 93 | public native long C_SignRecoverInit(long hSession, CKM pMechanism, long hKey); 94 | public native long C_SignRecover(long hSession, byte[] pData, long ulDataLen, byte[] pSignature, LongRef pulSignatureLen); 95 | public native long C_VerifyInit(long hSession, CKM pMechanism, long hKey); 96 | public native long C_Verify(long hSession, byte[] pData, long ulDataLen, byte[] pSignature, long ulSignatureLen); 97 | public native long C_VerifyUpdate(long hSession, byte[] pPart, long ulPartLen); 98 | public native long C_VerifyFinal(long hSession, byte[] pSignature, long ulSignatureLen); 99 | public native long C_VerifyRecoverInit(long hSession, CKM pMechanism, long hKey); 100 | public native long C_VerifyRecover(long hSession, byte[] pSignature, long ulSignatureLen, byte[] pData, LongRef pulDataLen); 101 | public native long C_DigestEncryptUpdate(long hSession, byte[] pPart, long ulPartLen, byte[] pEncryptedPart, LongRef pulEncryptedPartLen); 102 | public native long C_DecryptDigestUpdate(long hSession, byte[] pEncryptedPart, long ulEncryptedPartLen, byte[] pPart, LongRef pulPartLen); 103 | public native long C_SignEncryptUpdate(long hSession, byte[] pPart, long ulPartLen, byte[] pEncryptedPart, LongRef pulEncryptedPartLen); 104 | public native long C_DecryptVerifyUpdate(long hSession, byte[] pEncryptedPart, long ulEncryptedPartLen, byte[] pPart, LongRef pulPartLen); 105 | public native long C_GenerateKey(long hSession, CKM pMechanism, CKA[] pTemplate, long ulCount, LongRef phKey); 106 | public native long C_GenerateKeyPair(long hSession, CKM pMechanism, CKA[] pPublicKeyTemplate, long ulPublicKeyAttributeCount, CKA[] pPrivateKeyTemplate, long ulPrivateKeyAttributeCount, LongRef phPublicKey, LongRef phPrivateKey); 107 | public native long C_WrapKey(long hSession, CKM pMechanism, long hWrappingKey, long hKey, byte[] pWrappedKey, LongRef pulWrappedKeyLen); 108 | public native long C_UnwrapKey(long hSession, CKM pMechanism, long hUnwrappingKey, byte[] pWrappedKey, long ulWrappedKeyLen, CKA[] pTemplate, long ulAttributeCount, LongRef phKey); 109 | public native long C_DeriveKey(long hSession, CKM pMechanism, long hBaseKey, CKA[] pTemplate, long ulAttributeCount, LongRef phKey); 110 | public native long C_SeedRandom(long hSession, byte[] pSeed, long ulSeedLen); 111 | public native long C_GenerateRandom(long hSession, byte[] pRandom, long ulRandomLen); 112 | public native long C_GetFunctionStatus(long hSession); 113 | public native long C_CancelFunction(long hSession); 114 | } 115 | -------------------------------------------------------------------------------- /src/main/java/org/pkcs11/jacknji11/jna/JNANativeI.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2010-2011 Joel Hockey (joel.hockey@gmail.com). All rights reserved. 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy 4 | * of this software and associated documentation files (the "Software"), to deal 5 | * in the Software without restriction, including without limitation the rights 6 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | * copies of the Software, and to permit persons to whom the Software is 8 | * furnished to do so, subject to the following conditions: 9 | * 10 | * The above copyright notice and this permission notice shall be included in 11 | * all copies or substantial portions of the Software. 12 | * 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 | * THE SOFTWARE. 20 | */ 21 | 22 | package org.pkcs11.jacknji11.jna; 23 | 24 | import com.sun.jna.NativeLong; 25 | import com.sun.jna.Pointer; 26 | import com.sun.jna.ptr.NativeLongByReference; 27 | 28 | /** 29 | * JNA Native class with direct mapped methods. 30 | * @author Joel Hockey (joel.hockey@gmail.com) 31 | */ 32 | public interface JNANativeI extends com.sun.jna.Library { 33 | 34 | public int C_Initialize(JNA_CK_C_INITIALIZE_ARGS pInitArgs); 35 | public int C_Finalize(Pointer pReserved); 36 | public int C_GetInfo(JNA_CK_INFO pInfo); 37 | public int C_GetSlotList(byte tokenPresent, LongArray pSlotList, NativeLongByReference pulCount); 38 | public int C_GetSlotInfo(NativeLong slotID, JNA_CK_SLOT_INFO pInfo); 39 | public int C_GetTokenInfo(NativeLong slotID, JNA_CK_TOKEN_INFO pInfo); 40 | public int C_WaitForSlotEvent(NativeLong flags, NativeLongByReference pSlot, Pointer pReserved); 41 | public int C_GetMechanismList(NativeLong slotID, LongArray pMechanismList, NativeLongByReference pulCount); 42 | public int C_GetMechanismInfo(NativeLong slotID, NativeLong type, JNA_CK_MECHANISM_INFO pInfo); 43 | public int C_InitToken(NativeLong slotID, byte[] pPin, NativeLong ulPinLen, byte[] pLabel32); 44 | public int C_InitPIN(NativeLong hSession, byte[] pPin, NativeLong ulPinLen); 45 | public int C_SetPIN(NativeLong hSession, byte[] pOldPin, NativeLong ulOldLen, byte[] pNewPin, NativeLong ulNewLen); 46 | public int C_OpenSession(NativeLong slotID, NativeLong flags, Pointer application, JNA_CK_NOTIFY notify, NativeLongByReference phSession); 47 | public int C_CloseSession(NativeLong hSession); 48 | public int C_CloseAllSessions(NativeLong slotID); 49 | public int C_GetSessionInfo(NativeLong hSession, JNA_CK_SESSION_INFO pInfo); 50 | public int C_GetOperationState(NativeLong hSession, byte[] pOperationState, NativeLongByReference pulOperationStateLen); 51 | public int C_SetOperationState(NativeLong hSession, byte[] pOperationState, NativeLong ulOperationStateLen, NativeLong hEncryptionKey, NativeLong hAuthenticationKey); 52 | public int C_Login(NativeLong hSession, NativeLong userType, byte[] pPin, NativeLong ulPinLen); 53 | public int C_Logout(NativeLong hSession); 54 | public int C_CreateObject(NativeLong hSession, Template pTemplate, NativeLong ulCount, NativeLongByReference phObject); 55 | public int C_CopyObject(NativeLong hSession, NativeLong hObject, Template pTemplate, NativeLong ulCount, NativeLongByReference phNewObject); 56 | public int C_DestroyObject(NativeLong hSession, NativeLong hObject); 57 | public int C_GetObjectSize(NativeLong hSession, NativeLong hObject, NativeLongByReference pulSize); 58 | public int C_GetAttributeValue(NativeLong hSession, NativeLong hObject, Template pTemplate, NativeLong ulCount); 59 | public int C_SetAttributeValue(NativeLong hSession, NativeLong hObject, Template pTemplate, NativeLong ulCount); 60 | public int C_FindObjectsInit(NativeLong hSession, Template pTemplate, NativeLong ulCount); 61 | public int C_FindObjects(NativeLong hSession, LongArray phObject, NativeLong ulMaxObjectCount, NativeLongByReference pulObjectCount); 62 | public int C_FindObjectsFinal(NativeLong hSession); 63 | public int C_EncryptInit(NativeLong hSession, JNA_CKM pMechanism, NativeLong hKey); 64 | public int C_Encrypt(NativeLong hSession, byte[] pData, NativeLong ulDataLen, byte[] pEncryptedData, NativeLongByReference pulEncryptedDataLen); 65 | public int C_EncryptUpdate(NativeLong hSession, byte[] pPart, NativeLong ulPartLen, byte[] pEncryptedPart, NativeLongByReference pulEncryptedPartLen); 66 | public int C_EncryptFinal(NativeLong hSession, byte[] pLastEncryptedPart, NativeLongByReference pulLastEncryptedPartLen); 67 | public int C_DecryptInit(NativeLong hSession, JNA_CKM pMechanism, NativeLong hKey); 68 | public int C_Decrypt(NativeLong hSession, byte[] pEncryptedData, NativeLong ulEncryptedDataLen, byte[] pData, NativeLongByReference pulDataLen); 69 | public int C_DecryptUpdate(NativeLong hSession, byte[] pEncryptedPart, NativeLong ulEncryptedPartLen, byte[] pData, NativeLongByReference pulDataLen); 70 | public int C_DecryptFinal(NativeLong hSession, byte[] pLastPart, NativeLongByReference pulLastPartLen); 71 | public int C_DigestInit(NativeLong hSession, JNA_CKM pMechanism); 72 | public int C_Digest(NativeLong hSession, byte[] pData, NativeLong ulDataLen, byte[] pDigest, NativeLongByReference pulDigestLen); 73 | public int C_DigestUpdate(NativeLong hSession, byte[] pPart, NativeLong ulPartLen); 74 | public int C_DigestKey(NativeLong hSession, NativeLong hKey); 75 | public int C_DigestFinal(NativeLong hSession, byte[] pDigest, NativeLongByReference pulDigestLen); 76 | public int C_SignInit(NativeLong hSession, JNA_CKM pMechanism, NativeLong hKey); 77 | public int C_Sign(NativeLong hSession, byte[] pData, NativeLong ulDataLen, byte[] pSignature, NativeLongByReference pulSignatureLen); 78 | public int C_SignUpdate(NativeLong hSession, byte[] pPart, NativeLong ulPartLen); 79 | public int C_SignFinal(NativeLong hSession, byte[] pSignature, NativeLongByReference pulSignatureLen); 80 | public int C_SignRecoverInit(NativeLong hSession, JNA_CKM pMechanism, NativeLong hKey); 81 | public int C_SignRecover(NativeLong hSession, byte[] pData, NativeLong ulDataLen, byte[] pSignature, NativeLongByReference pulSignatureLen); 82 | public int C_VerifyInit(NativeLong hSession, JNA_CKM pMechanism, NativeLong hKey); 83 | public int C_Verify(NativeLong hSession, byte[] pData, NativeLong ulDataLen, byte[] pSignature, NativeLong ulSignatureLen); 84 | public int C_VerifyUpdate(NativeLong hSession, byte[] pPart, NativeLong ulPartLen); 85 | public int C_VerifyFinal(NativeLong hSession, byte[] pSignature, NativeLong ulSignatureLen); 86 | public int C_VerifyRecoverInit(NativeLong hSession, JNA_CKM pMechanism, NativeLong hKey); 87 | public int C_VerifyRecover(NativeLong hSession, byte[] pSignature, NativeLong ulSignatureLen, byte[] pData, NativeLongByReference pulDataLen); 88 | public int C_DigestEncryptUpdate(NativeLong hSession, byte[] pPart, NativeLong ulPartLen, byte[] pEncryptedPart, NativeLongByReference pulEncryptedPartLen); 89 | public int C_DecryptDigestUpdate(NativeLong hSession, byte[] pEncryptedPart, NativeLong ulEncryptedPartLen, byte[] pPart, NativeLongByReference pulPartLen); 90 | public int C_SignEncryptUpdate(NativeLong hSession, byte[] pPart, NativeLong ulPartLen, byte[] pEncryptedPart, NativeLongByReference pulEncryptedPartLen); 91 | public int C_DecryptVerifyUpdate(NativeLong hSession, byte[] pEncryptedPart, NativeLong ulEncryptedPartLen, byte[] pPart, NativeLongByReference pulPartLen); 92 | public int C_GenerateKey(NativeLong hSession, JNA_CKM pMechanism, Template pTemplate, NativeLong ulCount, NativeLongByReference phKey); 93 | public int C_GenerateKeyPair(NativeLong hSession, JNA_CKM pMechanism, Template pPublicKeyTemplate, NativeLong ulPublicKeyAttributeCount, Template pPrivateKeyTemplate, NativeLong ulPrivateKeyAttributeCount, NativeLongByReference phPublicKey, NativeLongByReference phPrivateKey); 94 | public int C_WrapKey(NativeLong hSession, JNA_CKM pMechanism, NativeLong hWrappingKey, NativeLong hKey, byte[] pWrappedKey, NativeLongByReference pulWrappedKeyLen); 95 | public int C_UnwrapKey(NativeLong hSession, JNA_CKM pMechanism, NativeLong hUnwrappingKey, byte[] pWrappedKey, NativeLong ulWrappedKeyLen, Template pTemplate, NativeLong ulAttributeCount, NativeLongByReference phKey); 96 | public int C_DeriveKey(NativeLong hSession, JNA_CKM pMechanism, NativeLong hBaseKey, Template pTemplate, NativeLong ulAttributeCount, NativeLongByReference phKey); 97 | public int C_SeedRandom(NativeLong hSession, byte[] pSeed, NativeLong ulSeedLen); 98 | public int C_GenerateRandom(NativeLong hSession, byte[] pRandom, NativeLong ulRandomLen); 99 | public int C_GetFunctionStatus(NativeLong hSession); 100 | public int C_CancelFunction(NativeLong hSession); 101 | } 102 | -------------------------------------------------------------------------------- /src/main/java/org/pkcs11/jacknji11/jffi/JFFINative.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2010-2011 Joel Hockey (joel.hockey@gmail.com). All rights reserved. 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy 4 | * of this software and associated documentation files (the "Software"), to deal 5 | * in the Software without restriction, including without limitation the rights 6 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | * copies of the Software, and to permit persons to whom the Software is 8 | * furnished to do so, subject to the following conditions: 9 | * 10 | * The above copyright notice and this permission notice shall be included in 11 | * all copies or substantial portions of the Software. 12 | * 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 | * THE SOFTWARE. 20 | */ 21 | 22 | package org.pkcs11.jacknji11.jffi; 23 | 24 | import jnr.ffi.Address; 25 | import jnr.ffi.Library; 26 | import jnr.ffi.Pointer; 27 | import jnr.ffi.annotations.In; 28 | import jnr.ffi.annotations.Out; 29 | import jnr.ffi.byref.NativeLongByReference; 30 | 31 | /** 32 | * JFFI Native class. 33 | * @author Joel Hockey (joel.hockey@gmail.com) 34 | */ 35 | public class JFFINative { 36 | static { 37 | Library.loadLibrary(JFFINative.class, "cryptoki"); 38 | } 39 | 40 | public static native int C_Initialize(@In JFFI_CK_C_INITIALIZE_ARGS pInitArgs); 41 | public static native int C_Finalize(@In Address pReserved); 42 | public static native int C_GetInfo(@Out JFFI_CK_INFO pInfo); 43 | public static native int C_GetSlotList(@In boolean tokenPresent, @Out long[] pSlotList, @In @Out NativeLongByReference pulCount); 44 | public static native int C_GetSlotInfo(@In long slotID, @Out JFFI_CK_SLOT_INFO pInfo); 45 | public static native int C_GetTokenInfo(@In long slotID, @Out JFFI_CK_TOKEN_INFO pInfo); 46 | public static native int C_WaitForSlotEvent(@In long flags, @Out NativeLongByReference pSlot, @In Address pReserved); 47 | public static native int C_GetMechanismList(@In long slotID, @Out long[] pMechanismList, @In @Out NativeLongByReference pulCount); 48 | public static native int C_GetMechanismInfo(@In long slotID, @In long type, @Out JFFI_CK_MECHANISM_INFO pInfo); 49 | public static native int C_InitToken(@In long slotID, @In byte[] pPin, @In long ulPinLen, @In byte[] pLabel32); 50 | public static native int C_InitPIN(@In long hSession, @In byte[] pPin, @In long ulPinLen); 51 | public static native int C_SetPIN(@In long hSession, @In byte[] pOldPin, @In long ulOldLen, @In byte[] pNewPin, @In long ulNewLen); 52 | public static native int C_OpenSession(@In long slotID, @In long flags, @In Address application, @In Address notify, @In @Out NativeLongByReference phSession); 53 | public static native int C_CloseSession(@In long hSession); 54 | public static native int C_CloseAllSessions(@In long slotID); 55 | public static native int C_GetSessionInfo(@In long hSession, @Out JFFI_CK_SESSION_INFO pInfo); 56 | public static native int C_GetOperationState(@In long hSession, @In byte[] pOperationState, @In @Out NativeLongByReference pulOperationStateLen); 57 | public static native int C_SetOperationState(@In long hSession, @In byte[] pOperationState, @In long ulOperationStateLen, @In long hEncryptionKey, @In long hAuthenticationKey); 58 | public static native int C_Login(@In long hSession, @In long userType, @In byte[] pPin, @In long ulPinLen); 59 | public static native int C_Logout(@In long hSession); 60 | public static native int C_CreateObject(@In long hSession, @In Pointer pTemplate, @In long ulCount, @In @Out NativeLongByReference phObject); 61 | public static native int C_CopyObject(@In long hSession, long hObject, @In Pointer pTemplate, @In long ulCount, @In @Out NativeLongByReference phNewObject); 62 | public static native int C_DestroyObject(@In long hSession, @In long hObject); 63 | public static native int C_GetObjectSize(@In long hSession, @In long hObject, @In @Out NativeLongByReference pulSize); 64 | public static native int C_GetAttributeValue(@In long hSession, @In long hObject, @In @Out Pointer pTemplate, @In long ulCount); 65 | public static native int C_SetAttributeValue(@In long hSession, @In long hObject, @In Pointer pTemplate, @In long ulCount); 66 | public static native int C_FindObjectsInit(@In long hSession, @In Pointer pTemplate, @In long ulCount); 67 | public static native int C_FindObjects(@In long hSession, @Out long[] phObject, @In long ulMaxObjectCount, @In @Out NativeLongByReference pulObjectCount); 68 | public static native int C_FindObjectsFinal(@In long hSession); 69 | public static native int C_EncryptInit(@In long hSession, @In JFFI_CKM pMechanism, @In long hKey); 70 | public static native int C_Encrypt(@In long hSession, @In byte[] pData, @In long ulDataLen, @In byte[] pEncryptedData, @In @Out NativeLongByReference pulEncryptedDataLen); 71 | public static native int C_EncryptUpdate(@In long hSession, @In byte[] pPart, @In long ulPartLen, @Out byte[] pEncryptedPart, @In @Out NativeLongByReference pulEncryptedPartLen); 72 | public static native int C_EncryptFinal(@In long hSession, @Out byte[] pLastEncryptedPart, @In @Out NativeLongByReference pulLastEncryptedPartLen); 73 | public static native int C_DecryptInit(@In long hSession, @In JFFI_CKM pMechanism, @In long hKey); 74 | public static native int C_Decrypt(@In long hSession, @In byte[] pEncryptedData, @In long ulEncryptedDataLen, @In byte[] pData, @In @Out NativeLongByReference pulDataLen); 75 | public static native int C_DecryptUpdate(@In long hSession, @In byte[] pEncryptedPart, @In long ulEncryptedPartLen, @Out byte[] pData, @In @Out NativeLongByReference pulDataLen); 76 | public static native int C_DecryptFinal(@In long hSession, @Out byte[] pLastPart, @In @Out NativeLongByReference pulLastPartLen); 77 | public static native int C_DigestInit(@In long hSession, @In JFFI_CKM pMechanism); 78 | public static native int C_Digest(@In long hSession, @In byte[] pData, @In long ulDataLen, @Out byte[] pDigest, @In @Out NativeLongByReference pulDigestLen); 79 | public static native int C_DigestUpdate(@In long hSession, @In byte[] pPart, @In long ulPartLen); 80 | public static native int C_DigestKey(@In long hSession, @In long hKey); 81 | public static native int C_DigestFinal(@In long hSession, @Out byte[] pDigest, @In @Out NativeLongByReference pulDigestLen); 82 | public static native int C_SignInit(@In long hSession, @In JFFI_CKM pMechanism, @In long hKey); 83 | public static native int C_Sign(@In long hSession, @In byte[] pData, @In long ulDataLen, @Out byte[] pSignature, @In @Out NativeLongByReference pulSignatureLen); 84 | public static native int C_SignUpdate(@In long hSession, @In byte[] pPart, @In long ulPartLen); 85 | public static native int C_SignFinal(@In long hSession, @Out byte[] pSignature, @In @Out NativeLongByReference pulSignatureLen); 86 | public static native int C_SignRecoverInit(@In long hSession, @In JFFI_CKM pMechanism, @In long hKey); 87 | public static native int C_SignRecover(@In long hSession, @In byte[] pData, @In long ulDataLen, @Out byte[] pSignature, @In @Out NativeLongByReference pulSignatureLen); 88 | public static native int C_VerifyInit(@In long hSession, @In JFFI_CKM pMechanism, @In long hKey); 89 | public static native int C_Verify(@In long hSession, @In byte[] pData, @In long ulDataLen, @In byte[] pSignature, @In long ulSignatureLen); 90 | public static native int C_VerifyUpdate(@In long hSession, @In byte[] pPart, @In long ulPartLen); 91 | public static native int C_VerifyFinal(@In long hSession, @In byte[] pSignature, @In long ulSignatureLen); 92 | public static native int C_VerifyRecoverInit(@In long hSession, @In JFFI_CKM pMechanism, @In long hKey); 93 | public static native int C_VerifyRecover(@In long hSession, @In byte[] pSignature, @In long ulSignatureLen, @In byte[] pData, @In @Out NativeLongByReference pulDataLen); 94 | public static native int C_DigestEncryptUpdate(@In long hSession, @In byte[] pPart, @In long ulPartLen, @Out byte[] pEncryptedPart, @In @Out NativeLongByReference pulEncryptedPartLen); 95 | public static native int C_DecryptDigestUpdate(@In long hSession, @In byte[] pEncryptedPart, @In long ulEncryptedPartLen, @Out byte[] pPart, @In @Out NativeLongByReference pulPartLen); 96 | public static native int C_SignEncryptUpdate(@In long hSession, @In byte[] pPart, @In long ulPartLen, @Out byte[] pEncryptedPart, @In @Out NativeLongByReference pulEncryptedPartLen); 97 | public static native int C_DecryptVerifyUpdate(@In long hSession, @In byte[] pEncryptedPart, @In long ulEncryptedPartLen, @Out byte[] pPart, @In @Out NativeLongByReference pulPartLen); 98 | public static native int C_GenerateKey(@In long hSession, @In JFFI_CKM pMechanism, @In Pointer pTemplate, @In long ulCount, @Out NativeLongByReference phKey); 99 | public static native int C_GenerateKeyPair(@In long hSession, @In JFFI_CKM pMechanism, @In Pointer pPublicKeyTemplate, @In long ulPublicKeyAttributeCount, @In Pointer pPrivateKeyTemplate, @In long ulPrivateKeyAttributeCount, @Out NativeLongByReference phPublicKey, @Out NativeLongByReference phPrivateKey); 100 | public static native int C_WrapKey(@In long hSession, @In JFFI_CKM pMechanism, @In long hWrappingKey, @In long hKey, @Out byte[] pWrappedKey, @In @Out NativeLongByReference pulWrappedKeyLen); 101 | public static native int C_UnwrapKey(@In long hSession, @In JFFI_CKM pMechanism, @In long hUnwrappingKey, @In byte[] pWrappedKey, @In long ulWrappedKeyLen, @In Pointer pTemplate, @In long ulAttributeCount, @Out NativeLongByReference phKey); 102 | public static native int C_DeriveKey(@In long hSession, @In JFFI_CKM pMechanism, @In long hBaseKey, @In Pointer pTemplate, @In long ulAttributeCount, @Out NativeLongByReference phKey); 103 | public static native int C_SeedRandom(@In long hSession, @In byte[] pSeed, @In long ulSeedLen); 104 | public static native int C_GenerateRandom(@In long hSession, @In byte[] pRandomData, @In long ulRandomLen); 105 | public static native int C_GetFunctionStatus(@In long hSession); 106 | public static native int C_CancelFunction(@In long hSession); 107 | } 108 | -------------------------------------------------------------------------------- /src/test/java/org/pkcs11/jacknji11/CKATest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2010-2011 Joel Hockey (joel.hockey@gmail.com). All rights reserved. 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy 4 | * of this software and associated documentation files (the "Software"), to deal 5 | * in the Software without restriction, including without limitation the rights 6 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | * copies of the Software, and to permit persons to whom the Software is 8 | * furnished to do so, subject to the following conditions: 9 | * 10 | * The above copyright notice and this permission notice shall be included in 11 | * all copies or substantial portions of the Software. 12 | * 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 | * THE SOFTWARE. 20 | */ 21 | package org.pkcs11.jacknji11; 22 | 23 | import org.junit.Test; 24 | 25 | import java.math.BigInteger; 26 | import java.nio.charset.StandardCharsets; 27 | 28 | import static org.junit.Assert.assertArrayEquals; 29 | import static org.junit.Assert.assertEquals; 30 | import static org.junit.Assert.assertFalse; 31 | import static org.junit.Assert.assertNotNull; 32 | import static org.junit.Assert.assertNull; 33 | import static org.junit.Assert.assertTrue; 34 | 35 | public class CKATest { 36 | 37 | 38 | @Test 39 | public void testAllocate() { 40 | CKA cka = CKA.allocate(CKA.MODULUS, 100); 41 | 42 | // value is null because the buffer is not really set with anything 43 | assertNull(cka.getValue()); 44 | assertEquals(100, cka.ulValueLen); 45 | // however the pValue points at a buffer 46 | assertNotNull(cka.pValue); 47 | 48 | // it can also be checked by hasValue 49 | assertFalse(cka.hasValue()); 50 | 51 | // to string should not display any value, however it shall display its allocated state 52 | assertEquals(cka.toString(), "type=0x00000120{MODULUS} ALLOCATED [100B]"); 53 | 54 | // now let's emulate setting the value (this happens when Cryptoki native structure is applied back onto CKA) 55 | cka.set(); 56 | 57 | // value is no longer null 58 | assertNotNull(cka.getValue()); 59 | 60 | // and value is set therfeore hasValue returns true 61 | assertTrue(cka.hasValue()); 62 | 63 | // to string should now display the value and the BUFFERED state shall not be displayed 64 | assertTrue(cka.toString().contains("type=0x00000120{MODULUS} [100B]")); 65 | 66 | } 67 | 68 | @Test 69 | public void testIndefinite() { 70 | CKA cka = CKA.indefinite(CKA.VALUE); 71 | 72 | // the CKA has no value 73 | assertNull(cka.getValue()); 74 | // it also does not have a buffer 75 | assertNull(cka.pValue); 76 | // and the length is 0 77 | assertEquals(0, cka.ulValueLen); 78 | 79 | // it can also be checked by hasValue 80 | assertFalse(cka.hasValue()); 81 | 82 | // to string should not display any value, however it shall display its indefinite state 83 | assertEquals(cka.toString(), "type=0x00000011{VALUE} INDEFINITE"); 84 | 85 | // let's emulate setting the value (this happens when Cryptoki native structure is applied back onto CKA) 86 | // in this case as buffer is null, only the length is set 87 | cka.set(); 88 | cka.ulValueLen = 100; 89 | 90 | // value is still null 91 | assertNull(cka.getValue()); 92 | 93 | // but the length is set 94 | assertEquals(100, cka.ulValueLen); 95 | 96 | // however hasValue still returns false (as there is actually no value set) 97 | assertFalse(cka.hasValue()); 98 | 99 | // to string should now display the length and the INDEFINITE state shall not be displayed 100 | assertEquals(cka.toString(), cka.toString(), "type=0x00000011{VALUE} DEFINITE [100B]"); 101 | 102 | } 103 | 104 | @Test 105 | public void testEmpty() { 106 | // lets create the empty attribute 107 | CKA cka = new CKA(CKA.VALUE, null); 108 | 109 | // the CKA has no value 110 | assertNull(cka.getValue()); 111 | // it also does not have a buffer 112 | assertNull(cka.pValue); 113 | // and the length is 0 114 | assertEquals(0, cka.ulValueLen); 115 | 116 | // it can also be checked by hasValue 117 | assertFalse(cka.hasValue()); 118 | 119 | // to string should not display any value, however it shall display its empty state 120 | assertEquals(cka.toString(), "type=0x00000011{VALUE} EMPTY"); 121 | 122 | } 123 | 124 | @Test 125 | public void testEmptyFromQuery() { 126 | // lets create the buffered attribute 127 | CKA cka = CKA.allocate(CKA.VALUE, 100); 128 | 129 | // now let's set the empty state 130 | cka.ulValueLen = 0; 131 | cka.set(); 132 | 133 | // the CKA has no value 134 | assertNull(cka.getValue()); 135 | 136 | // and the length is 0 137 | assertEquals(0, cka.ulValueLen); 138 | 139 | // it can also be checked by hasValue 140 | assertFalse(cka.hasValue()); 141 | 142 | // to string should not display any value, however it shall display its empty state 143 | assertEquals(cka.toString(), "type=0x00000011{VALUE} EMPTY"); 144 | 145 | } 146 | 147 | @Test 148 | public void testBool() { 149 | CKA cka = new CKA(CKA.SENSITIVE, true); 150 | 151 | // we check the encoding 152 | byte[] bytes = cka.getValue(); 153 | assertArrayEquals(new byte[]{1}, bytes); 154 | 155 | // and the value 156 | assertTrue(cka.getValueBool()); 157 | 158 | // now let's create this attribute from the encoding of value 159 | CKA cka2 = new CKA(CKA.SENSITIVE, bytes); 160 | 161 | // and check the value 162 | assertTrue(cka2.getValueBool()); 163 | 164 | // and the encoded bytes (should be the same) 165 | assertArrayEquals(new byte[]{1}, cka2.getValue()); 166 | 167 | // also while we have the object let's check the toString 168 | assertTrue(cka.toString(), cka.toString().contains("type=0x00000103{SENSITIVE} [1B] value=TRUE")); 169 | 170 | assertEquals(cka, cka2); 171 | assertEquals(cka.hashCode(), cka2.hashCode()); 172 | } 173 | 174 | @Test 175 | public void testULong() { 176 | CKA cka = new CKA(CKA.VALUE_LEN, 100L); 177 | byte[] expectedBytes = ULong.ulong2b(100L); 178 | 179 | // we check the encoding 180 | byte[] bytes = cka.getValue(); 181 | assertArrayEquals(expectedBytes, bytes); 182 | 183 | // and the value 184 | assertEquals(100L, cka.getValueLong().longValue()); 185 | 186 | // now let's create this attribute from the encoding of value 187 | CKA cka2 = new CKA(CKA.VALUE_LEN, bytes); 188 | 189 | // and check the value 190 | assertEquals(100L, cka2.getValueLong().longValue()); 191 | 192 | // and the encoded bytes (should be the same) 193 | assertArrayEquals(expectedBytes, cka2.getValue()); 194 | 195 | // also while we have the object let's check the toString 196 | assertTrue(cka.toString(), cka.toString().contains("type=0x00000161{VALUE_LEN} [4B] value=100")); 197 | 198 | assertEquals(cka, cka2); 199 | assertEquals(cka.hashCode(), cka2.hashCode()); 200 | } 201 | 202 | 203 | @Test 204 | public void testString() { 205 | String expectedStr = "test"; 206 | byte[] expectedBytes = expectedStr.getBytes(StandardCharsets.US_ASCII); 207 | CKA cka = new CKA(CKA.LABEL, expectedStr); 208 | 209 | // we check the encoding 210 | byte[] bytes = cka.getValue(); 211 | assertArrayEquals(expectedBytes, bytes); 212 | 213 | // and the value 214 | assertEquals(expectedStr, cka.getValueStr()); 215 | 216 | // now let's create this attribute from the encoding of value 217 | CKA cka2 = new CKA(CKA.LABEL, bytes); 218 | 219 | // and check the value 220 | assertEquals(expectedStr, cka2.getValueStr()); 221 | 222 | // and the encoded bytes (should be the same) 223 | assertArrayEquals(expectedBytes, cka2.getValue()); 224 | 225 | // also while we have the object let's check the toString 226 | assertTrue(cka.toString(), cka.toString().contains("type=0x00000003{LABEL} [4B] value=\"test\"")); 227 | 228 | assertEquals(cka, cka2); 229 | assertEquals(cka.hashCode(), cka2.hashCode()); 230 | 231 | } 232 | 233 | @Test 234 | public void testBigInt() { 235 | 236 | // the test value is taken directly from PKCS#11 spec 237 | // Big integer 238 | // a string of CK_BYTEs representing an unsigned integer of arbitrary size, 239 | // most-significant byte first (e.g., the integer 32768 is represented as the 2-byte string 0x80 0x00) 240 | // this encoding when using two-complement encoding in big endian is a negative number 241 | // therefore needs special handling 242 | 243 | BigInteger expectedBint = BigInteger.valueOf(32768L); 244 | byte[] expectedBytes = {(byte) 0x80, 0x00}; 245 | 246 | // lets create the attribute from value 247 | CKA cka = new CKA(CKA.MODULUS, expectedBint); 248 | 249 | // we check the encoding 250 | byte[] bytes = cka.getValue(); 251 | assertArrayEquals(expectedBytes, bytes); 252 | 253 | // and the value 254 | BigInteger bint = cka.getValueBigInt(); 255 | assertEquals(expectedBint, bint); 256 | 257 | // now let's create this attribute from the encoding of value 258 | CKA cka2 = new CKA(CKA.MODULUS, bytes); 259 | 260 | // and check the value 261 | bint = cka2.getValueBigInt(); 262 | assertEquals(expectedBint, bint); 263 | 264 | // and the encoded bytes (should be the same) 265 | assertArrayEquals(expectedBytes, cka2.getValue()); 266 | 267 | // also while we have the object let's check the toString 268 | assertTrue(cka.toString().contains("type=0x00000120{MODULUS} [2B]")); 269 | assertTrue(cka.toString().contains("80 00")); 270 | 271 | assertEquals(cka, cka2); 272 | assertEquals(cka.hashCode(), cka2.hashCode()); 273 | } 274 | 275 | 276 | } 277 | --------------------------------------------------------------------------------