-
Notifications
You must be signed in to change notification settings - Fork 2
/
PyCadesAttribute.cpp
executable file
·174 lines (159 loc) · 7.1 KB
/
PyCadesAttribute.cpp
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
#include "PyCadesAttribute.h"
#include "PyCadesOID.h"
using namespace CryptoPro::PKI::CAdES;
static void Attribute_dealloc(Attribute *self)
{
self->m_pCppCadesImpl.reset();
Py_TYPE(self)->tp_free((PyObject *)self);
}
static PyObject *Attribute_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
Attribute *self;
self = (Attribute *)type->tp_alloc(type, 0);
if (self != NULL)
{
self->m_pCppCadesImpl = NS_SHARED_PTR::shared_ptr<CPPCadesCPAttributeObject>(new CPPCadesCPAttributeObject());
}
return (PyObject *)self;
}
static PyObject *Attribute_getOID(Attribute *self)
{
NS_SHARED_PTR::shared_ptr<CPPCadesCPOIDObject> pCPPCadesCPOID(new CPPCadesCPOIDObject());
HR_METHOD_ERRORCHECK_RETURN(self->m_pCppCadesImpl->get_OID(pCPPCadesCPOID));
PyObject *pPyOID = PyObject_CallObject((PyObject *)&OIDType, NULL);
OID *pOID = (OID *)pPyOID;
pOID->m_pCppCadesImpl = pCPPCadesCPOID;
return Py_BuildValue("N", pOID);
}
static int Attribute_setValue(Attribute *self, PyObject *value)
{
char *szValue = "";
if (!PyArg_Parse(value, "s", &szValue))
{
return -1;
}
CADESCOM_ATTRIBUTE Name;
HR_SETTER_ERRORCHECK_RETURN(self->m_pCppCadesImpl->get_Name(&Name));
if (Name == CADESCOM_AUTHENTICATED_ATTRIBUTE_SIGNING_TIME)
{
CryptoPro::CDateTime time(szValue);
HR_SETTER_ERRORCHECK_RETURN(self->m_pCppCadesImpl->put_DateTimeValue(time));
}
else
{
CryptoPro::CBlob blobValue;
blobValue.assign((unsigned char *)szValue, strlen(szValue));
HR_SETTER_ERRORCHECK_RETURN(self->m_pCppCadesImpl->put_Value(blobValue));
}
return 0;
}
static PyObject *Attribute_getValue(Attribute *self)
{
CryptoPro::CBlob blobValue;
CADESCOM_ATTRIBUTE Name;
HR_METHOD_ERRORCHECK_RETURN(self->m_pCppCadesImpl->get_Value(blobValue));
HR_METHOD_ERRORCHECK_RETURN(self->m_pCppCadesImpl->get_Name(&Name));
DWORD dwLen = blobValue.cbData();
std::vector<BYTE> vbValue(dwLen + 1, 0);
if (Name != CADESCOM_AUTHENTICATED_ATTRIBUTE_SIGNING_TIME)
{
memcpy(&vbValue[0], blobValue.pbData(), dwLen);
}
else
{
FILETIME fTime;
DWORD fTimeSize = sizeof(FILETIME);
CryptStringToBinary(reinterpret_cast<TCHAR *>(blobValue.pbData()), dwLen,
CRYPT_STRING_BASE64, &vbValue[0], &dwLen, NULL, NULL);
CryptDecodeObject(X509_ASN_ENCODING | PKCS_7_ASN_ENCODING,
(LPCSTR)szOID_RSA_signingTime, &vbValue[0], dwLen, 0, &fTime,
&fTimeSize);
CryptoPro::CDateTime Time(fTime);
CryptoPro::CStringProxy strProxyTime = Time.tostring();
return Py_BuildValue("s", strProxyTime.c_str());
}
return Py_BuildValue("s", &vbValue[0]);
}
static int Attribute_setName(Attribute *self, PyObject *value)
{
long lName = 0;
if (!PyArg_Parse(value, "l", &lName))
{
return -1;
}
CADESCOM_ATTRIBUTE Name = (CADESCOM_ATTRIBUTE)lName;
HR_SETTER_ERRORCHECK_RETURN(self->m_pCppCadesImpl->put_Name(Name));
return 0;
}
static PyObject *Attribute_getName(Attribute *self)
{
CADESCOM_ATTRIBUTE Name;
HR_METHOD_ERRORCHECK_RETURN(self->m_pCppCadesImpl->get_Name(&Name));
return Py_BuildValue("l", (long)Name);
}
static int Attribute_setValueEncoding(Attribute *self, PyObject *value)
{
long lType = 0;
if (!PyArg_Parse(value, "l", &lType))
{
return -1;
}
CAPICOM_ENCODING_TYPE Type = (CAPICOM_ENCODING_TYPE)lType;
HR_SETTER_ERRORCHECK_RETURN(self->m_pCppCadesImpl->put_ValueEncoding(Type));
return 0;
}
static PyObject *Attribute_getValueEncoding(Attribute *self)
{
CAPICOM_ENCODING_TYPE Type;
HR_METHOD_ERRORCHECK_RETURN(self->m_pCppCadesImpl->get_ValueEncoding(&Type));
return Py_BuildValue("l", (long)Type);
}
static PyGetSetDef Attribute_getset[] = {
{"OID", (getter)Attribute_getOID, NULL, "OID", NULL},
{"Value", (getter)Attribute_getValue, (setter)Attribute_setValue, "Value", NULL},
{"Name", (getter)Attribute_getName, (setter)Attribute_setName, "Name", NULL},
{"ValueEncoding", (getter)Attribute_getValueEncoding, (setter)Attribute_setValueEncoding, "ValueEncoding", NULL},
{NULL} /* Sentinel */
};
static PyMethodDef Attribute_methods[] = {
{NULL} /* Sentinel */
};
PyTypeObject AttributeType = {
PyVarObject_HEAD_INIT(NULL, 0) "pycades.Attribute", /* tp_name */
sizeof(Attribute), /* tp_basicsize */
0, /* tp_itemsize */
(destructor)Attribute_dealloc, /* tp_dealloc */
0, /* tp_print */
0, /* tp_getattr */
0, /* tp_setattr */
0, /* tp_compare */
0, /* tp_repr */
0, /* tp_as_number */
0, /* tp_as_sequence */
0, /* tp_as_mapping */
0, /* tp_hash */
0, /* tp_call */
0, /* tp_str */
0, /* tp_getattro */
0, /* tp_setattro */
0, /* tp_as_buffer */
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
"Attribute object", /* tp_doc */
0, /* tp_traverse */
0, /* tp_clear */
0, /* tp_richcompare */
0, /* tp_weaklistoffset */
0, /* tp_iter */
0, /* tp_iternext */
Attribute_methods, /* tp_methods */
0, /* tp_members */
Attribute_getset, /* tp_getset */
0, /* tp_base */
0, /* tp_dict */
0, /* tp_descr_get */
0, /* tp_descr_set */
0, /* tp_dictoffset */
0, /* tp_init */
0, /* tp_alloc */
Attribute_new, /* tp_new */
};