Module asn1
asn1 module to generate or parse ASN.1 data.
Provide asn1_object, asn1_string, asn1_object as lua object.
Usage:
asn1 = require('openssl').asn1
Functions
new_type (value) | create asn1_type object |
get_object (der[, start=1[, stop=-i]]) | parse der encoded string |
put_object (tag, class[, length=nil[, constructed=nil]]) | do der encode and return encoded string partly head or full |
tostring (clsortag, range) | make tag, class number to string |
new_string (data[, type]) | create asn1_string object |
new_integer (value) | create asn1_integer object |
new_generalizedtime (time) | create asn1_time object using generalized time format |
new_utctime (time) | create asn1_time object using UTC time format |
txt2nid (txt) | get nid for txt, which can be short name, long name, or numerical oid |
new_object (name_or_oid[, no_name=false]) | create asn1_object from string identifier |
new_object (nid) | create asn1_object from NID |
new_object (options) | create asn1_object from table definition |
asn1type_di2 (der) | convert der encoded asn1type string to object |
type () | get the ASN.1 type tag number |
octet ([data]) | get or set octet string data for asn1_type object |
cmp (other) | compare two asn1_type objects |
asn1string () | convert asn1_type to asn1_string object |
i2d () | serialize asn1_type object to DER encoded string |
info () | get detailed information about asn1_type object |
Class asn1_object
asn1_object:nid () | get nid of asn1_object. |
asn1_object:name () | get name of asn1_object. |
asn1_object:ln () | get long name of asn1_object. |
asn1_object:sn () | get short name of asn1_object. |
asn1_object:txt ([no_name]) | get text of asn1_object. |
asn1_object:equals (another) | compare two asn1_objects, if equals return true |
asn1_object:data () | get data of asn1_object |
asn1_object:dup () | make a clone of asn1_object |
asn1_object:d2i () | read der in to asn1_object |
asn1_object:i2d () | get der encoded of asn1_object |
Class asn1_integer
asn1_integer:bn ([number]) | convert ASN1 integer to/from big number |
Class asn1_string
asn1_string:set () | |
asn1_string:get () | |
asn1_string:i2d () | |
asn1_string:d2i () | |
asn1_string:type () | get type of asn1_string |
asn1_string:length () | get length two asn1_string |
asn1_string:data () | get data of asn1_string |
asn1_string:data (data) | set data of asn1_string |
asn1_string:equals (another) | compare two asn1_string, if equals return true |
asn1_string:__eq (other) | compare two ASN1 string objects for equality |
asn1_string:__tostring () | convert asn1_string to lua string |
asn1_string:toprint () | get data as printable encode string |
asn1_string:toutf8 () | get data as utf8 encode string |
asn1_string:dup () | duplicate a new asn1_string |
asn1_string:check () | check if asn1_time object is valid |
asn1_string:adj (time[, offset_day=0[, offset_sec=0]]) | adjust asn1_time by specified offset |
asn1_string:diff (to) | calculate difference between two asn1_time objects |
Functions
- new_type (value)
-
create asn1_type object
Parameters:
- value boolean, number, string or asn1_string value to create ASN1_TYPE from
Returns:
-
asn1_type or nil
new ASN1_TYPE object or nil on error
- get_object (der[, start=1[, stop=-i]])
-
parse der encoded string
Parameters:
- der string string
- start number offset to parse (default 1)
- stop number offset to parse this like string.sub() (default -i)
Returns:
- number tag
- number class
- number parsed data start offset
- number parsed data stop offset
- boolean true for constructed data
Or
- nil for fail
- string error msg
- number inner error code
- put_object (tag, class[, length=nil[, constructed=nil]])
-
do der encode and return encoded string partly head or full
Parameters:
- tag number
- class number
- length number or string or date to encode, defualt will make indefinite length constructed (default nil)
- constructed boolean or not (default nil)
Returns:
-
string
der encoded string or head when not give data
- tostring (clsortag, range)
-
make tag, class number to string
Parameters:
- clsortag number which to string
- range string only accept ‘class’ or ‘tag’
Returns:
-
string
result
- new_string (data[, type])
-
create asn1_string object
asn1_string object support types: “integer”, “enumerated”, “bit”, “octet”, “utf8”, “numeric”, “printable”, “t61”, “teletex”, “videotex”, “ia5”, “graphics”, “iso64”, “visible”, “general”, “unversal”, “bmp”, “utctime”
Parameters:
Returns:
-
asn1_string
- new_integer (value)
-
create asn1_integer object
Parameters:
- value number or bignum integer value or bignum
Returns:
-
asn1_integer
new ASN1_INTEGER object
See also:
- new_generalizedtime (time)
-
create asn1_time object using generalized time format
Parameters:
- time none, number or string
Returns:
-
asn1_time
See also:
- new_utctime (time)
-
create asn1_time object using UTC time format
Parameters:
- time none, number or string
Returns:
-
asn1_time
See also:
- txt2nid (txt)
-
get nid for txt, which can be short name, long name, or numerical oid
Parameters:
- txt string which get to nid
Returns:
-
integer
nid or nil on fail
- new_object (name_or_oid[, no_name=false])
-
create asn1_object from string identifier
Parameters:
- name_or_oid string short name (e.g., “C”), long name (e.g., “countryName”), or OID string (e.g., “2.5.4.6”)
- no_name boolean true for only oid string parsing, false to allow names (default false)
Returns:
-
asn1_object or nil
ASN1_OBJECT mapping or nil on error
See also:
Usage:
local obj1 = asn1.new_object("C") -- short name local obj2 = asn1.new_object("countryName") -- long name local obj3 = asn1.new_object("2.5.4.6") -- OID string
- new_object (nid)
-
create asn1_object from NID
Parameters:
- nid integer numeric identifier for the ASN1_OBJECT
Returns:
-
asn1_object or nil
ASN1_OBJECT mapping or nil on error
See also:
Usage:
local obj = asn1.new_object(14) -- NID for countryName
- new_object (options)
-
create asn1_object from table definition
Parameters:
- options table table with sn (short name), ln (long name), oid keys to create new asn1_object
Returns:
-
asn1_object or nil
ASN1_OBJECT mapping or nil on error
See also:
Usage:
local obj = asn1.new_object({ oid = "1.2.3.4.5.6", sn = "myShortName", ln = "myLongName" })
- asn1type_di2 (der)
-
convert der encoded asn1type string to object
Parameters:
- der string
Returns:
-
asn1type
object for success, and nil for fail
- type ()
-
get the ASN.1 type tag number
Returns:
-
number
the ASN.1 type tag number
- octet ([data])
-
get or set octet string data for asn1_type object
Parameters:
- data string optional octet string data to set (optional)
Returns:
- string current octet string data when called without parameters
- boolean true when setting data successfully
- cmp (other)
-
compare two asn1_type objects
Parameters:
- other asn1_type another asn1_type object to compare with
Returns:
-
boolean
true if both asn1_type objects are equal, false otherwise
- asn1string ()
-
convert asn1_type to asn1_string object
Returns:
-
asn1_string or nil
asn1_string object or nil if conversion is not supported
- i2d ()
-
serialize asn1_type object to DER encoded string
Returns:
-
string
DER encoded representation of the asn1_type object
- info ()
-
get detailed information about asn1_type object
Returns:
-
table
containing type information and data
Class asn1_object
openssl.asn1_object object
- asn1_object:nid ()
-
get nid of asn1_object.
Returns:
-
integer
nid of asn1_object
- asn1_object:name ()
-
get name of asn1_object.
Returns:
-
string
short name and followed by long name of asn1_object
- asn1_object:ln ()
-
get long name of asn1_object.
Returns:
-
string
long name of asn1_object
- asn1_object:sn ()
-
get short name of asn1_object.
Returns:
-
string
short name of asn1_object
- asn1_object:txt ([no_name])
-
get text of asn1_object.
Parameters:
- no_name boolean true for only oid or name, default with false (optional)
Returns:
-
string
long or short name, even oid of asn1_object
- asn1_object:equals (another)
-
compare two asn1_objects, if equals return true
Parameters:
- another asn1_object to compre
Returns:
-
boolean
true if equals
- asn1_object:data ()
-
get data of asn1_object
Returns:
-
string
asn1_object data
- asn1_object:dup ()
-
make a clone of asn1_object
Returns:
-
asn1_object
clone for self
- asn1_object:d2i ()
-
read der in to asn1_object
Returns:
-
boolean
- asn1_object:i2d ()
-
get der encoded of asn1_object
Returns:
Class asn1_integer
openssl.asn1_integer object
- asn1_integer:bn ([number])
-
convert ASN1 integer to/from big number
Parameters:
- number bn big number to set, or nil to get current value (optional)
Returns:
-
bn
big number representation if getting, or previous value if setting
Class asn1_string
openssl.asn1_string object
- asn1_string:set ()
- asn1_string:get ()
-
Returns:
-
userdata
object created
- asn1_string:i2d ()
-
Returns:
-
string
result
- asn1_string:d2i ()
-
Returns:
-
boolean
success status
- asn1_string:type ()
-
get type of asn1_string
Returns:
-
string
type of asn1_string
See also:
- asn1_string:length ()
-
get length two asn1_string
Returns:
-
integer
length of asn1_string
Usage:
local astr = asn1.new_string('ABCD') print('length:',#astr) print('length:',astr:length()) assert(#astr==astr:length,"must equals")
- asn1_string:data ()
-
get data of asn1_string
Returns:
-
string
raw data of asn1_string
- asn1_string:data (data)
-
set data of asn1_string
Parameters:
- data string set to asn1_string
Returns:
- boolean success if value set true, or follow by errmsg
- string fail error message
- asn1_string:equals (another)
-
compare two asn1_string, if equals return true
Parameters:
- another asn1_string to compre
Returns:
-
boolean
true if equals
Usage:
local obj = astr:dup() assert(obj==astr, "must equals")
- asn1_string:__eq (other)
-
compare two ASN1 string objects for equality
Parameters:
- other asn1_string ASN1 string object to compare with
Returns:
-
boolean
true if objects are equal
- asn1_string:__tostring ()
-
convert asn1_string to lua string
Returns:
-
string
result format match with type:data
- asn1_string:toprint ()
-
get data as printable encode string
Returns:
-
string
printable encoded string
- asn1_string:toutf8 ()
-
get data as utf8 encode string
Returns:
-
string
utf8 encoded string
- asn1_string:dup ()
-
duplicate a new asn1_string
Returns:
-
asn1_string
clone for self
- asn1_string:check ()
-
check if asn1_time object is valid
Returns:
-
boolean
true if asn1_time is valid, false otherwise
- asn1_string:adj (time[, offset_day=0[, offset_sec=0]])
-
adjust asn1_time by specified offset
Parameters:
- time number base time as Unix timestamp
- offset_day number offset in days (default 0)
- offset_sec number offset in seconds (default 0)
Returns:
-
asn1_time
adjusted asn1_time object (self)
- asn1_string:diff (to)
-
calculate difference between two asn1_time objects
Parameters:
- to asn1_time target time to compare with
Returns:
- number difference in days
- number difference in seconds
Or
-
nil
when comparison fails