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 {&nbsp;&nbsp;
  59  *   CK_ATTRIBUTE_TYPE type;&nbsp;&nbsp;
  60  *   CK_VOID_PTR pValue;&nbsp;&nbsp;
  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 SENSITIVE_TRUE =
  82                                     new CK_ATTRIBUTE(CKA_SENSITIVE, true);
  83 
  84     public final static CK_ATTRIBUTE EXTRACTABLE_TRUE =
  85                                     new CK_ATTRIBUTE(CKA_EXTRACTABLE, true);
  86 
  87     public final static CK_ATTRIBUTE ENCRYPT_TRUE =
  88                                     new CK_ATTRIBUTE(CKA_ENCRYPT, true);
  89 
  90     public final static CK_ATTRIBUTE DECRYPT_TRUE =
  91                                     new CK_ATTRIBUTE(CKA_DECRYPT, true);
  92 
  93     public final static CK_ATTRIBUTE WRAP_TRUE =
  94                                     new CK_ATTRIBUTE(CKA_WRAP, true);
  95 
  96     public final static CK_ATTRIBUTE UNWRAP_TRUE =
  97                                     new CK_ATTRIBUTE(CKA_UNWRAP, true);
  98 
  99     public final static CK_ATTRIBUTE SIGN_TRUE =
 100                                     new CK_ATTRIBUTE(CKA_SIGN, true);
 101 
 102     public final static CK_ATTRIBUTE VERIFY_TRUE =
 103                                     new CK_ATTRIBUTE(CKA_VERIFY, true);
 104 
 105     public final static CK_ATTRIBUTE SIGN_RECOVER_TRUE =
 106                                     new CK_ATTRIBUTE(CKA_SIGN_RECOVER, true);
 107 
 108     public final static CK_ATTRIBUTE VERIFY_RECOVER_TRUE =
 109                                     new CK_ATTRIBUTE(CKA_VERIFY_RECOVER, true);
 110 
 111     public final static CK_ATTRIBUTE DERIVE_TRUE =
 112                                     new CK_ATTRIBUTE(CKA_DERIVE, true);
 113 
 114     public final static CK_ATTRIBUTE ENCRYPT_NULL =
 115                                     new CK_ATTRIBUTE(CKA_ENCRYPT);
 116 
 117     public final static CK_ATTRIBUTE DECRYPT_NULL =
 118                                     new CK_ATTRIBUTE(CKA_DECRYPT);
 119 
 120     public final static CK_ATTRIBUTE WRAP_NULL =
 121                                     new CK_ATTRIBUTE(CKA_WRAP);
 122 
 123     public final static CK_ATTRIBUTE UNWRAP_NULL =
 124                                     new CK_ATTRIBUTE(CKA_UNWRAP);
 125 
 126     public CK_ATTRIBUTE() {
 127         // empty
 128     }
 129 
 130     public CK_ATTRIBUTE(long type) {
 131         this.type = type;
 132     }
 133 
 134     public CK_ATTRIBUTE(long type, Object pValue) {
 135         this.type = type;
 136         this.pValue = pValue;
 137     }
 138 
 139     public CK_ATTRIBUTE(long type, boolean value) {
 140         this.type = type;
 141         this.pValue = Boolean.valueOf(value);
 142     }
 143 
 144     public CK_ATTRIBUTE(long type, long value) {
 145         this.type = type;
 146         this.pValue = Long.valueOf(value);
 147     }
 148 
 149     public CK_ATTRIBUTE(long type, BigInteger value) {
 150         this.type = type;
 151         this.pValue = sun.security.pkcs11.P11Util.getMagnitude(value);
 152     }
 153 
 154     public BigInteger getBigInteger() {
 155         if (pValue instanceof byte[] == false) {
 156             throw new RuntimeException("Not a byte[]");
 157         }
 158         return new BigInteger(1, (byte[])pValue);
 159     }
 160 
 161     public boolean getBoolean() {
 162         if (pValue instanceof Boolean == false) {
 163             throw new RuntimeException
 164                 ("Not a Boolean: " + pValue.getClass().getName());
 165         }
 166         return ((Boolean)pValue).booleanValue();
 167     }
 168 
 169     public char[] getCharArray() {
 170         if (pValue instanceof char[] == false) {
 171             throw new RuntimeException("Not a char[]");
 172         }
 173         return (char[])pValue;
 174     }
 175 
 176     public byte[] getByteArray() {
 177         if (pValue instanceof byte[] == false) {
 178             throw new RuntimeException("Not a byte[]");
 179         }
 180         return (byte[])pValue;
 181     }
 182 
 183     public long getLong() {
 184         if (pValue instanceof Long == false) {
 185             throw new RuntimeException
 186                 ("Not a Long: " + pValue.getClass().getName());
 187         }
 188         return ((Long)pValue).longValue();
 189     }
 190 
 191     /**
 192      * <B>PKCS#11:</B>
 193      * <PRE>
 194      *   CK_ATTRIBUTE_TYPE type;
 195      * </PRE>
 196      */
 197     public long type;
 198 
 199     /**
 200      * <B>PKCS#11:</B>
 201      * <PRE>
 202      *   CK_VOID_PTR pValue;
 203      *   CK_ULONG ulValueLen;
 204      * </PRE>
 205      */
 206     public Object pValue;
 207 
 208     /**
 209      * Returns the string representation of CK_ATTRIBUTE.
 210      *
 211      * @return the string representation of CK_ATTRIBUTE
 212      */
 213     public String toString() {
 214         String prefix = Functions.getAttributeName(type) + " = ";
 215         if (type == CKA_CLASS) {
 216             return prefix + Functions.getObjectClassName(getLong());
 217         } else if (type == CKA_KEY_TYPE) {
 218             return prefix + Functions.getKeyName(getLong());
 219         } else {
 220             String s;
 221             if (pValue instanceof char[]) {
 222                 s = new String((char[])pValue);
 223             } else if (pValue instanceof byte[]) {
 224                 s = Functions.toHexString((byte[])pValue);
 225             } else {
 226                 s = String.valueOf(pValue);
 227             }
 228             return prefix + s;
 229         }
 230     }
 231 
 232 }