1 /* 2 * Copyright (c) 2003, 2019, Oracle and/or its affiliates. All rights reserved. 3 */ 4 5 /* Copyright (c) 2002 Graz University of Technology. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions are met: 9 * 10 * 1. Redistributions of source code must retain the above copyright notice, 11 * this list of conditions and the following disclaimer. 12 * 13 * 2. Redistributions in binary form must reproduce the above copyright notice, 14 * this list of conditions and the following disclaimer in the documentation 15 * and/or other materials provided with the distribution. 16 * 17 * 3. The end-user documentation included with the redistribution, if any, must 18 * include the following acknowledgment: 19 * 20 * "This product includes software developed by IAIK of Graz University of 21 * Technology." 22 * 23 * Alternately, this acknowledgment may appear in the software itself, if 24 * and wherever such third-party acknowledgments normally appear. 25 * 26 * 4. The names "Graz University of Technology" and "IAIK of Graz University of 27 * Technology" must not be used to endorse or promote products derived from 28 * this software without prior written permission. 29 * 30 * 5. Products derived from this software may not be called 31 * "IAIK PKCS Wrapper", nor may "IAIK" appear in their name, without prior 32 * written permission of Graz University of Technology. 33 * 34 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED 35 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 36 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 37 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE LICENSOR BE 38 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, 39 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 40 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, 41 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 42 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 43 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 44 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 45 * POSSIBILITY OF SUCH DAMAGE. 46 */ 47 48 package sun.security.pkcs11.wrapper; 49 50 import java.math.BigInteger; 51 import static sun.security.pkcs11.wrapper.PKCS11Constants.*; 52 53 /** 54 * class CK_MECHANISM specifies a particular mechanism and any parameters it 55 * requires.<p> 56 * <B>PKCS#11 structure:</B> 57 * <PRE> 58 * typedef struct CK_MECHANISM { 59 * CK_MECHANISM_TYPE mechanism; 60 * CK_VOID_PTR pParameter; 61 * CK_ULONG ulParameterLen; 62 * } CK_MECHANISM; 63 * </PRE> 64 * 65 * @author Karl Scheibelhofer <Karl.Scheibelhofer@iaik.at> 66 * @author Martin Schlaeffer <schlaeff@sbox.tugraz.at> 67 */ 68 public class CK_MECHANISM { 69 70 /** 71 * <B>PKCS#11:</B> 72 * <PRE> 73 * CK_MECHANISM_TYPE mechanism; 74 * </PRE> 75 */ 76 public long mechanism; 77 78 /** 79 * <B>PKCS#11:</B> 80 * <PRE> 81 * CK_VOID_PTR pParameter; 82 * CK_ULONG ulParameterLen; 83 * </PRE> 84 */ 85 public Object pParameter = null; 86 87 // pointer to native CK_MECHANISM structure 88 // For mechanisms which have only mechanism id, the native structure 89 // can be freed right after init and this field will not be used. However, 90 // for mechanisms which have both mechanism id and parameters, it can 91 // only be freed after operation is finished. Thus, the native pointer 92 // will be stored here and then be explicitly freed by caller. 93 private long pHandle = 0L; 94 CK_MECHANISM(long mechanism)95 public CK_MECHANISM(long mechanism) { 96 this.mechanism = mechanism; 97 } 98 99 // We don't have a (long,Object) constructor to force type checking. 100 // This makes sure we don't accidentally pass a class that the native 101 // code cannot handle. CK_MECHANISM(long mechanism, byte[] pParameter)102 public CK_MECHANISM(long mechanism, byte[] pParameter) { 103 init(mechanism, pParameter); 104 } 105 CK_MECHANISM(long mechanism, BigInteger b)106 public CK_MECHANISM(long mechanism, BigInteger b) { 107 init(mechanism, sun.security.pkcs11.P11Util.getMagnitude(b)); 108 } 109 CK_MECHANISM(long mechanism, CK_VERSION version)110 public CK_MECHANISM(long mechanism, CK_VERSION version) { 111 init(mechanism, version); 112 } 113 CK_MECHANISM(long mechanism, CK_SSL3_MASTER_KEY_DERIVE_PARAMS params)114 public CK_MECHANISM(long mechanism, CK_SSL3_MASTER_KEY_DERIVE_PARAMS params) { 115 init(mechanism, params); 116 } 117 CK_MECHANISM(long mechanism, CK_TLS12_MASTER_KEY_DERIVE_PARAMS params)118 public CK_MECHANISM(long mechanism, CK_TLS12_MASTER_KEY_DERIVE_PARAMS params) { 119 init(mechanism, params); 120 } 121 CK_MECHANISM(long mechanism, CK_SSL3_KEY_MAT_PARAMS params)122 public CK_MECHANISM(long mechanism, CK_SSL3_KEY_MAT_PARAMS params) { 123 init(mechanism, params); 124 } 125 CK_MECHANISM(long mechanism, CK_TLS12_KEY_MAT_PARAMS params)126 public CK_MECHANISM(long mechanism, CK_TLS12_KEY_MAT_PARAMS params) { 127 init(mechanism, params); 128 } 129 CK_MECHANISM(long mechanism, CK_TLS_PRF_PARAMS params)130 public CK_MECHANISM(long mechanism, CK_TLS_PRF_PARAMS params) { 131 init(mechanism, params); 132 } 133 CK_MECHANISM(long mechanism, CK_TLS_MAC_PARAMS params)134 public CK_MECHANISM(long mechanism, CK_TLS_MAC_PARAMS params) { 135 init(mechanism, params); 136 } 137 CK_MECHANISM(long mechanism, CK_ECDH1_DERIVE_PARAMS params)138 public CK_MECHANISM(long mechanism, CK_ECDH1_DERIVE_PARAMS params) { 139 init(mechanism, params); 140 } 141 CK_MECHANISM(long mechanism, Long params)142 public CK_MECHANISM(long mechanism, Long params) { 143 init(mechanism, params); 144 } 145 CK_MECHANISM(long mechanism, CK_AES_CTR_PARAMS params)146 public CK_MECHANISM(long mechanism, CK_AES_CTR_PARAMS params) { 147 init(mechanism, params); 148 } 149 CK_MECHANISM(long mechanism, CK_GCM_PARAMS params)150 public CK_MECHANISM(long mechanism, CK_GCM_PARAMS params) { 151 init(mechanism, params); 152 } 153 CK_MECHANISM(long mechanism, CK_CCM_PARAMS params)154 public CK_MECHANISM(long mechanism, CK_CCM_PARAMS params) { 155 init(mechanism, params); 156 } 157 158 // For PSS. the parameter may be set multiple times, use the 159 // CK_MECHANISM(long) constructor and setParameter(CK_RSA_PKCS_PSS_PARAMS) 160 // methods instead of creating yet another constructor setParameter(CK_RSA_PKCS_PSS_PARAMS params)161 public void setParameter(CK_RSA_PKCS_PSS_PARAMS params) { 162 assert(this.mechanism == CKM_RSA_PKCS_PSS); 163 assert(params != null); 164 if (this.pParameter != null && this.pParameter.equals(params)) { 165 return; 166 } 167 freeHandle(); 168 this.pParameter = params; 169 } 170 freeHandle()171 public void freeHandle() { 172 if (this.pHandle != 0L) { 173 this.pHandle = PKCS11.freeMechanism(pHandle); 174 } 175 } 176 init(long mechanism, Object pParameter)177 private void init(long mechanism, Object pParameter) { 178 this.mechanism = mechanism; 179 this.pParameter = pParameter; 180 } 181 182 /** 183 * Returns the string representation of CK_MECHANISM. 184 * 185 * @return the string representation of CK_MECHANISM 186 */ toString()187 public String toString() { 188 StringBuilder sb = new StringBuilder(); 189 190 sb.append(Constants.INDENT); 191 sb.append("mechanism: "); 192 sb.append(mechanism); 193 sb.append(Constants.NEWLINE); 194 195 sb.append(Constants.INDENT); 196 sb.append("pParameter: "); 197 sb.append(pParameter.toString()); 198 sb.append(Constants.NEWLINE); 199 200 /* 201 sb.append(Constants.INDENT); 202 sb.append("ulParameterLen: ??"); 203 sb.append(Constants.NEWLINE); 204 */ 205 if (pHandle != 0L) { 206 sb.append(Constants.INDENT); 207 sb.append("pHandle: "); 208 sb.append(pHandle); 209 sb.append(Constants.NEWLINE); 210 } 211 return sb.toString() ; 212 } 213 } 214