1 /* 2 * Copyright (c) 2003, 2006, 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 52 import static sun.security.pkcs11.wrapper.PKCS11Constants.*; 53 54 /** 55 * class CK_ATTRIBUTE includes the type, value and length of an attribute.<p> 56 * <B>PKCS#11 structure:</B> 57 * <PRE> 58 * typedef struct CK_ATTRIBUTE { 59 * CK_ATTRIBUTE_TYPE type; 60 * CK_VOID_PTR pValue; 61 * CK_ULONG ulValueLen; 62 * } CK_ATTRIBUTE; 63 * </PRE> 64 * 65 * @author Karl Scheibelhofer <Karl.Scheibelhofer@iaik.at> 66 * @author Martin Schlaeffer <schlaeff@sbox.tugraz.at> 67 */ 68 public class CK_ATTRIBUTE { 69 70 // common attributes 71 // NOTE that CK_ATTRIBUTE is a mutable classes but these attributes 72 // *MUST NEVER* be modified, e.g. by using them in a 73 // C_GetAttributeValue() call! 74 75 public final static CK_ATTRIBUTE TOKEN_FALSE = 76 new CK_ATTRIBUTE(CKA_TOKEN, false); 77 78 public final static CK_ATTRIBUTE SENSITIVE_FALSE = 79 new CK_ATTRIBUTE(CKA_SENSITIVE, false); 80 81 public final static CK_ATTRIBUTE EXTRACTABLE_TRUE = 82 new CK_ATTRIBUTE(CKA_EXTRACTABLE, true); 83 84 public final static CK_ATTRIBUTE ENCRYPT_TRUE = 85 new CK_ATTRIBUTE(CKA_ENCRYPT, true); 86 87 public final static CK_ATTRIBUTE DECRYPT_TRUE = 88 new CK_ATTRIBUTE(CKA_DECRYPT, true); 89 90 public final static CK_ATTRIBUTE WRAP_TRUE = 91 new CK_ATTRIBUTE(CKA_WRAP, true); 92 93 public final static CK_ATTRIBUTE UNWRAP_TRUE = 94 new CK_ATTRIBUTE(CKA_UNWRAP, true); 95 96 public final static CK_ATTRIBUTE SIGN_TRUE = 97 new CK_ATTRIBUTE(CKA_SIGN, true); 98 99 public final static CK_ATTRIBUTE VERIFY_TRUE = 100 new CK_ATTRIBUTE(CKA_VERIFY, true); 101 102 public final static CK_ATTRIBUTE SIGN_RECOVER_TRUE = 103 new CK_ATTRIBUTE(CKA_SIGN_RECOVER, true); 104 105 public final static CK_ATTRIBUTE VERIFY_RECOVER_TRUE = 106 new CK_ATTRIBUTE(CKA_VERIFY_RECOVER, true); 107 108 public final static CK_ATTRIBUTE DERIVE_TRUE = 109 new CK_ATTRIBUTE(CKA_DERIVE, true); 110 111 public final static CK_ATTRIBUTE ENCRYPT_NULL = 112 new CK_ATTRIBUTE(CKA_ENCRYPT); 113 114 public final static CK_ATTRIBUTE DECRYPT_NULL = 115 new CK_ATTRIBUTE(CKA_DECRYPT); 116 117 public final static CK_ATTRIBUTE WRAP_NULL = 118 new CK_ATTRIBUTE(CKA_WRAP); 119 120 public final static CK_ATTRIBUTE UNWRAP_NULL = 121 new CK_ATTRIBUTE(CKA_UNWRAP); 122 123 public CK_ATTRIBUTE() { 124 // empty 125 } 126 127 public CK_ATTRIBUTE(long type) { 128 this.type = type; 129 } 130 131 public CK_ATTRIBUTE(long type, Object pValue) { 132 this.type = type; 133 this.pValue = pValue; 134 } 135 136 public CK_ATTRIBUTE(long type, boolean value) { 137 this.type = type; 138 this.pValue = Boolean.valueOf(value); 139 } 140 141 public CK_ATTRIBUTE(long type, long value) { 142 this.type = type; 143 this.pValue = Long.valueOf(value); 144 } 145 146 public CK_ATTRIBUTE(long type, BigInteger value) { 147 this.type = type; 148 this.pValue = sun.security.pkcs11.P11Util.getMagnitude(value); 149 } 150 151 public BigInteger getBigInteger() { 152 if (pValue instanceof byte[] == false) { 153 throw new RuntimeException("Not a byte[]"); 154 } 155 return new BigInteger(1, (byte[])pValue); 156 } 157 158 public boolean getBoolean() { 159 if (pValue instanceof Boolean == false) { 160 throw new RuntimeException 161 ("Not a Boolean: " + pValue.getClass().getName()); 162 } 163 return ((Boolean)pValue).booleanValue(); 164 } 165 166 public char[] getCharArray() { 167 if (pValue instanceof char[] == false) { 168 throw new RuntimeException("Not a char[]"); 169 } 170 return (char[])pValue; 171 } 172 173 public byte[] getByteArray() { 174 if (pValue instanceof byte[] == false) { 175 throw new RuntimeException("Not a byte[]"); 176 } 177 return (byte[])pValue; 178 } 179 180 public long getLong() { 181 if (pValue instanceof Long == false) { 182 throw new RuntimeException 183 ("Not a Long: " + pValue.getClass().getName()); 184 } 185 return ((Long)pValue).longValue(); 186 } 187 188 /** 189 * <B>PKCS#11:</B> 190 * <PRE> 191 * CK_ATTRIBUTE_TYPE type; 192 * </PRE> 193 */ 194 public long type; 195 196 /** 197 * <B>PKCS#11:</B> 198 * <PRE> 199 * CK_VOID_PTR pValue; 200 * CK_ULONG ulValueLen; 201 * </PRE> 202 */ 203 public Object pValue; 204 205 /** 206 * Returns the string representation of CK_ATTRIBUTE. 207 * 208 * @return the string representation of CK_ATTRIBUTE 209 */ 210 public String toString() { 211 String prefix = Functions.getAttributeName(type) + " = "; 212 if (type == CKA_CLASS) { 213 return prefix + Functions.getObjectClassName(getLong()); 214 } else if (type == CKA_KEY_TYPE) { 215 return prefix + Functions.getKeyName(getLong()); 216 } else { 217 String s; 218 if (pValue instanceof char[]) { 219 s = new String((char[])pValue); 220 } else if (pValue instanceof byte[]) { 221 s = Functions.toHexString((byte[])pValue); 222 } else { 223 s = String.valueOf(pValue); 224 } 225 return prefix + s; 226 } 227 } 228 229 }