[go: up one dir, main page]

File: object.h

package info (click to toggle)
coolkey 1.1.0-6
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 2,156 kB
  • ctags: 2,124
  • sloc: sh: 8,397; cpp: 6,598; ansic: 3,991; makefile: 181
file content (205 lines) | stat: -rw-r--r-- 6,337 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/* ***** BEGIN COPYRIGHT BLOCK *****
 * Copyright (C) 2005 Red Hat, Inc.
 * All rights reserved.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation version
 * 2.1 of the License.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 * ***** END COPYRIGHT BLOCK *****/

#ifndef COOLKEY_OBJECT_H
#define COOLKEY_OBJECT_H

#include "mypkcs11.h"
#include "cky_base.h"
#include <list>
#include "log.h"

using std::list;

class PKCS11Attribute {
  private:
    CK_ATTRIBUTE_TYPE type;
    CKYBuffer value;

  public:
    const CKYBuffer *getValue() const { return &value; }
    CK_ATTRIBUTE_TYPE getType() const {return type; }
    void setValue(const CKYByte *data, CKYSize size) {
	CKYBuffer_Replace(&value, 0, data, size);
    }
    void setType(CK_ATTRIBUTE_TYPE type_) { type = type_; }
    PKCS11Attribute(const PKCS11Attribute &cpy) {
	type = cpy.type;
	CKYBuffer_InitFromCopy(&value, &cpy.value);
    }
    PKCS11Attribute &operator=(PKCS11Attribute &cpy) {
	type = cpy.type;
	CKYBuffer_Replace(&value, 0, CKYBuffer_Data(&cpy.value),
				CKYBuffer_Size(&cpy.value));
	return *this;
    }
    PKCS11Attribute() { CKYBuffer_InitEmpty(&value); }
    PKCS11Attribute(CK_ATTRIBUTE_TYPE type_, const CKYBuffer *value_)
        : type(type_) { CKYBuffer_InitFromCopy(&value, value_); }
    ~PKCS11Attribute() { CKYBuffer_FreeData(&value); }
};

class PKCS11Object {
  public:

    typedef list<PKCS11Attribute> AttributeList;
    typedef AttributeList::iterator AttributeIter;
    typedef AttributeList::const_iterator AttributeConstIter;

  private:
    AttributeList attributes;
    unsigned long muscleObjID;
    CK_OBJECT_HANDLE handle;
    char *label;

    void parseOldObject(const CKYBuffer *data);
    void parseNewObject(const CKYBuffer *data);
    void expandAttributes(unsigned long fixedAttrs);

    PKCS11Object &operator=(PKCS11Object &cpy) { return *this; } //Disallow

  protected :
    CKYBuffer pubKey; 
    char *name;

  public:
    PKCS11Object(unsigned long muscleObjID, CK_OBJECT_HANDLE handle);
    PKCS11Object(unsigned long muscleObjID, const CKYBuffer *data,
        CK_OBJECT_HANDLE handle);
    ~PKCS11Object() { delete label; delete name; CKYBuffer_FreeData(&pubKey); }

    PKCS11Object(const PKCS11Object& cpy) :
        attributes(cpy.attributes), muscleObjID(cpy.muscleObjID),
        handle(cpy.handle), label(NULL),  name(NULL) { 
			CKYBuffer_InitFromCopy(&pubKey,&cpy.pubKey); }


    unsigned long getMuscleObjID() const { return muscleObjID; }
    const CK_OBJECT_HANDLE getHandle() const { return handle; }

    /* PKCS11Attribute* getAttribute(CK_ATTRIBUTE_TYPE type); */
    const char *getLabel();
    CK_OBJECT_CLASS getClass();
    const char *getName() { return name; }

    void setAttribute(CK_ATTRIBUTE_TYPE type, const CKYBuffer *value);
    void setAttribute(CK_ATTRIBUTE_TYPE type, const char *);
    /* bools and ulongs are too close, don't abuse function overloading
     * for these cases */
    void setAttributeBool(CK_ATTRIBUTE_TYPE type, CK_BBOOL);
    void setAttributeULong(CK_ATTRIBUTE_TYPE type, CK_ULONG);

    void removeAttribute(CK_ATTRIBUTE_TYPE type);

    bool matchesTemplate(CK_ATTRIBUTE_PTR pTemplate, CK_ULONG count) const;

    const CKYBuffer *getAttribute(CK_ATTRIBUTE_TYPE type) const;
    void getAttributeValue(CK_ATTRIBUTE_PTR pTemplate, CK_ULONG ulCount,
        Log* log) const;
    bool attributeExists(CK_ATTRIBUTE_TYPE type) const;
    const CKYBuffer *getPubKey(void) const {
	return &pubKey;
    }
};

class Key : public PKCS11Object {

  public:
    Key(unsigned long muscleObjID, const CKYBuffer *data, CK_OBJECT_HANDLE handle);
    void completeKey(const PKCS11Object &cert);
	
};

class Cert : public PKCS11Object {
  public:
    Cert(unsigned long muscleObjID, const CKYBuffer *data,
        CK_OBJECT_HANDLE handle, const CKYBuffer *derCert);
};

class CACPrivKey : public PKCS11Object {
  public:
    CACPrivKey(CKYByte instance, const PKCS11Object &cert);
};

class CACPubKey : public PKCS11Object {
  public:
    CACPubKey(CKYByte instance, const PKCS11Object &cert);
};

class CACCert : public PKCS11Object {
  public:
    CACCert(CKYByte instance, const CKYBuffer *derCert);
};

class Reader : public PKCS11Object {
  public:
    Reader(unsigned long muscleObjID, CK_OBJECT_HANDLE handle, 
		const char *reader, const CKYBuffer *cardATR, bool isCoolkey);
};

class AttributeMatch {

  private:
    const CK_ATTRIBUTE *attr;
  public:
    AttributeMatch( const CK_ATTRIBUTE *attr_) : attr(attr_) { }

    bool operator()(const PKCS11Attribute& cmp);
};

inline unsigned int
makeLEUInt(const CKYBuffer *buf, unsigned int offset)
{
    /* assert( offset + 4 <= CKYBuffer_Size(buf) ); */
    const CKYByte *b = CKYBuffer_Data(buf);

    return  (b[offset+3] << 24) |
            (b[offset+2] << 16) |
            (b[offset+1] <<  8) |
            (b[offset+0] <<  0) ;
}

// fixed object ID constants 
#define READER_ID 0x72300000 /* 'r0\0\0' */
#define COMBINED_ID 0x7a300000 /* 'z0\0\0' */

const CKYByte COMP_NONE=0;
const CKYByte COMP_ZLIB=1;
const CKYByte DATATYPE_STRING=0;
const CKYByte DATATYPE_INTEGER=1;
const CKYByte DATATYPE_BOOL_FALSE=2;
const CKYByte DATATYPE_BOOL_TRUE=3;

// relative to the header
const CKYOffset OBJ_FORMAT_VERSION_OFFSET = 0;
const CKYOffset OBJ_OBJECT_VERSION_OFFSET = 2;
const CKYOffset OBJ_CUID_OFFSET = 4;
const CKYSize OBJ_CUID_SIZE = 10;
const CKYOffset OBJ_COMP_TYPE_OFFSET = 14;
const CKYOffset OBJ_COMP_SIZE_OFFSET = 16;
const CKYOffset OBJ_COMP_OFFSET_OFFSET = 18;
const CKYSize OBJ_HEADER_SIZE = 20;

// relative to the start of the decompressed block 
const CKYOffset OBJ_OBJECT_OFFSET_OFFSET = 0;
const CKYOffset OBJ_OBJECT_COUNT_OFFSET = 2;
const CKYOffset OBJ_TOKENNAME_SIZE_OFFSET = 4;
const CKYOffset OBJ_TOKENNAME_OFFSET = 5;

#endif