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 58 of file asn1_tool.h.

Referenced by _DER_parse_children().

#define del_DER (  )     del_tTree((a))

Definition at line 57 of file asn1_tool.h.

#define del_DER_node (  )     del_tTree_node((a))

Definition at line 56 of file asn1_tool.h.

Referenced by _DER_parse_children().

 
#define new_DER_node (  )     new_tTree_node()

Definition at line 55 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 262 of file asn1_tool.c.

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().

00263 {
00264     if (der==NULL) return;
00265     if (buf==NULL) buf = &(der->ldat.val);
00266     if (buf->buf==NULL) return;
00267 
00268     unsigned char* pp = buf->buf;
00269 
00270     int pos = 0;
00271     do {
00272         int sz = get_DER_size(pp+pos, NULL);
00273         if (sz > buf->vldsz - pos) return; 
00274 
00275         tDER* tmp = new_DER_node();
00276         int len = set_DER_node(tmp, pp+pos); 
00277         if (len==0) {
00278             del_DER_node(&tmp);
00279             return;
00280         }
00281         //
00282         add_DER_node(der, tmp);
00283         if (tmp->ldat.id & JBXL_ASN1_CNSTRCTD) {
00284             _DER_parse_children(tmp, NULL);
00285             free_Buffer(&(tmp->ldat.val));      // メモリが勿体ないので解放
00286         }
00287         pos += len;
00288 
00289     } while (pos<buf->vldsz);
00290     
00291     return;
00292 }

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

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

Referenced by asn1_print_tag_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() で消去すること.

Definition at line 241 of file asn1_tool.c.

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

00242 {
00243     if (buf==NULL || buf->buf==NULL) return NULL;
00244 
00245     if (der==NULL) {
00246         der = new_DER_node();
00247         der->ldat.id = JBXL_ASN1_ANCHOR;
00248         der->ldat.lv = buf->vldsz;
00249         der->state   = JBXL_STATE_ANCHOR;
00250     }
00251     
00252     int sz = get_DER_size(buf->buf, NULL);
00253     if (sz > buf->vldsz) return NULL;     // データが短い
00254 
00255     _DER_parse_children(der, buf);
00256     
00257     return der;
00258 }

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 334 of file asn1_tool.c.

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

00335 {
00336     if (buf==NULL) return 0;
00337 
00338     int cnt = 0;
00339     int len = 0;
00340     cnt++;
00341     buf++;
00342     if (*buf>=0x80) {
00343         int i;
00344         int sz = (int)(*buf - 0x80);
00345         cnt++;
00346         buf++;
00347         for (i=0; i<sz; i++) {
00348             len = len*256 + (int)(*buf);
00349             cnt++;
00350             buf++;
00351         } 
00352     }
00353     else {
00354         len  = (int)(*buf);
00355         cnt++;
00356         buf++;
00357     }
00358     
00359     if (valsz!=NULL) *valsz = len;
00360     cnt += len;
00361 
00362     return cnt;
00363 }

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 204 of file asn1_tool.c.

References JBXL_ASN1_BIT, and Buffer::vldsz.

Referenced by node2DER().

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

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

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

Referenced by join_DHpubkey().

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:

Here is the caller 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.c.

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

Referenced by join_DHpubkey().

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:

Here is the caller 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 表示を開始するノードへのポインタ.

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

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 385 of file asn1_tool.c.

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

Referenced by print_tDER().

00386 {
00387     if (fp==NULL) fp = stderr;
00388 
00389     if (pp!=NULL) {
00390         while(pp->esis!=NULL) pp = pp->esis;
00391         do {
00392             int i;
00393             tList_data ld = pp->ldat;
00394 
00395             for(i=0; i<pp->depth; i++) fprintf(fp, "    ");
00396             if (pp->depth>0) fprintf(fp, " -> ");
00397             fprintf(fp, "%d: ", pp->depth);
00398             asn1_print_id(fp, ld.id);
00399             fprintf(fp, "%d, %d ", ld.lv, ld.val.vldsz);
00400             asn1_print_tag_value(fp, ld.id, ld.val);
00401             fprintf(fp, "\n");
00402 
00403             if (pp->next!=NULL) print_tDER(fp, pp->next);
00404 
00405             pp = pp->ysis;
00406             //if (pp!=NULL) {
00407             //    for(i=1; i<pp->depth-1; i++) fprintf(fp, "           ");      // for " -> "
00408             //}
00409         } while(pp!=NULL);
00410     }
00411     else {
00412         fprintf(fp, "(Tree is NULL)\n");
00413     }
00414     fflush(fp);
00415 
00416     return;
00417 }

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 306 of file asn1_tool.c.

References get_DER_size(), and set_Buffer().

Referenced by _DER_parse_children().

00307 {
00308     if (der==NULL || buf==NULL) return 0;
00309 
00310     int len = 0;
00311     int cnt = get_DER_size(buf, &len);
00312 
00313     der->ldat.id  = (int)(*buf);         // buf[0] Tag
00314     der->ldat.lv  = cnt;
00315     der->ldat.val = set_Buffer((void*)(buf+cnt-len), len);
00316 
00317     return cnt;
00318 }

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

References Buffer::buf.

Referenced by get_DHalgorism(), get_DHGkey(), get_DHPkey(), get_DHYkey(), and join_DHpubkey().

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 }

Here is the caller graph for this function:


Generated on 15 Nov 2023 for JunkBox_Lib by  doxygen 1.6.1