├── .gitignore ├── LICENSE.md ├── README.md ├── build.gradle ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── settings.gradle └── src └── main └── java └── to └── noc └── hsm └── lunasa └── example ├── HsmManager.java ├── KeyWrappingExample.java ├── KeyWrappingWithHsmGeneratedKeysExample.java └── WrappedKeySerializationExample.java /.gitignore: -------------------------------------------------------------------------------- 1 | # Generated files 2 | gen/ 3 | bin/ 4 | build/ 5 | target/ 6 | classes/ 7 | *.log 8 | local.properties 9 | *.class 10 | *.jar 11 | *.war 12 | *.ear 13 | *.apk 14 | *.ap_ 15 | *.dex 16 | .gradle/ 17 | 18 | # Backup files 19 | *~ 20 | *.bak 21 | *.orig 22 | *.swp 23 | 24 | # Android Studio and Intellij IDEA config files 25 | .idea 26 | *.iml 27 | 28 | # Eclipse config files 29 | .settings 30 | .classpath 31 | .project 32 | .metadata 33 | 34 | # Netbeans config files 35 | nb-configuration.xml 36 | nbactions.xml 37 | .nb-gradle 38 | nbproject/ 39 | 40 | # Mac files 41 | .DS_Store 42 | 43 | # Overrides 44 | !**/gradle/wrapper/*.jar 45 | !**/gradle/wrapper/*.properties 46 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | GNU LESSER GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | 9 | This version of the GNU Lesser General Public License incorporates 10 | the terms and conditions of version 3 of the GNU General Public 11 | License, supplemented by the additional permissions listed below. 12 | 13 | 0. Additional Definitions. 14 | 15 | As used herein, "this License" refers to version 3 of the GNU Lesser 16 | General Public License, and the "GNU GPL" refers to version 3 of the GNU 17 | General Public License. 18 | 19 | "The Library" refers to a covered work governed by this License, 20 | other than an Application or a Combined Work as defined below. 21 | 22 | An "Application" is any work that makes use of an interface provided 23 | by the Library, but which is not otherwise based on the Library. 24 | Defining a subclass of a class defined by the Library is deemed a mode 25 | of using an interface provided by the Library. 26 | 27 | A "Combined Work" is a work produced by combining or linking an 28 | Application with the Library. The particular version of the Library 29 | with which the Combined Work was made is also called the "Linked 30 | Version". 31 | 32 | The "Minimal Corresponding Source" for a Combined Work means the 33 | Corresponding Source for the Combined Work, excluding any source code 34 | for portions of the Combined Work that, considered in isolation, are 35 | based on the Application, and not on the Linked Version. 36 | 37 | The "Corresponding Application Code" for a Combined Work means the 38 | object code and/or source code for the Application, including any data 39 | and utility programs needed for reproducing the Combined Work from the 40 | Application, but excluding the System Libraries of the Combined Work. 41 | 42 | 1. Exception to Section 3 of the GNU GPL. 43 | 44 | You may convey a covered work under sections 3 and 4 of this License 45 | without being bound by section 3 of the GNU GPL. 46 | 47 | 2. Conveying Modified Versions. 48 | 49 | If you modify a copy of the Library, and, in your modifications, a 50 | facility refers to a function or data to be supplied by an Application 51 | that uses the facility (other than as an argument passed when the 52 | facility is invoked), then you may convey a copy of the modified 53 | version: 54 | 55 | a) under this License, provided that you make a good faith effort to 56 | ensure that, in the event an Application does not supply the 57 | function or data, the facility still operates, and performs 58 | whatever part of its purpose remains meaningful, or 59 | 60 | b) under the GNU GPL, with none of the additional permissions of 61 | this License applicable to that copy. 62 | 63 | 3. Object Code Incorporating Material from Library Header Files. 64 | 65 | The object code form of an Application may incorporate material from 66 | a header file that is part of the Library. You may convey such object 67 | code under terms of your choice, provided that, if the incorporated 68 | material is not limited to numerical parameters, data structure 69 | layouts and accessors, or small macros, inline functions and templates 70 | (ten or fewer lines in length), you do both of the following: 71 | 72 | a) Give prominent notice with each copy of the object code that the 73 | Library is used in it and that the Library and its use are 74 | covered by this License. 75 | 76 | b) Accompany the object code with a copy of the GNU GPL and this license 77 | document. 78 | 79 | 4. Combined Works. 80 | 81 | You may convey a Combined Work under terms of your choice that, 82 | taken together, effectively do not restrict modification of the 83 | portions of the Library contained in the Combined Work and reverse 84 | engineering for debugging such modifications, if you also do each of 85 | the following: 86 | 87 | a) Give prominent notice with each copy of the Combined Work that 88 | the Library is used in it and that the Library and its use are 89 | covered by this License. 90 | 91 | b) Accompany the Combined Work with a copy of the GNU GPL and this license 92 | document. 93 | 94 | c) For a Combined Work that displays copyright notices during 95 | execution, include the copyright notice for the Library among 96 | these notices, as well as a reference directing the user to the 97 | copies of the GNU GPL and this license document. 98 | 99 | d) Do one of the following: 100 | 101 | 0) Convey the Minimal Corresponding Source under the terms of this 102 | License, and the Corresponding Application Code in a form 103 | suitable for, and under terms that permit, the user to 104 | recombine or relink the Application with a modified version of 105 | the Linked Version to produce a modified Combined Work, in the 106 | manner specified by section 6 of the GNU GPL for conveying 107 | Corresponding Source. 108 | 109 | 1) Use a suitable shared library mechanism for linking with the 110 | Library. A suitable mechanism is one that (a) uses at run time 111 | a copy of the Library already present on the user's computer 112 | system, and (b) will operate properly with a modified version 113 | of the Library that is interface-compatible with the Linked 114 | Version. 115 | 116 | e) Provide Installation Information, but only if you would otherwise 117 | be required to provide such information under section 6 of the 118 | GNU GPL, and only to the extent that such information is 119 | necessary to install and execute a modified version of the 120 | Combined Work produced by recombining or relinking the 121 | Application with a modified version of the Linked Version. (If 122 | you use option 4d0, the Installation Information must accompany 123 | the Minimal Corresponding Source and Corresponding Application 124 | Code. If you use option 4d1, you must provide the Installation 125 | Information in the manner specified by section 6 of the GNU GPL 126 | for conveying Corresponding Source.) 127 | 128 | 5. Combined Libraries. 129 | 130 | You may place library facilities that are a work based on the 131 | Library side by side in a single library together with other library 132 | facilities that are not Applications and are not covered by this 133 | License, and convey such a combined library under terms of your 134 | choice, if you do both of the following: 135 | 136 | a) Accompany the combined library with a copy of the same work based 137 | on the Library, uncombined with any other library facilities, 138 | conveyed under the terms of this License. 139 | 140 | b) Give prominent notice with the combined library that part of it 141 | is a work based on the Library, and explaining where to find the 142 | accompanying uncombined form of the same work. 143 | 144 | 6. Revised Versions of the GNU Lesser General Public License. 145 | 146 | The Free Software Foundation may publish revised and/or new versions 147 | of the GNU Lesser General Public License from time to time. Such new 148 | versions will be similar in spirit to the present version, but may 149 | differ in detail to address new problems or concerns. 150 | 151 | Each version is given a distinguishing version number. If the 152 | Library as you received it specifies that a certain numbered version 153 | of the GNU Lesser General Public License "or any later version" 154 | applies to it, you have the option of following the terms and 155 | conditions either of that published version or of any later version 156 | published by the Free Software Foundation. If the Library as you 157 | received it does not specify a version number of the GNU Lesser 158 | General Public License, you may choose any version of the GNU Lesser 159 | General Public License ever published by the Free Software Foundation. 160 | 161 | If the Library as you received it specifies that a proxy can decide 162 | whether future versions of the GNU Lesser General Public License shall 163 | apply, that proxy's public statement of acceptance of any version is 164 | permanent authorization for you to choose that version for the 165 | Library. 166 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # HsmKeyWrappingExample 2 | 3 | This code provides simple examples to play with key wrapping on the 4 | SafeNet Luna SA HSM provided by Amazon's AWS CloudHSM offering. 5 | 6 | ## Why this is interesting? 7 | If you're only managing, say 20k keys, you can store them on the Luna SA HSM. 8 | Each key is physically stored on the HSM and accessed via a string label that 9 | you set. 10 | 11 | Managing larger numbers of keys is typically done by storing encrypted versions 12 | of those keys outside the HSM. A Key Encrption Key (KEK) is stored on the HSM 13 | to generate the encrypted host key. 14 | 15 | ## Severe issue with the AWS CloudHSM 16 | Ideally, the KEK can unwrap the encrypted host key on the HSM and allow 17 | the unwrapped key to be used in crypto operations, but never allow the HSM 18 | client to query back the unwrapped host key in the clear. In my testing 19 | with the Luna SA 8000 provided by Amazon CloudHSM, there is no way to do this. 20 | The HSM was always willing to give back the unwrapped key in the clear when 21 | calling *getEncoded()* on the key. 22 | 23 | **Update: The behavior I'm seeing is due to a defect in the Luna SA 5.1 24 | firmware. This version has a bug where any key unwrapped on the HSM is 25 | extractable. (An impressive defect considering the cost and purpose of this 26 | device!) Safenet claims release 5.2 and above fixes the issue, but the AWS 27 | CloudHSM version at the time of this writing is 5.1.2-2 so I am unable to 28 | confirm.** 29 | 30 | **Update 2: Amazon says they can update CloudHSM instances to firmware 31 | 5.3.1. I'll update this REAMDE again in a few months after I get sign-off on 32 | upgrading and try the updated firmware.** 33 | http://docs.aws.amazon.com/cloudhsm/latest/upgrade/cloud-hsm-upgrade-guide.html 34 | 35 | 36 | ## How to use this code 37 | 38 | 1) Create a test partition on your HSM and record the partition name and password. 39 | 40 | 2) Create a partition.properties file in your home directory with the following 41 | two properties: 42 | ``` 43 | partitionName = YourTestPartitionName 44 | partitionPass = PasswordForYourTestPartition 45 | ``` 46 | 47 | 3) From a Linux HSM client host with the Luna SA JSP client software installed: 48 | ``` 49 | $ git clone https://github.com/dimalinux/HsmKeyWrappingExample.git 50 | $ cd HsmKeyWrappingExample 51 | 52 | $ ./gradlew clean run 53 | # Or: 54 | $ ./gradlew clean build 55 | $ java -Djava.library.path=/usr/lunasa/jsp/lib -cp build/libs/HsmKeyWrappingExample.jar:/usr/lunasa/jsp/lib/LunaProvider.jar to.noc.hsm.lunasa.example.KeyWrappingExample 56 | ``` 57 | 58 | 4) To run one of the the alternate examples, swap the main class declared in 59 | the build.gradle file or the main class on the command line if invoking java 60 | directly. 61 | -------------------------------------------------------------------------------- /build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'application' 2 | apply plugin: 'eclipse' 3 | 4 | tasks.eclipse.dependsOn(cleanEclipse) 5 | 6 | String LUNA_LIB_DIR = '/usr/safenet/lunaclient/jsp/lib/' 7 | 8 | mainClassName = 'to.noc.hsm.lunasa.example.KeyWrappingExample' 9 | //mainClassName = 'to.noc.hsm.lunasa.example.WrappedKeySerializationExample' 10 | //mainClassName = 'to.noc.hsm.lunasa.example.KeyWrappingWithHsmGeneratedKeysExample' 11 | sourceCompatibility = '1.7' 12 | 13 | applicationDefaultJvmArgs = ["-D=java.library.path=$LUNA_LIB_DIR"] 14 | 15 | repositories { 16 | mavenCentral() 17 | } 18 | 19 | dependencies { 20 | compile 'com.google.guava:guava:18.0' // provides Bytes.indexOf 21 | compile 'org.apache.commons:commons-lang3:3.3.2' // provides SerializationUtils 22 | compile fileTree(dir: LUNA_LIB_DIR, include: 'LunaProvider.jar') 23 | } 24 | 25 | run { 26 | systemProperty 'java.library.path', LUNA_LIB_DIR 27 | } 28 | 29 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dimalinux/HsmKeyWrappingExample/1b0ee5e95a5321360e2549fa950b01fbcbe5b4b9/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Fri Feb 20 17:23:28 CST 2015 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | distributionUrl=https\://services.gradle.org/distributions/gradle-2.2.1-bin.zip 7 | -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | ############################################################################## 4 | ## 5 | ## Gradle start up script for UN*X 6 | ## 7 | ############################################################################## 8 | 9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 10 | DEFAULT_JVM_OPTS="" 11 | 12 | APP_NAME="Gradle" 13 | APP_BASE_NAME=`basename "$0"` 14 | 15 | # Use the maximum available, or set MAX_FD != -1 to use that value. 16 | MAX_FD="maximum" 17 | 18 | warn ( ) { 19 | echo "$*" 20 | } 21 | 22 | die ( ) { 23 | echo 24 | echo "$*" 25 | echo 26 | exit 1 27 | } 28 | 29 | # OS specific support (must be 'true' or 'false'). 30 | cygwin=false 31 | msys=false 32 | darwin=false 33 | case "`uname`" in 34 | CYGWIN* ) 35 | cygwin=true 36 | ;; 37 | Darwin* ) 38 | darwin=true 39 | ;; 40 | MINGW* ) 41 | msys=true 42 | ;; 43 | esac 44 | 45 | # For Cygwin, ensure paths are in UNIX format before anything is touched. 46 | if $cygwin ; then 47 | [ -n "$JAVA_HOME" ] && JAVA_HOME=`cygpath --unix "$JAVA_HOME"` 48 | fi 49 | 50 | # Attempt to set APP_HOME 51 | # Resolve links: $0 may be a link 52 | PRG="$0" 53 | # Need this for relative symlinks. 54 | while [ -h "$PRG" ] ; do 55 | ls=`ls -ld "$PRG"` 56 | link=`expr "$ls" : '.*-> \(.*\)$'` 57 | if expr "$link" : '/.*' > /dev/null; then 58 | PRG="$link" 59 | else 60 | PRG=`dirname "$PRG"`"/$link" 61 | fi 62 | done 63 | SAVED="`pwd`" 64 | cd "`dirname \"$PRG\"`/" >&- 65 | APP_HOME="`pwd -P`" 66 | cd "$SAVED" >&- 67 | 68 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 69 | 70 | # Determine the Java command to use to start the JVM. 71 | if [ -n "$JAVA_HOME" ] ; then 72 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 73 | # IBM's JDK on AIX uses strange locations for the executables 74 | JAVACMD="$JAVA_HOME/jre/sh/java" 75 | else 76 | JAVACMD="$JAVA_HOME/bin/java" 77 | fi 78 | if [ ! -x "$JAVACMD" ] ; then 79 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 80 | 81 | Please set the JAVA_HOME variable in your environment to match the 82 | location of your Java installation." 83 | fi 84 | else 85 | JAVACMD="java" 86 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 87 | 88 | Please set the JAVA_HOME variable in your environment to match the 89 | location of your Java installation." 90 | fi 91 | 92 | # Increase the maximum file descriptors if we can. 93 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then 94 | MAX_FD_LIMIT=`ulimit -H -n` 95 | if [ $? -eq 0 ] ; then 96 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 97 | MAX_FD="$MAX_FD_LIMIT" 98 | fi 99 | ulimit -n $MAX_FD 100 | if [ $? -ne 0 ] ; then 101 | warn "Could not set maximum file descriptor limit: $MAX_FD" 102 | fi 103 | else 104 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 105 | fi 106 | fi 107 | 108 | # For Darwin, add options to specify how the application appears in the dock 109 | if $darwin; then 110 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 111 | fi 112 | 113 | # For Cygwin, switch paths to Windows format before running java 114 | if $cygwin ; then 115 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 116 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 117 | 118 | # We build the pattern for arguments to be converted via cygpath 119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 120 | SEP="" 121 | for dir in $ROOTDIRSRAW ; do 122 | ROOTDIRS="$ROOTDIRS$SEP$dir" 123 | SEP="|" 124 | done 125 | OURCYGPATTERN="(^($ROOTDIRS))" 126 | # Add a user-defined pattern to the cygpath arguments 127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 129 | fi 130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 131 | i=0 132 | for arg in "$@" ; do 133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 135 | 136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 138 | else 139 | eval `echo args$i`="\"$arg\"" 140 | fi 141 | i=$((i+1)) 142 | done 143 | case $i in 144 | (0) set -- ;; 145 | (1) set -- "$args0" ;; 146 | (2) set -- "$args0" "$args1" ;; 147 | (3) set -- "$args0" "$args1" "$args2" ;; 148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;; 149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 154 | esac 155 | fi 156 | 157 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules 158 | function splitJvmOpts() { 159 | JVM_OPTS=("$@") 160 | } 161 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS 162 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME" 163 | 164 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@" 165 | -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 12 | set DEFAULT_JVM_OPTS= 13 | 14 | set DIRNAME=%~dp0 15 | if "%DIRNAME%" == "" set DIRNAME=. 16 | set APP_BASE_NAME=%~n0 17 | set APP_HOME=%DIRNAME% 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto init 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto init 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :init 49 | @rem Get command-line arguments, handling Windowz variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | if "%@eval[2+2]" == "4" goto 4NT_args 53 | 54 | :win9xME_args 55 | @rem Slurp the command line arguments. 56 | set CMD_LINE_ARGS= 57 | set _SKIP=2 58 | 59 | :win9xME_args_slurp 60 | if "x%~1" == "x" goto execute 61 | 62 | set CMD_LINE_ARGS=%* 63 | goto execute 64 | 65 | :4NT_args 66 | @rem Get arguments from the 4NT Shell from JP Software 67 | set CMD_LINE_ARGS=%$ 68 | 69 | :execute 70 | @rem Setup the command line 71 | 72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 73 | 74 | @rem Execute Gradle 75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 76 | 77 | :end 78 | @rem End local scope for the variables with windows NT shell 79 | if "%ERRORLEVEL%"=="0" goto mainEnd 80 | 81 | :fail 82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 83 | rem the _cmd.exe /c_ return code! 84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 85 | exit /b 1 86 | 87 | :mainEnd 88 | if "%OS%"=="Windows_NT" endlocal 89 | 90 | :omega 91 | -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dimalinux/HsmKeyWrappingExample/1b0ee5e95a5321360e2549fa950b01fbcbe5b4b9/settings.gradle -------------------------------------------------------------------------------- /src/main/java/to/noc/hsm/lunasa/example/HsmManager.java: -------------------------------------------------------------------------------- 1 | package to.noc.hsm.lunasa.example; 2 | 3 | import com.safenetinc.luna.LunaSlotManager; 4 | 5 | import java.io.File; 6 | import java.io.FileInputStream; 7 | import java.io.IOException; 8 | import java.io.InputStream; 9 | import java.security.*; 10 | import java.security.cert.Certificate; 11 | import java.security.cert.CertificateException; 12 | import java.util.Properties; 13 | 14 | /* 15 | * This class looks for a file named "partition.properties" in the current 16 | * user's home directory. The file needs the following two properties: 17 | * 18 | * partitionName = YourPartitionName 19 | * partitionPass = PasswordForYourTestPartition 20 | * 21 | */ 22 | public class HsmManager { 23 | private static final LunaSlotManager slotManager; 24 | private static KeyStore keyStore; 25 | private static final String partitionName; 26 | private static final String partitionPass; 27 | 28 | static { 29 | Security.addProvider(new com.safenetinc.luna.provider.LunaProvider()); 30 | slotManager = LunaSlotManager.getInstance(); 31 | 32 | Properties prop = new Properties(); 33 | try { 34 | File propFile = new File(System.getProperty("user.home"), "partition.properties"); 35 | InputStream in = new FileInputStream(propFile); 36 | prop.load(in); 37 | in.close(); 38 | } catch (IOException ex) { 39 | ex.printStackTrace(); 40 | System.exit(-1); 41 | } 42 | 43 | partitionName = prop.getProperty("partitionName"); 44 | partitionPass = prop.getProperty("partitionPass"); 45 | 46 | if (partitionName == null || partitionPass == null) { 47 | System.err.println("Aborting, mandatory properties not set"); 48 | System.exit(-1); 49 | } 50 | } 51 | 52 | public static void login() throws KeyStoreException, CertificateException, NoSuchAlgorithmException, IOException { 53 | slotManager.login(partitionName, partitionPass); 54 | keyStore = KeyStore.getInstance("Luna"); 55 | keyStore.load(null, null); 56 | } 57 | 58 | public static void logout() { 59 | slotManager.logout(); 60 | keyStore = null; 61 | } 62 | 63 | public static boolean hasSavedKey(String alias) throws KeyStoreException { 64 | // I think the second check alone is sufficient 65 | return keyStore.containsAlias(alias) && keyStore.isKeyEntry(alias); 66 | } 67 | 68 | public static Key getSavedKey(String alias) throws UnrecoverableEntryException, NoSuchAlgorithmException, KeyStoreException { 69 | return keyStore.getKey(alias, null); 70 | } 71 | 72 | public static void saveKey(String alias, Key key) throws KeyStoreException { 73 | keyStore.setKeyEntry(alias, key, null, null); 74 | } 75 | 76 | public static void saveRsaKey(String alias, Key key, Certificate[] chain) throws KeyStoreException { 77 | keyStore.setKeyEntry(alias, key, null, chain); 78 | } 79 | 80 | public static void deleteKey(String alias) throws KeyStoreException { 81 | keyStore.deleteEntry(alias); 82 | } 83 | 84 | public static void setSecretKeysExtractable(boolean isExtractable) { 85 | slotManager.setSecretKeysExtractable(isExtractable); 86 | } 87 | 88 | } 89 | -------------------------------------------------------------------------------- /src/main/java/to/noc/hsm/lunasa/example/KeyWrappingExample.java: -------------------------------------------------------------------------------- 1 | package to.noc.hsm.lunasa.example; 2 | 3 | import com.safenetinc.luna.LunaUtils; 4 | import com.safenetinc.luna.provider.key.LunaSecretKey; 5 | 6 | import javax.crypto.Cipher; 7 | import javax.crypto.KeyGenerator; 8 | import javax.crypto.SecretKey; 9 | import javax.crypto.spec.IvParameterSpec; 10 | import java.security.AlgorithmParameters; 11 | import java.security.GeneralSecurityException; 12 | import java.security.SecureRandom; 13 | 14 | import static java.lang.System.out; 15 | 16 | /* 17 | * Example of how to use encrypted host keys with the Luna SA cryptographic provider. 18 | * Unfortunately, this example also shows that the AWS CloudHSM device is not protecting 19 | * keys that were unwrapped on the HSM from extraction. An attacker gaining access to 20 | * both your database (with encrypted host keys) and access to your HSM client host can 21 | * easily and quickly convert all encrypted host keys into their unencrypted form. 22 | * 23 | * Sample output from the code below: 24 | * 25 | * HSM KEK ID (a handle, not in clear): 26 | * 0x0000000000000011 27 | * 28 | * Software-Only 3DES Key (in the clear): 29 | * 0x835B7534DC266DA22646E3DF3D5D7A6B4A348FCB2F67DA76 30 | * 31 | * KEK wrapped 3DES key (host key): 32 | * 0x511382325E89988B500DA3F9DFF5A53CCECF63B445A68450F426EC83464493E4 33 | * 34 | * Stopping and starting session with HSM. 35 | * Pretend that the host key was stored and restored from a database while disconnected 36 | * 37 | * Unwrapped 3DES key is same as original (in clear): 38 | * 0x835B7534DC266DA22646E3DF3D5D7A6B4A348FCB2F67DA76 39 | * 40 | * Class of unwrapped key: com.safenetinc.luna.provider.key.LunaSecretKey 41 | * 42 | * Original plaintext: 43 | * 12345678 44 | * 45 | * SunJCE encryption result: 46 | * 0x11559F95F6E862AF 47 | * 48 | * LunaProvider HSM decrypt result: 49 | * 12345678 50 | */ 51 | public class KeyWrappingExample { 52 | // 53 | // Alias used to store the the KEK (Key Encryption Key) on the HSM 54 | // 55 | private static final String KEK_ALIAS = "KEK_AES_TEST"; 56 | 57 | // OpenJDK handles AES 256 just fine, but I had to lower this to 128 for 58 | // Oracle's off-the-shelf JDK which has strong crypto disabled. 59 | private static int KEK_NUM_KEY_BITS = 128; 60 | 61 | // 62 | // AES has a block size of 128 bits or 16 bytes. Used easy HEX values so you can plug them 63 | // into a website like http://aes.online-domain-tools.com/ to verify encrypted values. 64 | // 65 | private static final byte[] FIXED_128BIT_IV_FOR_TESTS = 66 | LunaUtils.hexStringToByteArray("DEADD00D8BADF00DDEADBABED15EA5ED"); 67 | 68 | 69 | 70 | public static void main(String[] args) throws Exception { 71 | final String hostKeyType = "DESede"; 72 | 73 | HsmManager.login(); 74 | HsmManager.setSecretKeysExtractable(false); 75 | 76 | SecretKey kek = createNewHsmKek(); 77 | out.println("HSM KEK ID (a handle, not in clear):\n\t" + getHex(kek.getEncoded())); 78 | 79 | SecretKey des3Key = createSoftwareKey(hostKeyType); 80 | out.println("Software-Only 3DES Key (in the clear):\n\t" + getHex(des3Key.getEncoded())); 81 | 82 | byte[] wrappedHostKey = wrapKeyWithKek(kek, des3Key); 83 | out.println("KEK wrapped 3DES key (host key):\n\t" + getHex(wrappedHostKey)); 84 | 85 | 86 | out.println("\nStopping and starting session with HSM."); 87 | HsmManager.logout(); 88 | out.println("Pretend that the host key was stored and restored from a database while disconnected\n"); 89 | kek = null; 90 | des3Key = null; 91 | HsmManager.login(); 92 | 93 | kek = getExistingHsmKek(); 94 | SecretKey unwrapped3DesKey = unwrapKeyWithKek(kek, hostKeyType, wrappedHostKey); 95 | out.println("Unwrapped 3DES key is same as original (in clear):\n\t" + getHex(unwrapped3DesKey.getEncoded())); 96 | out.println("Class of unwrapped key: " + unwrapped3DesKey.getClass().getCanonicalName()); 97 | 98 | // 99 | // Give an example using using the unwrapped LunaSecretKey in both a software 100 | // cipher operation and an HSM cipher operation. If the LunaSecretKey was 101 | // protected in hardware, the SunJCE operation with it would fail. 102 | // 103 | String plainText = "12345678"; 104 | byte[] plainTextBytes = plainText.getBytes(); 105 | out.println("Original plaintext:\n\t" + plainText); 106 | 107 | // Start software cipher operation 108 | Cipher sunJceCipher = Cipher.getInstance("DESede/ECB/NoPadding", "SunJCE"); 109 | sunJceCipher.init(Cipher.ENCRYPT_MODE, unwrapped3DesKey); 110 | byte[] cipherText = sunJceCipher.doFinal(plainText.getBytes()); 111 | out.println("SunJCE encryption result:\n\t" + getHex(cipherText)); 112 | 113 | // Start Luna HSM cipher operation 114 | Cipher lunaHsmCipher = Cipher.getInstance("DESede/ECB/NoPadding", "LunaProvider"); 115 | lunaHsmCipher.init(Cipher.DECRYPT_MODE, unwrapped3DesKey); 116 | byte[] originalClearText = lunaHsmCipher.doFinal(cipherText); 117 | out.println("LunaProvider HSM decrypt result:\n\t" + new String(originalClearText)); 118 | 119 | 120 | HsmManager.logout(); 121 | } 122 | 123 | 124 | // 125 | // KEK is generated on HSM. We never know its value, just it's alias to 126 | // access it. 127 | // 128 | private static SecretKey createNewHsmKek() throws GeneralSecurityException { 129 | 130 | if (HsmManager.hasSavedKey(KEK_ALIAS)) { 131 | HsmManager.deleteKey(KEK_ALIAS); 132 | } 133 | 134 | KeyGenerator kg = KeyGenerator.getInstance("AES", "LunaProvider"); 135 | kg.init(KEK_NUM_KEY_BITS); 136 | 137 | LunaSecretKey kek = (LunaSecretKey) kg.generateKey(); 138 | 139 | // 140 | // Since our KEK will only be used for wrap and unwrap operations, we should 141 | // disable encryption and decryption operations. 142 | // 143 | // Note: In theory, the commented out lines below will work on Luna SA 144 | // firmwares 5.2 and above, but on our AWS CloudHSM firmware disabling 145 | // encrypt and decrypt functionality will disable Cipher.WRAP_MODE 146 | // and Cipher.UNWRAP_MODE respectively. This is makes sense. 147 | // If the unwrapped keys are not stored and protected on the HSM, 148 | // wrapping and unwrapping operations *are* providing somewhat general 149 | // encrypt and decrypt functionality. 150 | // 151 | //LunaTokenObject obj = LunaTokenObject.LocateObjectByHandle(kek.GetKeyHandle()); 152 | //obj.SetBooleanAttribute(LunaAPI.CKA_ENCRYPT, false); 153 | //obj.SetBooleanAttribute(LunaAPI.CKA_DECRYPT, false); 154 | 155 | HsmManager.saveKey(KEK_ALIAS, kek); 156 | 157 | return kek; 158 | } 159 | 160 | 161 | private static SecretKey getExistingHsmKek() throws GeneralSecurityException { 162 | // casting KEY -> SecretKey 163 | return (SecretKey) HsmManager.getSavedKey(KEK_ALIAS); 164 | } 165 | 166 | // 167 | // Create software-only (not stored on HSM) key 168 | // 169 | private static SecretKey createSoftwareKey(String keyType) throws GeneralSecurityException { 170 | // 171 | // SunJCE would be used by default when running openjdk 1.7, but I decided 172 | // to be explicit to ensure we are generating a key in software for this 173 | // example. 174 | // 175 | KeyGenerator generator = KeyGenerator.getInstance(keyType, "SunJCE"); 176 | generator.init(new SecureRandom()); 177 | return generator.generateKey(); 178 | } 179 | 180 | // 181 | // Wrap the passed in key with the KEK on the HSM 182 | // 183 | private static byte[] wrapKeyWithKek(SecretKey hsmKek, SecretKey softwareKey) throws GeneralSecurityException { 184 | Cipher wrappingCipher = Cipher.getInstance("AES/CBC/PKCS5Padding", "LunaProvider"); 185 | AlgorithmParameters algParams = AlgorithmParameters.getInstance("IV", "LunaProvider"); 186 | algParams.init(new IvParameterSpec(FIXED_128BIT_IV_FOR_TESTS)); 187 | wrappingCipher.init(Cipher.WRAP_MODE, hsmKek, algParams); 188 | return wrappingCipher.wrap(softwareKey); 189 | } 190 | 191 | // 192 | // Unwrap the passed in key with the KEK on the HSM 193 | // 194 | private static SecretKey unwrapKeyWithKek(SecretKey hsmKey, String keyAlgorithm, byte[] wrappedKeyBytes) throws GeneralSecurityException { 195 | Cipher wrappingCipher = Cipher.getInstance("AES/CBC/PKCS5Padding", "LunaProvider"); 196 | AlgorithmParameters algParams = AlgorithmParameters.getInstance("IV", "LunaProvider"); 197 | algParams.init(new IvParameterSpec(FIXED_128BIT_IV_FOR_TESTS)); 198 | wrappingCipher.init(Cipher.UNWRAP_MODE, hsmKey, algParams); 199 | return (SecretKey) wrappingCipher.unwrap(wrappedKeyBytes, keyAlgorithm, Cipher.SECRET_KEY); 200 | } 201 | 202 | 203 | private static String getHex(byte array[]) { 204 | return "0x" + LunaUtils.getHexString(array, false).toUpperCase(); 205 | } 206 | } 207 | 208 | 209 | -------------------------------------------------------------------------------- /src/main/java/to/noc/hsm/lunasa/example/KeyWrappingWithHsmGeneratedKeysExample.java: -------------------------------------------------------------------------------- 1 | package to.noc.hsm.lunasa.example; 2 | 3 | import com.safenetinc.luna.LunaUtils; 4 | import com.safenetinc.luna.provider.key.LunaKey; 5 | 6 | import javax.crypto.Cipher; 7 | import javax.crypto.KeyGenerator; 8 | import javax.crypto.SecretKey; 9 | import javax.crypto.spec.IvParameterSpec; 10 | import java.security.Key; 11 | 12 | import static java.lang.System.out; 13 | 14 | 15 | /* 16 | * This is similar to the other KeyWrapping example, but in this example we generate all keys 17 | * on the HSM and call LunaSlotManager::setSecretKeysExtractable. As before, the unwrapped 18 | * key is exposed to the application layer. 19 | * 20 | * Output from a sample run: 21 | * 22 | * AES Key generated on HSM: 23 | * 0x834869E03E3054C127712883B0F7CB9716E2DF619D50002F164934EB9C5C7242 24 | * 25 | * Class of key to wrap: 26 | * class com.safenetinc.luna.provider.key.LunaSecretKey 27 | * 28 | * Key to wrap: 29 | * 0xBD8B34A1649FB3EC9AF57DD157FA8BB9C2CCDB1C35D37CC51EB32EC39533509D 30 | * 31 | * Wrapped Key: 32 | * 0xBF2DFCC8BB0679B01B03BF0CEB0F666C2C4E35A75743E39B678F1C3007E55F48E02B1DA8A90E227CF6DF6B2BBE8D1277 33 | * 34 | * Unwrapped key (in clear same as original): 35 | * 0xBD8B34A1649FB3EC9AF57DD157FA8BB9C2CCDB1C35D37CC51EB32EC39533509D 36 | * 37 | * Plain Text ('SixteenByteClear'): 38 | * 0x5369787465656E42797465436C656172 39 | * 40 | * Cipher Text: 41 | * 0x2F5542DA100B07AF26262A8E31050E85A6F554397AC6EAE161769C93FDC6E81A 42 | * 43 | */ 44 | public class KeyWrappingWithHsmGeneratedKeysExample { 45 | 46 | // 47 | // AES block size is 128 bits or 16 bytes. Used easy HEX values so you can plug them 48 | // into a website like http://aes.online-domain-tools.com/ to verify encrypted values. 49 | // 50 | private static final IvParameterSpec FIXED_128BIT_IV_FOR_TESTS = new IvParameterSpec( 51 | LunaUtils.hexStringToByteArray("DEADD00D8BADF00DDEADBABED15EA5ED") 52 | ); 53 | 54 | 55 | public static void main(String[] args) throws Exception { 56 | 57 | HsmManager.login(); 58 | HsmManager.setSecretKeysExtractable(true); 59 | 60 | KeyGenerator lunaKeyGenerator = KeyGenerator.getInstance("AES", "LunaProvider"); 61 | Cipher lunaAesCbcCipher = Cipher.getInstance("AES/CBC/PKCS5Padding", "LunaProvider"); 62 | 63 | // 64 | // Generate a wrapping key (KEK: Key Encryption Key) using the Luna SA HSM. 65 | // OpenJDK handles AES 256 just fine, but I had to back this down to 128 for 66 | // Oracle's JDK which, by default, has strong crypto disabled. 67 | // 68 | lunaKeyGenerator.init(128); 69 | Key kek = lunaKeyGenerator.generateKey(); 70 | 71 | 72 | // 73 | // If we hadn't set setSecretKeysExtractable(true), the key printed below would 74 | // just be an ID. Since we did make secret keys extractable, the line below will 75 | // print the actual key value in the clear. 76 | // 77 | out.println("AES Key generated on HSM: " + getHex(kek.getEncoded())); 78 | 79 | 80 | // 81 | // Generate a second AES key to be wrapped. As with the KEK above, the key printed 82 | // below will be the actual key in the clear. 83 | // 84 | SecretKey keyToWrap = lunaKeyGenerator.generateKey(); 85 | out.println("Class of key to wrap: " + keyToWrap.getClass()); 86 | out.println("Key to wrap: " + getHex(keyToWrap.getEncoded())); 87 | 88 | 89 | // 90 | // Wrap the key 91 | // 92 | // Note: If you set 'setSecretKeysExtractable' above to false, this exception 93 | // will be thrown during the call to 'wrap': 94 | // 95 | // LunaCryptokiException: function 'C_WrapKey' returns 0x6a 96 | // 97 | // 0x6a is 'CKR_KEY_UNEXTRACTABLE' 98 | // 99 | lunaAesCbcCipher.init(Cipher.WRAP_MODE, kek, FIXED_128BIT_IV_FOR_TESTS); 100 | byte[] wrappedKey = lunaAesCbcCipher.wrap(keyToWrap); 101 | out.println("Wrapped Key: " + getHex(wrappedKey)); 102 | 103 | 104 | // 105 | // Unwrap the key 106 | // 107 | lunaAesCbcCipher.init(Cipher.UNWRAP_MODE, kek, FIXED_128BIT_IV_FOR_TESTS); 108 | LunaKey unwrappedKey = (LunaKey)lunaAesCbcCipher.unwrap(wrappedKey, "AES", Cipher.SECRET_KEY); 109 | out.println("Unwrapped key (in clear same as original): " + getHex(unwrappedKey.getEncoded())); 110 | 111 | 112 | // 113 | // Encrypt data with unwrapped key 114 | // 115 | lunaAesCbcCipher.init(Cipher.ENCRYPT_MODE, unwrappedKey, FIXED_128BIT_IV_FOR_TESTS); 116 | String plainTextStr = "SixteenByteClear"; 117 | byte[] plainText = "SixteenByteClear".getBytes(); 118 | byte[] cipherText = lunaAesCbcCipher.doFinal(plainText); 119 | out.println("Plain Text ('" + plainTextStr + "'): " + getHex(plainText)); 120 | out.println("Cipher Text: " + getHex(cipherText)); 121 | 122 | unwrappedKey.DestroyKey(); 123 | HsmManager.logout(); 124 | } 125 | 126 | 127 | private static String getHex(byte array[]) { 128 | return "0x" + LunaUtils.getHexString(array, false).toUpperCase(); 129 | } 130 | 131 | } 132 | -------------------------------------------------------------------------------- /src/main/java/to/noc/hsm/lunasa/example/WrappedKeySerializationExample.java: -------------------------------------------------------------------------------- 1 | package to.noc.hsm.lunasa.example; 2 | 3 | import com.google.common.primitives.Bytes; 4 | import com.safenetinc.luna.LunaUtils; 5 | import com.safenetinc.luna.provider.key.LunaSecretKey; 6 | import org.apache.commons.lang3.SerializationUtils; 7 | 8 | import javax.crypto.Cipher; 9 | import javax.crypto.KeyGenerator; 10 | import javax.crypto.SecretKey; 11 | import javax.crypto.spec.IvParameterSpec; 12 | import java.io.Serializable; 13 | import java.security.AlgorithmParameters; 14 | import java.security.GeneralSecurityException; 15 | import java.security.NoSuchAlgorithmException; 16 | import java.security.SecureRandom; 17 | 18 | import static java.lang.System.out; 19 | 20 | 21 | /* 22 | * In some cases we may want use Java serialization with encrypted (by the Luna SA HSM) 23 | * host keys. This is a simple example of one way it might be done. 24 | * 25 | * Example output: 26 | * 27 | * HSM KEK (not in the clear): 28 | * 0x0000000000000010 29 | * 30 | * Initial Host Key (in the clear): 31 | * 0x313DDA5BBA4F136B290BDFDCB9B33245311543EC15AD8645 32 | * 33 | * Wrapping IV for Host Key: 34 | * 0x653782F3BA649860 35 | * 36 | * KEK Wrapped Host Key: 37 | * 0x75102B24F1BD0F3DF80D14CA85276A3EC2B82FB1E8E87ACA0756EB5ACA13DBD8 38 | * 39 | * Serialized Key Holder: 40 | * 0xACED000573720047746F2E6E6F632E68736D2E6C756E6173612E6578616D70... 41 | * 42 | * Index of IV in Serialized Data: 43 | * 227 44 | * 45 | * Index of Encrypted Key in Serialized Data: 46 | * 185 47 | * 48 | * Index of Unencrypted Key in Serialized Data: 49 | * -1 50 | * 51 | * Is unwrapped secret key null after deserialization? 52 | * true 53 | * 54 | * Deserialized and Unwrapped Host Key (same as original): 55 | * 0x313DDA5BBA4F136B290BDFDCB9B33245311543EC15AD8645 56 | * 57 | * Plain Text: 58 | * 0xDEADD00D8BADF00DDEADBABED15EA5ED 59 | * 60 | * Cipher Text: 61 | * 0xA2162BBE595A50FE766E894EC0F6BD7F 62 | * 63 | * Plain Text Decrypted: 64 | * 0xDEADD00D8BADF00DDEADBABED15EA5ED 65 | */ 66 | public class WrappedKeySerializationExample { 67 | 68 | // Label used to store the the KEK (Key Encryption Key) on the HSM 69 | private static final String KEK_ALIAS = "KEK_3DES"; 70 | 71 | 72 | /* 73 | * Provides a serializable wrapper around the encrypted host key. Key will automatically 74 | * be unwrapped the first time it is used after deserialization. 75 | */ 76 | private static class SerializableKeyHolder implements Serializable { 77 | 78 | // Transient objects are not serialized and are null after deserialization 79 | transient private SecretKey secretKey; 80 | 81 | private byte[] encryptedHostKey; 82 | private byte[] iv; 83 | private String keyAlgorithm; 84 | 85 | 86 | private SerializableKeyHolder(String keyAlgorithm, byte[] iv, byte[] encryptedHostKey) { 87 | this.keyAlgorithm = keyAlgorithm; 88 | this.iv = iv; 89 | this.encryptedHostKey = encryptedHostKey; 90 | } 91 | 92 | public SecretKey getSecretKey() throws GeneralSecurityException { 93 | if (secretKey == null) { 94 | secretKey = unwrapKeyWithKek(getExistingHsmKek(), keyAlgorithm, iv, encryptedHostKey); 95 | } 96 | return secretKey; 97 | } 98 | 99 | } 100 | 101 | 102 | 103 | public static void main(String[] args) throws Exception { 104 | final String hostKeyType = "DESede"; // 3 DES 105 | 106 | HsmManager.login(); 107 | HsmManager.setSecretKeysExtractable(false); 108 | 109 | // 110 | // Create the HSM KEK (Key Encryption Key) 111 | // 112 | SecretKey kek = createNewHsm3desKek(); 113 | out.println("HSM KEK (not in the clear):\n\t" + getHex(kek.getEncoded())); 114 | 115 | 116 | // 117 | // Create a key that's not on the HSM for the purposes of this example 118 | // 119 | SecretKey hostKeyUnencrypted = createSoftwareKey(hostKeyType); 120 | byte[] hostKeyWrappingIv = new byte[8]; 121 | new SecureRandom().nextBytes(hostKeyWrappingIv); 122 | out.println("Initial Host Key (in the clear):\n\t " + getHex(hostKeyUnencrypted.getEncoded())); 123 | out.println("Wrapping IV for Host Key:\n\t "+ getHex(hostKeyWrappingIv)); 124 | 125 | // 126 | // Use the HSM to wrap our host key 127 | // 128 | byte[] wrappedHostKey = wrapKeyWithKek(kek, hostKeyWrappingIv, hostKeyUnencrypted); 129 | out.println("KEK Wrapped Host Key:\n\t " + getHex(wrappedHostKey)); 130 | 131 | // 132 | // Put the encrypted host key in our serializable holder 133 | // 134 | SerializableKeyHolder keyHolder = new SerializableKeyHolder(hostKeyType, hostKeyWrappingIv, wrappedHostKey); 135 | keyHolder.getSecretKey(); // force key unwrapping before serialization 136 | 137 | 138 | // 139 | // Serialize our holder into a byte array and destroy our reference to the original object 140 | // 141 | byte[] serializedBytes = SerializationUtils.serialize(keyHolder); 142 | keyHolder = null; 143 | out.println("Serialized Key Holder:\n\t" + getHex(serializedBytes)); 144 | 145 | // 146 | // Show that the IV and the encrypted key are subarrays of the serialized data, but that 147 | // the unencrypted key is not. It doesn't prove that the unencrypted key isn't there, but 148 | // it's a sanity check nonetheless. 149 | // 150 | int indexOfIvInSerializedData = Bytes.indexOf(serializedBytes, hostKeyWrappingIv); 151 | out.println("Index of IV in Serialized Data:\n\t" + indexOfIvInSerializedData); 152 | int indexOfEncryptedKeyInSerializedData = Bytes.indexOf(serializedBytes, wrappedHostKey); 153 | out.println("Index of Encrypted Key in Serialized Data:\n\t" + indexOfEncryptedKeyInSerializedData); 154 | int indexOfUnencryptedKeyInSerializedData = Bytes.indexOf(serializedBytes, hostKeyUnencrypted.getEncoded()); 155 | out.println("Index of Unencrypted Key in Serialized Data:\n\t" + indexOfUnencryptedKeyInSerializedData); 156 | 157 | 158 | // 159 | // Reconstitute (deserialize) the holder from the byte array 160 | // 161 | keyHolder = SerializationUtils.deserialize(serializedBytes); 162 | out.println("Is unwrapped secret key null after deserialization?\n\t" + (keyHolder.secretKey == null)); 163 | out.println("Deserialized and Unwrapped Host Key (same as original):\n\t" + 164 | getHex(keyHolder.getSecretKey().getEncoded())); 165 | 166 | 167 | 168 | // 169 | // Encrypt and decrypt some meaningless data using the HSM just for kicks! 170 | // 171 | byte[] plainText = LunaUtils.hexStringToByteArray("DEADD00D8BADF00DDEADBABED15EA5ED"); 172 | out.println("Plain Text:\n\t" + getHex(plainText)); 173 | 174 | Cipher cipher = Cipher.getInstance("DESede/ECB/NoPadding", "LunaProvider"); 175 | SecretKey hostKey = keyHolder.getSecretKey(); 176 | cipher.init(Cipher.ENCRYPT_MODE, hostKey); 177 | byte[] cipherText = cipher.doFinal(plainText); 178 | out.println("Cipher Text:\n\t" + getHex(cipherText)); 179 | cipher.init(Cipher.DECRYPT_MODE, hostKey); 180 | 181 | byte[] plainTextDecrypted = cipher.doFinal(cipherText); 182 | out.println("Plain Text Decrypted:\n\t" + getHex(plainTextDecrypted)); 183 | 184 | HsmManager.logout(); 185 | } 186 | 187 | 188 | // KEK is generated on HSM. We never know its value, just it's alias name. 189 | private static SecretKey createNewHsm3desKek() throws GeneralSecurityException { 190 | 191 | if (HsmManager.hasSavedKey(KEK_ALIAS)) { 192 | HsmManager.deleteKey(KEK_ALIAS); 193 | } 194 | 195 | KeyGenerator kg = KeyGenerator.getInstance("DESede", "LunaProvider"); 196 | kg.init(256); 197 | LunaSecretKey kek = (LunaSecretKey) kg.generateKey(); 198 | 199 | HsmManager.saveKey(KEK_ALIAS, kek); 200 | 201 | return kek; 202 | } 203 | 204 | 205 | private static SecretKey getExistingHsmKek() throws GeneralSecurityException { 206 | // casting KEY -> SecretKey 207 | return (SecretKey) HsmManager.getSavedKey(KEK_ALIAS); 208 | } 209 | 210 | 211 | // create software-only (not stored on HSM) key 212 | private static SecretKey createSoftwareKey(String keyType) throws NoSuchAlgorithmException { 213 | KeyGenerator generator = KeyGenerator.getInstance(keyType); 214 | generator.init(new SecureRandom()); 215 | return generator.generateKey(); 216 | } 217 | 218 | 219 | // wrapping operation is performed on the HSM 220 | private static byte[] wrapKeyWithKek(SecretKey hsmKek, byte[] wrappingIv, SecretKey keyToBeWrapped) throws GeneralSecurityException { 221 | Cipher wrappingCipher = Cipher.getInstance("DESede/CBC/PKCS5Padding", "LunaProvider"); 222 | AlgorithmParameters algParams = AlgorithmParameters.getInstance("IV", "LunaProvider"); 223 | algParams.init(new IvParameterSpec(wrappingIv)); 224 | wrappingCipher.init(Cipher.WRAP_MODE, hsmKek, algParams); 225 | return wrappingCipher.wrap(keyToBeWrapped); 226 | } 227 | 228 | 229 | private static SecretKey unwrapKeyWithKek(SecretKey hsmKek, String wrappedKeyAlgo, byte[] wrappedKeyIv, byte[] wrappedKeyBytes) throws GeneralSecurityException { 230 | Cipher wrappingCipher = Cipher.getInstance("DESede/CBC/PKCS5Padding", "LunaProvider"); 231 | AlgorithmParameters algParams = AlgorithmParameters.getInstance("IV", "LunaProvider"); 232 | algParams.init(new IvParameterSpec(wrappedKeyIv)); 233 | wrappingCipher.init(Cipher.UNWRAP_MODE, hsmKek, algParams); 234 | return (SecretKey) wrappingCipher.unwrap(wrappedKeyBytes, wrappedKeyAlgo, Cipher.SECRET_KEY); 235 | } 236 | 237 | 238 | private static String getHex(byte array[]) { 239 | return "0x" + LunaUtils.getHexString(array, false).toUpperCase(); 240 | } 241 | 242 | } 243 | --------------------------------------------------------------------------------