JunkBox_Lib  1.10.2
asn1_tool.h File Reference

ASN.1/DER 用ライブラリヘッダ More...

#include "xtools.h"
#include "ttree.h"
#include "asn1_node.h"
Include dependency graph for asn1_tool.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Macros

#define new_DER_node()   new_tTree_node()
 
#define del_DER_node(a)   del_tTree_node((a))
 
#define del_DER(a)   del_tTree((a))
 
#define add_DER_node(a, n)   add_tTree_node((a), (n))
 

Typedefs

typedef tTree tDER
 

Functions

Buffer int2bin_DER (long int n)
 
long int bin2int_DER (Buffer buf)
 
Buffer node2DER (Buffer, unsigned char)
 
int get_size_toDER (Buffer, unsigned char)
 
int skip_DER_node (Buffer, unsigned char, int, int *)
 
tDERDER_parse (tDER *der, Buffer *buf)
 
void _DER_parse_children (tDER *der, Buffer *buf)
 
int set_DER_node (tDER *ser, unsigned char *buf)
 
int get_DER_size (unsigned char *buf, int *len)
 
void print_tDER (FILE *fp, tDER *pp)
 

Detailed Description

Author
Fumi.Iseki (C)

Definition in file asn1_tool.h.

Macro Definition Documentation

◆ add_DER_node

#define add_DER_node (   a,
 
)    add_tTree_node((a), (n))

Definition at line 54 of file asn1_tool.h.

◆ del_DER

#define del_DER (   a)    del_tTree((a))

Definition at line 53 of file asn1_tool.h.

◆ del_DER_node

#define del_DER_node (   a)    del_tTree_node((a))

Definition at line 52 of file asn1_tool.h.

◆ new_DER_node

#define new_DER_node ( )    new_tTree_node()

Definition at line 51 of file asn1_tool.h.

Typedef Documentation

◆ tDER

typedef tTree tDER

Definition at line 15 of file asn1_tool.h.

Function Documentation

◆ _DER_parse_children()

void _DER_parse_children ( tDER der,
Buffer buf 
)

Definition at line 255 of file asn1_tool.c.

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 }
#define JBXL_ASN1_CNSTRCTD
CONSTRUCTED 構造化フラグ
Definition: asn1_node.h:14
int set_DER_node(tDER *der, unsigned char *buf)
Definition: asn1_tool.c:298
int get_DER_size(unsigned char *buf, int *valsz)
Definition: asn1_tool.c:325
void _DER_parse_children(tDER *der, Buffer *buf)
Definition: asn1_tool.c:255
#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
void free_Buffer(Buffer *buf)
Buffer型変数のバッファ部を解放する
Definition: buffer.c:128
unsigned char ** buf
Definition: jpeg_tool.h:96
unsigned char unsigned long * len
Definition: jpeg_tool.h:96

References add_DER_node, buf, del_DER_node, free_Buffer(), get_DER_size(), JBXL_ASN1_CNSTRCTD, len, new_DER_node, and set_DER_node().

Referenced by DER_parse().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ bin2int_DER()

long int bin2int_DER ( Buffer  buf)

Definition at line 163 of file asn1_tool.c.

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 }
#define OFF
Definition: common.h:231
#define ON
Definition: common.h:230

References buf, OFF, and ON.

Referenced by asn1_print_tag_value().

Here is the caller graph for this function:

◆ DER_parse()

tDER* DER_parse ( tDER der,
Buffer buf 
)

tDER* DER_parse(tDER* der, Buffer* buf)

DER形式のバイナリをパースして,tDER (tTree) 形式のツリー構造に格納する

Parameters
der生成したツリー構造を繋げるツリー.NULLなら新規のツリーを作る(トップのノードは JBXL_ASN1_ANCHOR)
buf解析を行う DER形式のバイナリー
Return values
NULL以外木構造 tDER のトップへのポインタ.
NULL処理不能.
Attention
この関数によって取得した木構造 tDER は必ず del_DER() で消去すること.

Definition at line 235 of file asn1_tool.c.

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 }
#define JBXL_ASN1_ANCHOR
ANCHOR.
Definition: asn1_node.h:11
#define JBXL_STATE_ANCHOR
アンカーノード
Definition: jbxl_state.h:30

References _DER_parse_children(), buf, get_DER_size(), JBXL_ASN1_ANCHOR, JBXL_STATE_ANCHOR, and new_DER_node.

Here is the call graph for this function:

◆ get_DER_size()

int get_DER_size ( unsigned char *  buf,
int *  valsz 
)

int get_DER_size(unsigned char* buf, int* valsz)

DER形式のバイナリ buf に格納されている最初のデータの,全体の長さとノード値の長さを返す.
返された値は計算上の値であり,bufがそれだけのデータを持っていない可能性もある

Parameters
[in]buf解析を行う DER形式のバイナリー
[out]valszノード値のデータの長さを格納する変数.NULLなら計算しない.
Returns
データ全体の長さ
Return values
valノード値のデータの長さ

Definition at line 325 of file asn1_tool.c.

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 }

References buf, and len.

Referenced by _DER_parse_children(), DER_parse(), and set_DER_node().

Here is the caller graph for this function:

◆ get_size_toDER()

int get_size_toDER ( Buffer  pt,
unsigned char  node 
)

int get_size_toDER(Buffer pt, unsigned char node)

データをDER形式に変換した場合の長さを計算する. 整数 JBXL_ASN1_INT の場合は,先に int2bin_DER() でバイナリ化しておかなければならない.

Parameters
pt計算対象のデータ.
nodeデータ形式のノード.JBXL_ASN1_INT, JBXL_ASN1_SEQ, JBXL_ASN1_BIT をサポート.デフォルト動作は JBXL_ASN1_SEQ
Returns
データをDER形式に変換した場合の長さ.エラーの場合は 0.

Definition at line 198 of file asn1_tool.c.

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 }
#define JBXL_ASN1_BIT
BIT_STRING.
Definition: asn1_node.h:24
int vldsz
データの長さ.バイナリデータの場合も使用可能.文字列の場合は 0x00 を含まない.
Definition: buffer.h:37

References JBXL_ASN1_BIT, and Buffer::vldsz.

Referenced by node2DER().

Here is the caller graph for this function:

◆ int2bin_DER()

Buffer int2bin_DER ( long int  n)

Buffer int2bin_DER(long int n)

整数を DER形式のバイナリに変換する.ただし,INTノードは付けない.

Parameters
n変換する整数値
Returns
DERのバナリ形式.
Bug:
この関数では, long int型の範囲の数値しか扱えない.

Definition at line 129 of file asn1_tool.c.

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 }
Buffer make_Buffer(int sz)
Buffer型変数のバッファ部をつくり出す.
Definition: buffer.c:71
Definition: buffer.h:35
unsigned char * buf
バッファの先頭へのポインタ.str[bufsz]は必ず 0x00となる.
Definition: buffer.h:39

References Buffer::buf, make_Buffer(), and Buffer::vldsz.

Here is the call graph for this function:

◆ node2DER()

Buffer node2DER ( Buffer  pt,
unsigned char  node 
)

Buffer node2DER(Buffer pt, unsigned char node)

データを DER形式に変換する. 現在は JBXL_ASN1_INT, JBXL_ASN1_SEQ, JBXL_ASN1_BIT のみをサポート.

Parameters
pt変換するデータ. JBXL_ASN1_BIT は 8bit区切りのみ対応.
nodeデータ形式のノード.JBXL_ASN1_INT, JBXL_ASN1_SEQ, JBXL_ASN1_BIT をサポート.デフォルト動作は JBXL_ASN1_SEQ
Returns
DER形式へ変換されたデータ.

Definition at line 70 of file asn1_tool.c.

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 }
#define JBXL_ASN1_INT
INTEGER.
Definition: asn1_node.h:23
int get_size_toDER(Buffer pt, unsigned char node)
Definition: asn1_tool.c:198

References buf, Buffer::buf, get_size_toDER(), JBXL_ASN1_BIT, JBXL_ASN1_INT, len, make_Buffer(), and Buffer::vldsz.

Here is the call graph for this function:

◆ print_tDER()

void print_tDER ( FILE *  fp,
tDER pp 
)

void print_tDER(FILE* fp, tDER* pp)

tDERツリーの表示.ポインタ pp以降の全てのノードを fp に出力する.

Parameters
fp出力するファイルへのポインタ.NULLの場合は stderr
pp表示を開始するノードへのポインタ.

///////////////////////////////////////////// 最初の行はアンカー

04 05 69 73 65 6b 69 [04] UNIVERSAL PRIMITIVE OCTET_STRING 7, 5 : "iseki" 7: 全体長 5: iseki の長さ

[ID] CLASS PRIMITIVE/CONSTRUCTED TAG/(num) data_size, contents_size : contents

Definition at line 375 of file asn1_tool.c.

376 {
377  if (fp==NULL) fp = stderr;
378  if (pp!=NULL && pp->ldat.id==JBXL_ASN1_ANCHOR) pp = pp->next;
379 
380  if (pp!=NULL) {
381  while(pp->esis!=NULL) pp = pp->esis;
382  do {
383  int i;
384  tList_data ld = pp->ldat;
385 
386  for(i=0; i<pp->depth-1; i++) fprintf(fp, " ");
387  if (pp->depth-1>0) fprintf(fp, " -> ");
388  fprintf(fp, "%d: ", pp->depth);
389  asn1_print_id(fp, ld.id);
390  fprintf(fp, "%d, %d ", ld.lv, ld.val.vldsz);
391  asn1_print_tag_value(fp, ld.id, ld.val);
392  fprintf(fp, "\n");
393 
394  if (pp->next!=NULL) print_tDER(fp, pp->next);
395 
396  pp = pp->ysis;
397  } while(pp!=NULL);
398  }
399  else {
400  fprintf(fp, "(Tree is NULL)\n");
401  }
402  fflush(fp);
403 
404  return;
405 }
void asn1_print_id(FILE *fp, int id)
Definition: asn1_node.c:76
void asn1_print_tag_value(FILE *fp, int id, Buffer buf)
Definition: asn1_node.c:92
void print_tDER(FILE *fp, tDER *pp)
Definition: asn1_tool.c:375

References asn1_print_id(), asn1_print_tag_value(), and JBXL_ASN1_ANCHOR.

Here is the call graph for this function:

◆ set_DER_node()

int set_DER_node ( tDER der,
unsigned char *  buf 
)

int set_DER_node(tDER* der, unsigned char* buf)

DER形式のバイナリ buf に格納されている最初のデータを tDER ツリーの最期に格納する.

Parameters
der格納先の tDER ツリー
buf格納との tDER形式のバイナリー
Returns
bufの格納されたデータの長さ.

Definition at line 298 of file asn1_tool.c.

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 }
Buffer set_Buffer(void *dat, int len)
Buffer型変数のバッファ部を新しく作り, そのバッファに bufをコピーする.
Definition: buffer.c:170

References buf, get_DER_size(), len, and set_Buffer().

Referenced by _DER_parse_children().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ skip_DER_node()

int skip_DER_node ( Buffer  param,
unsigned char  node,
int  ls,
int *  lp 
)

int skip_DER_node(Buffer param, unsigned char node, int ls, int* lp)

ノードオブジェクト paramの中の ls の位置から[ノード(node)]と[ノード値の長さ]を スキップして,[ノード値]の位置(return値)とそのノード値の長さ *lp を返す.

Parameters
paramノードオブジェクト全体
node現在のノード(確認のために指定)
lsノードオブジェクト全体内での操作開始点
[out]lp関数終了時に,そのノードのノード値の長さが格納される.値は指定しない.
Returns
そのノードのノード値の先頭位置.
参考: ↓ls
[ノードオブジェクト] = .....[ノード(node)][ノード値の長さ(*lp)][ノード値][ノード].......
↑return
[ノード値]として,その中に[ノードオブジェクト]を含む場合もある(階層構造を取り得る)

Definition at line 34 of file asn1_tool.c.

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 }

References Buffer::buf.