| 1 |
/* |
| 2 |
* Copyright (c) 2010 SURFnet bv |
| 3 |
* All rights reserved. |
| 4 |
* |
| 5 |
* Redistribution and use in source and binary forms, with or without |
| 6 |
* modification, are permitted provided that the following conditions |
| 7 |
* are met: |
| 8 |
* 1. Redistributions of source code must retain the above copyright |
| 9 |
* notice, this list of conditions and the following disclaimer. |
| 10 |
* 2. Redistributions in binary form must reproduce the above copyright |
| 11 |
* notice, this list of conditions and the following disclaimer in the |
| 12 |
* documentation and/or other materials provided with the distribution. |
| 13 |
* |
| 14 |
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 15 |
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 16 |
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 17 |
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY |
| 18 |
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 19 |
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE |
| 20 |
* GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 21 |
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER |
| 22 |
* IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
| 23 |
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN |
| 24 |
* IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 25 |
*/ |
| 26 |
|
| 27 |
/***************************************************************************** |
| 28 |
OSAttribute.cpp |
| 29 |
|
| 30 |
This class represents the object store view on an object's attribute |
| 31 |
*****************************************************************************/ |
| 32 |
|
| 33 |
#include "config.h" |
| 34 |
#include "OSAttribute.h" |
| 35 |
|
| 36 |
// Copy constructor |
| 37 |
OSAttribute::OSAttribute(const OSAttribute& in) |
| 38 |
{ |
| 39 |
attributeType = in.attributeType; |
| 40 |
boolValue = in.boolValue; |
| 41 |
ulongValue = in.ulongValue; |
| 42 |
byteStrValue = in.byteStrValue; |
| 43 |
mechSetValue = in.mechSetValue; |
| 44 |
attrMapValue = in.attrMapValue; |
| 45 |
} |
| 46 |
|
| 47 |
// Constructor for a boolean type attribute |
| 48 |
OSAttribute::OSAttribute(const bool value) |
| 49 |
{ |
| 50 |
boolValue = value; |
| 51 |
attributeType = BOOL; |
| 52 |
|
| 53 |
ulongValue = 0; |
| 54 |
} |
| 55 |
|
| 56 |
// Constructor for an unsigned long type attribute |
| 57 |
OSAttribute::OSAttribute(const unsigned long value) |
| 58 |
{ |
| 59 |
ulongValue = value; |
| 60 |
attributeType = ULONG; |
| 61 |
|
| 62 |
boolValue = false; |
| 63 |
} |
| 64 |
|
| 65 |
// Constructor for a byte string type attribute |
| 66 |
OSAttribute::OSAttribute(const ByteString& value) |
| 67 |
{ |
| 68 |
byteStrValue = value; |
| 69 |
attributeType = BYTESTR; |
| 70 |
|
| 71 |
boolValue = false; |
| 72 |
ulongValue = 0; |
| 73 |
} |
| 74 |
|
| 75 |
// Constructor for a mechanism type set attribute |
| 76 |
OSAttribute::OSAttribute(const std::set<CK_MECHANISM_TYPE>& value) |
| 77 |
{ |
| 78 |
mechSetValue = value; |
| 79 |
attributeType = MECHSET; |
| 80 |
|
| 81 |
boolValue = false; |
| 82 |
ulongValue = 0; |
| 83 |
} |
| 84 |
|
| 85 |
// Constructor for an attribute map type attribute |
| 86 |
OSAttribute::OSAttribute(const std::map<CK_ATTRIBUTE_TYPE,OSAttribute>& value) |
| 87 |
{ |
| 88 |
attrMapValue = value; |
| 89 |
attributeType = ATTRMAP; |
| 90 |
|
| 91 |
boolValue = false; |
| 92 |
ulongValue = 0; |
| 93 |
} |
| 94 |
|
| 95 |
// Check the attribute type |
| 96 |
bool OSAttribute::isBooleanAttribute() const |
| 97 |
{ |
| 98 |
return (attributeType == BOOL); |
| 99 |
} |
| 100 |
|
| 101 |
bool OSAttribute::isUnsignedLongAttribute() const |
| 102 |
{ |
| 103 |
return (attributeType == ULONG); |
| 104 |
} |
| 105 |
|
| 106 |
bool OSAttribute::isByteStringAttribute() const |
| 107 |
{ |
| 108 |
return (attributeType == BYTESTR); |
| 109 |
} |
| 110 |
|
| 111 |
bool OSAttribute::isMechanismTypeSetAttribute() const |
| 112 |
{ |
| 113 |
return (attributeType == MECHSET); |
| 114 |
} |
| 115 |
|
| 116 |
bool OSAttribute::isAttributeMapAttribute() const |
| 117 |
{ |
| 118 |
return (attributeType == ATTRMAP); |
| 119 |
} |
| 120 |
|
| 121 |
// Retrieve the attribute value |
| 122 |
bool OSAttribute::getBooleanValue() const |
| 123 |
{ |
| 124 |
return boolValue; |
| 125 |
} |
| 126 |
|
| 127 |
unsigned long OSAttribute::getUnsignedLongValue() const |
| 128 |
{ |
| 129 |
return ulongValue; |
| 130 |
} |
| 131 |
|
| 132 |
const ByteString& OSAttribute::getByteStringValue() const |
| 133 |
{ |
| 134 |
return byteStrValue; |
| 135 |
} |
| 136 |
|
| 137 |
const std::set<CK_MECHANISM_TYPE>& OSAttribute::getMechanismTypeSetValue() const |
| 138 |
{ |
| 139 |
return mechSetValue; |
| 140 |
} |
| 141 |
|
| 142 |
const std::map<CK_ATTRIBUTE_TYPE,OSAttribute>& OSAttribute::getAttributeMapValue() const |
| 143 |
{ |
| 144 |
return attrMapValue; |
| 145 |
} |
| 146 |
|
| 147 |
// Helper for template (aka array) matching |
| 148 |
|
| 149 |
bool OSAttribute::peekValue(ByteString& value) const |
| 150 |
{ |
| 151 |
size_t counter = 0; |
| 152 |
CK_MECHANISM_TYPE mech; |
| 153 |
|
| 154 |
switch (attributeType) |
| 155 |
{ |
| 156 |
case BOOL: |
| 157 |
value.resize(sizeof(boolValue)); |
| 158 |
memcpy(&value[0], &boolValue, value.size()); |
| 159 |
return true; |
| 160 |
|
| 161 |
case ULONG: |
| 162 |
value.resize(sizeof(ulongValue)); |
| 163 |
memcpy(&value[0], &ulongValue, value.size()); |
| 164 |
return true; |
| 165 |
|
| 166 |
case BYTESTR: |
| 167 |
value.resize(byteStrValue.size()); |
| 168 |
memcpy(&value[0], byteStrValue.const_byte_str(), value.size()); |
| 169 |
return true; |
| 170 |
|
| 171 |
case MECHSET: |
| 172 |
value.resize(mechSetValue.size() * sizeof(mech)); |
| 173 |
for (std::set<CK_MECHANISM_TYPE>::const_iterator i = mechSetValue.begin(); i != mechSetValue.end(); ++i) |
| 174 |
{ |
| 175 |
mech = *i; |
| 176 |
memcpy(&value[0] + counter * sizeof(mech), &mech, sizeof(mech)); |
| 177 |
counter++; |
| 178 |
} |
| 179 |
return true; |
| 180 |
|
| 181 |
case ATTRMAP: |
| 182 |
return false; |
| 183 |
|
| 184 |
default: |
| 185 |
return false; |
| 186 |
} |
| 187 |
} |