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.
Value
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
Note
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.
Author(s)
Simon Urbanek
Examples
# 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
# 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]])
# encoding it back should yield the same representation since it is DER
stopifnot(identical(ASN1.encode(x), as.raw(pub)))
Results
R version 3.3.1 (2016-06-21) -- "Bug in Your Hair"
Copyright (C) 2016 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu (64-bit)
R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.
R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.
Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.
> library(PKI)
Loading required package: base64enc
> png(filename="/home/ddbj/snapshot/RGM3/R_CC/result/PKI/ASN1.Rd_%03d_medium.png", width=480, height=480)
> ### Name: ASN1
> ### Title: Functions for handling ASN.1 format (typically DER)
> ### Aliases: ASN1 ASN1.decode ASN1.encode ASN1.item ASN1.type
> ### Keywords: manip
>
> ### ** Examples
>
> # 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 b0 de c1 59 8d 95 7d 7a ec ba 80 b9 f8 ac 27 74 c6 07 89 fc
[26] 42 fa 83 b9 5f ae b4 7c b1 6a d9 bc 9c 8c ed de 66 26 0b b4 03 67 79 f5 6a
[51] 41 89 45 5f 4a 5f 6d 64 f1 42 91 c1 4e 51 1f 8c 95 28 f1 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 b0 de c1 59 8d 95 7d 7a ec ba 80 b9 f8 ac 27 74 c6 07 89 fc 42 fa 83 b9
[26] 5f ae b4 7c b1 6a d9 bc 9c 8c ed de 66 26 0b b4 03 67 79 f5 6a 41 89 45 5f
[51] 4a 5f 6d 64 f1 42 91 c1 4e 51 1f 8c 95 28 f1
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)))
>
>
>
>
>
>
> dev.off()
null device
1
>