forked from arnaudmorin/libgtop11dotnet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
attrcert.cpp
215 lines (178 loc) · 4.79 KB
/
attrcert.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
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
/*
* PKCS#11 library for .Net smart cards
* Copyright (C) 2007-2009 Gemalto <[email protected]>
*
* 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; either
* version 2.1 of the License, or (at your option) any later version.
*
* 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
*
*/
// This code is based on class in ACS baseline.
//#pragma warning(disable : 4251)
#include <cstdio>
#include "cryptoki.h"
#include <numeric>
#include <functional>
#include "attrcert.h"
#include "digest.h"
#include "sha1.h"
namespace
{
class JoinWith
: public std::binary_function<std::string, std::string, std::string>
{
public:
explicit
JoinWith(second_argument_type const &rGlue)
: m_Glue(rGlue)
{}
result_type
operator()(std::string const &rFirst,
std::string const &rSecond) const
{
return rFirst + m_Glue + rSecond;
}
private:
second_argument_type const m_Glue;
};
std::string
Combine(std::vector<std::string> const &rvsNames)
{
static std::string::value_type const cBlank = ' ';
static std::string const sBlank(1, cBlank);
if(!rvsNames.empty())
return accumulate(rvsNames.begin() + 1, rvsNames.end(),
*rvsNames.begin(), JoinWith(sBlank));
else
return std::string();
}
} // namespace
CAttributedCertificate::CAttributedCertificate(BEROctet::Blob const &cert) : m_x509cert(cert)
{
}
CAttributedCertificate::CAttributedCertificate(const unsigned char * cert, size_t length)
: m_x509cert(cert, static_cast<unsigned long>(length))
{
}
CAttributedCertificate::~CAttributedCertificate()
{
}
BEROctet::Blob
CAttributedCertificate::Modulus() const
{
return m_x509cert.Modulus();
}
BEROctet::Blob
CAttributedCertificate::PublicExponent() const
{
return m_x509cert.PublicExponent();
}
BEROctet::Blob
CAttributedCertificate::Subject() const
{
return m_x509cert.Subject();
}
BEROctet::Blob
CAttributedCertificate::Issuer() const
{
return m_x509cert.Issuer();
}
BEROctet::Blob
CAttributedCertificate::SerialNumber() const
{
return m_x509cert.SerialNumber();
}
std::string
CAttributedCertificate::DerivedName() const
{
std::string sDerivedName(Combine(m_x509cert.UTF8SubjectCommonName()));
if(sDerivedName.empty())
sDerivedName.assign("Smart Card User");
return sDerivedName;
}
std::string
CAttributedCertificate::DerivedLabel() const
{
return Combine(m_x509cert.SubjectCommonName());
}
BEROctet::Blob
CAttributedCertificate::DerivedId() const
{
return DerivedId(m_x509cert.Modulus());
}
BEROctet::Blob
CAttributedCertificate::DerivedId(BEROctet::Blob const & data)
{
return DerivedId(data.c_str(), data.size());
}
BEROctet::Blob
CAttributedCertificate::DerivedId(unsigned char const * data, size_t length)
{
CSHA1 sha1;
unsigned char hash[20];
sha1.hashCore(const_cast<CK_BYTE_PTR>(data), 0, static_cast<CK_LONG>(length));
sha1.hashFinal(hash);
return BEROctet::Blob(hash, 20);
}
std::string
CAttributedCertificate::DerivedUniqueName() const
{
return DerivedUniqueName(m_x509cert.Modulus());
}
std::string
CAttributedCertificate::DerivedUniqueName(BEROctet::Blob const & data)
{
return DerivedUniqueName(data.c_str(), data.size());
}
std::string
CAttributedCertificate::DerivedUniqueName(unsigned char const * data, size_t length)
{
CSHA1 sha1;
unsigned char hash[20];
sha1.hashCore(const_cast<CK_BYTE_PTR>(data), 0, static_cast<CK_LONG>(length));
sha1.hashFinal(hash);
// Format as a GUID
char name[40];
unsigned char *id = hash;
int i, n = 0;
char *c = name;
for(i=0; i<4; i++) {
sprintf(c,"%02x",id[n]);
n++; c+=2;
}
sprintf(c,"-");
c++;
for(i=0; i<2; i++) {
sprintf(c,"%02x",id[n]);
n++; c+=2;
}
sprintf(c,"-");
c++;
for(i=0; i<2; i++) {
sprintf(c,"%02x",id[n]);
n++; c+=2;
}
sprintf(c,"-");
c++;
for(i=0; i<2; i++) {
sprintf(c,"%02x",id[n]);
n++; c+=2;
}
sprintf(c,"-");
c++;
for(i=0; i<6; i++) {
sprintf(c,"%02x",id[n]);
n++; c+=2;
}
return std::string(name);
}