ExtendLib/xLib/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.

Defines

#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.


Define Documentation

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

Definition at line 39 of file asn1_tool.h.

Referenced by _DER_parse_children().

#define del_DER (  )     del_tTree((a))

Definition at line 38 of file asn1_tool.h.

#define del_DER_node (  )     del_tTree_node((a))

Definition at line 37 of file asn1_tool.h.

Referenced by _DER_parse_children().

 
#define new_DER_node (  )     new_tTree_node()

Definition at line 36 of file asn1_tool.h.

Referenced by _DER_parse_children(), and DER_parse().


Typedef Documentation

typedef tTree tDER

Definition at line 18 of file asn1_tool.h.


Function Documentation

void _DER_parse_children ( tDER der,
Buffer buf 
)

Definition at line 282 of file asn1_tool.cpp.

References _DER_parse_children(), add_DER_node, Buffer::buf, del_DER_node, free_Buffer(), get_DER_size(), JBXL_ASN1_CNSTRCTD, new_DER_node, set_DER_node(), and Buffer::vldsz.

Referenced by _DER_parse_children(), and DER_parse().

00283 {
00284     if (der==NULL) return;
00285     if (buf==NULL) buf = &(der->ldat.val);
00286     if (buf->buf==NULL) return;
00287 
00288     unsigned char* pp = buf->buf;
00289 
00290     int pos = 0;
00291     do {
00292         int sz = get_DER_size(pp+pos, NULL);
00293         if (sz > buf->vldsz - pos) return; 
00294 
00295         tDER* tmp = new_DER_node();
00296         int len = set_DER_node(tmp, pp+pos); 
00297         if (len==0) {
00298             del_DER_node(&tmp);
00299             return;
00300         }
00301         //
00302         add_DER_node(der, tmp);
00303         if (tmp->ldat.id & JBXL_ASN1_CNSTRCTD) {
00304             _DER_parse_children(tmp, NULL);
00305             free_Buffer(&(tmp->ldat.val));      // メモリが勿体ないので解放
00306         }
00307         pos += len;
00308 
00309     } while (pos<buf->vldsz);
00310     
00311     return;
00312 }

Here is the call graph for this function:

Here is the caller graph for this function:

long int bin2int_DER ( Buffer  buf  ) 

Definition at line 168 of file asn1_tool.cpp.

References Buffer::buf, OFF, ON, and Buffer::vldsz.

Referenced by asn1_print_node_value().

00169 {
00170     if (buf.buf==NULL || buf.vldsz==0) return 0;
00171 
00172     int i;
00173     int sz = buf.vldsz;
00174     unsigned char* pp = buf.buf;
00175 
00176     int minus = OFF;
00177     if (*pp & 0x80) {   // 負数
00178         minus = ON;
00179         for (i=0; i<sz; i++) pp[i] = pp[i] ^ 0xff;  // bit反転
00180     }
00181 
00182     long int ret = pp[sz-1];
00183     for (i=sz-2; i>=0; i--) {
00184         ret += pp[i] * 256;
00185     }
00186     if (minus) ret = - (ret + 1);
00187 
00188     return ret;
00189 }

Here is the caller graph for this function:

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() で消去すること.
tDER {
    tList_data  lvat {              データを格納
        int                 id      ノードの種類
        int                 lv      データ全体のサイズ(実際に格納されているサイズ)
        Buffer              key     未使用
        Buffer  val {               ノード値を格納
            .....
            int             vldsz   ノード値のサイズ. (実際に格納されているサイズ)
            int             state   ノードデータの状態
            unsigned char*  buf     ノード値へのポインタ
        } 
        ..... 
    } 
    .....
}

Definition at line 261 of file asn1_tool.cpp.

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

00262 {
00263     if (buf==NULL || buf->buf==NULL) return NULL;
00264 
00265     if (der==NULL) {
00266         der = new_DER_node();
00267         der->ldat.id = JBXL_ASN1_ANCHOR;
00268         der->ldat.lv = buf->vldsz;
00269         der->state   = JBXL_STATE_ANCHOR;
00270     }
00271     
00272     int sz = get_DER_size(buf->buf, NULL);
00273     if (sz > buf->vldsz) return NULL;     // データが短い
00274 
00275     _DER_parse_children(der, buf);
00276     
00277     return der;
00278 }

Here is the call graph for this function:

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 354 of file asn1_tool.cpp.

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

00355 {
00356     if (buf==NULL) return 0;
00357 
00358     int cnt = 0;
00359     int len = 0;
00360     cnt++;
00361     buf++;
00362     if (*buf>=0x80) {
00363         int i;
00364         int sz = (int)(*buf - 0x80);
00365         cnt++;
00366         buf++;
00367         for (i=0; i<sz; i++) {
00368             len = len*256 + (int)(*buf);
00369             cnt++;
00370             buf++;
00371         } 
00372     }
00373     else {
00374         len  = (int)(*buf);
00375         cnt++;
00376         buf++;
00377     }
00378     
00379     if (valsz!=NULL) *valsz = len;
00380     cnt += len;
00381 
00382     return cnt;
00383 }

Here is the caller graph for this function:

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 205 of file asn1_tool.cpp.

References JBXL_ASN1_BIT, and Buffer::vldsz.

Referenced by node2DER().

00206 {
00207     int  sz, div, cnt;
00208 
00209     sz = pt.vldsz;
00210     if (node==JBXL_ASN1_BIT) sz++;    // 未使用bit数を格納する先頭データ用
00211 
00212     cnt = 1;                    // ノード長のバイト数
00213     div = sz;
00214     if (div>=128) cnt++;        // ノード長のバイト数が2以上の場合
00215     while (div>=256) {
00216         div >>= 8;
00217         cnt++;
00218     }
00219     sz = sz + cnt + 1;          // 1=>node
00220     return sz;
00221 }

Here is the caller graph for this function:

Buffer int2bin_DER ( long int  n  ) 

Buffer int2bin_DER(long int n)

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

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

Definition at line 133 of file asn1_tool.cpp.

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

00134 {
00135     int  ii, cnt;
00136     unsigned long int div;
00137     Buffer bin;
00138 
00139     cnt = 1;
00140     div = n;
00141     while(div>=256) {
00142         div >>= 8;
00143         cnt++;
00144     }
00145     bin = make_Buffer(cnt+1);           // cnt : バイト数
00146     ii = cnt - 1;
00147 
00148     while(n>=256) {
00149         bin.buf[ii--] = (unsigned char)(n % 256);
00150         n >>= 8;
00151     }
00152     bin.buf[0] = (unsigned char)n;
00153 
00154     // 負数ではない場合 先頭に 0x00 を追加
00155     if (n>0 && bin.buf[0]>=0x80) {     
00156         for (ii=cnt; ii>0; ii--) bin.buf[ii] = bin.buf[ii-1];
00157         bin.buf[0] = 0x00;
00158         cnt++;
00159     }
00160 
00161     bin.vldsz  = cnt;
00162 
00163     return bin;
00164 }

Here is the call graph for this function:

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 73 of file asn1_tool.cpp.

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

00074 {
00075     int  sz, len, pp;
00076     unsigned char cnt;
00077     Buffer    buf;
00078 
00079     len = get_size_toDER(pt, node);  
00080     buf = make_Buffer(len);
00081     sz = pt.vldsz;
00082     pp = len - sz;                  // ノード値の格納場所の先頭
00083     memcpy(buf.buf+pp, pt.buf, sz);
00084 
00085     pp--;
00086     if (node==JBXL_ASN1_INT) {
00087 /*
00088         int2bin_DER() で処理済み
00089         if (pt.buf[0]>=0x80) {
00090             sz++;
00091             buf.buf[pp--] = 0x00;
00092         }
00093 */
00094     }
00095     else if (node==JBXL_ASN1_BIT) {
00096         sz++;
00097         buf.buf[pp--] = 0x00;
00098     }
00099 
00100     buf.buf[0] = node;
00101 
00102     cnt = 0;
00103     if (sz>=128) {
00104         cnt++;
00105         while (sz>=256) {
00106             buf.buf[pp--] = sz % 256;
00107             sz >>= 8;
00108             cnt++;
00109         }
00110         buf.buf[pp] = (unsigned char)sz;
00111         buf.buf[1]  = 0x80 + cnt;
00112     }
00113     else {
00114         buf.buf[1] = (unsigned char)sz;
00115     }
00116 
00117     buf.vldsz = len;
00118     return buf;
00119 }

Here is the call graph for this function:

void print_tDER ( FILE *  fp,
tDER pp 
)

void print_tDER(FILE* fp, tDER* pp)

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

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

Definition at line 395 of file asn1_tool.cpp.

References asn1_print_node(), asn1_print_node_value(), and print_tDER().

Referenced by print_tDER().

00396 {
00397     if (fp==NULL) fp = stderr;
00398 
00399     if (pp!=NULL) {
00400         while(pp->esis!=NULL) pp = pp->esis;
00401         do {
00402             int i;
00403             tList_data ld = pp->ldat;
00404 
00405             for(i=0; i<pp->depth; i++) fprintf(fp, "    ");
00406             if (pp->depth>0) fprintf(fp, " -> ");
00407             fprintf(fp, "%d: ", pp->depth);
00408             asn1_print_node(fp, ld.id);
00409             fprintf(fp, "%d,%d ", ld.lv, ld.val.vldsz);
00410             asn1_print_node_value(fp, ld.id, ld.val);
00411             fprintf(fp, "\n");
00412 
00413             if (pp->next!=NULL) print_tDER(fp, pp->next);
00414 
00415             pp = pp->ysis;
00416             //if (pp!=NULL) {
00417             //    for(i=1; i<pp->depth-1; i++) fprintf(fp, "           ");      // for " -> "
00418             //}
00419         } while(pp!=NULL);
00420     }
00421     else {
00422         fprintf(fp, "(Tree is NULL)\n");
00423     }
00424     fflush(fp);
00425 
00426     return;
00427 }

Here is the call graph for this function:

Here is the caller graph for this function:

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 326 of file asn1_tool.cpp.

References get_DER_size(), and set_Buffer().

Referenced by _DER_parse_children().

00327 {
00328     if (der==NULL || buf==NULL) return 0;
00329 
00330     int len = 0;
00331     int cnt = get_DER_size(buf, &len);
00332 
00333     der->ldat.id  = (int)(*buf);         // buf[0] Tag
00334     der->ldat.lv  = cnt;
00335     der->ldat.val = set_Buffer((void*)(buf+cnt-len), len);
00336 
00337     return cnt;
00338 }

Here is the call graph for this function:

Here is the caller graph for this function:

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 36 of file asn1_tool.cpp.

References Buffer::buf.

00037 {
00038     int i, sz=-1;
00039 
00040     if ((unsigned char)param.buf[ls]==node) {
00041         if ((unsigned char)param.buf[ls+1]>=0x80) {
00042             sz = (int)param.buf[ls+1] - 128;
00043             *lp = 0;
00044             for (i=ls+2; i<ls+sz+2; i++) {
00045                 *lp *= 256;
00046                 *lp += (unsigned char)param.buf[i];
00047             }    
00048         }
00049         else {
00050             sz = 0;
00051             *lp = (int)param.buf[ls+1];
00052         }
00053         //if ((unsigned char)param.buf[ls]==JBXL_ASN1_BIT) sz++;
00054         sz = ls + sz + 2;
00055     }
00056 
00057     return sz;
00058 }


Generated on 15 Nov 2023 for JunkBox_Lib++ (for Windows) by  doxygen 1.6.1