JunkBox_Lib++ (for Windows) 1.10.1
Loading...
Searching...
No Matches
asn1_tool.cpp
Go to the documentation of this file.
1
7#include "asn1_tool.h"
8
9
11// ASN.1/DER
12//
13
34int skip_DER_node(Buffer param, unsigned char node, int ls, int* lp)
35{
36 int i, sz=-1;
37
38 if ((unsigned char)param.buf[ls]==node) {
39 if ((unsigned char)param.buf[ls+1]>=0x80) {
40 sz = (int)param.buf[ls+1] - 128;
41 *lp = 0;
42 for (i=ls+2; i<ls+sz+2; i++) {
43 *lp *= 256;
44 *lp += (unsigned char)param.buf[i];
45 }
46 }
47 else {
48 sz = 0;
49 *lp = (int)param.buf[ls+1];
50 }
51 //if ((unsigned char)param.buf[ls]==JBXL_ASN1_BIT) sz++;
52 sz = ls + sz + 2;
53 }
54
55 return sz;
56}
57
58
70Buffer node2DER(Buffer pt, unsigned char node)
71{
72 int sz, len, pp;
73 unsigned char cnt;
74 Buffer buf;
75
76 len = get_size_toDER(pt, node);
77 buf = make_Buffer(len);
78 sz = pt.vldsz;
79 pp = len - sz; // ノード値の格納場所の先頭
80 memcpy(buf.buf+pp, pt.buf, sz);
81
82 pp--;
83 if (node==JBXL_ASN1_INT) {
84/*
85 int2bin_DER() で処理済み
86 if (pt.buf[0]>=0x80) {
87 sz++;
88 buf.buf[pp--] = 0x00;
89 }
90*/
91 }
92 else if (node==JBXL_ASN1_BIT) {
93 sz++;
94 buf.buf[pp--] = 0x00;
95 }
96
97 buf.buf[0] = node;
98
99 cnt = 0;
100 if (sz>=128) {
101 cnt++;
102 while (sz>=256) {
103 buf.buf[pp--] = sz % 256;
104 sz >>= 8;
105 cnt++;
106 }
107 buf.buf[pp] = (unsigned char)sz;
108 buf.buf[1] = 0x80 + cnt;
109 }
110 else {
111 buf.buf[1] = (unsigned char)sz;
112 }
113
114 buf.vldsz = len;
115 return buf;
116}
117
118
130{
131 int ii, cnt;
132 unsigned long int div;
133 Buffer bin;
134
135 cnt = 1;
136 div = n;
137 while(div>=256) {
138 div >>= 8;
139 cnt++;
140 }
141 bin = make_Buffer(cnt+1); // cnt : バイト数
142 ii = cnt - 1;
143
144 while(n>=256) {
145 bin.buf[ii--] = (unsigned char)(n % 256);
146 n >>= 8;
147 }
148 bin.buf[0] = (unsigned char)n;
149
150 // 負数ではない場合 先頭に 0x00 を追加
151 if (n>0 && bin.buf[0]>=0x80) {
152 for (ii=cnt; ii>0; ii--) bin.buf[ii] = bin.buf[ii-1];
153 bin.buf[0] = 0x00;
154 cnt++;
155 }
156
157 bin.vldsz = cnt;
158
159 return bin;
160}
161
162
163long int bin2int_DER(Buffer buf)
164{
165 if (buf.buf==NULL || buf.vldsz==0) return 0;
166
167 int i;
168 int sz = buf.vldsz;
169 unsigned char* pp = buf.buf;
170
171 int minus = OFF;
172 if (*pp & 0x80) { // 負数
173 minus = ON;
174 for (i=0; i<sz; i++) pp[i] = pp[i] ^ 0xff; // bit反転
175 }
176
177 long int ret = pp[sz-1];
178 for (i=sz-2; i>=0; i--) {
179 ret += pp[i] * 256;
180 }
181 if (minus) ret = - (ret + 1);
182
183 return ret;
184}
185
186
198int get_size_toDER(Buffer pt, unsigned char node)
199{
200 int sz, div, cnt;
201
202 sz = pt.vldsz;
203 if (node==JBXL_ASN1_BIT) sz++; // 未使用bit数を格納する先頭データ用
204
205 cnt = 1; // ノード長のバイト数
206 div = sz;
207 if (div>=128) cnt++; // ノード長のバイト数が2以上の場合
208 while (div>=256) {
209 div >>= 8;
210 cnt++;
211 }
212 sz = sz + cnt + 1; // 1=>node
213 return sz;
214}
215
216
217
219//
220//
221
236{
237 if (buf==NULL || buf->buf==NULL) return NULL;
238
239 if (der==NULL) {
240 der = new_DER_node();
241 der->ldat.id = JBXL_ASN1_ANCHOR;
242 der->ldat.lv = buf->vldsz;
243 der->state = JBXL_STATE_ANCHOR;
244 }
245
246 int sz = get_DER_size(buf->buf, NULL);
247 if (sz > buf->vldsz) return NULL; // データが短い
248
249 _DER_parse_children(der, buf);
250
251 return der;
252}
253
254
256{
257 if (der==NULL) return;
258 if (buf==NULL) buf = &(der->ldat.val);
259 if (buf->buf==NULL) return;
260
261 unsigned char* pp = buf->buf;
262
263 int pos = 0;
264 do {
265 int sz = get_DER_size(pp+pos, NULL);
266 if (sz > buf->vldsz - pos) return;
267
268 tDER* tmp = new_DER_node();
269 int len = set_DER_node(tmp, pp+pos);
270 if (len==0) {
271 del_DER_node(&tmp);
272 return;
273 }
274 //
275 add_DER_node(der, tmp);
276 if (tmp->ldat.id & JBXL_ASN1_CNSTRCTD) {
277 _DER_parse_children(tmp, NULL);
278 free_Buffer(&(tmp->ldat.val)); // メモリが勿体ないので解放
279 }
280 pos += len;
281
282 } while (pos<buf->vldsz);
283
284 return;
285}
286
287
298int set_DER_node(tDER* der, unsigned char* buf)
299{
300 if (der==NULL || buf==NULL) return 0;
301
302 int len = 0;
303 int cnt = get_DER_size(buf, &len);
304
305 der->ldat.id = (int)(*buf); // buf[0] Tag
306 der->ldat.lv = cnt;
307 der->ldat.val = set_Buffer((void*)(buf+cnt-len), len);
308
309 return cnt;
310}
311
312
325int get_DER_size(unsigned char* buf, int* valsz)
326{
327 if (buf==NULL) return 0;
328
329 int cnt = 0;
330 int len = 0;
331 cnt++;
332 buf++;
333 if (*buf>=0x80) {
334 int i;
335 int sz = (int)(*buf - 0x80);
336 cnt++;
337 buf++;
338 for (i=0; i<sz; i++) {
339 len = len*256 + (int)(*buf);
340 cnt++;
341 buf++;
342 }
343 }
344 else {
345 len = (int)(*buf);
346 cnt++;
347 buf++;
348 }
349
350 if (valsz!=NULL) *valsz = len;
351 cnt += len;
352
353 return cnt;
354}
355
356
366
375void print_tDER(FILE* fp, tDER* pp)
376{
377 if (fp==NULL) fp = stderr;
378
379 if (pp!=NULL) {
380 while(pp->esis!=NULL) pp = pp->esis;
381 do {
382 int i;
383 tList_data ld = pp->ldat;
384
385 for(i=0; i<pp->depth; i++) fprintf(fp, " ");
386 if (pp->depth>0) fprintf(fp, " -> ");
387 fprintf(fp, "%d: ", pp->depth);
388 asn1_print_id(fp, ld.id);
389 fprintf(fp, "%d, %d ", ld.lv, ld.val.vldsz);
390 asn1_print_tag_value(fp, ld.id, ld.val);
391 fprintf(fp, "\n");
392
393 if (pp->next!=NULL) print_tDER(fp, pp->next);
394
395 pp = pp->ysis;
396 //if (pp!=NULL) {
397 // for(i=1; i<pp->depth-1; i++) fprintf(fp, " "); // for " -> "
398 //}
399 } while(pp!=NULL);
400 }
401 else {
402 fprintf(fp, "(Tree is NULL)\n");
403 }
404 fflush(fp);
405
406 return;
407}
408
409
410/*
411Buffer* DER_inverse_parse(tDER* pp);
412{
413 Buffer* buf = new_Buffer(0);
414
415 return buf;
416}
417*/
void asn1_print_id(FILE *fp, int id)
Definition asn1_node.cpp:76
void asn1_print_tag_value(FILE *fp, int id, Buffer buf)
Definition asn1_node.cpp:92
#define JBXL_ASN1_BIT
BIT_STRING.
Definition asn1_node.h:24
#define JBXL_ASN1_INT
INTEGER.
Definition asn1_node.h:23
#define JBXL_ASN1_CNSTRCTD
CONSTRUCTED 構造化フラグ
Definition asn1_node.h:14
#define JBXL_ASN1_ANCHOR
ANCHOR.
Definition asn1_node.h:11
Buffer int2bin_DER(long int n)
void print_tDER(FILE *fp, tDER *pp)
long int bin2int_DER(Buffer buf)
Buffer node2DER(Buffer pt, unsigned char node)
Definition asn1_tool.cpp:70
int set_DER_node(tDER *der, unsigned char *buf)
tDER * DER_parse(tDER *der, Buffer *buf)
int skip_DER_node(Buffer param, unsigned char node, int ls, int *lp)
Definition asn1_tool.cpp:34
int get_DER_size(unsigned char *buf, int *valsz)
void _DER_parse_children(tDER *der, Buffer *buf)
int get_size_toDER(Buffer pt, unsigned char node)
ASN.1/DER 用ライブラリヘッダ
#define del_DER_node(a)
Definition asn1_tool.h:52
#define add_DER_node(a, n)
Definition asn1_tool.h:54
#define new_DER_node()
Definition asn1_tool.h:51
Buffer make_Buffer(int sz)
Buffer型変数のバッファ部をつくり出す.
Definition buffer.cpp:71
Buffer set_Buffer(void *dat, int len)
Buffer型変数のバッファ部を新しく作り, そのバッファに bufをコピーする.
Definition buffer.cpp:170
void free_Buffer(Buffer *buf)
Buffer型変数のバッファ部を解放する
Definition buffer.cpp:128
#define OFF
Definition common.h:231
#define ON
Definition common.h:230
#define JBXL_STATE_ANCHOR
アンカーノード
Definition jbxl_state.h:30
int vldsz
データの長さ.バイナリデータの場合も使用可能.文字列の場合は 0x00 を含まない.
Definition buffer.h:37
unsigned char * buf
バッファの先頭へのポインタ.str[bufsz]は必ず 0x00となる.
Definition buffer.h:39