File: pk11_attribute.cpp

package info (click to toggle)
xca 2.9.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 8,328 kB
  • sloc: cpp: 30,584; sh: 341; xml: 74; makefile: 56; python: 34
file content (143 lines) | stat: -rw-r--r-- 3,355 bytes parent folder | download | duplicates (2)
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
/* vi: set sw=4 ts=4:
 *
 * Copyright (C) 2009 - 2010 Christian Hohnstaedt.
 *
 * All rights reserved.
 */

#include "pkcs11.h"
#include "pk11_attribute.h"
#include "exception.h"
#include <QObject>

void pk11_attribute::load(const slotid &slot,
			CK_SESSION_HANDLE sess, CK_OBJECT_HANDLE obj)
{
	CK_RV rv;
	rv = slot.p11()->C_GetAttributeValue(sess, obj, &attr, 1);
	if (rv != CKR_OK)
		pk11error("C_GetAttribute()", rv);
}

void pk11_attr_data::load(const slotid &slot,
			CK_SESSION_HANDLE sess, CK_OBJECT_HANDLE obj)
{
	CK_RV rv;
	if (attr.pValue) {
		free(attr.pValue);
		attr.pValue = NULL;
	}
	attr.ulValueLen = 0;
	rv = slot.p11()->C_GetAttributeValue(sess, obj, &attr, 1);
	if (rv == CKR_OK) {
		attr.pValue = malloc(attr.ulValueLen +1);
		Q_CHECK_PTR(attr.pValue);
		rv = slot.p11()->C_GetAttributeValue(sess, obj, &attr, 1); \
		if (rv == CKR_OK)
			return;
	}
	pk11error("C_GetAttributeValue(data)", rv); \
}

void pk11_attr_data::setValue(const unsigned char *ptr, unsigned long len)
{
	if (attr.pValue)
		free(attr.pValue);
	if (!ptr || len == 0) {
		attr.ulValueLen = 0;
		attr.pValue = NULL;
		return;
	}
	attr.pValue = malloc(len+1);
	Q_CHECK_PTR(attr.pValue);
	memcpy(attr.pValue, ptr, len);
	attr.ulValueLen = len;
	((char*)attr.pValue)[len] = 0;
}

void pk11_attr_data::setConstBignum(const BIGNUM *bn)
{
	attr.ulValueLen = BN_num_bytes(bn);
	if (attr.pValue)
		free(attr.pValue);
	attr.pValue = malloc(attr.ulValueLen);
	Q_CHECK_PTR(attr.pValue);
	attr.ulValueLen = BN_bn2bin(bn, (unsigned char *)attr.pValue);
}

void pk11_attr_data::setBignum(BIGNUM *bn, bool consume)
{
	setConstBignum(bn);
	if (consume)
		BN_free(bn);
}

void pk11_attribute::store(const slotid &slot,
			CK_SESSION_HANDLE sess, CK_OBJECT_HANDLE obj)
{
	CK_RV rv;
	rv = slot.p11()->C_SetAttributeValue(sess, obj, &attr, 1);
	if (rv != CKR_OK)
		pk11error("C_SetAttributeValue", rv);
}

void pk11_attlist::copy(const pk11_attlist &a)
{
	reset();
	attlen = a.attlen;
	alloc_len = a.alloc_len;
	if (alloc_len) {
		attributes =
			(CK_ATTRIBUTE *)malloc(alloc_len *sizeof(*attributes));
		Q_CHECK_PTR(attributes);
		memcpy(attributes, a.attributes, attlen *sizeof(*attributes));
	}
	for (unsigned long i=0; i<attlen; i++) {
		char *p = (char*)malloc(attributes[i].ulValueLen +1);
		Q_CHECK_PTR(p);
		memcpy(p, a.attributes[i].pValue, attributes[i].ulValueLen);
		p[attributes[i].ulValueLen] = '\0';
		attributes[i].pValue = p;
	}
}

pk11_attlist::pk11_attlist(const pk11_attlist &a)
{
	copy(a);
}

pk11_attlist::~pk11_attlist()
{
	for (unsigned long i=0; i<attlen; i++) {
		memset(attributes[i].pValue, 0, attributes[i].ulValueLen);
		free(attributes[i].pValue);
	}
	if (attributes)
		free(attributes);
}

void pk11_attlist::addAttribute(const pk11_attribute &a)
{
	CK_ATTRIBUTE *attr;
	if (attlen == alloc_len) {
		alloc_len = alloc_len ? alloc_len *2 : 16;
		attributes = (CK_ATTRIBUTE *)realloc(attributes,
			alloc_len * sizeof(*attributes));
		Q_CHECK_PTR(attributes);
	}
	attr = attributes + attlen++;
	attr->type = a.attr.type;
	attr->ulValueLen = a.attr.ulValueLen;
	attr->pValue = malloc(attr->ulValueLen +1);
	Q_CHECK_PTR(attr->pValue);
	memcpy(attr->pValue, a.attr.pValue, attr->ulValueLen);
	((char*)attr->pValue)[attr->ulValueLen] = 0;
}

void pk11_attlist::reset()
{
	for (unsigned long i=0; i<attlen; i++)
		free(attributes[i].pValue);

	attlen = 0;
}