forked from arnaudmorin/libgtop11dotnet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
beroctet.cpp
761 lines (563 loc) · 17.6 KB
/
beroctet.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
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
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
/*
* 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 implementation is based on X.690 specification. Access to this specification is a pre-requisite to understand the logic.
// The spec can be purchased from International Telecommunication Union (ITU) at http://www.itu.int
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include "beroctet.h"
BEROctet::BEROctet() : m_tcClass(tcUniversal),
m_fConstructed(fBerPcPrimitive),
m_dwTag(dwBerUnivZero),
m_fDefinite(true),
m_fModified(true)
{
}
BEROctet::BEROctet(BEROctet const &oct)
{
*this = oct;
}
BEROctet::BEROctet(Blob const &blb)
{
Decode(blb);
}
BEROctet::BEROctet(TagClass tcClass, bool fConstructed, unsigned int dwTag, bool fDefinite) :
m_tcClass(tcClass),
m_fConstructed(fConstructed),
m_dwTag(dwTag),
m_fDefinite(fDefinite),
m_fModified(true)
{
if(!m_fDefinite && !m_fConstructed)
throw std::runtime_error("BERPrimitiveIndefiniteLength");
}
BEROctet::~BEROctet(void)
{
size_t l = m_SubOctetList.size( );
for( std::vector< BEROctet const* >::size_type i = 0 ; i < l ; ++i ) {
delete m_SubOctetList[ i ];
}
}
BEROctet& BEROctet::operator=(BEROctet const &Oct)
{
size_t l = m_SubOctetList.size( );
for( std::vector< BEROctet const* >::size_type i = 0 ; i < l ; ++i ) {
delete m_SubOctetList[ i ];
}
/*for(std::vector<BEROctet const*>::size_type i=0; i<m_SubOctetList.size(); i++)
delete m_SubOctetList[i];
*/
m_SubOctetList.resize(0);
m_blbData.resize(0);
m_fModified = Oct.m_fModified;
m_blbOrigOctet = Oct.m_blbOrigOctet;
m_fConstructed = Oct.m_fConstructed;
m_tcClass = Oct.m_tcClass;
m_dwTag = Oct.m_dwTag;
m_fDefinite = Oct.m_fDefinite;
if(m_fConstructed)
{
size_t l = Oct.m_SubOctetList.size( );
for( std::vector< BEROctet const*>::size_type i = 0 ; i < l; ++i ) {
m_SubOctetList.push_back(new BEROctet(*Oct.m_SubOctetList[i]));
}
}
else
m_blbData = Oct.m_blbData;
return *this;
}
// Returns a the data part of the octet
BEROctet::Blob BEROctet::Data() const
{
if(m_fConstructed)
{
// Traverse the tree
Blob data;
size_t l = m_SubOctetList.size( );
for( std::vector< BEROctet const* >::size_type i = 0 ; i < l; ++i ) {
data += m_SubOctetList[ i ]->Octet( );
}
return data;
}
else
return m_blbData;
}
// Sets the data part of the octet
void BEROctet::Data(Blob const &blb)
{
if(m_fConstructed)
throw std::runtime_error("BERInconsistentOperation");
m_blbData = blb;
m_fModified = true;
}
// If the octet is a constructed type, this returns list of sub-octets
std::vector<BEROctet*> BEROctet::SubOctetList() const
{
if(!m_fConstructed)
throw std::runtime_error("BERInconsistentOperation");
return m_SubOctetList;
}
// Insert an octet as a sub-octet of a constructed octet
void BEROctet::Insert(BEROctet const &oct)
{
if(!m_fConstructed)
throw std::runtime_error("BERInconsistentOperation");
BEROctet *pOct = new BEROctet(oct);
m_SubOctetList.push_back(pOct);
m_fModified = true;
}
// Returns the whole octet
BEROctet::Blob BEROctet::Octet() const
{
if(Modified())
{
Blob blbOct = IdentOctets(m_tcClass, m_fConstructed, m_dwTag);
Blob blbData = Data();
if(m_fDefinite)
blbOct += LengthOctets(static_cast<unsigned int>(blbData.size()));
else
blbOct += 0x80; // Indefinite length octet
blbOct += blbData;
if(!m_fDefinite)
{
// Terminate with end-of-contents octet
BEROctet blbZero;
blbOct += blbZero.Octet();
}
return blbOct;
}
else
return m_blbOrigOctet;
}
// Returns the class of the octet
TagClass BEROctet::Class() const
{
return m_tcClass;
}
// Returns true if the octet is constructet, false otherwise
bool BEROctet::Constructed() const
{
return m_fConstructed;
}
// Returns the tag of the octet
unsigned int BEROctet::Tag() const
{
return m_dwTag;
}
// Decode the contents of an OID
std::string BEROctet::ObjectID() const
{
if(m_tcClass!=tcUniversal || m_dwTag!=dwBerUnivObjectIdent)
throw std::runtime_error("BERInconsistentOperation");
if(!m_blbData.size())
throw std::runtime_error("BEREmptyOctet");
std::string OID;
// The scratch buffer "text" below needs to be large enough to hold
// the decimal encoding of two 32 bit integers, including a space
// and the terminating zero.
char text[40];
unsigned int subid;
const unsigned char *c = m_blbData.data();
const unsigned char *Last = c + m_blbData.size();
bool First = true;
while(c<Last)
{
subid = (*c)&0x7F;
while((*c)&0x80)
{
c++;
if(c>=Last)
throw std::runtime_error("BERUnexpectedEndOfOctet");
if(subid>0x01FFFFFF)
throw std::runtime_error("BEROIDSubIdentifierOverflow");
subid = (subid<<7) | ((*c)&0x7F);
}
if(First)
{
unsigned int X,Y;
if(subid<40)
X=0;
else if(subid<80)
X=1;
else
X=2;
Y = subid-X*40;
sprintf(text,"%d %d",X,Y);
OID = text;
First = false;
}
else
{
sprintf(text," %d",subid);
OID += text;
}
c++;
}
return OID;
}
// Encode an OID
void BEROctet::ObjectID(std::string const &str)
{
if(m_tcClass!=tcUniversal)
throw std::runtime_error("BERInconsistentOperation");
if(m_dwTag==dwBerUnivZero)
m_dwTag = dwBerUnivObjectIdent;
if(m_dwTag!=dwBerUnivObjectIdent)
throw std::runtime_error("BERInconsistentOperation");
char *oid = 0;
try
{
#if defined(_WIN32)
oid = _strdup(str.c_str());
#else
oid = strdup(str.c_str());
#endif
char *s;
if(0==(s = strtok(oid," ")))
throw std::runtime_error("BERIllegalObjectIdentifier");
unsigned int X,Y,dwSubOID;
if(sscanf(s,"%u",&X)!=1)
throw std::runtime_error("BERIllegalObjectIdentifier");
if(X>2)
throw std::runtime_error("BERIllegalObjectIdentifier");
if(0==(s = strtok(0," ")))
throw std::runtime_error("BERIllegalObjectIdentifier");
if(sscanf(s,"%u",&Y)!=1)
throw std::runtime_error("BERIllegalObjectIdentifier");
if(X<2 && Y>39)
throw std::runtime_error("BERIllegalObjectIdentifier");
dwSubOID = X*40;
if(Y>0xFFFFFFFF-dwSubOID)
throw std::runtime_error("BERDataOverflow");
dwSubOID += Y;
Blob blbData;
while(true)
{
unsigned char buf[2*sizeof(dwSubOID)];
int n=0;
while(dwSubOID>0x7F)
{
buf[n] = static_cast<unsigned char>(dwSubOID & 0x7F);
dwSubOID >>=7;
n++;
}
buf[n] = static_cast<unsigned char>(dwSubOID & 0x7F);
n++;
for(int i=0; i<n; i++)
{
unsigned char b = buf[n-i-1];
if((i+1)<n) b |= 0x80;
blbData += b;
}
if(0==(s = strtok(0," ")))
break;
if(sscanf(s,"%u",&dwSubOID)!=1)
break;
}
Data(blbData);
}
catch(...)
{
if(oid)
free(oid);
throw;
}
if(oid)
free(oid);
}
// Decode a Time octet. Output format: "YYYYMMDDHHMMSS"
// We here apply the convention from RFC 2459 that the 2 digit year
// encoded in UTCTime is in the range 1950-2049.
std::string BEROctet::Time() const
{
static const Blob::size_type UnivUTCTimeSize = 13;
static const Blob::size_type UnivGenTimeSize = 15;
if(m_tcClass!=tcUniversal)
throw std::runtime_error("BERInconsistentOperation");
if(m_dwTag==dwBerUnivUTCTime)
{
// UTCTime
if(m_blbData.size()!=UnivUTCTimeSize)
throw std::runtime_error("BERInconsistentDataLength");
std::string strCentury, strYear((char*)m_blbData.data(),2);
int iYear;
if(sscanf(strYear.c_str(),"%d",&iYear)!=1)
throw std::runtime_error("FormatDecodingError");
if(iYear>=50) strCentury = "19";
else strCentury = "20";
// Add century and strip off the 'Z'
return strCentury + std::string((char*)m_blbData.data(),UnivUTCTimeSize-1);
}
else if(m_dwTag==dwBerUnivGenTime)
{
// GeneralizedTime
if(m_blbData.size()!=UnivGenTimeSize)
throw std::runtime_error("BERInconsistentDataLength");
// Return the string as is, stripping off the 'Z'
return std::string((char*)m_blbData.data(),UnivGenTimeSize-1);
}
else
throw std::runtime_error("BERInconsistentOperation");
}
// Encode a Time. Input format: "YYYYMMDDHHMMSS"
// If the Tag is not set to be either UTC Time or Generalized Time,
// we apply the convention from RFC 2459 where years in the range
// 1950-2049 are encoded as UTC Time and years later are encoded as
// Generalized time. In this case, years < 1950 are not allowed.
void BEROctet::Time(std::string const &str)
{
static const Blob::size_type ExpectedSize = 14;
if(m_tcClass!=tcUniversal)
throw std::runtime_error("BERInconsistentOperation");
if(str.size()!=ExpectedSize)
throw std::runtime_error("IllegalParameter");
// If m_dwTag is zero, chose appropriate tag according to year
int iYear;
if(sscanf(str.substr(0,4).c_str(),"%d",&iYear)!=1)
throw std::runtime_error("IllegalParameter");
if(m_dwTag==dwBerUnivZero)
{
if(iYear<1950)
throw std::runtime_error("IllegalParameter");
else if(iYear<2050)
m_dwTag = dwBerUnivUTCTime;
else
m_dwTag = dwBerUnivGenTime;
}
Blob blbData;
if(m_dwTag==dwBerUnivUTCTime)
blbData.assign(((unsigned char*)str.data()+2),str.size()-2);
else if(m_dwTag==dwBerUnivGenTime)
blbData.assign((unsigned char*)str.data(),str.size());
else
throw std::runtime_error("BERInconsistentOperation");
blbData += 'Z';
Data(blbData);
}
// SearchOID returns all the constructed octets that contain a particular OID
void BEROctet::SearchOID(std::string const &OID, std::vector<BEROctet const*> &result) const
{
size_t l = m_SubOctetList.size( );
for( std::vector< BEROctet const* >::size_type i = 0; i < l ; ++i ) {
if(m_SubOctetList[i]->Class()==tcUniversal &&
m_SubOctetList[i]->Tag()==dwBerUnivObjectIdent)
{
if(OID==m_SubOctetList[i]->ObjectID())
result.push_back(this);
}
else if(m_SubOctetList[i]->Constructed())
m_SubOctetList[i]->SearchOID(OID,result);
}
return;
}
// SearchOIDNext returns all the octets following a particular OID
void BEROctet::SearchOIDNext(std::string const &OID, std::vector<BEROctet const*> &result) const
{
size_t l = m_SubOctetList.size( );
for( std::vector< BEROctet const* >::size_type i = 0; i < l ; ++i ) {
if(m_SubOctetList[i]->Class()==tcUniversal &&
m_SubOctetList[i]->Tag()==dwBerUnivObjectIdent)
{
if(OID==m_SubOctetList[i]->ObjectID())
{
if((i+1) < m_SubOctetList.size())
result.push_back(m_SubOctetList[i+1]);
}
}
else if(m_SubOctetList[i]->Constructed())
m_SubOctetList[i]->SearchOIDNext(OID,result);
}
return;
}
// Construct the Identifier octets
BEROctet::Blob BEROctet::IdentOctets(TagClass tcClass, bool fConstructed, unsigned int dwTag)
{
unsigned char bLeadingOct;
switch(tcClass)
{
case tcUniversal:
bLeadingOct = 0x00;
break;
case tcApplication:
bLeadingOct = 0x40;
break;
case tcContext:
bLeadingOct = 0x80;
break;
case tcPrivate:
bLeadingOct = 0xC0;
break;
default:
throw std::runtime_error("BERIllegalClass");
}
if(fConstructed)
bLeadingOct |= 0x20;
int n = 0;
unsigned char buf[sizeof(dwTag)];
if(dwTag<=30)
bLeadingOct |= dwTag;
else
{
bLeadingOct |= 0x1F;
while(dwTag>0)
{
buf[n] = static_cast<unsigned char>(dwTag & 0x000000FF);
dwTag >>= 8;
n++;
}
}
Blob IdentOcts(&bLeadingOct,1);
for(int i=0; i<n; ++i)
IdentOcts +=buf[n-i-1];
return IdentOcts;
}
// Construct the Length octets
BEROctet::Blob BEROctet::LengthOctets(unsigned int dwLength)
{
int n = 0;
unsigned char buf[sizeof(dwLength)];
unsigned char bLeadingOct;
if(dwLength<=0x7F)
bLeadingOct = static_cast<unsigned char>(dwLength);
else
{
bLeadingOct = 0x80;
while(dwLength>0)
{
buf[n] = static_cast<unsigned char>(dwLength & 0x000000FF);
dwLength >>= 8;
n++;
}
bLeadingOct |= n;
}
Blob LengthOcts(&bLeadingOct,1);
for(int i=0; i<n; ++i)
LengthOcts +=buf[n-i-1];
return LengthOcts;
}
// Decodes recursively a BER octet.
void BEROctet::Decode(Blob const &blb)
{
if(!blb.size())
throw std::runtime_error("BEREmptyOctet");
size_t BufferSize = blb.size();
m_fConstructed = (blb[0]&0x20) ? true : false;
switch(blb[0]&0xC0)
{
case 0x00:
m_tcClass = tcUniversal;
break;
case 0x40:
m_tcClass = tcApplication;
break;
case 0x80:
m_tcClass = tcContext;
break;
case 0xC0:
m_tcClass = tcPrivate;
break;
default:
throw std::runtime_error("BERIllegalClass");
}
const unsigned char *c = blb.data();
const unsigned char *Last = c + blb.size() - 1;
m_dwTag = *c & 0x1F;
if(m_dwTag>30)
{
m_dwTag = 0;
c++;
if(c>Last)
throw std::runtime_error("BERUnexpectedEndOfOctet");
while (*c & 0x80)
{
m_dwTag = (m_dwTag << 7) | ((*c) & 0x7F);
c++;
if(c>Last)
throw std::runtime_error("BERUnexpectedEndOfOctet");
}
if(m_dwTag > 0x01FFFFFF)
throw std::runtime_error("BERTagValueOverflow");
m_dwTag = (m_dwTag << 7) | ((*c) & 0x7F);
}
c++;
if(c>Last)
throw std::runtime_error("BERUnexpectedEndOfOctet");
size_t DataSize;
if((*c)&0x80)
{
int n = (*c) & 0x7F;
if(n)
{
DataSize = 0;
for(int i=0; i<n; ++i)
{
c++; if(c>Last)
throw std::runtime_error("BERUnexpectedEndOfOctet");
if(DataSize>0x007FFFFF)
throw std::runtime_error("BERDataOverflow");
DataSize = (DataSize<<8) | (*c);
}
}
else
throw std::runtime_error("BERUnexpectedIndefiniteLength");
}
else DataSize = *c;
c++;
const unsigned char *bpData = c;
size_t OctetSize = DataSize + (bpData-blb.data());
m_blbOrigOctet = blb.substr(0,OctetSize);
m_fModified = false;
if(OctetSize>static_cast<unsigned int>(BufferSize))
throw std::runtime_error("BERInconsistentDataLength");
size_t l = m_SubOctetList.size( );
for( std::vector< BEROctet const* >::size_type i = 0; i < l; ++i ) {
delete m_SubOctetList[i];
}
m_SubOctetList.resize(0);
m_blbData = Blob();
if(m_fConstructed)
{
// Constructed type
while(DataSize)
{
BEROctet *suboct = new BEROctet(Blob(bpData,DataSize));
m_SubOctetList.push_back(suboct);
Blob blbSubOct = suboct->Octet();
bpData += blbSubOct.size();
DataSize -= blbSubOct.size();
}
}
else
m_blbData = Blob(bpData,DataSize);
}
bool BEROctet::Modified() const
{
if(m_fModified)
return true;
if(m_fConstructed) {
std::vector< BEROctet const* >::size_type l = m_SubOctetList.size( );
for( std::vector< BEROctet const* >::size_type i = 0; i < l; ++i ) {
if(m_SubOctetList[i]->Modified()) {
return true;
}
}
}
return false;
}