summaryrefslogtreecommitdiffstats
path: root/pki/base/tps/src/main/PKCS11Obj.cpp
blob: 061dc7a91ab888fa490a74925f44e50994cc679b (plain)
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
// --- BEGIN COPYRIGHT BLOCK ---
// 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 Street, Fifth Floor,
// Boston, MA  02110-1301  USA 
// 
// Copyright (C) 2007 Red Hat, Inc.
// All rights reserved.
// --- END COPYRIGHT BLOCK ---

#include <string.h>
#include "prmem.h"
#include "pk11func.h"
#include "zlib.h"
#include "engine/RA.h"
#include "main/Buffer.h"
#include "main/PKCS11Obj.h"

#ifdef XP_WIN32
#define TPS_PUBLIC __declspec(dllexport)
#else /* !XP_WIN32 */
#define TPS_PUBLIC
#endif /* !XP_WIN32 */

PKCS11Obj::PKCS11Obj ()
{
	for (int i = 0; i < MAX_OBJECT_SPEC; i++) {
			m_objSpec[i] = NULL;
	}
}

PKCS11Obj::~PKCS11Obj ()
{
	for (int i = 0; i < MAX_OBJECT_SPEC; i++) {
		if (m_objSpec[i] != NULL) {
			delete m_objSpec[i];
			m_objSpec[i] = NULL;
		}
	}
}

PKCS11Obj *PKCS11Obj::Parse(Buffer *b, int offset)
{
	PKCS11Obj *o = new PKCS11Obj();

	unsigned short formatVersion = (((BYTE *)*b)[offset + 0] << 8) + 
		(((BYTE *)*b)[offset + 1]);
	o->SetFormatVersion(formatVersion);
	unsigned short objectVersion = (((BYTE *)*b)[offset + 2] << 8) + 

		(((BYTE *)*b)[offset + 3]);
	o->SetObjectVersion(objectVersion);
	o->SetCUID(b->substr(offset + 4, 10));

	unsigned short compressionType = 
		(((BYTE *)*b)[offset + 14] << 8) + (((BYTE *)*b)[offset + 15]);
	unsigned short compressedDataSize = 
		(((BYTE *)*b)[offset + 16] << 8) + (((BYTE *)*b)[offset + 17]);
#if 0
	unsigned short compressedDataOffset = 
		(unsigned short)(((unsigned char *)*b)[offset + 18] << 8) + (((unsigned char *)*b)[offset + 19]);
#endif

	Buffer data;
       	if (compressionType == 0) { /* no compression */
           data = b->substr(offset + 20, compressedDataSize);
       	} else if (compressionType == 1) { /* zlib */
           Buffer compressedData = b->substr(offset + 20, compressedDataSize);

#define MAX_UNCOMPRESS_SIZE 20000
	   unsigned char buf[MAX_UNCOMPRESS_SIZE];
           int rc = 0;
           uLong len = MAX_UNCOMPRESS_SIZE;
           rc = uncompress((Bytef*)buf, (uLongf*)&len, 
			   (Bytef*)((BYTE*)compressedData), 
			   (uLong)compressedData.size());
           RA::Debug("PKCS11Obj::Parse","uncompress ret=%d",rc);
	   data = Buffer(buf,(unsigned int) len);
       	} else {
		/* error */
       	}


	unsigned short objOffset = (((BYTE *)data)[0] << 8) + 
		((BYTE *)data)[1];
	unsigned short objCount = (((BYTE *)data)[2] << 8) + 
		((BYTE *)data)[3];
	Buffer tokenName = data.substr(5, ((BYTE *)data)[4]);
	o->SetTokenName(tokenName);

        RA::Debug("PKCS11Obj::Parse", "objcount = %d", objCount);

	int curpos = (int)objOffset;
	int nread = 0;
	for (int i = 0; i < objCount; i++) {
                RA::Debug("PKCS11Obj::Parse", "working on object %d", i);                
		ObjectSpec *objSpec = ObjectSpec::Parse(&data, curpos, &nread);
                if(!objSpec)
                    continue;
		o->AddObjectSpec(objSpec);

		unsigned long oid = objSpec->GetObjectID();
		char b[2];

		b[0] = (char)((oid >> 24) & 0xff);
		b[1] = (char)((oid >> 16) & 0xff);

                RA::Debug("PKCS11Obj::Parse", "About to parse = %c%c", b[0],b[1]);

		// add corresponding 'C' object for 'c'
	        if (b[0] == 'c') { 
			for (int j = 0; j < objSpec->GetAttributeSpecCount();
						j++) {
				AttributeSpec *as = objSpec->GetAttributeSpec(j);
				if (as->GetAttributeID() == CKA_VALUE) {
				  if (as->GetType() == (BYTE) 0) {
                                        Buffer cert = as->GetValue();

					unsigned long certid = 
						('C' << 24) + (b[1] << 16);
					ObjectSpec *certSpec = 
						ObjectSpec::ParseFromTokenData(
						certid, &cert);
					o->AddObjectSpec(certSpec);

					objSpec->RemoveAttributeSpec(j);
					break;
				  }
				}
			}

		}	

		Buffer objSpecData = objSpec->GetData();
		curpos += nread;
	}

	return o;
}


void PKCS11Obj::SetFormatVersion(unsigned short v)
{
	m_formatVersion = v;
}

void PKCS11Obj::SetObjectVersion(unsigned short v)
{
	m_objectVersion = v;
}

unsigned short PKCS11Obj::GetFormatVersion()
{
	return m_formatVersion;
}

unsigned short PKCS11Obj::GetObjectVersion()
{
	return m_objectVersion;
}

void PKCS11Obj::SetCUID(Buffer CUID)
{
	m_CUID = CUID;
}

Buffer PKCS11Obj::GetCUID()
{
	return m_CUID;
}

void PKCS11Obj::SetTokenName(Buffer tokenName)
{
	m_tokenName = tokenName;
}

Buffer PKCS11Obj::GetTokenName()
{
	return m_tokenName;
}

int PKCS11Obj::GetObjectSpecCount()
{
	for (int i = 0; i < MAX_OBJECT_SPEC; i++) {
		if (m_objSpec[i] == NULL) {
			return i;
		}
	}
        return 0;
}

ObjectSpec *PKCS11Obj::GetObjectSpec(int p)
{
	if (p < MAX_OBJECT_SPEC) {
		if (m_objSpec[p] != NULL) {
			return m_objSpec[p];
		}
	}
        return NULL;
}

void PKCS11Obj::AddObjectSpec(ObjectSpec *p)
{
        for (int i = 0; i < MAX_OBJECT_SPEC; i++) {
		if (m_objSpec[i] == NULL) {
			m_objSpec[i] = p;
			return;
		} else {
			// check duplicated
		        if (p->GetObjectID() == m_objSpec[i]->GetObjectID()) {
				delete m_objSpec[i];
				m_objSpec[i] = p;
				return;
			}	
		}
	}
}

void PKCS11Obj::RemoveObjectSpec(int p)
{
	if (p < MAX_OBJECT_SPEC) {
		if (m_objSpec[p] != NULL) {
			delete m_objSpec[p];
			m_objSpec[p] = NULL;
		}
		// fill hole
		int empty = p;
	        for (int x = p+1; x < MAX_OBJECT_SPEC; x++) {
			if (m_objSpec[x] != NULL) {
				m_objSpec[empty] = m_objSpec[x];
				m_objSpec[x] = NULL;
				empty++;
			}
		}	
	}
}

Buffer PKCS11Obj::GetData()
{
	Buffer data = Buffer();

	unsigned short objectOffset = m_tokenName.size() + 2 + 3;
	data += Buffer(1, (objectOffset >> 8) & 0xff);
	data += Buffer(1, objectOffset & 0xff);
	unsigned short objectCount = GetObjectSpecCount();
	unsigned short objectCountX = objectCount;
	if (objectCountX == 0) {
		objectCountX = 0;
	} else {
		objectCountX = objectCountX - (objectCountX / 4);
	}
	data += Buffer(1, (objectCountX >> 8) & 0xff);
	data += Buffer(1, objectCountX & 0xff);
	data += Buffer(1, m_tokenName.size() & 0xff);
	data += m_tokenName;
	for (int i = 0; i < objectCount; i++) {
	    ObjectSpec *spec = GetObjectSpec(i);
	    unsigned long objectID = spec->GetObjectID();
	    char c = (char)((objectID >> 24) & 0xff);
	    unsigned long fixedAttrs = spec->GetFixedAttributes();
	    unsigned int xclass = (fixedAttrs & 0x70) >> 4;
            char cont_id = (char) ((objectID >> 16) & 0xff);
	    unsigned int id = (fixedAttrs & 0x0f);
	    /* locate all certificate objects */
	    if (c == 'c' && xclass == CKO_CERTIFICATE) {

                //We need to use the container id, there may be more than one cert
                //with the same CKA_ID byte

                id = (unsigned int) (cont_id - '0');

		/* locate the certificate object */
	        for (int u = 0; u < objectCount; u++) {
	    		ObjectSpec *u_spec = GetObjectSpec(u);
	    		unsigned long u_objectID = u_spec->GetObjectID();
	    		char u_c = (char)((u_objectID >> 24) & 0xff);
	    		unsigned long u_fixedAttrs = 
				u_spec->GetFixedAttributes();
	    		unsigned int u_xclass = (u_fixedAttrs & 0x70) >> 4;
	    		unsigned int u_id = (u_fixedAttrs & 0x0f);
	    		if (u_c == 'C' && u_xclass == CKO_CERTIFICATE && u_id == id) {
	    			AttributeSpec * u_attr = 
					u_spec->GetAttributeSpec(0);
	    		AttributeSpec * n_attr = new AttributeSpec();
                n_attr->SetAttributeID(u_attr->GetAttributeID());
                n_attr->SetType(u_attr->GetType());
                n_attr->SetData(u_attr->GetValue());
				spec->AddAttributeSpec(n_attr);
			}
		}

	    	data += spec->GetData();

		/* locate public object */
	        for (int x = 0; x < objectCount; x++) {
	    		ObjectSpec *x_spec = GetObjectSpec(x);
	    		unsigned long x_fixedAttrs = 
				x_spec->GetFixedAttributes();
	    		unsigned int x_xclass = (x_fixedAttrs & 0x70) >> 4;
	    		unsigned int x_id = (x_fixedAttrs & 0x0f);
	    		if (x_xclass == CKO_PUBLIC_KEY && x_id == id) {
	    			data += x_spec->GetData();
			}
		}

		/* locate private object */
	        for (int y = 0; y < objectCount; y++) {
	    		ObjectSpec *y_spec = GetObjectSpec(y);
	    		unsigned long y_fixedAttrs = 
				y_spec->GetFixedAttributes();
	    		unsigned int y_xclass = (y_fixedAttrs & 0x70) >> 4;
	    		unsigned int y_id = (y_fixedAttrs & 0x0f);
	    		if (y_xclass == CKO_PRIVATE_KEY && y_id == id) {
	    			data += y_spec->GetData();
			}
		}
	    }
	}

	Buffer header = Buffer();
	header += Buffer(1, (m_formatVersion >> 8) & 0xff);
	header += Buffer(1, m_formatVersion & 0xff);
	header += Buffer(1, (m_objectVersion >> 8) & 0xff);
	header += Buffer(1, m_objectVersion & 0xff);
	header += m_CUID;
	// COMP_NONE = 0x00
	// COMP_ZLIB = 0x01
	unsigned short compressionType = 0x00;
	header += Buffer(1, (compressionType >> 8) & 0xff);
	header += Buffer(1, compressionType & 0xff);
	unsigned short compressedDataSize = data.size();
	header += Buffer(1, (compressedDataSize >> 8) & 0xff);
	header += Buffer(1, compressedDataSize & 0xff);
	unsigned short compressedDataOffset = 20;
	header += Buffer(1, (compressedDataOffset >> 8) & 0xff);
	header += Buffer(1, compressedDataOffset & 0xff);

	return header + data;
}

Buffer PKCS11Obj::GetCompressedData()
{
	Buffer data = Buffer();
        Buffer error = Buffer(0);

	unsigned short objectOffset = m_tokenName.size() + 2 + 3;
	data += Buffer(1, (objectOffset >> 8) & 0xff);
	data += Buffer(1, objectOffset & 0xff);
	unsigned short objectCount = GetObjectSpecCount();
	unsigned short objectCountX = objectCount;
	if (objectCountX == 0) {
		objectCountX = 0;
	} else {
		objectCountX = objectCountX - (objectCountX / 4);
	}
	data += Buffer(1, (objectCountX >> 8) & 0xff);
	data += Buffer(1, objectCountX & 0xff);
	data += Buffer(1, m_tokenName.size() & 0xff);
	data += m_tokenName;
        RA::Debug("PKCS11Obj::GetCompressedData", "object count = %d", objectCount);
	for (int i = 0; i < objectCount; i++) {
	    ObjectSpec *spec = GetObjectSpec(i);
	    unsigned long objectID = spec->GetObjectID();
            RA::Debug("PKCS11Obj::GetCompressedData", "objid = %lu", objectID);
	    char c = (char)((objectID >> 24) & 0xff);
	    unsigned long fixedAttrs = spec->GetFixedAttributes();
	    unsigned int xclass = (fixedAttrs & 0x70) >> 4;
            char cont_id = (char) ((objectID >> 16) & 0xff);
	    unsigned int id = (fixedAttrs & 0x0f);

	    /* locate all certificate objects */
	    if (c == 'c' && xclass == CKO_CERTIFICATE) {

                //We need to use the container id, there may be more than one cert
                //with the same CKA_ID byte

                id = (unsigned int) (cont_id - '0');

		/* locate the certificate object */
	        for (int u = 0; u < objectCount; u++) {
	    		ObjectSpec *u_spec = GetObjectSpec(u);
	    		unsigned long u_objectID = u_spec->GetObjectID();
	    		char u_c = (char)((u_objectID >> 24) & 0xff);
	    		unsigned long u_fixedAttrs = 
				u_spec->GetFixedAttributes();
	    		unsigned int u_xclass = (u_fixedAttrs & 0x70) >> 4;
	    		unsigned int u_id = (u_fixedAttrs & 0x0f);
                        char cont_u_id = (char) ((u_objectID >>  16) & 0xff);
	    		if (u_c == 'C' && u_xclass == CKO_CERTIFICATE && u_id == id) {
                            RA::Debug("PKCS11Obj::GetCompressedData", "located Certificate id = %d cont_u_id = %c", u_id,cont_u_id);
	    			AttributeSpec * u_attr = 
					u_spec->GetAttributeSpec(0);
	    		AttributeSpec * n_attr = new AttributeSpec();
                n_attr->SetAttributeID(u_attr->GetAttributeID());
                n_attr->SetType(u_attr->GetType());
                n_attr->SetData(u_attr->GetValue());
				spec->AddAttributeSpec(n_attr);
			}
		}

		/* output certificate attribute object */
	    	data += spec->GetData();

		/* locate public object */
	        for (int x = 0; x < objectCount; x++) {
	    		ObjectSpec *x_spec = GetObjectSpec(x);
	    		unsigned long x_fixedAttrs = 
				x_spec->GetFixedAttributes();
	    		unsigned int x_xclass = (x_fixedAttrs & 0x70) >> 4;
	    		unsigned int x_id = (x_fixedAttrs & 0x0f);
	    		if (x_xclass == CKO_PUBLIC_KEY && x_id == id) {
                                RA::Debug("PKCS11Obj::GetCompressedData", "located Public Key = %d", x_id);
	    			data += x_spec->GetData();
			}

		}

		/* locate private object */
	        for (int y = 0; y < objectCount; y++) {
	    		ObjectSpec *y_spec = GetObjectSpec(y);
	    		unsigned long y_fixedAttrs = 
				y_spec->GetFixedAttributes();
	    		unsigned int y_xclass = (y_fixedAttrs & 0x70) >> 4;
	    		unsigned int y_id = (y_fixedAttrs & 0x0f);
	    		if (y_xclass == CKO_PRIVATE_KEY && y_id == id) {
                                RA::Debug("PKCS11Obj::GetCompressedData", "located Private Key = %d", y_id);
	    			data += y_spec->GetData();
			}
		}
	    }
	}

#define MAX_COMPRESS_SIZE 50000 
	char buffer[MAX_COMPRESS_SIZE];
	unsigned long len = MAX_COMPRESS_SIZE ;

    int rc = 0;
  
    RA::Debug("PKCS11Obj", "before compress length = %d", len);

    BYTE *src_buffer = (BYTE*)data;

    RA::Debug("PKCS11Obj", "sizeof src_buffer = %d", sizeof(src_buffer));
    RA::Debug("PKCS11Obj", "data size = %d", data.size());

    rc = compress((Bytef*)buffer, (uLongf*)&len, (Bytef*)src_buffer,
               (uLong)data.size());


    if(rc != Z_OK) {
        RA::Debug("PKCS11Obj", "failure compressing data, possibly buffer overrun! Error: %d ",rc);

        return error;
    }

    RA::Debug("PKCS11Obj", "after compress length = %d", len);
    RA::Debug("PKCS11Obj", "rc = %d", rc);

	Buffer compressedData = Buffer((BYTE*)buffer, len);

	Buffer header = Buffer();
	header += Buffer(1, (m_formatVersion >> 8) & 0xff);
	header += Buffer(1, m_formatVersion & 0xff);
	header += Buffer(1, (m_objectVersion >> 8) & 0xff);
	header += Buffer(1, m_objectVersion & 0xff);
	header += m_CUID;
	// COMP_NONE = 0x00
	// COMP_ZLIB = 0x01
	unsigned short compressionType = 0x01;
	header += Buffer(1, (compressionType >> 8) & 0xff);
	header += Buffer(1, compressionType & 0xff);
	unsigned short compressedDataSize = compressedData.size();
	header += Buffer(1, (compressedDataSize >> 8) & 0xff);
	header += Buffer(1, compressedDataSize & 0xff);
	unsigned short compressedDataOffset = 20;
	header += Buffer(1, (compressedDataOffset >> 8) & 0xff);
	header += Buffer(1, compressedDataOffset & 0xff);

	return header + compressedData;
}