PKI/ASN.1

ASN.1 ์˜คํ”ˆ์†Œ์Šค ์ปดํŒŒ์ผ๋Ÿฌ asn1c ์‚ฌ์šฉ๋ฒ•

JayKim๐Ÿ™‚ 2023. 5. 9. 10:21

ASN.1 ์ •๋ณด๋ฅผ ๊ฐ€์ง€๊ณ  ๊ฐœ๋ฐœ ํ•˜๊ธฐ ์œ„ํ•ด์„œ ASN.1 ์ปดํŒŒ์ผ๋Ÿฌ๊ฐ€ ํ•„์š”ํ•˜๋‹ค.
์‹ค์ œ๋กœ ์ƒ์šฉ ๋ฒ„์ „์˜ ์ปดํŒŒ์ผ๋Ÿฌ๋Š” ๋ผ์ด์„ผ์Šค ๋น„์šฉ์ด ๋น„์‹ธ๋‹ค
์—ฌ๊ธฐ์„œ๋Š” ํ˜„์žฌ ๋ฌด๋ฃŒ๋กœ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•œ ์˜คํ”ˆ์†Œ์Šค์ธ asn1c ๊ธฐ๋ณธ ์‚ฌ์šฉ๋ฒ•์ด๋‹ค.

ํ•ด๋‹น ํˆด์˜ ํ™ˆํŽ˜์ด์ง€์—์„œ ๊ด€๋ จ ์„ค๋ช…๊ณผ GitHub ์—์„œ ์†Œ์Šค๋ฅผ ๋‹ค์šด ๋ฐ›์„ ์ˆ˜ ์žˆ๋‹ค.
๊ทธ๋Ÿผ asn1c ์— ๋Œ€ํ•ด ์‚ฌ์šฉ๋ฒ•์„ ์•Œ์•„๋ณด์ž.

ASN1C ์„ค์น˜

asn1c ๋ฅผ ์†Œ์ŠคํŒŒ์ผ์„ ํ’€์–ด ๋ณด์ž
ํ˜„์žฌ ๋‹ค์šด ๋ฒ„์ „์€ 0.9.28 ๋ฒ„์ „์ด๋‹ค.

tar zxvf asn1c-0.9.28.tar.gz
cd asn1c-0.9.28

./configure
make
make install

์„ค์น˜๋Š” ๊ฐ„๋‹จํžˆ ํ•œ๋‹ค.
์ฐธ๊ณ ๋กœ make install ์€ root ๊ถŒํ•œ์ด ํ•„์š”ํ•˜๋‹ค.
root ๊ถŒํ•œ์ด ์•„๋‹ˆ๋ผ๋ฉด sudo make install ์„ ํ•ด์ฃผ๋ฉด ๋œ๋‹ค.
๋˜๋Š” ์„ค์น˜ ํ™˜๊ฒฝ์— ๋”ฐ๋ผ ๋ฃจํŠธ ๊ถŒํ•œ์„ ์ด์šฉํ•˜๋ฉด ๋œ๋‹ค.

๊ธฐ๋ณธ์ ์œผ๋กœ๋Š” /usr/local/bin/asn1c ์œ„์น˜์— asn1c ๋ช…๋ น์–ด๊ฐ€ ์„ค์น˜ ๋œ๋‹ค.
๋งŒ์•ฝ ์„ค์น˜ ๊ฒฝ๋กœ๋ฅผ ๋ณ€๊ฒฝ ํ•˜๋ ค๋ฉด ./configure --prefix=/path ์˜ต์…˜์„ ์‚ฌ์šฉํ•ด์„œ ๋ณ€๊ฒฝ ํ•˜๋ฉด ๋œ๋‹ค.
prefix ์˜ต์…˜์„ ์‚ฌ์šฉํ•ด ๋ฃจํŠธ ๊ถŒํ•œ์ด ํ•„์š”ํ•˜์ง€ ์•Š๋Š” ์œ„์น˜๋ผ๋ฉด make install ๋งŒ ํ•ด์ฃผ์–ด์„œ ์‚ฌ์šฉ ํ•˜๋ฉด ๋œ๋‹ค.

์ด๋ ‡๊ฒŒ ์„ค์น˜๊ฐ€ ๋˜๋ฉด asn1c ๋ฐ”์ด๋„ˆ๋ฆฌ ํŒŒ์ผ์ด ์ƒ์„ฑ ๋˜๋Š”๋ฐ ์ด ๋ช…๋ น์–ด์— ๋Œ€ํ•œ man ์œผ๋กœ ๊ธฐ๋Šฅ ์˜ต์…˜์„ ํ™•์ธ ํ•  ์ˆ˜ ์žˆ๋‹ค.
๊ฐ„๋‹จํžˆ man asn1c ๋ผ๊ณ  ์ž…๋ ฅ ํ•˜๋ฉด ์•„๋ž˜ ์ฒ˜๋Ÿผ ์„ค๋ช…์ด ๋‚˜์˜จ๋‹ค.

man asn1c

NAME
       asn1c -- the ASN.1 Compiler

SYNOPSIS
       asn1c [-E [-F] | -P | -R]
       [-Sdir] [-X]
       [-Wdebug-...] [-foption] [-gen-option] [-pdu={all|auto|Type}]
       [-print-option]
       input-filenames...

DESCRIPTION
       asn1c compiles ASN.1 specifications into a set of target language
       (C/C++) encoders and decoders for BER, DER, PER, XER and other encoding
       rules.

๋กœ ๊ฐ„๋‹จํžˆ ๋ช…๋ น์–ด์— ๋Œ€ํ•œ ์„ค๋ช…์„ ํ™•์ธ ํ•  ์ˆ˜ ์žˆ๋‹ค.

ASN1C ๊ธฐ๋ณธ ์‚ฌ์šฉ๋ฒ•

์˜ˆ์ œ ASN1 ์•„๋ž˜ ์ฒ˜๋Ÿผ ์žˆ๋‹ค๊ณ  ํ•˜์ž

TestModule DEFINITIONS ::= BEGIN -- Module parameters preamble
     Circle ::= SEQUENCE { -- Definition of Circle type
    position-x INTEGER, -- Integer position
     position-y INTEGER, -- Position along Y-axis
     radius INTEGER (0..MAX) -- Positive radius
     } -- End of Circle type
END -- End of TestModule

์ด ๋‚ด์šฉ์ด๋ฅด ex.asn1 ํŒŒ์ผ๋กœ ์ €์žฅ์„ ํ•˜์ž

ASN1.C ๋ฌธ๋ฒ• ํ…Œ์ŠคํŠธ

์ด ๋ชจ๋“ˆ์„ ์ปดํŒŒ์ผ์„ ํ•˜๋ฉด ๋‹ค์Œ ๋ช…๋ น์–ด๋ฅผ ์‹คํ–‰ ํ•œ๋‹ค.

asn1c -EF ex1.asn1

์ด๋ ‡๊ฒŒ ์—๋Ÿฌ๊ฐ€ ์—†์ด ASN1 ๋ชจ๋“ˆ ๊ฐ’์ด ๋‚˜์˜ค๋ฉด ์ •์ƒ ํŒŒ์ผ์ด๋‹ค.

BER ๋“ฑ ์—ฌ๋Ÿฌ๊ฐ€์ง€ ์ธ์ฝ”๋“œ/๋””์ฝ”๋“œ ์†Œ์Šค ๋งŒ๋“ค๊ธฐ

asn1c ex.asn1

์ด ๋ช…๋ น์–ด๋ฅผ ์„ฑ๊ณต์ ์œผ๋กœ ๋งˆ์น˜๋ฉด ์—ฌ๋Ÿฌ๊ฐ€์ง€ ์†Œ์Šค ํŒŒ์ผ์ด ์ƒ์„ฑ๋œ๋‹ค.
๋ณดํ†ต ์ƒ์šฉ ์ปดํŒŒ์ผ๋Ÿฌ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ž‘ ํ—ค๋”๋ฅผ ์ƒ์„ฑ์„ ํ•ด์ฃผ๋Š”๋ฐ
asn1c ๊ฒฝ์šฐ ์†Œ์Šค๋ฅผ ์ƒ์„ฑ ํ•œ๋‹ค.

์ฐธ๊ณ ๋กœ ๊ฐ„๋‹จํ•œ ๋ชจ๋“ˆ์ด๋ผ๋„ ๊ธฐ๋ณธ์ ์œผ๋กœ ์ƒ์„ฑํ•ด์•ผํ•˜๋Š” ASN.1 ๊ธฐ๋Šฅ ์†Œ์Šค๊ฐ€ ์žˆ์–ด์„œ ์ƒ๋‹นํžˆ ๋งŽ์€ ํŒŒ์ผ์ด ์ƒ๊ธด๋‹ค.
์˜ˆ์ œ๋กœ ์ƒ๊ธด ํŒŒ์ผ์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค

jykim@jykim-VirtualBox:~/work/test$ ls
asn_application.h  BIT_STRING.h        INTEGER.c           per_opentype.c
asn_codecs.h       Circle.c            INTEGER.h           per_opentype.h
asn_codecs_prim.c  Circle.h            Makefile.am.sample  per_support.c
asn_codecs_prim.h  constraints.c       NativeEnumerated.c  per_support.h
asn_internal.h     constraints.h       NativeEnumerated.h  xer_decoder.c
asn_system.h       constr_SEQUENCE.c   NativeInteger.c     xer_decoder.h
ber_decoder.c      constr_SEQUENCE.h   NativeInteger.h     xer_encoder.c
ber_decoder.h      constr_TYPE.c       OCTET_STRING.c      xer_encoder.h
ber_tlv_length.c   constr_TYPE.h       OCTET_STRING.h      xer_support.c
ber_tlv_length.h   converter-sample.c  per_decoder.c       xer_support.h
ber_tlv_tag.c      der_encoder.c       per_decoder.h
ber_tlv_tag.h      der_encoder.h       per_encoder.c
BIT_STRING.c       ex.asn1             per_encoder.h

์ƒ์„ฑ๋œ ํŒŒ์ผ์„ ๋ณด๋ฉด ber, der, per, xer ์ธ์ฝ”๋”ฉ ๋””์ฝ”๋”ฉ API ๊ฐ€ ์ƒ์„ฑ ๋œ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค.
๊ธฐ๋ณธ์ ์œผ๋กœ BER ๊ณผ PER์€ ๋””ํดํŠธ๋กœ ์ง€์›ํ•œ๋‹ค.
๊ทธ๋ฆฌ๊ณ  BER๊ณผ PER ๋””ํดํŠธ ์ง€์›์— PER ํ•˜๋‚˜๋งŒ ์ง€์›์„ ํ•˜๋ ค๋ฉด ๋‹ค์Œ ์ฒ˜๋Ÿผ ์‚ฌ์šฉํ•œ๋‹ค.

asn1c -gen-PER ex.asn1

์ƒ์„ฑ๋œ ์†Œ์Šค ์ฝ”๋“œ ์ปดํŒŒ์ผ

cc -DPDU=Circle -I. -o EX.ex *.c 

์ด ๋ช…๋ น์–ด๋ฅผ ์‹คํ–‰ํ•˜๋ฉด EX.ex ๋ผ๋Š” ๋ฐ”์ด๋„ˆ๋ฆด๊ฐ€ ์ƒ์„ฑ ๋œ๋‹ค.
convert-samlep.c ๊ฐ€ ๋ฉ”์ธ ์†Œ์Šค์ด๋‹ค.

Usage: ./EX.ex [options] <data.ber> ...
Where options are:
  -iper        Input is in Unaligned PER (Packed Encoding Rules) (DEFAULT)
  -iber        Input is in BER (Basic Encoding Rules)
  -ixer        Input is in XER (XML Encoding Rules)
  -oper        Output in Unaligned PER (Packed Encoding Rules)
  -oder        Output in DER (Distinguished Encoding Rules)
  -oxer        Output in XER (XML Encoding Rules) (DEFAULT)
  -otext       Output in plain semi-structured text (dump)
  -onull       Verify (decode) input, but do not output
  -per-nopad   Assume PER PDUs are not padded (-iper)
  -1           Decode only the first PDU in file
  -b <size>    Set the i/o buffer size (default is 8192)
  -c           Check ASN.1 constraints after decoding
  -d           Enable debugging (-dd is even better)
  -n <num>     Process files <num> times
  -s <size>    Set the stack usage limit (default is 30000)

์—ฌ๊ธฐ์„œ ๊ฐœ๋ฐœ ํ”„๋กœ๊ทธ๋žจ ๋‚ด์—์„œ ์‚ฌ์šฉ ํ•˜๋ ค๋ฉด ์ด conver-sample.c ์†Œ์Šค๋ฅผ ์ œ์™ธํ•˜๊ณ  ๊ฐ™์ด ํฌํ•จํ•˜๊ฑฐ๋‚˜
๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋กœ ๋งŒ๋“ค์–ด ์‚ฌ์šฉํ•˜๋ฉด ๋œ๋‹ค.

RFC ๋ฌธ์„œ๋ฅผ ASN1 ํŒŒ์ผ ๋ณ€ํ™˜

asn1c ๋ฅผ ์„ค์น˜ํ•˜๋ฉด examples ํŒŒ์ผ์— crfc2asn.pl ์ด๋ผ๋Š” Perl ํˆด์ด ์žˆ๋Š”๋ฐ
์ด ํˆด์€ RFC ๋ฌธ์„œ ๋‚ด์— ์žˆ๋Š” ASN.1 ๋ฌธ๋ฒ• ํŒŒ์ผ์„ ์ถ”์ถœ ํ•˜๋Š” ์œ ํ‹ธ์ด๋‹ค
์‚ฌ์šฉ๋ฒ•์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค

./crfc2asn1.pl 
Extract the ASN.1 specification from the RFC file(s).
Usage 1: ./crfc2asn1.pl <rfc-file.txt> ...
Usage 2: <someprog> | ./crfc2asn1.pl

์ด ํˆด์„ ์ด์šฉํ•ด RFC๋ฌธ์„œ์—์„œ ๊ฐ„๋‹จํžˆ ASN1 ๋ฌธ๋ฒ•์„ ๊ฐ€์ ธ์™€์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

์ฐธ๊ณ ๋กœ ๋ชจ๋“  ๋ฌธ์„œ๊ฐ€ ์ž˜ ๋˜์ง€๋Š” ์•Š์•˜๋‹ค.
ํ•„์š”์‹œ ๋ฌธ์„œ๋ฅผ ์ˆ˜์ • ํ›„ ์‹คํ–‰ ํ•ด์•ผ ํ•œ๋‹ค.

ํ™ˆํŽ˜์ด์ง€์—์„œ ๋‹ค์šด๋ฐ›์€ ๊ฐ€์ด๋“œ ํŒŒ์ผ์ด๋‹ค

asn1c-usage.pdf
0.20MB

๊ฐœ์ธ์ ์œผ๋กœ OpenSSL ์„ ์‚ฌ์šฉํ•˜๋ฉด ASN.1 ์ปดํŒŒ์ผ๋Ÿฌ๋ฅผ ๋ณ„๋ฃจ ์“ฐ์ง€๋Š” ์•Š๋Š”๋‹ค
๊ฐ€๋” OpenSSL ์—์„œ ์ •์˜ ๋˜์ง€ ์•Š์€ ASN.1 ๋ชจ๋“ˆ์ด ํ•„์š”์‹œ ์ง์ ‘ ์ถ”๊ฐ€ํ•ด์„œ ์‚ฌ์šฉํ•œ๋‹ค.

ํ•˜์ง€๋งŒ BER ๋˜๋Š” DER ์ธ์ฝ”๋”ฉ์ด ์•„๋‹ˆ๋ผ PER ๊ฐ™์€ ์ธ์ฝ”๋”ฉ์„ ์‚ฌ์šฉํ•œ๋‹ค๋ฉด OpenSSL ์€ ์ง€์› ํ•˜์ง€ ์•Š๋Š”๋‹ค.
์ด๋•Œ๋Š” asn1c๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค.
asn1c ๋„ ํ•„์š”ํ•ด์„œ ์‚ฌ์šฉ๋ฒ• ์ •๋ฆฌ๋ฅผ ํ•ด ๋ณด์•˜๋‹ค.

'PKI > ASN.1' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

CER( Canonical Encoding Rules ) ์˜ ํŠน์„ฑ (DER๊ณผ ๋น„๊ต)  (0) 2023.06.30
ASN.1 ์ธ์ฝ”๋”ฉ ๋ฐฉ์‹  (0) 2023.06.29
ECDSA ๊ฐœ์ธํ‚ค ASN.1 ํ˜•์‹  (0) 2023.05.02
Distinguished Encoding Rules  (0) 2022.11.07
Basic Encoding Rules (3/3)  (0) 2022.11.07