ASN1.Rd
ASN1.decode
decodes ASN.1
binary format into
raw format chunks tagged with class types.
ASN1.encode
converts structured objects into
ASN.1
binary format.
ASN1.item
creates an item - basic object in
structures that can be encoded using ASN1.encode
.
ASN1.type
extracts the class type from an
ASN.1
item
ASN1.decode(what)
ASN1.encode(what)
ASN1.item(what, type)
ASN1.type(what)
object to decode/encode/query
class type of the item (integer value)
This is a suite of low-level tools to deal with ASN.1 (Abstract Syntax
Notation One) binary formats DER, BER and CER. The tools were written
specifically to handle the various DER-encoded key structures so it
provides only a subset of the ASN.1 specification. They are used
internally by the PKI
poackage.
ASN1.decode
decodes the binary representation (as raw vector)
into individual items. Sequences are convered into lists, all other
objects are retained in their binary form and tagged with the integer
class type - which can be obtained using ASN1.type
function.
ASN1.encode
expects item (or a list of items) either created
using ASN1.decode
or ASN1.item
and converts them into
DER
binary format.
The result of ASN1.encode(ASN1.decode(x))
will be x
if
x
was in DER
format.
ASN1.decode
returns either one item or a list.
ASN1.encode
returns a raw vector in DER format.
ASN1.type
returns an integer class type
ASN1.item
returns an ASN.1 item object
ASN1.encode
uses a fixed buffer for encoding which currently
limits the total size of the resulting structure to 1MB.
Only definite length forms are supported. The validity of individual items is not checked.
# generate a small key
key <- PKI.genRSAkey(bits = 512L)
# extract private and public parts in DER format
prv <- PKI.save.key(key, format="DER")
pub <- PKI.save.key(key, private=FALSE, format="DER")
# parse the public key
x <- ASN1.decode(pub)
x
#> [[1]]
#> [[1]][[1]]
#> [1] 2a 86 48 86 f7 0d 01 01 01
#> attr(,"type")
#> [1] 6
#>
#> [[1]][[2]]
#> raw(0)
#> attr(,"type")
#> [1] 5
#>
#>
#> [[2]]
#> [1] 30 48 02 41 00 9b c1 d9 83 20 2d 7b 43 f7 39 ca dd 4d 57 52 57 3f 65 b2 7a
#> [26] 31 7c 48 50 66 2c 74 af e1 02 7e da 00 10 72 93 04 93 7b 9c ad 8c 01 31 6a
#> [51] e9 dc a9 d7 e4 a9 9f 57 5e df 4a 3a 6b 81 60 aa 90 77 81 02 03 01 00 01
#> attr(,"type")
#> [1] 3
#> attr(,"padded.bits")
#> [1] 0
#>
# the second element is the actual key
# as a bit string that's itself in DER
# two integers - modulus and exponent
# Note that this is in fact the pure PKCS#1 key format
ASN1.decode(x[[2]])
#> [[1]]
#> [1] 00 9b c1 d9 83 20 2d 7b 43 f7 39 ca dd 4d 57 52 57 3f 65 b2 7a 31 7c 48 50
#> [26] 66 2c 74 af e1 02 7e da 00 10 72 93 04 93 7b 9c ad 8c 01 31 6a e9 dc a9 d7
#> [51] e4 a9 9f 57 5e df 4a 3a 6b 81 60 aa 90 77 81
#> attr(,"type")
#> [1] 2
#>
#> [[2]]
#> [1] 01 00 01
#> attr(,"type")
#> [1] 2
#>
# encoding it back should yield the same representation since it is DER
stopifnot(identical(ASN1.encode(x), as.raw(pub)))