2003-09-12 17:46:48 +04:00
|
|
|
/*
|
|
|
|
* $Id$
|
|
|
|
* 'OpenSSL for Ruby' team members
|
|
|
|
* Copyright (C) 2003
|
|
|
|
* All rights reserved.
|
|
|
|
*/
|
|
|
|
/*
|
|
|
|
* This program is licenced under the same licence as Ruby.
|
|
|
|
* (See the file 'LICENCE'.)
|
|
|
|
*/
|
|
|
|
#include "ossl.h"
|
|
|
|
|
|
|
|
#if defined(HAVE_SYS_TIME_H)
|
|
|
|
# include <sys/time.h>
|
|
|
|
#elif !defined(NT) && !defined(_WIN32)
|
|
|
|
struct timeval {
|
|
|
|
long tv_sec; /* seconds */
|
|
|
|
long tv_usec; /* and microseconds */
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
2010-12-11 20:45:42 +03:00
|
|
|
static VALUE join_der(VALUE enumerable);
|
2011-05-23 01:01:13 +04:00
|
|
|
static VALUE ossl_asn1_decode0(unsigned char **pp, long length, long *offset,
|
|
|
|
int depth, int yield, long *num_read);
|
|
|
|
static VALUE ossl_asn1_initialize(int argc, VALUE *argv, VALUE self);
|
|
|
|
static VALUE ossl_asn1eoc_initialize(VALUE self);
|
2010-12-11 20:45:42 +03:00
|
|
|
|
2003-09-12 17:46:48 +04:00
|
|
|
/*
|
|
|
|
* DATE conversion
|
|
|
|
*/
|
|
|
|
VALUE
|
|
|
|
asn1time_to_time(ASN1_TIME *time)
|
|
|
|
{
|
|
|
|
struct tm tm;
|
|
|
|
VALUE argv[6];
|
2010-04-22 12:04:13 +04:00
|
|
|
|
2003-12-04 03:21:31 +03:00
|
|
|
if (!time || !time->data) return Qnil;
|
2003-09-12 17:46:48 +04:00
|
|
|
memset(&tm, 0, sizeof(struct tm));
|
2010-04-22 12:21:01 +04:00
|
|
|
|
2003-09-12 17:46:48 +04:00
|
|
|
switch (time->type) {
|
|
|
|
case V_ASN1_UTCTIME:
|
2008-07-22 19:34:23 +04:00
|
|
|
if (sscanf((const char *)time->data, "%2d%2d%2d%2d%2d%2dZ", &tm.tm_year, &tm.tm_mon,
|
2003-09-12 17:46:48 +04:00
|
|
|
&tm.tm_mday, &tm.tm_hour, &tm.tm_min, &tm.tm_sec) != 6) {
|
|
|
|
ossl_raise(rb_eTypeError, "bad UTCTIME format");
|
2010-04-22 12:04:13 +04:00
|
|
|
}
|
2003-09-12 17:46:48 +04:00
|
|
|
if (tm.tm_year < 69) {
|
|
|
|
tm.tm_year += 2000;
|
|
|
|
} else {
|
|
|
|
tm.tm_year += 1900;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case V_ASN1_GENERALIZEDTIME:
|
2008-07-22 19:34:23 +04:00
|
|
|
if (sscanf((const char *)time->data, "%4d%2d%2d%2d%2d%2dZ", &tm.tm_year, &tm.tm_mon,
|
2003-09-12 17:46:48 +04:00
|
|
|
&tm.tm_mday, &tm.tm_hour, &tm.tm_min, &tm.tm_sec) != 6) {
|
|
|
|
ossl_raise(rb_eTypeError, "bad GENERALIZEDTIME format" );
|
2010-04-22 12:04:13 +04:00
|
|
|
}
|
2003-09-12 17:46:48 +04:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
rb_warning("unknown time format");
|
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
argv[0] = INT2NUM(tm.tm_year);
|
2005-06-19 20:29:17 +04:00
|
|
|
argv[1] = INT2NUM(tm.tm_mon);
|
2003-09-12 17:46:48 +04:00
|
|
|
argv[2] = INT2NUM(tm.tm_mday);
|
|
|
|
argv[3] = INT2NUM(tm.tm_hour);
|
|
|
|
argv[4] = INT2NUM(tm.tm_min);
|
|
|
|
argv[5] = INT2NUM(tm.tm_sec);
|
|
|
|
|
|
|
|
return rb_funcall2(rb_cTime, rb_intern("utc"), 6, argv);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This function is not exported in Ruby's *.h
|
|
|
|
*/
|
|
|
|
extern struct timeval rb_time_timeval(VALUE);
|
|
|
|
|
|
|
|
time_t
|
|
|
|
time_to_time_t(VALUE time)
|
|
|
|
{
|
2003-10-03 12:58:01 +04:00
|
|
|
return (time_t)NUM2LONG(rb_Integer(time));
|
2003-09-12 17:46:48 +04:00
|
|
|
}
|
|
|
|
|
2005-09-07 11:40:55 +04:00
|
|
|
/*
|
|
|
|
* STRING conversion
|
|
|
|
*/
|
|
|
|
VALUE
|
|
|
|
asn1str_to_str(ASN1_STRING *str)
|
|
|
|
{
|
2008-07-22 19:34:23 +04:00
|
|
|
return rb_str_new((const char *)str->data, str->length);
|
2005-09-07 11:40:55 +04:00
|
|
|
}
|
|
|
|
|
2003-09-12 17:46:48 +04:00
|
|
|
/*
|
|
|
|
* ASN1_INTEGER conversions
|
|
|
|
* TODO: Make a decision what's the right way to do this.
|
|
|
|
*/
|
|
|
|
#define DO_IT_VIA_RUBY 0
|
|
|
|
VALUE
|
|
|
|
asn1integer_to_num(ASN1_INTEGER *ai)
|
|
|
|
{
|
|
|
|
BIGNUM *bn;
|
|
|
|
#if DO_IT_VIA_RUBY
|
|
|
|
char *txt;
|
|
|
|
#endif
|
|
|
|
VALUE num;
|
|
|
|
|
|
|
|
if (!ai) {
|
|
|
|
ossl_raise(rb_eTypeError, "ASN1_INTEGER is NULL!");
|
|
|
|
}
|
|
|
|
if (!(bn = ASN1_INTEGER_to_BN(ai, NULL))) {
|
|
|
|
ossl_raise(eOSSLError, NULL);
|
|
|
|
}
|
|
|
|
#if DO_IT_VIA_RUBY
|
|
|
|
if (!(txt = BN_bn2dec(bn))) {
|
|
|
|
BN_free(bn);
|
|
|
|
ossl_raise(eOSSLError, NULL);
|
|
|
|
}
|
|
|
|
num = rb_cstr_to_inum(txt, 10, Qtrue);
|
|
|
|
OPENSSL_free(txt);
|
|
|
|
#else
|
|
|
|
num = ossl_bn_new(bn);
|
|
|
|
#endif
|
|
|
|
BN_free(bn);
|
|
|
|
|
|
|
|
return num;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if DO_IT_VIA_RUBY
|
2003-10-03 12:58:01 +04:00
|
|
|
ASN1_INTEGER *
|
|
|
|
num_to_asn1integer(VALUE obj, ASN1_INTEGER *ai)
|
2003-09-12 17:46:48 +04:00
|
|
|
{
|
|
|
|
BIGNUM *bn = NULL;
|
|
|
|
|
|
|
|
if (RTEST(rb_obj_is_kind_of(obj, cBN))) {
|
|
|
|
bn = GetBNPtr(obj);
|
|
|
|
} else {
|
|
|
|
obj = rb_String(obj);
|
|
|
|
if (!BN_dec2bn(&bn, StringValuePtr(obj))) {
|
|
|
|
ossl_raise(eOSSLError, NULL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!(ai = BN_to_ASN1_INTEGER(bn, ai))) {
|
|
|
|
BN_free(bn);
|
|
|
|
ossl_raise(eOSSLError, NULL);
|
|
|
|
}
|
|
|
|
BN_free(bn);
|
|
|
|
return ai;
|
|
|
|
}
|
|
|
|
#else
|
2003-10-03 12:58:01 +04:00
|
|
|
ASN1_INTEGER *
|
|
|
|
num_to_asn1integer(VALUE obj, ASN1_INTEGER *ai)
|
2003-09-12 17:46:48 +04:00
|
|
|
{
|
|
|
|
BIGNUM *bn = GetBNPtr(obj);
|
2010-04-22 12:04:13 +04:00
|
|
|
|
2003-09-12 17:46:48 +04:00
|
|
|
if (!(ai = BN_to_ASN1_INTEGER(bn, ai))) {
|
|
|
|
ossl_raise(eOSSLError, NULL);
|
|
|
|
}
|
|
|
|
return ai;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2003-10-03 12:58:01 +04:00
|
|
|
/********/
|
|
|
|
/*
|
|
|
|
* ASN1 module
|
|
|
|
*/
|
2011-05-22 18:27:02 +04:00
|
|
|
#define ossl_asn1_get_value(o) rb_attr_get((o),sivVALUE)
|
|
|
|
#define ossl_asn1_get_tag(o) rb_attr_get((o),sivTAG)
|
|
|
|
#define ossl_asn1_get_tagging(o) rb_attr_get((o),sivTAGGING)
|
|
|
|
#define ossl_asn1_get_tag_class(o) rb_attr_get((o),sivTAG_CLASS)
|
|
|
|
#define ossl_asn1_get_infinite_length(o) rb_attr_get((o),sivINFINITE_LENGTH)
|
|
|
|
|
|
|
|
#define ossl_asn1_set_value(o,v) rb_ivar_set((o),sivVALUE,(v))
|
|
|
|
#define ossl_asn1_set_tag(o,v) rb_ivar_set((o),sivTAG,(v))
|
|
|
|
#define ossl_asn1_set_tagging(o,v) rb_ivar_set((o),sivTAGGING,(v))
|
|
|
|
#define ossl_asn1_set_tag_class(o,v) rb_ivar_set((o),sivTAG_CLASS,(v))
|
|
|
|
#define ossl_asn1_set_infinite_length(o,v) rb_ivar_set((o),sivINFINITE_LENGTH,(v))
|
2003-10-03 12:58:01 +04:00
|
|
|
|
|
|
|
VALUE mASN1;
|
|
|
|
VALUE eASN1Error;
|
|
|
|
|
|
|
|
VALUE cASN1Data;
|
|
|
|
VALUE cASN1Primitive;
|
|
|
|
VALUE cASN1Constructive;
|
|
|
|
|
2010-12-11 20:45:42 +03:00
|
|
|
VALUE cASN1EndOfContent;
|
2003-10-03 12:58:01 +04:00
|
|
|
VALUE cASN1Boolean; /* BOOLEAN */
|
|
|
|
VALUE cASN1Integer, cASN1Enumerated; /* INTEGER */
|
|
|
|
VALUE cASN1BitString; /* BIT STRING */
|
|
|
|
VALUE cASN1OctetString, cASN1UTF8String; /* STRINGs */
|
|
|
|
VALUE cASN1NumericString, cASN1PrintableString;
|
|
|
|
VALUE cASN1T61String, cASN1VideotexString;
|
|
|
|
VALUE cASN1IA5String, cASN1GraphicString;
|
|
|
|
VALUE cASN1ISO64String, cASN1GeneralString;
|
|
|
|
VALUE cASN1UniversalString, cASN1BMPString;
|
|
|
|
VALUE cASN1Null; /* NULL */
|
|
|
|
VALUE cASN1ObjectId; /* OBJECT IDENTIFIER */
|
|
|
|
VALUE cASN1UTCTime, cASN1GeneralizedTime; /* TIME */
|
|
|
|
VALUE cASN1Sequence, cASN1Set; /* CONSTRUCTIVE */
|
|
|
|
|
|
|
|
static ID sIMPLICIT, sEXPLICIT;
|
|
|
|
static ID sUNIVERSAL, sAPPLICATION, sCONTEXT_SPECIFIC, sPRIVATE;
|
2011-05-22 18:27:02 +04:00
|
|
|
static ID sivVALUE, sivTAG, sivTAG_CLASS, sivTAGGING, sivINFINITE_LENGTH, sivUNUSED_BITS;
|
2003-10-03 12:58:01 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Ruby to ASN1 converters
|
|
|
|
*/
|
|
|
|
static ASN1_BOOLEAN
|
|
|
|
obj_to_asn1bool(VALUE obj)
|
|
|
|
{
|
2010-08-23 08:12:08 +04:00
|
|
|
#if OPENSSL_VERSION_NUMBER < 0x00907000L
|
2004-05-24 20:50:59 +04:00
|
|
|
return RTEST(obj) ? 0xff : 0x100;
|
2010-08-23 08:12:08 +04:00
|
|
|
#else
|
|
|
|
return RTEST(obj) ? 0xff : 0x0;
|
|
|
|
#endif
|
2003-10-03 12:58:01 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static ASN1_INTEGER*
|
|
|
|
obj_to_asn1int(VALUE obj)
|
|
|
|
{
|
|
|
|
return num_to_asn1integer(obj, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
static ASN1_BIT_STRING*
|
|
|
|
obj_to_asn1bstr(VALUE obj, long unused_bits)
|
|
|
|
{
|
|
|
|
ASN1_BIT_STRING *bstr;
|
|
|
|
|
|
|
|
if(unused_bits < 0) unused_bits = 0;
|
|
|
|
StringValue(obj);
|
|
|
|
if(!(bstr = ASN1_BIT_STRING_new()))
|
|
|
|
ossl_raise(eASN1Error, NULL);
|
2011-03-24 10:29:21 +03:00
|
|
|
ASN1_BIT_STRING_set(bstr, (unsigned char *)RSTRING_PTR(obj), RSTRING_LENINT(obj));
|
2003-10-03 12:58:01 +04:00
|
|
|
bstr->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT|0x07); /* clear */
|
|
|
|
bstr->flags |= ASN1_STRING_FLAG_BITS_LEFT|(unused_bits&0x07);
|
|
|
|
|
|
|
|
return bstr;
|
|
|
|
}
|
|
|
|
|
|
|
|
static ASN1_STRING*
|
|
|
|
obj_to_asn1str(VALUE obj)
|
|
|
|
{
|
|
|
|
ASN1_STRING *str;
|
|
|
|
|
|
|
|
StringValue(obj);
|
|
|
|
if(!(str = ASN1_STRING_new()))
|
|
|
|
ossl_raise(eASN1Error, NULL);
|
2011-03-24 10:29:21 +03:00
|
|
|
ASN1_STRING_set(str, RSTRING_PTR(obj), RSTRING_LENINT(obj));
|
2003-10-03 12:58:01 +04:00
|
|
|
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
|
|
|
static ASN1_NULL*
|
|
|
|
obj_to_asn1null(VALUE obj)
|
|
|
|
{
|
|
|
|
ASN1_NULL *null;
|
|
|
|
|
|
|
|
if(!NIL_P(obj))
|
|
|
|
ossl_raise(eASN1Error, "nil expected");
|
|
|
|
if(!(null = ASN1_NULL_new()))
|
|
|
|
ossl_raise(eASN1Error, NULL);
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
static ASN1_OBJECT*
|
|
|
|
obj_to_asn1obj(VALUE obj)
|
|
|
|
{
|
|
|
|
ASN1_OBJECT *a1obj;
|
|
|
|
|
|
|
|
StringValue(obj);
|
2006-08-31 14:30:33 +04:00
|
|
|
a1obj = OBJ_txt2obj(RSTRING_PTR(obj), 0);
|
|
|
|
if(!a1obj) a1obj = OBJ_txt2obj(RSTRING_PTR(obj), 1);
|
2003-10-03 12:58:01 +04:00
|
|
|
if(!a1obj) ossl_raise(eASN1Error, "invalid OBJECT ID");
|
|
|
|
|
|
|
|
return a1obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
static ASN1_UTCTIME*
|
|
|
|
obj_to_asn1utime(VALUE time)
|
|
|
|
{
|
|
|
|
time_t sec;
|
|
|
|
ASN1_UTCTIME *t;
|
|
|
|
|
|
|
|
sec = time_to_time_t(time);
|
|
|
|
if(!(t = ASN1_UTCTIME_set(NULL, sec)))
|
|
|
|
ossl_raise(eASN1Error, NULL);
|
|
|
|
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
|
|
|
|
static ASN1_GENERALIZEDTIME*
|
|
|
|
obj_to_asn1gtime(VALUE time)
|
|
|
|
{
|
|
|
|
time_t sec;
|
|
|
|
ASN1_GENERALIZEDTIME *t;
|
|
|
|
|
|
|
|
sec = time_to_time_t(time);
|
|
|
|
if(!(t =ASN1_GENERALIZEDTIME_set(NULL, sec)))
|
|
|
|
ossl_raise(eASN1Error, NULL);
|
|
|
|
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
|
|
|
|
static ASN1_STRING*
|
|
|
|
obj_to_asn1derstr(VALUE obj)
|
|
|
|
{
|
|
|
|
ASN1_STRING *a1str;
|
|
|
|
VALUE str;
|
|
|
|
|
|
|
|
str = ossl_to_der(obj);
|
|
|
|
if(!(a1str = ASN1_STRING_new()))
|
|
|
|
ossl_raise(eASN1Error, NULL);
|
2011-03-24 10:29:21 +03:00
|
|
|
ASN1_STRING_set(a1str, RSTRING_PTR(str), RSTRING_LENINT(str));
|
2003-10-03 12:58:01 +04:00
|
|
|
|
|
|
|
return a1str;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* DER to Ruby converters
|
|
|
|
*/
|
|
|
|
static VALUE
|
|
|
|
decode_bool(unsigned char* der, int length)
|
|
|
|
{
|
2009-03-13 04:42:21 +03:00
|
|
|
int val;
|
2008-07-22 19:34:23 +04:00
|
|
|
const unsigned char *p;
|
2003-10-03 12:58:01 +04:00
|
|
|
|
|
|
|
p = der;
|
2009-03-13 04:42:21 +03:00
|
|
|
if((val = d2i_ASN1_BOOLEAN(NULL, &p, length)) < 0)
|
2003-10-03 12:58:01 +04:00
|
|
|
ossl_raise(eASN1Error, NULL);
|
|
|
|
|
2009-03-13 04:42:21 +03:00
|
|
|
return val ? Qtrue : Qfalse;
|
2003-10-03 12:58:01 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
decode_int(unsigned char* der, int length)
|
|
|
|
{
|
|
|
|
ASN1_INTEGER *ai;
|
2008-07-22 19:34:23 +04:00
|
|
|
const unsigned char *p;
|
2010-04-22 12:04:13 +04:00
|
|
|
VALUE ret;
|
2003-10-03 12:58:01 +04:00
|
|
|
int status = 0;
|
|
|
|
|
|
|
|
p = der;
|
|
|
|
if(!(ai = d2i_ASN1_INTEGER(NULL, &p, length)))
|
|
|
|
ossl_raise(eASN1Error, NULL);
|
|
|
|
ret = rb_protect((VALUE(*)_((VALUE)))asn1integer_to_num,
|
|
|
|
(VALUE)ai, &status);
|
|
|
|
ASN1_INTEGER_free(ai);
|
|
|
|
if(status) rb_jump_tag(status);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
decode_bstr(unsigned char* der, int length, long *unused_bits)
|
|
|
|
{
|
|
|
|
ASN1_BIT_STRING *bstr;
|
2008-07-22 19:34:23 +04:00
|
|
|
const unsigned char *p;
|
2003-10-03 12:58:01 +04:00
|
|
|
long len;
|
|
|
|
VALUE ret;
|
|
|
|
|
|
|
|
p = der;
|
|
|
|
if(!(bstr = d2i_ASN1_BIT_STRING(NULL, &p, length)))
|
|
|
|
ossl_raise(eASN1Error, NULL);
|
|
|
|
len = bstr->length;
|
|
|
|
*unused_bits = 0;
|
|
|
|
if(bstr->flags & ASN1_STRING_FLAG_BITS_LEFT)
|
|
|
|
*unused_bits = bstr->flags & 0x07;
|
2008-07-22 19:34:23 +04:00
|
|
|
ret = rb_str_new((const char *)bstr->data, len);
|
2003-10-03 12:58:01 +04:00
|
|
|
ASN1_BIT_STRING_free(bstr);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
decode_enum(unsigned char* der, int length)
|
|
|
|
{
|
|
|
|
ASN1_ENUMERATED *ai;
|
2008-07-22 19:34:23 +04:00
|
|
|
const unsigned char *p;
|
2010-04-22 12:04:13 +04:00
|
|
|
VALUE ret;
|
2003-10-03 12:58:01 +04:00
|
|
|
int status = 0;
|
|
|
|
|
|
|
|
p = der;
|
|
|
|
if(!(ai = d2i_ASN1_ENUMERATED(NULL, &p, length)))
|
|
|
|
ossl_raise(eASN1Error, NULL);
|
|
|
|
ret = rb_protect((VALUE(*)_((VALUE)))asn1integer_to_num,
|
|
|
|
(VALUE)ai, &status);
|
|
|
|
ASN1_ENUMERATED_free(ai);
|
|
|
|
if(status) rb_jump_tag(status);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
decode_null(unsigned char* der, int length)
|
|
|
|
{
|
|
|
|
ASN1_NULL *null;
|
2008-07-22 19:34:23 +04:00
|
|
|
const unsigned char *p;
|
2003-10-03 12:58:01 +04:00
|
|
|
|
|
|
|
p = der;
|
|
|
|
if(!(null = d2i_ASN1_NULL(NULL, &p, length)))
|
|
|
|
ossl_raise(eASN1Error, NULL);
|
|
|
|
ASN1_NULL_free(null);
|
|
|
|
|
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
decode_obj(unsigned char* der, int length)
|
|
|
|
{
|
|
|
|
ASN1_OBJECT *obj;
|
2008-07-22 19:34:23 +04:00
|
|
|
const unsigned char *p;
|
2003-10-03 12:58:01 +04:00
|
|
|
VALUE ret;
|
|
|
|
int nid;
|
|
|
|
BIO *bio;
|
|
|
|
|
|
|
|
p = der;
|
|
|
|
if(!(obj = d2i_ASN1_OBJECT(NULL, &p, length)))
|
|
|
|
ossl_raise(eASN1Error, NULL);
|
|
|
|
if((nid = OBJ_obj2nid(obj)) != NID_undef){
|
|
|
|
ASN1_OBJECT_free(obj);
|
|
|
|
ret = rb_str_new2(OBJ_nid2sn(nid));
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
if(!(bio = BIO_new(BIO_s_mem()))){
|
|
|
|
ASN1_OBJECT_free(obj);
|
|
|
|
ossl_raise(eASN1Error, NULL);
|
|
|
|
}
|
|
|
|
i2a_ASN1_OBJECT(bio, obj);
|
|
|
|
ASN1_OBJECT_free(obj);
|
|
|
|
ret = ossl_membio2str(bio);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
decode_time(unsigned char* der, int length)
|
|
|
|
{
|
|
|
|
ASN1_TIME *time;
|
2008-07-22 19:34:23 +04:00
|
|
|
const unsigned char *p;
|
2003-10-03 12:58:01 +04:00
|
|
|
VALUE ret;
|
|
|
|
int status = 0;
|
|
|
|
|
|
|
|
p = der;
|
|
|
|
if(!(time = d2i_ASN1_TIME(NULL, &p, length)))
|
|
|
|
ossl_raise(eASN1Error, NULL);
|
|
|
|
ret = rb_protect((VALUE(*)_((VALUE)))asn1time_to_time,
|
|
|
|
(VALUE)time, &status);
|
|
|
|
ASN1_TIME_free(time);
|
|
|
|
if(status) rb_jump_tag(status);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2011-05-23 01:01:13 +04:00
|
|
|
static VALUE
|
|
|
|
decode_eoc(unsigned char *der, int length)
|
|
|
|
{
|
|
|
|
VALUE ret;
|
|
|
|
if (length != 2 || !(der[0] == 0x00 && der[1] == 0x00))
|
|
|
|
ossl_raise(eASN1Error, NULL);
|
|
|
|
|
|
|
|
return rb_str_new("", 0);
|
|
|
|
}
|
|
|
|
|
2003-10-03 12:58:01 +04:00
|
|
|
/********/
|
|
|
|
|
|
|
|
typedef struct {
|
* sprintf.c (rb_str_format): allow %c to print one character
string (e.g. ?x).
* lib/tempfile.rb (Tempfile::make_tmpname): put dot between
basename and pid. [ruby-talk:196272]
* parse.y (do_block): remove -> style block.
* parse.y (parser_yylex): remove tLAMBDA_ARG.
* eval.c (rb_call0): binding for the return event hook should have
consistent scope. [ruby-core:07928]
* eval.c (proc_invoke): return behavior should depend whether it
is surrounded by a lambda or a mere block.
* eval.c (formal_assign): handles post splat arguments.
* eval.c (rb_call0): ditto.
* st.c (strhash): use FNV-1a hash.
* parse.y (parser_yylex): removed experimental ';;' terminator.
* eval.c (rb_node_arity): should be aware of post splat arguments.
* eval.c (rb_proc_arity): ditto.
* parse.y (f_args): syntax rule enhanced to support arguments
after the splat.
* parse.y (block_param): ditto for block parameters.
* parse.y (f_post_arg): mandatory formal arguments after the splat
argument.
* parse.y (new_args_gen): generate nodes for mandatory formal
arguments after the splat argument.
* eval.c (rb_eval): dispatch mandatory formal arguments after the
splat argument.
* parse.y (args): allow more than one splat in the argument list.
* parse.y (method_call): allow aref [] to accept all kind of
method argument, including assocs, splat, and block argument.
* eval.c (SETUP_ARGS0): prepare block argument as well.
* lib/mathn.rb (Integer): remove Integer#gcd2. [ruby-core:07931]
* eval.c (error_line): print receivers true/false/nil specially.
* eval.c (rb_proc_yield): handles parameters in yield semantics.
* eval.c (nil_yield): gives LocalJumpError to denote no block
error.
* io.c (rb_io_getc): now takes one-character string.
* string.c (rb_str_hash): use FNV-1a hash from Fowler/Noll/Vo
hashing algorithm.
* string.c (rb_str_aref): str[0] now returns 1 character string,
instead of a fixnum. [Ruby2]
* parse.y (parser_yylex): ?c now returns 1 character string,
instead of a fixnum. [Ruby2]
* string.c (rb_str_aset): no longer support fixnum insertion.
* eval.c (umethod_bind): should not update original class.
[ruby-dev:28636]
* eval.c (ev_const_get): should support constant access from
within instance_eval(). [ruby-dev:28327]
* time.c (time_timeval): should round for usec floating
number. [ruby-core:07896]
* time.c (time_add): ditto.
* dir.c (sys_warning): should not call a vararg function
rb_sys_warning() indirectly. [ruby-core:07886]
* numeric.c (flo_divmod): the first element of Float#divmod should
be an integer. [ruby-dev:28589]
* test/ruby/test_float.rb: add tests for divmod, div, modulo and remainder.
* re.c (rb_reg_initialize): should not allow modifying literal
regexps. frozen check moved from rb_reg_initialize_m as well.
* re.c (rb_reg_initialize): should not modify untainted objects in
safe levels higher than 3.
* re.c (rb_memcmp): type change from char* to const void*.
* dir.c (dir_close): should not close untainted dir stream.
* dir.c (GetDIR): add tainted/frozen check for each dir operation.
* lib/rdoc/parsers/parse_rb.rb (RDoc::RubyParser::parse_symbol_arg):
typo fixed. a patch from Florian Gross <florg at florg.net>.
* eval.c (EXEC_EVENT_HOOK): trace_func may remove itself from
event_hooks. no guarantee for arbitrary hook deletion.
[ruby-dev:28632]
* util.c (ruby_strtod): differ addition to minimize error.
[ruby-dev:28619]
* util.c (ruby_strtod): should not raise ERANGE when the input
string does not have any digits. [ruby-dev:28629]
* eval.c (proc_invoke): should restore old ruby_frame->block.
thanks to ts <decoux at moulon.inra.fr>. [ruby-core:07833]
also fix [ruby-dev:28614] as well.
* signal.c (trap): sig should be less then NSIG. Coverity found
this bug. a patch from Kevin Tew <tewk at tewk.com>.
[ruby-core:07823]
* math.c (math_log2): add new method inspired by
[ruby-talk:191237].
* math.c (math_log): add optional base argument to Math::log().
[ruby-talk:191308]
* ext/syck/emitter.c (syck_scan_scalar): avoid accessing
uninitialized array element. a patch from Pat Eyler
<rubypate at gmail.com>. [ruby-core:07809]
* array.c (rb_ary_fill): initialize local variables first. a
patch from Pat Eyler <rubypate at gmail.com>. [ruby-core:07810]
* ext/syck/yaml2byte.c (syck_yaml2byte_handler): need to free
type_tag. a patch from Pat Eyler <rubypate at gmail.com>.
[ruby-core:07808]
* ext/socket/socket.c (make_hostent_internal): accept ai_family
check from Sam Roberts <sroberts at uniserve.com>.
[ruby-core:07691]
* util.c (ruby_strtod): should not cut off 18 digits for no
reason. [ruby-core:07796]
* array.c (rb_ary_fill): internalize local variable "beg" to
pacify Coverity. [ruby-core:07770]
* pack.c (pack_unpack): now supports CRLF newlines. a patch from
<tommy at tmtm.org>. [ruby-dev:28601]
* applied code clean-up patch from Stefan Huehner
<stefan at huehner.org>. [ruby-core:07764]
* lib/jcode.rb (String::tr_s): should have translated non
squeezing character sequence (i.e. a character) as well. thanks
to Hiroshi Ichikawa <gimite at gimite.ddo.jp> [ruby-list:42090]
* ext/socket/socket.c: document update patch from Sam Roberts
<sroberts at uniserve.com>. [ruby-core:07701]
* lib/mathn.rb (Integer): need not to remove gcd2. a patch from
NARUSE, Yui <naruse at airemix.com>. [ruby-dev:28570]
* parse.y (arg): too much NEW_LIST()
* eval.c (SETUP_ARGS0): remove unnecessary access to nd_alen.
* eval.c (rb_eval): use ARGSCAT for NODE_OP_ASGN1.
[ruby-dev:28585]
* parse.y (arg): use NODE_ARGSCAT for placeholder.
* lib/getoptlong.rb (GetoptLong::get): RDoc update patch from
mathew <meta at pobox.com>. [ruby-core:07738]
* variable.c (rb_const_set): raise error when no target klass is
supplied. [ruby-dev:28582]
* prec.c (prec_prec_f): documentation patch from
<gerardo.santana at gmail.com>. [ruby-core:07689]
* bignum.c (rb_big_pow): second operand may be too big even if
it's a Fixnum. [ruby-talk:187984]
* README.EXT: update symbol description. [ruby-talk:188104]
* COPYING: explicitly note GPLv2. [ruby-talk:187922]
* parse.y: remove some obsolete syntax rules (unparenthesized
method calls in argument list).
* eval.c (rb_call0): insecure calling should be checked for non
NODE_SCOPE method invocations too.
* eval.c (rb_alias): should preserve the current safe level as
well as method definition.
* process.c (rb_f_sleep): remove RDoc description about SIGALRM
which is not valid on the current implementation. [ruby-dev:28464]
Thu Mar 23 21:40:47 2006 K.Kosako <sndgk393 AT ybb.ne.jp>
* eval.c (method_missing): should support argument splat in
super. a bug in combination of super, splat and
method_missing. [ruby-talk:185438]
* configure.in: Solaris SunPro compiler -rapth patch from
<kuwa at labs.fujitsu.com>. [ruby-dev:28443]
* configure.in: remove enable_rpath=no for Solaris.
[ruby-dev:28440]
* ext/win32ole/win32ole.c (ole_val2olevariantdata): change behavior
of converting OLE Variant object with VT_ARRAY|VT_UI1 and Ruby
String object.
* ruby.1: a clarification patch from David Lutterkort
<dlutter at redhat.com>. [ruby-core:7508]
* lib/rdoc/ri/ri_paths.rb (RI::Paths): adding paths from rubygems
directories. a patch from Eric Hodel <drbrain at segment7.net>.
[ruby-core:07423]
* eval.c (rb_clear_cache_by_class): clearing wrong cache.
* ext/extmk.rb: use :remove_destination to install extension libraries
to avoid SEGV. [ruby-dev:28417]
* eval.c (rb_thread_fd_writable): should not re-schedule output
from KILLED thread (must be error printing).
* array.c (rb_ary_flatten_bang): allow specifying recursion
level. [ruby-talk:182170]
* array.c (rb_ary_flatten): ditto.
* gc.c (add_heap): a heap_slots may overflow. a patch from Stefan
Weil <weil at mail.berlios.de>.
* eval.c (rb_call): use separate cache for fcall/vcall
invocation.
* eval.c (rb_eval): NODE_FCALL, NODE_VCALL can call local
functions.
* eval.c (rb_mod_local): a new method to specify newly added
visibility "local".
* eval.c (search_method): search for local methods which are
visible only from the current class.
* class.c (rb_class_local_methods): a method to list local methods.
* object.c (Init_Object): add BasicObject class as a top level
BlankSlate class.
* ruby.h (SYM2ID): should not cast to signed long.
[ruby-core:07414]
* class.c (rb_include_module): allow module duplication.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@10235 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2006-06-10 01:20:17 +04:00
|
|
|
const char *name;
|
2003-10-03 12:58:01 +04:00
|
|
|
VALUE *klass;
|
|
|
|
} ossl_asn1_info_t;
|
|
|
|
|
|
|
|
static ossl_asn1_info_t ossl_asn1_info[] = {
|
2010-12-11 20:45:42 +03:00
|
|
|
{ "EOC", &cASN1EndOfContent, }, /* 0 */
|
2003-10-03 12:58:01 +04:00
|
|
|
{ "BOOLEAN", &cASN1Boolean, }, /* 1 */
|
|
|
|
{ "INTEGER", &cASN1Integer, }, /* 2 */
|
|
|
|
{ "BIT_STRING", &cASN1BitString, }, /* 3 */
|
|
|
|
{ "OCTET_STRING", &cASN1OctetString, }, /* 4 */
|
|
|
|
{ "NULL", &cASN1Null, }, /* 5 */
|
|
|
|
{ "OBJECT", &cASN1ObjectId, }, /* 6 */
|
|
|
|
{ "OBJECT_DESCRIPTOR", NULL, }, /* 7 */
|
|
|
|
{ "EXTERNAL", NULL, }, /* 8 */
|
|
|
|
{ "REAL", NULL, }, /* 9 */
|
|
|
|
{ "ENUMERATED", &cASN1Enumerated, }, /* 10 */
|
|
|
|
{ "EMBEDDED_PDV", NULL, }, /* 11 */
|
|
|
|
{ "UTF8STRING", &cASN1UTF8String, }, /* 12 */
|
|
|
|
{ "RELATIVE_OID", NULL, }, /* 13 */
|
|
|
|
{ "[UNIVERSAL 14]", NULL, }, /* 14 */
|
|
|
|
{ "[UNIVERSAL 15]", NULL, }, /* 15 */
|
|
|
|
{ "SEQUENCE", &cASN1Sequence, }, /* 16 */
|
|
|
|
{ "SET", &cASN1Set, }, /* 17 */
|
|
|
|
{ "NUMERICSTRING", &cASN1NumericString, }, /* 18 */
|
|
|
|
{ "PRINTABLESTRING", &cASN1PrintableString, }, /* 19 */
|
|
|
|
{ "T61STRING", &cASN1T61String, }, /* 20 */
|
|
|
|
{ "VIDEOTEXSTRING", &cASN1VideotexString, }, /* 21 */
|
|
|
|
{ "IA5STRING", &cASN1IA5String, }, /* 22 */
|
|
|
|
{ "UTCTIME", &cASN1UTCTime, }, /* 23 */
|
|
|
|
{ "GENERALIZEDTIME", &cASN1GeneralizedTime, }, /* 24 */
|
|
|
|
{ "GRAPHICSTRING", &cASN1GraphicString, }, /* 25 */
|
|
|
|
{ "ISO64STRING", &cASN1ISO64String, }, /* 26 */
|
|
|
|
{ "GENERALSTRING", &cASN1GeneralString, }, /* 27 */
|
|
|
|
{ "UNIVERSALSTRING", &cASN1UniversalString, }, /* 28 */
|
|
|
|
{ "CHARACTER_STRING", NULL, }, /* 29 */
|
|
|
|
{ "BMPSTRING", &cASN1BMPString, }, /* 30 */
|
|
|
|
};
|
|
|
|
|
|
|
|
int ossl_asn1_info_size = (sizeof(ossl_asn1_info)/sizeof(ossl_asn1_info[0]));
|
|
|
|
|
2011-05-22 04:01:06 +04:00
|
|
|
static VALUE class_tag_map;
|
|
|
|
|
2003-10-03 12:58:01 +04:00
|
|
|
static int ossl_asn1_default_tag(VALUE obj);
|
|
|
|
|
|
|
|
ASN1_TYPE*
|
|
|
|
ossl_asn1_get_asn1type(VALUE obj)
|
|
|
|
{
|
|
|
|
ASN1_TYPE *ret;
|
|
|
|
VALUE value, rflag;
|
|
|
|
void *ptr;
|
|
|
|
void (*free_func)();
|
2011-03-24 10:29:21 +03:00
|
|
|
int tag, flag;
|
2003-10-03 12:58:01 +04:00
|
|
|
|
|
|
|
tag = ossl_asn1_default_tag(obj);
|
|
|
|
value = ossl_asn1_get_value(obj);
|
|
|
|
switch(tag){
|
|
|
|
case V_ASN1_BOOLEAN:
|
2008-07-31 05:51:44 +04:00
|
|
|
ptr = (void*)(VALUE)obj_to_asn1bool(value);
|
2003-10-03 12:58:01 +04:00
|
|
|
free_func = NULL;
|
|
|
|
break;
|
|
|
|
case V_ASN1_INTEGER: /* FALLTHROUGH */
|
|
|
|
case V_ASN1_ENUMERATED:
|
|
|
|
ptr = obj_to_asn1int(value);
|
|
|
|
free_func = ASN1_INTEGER_free;
|
|
|
|
break;
|
|
|
|
case V_ASN1_BIT_STRING:
|
2011-05-22 18:27:02 +04:00
|
|
|
rflag = rb_attr_get(obj, sivUNUSED_BITS);
|
2003-10-03 12:58:01 +04:00
|
|
|
flag = NIL_P(rflag) ? -1 : NUM2INT(rflag);
|
|
|
|
ptr = obj_to_asn1bstr(value, flag);
|
|
|
|
free_func = ASN1_BIT_STRING_free;
|
|
|
|
break;
|
|
|
|
case V_ASN1_NULL:
|
|
|
|
ptr = obj_to_asn1null(value);
|
|
|
|
free_func = ASN1_NULL_free;
|
|
|
|
break;
|
|
|
|
case V_ASN1_OCTET_STRING: /* FALLTHROUGH */
|
|
|
|
case V_ASN1_UTF8STRING: /* FALLTHROUGH */
|
|
|
|
case V_ASN1_NUMERICSTRING: /* FALLTHROUGH */
|
|
|
|
case V_ASN1_PRINTABLESTRING: /* FALLTHROUGH */
|
|
|
|
case V_ASN1_T61STRING: /* FALLTHROUGH */
|
|
|
|
case V_ASN1_VIDEOTEXSTRING: /* FALLTHROUGH */
|
|
|
|
case V_ASN1_IA5STRING: /* FALLTHROUGH */
|
|
|
|
case V_ASN1_GRAPHICSTRING: /* FALLTHROUGH */
|
|
|
|
case V_ASN1_ISO64STRING: /* FALLTHROUGH */
|
|
|
|
case V_ASN1_GENERALSTRING: /* FALLTHROUGH */
|
|
|
|
case V_ASN1_UNIVERSALSTRING: /* FALLTHROUGH */
|
2010-04-22 12:04:13 +04:00
|
|
|
case V_ASN1_BMPSTRING:
|
2003-10-03 12:58:01 +04:00
|
|
|
ptr = obj_to_asn1str(value);
|
|
|
|
free_func = ASN1_STRING_free;
|
|
|
|
break;
|
|
|
|
case V_ASN1_OBJECT:
|
|
|
|
ptr = obj_to_asn1obj(value);
|
2003-10-06 20:55:00 +04:00
|
|
|
free_func = ASN1_OBJECT_free;
|
2003-10-03 12:58:01 +04:00
|
|
|
break;
|
|
|
|
case V_ASN1_UTCTIME:
|
|
|
|
ptr = obj_to_asn1utime(value);
|
|
|
|
free_func = ASN1_TIME_free;
|
|
|
|
break;
|
|
|
|
case V_ASN1_GENERALIZEDTIME:
|
|
|
|
ptr = obj_to_asn1gtime(value);
|
|
|
|
free_func = ASN1_TIME_free;
|
|
|
|
break;
|
|
|
|
case V_ASN1_SET: /* FALLTHROUGH */
|
|
|
|
case V_ASN1_SEQUENCE:
|
|
|
|
ptr = obj_to_asn1derstr(obj);
|
|
|
|
free_func = ASN1_STRING_free;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
ossl_raise(eASN1Error, "unsupported ASN.1 type");
|
|
|
|
}
|
|
|
|
if(!(ret = OPENSSL_malloc(sizeof(ASN1_TYPE)))){
|
|
|
|
if(free_func) free_func(ptr);
|
|
|
|
ossl_raise(eASN1Error, "ASN1_TYPE alloc failure");
|
|
|
|
}
|
|
|
|
memset(ret, 0, sizeof(ASN1_TYPE));
|
|
|
|
ASN1_TYPE_set(ret, tag, ptr);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
ossl_asn1_default_tag(VALUE obj)
|
|
|
|
{
|
2011-05-22 14:17:46 +04:00
|
|
|
VALUE tmp_class, tag;
|
|
|
|
|
|
|
|
tmp_class = CLASS_OF(obj);
|
2011-05-22 04:01:06 +04:00
|
|
|
while (tmp_class) {
|
2011-05-22 14:17:46 +04:00
|
|
|
tag = rb_hash_lookup(class_tag_map, tmp_class);
|
|
|
|
if (tag != Qnil) {
|
|
|
|
return NUM2INT(tag);
|
|
|
|
}
|
|
|
|
tmp_class = rb_class_superclass(tmp_class);
|
2003-10-03 12:58:01 +04:00
|
|
|
}
|
2004-03-29 11:54:38 +04:00
|
|
|
ossl_raise(eASN1Error, "universal tag for %s not found",
|
2003-10-03 12:58:01 +04:00
|
|
|
rb_class2name(CLASS_OF(obj)));
|
|
|
|
|
|
|
|
return -1; /* dummy */
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
ossl_asn1_tag(VALUE obj)
|
|
|
|
{
|
|
|
|
VALUE tag;
|
|
|
|
|
|
|
|
tag = ossl_asn1_get_tag(obj);
|
|
|
|
if(NIL_P(tag))
|
|
|
|
ossl_raise(eASN1Error, "tag number not specified");
|
|
|
|
|
|
|
|
return NUM2INT(tag);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
ossl_asn1_is_explicit(VALUE obj)
|
|
|
|
{
|
|
|
|
VALUE s;
|
|
|
|
int ret = -1;
|
|
|
|
|
|
|
|
s = ossl_asn1_get_tagging(obj);
|
|
|
|
if(NIL_P(s)) return 0;
|
|
|
|
else if(SYMBOL_P(s)){
|
|
|
|
if (SYM2ID(s) == sIMPLICIT)
|
|
|
|
ret = 0;
|
|
|
|
else if (SYM2ID(s) == sEXPLICIT)
|
|
|
|
ret = 1;
|
|
|
|
}
|
|
|
|
if(ret < 0){
|
|
|
|
ossl_raise(eASN1Error, "invalid tag default");
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
ossl_asn1_tag_class(VALUE obj)
|
|
|
|
{
|
|
|
|
VALUE s;
|
|
|
|
int ret = -1;
|
|
|
|
|
|
|
|
s = ossl_asn1_get_tag_class(obj);
|
|
|
|
if(NIL_P(s)) ret = V_ASN1_UNIVERSAL;
|
|
|
|
else if(SYMBOL_P(s)){
|
|
|
|
if (SYM2ID(s) == sUNIVERSAL)
|
|
|
|
ret = V_ASN1_UNIVERSAL;
|
|
|
|
else if (SYM2ID(s) == sAPPLICATION)
|
|
|
|
ret = V_ASN1_APPLICATION;
|
|
|
|
else if (SYM2ID(s) == sCONTEXT_SPECIFIC)
|
|
|
|
ret = V_ASN1_CONTEXT_SPECIFIC;
|
|
|
|
else if (SYM2ID(s) == sPRIVATE)
|
|
|
|
ret = V_ASN1_PRIVATE;
|
|
|
|
}
|
|
|
|
if(ret < 0){
|
|
|
|
ossl_raise(eASN1Error, "invalid tag class");
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
ossl_asn1_class2sym(int tc)
|
|
|
|
{
|
|
|
|
if((tc & V_ASN1_PRIVATE) == V_ASN1_PRIVATE)
|
|
|
|
return ID2SYM(sPRIVATE);
|
|
|
|
else if((tc & V_ASN1_CONTEXT_SPECIFIC) == V_ASN1_CONTEXT_SPECIFIC)
|
|
|
|
return ID2SYM(sCONTEXT_SPECIFIC);
|
|
|
|
else if((tc & V_ASN1_APPLICATION) == V_ASN1_APPLICATION)
|
|
|
|
return ID2SYM(sAPPLICATION);
|
|
|
|
else
|
|
|
|
return ID2SYM(sUNIVERSAL);
|
|
|
|
}
|
|
|
|
|
2011-05-16 00:15:38 +04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* OpenSSL::ASN1::ASN1Data.new(value, tag, tag_class) => ASN1Data
|
|
|
|
*
|
|
|
|
* +value+: Please have a look at Constructive and Primitive to see how Ruby
|
|
|
|
* types are mapped to ASN.1 types and vice versa.
|
2011-05-17 01:51:12 +04:00
|
|
|
*
|
2011-05-16 00:15:38 +04:00
|
|
|
* +tag+: A +Number+ indicating the tag number.
|
2011-05-17 01:51:12 +04:00
|
|
|
*
|
|
|
|
* +tag_class+: A +Symbol+ indicating the tag class. Please cf. ASN1 for
|
2011-05-16 00:15:38 +04:00
|
|
|
* possible values.
|
|
|
|
*
|
|
|
|
* == Example
|
2011-05-17 01:51:12 +04:00
|
|
|
* asn1_int = OpenSSL::ASN1Data.new(42, 2, :UNIVERSAL) # => Same as OpenSSL::ASN1::Integer.new(42)
|
|
|
|
* tagged_int = OpenSSL::ASN1Data.new(42, 0, :CONTEXT_SPECIFIC) # implicitly 0-tagged INTEGER
|
2011-05-16 16:20:53 +04:00
|
|
|
*/
|
2003-10-03 12:58:01 +04:00
|
|
|
static VALUE
|
|
|
|
ossl_asn1data_initialize(VALUE self, VALUE value, VALUE tag, VALUE tag_class)
|
|
|
|
{
|
|
|
|
if(!SYMBOL_P(tag_class))
|
|
|
|
ossl_raise(eASN1Error, "invalid tag class");
|
|
|
|
if((SYM2ID(tag_class) == sUNIVERSAL) && NUM2INT(tag) > 31)
|
2004-03-29 11:54:38 +04:00
|
|
|
ossl_raise(eASN1Error, "tag number for Universal too large");
|
2003-10-03 12:58:01 +04:00
|
|
|
ossl_asn1_set_tag(self, tag);
|
|
|
|
ossl_asn1_set_value(self, value);
|
|
|
|
ossl_asn1_set_tag_class(self, tag_class);
|
2010-12-11 20:45:42 +03:00
|
|
|
ossl_asn1_set_infinite_length(self, Qfalse);
|
2003-10-03 12:58:01 +04:00
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2010-04-22 12:04:13 +04:00
|
|
|
static VALUE
|
|
|
|
join_der_i(VALUE i, VALUE str)
|
2003-10-03 12:58:01 +04:00
|
|
|
{
|
|
|
|
i = ossl_to_der_if_possible(i);
|
|
|
|
StringValue(i);
|
|
|
|
rb_str_append(str, i);
|
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
join_der(VALUE enumerable)
|
|
|
|
{
|
|
|
|
VALUE str = rb_str_new(0, 0);
|
2006-02-03 12:15:42 +03:00
|
|
|
rb_block_call(enumerable, rb_intern("each"), 0, 0, join_der_i, str);
|
2003-10-03 12:58:01 +04:00
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
2011-05-16 00:15:38 +04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* asn1.to_der => DER-encoded String
|
|
|
|
*
|
|
|
|
* Encodes this ASN1Data into a DER-encoded String value. The result is
|
|
|
|
* DER-encoded except for the possibility of infinite length encodings.
|
2011-05-16 16:20:53 +04:00
|
|
|
* Infinite length encodings are not allowed in strict DER, so strictly
|
2011-05-16 00:15:38 +04:00
|
|
|
* speaking the result of such an encoding would be a BER-encoding.
|
|
|
|
*/
|
2003-10-03 12:58:01 +04:00
|
|
|
static VALUE
|
|
|
|
ossl_asn1data_to_der(VALUE self)
|
|
|
|
{
|
2010-12-11 20:45:42 +03:00
|
|
|
VALUE value, der, inf_length;
|
2011-05-22 22:48:31 +04:00
|
|
|
int tag, tag_class, is_cons = 0;
|
2003-10-03 12:58:01 +04:00
|
|
|
long length;
|
|
|
|
unsigned char *p;
|
|
|
|
|
|
|
|
value = ossl_asn1_get_value(self);
|
|
|
|
if(rb_obj_is_kind_of(value, rb_cArray)){
|
|
|
|
is_cons = 1;
|
|
|
|
value = join_der(value);
|
|
|
|
}
|
|
|
|
StringValue(value);
|
|
|
|
|
|
|
|
tag = ossl_asn1_tag(self);
|
|
|
|
tag_class = ossl_asn1_tag_class(self);
|
2010-12-11 20:45:42 +03:00
|
|
|
inf_length = ossl_asn1_get_infinite_length(self);
|
|
|
|
if (inf_length == Qtrue) {
|
2011-05-22 22:48:31 +04:00
|
|
|
is_cons = 2;
|
2010-12-11 20:45:42 +03:00
|
|
|
}
|
2011-05-22 22:48:31 +04:00
|
|
|
if((length = ASN1_object_size(is_cons, RSTRING_LENINT(value), tag)) <= 0)
|
2003-10-03 12:58:01 +04:00
|
|
|
ossl_raise(eASN1Error, NULL);
|
|
|
|
der = rb_str_new(0, length);
|
2008-07-22 19:34:23 +04:00
|
|
|
p = (unsigned char *)RSTRING_PTR(der);
|
2011-03-24 10:29:21 +03:00
|
|
|
ASN1_put_object(&p, is_cons, RSTRING_LENINT(value), tag, tag_class);
|
2006-08-31 14:30:33 +04:00
|
|
|
memcpy(p, RSTRING_PTR(value), RSTRING_LEN(value));
|
|
|
|
p += RSTRING_LEN(value);
|
2003-10-03 12:58:01 +04:00
|
|
|
ossl_str_adjust(der, p);
|
|
|
|
|
|
|
|
return der;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
2011-05-23 01:01:13 +04:00
|
|
|
int_ossl_asn1_decode0_prim(unsigned char **pp, long length, int hlen, int tag,
|
|
|
|
VALUE tc, long *num_read)
|
2003-10-03 12:58:01 +04:00
|
|
|
{
|
2011-05-23 01:01:13 +04:00
|
|
|
VALUE value, asn1data;
|
|
|
|
unsigned char *p;
|
|
|
|
long flag = 0;
|
2003-10-03 12:58:01 +04:00
|
|
|
|
|
|
|
p = *pp;
|
2011-05-23 01:01:13 +04:00
|
|
|
|
|
|
|
if(tc == sUNIVERSAL && tag < ossl_asn1_info_size) {
|
|
|
|
switch(tag){
|
|
|
|
case V_ASN1_EOC:
|
|
|
|
value = decode_eoc(p, hlen+length);
|
|
|
|
break;
|
|
|
|
case V_ASN1_BOOLEAN:
|
|
|
|
value = decode_bool(p, hlen+length);
|
|
|
|
break;
|
|
|
|
case V_ASN1_INTEGER:
|
|
|
|
value = decode_int(p, hlen+length);
|
|
|
|
break;
|
|
|
|
case V_ASN1_BIT_STRING:
|
|
|
|
value = decode_bstr(p, hlen+length, &flag);
|
|
|
|
break;
|
|
|
|
case V_ASN1_NULL:
|
|
|
|
value = decode_null(p, hlen+length);
|
|
|
|
break;
|
|
|
|
case V_ASN1_ENUMERATED:
|
|
|
|
value = decode_enum(p, hlen+length);
|
|
|
|
break;
|
|
|
|
case V_ASN1_OBJECT:
|
|
|
|
value = decode_obj(p, hlen+length);
|
|
|
|
break;
|
|
|
|
case V_ASN1_UTCTIME: /* FALLTHROUGH */
|
|
|
|
case V_ASN1_GENERALIZEDTIME:
|
|
|
|
value = decode_time(p, hlen+length);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
/* use original value */
|
|
|
|
p += hlen;
|
|
|
|
value = rb_str_new((const char *)p, length);
|
|
|
|
break;
|
2003-10-03 12:58:01 +04:00
|
|
|
}
|
2011-05-23 01:01:13 +04:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
p += hlen;
|
|
|
|
value = rb_str_new((const char *)p, length);
|
|
|
|
}
|
|
|
|
|
|
|
|
*pp += hlen + length;
|
|
|
|
*num_read = hlen + length;
|
|
|
|
|
|
|
|
if (tc == sUNIVERSAL && tag < ossl_asn1_info_size && ossl_asn1_info[tag].klass) {
|
|
|
|
VALUE klass = *ossl_asn1_info[tag].klass;
|
|
|
|
VALUE args[4];
|
|
|
|
args[0] = value;
|
|
|
|
args[1] = INT2NUM(tag);
|
|
|
|
args[2] = Qnil;
|
|
|
|
args[3] = ID2SYM(tc);
|
|
|
|
asn1data = rb_obj_alloc(klass);
|
|
|
|
ossl_asn1_initialize(4, args, asn1data);
|
|
|
|
if(tag == V_ASN1_BIT_STRING){
|
|
|
|
rb_ivar_set(asn1data, sivUNUSED_BITS, LONG2NUM(flag));
|
2003-10-03 12:58:01 +04:00
|
|
|
}
|
2011-05-23 01:01:13 +04:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
asn1data = rb_obj_alloc(cASN1Data);
|
|
|
|
ossl_asn1data_initialize(asn1data, value, INT2NUM(tag), ID2SYM(tc));
|
|
|
|
}
|
|
|
|
|
|
|
|
return asn1data;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
int_ossl_asn1_decode0_cons(unsigned char **pp, long length, long *offset,
|
|
|
|
int depth, int yield, int j, int tag, VALUE tc,
|
|
|
|
long *num_read)
|
|
|
|
{
|
|
|
|
VALUE value, asn1data, ary;
|
|
|
|
int infinite;
|
|
|
|
long off = *offset;
|
|
|
|
|
|
|
|
infinite = (j == 0x21);
|
|
|
|
ary = rb_ary_new();
|
|
|
|
|
|
|
|
while (length > 0 || infinite) {
|
|
|
|
long inner_read = 0;
|
|
|
|
value = ossl_asn1_decode0(pp, length, &off, depth + 1, yield, &inner_read);
|
|
|
|
*num_read += inner_read;
|
|
|
|
rb_ary_push(ary, value);
|
|
|
|
length -= inner_read;
|
|
|
|
|
|
|
|
if (infinite && NUM2INT(ossl_asn1_get_tag(value)) == V_ASN1_EOC)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tc == sUNIVERSAL && (tag == V_ASN1_SEQUENCE || V_ASN1_SET)) {
|
|
|
|
VALUE args[4];
|
|
|
|
VALUE klass = *ossl_asn1_info[tag].klass;
|
|
|
|
if (infinite && tag != V_ASN1_SEQUENCE && tag != V_ASN1_SET) {
|
|
|
|
asn1data = rb_obj_alloc(cASN1Constructive);
|
2003-10-03 12:58:01 +04:00
|
|
|
}
|
2011-05-23 01:01:13 +04:00
|
|
|
else {
|
|
|
|
asn1data = rb_obj_alloc(klass);
|
2003-10-03 12:58:01 +04:00
|
|
|
}
|
2011-05-23 01:01:13 +04:00
|
|
|
args[0] = ary;
|
|
|
|
args[1] = INT2NUM(tag);
|
|
|
|
args[2] = Qnil;
|
|
|
|
args[3] = ID2SYM(tc);
|
|
|
|
ossl_asn1_initialize(4, args, asn1data);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
VALUE args[3];
|
|
|
|
args[0] = ary;
|
|
|
|
args[1] = INT2NUM(tag);
|
|
|
|
args[2] = ID2SYM(tc);
|
|
|
|
asn1data = rb_obj_alloc(cASN1Data);
|
|
|
|
ossl_asn1data_initialize(asn1data, ary, INT2NUM(tag), ID2SYM(tc));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (infinite)
|
|
|
|
ossl_asn1_set_infinite_length(asn1data, Qtrue);
|
|
|
|
else
|
|
|
|
ossl_asn1_set_infinite_length(asn1data, Qfalse);
|
|
|
|
|
|
|
|
*offset = off;
|
|
|
|
return asn1data;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
ossl_asn1_decode0(unsigned char **pp, long length, long *offset, int depth,
|
|
|
|
int yield, long *num_read)
|
|
|
|
{
|
|
|
|
unsigned char *start, *p;
|
|
|
|
const unsigned char *p0;
|
|
|
|
long len, inner_read = 0, off = *offset;
|
|
|
|
int hlen, tag, tc, j;
|
|
|
|
VALUE asn1data, tag_class;
|
|
|
|
|
|
|
|
p = *pp;
|
|
|
|
start = p;
|
|
|
|
p0 = p;
|
|
|
|
j = ASN1_get_object(&p0, &len, &tag, &tc, length);
|
|
|
|
p = (unsigned char *)p0;
|
|
|
|
if(j & 0x80) ossl_raise(eASN1Error, NULL);
|
|
|
|
if(len > length) ossl_raise(eASN1Error, "value is too short");
|
|
|
|
if((tc & V_ASN1_PRIVATE) == V_ASN1_PRIVATE)
|
|
|
|
tag_class = sPRIVATE;
|
|
|
|
else if((tc & V_ASN1_CONTEXT_SPECIFIC) == V_ASN1_CONTEXT_SPECIFIC)
|
|
|
|
tag_class = sCONTEXT_SPECIFIC;
|
|
|
|
else if((tc & V_ASN1_APPLICATION) == V_ASN1_APPLICATION)
|
|
|
|
tag_class = sAPPLICATION;
|
|
|
|
else
|
|
|
|
tag_class = sUNIVERSAL;
|
|
|
|
|
|
|
|
hlen = p - start;
|
|
|
|
|
|
|
|
if(yield) {
|
|
|
|
VALUE arg = rb_ary_new();
|
|
|
|
rb_ary_push(arg, LONG2NUM(depth));
|
|
|
|
rb_ary_push(arg, LONG2NUM(*offset));
|
|
|
|
rb_ary_push(arg, LONG2NUM(hlen));
|
|
|
|
rb_ary_push(arg, LONG2NUM(len));
|
|
|
|
rb_ary_push(arg, (j & V_ASN1_CONSTRUCTED) ? Qtrue : Qfalse);
|
|
|
|
rb_ary_push(arg, ossl_asn1_class2sym(tc));
|
|
|
|
rb_ary_push(arg, INT2NUM(tag));
|
|
|
|
rb_yield(arg);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(j & V_ASN1_CONSTRUCTED) {
|
|
|
|
long max_len = len == 0 ? length : len;
|
|
|
|
*pp += hlen;
|
|
|
|
off += hlen;
|
|
|
|
asn1data = int_ossl_asn1_decode0_cons(pp, max_len, &off, depth, yield, j, tag, tag_class, &inner_read);
|
|
|
|
inner_read += hlen;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if ((j & 0x01) && (len == 0)) ossl_raise(eASN1Error, "Infinite length for primitive value");
|
|
|
|
asn1data = int_ossl_asn1_decode0_prim(pp, len, hlen, tag, tag_class, &inner_read);
|
|
|
|
off += hlen + len;
|
2003-10-03 12:58:01 +04:00
|
|
|
}
|
2011-05-23 01:01:13 +04:00
|
|
|
if (num_read)
|
|
|
|
*num_read = inner_read;
|
|
|
|
if (len != 0 && inner_read != hlen + len) {
|
|
|
|
ossl_raise(eASN1Error,
|
|
|
|
"Type mismatch. Bytes read: %ld Bytes available: %ld",
|
|
|
|
inner_read, hlen + len);
|
|
|
|
}
|
|
|
|
|
2003-10-03 12:58:01 +04:00
|
|
|
*offset = off;
|
2011-05-23 01:01:13 +04:00
|
|
|
return asn1data;
|
|
|
|
}
|
2003-10-03 12:58:01 +04:00
|
|
|
|
2011-05-23 01:01:13 +04:00
|
|
|
static void
|
|
|
|
int_ossl_decode_sanity_check(long len, long read, long offset)
|
|
|
|
{
|
|
|
|
if (len != 0 && (read != len || offset != len)) {
|
|
|
|
ossl_raise(eASN1Error,
|
|
|
|
"Type mismatch. Total bytes read: %ld Bytes available: %ld Offset: %ld",
|
|
|
|
read, len, offset);
|
|
|
|
}
|
2003-10-03 12:58:01 +04:00
|
|
|
}
|
2011-05-16 16:20:53 +04:00
|
|
|
|
2011-05-16 00:15:38 +04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* OpenSSL::ASN1.traverse(asn1) -> nil
|
|
|
|
*
|
|
|
|
* If a block is given, it prints out each of the elements encountered.
|
|
|
|
* Block parameters are (in that order):
|
|
|
|
* * depth: The recursion depth, plus one with each constructed value being encountered (Number)
|
|
|
|
* * offset: Current byte offset (Number)
|
|
|
|
* * header length: Combined length in bytes of the Tag and Length headers. (Number)
|
|
|
|
* * length: The overall remaining length of the entire data (Number)
|
|
|
|
* * constructed: Whether this value is constructed or not (Boolean)
|
|
|
|
* * tag_class: Current tag class (Symbol)
|
|
|
|
* * tag: The current tag (Number)
|
|
|
|
*
|
|
|
|
* == Example
|
2011-05-17 01:51:12 +04:00
|
|
|
* der = File.binread('asn1data.der')
|
|
|
|
* OpenSSL::ASN1.traverse(der) do | depth, offset, header_len, length, constructed, tag_class, tag|
|
|
|
|
* puts "Depth: #{depth} Offset: #{offset} Length: #{length}"
|
|
|
|
* puts "Header length: #{header_len} Tag: #{tag} Tag class: #{tag_class} Constructed: #{constructed}"
|
|
|
|
* end
|
2011-05-16 00:15:38 +04:00
|
|
|
*/
|
2003-10-03 12:58:01 +04:00
|
|
|
static VALUE
|
|
|
|
ossl_asn1_traverse(VALUE self, VALUE obj)
|
|
|
|
{
|
|
|
|
unsigned char *p;
|
2004-12-21 16:09:26 +03:00
|
|
|
volatile VALUE tmp;
|
2011-05-23 01:01:13 +04:00
|
|
|
long len, read = 0, offset = 0;
|
2003-10-03 12:58:01 +04:00
|
|
|
|
|
|
|
obj = ossl_to_der_if_possible(obj);
|
2004-12-21 16:09:26 +03:00
|
|
|
tmp = rb_str_new4(StringValue(obj));
|
2008-07-22 19:34:23 +04:00
|
|
|
p = (unsigned char *)RSTRING_PTR(tmp);
|
2011-05-23 01:01:13 +04:00
|
|
|
len = RSTRING_LEN(tmp);
|
|
|
|
ossl_asn1_decode0(&p, len, &offset, 0, 1, &read);
|
|
|
|
int_ossl_decode_sanity_check(len, read, offset);
|
2004-12-21 06:44:17 +03:00
|
|
|
return Qnil;
|
2003-10-03 12:58:01 +04:00
|
|
|
}
|
|
|
|
|
2011-05-16 00:15:38 +04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* OpenSSL::ASN1.decode(der) -> ASN1Data
|
|
|
|
*
|
|
|
|
* Decodes a BER- or DER-encoded value and creates an ASN1Data instance. +der+
|
|
|
|
* may be a +String+ or any object that features a +#to_der+ method transforming
|
|
|
|
* it into a BER-/DER-encoded +String+.
|
|
|
|
*
|
|
|
|
* == Example
|
2011-05-17 01:51:12 +04:00
|
|
|
* der = File.binread('asn1data')
|
|
|
|
* asn1 = OpenSSL::ASN1.decode(der)
|
2011-05-16 16:20:53 +04:00
|
|
|
*/
|
2003-10-03 12:58:01 +04:00
|
|
|
static VALUE
|
|
|
|
ossl_asn1_decode(VALUE self, VALUE obj)
|
|
|
|
{
|
|
|
|
VALUE ret, ary;
|
|
|
|
unsigned char *p;
|
2004-12-21 16:09:26 +03:00
|
|
|
volatile VALUE tmp;
|
2011-05-23 01:01:13 +04:00
|
|
|
long len, read = 0, offset = 0;
|
2003-10-03 12:58:01 +04:00
|
|
|
|
|
|
|
obj = ossl_to_der_if_possible(obj);
|
2004-12-21 16:09:26 +03:00
|
|
|
tmp = rb_str_new4(StringValue(obj));
|
2008-07-22 19:34:23 +04:00
|
|
|
p = (unsigned char *)RSTRING_PTR(tmp);
|
2011-05-23 01:01:13 +04:00
|
|
|
len = RSTRING_LEN(tmp);
|
|
|
|
ret = ossl_asn1_decode0(&p, len, &offset, 0, 0, &read);
|
|
|
|
int_ossl_decode_sanity_check(len, read, offset);
|
2003-10-03 12:58:01 +04:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2011-05-16 00:15:38 +04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* OpenSSL::ASN1.decode_all(der) -> Array of ASN1Data
|
|
|
|
*
|
|
|
|
* Similar to +decode+ with the difference that +decode+ expects one
|
|
|
|
* distinct value represented in +der+. +decode_all+ on the contrary
|
|
|
|
* decodes a sequence of sequential BER/DER values lined up in +der+
|
|
|
|
* and returns them as an array.
|
|
|
|
*
|
|
|
|
* == Example
|
2011-05-17 01:51:12 +04:00
|
|
|
* ders = File.binread('asn1data_seq')
|
|
|
|
* asn1_ary = OpenSSL::ASN1.decode_all(ders)
|
2011-05-16 00:15:38 +04:00
|
|
|
*/
|
2003-10-03 12:58:01 +04:00
|
|
|
static VALUE
|
|
|
|
ossl_asn1_decode_all(VALUE self, VALUE obj)
|
|
|
|
{
|
2011-05-23 01:01:13 +04:00
|
|
|
VALUE ary, val;
|
2003-10-03 12:58:01 +04:00
|
|
|
unsigned char *p;
|
2011-05-23 01:01:13 +04:00
|
|
|
long len, tmp_len = 0, read = 0, offset = 0;
|
2004-12-21 16:09:26 +03:00
|
|
|
volatile VALUE tmp;
|
2003-10-03 12:58:01 +04:00
|
|
|
|
|
|
|
obj = ossl_to_der_if_possible(obj);
|
2004-12-21 16:09:26 +03:00
|
|
|
tmp = rb_str_new4(StringValue(obj));
|
2008-07-22 19:34:23 +04:00
|
|
|
p = (unsigned char *)RSTRING_PTR(tmp);
|
2011-05-23 01:01:13 +04:00
|
|
|
len = RSTRING_LEN(tmp);
|
|
|
|
tmp_len = len;
|
|
|
|
ary = rb_ary_new();
|
|
|
|
while (tmp_len > 0) {
|
|
|
|
long tmp_read = 0;
|
|
|
|
val = ossl_asn1_decode0(&p, tmp_len, &offset, 0, 0, &tmp_read);
|
|
|
|
rb_ary_push(ary, val);
|
|
|
|
read += tmp_read;
|
|
|
|
tmp_len -= tmp_read;
|
|
|
|
}
|
|
|
|
int_ossl_decode_sanity_check(len, read, offset);
|
|
|
|
return ary;
|
2003-10-03 12:58:01 +04:00
|
|
|
}
|
|
|
|
|
2011-05-16 00:15:38 +04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* OpenSSL::ASN1::Primitive.new( value [, tag, tagging, tag_class ]) => Primitive
|
|
|
|
*
|
|
|
|
* +value+: is mandatory.
|
2011-05-17 01:51:12 +04:00
|
|
|
*
|
2011-05-16 00:15:38 +04:00
|
|
|
* +tag+: optional, may be specified for tagged values. If no +tag+ is
|
|
|
|
* specified, the UNIVERSAL tag corresponding to the Primitive sub-class
|
|
|
|
* is used by default.
|
2011-05-17 01:51:12 +04:00
|
|
|
*
|
2011-05-16 00:15:38 +04:00
|
|
|
* +tagging+: may be used as an encoding hint to encode a value either
|
|
|
|
* explicitly or implicitly, see ASN1 for possible values.
|
2011-05-17 01:51:12 +04:00
|
|
|
*
|
2011-05-16 00:15:38 +04:00
|
|
|
* +tag_class+: if +tag+ and +tagging+ are +nil+ then this is set to
|
|
|
|
* +:UNIVERSAL+ by default. If either +tag+ or +tagging+ are set then
|
|
|
|
* +:CONTEXT_SPECIFIC+ is used as the default. For possible values please
|
|
|
|
* cf. ASN1.
|
|
|
|
*
|
|
|
|
* == Example
|
2011-05-17 01:51:12 +04:00
|
|
|
* int = OpenSSL::ASN1::Integer.new(42)
|
|
|
|
* zero_tagged_int = OpenSSL::ASN1::Integer.new(42, 0, :IMPLICIT)
|
|
|
|
* private_explicit_zero_tagged_int = OpenSSL::ASN1::Integer.new(42, 0, :EXPLICIT, :PRIVATE)
|
2011-05-16 00:15:38 +04:00
|
|
|
*/
|
2003-10-03 12:58:01 +04:00
|
|
|
static VALUE
|
|
|
|
ossl_asn1_initialize(int argc, VALUE *argv, VALUE self)
|
|
|
|
{
|
|
|
|
VALUE value, tag, tagging, tag_class;
|
|
|
|
|
|
|
|
rb_scan_args(argc, argv, "13", &value, &tag, &tagging, &tag_class);
|
|
|
|
if(argc > 1){
|
|
|
|
if(NIL_P(tag))
|
|
|
|
ossl_raise(eASN1Error, "must specify tag number");
|
2011-05-22 23:38:05 +04:00
|
|
|
if(!NIL_P(tagging) && !SYMBOL_P(tagging))
|
|
|
|
ossl_raise(eASN1Error, "invalid tagging method");
|
|
|
|
if(NIL_P(tag_class)) {
|
|
|
|
if (NIL_P(tagging))
|
|
|
|
tag_class = ID2SYM(sUNIVERSAL);
|
|
|
|
else
|
|
|
|
tag_class = ID2SYM(sCONTEXT_SPECIFIC);
|
|
|
|
}
|
2003-10-03 12:58:01 +04:00
|
|
|
if(!SYMBOL_P(tag_class))
|
2004-03-29 11:54:38 +04:00
|
|
|
ossl_raise(eASN1Error, "invalid tag class");
|
2003-10-03 12:58:01 +04:00
|
|
|
if(SYM2ID(tagging) == sIMPLICIT && NUM2INT(tag) > 31)
|
2004-03-29 11:54:38 +04:00
|
|
|
ossl_raise(eASN1Error, "tag number for Universal too large");
|
2003-10-03 12:58:01 +04:00
|
|
|
}
|
|
|
|
else{
|
|
|
|
tag = INT2NUM(ossl_asn1_default_tag(self));
|
2011-05-22 23:38:05 +04:00
|
|
|
tagging = Qnil;
|
2003-10-03 12:58:01 +04:00
|
|
|
tag_class = ID2SYM(sUNIVERSAL);
|
|
|
|
}
|
|
|
|
ossl_asn1_set_tag(self, tag);
|
|
|
|
ossl_asn1_set_value(self, value);
|
|
|
|
ossl_asn1_set_tagging(self, tagging);
|
|
|
|
ossl_asn1_set_tag_class(self, tag_class);
|
2010-12-11 20:45:42 +03:00
|
|
|
ossl_asn1_set_infinite_length(self, Qfalse);
|
2003-10-03 12:58:01 +04:00
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2010-12-11 20:45:42 +03:00
|
|
|
static VALUE
|
|
|
|
ossl_asn1eoc_initialize(VALUE self) {
|
|
|
|
VALUE tag, tagging, tag_class, value;
|
|
|
|
tag = INT2NUM(ossl_asn1_default_tag(self));
|
|
|
|
tagging = Qnil;
|
|
|
|
tag_class = ID2SYM(sUNIVERSAL);
|
|
|
|
value = rb_str_new("", 0);
|
|
|
|
ossl_asn1_set_tag(self, tag);
|
|
|
|
ossl_asn1_set_value(self, value);
|
|
|
|
ossl_asn1_set_tagging(self, tagging);
|
|
|
|
ossl_asn1_set_tag_class(self, tag_class);
|
|
|
|
ossl_asn1_set_infinite_length(self, Qfalse);
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2004-05-24 20:50:59 +04:00
|
|
|
static int
|
|
|
|
ossl_i2d_ASN1_TYPE(ASN1_TYPE *a, unsigned char **pp)
|
|
|
|
{
|
|
|
|
#if OPENSSL_VERSION_NUMBER < 0x00907000L
|
|
|
|
if(!a) return 0;
|
|
|
|
if(a->type == V_ASN1_BOOLEAN)
|
|
|
|
return i2d_ASN1_BOOLEAN(a->value.boolean, pp);
|
|
|
|
#endif
|
|
|
|
return i2d_ASN1_TYPE(a, pp);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
ossl_ASN1_TYPE_free(ASN1_TYPE *a)
|
|
|
|
{
|
|
|
|
#if OPENSSL_VERSION_NUMBER < 0x00907000L
|
|
|
|
if(!a) return;
|
|
|
|
if(a->type == V_ASN1_BOOLEAN){
|
|
|
|
OPENSSL_free(a);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
ASN1_TYPE_free(a);
|
|
|
|
}
|
|
|
|
|
2011-05-16 00:15:38 +04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* asn1.to_der => DER-encoded String
|
2011-05-16 16:20:53 +04:00
|
|
|
*
|
2011-05-16 00:15:38 +04:00
|
|
|
* See ASN1Data#to_der for details. *
|
|
|
|
*/
|
2003-10-03 12:58:01 +04:00
|
|
|
static VALUE
|
|
|
|
ossl_asn1prim_to_der(VALUE self)
|
|
|
|
{
|
|
|
|
ASN1_TYPE *asn1;
|
|
|
|
int tn, tc, explicit;
|
2008-02-26 10:07:26 +03:00
|
|
|
long len, reallen;
|
2003-10-03 12:58:01 +04:00
|
|
|
unsigned char *buf, *p;
|
|
|
|
VALUE str;
|
|
|
|
|
|
|
|
tn = NUM2INT(ossl_asn1_get_tag(self));
|
|
|
|
tc = ossl_asn1_tag_class(self);
|
|
|
|
explicit = ossl_asn1_is_explicit(self);
|
|
|
|
asn1 = ossl_asn1_get_asn1type(self);
|
|
|
|
|
2008-02-26 10:07:26 +03:00
|
|
|
len = ASN1_object_size(1, ossl_i2d_ASN1_TYPE(asn1, NULL), tn);
|
|
|
|
if(!(buf = OPENSSL_malloc(len))){
|
2004-05-24 20:50:59 +04:00
|
|
|
ossl_ASN1_TYPE_free(asn1);
|
2003-10-03 12:58:01 +04:00
|
|
|
ossl_raise(eASN1Error, "cannot alloc buffer");
|
|
|
|
}
|
|
|
|
p = buf;
|
2008-02-26 10:07:26 +03:00
|
|
|
if (tc == V_ASN1_UNIVERSAL) {
|
|
|
|
ossl_i2d_ASN1_TYPE(asn1, &p);
|
|
|
|
} else if (explicit) {
|
|
|
|
ASN1_put_object(&p, 1, ossl_i2d_ASN1_TYPE(asn1, NULL), tn, tc);
|
|
|
|
ossl_i2d_ASN1_TYPE(asn1, &p);
|
|
|
|
} else {
|
|
|
|
ossl_i2d_ASN1_TYPE(asn1, &p);
|
|
|
|
*buf = tc | tn | (*buf & V_ASN1_CONSTRUCTED);
|
2003-10-03 12:58:01 +04:00
|
|
|
}
|
2004-05-24 20:50:59 +04:00
|
|
|
ossl_ASN1_TYPE_free(asn1);
|
2003-10-03 12:58:01 +04:00
|
|
|
reallen = p - buf;
|
2008-02-26 10:07:26 +03:00
|
|
|
assert(reallen <= len);
|
2011-03-24 10:29:21 +03:00
|
|
|
str = ossl_buf2str((char *)buf, rb_long2int(reallen)); /* buf will be free in ossl_buf2str */
|
2003-10-03 12:58:01 +04:00
|
|
|
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
2011-05-16 00:15:38 +04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* asn1.to_der => DER-encoded String
|
|
|
|
*
|
|
|
|
* See ASN1Data#to_der for details.
|
|
|
|
*/
|
2003-10-03 12:58:01 +04:00
|
|
|
static VALUE
|
2004-07-14 13:09:36 +04:00
|
|
|
ossl_asn1cons_to_der(VALUE self)
|
2003-10-03 12:58:01 +04:00
|
|
|
{
|
2010-12-11 20:45:42 +03:00
|
|
|
int tag, tn, tc, explicit, constructed = 1;
|
2011-03-24 10:29:21 +03:00
|
|
|
int found_prim = 0, seq_len;
|
|
|
|
long length;
|
2003-10-03 12:58:01 +04:00
|
|
|
unsigned char *p;
|
2011-05-23 02:00:24 +04:00
|
|
|
VALUE value, str, inf_length;
|
2003-10-03 12:58:01 +04:00
|
|
|
|
|
|
|
tn = NUM2INT(ossl_asn1_get_tag(self));
|
|
|
|
tc = ossl_asn1_tag_class(self);
|
2010-12-11 20:45:42 +03:00
|
|
|
inf_length = ossl_asn1_get_infinite_length(self);
|
|
|
|
if (inf_length == Qtrue) {
|
2011-05-23 02:00:24 +04:00
|
|
|
VALUE ary, example;
|
2011-05-22 23:08:45 +04:00
|
|
|
constructed = 2;
|
|
|
|
if (CLASS_OF(self) == cASN1Sequence ||
|
|
|
|
CLASS_OF(self) == cASN1Set) {
|
|
|
|
tag = ossl_asn1_default_tag(self);
|
|
|
|
}
|
2011-05-23 02:00:24 +04:00
|
|
|
else { /* must be a constructive encoding of a primitive value */
|
2011-05-22 23:08:45 +04:00
|
|
|
ary = ossl_asn1_get_value(self);
|
2011-05-23 02:00:24 +04:00
|
|
|
if (!rb_obj_is_kind_of(ary, rb_cArray))
|
|
|
|
ossl_raise(eASN1Error, "Constructive value must be an Array");
|
2011-05-22 23:08:45 +04:00
|
|
|
/* Recursively descend until a primitive value is found.
|
|
|
|
The overall value of the entire constructed encoding
|
|
|
|
is of the type of the first primitive encoding to be
|
|
|
|
found. */
|
|
|
|
while (!found_prim){
|
|
|
|
example = rb_ary_entry(ary, 0);
|
|
|
|
if (rb_obj_is_kind_of(example, cASN1Primitive)){
|
|
|
|
found_prim = 1;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* example is another ASN1Constructive */
|
|
|
|
if (!rb_obj_is_kind_of(example, cASN1Constructive)){
|
|
|
|
ossl_raise(eASN1Error, "invalid constructed encoding");
|
|
|
|
return Qnil; /* dummy */
|
|
|
|
}
|
|
|
|
ary = ossl_asn1_get_value(example);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
tag = ossl_asn1_default_tag(example);
|
|
|
|
}
|
2010-12-11 20:45:42 +03:00
|
|
|
}
|
|
|
|
else {
|
2011-05-23 01:34:28 +04:00
|
|
|
if (CLASS_OF(self) == cASN1Constructive)
|
|
|
|
ossl_raise(eASN1Error, "Constructive shall only be used with infinite length");
|
2011-05-22 23:08:45 +04:00
|
|
|
tag = ossl_asn1_default_tag(self);
|
2010-12-11 20:45:42 +03:00
|
|
|
}
|
2003-10-03 12:58:01 +04:00
|
|
|
explicit = ossl_asn1_is_explicit(self);
|
|
|
|
value = join_der(ossl_asn1_get_value(self));
|
|
|
|
|
2011-03-24 10:29:21 +03:00
|
|
|
seq_len = ASN1_object_size(constructed, RSTRING_LENINT(value), tag);
|
2010-12-11 20:45:42 +03:00
|
|
|
length = ASN1_object_size(constructed, seq_len, tn);
|
2003-10-03 12:58:01 +04:00
|
|
|
str = rb_str_new(0, length);
|
2008-07-22 19:34:23 +04:00
|
|
|
p = (unsigned char *)RSTRING_PTR(str);
|
2003-10-03 12:58:01 +04:00
|
|
|
if(tc == V_ASN1_UNIVERSAL)
|
2011-03-24 10:29:21 +03:00
|
|
|
ASN1_put_object(&p, constructed, RSTRING_LENINT(value), tn, tc);
|
2003-10-03 12:58:01 +04:00
|
|
|
else{
|
|
|
|
if(explicit){
|
2011-03-24 10:29:21 +03:00
|
|
|
ASN1_put_object(&p, constructed, seq_len, tn, tc);
|
|
|
|
ASN1_put_object(&p, constructed, RSTRING_LENINT(value), tag, V_ASN1_UNIVERSAL);
|
2003-10-03 12:58:01 +04:00
|
|
|
}
|
2011-05-22 23:08:45 +04:00
|
|
|
else{
|
|
|
|
ASN1_put_object(&p, constructed, RSTRING_LENINT(value), tn, tc);
|
|
|
|
}
|
2003-10-03 12:58:01 +04:00
|
|
|
}
|
2006-08-31 14:30:33 +04:00
|
|
|
memcpy(p, RSTRING_PTR(value), RSTRING_LEN(value));
|
|
|
|
p += RSTRING_LEN(value);
|
2011-05-22 23:08:45 +04:00
|
|
|
|
|
|
|
/* In this case we need an additional EOC (one for the explicit part and
|
|
|
|
* one for the Constructive itself. The EOC for the Constructive is
|
|
|
|
* supplied by the user, but that for the "explicit wrapper" must be
|
|
|
|
* added here.
|
|
|
|
*/
|
|
|
|
if (explicit && inf_length == Qtrue) {
|
|
|
|
ASN1_put_eoc(&p);
|
|
|
|
}
|
2003-10-03 12:58:01 +04:00
|
|
|
ossl_str_adjust(str, p);
|
|
|
|
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
2011-05-16 00:15:38 +04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* asn1_ary.each { |asn1| block } => asn1_ary
|
|
|
|
*
|
|
|
|
* Calls <i>block</i> once for each element in +self+, passing that element
|
|
|
|
* as parameter +asn1+. If no block is given, an enumerator is returned
|
|
|
|
* instead.
|
|
|
|
*
|
|
|
|
* == Example
|
2011-05-17 01:51:12 +04:00
|
|
|
* asn1_ary.each do |asn1|
|
|
|
|
* puts asn1
|
|
|
|
* end
|
2011-05-16 00:15:38 +04:00
|
|
|
*/
|
2003-10-03 12:58:01 +04:00
|
|
|
static VALUE
|
|
|
|
ossl_asn1cons_each(VALUE self)
|
|
|
|
{
|
|
|
|
rb_ary_each(ossl_asn1_get_value(self));
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
ossl_asn1obj_s_register(VALUE self, VALUE oid, VALUE sn, VALUE ln)
|
|
|
|
{
|
|
|
|
StringValue(oid);
|
|
|
|
StringValue(sn);
|
|
|
|
StringValue(ln);
|
|
|
|
|
2006-08-31 14:30:33 +04:00
|
|
|
if(!OBJ_create(RSTRING_PTR(oid), RSTRING_PTR(sn), RSTRING_PTR(ln)))
|
2003-10-03 12:58:01 +04:00
|
|
|
ossl_raise(eASN1Error, NULL);
|
|
|
|
|
|
|
|
return Qtrue;
|
|
|
|
}
|
|
|
|
|
2003-10-06 20:55:00 +04:00
|
|
|
static VALUE
|
|
|
|
ossl_asn1obj_get_sn(VALUE self)
|
|
|
|
{
|
|
|
|
VALUE val, ret = Qnil;
|
|
|
|
int nid;
|
|
|
|
|
|
|
|
val = ossl_asn1_get_value(self);
|
|
|
|
if ((nid = OBJ_txt2nid(StringValuePtr(val))) != NID_undef)
|
|
|
|
ret = rb_str_new2(OBJ_nid2sn(nid));
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
ossl_asn1obj_get_ln(VALUE self)
|
|
|
|
{
|
|
|
|
VALUE val, ret = Qnil;
|
|
|
|
int nid;
|
|
|
|
|
|
|
|
val = ossl_asn1_get_value(self);
|
|
|
|
if ((nid = OBJ_txt2nid(StringValuePtr(val))) != NID_undef)
|
|
|
|
ret = rb_str_new2(OBJ_nid2ln(nid));
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
ossl_asn1obj_get_oid(VALUE self)
|
|
|
|
{
|
|
|
|
VALUE val;
|
|
|
|
ASN1_OBJECT *a1obj;
|
|
|
|
char buf[128];
|
|
|
|
|
|
|
|
val = ossl_asn1_get_value(self);
|
|
|
|
a1obj = obj_to_asn1obj(val);
|
|
|
|
OBJ_obj2txt(buf, sizeof(buf), a1obj, 1);
|
|
|
|
ASN1_OBJECT_free(a1obj);
|
|
|
|
|
|
|
|
return rb_str_new2(buf);
|
|
|
|
}
|
|
|
|
|
2003-10-03 12:58:01 +04:00
|
|
|
#define OSSL_ASN1_IMPL_FACTORY_METHOD(klass) \
|
|
|
|
static VALUE ossl_asn1_##klass(int argc, VALUE *argv, VALUE self)\
|
|
|
|
{ return rb_funcall3(cASN1##klass, rb_intern("new"), argc, argv); }
|
|
|
|
|
|
|
|
OSSL_ASN1_IMPL_FACTORY_METHOD(Boolean)
|
|
|
|
OSSL_ASN1_IMPL_FACTORY_METHOD(Integer)
|
|
|
|
OSSL_ASN1_IMPL_FACTORY_METHOD(Enumerated)
|
|
|
|
OSSL_ASN1_IMPL_FACTORY_METHOD(BitString)
|
|
|
|
OSSL_ASN1_IMPL_FACTORY_METHOD(OctetString)
|
|
|
|
OSSL_ASN1_IMPL_FACTORY_METHOD(UTF8String)
|
|
|
|
OSSL_ASN1_IMPL_FACTORY_METHOD(NumericString)
|
|
|
|
OSSL_ASN1_IMPL_FACTORY_METHOD(PrintableString)
|
|
|
|
OSSL_ASN1_IMPL_FACTORY_METHOD(T61String)
|
|
|
|
OSSL_ASN1_IMPL_FACTORY_METHOD(VideotexString)
|
|
|
|
OSSL_ASN1_IMPL_FACTORY_METHOD(IA5String)
|
|
|
|
OSSL_ASN1_IMPL_FACTORY_METHOD(GraphicString)
|
|
|
|
OSSL_ASN1_IMPL_FACTORY_METHOD(ISO64String)
|
|
|
|
OSSL_ASN1_IMPL_FACTORY_METHOD(GeneralString)
|
|
|
|
OSSL_ASN1_IMPL_FACTORY_METHOD(UniversalString)
|
|
|
|
OSSL_ASN1_IMPL_FACTORY_METHOD(BMPString)
|
|
|
|
OSSL_ASN1_IMPL_FACTORY_METHOD(Null)
|
|
|
|
OSSL_ASN1_IMPL_FACTORY_METHOD(ObjectId)
|
|
|
|
OSSL_ASN1_IMPL_FACTORY_METHOD(UTCTime)
|
|
|
|
OSSL_ASN1_IMPL_FACTORY_METHOD(GeneralizedTime)
|
|
|
|
OSSL_ASN1_IMPL_FACTORY_METHOD(Sequence)
|
|
|
|
OSSL_ASN1_IMPL_FACTORY_METHOD(Set)
|
2010-12-11 20:45:42 +03:00
|
|
|
OSSL_ASN1_IMPL_FACTORY_METHOD(EndOfContent)
|
2003-10-03 12:58:01 +04:00
|
|
|
|
|
|
|
void
|
|
|
|
Init_ossl_asn1()
|
|
|
|
{
|
|
|
|
VALUE ary;
|
|
|
|
int i;
|
|
|
|
|
2010-12-06 03:54:44 +03:00
|
|
|
#if 0
|
|
|
|
mOSSL = rb_define_module("OpenSSL"); /* let rdoc know about mOSSL */
|
2007-03-12 05:01:19 +03:00
|
|
|
#endif
|
|
|
|
|
2003-10-03 12:58:01 +04:00
|
|
|
sUNIVERSAL = rb_intern("UNIVERSAL");
|
|
|
|
sCONTEXT_SPECIFIC = rb_intern("CONTEXT_SPECIFIC");
|
|
|
|
sAPPLICATION = rb_intern("APPLICATION");
|
|
|
|
sPRIVATE = rb_intern("PRIVATE");
|
|
|
|
sEXPLICIT = rb_intern("EXPLICIT");
|
|
|
|
sIMPLICIT = rb_intern("IMPLICIT");
|
|
|
|
|
2011-05-22 18:27:02 +04:00
|
|
|
sivVALUE = rb_intern("@value");
|
|
|
|
sivTAG = rb_intern("@tag");
|
|
|
|
sivTAGGING = rb_intern("@tagging");
|
|
|
|
sivTAG_CLASS = rb_intern("@tag_class");
|
|
|
|
sivINFINITE_LENGTH = rb_intern("@infinite_length");
|
|
|
|
sivUNUSED_BITS = rb_intern("@unused_bits");
|
|
|
|
|
2011-05-16 00:15:38 +04:00
|
|
|
/*
|
|
|
|
* Document-module: OpenSSL::ASN1
|
|
|
|
*
|
2011-05-16 16:20:53 +04:00
|
|
|
* Abstract Syntax Notation One (or ASN.1) is a notation syntax to
|
|
|
|
* describe data structures and is defined in ITU-T X.680. ASN.1 itself
|
|
|
|
* does not mandate any encoding or parsing rules, but usually ASN.1 data
|
2011-05-16 00:15:38 +04:00
|
|
|
* structures are encoded using the Distinguished Encoding Rules (DER) or
|
|
|
|
* less often the Basic Encoding Rules (BER) described in ITU-T X.690. DER
|
2011-05-16 16:20:53 +04:00
|
|
|
* and BER encodings are binary Tag-Length-Value (TLV) encodings that are
|
2011-05-16 00:15:38 +04:00
|
|
|
* quite concise compared to other popular data description formats such
|
|
|
|
* as XML, JSON etc.
|
2011-05-16 16:20:53 +04:00
|
|
|
* ASN.1 data structures are very common in cryptographic applications,
|
|
|
|
* e.g. X.509 public key certificates or certificate revocation lists
|
|
|
|
* (CRLs) are all defined in ASN.1 and DER-encoded. ASN.1, DER and BER are
|
2011-05-16 00:15:38 +04:00
|
|
|
* the building blocks of applied cryptography.
|
|
|
|
* The ASN1 module provides the necessary classes that allow generation
|
|
|
|
* of ASN.1 data structures and the methods to encode them using a DER
|
|
|
|
* encoding. The decode method allows parsing arbitrary BER-/DER-encoded
|
|
|
|
* data to a Ruby object that can then be modified and re-encoded at will.
|
|
|
|
*
|
|
|
|
* == ASN.1 class hierarchy
|
2011-05-16 16:20:53 +04:00
|
|
|
*
|
|
|
|
* The base class representing ASN.1 structures is ASN1Data. ASN1Data offers
|
|
|
|
* attributes to read and set the +tag+, the +tag_class+ and finally the
|
|
|
|
* +value+ of a particular ASN.1 item. Upon parsing, any tagged values
|
2011-05-16 00:15:38 +04:00
|
|
|
* (implicit or explicit) will be represented by ASN1Data instances because
|
|
|
|
* their "real type" can only be determined using out-of-band information
|
2011-05-16 16:20:53 +04:00
|
|
|
* from the ASN.1 type declaration. Since this information is normally
|
2011-05-16 00:15:38 +04:00
|
|
|
* known when encoding a type, all sub-classes of ASN1Data offer an
|
|
|
|
* additional attribute +tagging+ that allows to encode a value implicitly
|
|
|
|
* (+:IMPLICIT+) or explicitly (+:EXPLICIT+).
|
|
|
|
*
|
|
|
|
* === Constructive
|
|
|
|
*
|
|
|
|
* Constructive is, as its name implies, the base class for all
|
|
|
|
* constructed encodings, i.e. those that consist of several values,
|
2011-05-16 16:20:53 +04:00
|
|
|
* opposed to "primitive" encodings with just one single value.
|
2011-05-16 00:15:38 +04:00
|
|
|
* Primitive values that are encoded with "infinite length" are typically
|
|
|
|
* constructed (their values come in multiple chunks) and are therefore
|
|
|
|
* represented by instances of Constructive. The value of an Constructive
|
|
|
|
* is always an Array.
|
2011-05-16 16:20:53 +04:00
|
|
|
*
|
2011-05-16 00:15:38 +04:00
|
|
|
* ==== ASN1::Set and ASN1::Sequence
|
|
|
|
*
|
|
|
|
* The most common constructive encodings are SETs and SEQUENCEs, which is
|
|
|
|
* why there are two sub-classes of Constructive representing each of
|
2011-05-16 16:20:53 +04:00
|
|
|
* them.
|
2011-05-16 00:15:38 +04:00
|
|
|
*
|
|
|
|
* === Primitive
|
|
|
|
*
|
|
|
|
* This is the super class of all primitive values. Primitive
|
|
|
|
* itself is not used when parsing ASN.1 data, all values are either
|
|
|
|
* instances of a corresponding sub-class of Primitive or they are
|
|
|
|
* instances of ASN1Data if the value was tagged implicitly or explicitly.
|
2011-05-16 16:20:53 +04:00
|
|
|
* Please cf. Primitive documentation for details on sub-classes and
|
2011-05-17 01:51:12 +04:00
|
|
|
* their respective mappings of ASN.1 data types to Ruby objects.
|
2011-05-16 00:15:38 +04:00
|
|
|
*
|
2011-05-17 01:51:12 +04:00
|
|
|
* == Possible values for +tagging+
|
2011-05-16 16:20:53 +04:00
|
|
|
*
|
2011-05-16 00:15:38 +04:00
|
|
|
* When constructing an ASN1Data object the ASN.1 type definition may
|
|
|
|
* require certain elements to be either implicitly or explicitly tagged.
|
2011-05-16 16:20:53 +04:00
|
|
|
* This can be achieved by setting the +tagging+ attribute manually for
|
|
|
|
* sub-classes of ASN1Data. Use the symbol +:IMPLICIT+ for implicit
|
2011-05-16 00:15:38 +04:00
|
|
|
* tagging and +:EXPLICIT+ if the element requires explicit tagging.
|
|
|
|
*
|
|
|
|
* == Possible values for +tag_class+
|
|
|
|
*
|
|
|
|
* It is possible to create arbitrary ASN1Data objects that also support
|
|
|
|
* a PRIVATE or APPLICATION tag class. Possible values for the +tag_class+
|
|
|
|
* attribute are:
|
|
|
|
* * +:UNIVERSAL+ (the default for untagged values)
|
|
|
|
* * +:CONTEXT_SPECIFIC+ (the default for tagged values)
|
|
|
|
* * +:APPLICATION+
|
2011-05-16 16:20:53 +04:00
|
|
|
* * +:PRIVATE+
|
2011-05-16 00:15:38 +04:00
|
|
|
*
|
|
|
|
* == Tag constants
|
|
|
|
*
|
|
|
|
* There is a constant defined for each universal tag:
|
|
|
|
* * OpenSSL::ASN1::EOC (0)
|
|
|
|
* * OpenSSL::ASN1::BOOLEAN (1)
|
|
|
|
* * OpenSSL::ASN1::INTEGER (2)
|
|
|
|
* * OpenSSL::ASN1::BIT_STRING (3)
|
|
|
|
* * OpenSSL::ASN1::OCTET_STRING (4)
|
|
|
|
* * OpenSSL::ASN1::NULL (5)
|
|
|
|
* * OpenSSL::ASN1::OBJECT (6)
|
|
|
|
* * OpenSSL::ASN1::ENUMERATED (10)
|
|
|
|
* * OpenSSL::ASN1::UTF8STRING (12)
|
|
|
|
* * OpenSSL::ASN1::SEQUENCE (16)
|
|
|
|
* * OpenSSL::ASN1::SET (17)
|
|
|
|
* * OpenSSL::ASN1::NUMERICSTRING (18)
|
|
|
|
* * OpenSSL::ASN1::PRINTABLESTRING (19)
|
|
|
|
* * OpenSSL::ASN1::T61STRING (20)
|
|
|
|
* * OpenSSL::ASN1::VIDEOTEXSTRING (21)
|
|
|
|
* * OpenSSL::ASN1::IA5STRING (22)
|
|
|
|
* * OpenSSL::ASN1::UTCTIME (23)
|
|
|
|
* * OpenSSL::ASN1::GENERALIZEDTIME (24)
|
|
|
|
* * OpenSSL::ASN1::GRAPHICSTRING (25)
|
|
|
|
* * OpenSSL::ASN1::ISO64STRING (26)
|
|
|
|
* * OpenSSL::ASN1::GENERALSTRING (27)
|
|
|
|
* * OpenSSL::ASN1::UNIVERSALSTRING (28)
|
|
|
|
* * OpenSSL::ASN1::BMPSTRING (30)
|
|
|
|
*
|
|
|
|
* == UNIVERSAL_TAG_NAME constant
|
|
|
|
*
|
|
|
|
* An Array that stores the name of a given tag number. These names are
|
|
|
|
* the same as the name of the tag constant that is additionally defined,
|
|
|
|
* e.g. UNIVERSAL_TAG_NAME[2] = "INTEGER" and OpenSSL::ASN1::INTEGER = 2.
|
2011-05-16 16:20:53 +04:00
|
|
|
*
|
2011-05-16 00:15:38 +04:00
|
|
|
* == Example usage
|
|
|
|
*
|
|
|
|
* === Decoding and viewing a DER-encoded file
|
2011-05-17 01:51:12 +04:00
|
|
|
* require 'openssl'
|
|
|
|
* require 'pp'
|
|
|
|
* der = File.binread('data.der')
|
|
|
|
* asn1 = OpenSSL::ASN1.decode(der)
|
|
|
|
* pp der
|
2011-05-16 00:15:38 +04:00
|
|
|
*
|
|
|
|
* === Creating an ASN.1 structure and DER-encoding it
|
2011-05-17 01:51:12 +04:00
|
|
|
* require 'openssl'
|
|
|
|
* version = OpenSSL::ASN1::Integer.new(1)
|
|
|
|
* # Explicitly 0-tagged implies context-specific tag class
|
|
|
|
* serial = OpenSSL::ASN1::Integer.new(12345, 0, :EXPLICIT, :CONTEXT_SPECIFIC)
|
|
|
|
* name = OpenSSL::ASN1::PrintableString.new('Data 1')
|
|
|
|
* sequence = OpenSSL::ASN1::Sequence.new( [ version, serial, name ] )
|
|
|
|
* der = sequence.to_der
|
2011-05-16 00:15:38 +04:00
|
|
|
*/
|
2003-10-03 12:58:01 +04:00
|
|
|
mASN1 = rb_define_module_under(mOSSL, "ASN1");
|
2011-05-16 16:20:53 +04:00
|
|
|
|
2011-05-16 00:15:38 +04:00
|
|
|
/* Document-class: OpenSSL::ASN1::ASN1Error
|
|
|
|
*
|
|
|
|
* Generic error class for all errors raised in ASN1 and any of the
|
2011-05-16 16:20:53 +04:00
|
|
|
* classes defined in it.
|
2011-05-16 00:15:38 +04:00
|
|
|
*/
|
2003-10-03 12:58:01 +04:00
|
|
|
eASN1Error = rb_define_class_under(mASN1, "ASN1Error", eOSSLError);
|
|
|
|
rb_define_module_function(mASN1, "traverse", ossl_asn1_traverse, 1);
|
|
|
|
rb_define_module_function(mASN1, "decode", ossl_asn1_decode, 1);
|
|
|
|
rb_define_module_function(mASN1, "decode_all", ossl_asn1_decode_all, 1);
|
|
|
|
ary = rb_ary_new();
|
2011-05-16 16:20:53 +04:00
|
|
|
|
2011-05-16 00:15:38 +04:00
|
|
|
/*
|
|
|
|
* Array storing tag names at the tag's index.
|
|
|
|
*/
|
2003-10-03 12:58:01 +04:00
|
|
|
rb_define_const(mASN1, "UNIVERSAL_TAG_NAME", ary);
|
|
|
|
for(i = 0; i < ossl_asn1_info_size; i++){
|
|
|
|
if(ossl_asn1_info[i].name[0] == '[') continue;
|
|
|
|
rb_define_const(mASN1, ossl_asn1_info[i].name, INT2NUM(i));
|
|
|
|
rb_ary_store(ary, i, rb_str_new2(ossl_asn1_info[i].name));
|
|
|
|
}
|
|
|
|
|
2011-05-16 00:15:38 +04:00
|
|
|
/* Document-class: OpenSSL::ASN1::ASN1Data
|
|
|
|
*
|
|
|
|
* The top-level class representing any ASN.1 object. When parsed by
|
2011-05-17 01:51:12 +04:00
|
|
|
* ASN1.decode, tagged values are always represented by an instance
|
2011-05-16 00:15:38 +04:00
|
|
|
* of ASN1Data.
|
|
|
|
*
|
|
|
|
* == The role of ASN1Data for parsing tagged values
|
|
|
|
*
|
|
|
|
* When encoding an ASN.1 type it is inherently clear what original
|
2011-05-17 01:51:12 +04:00
|
|
|
* type (e.g. INTEGER, OCTET STRING etc.) this value has, regardless
|
|
|
|
* of its tagging.
|
|
|
|
* But opposed to the time an ASN.1 type is to be encoded, when parsing
|
|
|
|
* them it is not possible to deduce the "real type" of tagged
|
2011-05-16 00:15:38 +04:00
|
|
|
* values. This is why tagged values are generally parsed into ASN1Data
|
|
|
|
* instances, but with a different outcome for implicit and explicit
|
|
|
|
* tagging.
|
2011-05-17 01:51:12 +04:00
|
|
|
*
|
2011-05-16 00:15:38 +04:00
|
|
|
* === Example of a parsed implicitly tagged value
|
|
|
|
*
|
2011-05-16 16:20:53 +04:00
|
|
|
* An implicitly 1-tagged INTEGER value will be parsed as an
|
2011-05-16 00:15:38 +04:00
|
|
|
* ASN1Data with
|
|
|
|
* * +tag+ equal to 1
|
|
|
|
* * +tag_class+ equal to +:CONTEXT_SPECIFIC+
|
|
|
|
* * +value+ equal to a +String+ that carries the raw encoding
|
|
|
|
* of the INTEGER.
|
|
|
|
* This implies that a subsequent decoding step is required to
|
2011-05-16 16:20:53 +04:00
|
|
|
* completely decode implicitly tagged values.
|
2011-05-16 00:15:38 +04:00
|
|
|
*
|
|
|
|
* === Example of a parsed explicitly tagged value
|
|
|
|
*
|
|
|
|
* An explicitly 1-tagged INTEGER value will be parsed as an
|
|
|
|
* ASN1Data with
|
|
|
|
* * +tag+ equal to 1
|
|
|
|
* * +tag_class+ equal to +:CONTEXT_SPECIFIC+
|
|
|
|
* * +value+ equal to an +Array+ with one single element, an
|
|
|
|
* instance of OpenSSL::ASN1::Integer, i.e. the inner element
|
|
|
|
* is the non-tagged primitive value, and the tagging is represented
|
|
|
|
* in the outer ASN1Data
|
|
|
|
*
|
|
|
|
* == Example - Decoding an implicitly tagged INTEGER
|
2011-05-17 01:51:12 +04:00
|
|
|
* int = OpenSSL::ASN1::Integer.new(1, 0, :IMPLICIT) # implicit 0-tagged
|
|
|
|
* seq = OpenSSL::ASN1::Sequence.new( [int] )
|
|
|
|
* der = seq.to_der
|
|
|
|
* asn1 = OpenSSL::ASN1.decode(der)
|
|
|
|
* # pp asn1 => #<OpenSSL::ASN1::Sequence:0x87326e0
|
|
|
|
* # @infinite_length=false,
|
|
|
|
* # @tag=16,
|
|
|
|
* # @tag_class=:UNIVERSAL,
|
|
|
|
* # @tagging=nil,
|
|
|
|
* # @value=
|
|
|
|
* # [#<OpenSSL::ASN1::ASN1Data:0x87326f4
|
|
|
|
* # @infinite_length=false,
|
|
|
|
* # @tag=0,
|
|
|
|
* # @tag_class=:CONTEXT_SPECIFIC,
|
|
|
|
* # @value="\x01">]>
|
|
|
|
* raw_int = asn1.value[0]
|
|
|
|
* # manually rewrite tag and tag class to make it an UNIVERSAL value
|
|
|
|
* raw_int.tag = OpenSSL::ASN1::INTEGER
|
|
|
|
* raw_int.tag_class = :UNIVERSAL
|
|
|
|
* int2 = OpenSSL::ASN1.decode(raw_int)
|
|
|
|
* puts int2.value # => 1
|
2011-05-16 00:15:38 +04:00
|
|
|
*
|
|
|
|
* == Example - Decoding an explicitly tagged INTEGER
|
2011-05-17 01:51:12 +04:00
|
|
|
* int = OpenSSL::ASN1::Integer.new(1, 0, :EXPLICIT) # explicit 0-tagged
|
|
|
|
* seq = OpenSSL::ASN1::Sequence.new( [int] )
|
|
|
|
* der = seq.to_der
|
|
|
|
* asn1 = OpenSSL::ASN1.decode(der)
|
|
|
|
* # pp asn1 => #<OpenSSL::ASN1::Sequence:0x87326e0
|
|
|
|
* # @infinite_length=false,
|
|
|
|
* # @tag=16,
|
|
|
|
* # @tag_class=:UNIVERSAL,
|
|
|
|
* # @tagging=nil,
|
|
|
|
* # @value=
|
|
|
|
* # [#<OpenSSL::ASN1::ASN1Data:0x87326f4
|
|
|
|
* # @infinite_length=false,
|
|
|
|
* # @tag=0,
|
|
|
|
* # @tag_class=:CONTEXT_SPECIFIC,
|
|
|
|
* # @value=
|
|
|
|
* # [#<OpenSSL::ASN1::Integer:0x85bf308
|
|
|
|
* # @infinite_length=false,
|
|
|
|
* # @tag=2,
|
|
|
|
* # @tag_class=:UNIVERSAL
|
|
|
|
* # @tagging=nil,
|
|
|
|
* # @value=1>]>]>
|
|
|
|
* int2 = asn1.value[0].value[0]
|
|
|
|
* puts int2.value # => 1
|
2011-05-16 00:15:38 +04:00
|
|
|
*/
|
2003-10-03 12:58:01 +04:00
|
|
|
cASN1Data = rb_define_class_under(mASN1, "ASN1Data", rb_cObject);
|
2011-05-17 01:51:12 +04:00
|
|
|
/*
|
|
|
|
* Carries the value of a ASN.1 type.
|
|
|
|
* Please confer Constructive and Primitive for the mappings between
|
|
|
|
* ASN.1 data types and Ruby classes.
|
|
|
|
*/
|
2007-01-04 11:05:08 +03:00
|
|
|
rb_attr(cASN1Data, rb_intern("value"), 1, 1, 0);
|
2011-05-17 01:51:12 +04:00
|
|
|
/*
|
|
|
|
* A +Number+ representing the tag number of this ASN1Data. Never +nil+.
|
|
|
|
*/
|
2007-01-04 11:05:08 +03:00
|
|
|
rb_attr(cASN1Data, rb_intern("tag"), 1, 1, 0);
|
2011-05-17 01:51:12 +04:00
|
|
|
/*
|
|
|
|
* A +Symbol+ representing the tag class of this ASN1Data. Never +nil+.
|
|
|
|
* See ASN1Data for possible values.
|
|
|
|
*/
|
2007-01-04 11:05:08 +03:00
|
|
|
rb_attr(cASN1Data, rb_intern("tag_class"), 1, 1, 0);
|
2011-05-17 01:51:12 +04:00
|
|
|
/*
|
|
|
|
* Never +nil+. A +Boolean+ indicating whether the encoding was infinite
|
|
|
|
* length (in the case of parsing) or whether an infinite length encoding
|
|
|
|
* shall be used (in the encoding case).
|
|
|
|
* In DER, every value has a finite length associated with it. But in
|
|
|
|
* scenarios where large amounts of data need to be transferred it
|
|
|
|
* might be desirable to have some kind of streaming support available.
|
|
|
|
* For example, huge OCTET STRINGs are preferably sent in smaller-sized
|
|
|
|
* chunks, each at a time.
|
|
|
|
* This is possible in BER by setting the length bytes of an encoding
|
|
|
|
* to zero and by this indicating that the following value will be
|
|
|
|
* sent in chunks. Infinite length encodings are always constructed.
|
|
|
|
* The end of such a stream of chunks is indicated by sending a EOC
|
|
|
|
* (End of Content) tag. SETs and SEQUENCEs may use an infinite length
|
|
|
|
* encoding, but also primitive types such as e.g. OCTET STRINGS or
|
|
|
|
* BIT STRINGS may leverage this functionality (cf. ITU-T X.690).
|
|
|
|
*/
|
2010-12-11 20:45:42 +03:00
|
|
|
rb_attr(cASN1Data, rb_intern("infinite_length"), 1, 1, 0);
|
2003-10-03 12:58:01 +04:00
|
|
|
rb_define_method(cASN1Data, "initialize", ossl_asn1data_initialize, 3);
|
|
|
|
rb_define_method(cASN1Data, "to_der", ossl_asn1data_to_der, 0);
|
|
|
|
|
2011-05-16 00:15:38 +04:00
|
|
|
/* Document-class: OpenSSL::ASN1::Primitive
|
|
|
|
*
|
|
|
|
* The parent class for all primitive encodings. Attributes are the same as
|
2011-05-19 04:07:25 +04:00
|
|
|
* for ASN1Data, with the addition of +tagging+.
|
2011-05-17 01:51:12 +04:00
|
|
|
* Primitive values can never be infinite length encodings, thus it is not
|
2011-05-16 00:15:38 +04:00
|
|
|
* possible to set the +infinite_length+ attribute for Primitive and its
|
|
|
|
* sub-classes.
|
|
|
|
*
|
|
|
|
* == Primitive sub-classes and their mapping to Ruby classes
|
|
|
|
* * OpenSSL::ASN1::EndOfContent <=> +value+ is always +nil+
|
|
|
|
* * OpenSSL::ASN1::Boolean <=> +value+ is a +Boolean+
|
|
|
|
* * OpenSSL::ASN1::Integer <=> +value+ is a +Number+
|
|
|
|
* * OpenSSL::ASN1::BitString <=> +value+ is a +String+
|
|
|
|
* * OpenSSL::ASN1::OctetString <=> +value+ is a +String+
|
|
|
|
* * OpenSSL::ASN1::Null <=> +value+ is always +nil+
|
|
|
|
* * OpenSSL::ASN1::Object <=> +value+ is a +String+
|
|
|
|
* * OpenSSL::ASN1::Enumerated <=> +value+ is a +Number+
|
|
|
|
* * OpenSSL::ASN1::UTF8String <=> +value+ is a +String+
|
|
|
|
* * OpenSSL::ASN1::NumericString <=> +value+ is a +String+
|
|
|
|
* * OpenSSL::ASN1::PrintableString <=> +value+ is a +String+
|
|
|
|
* * OpenSSL::ASN1::T61String <=> +value+ is a +String+
|
|
|
|
* * OpenSSL::ASN1::VideotexString <=> +value+ is a +String+
|
|
|
|
* * OpenSSL::ASN1::IA5String <=> +value+ is a +String+
|
|
|
|
* * OpenSSL::ASN1::UTCTime <=> +value+ is a +Time+
|
|
|
|
* * OpenSSL::ASN1::GeneralizedTime <=> +value+ is a +Time+
|
|
|
|
* * OpenSSL::ASN1::GraphicString <=> +value+ is a +String+
|
|
|
|
* * OpenSSL::ASN1::ISO64String <=> +value+ is a +String+
|
|
|
|
* * OpenSSL::ASN1::GeneralString <=> +value+ is a +String+
|
|
|
|
* * OpenSSL::ASN1::UniversalString <=> +value+ is a +String+
|
|
|
|
* * OpenSSL::ASN1::BMPString <=> +value+ is a +String+
|
|
|
|
*
|
|
|
|
* == OpenSSL::ASN1::BitString
|
|
|
|
*
|
|
|
|
* === Additional attributes
|
|
|
|
* +unused_bits+: if the underlying BIT STRING's
|
2011-05-16 16:20:53 +04:00
|
|
|
* length is a multiple of 8 then +unused_bits+ is 0. Otherwise
|
2011-05-16 00:15:38 +04:00
|
|
|
* +unused_bits+ indicates the number of bits that are to be ignored in
|
|
|
|
* the final octet of the +BitString+'s +value+.
|
|
|
|
*
|
|
|
|
* == OpenSSL::ASN1::ObjectId
|
|
|
|
*
|
|
|
|
* === Additional attributes
|
|
|
|
* * +sn+: the short name as defined in <openssl/objects.h>.
|
|
|
|
* * +ln+: the long name as defined in <openssl/objects.h>.
|
|
|
|
* * +oid+: the object identifier as a +String+, e.g. "1.2.3.4.5"
|
|
|
|
* * +short_name+: alias for +sn+.
|
|
|
|
* * +long_name+: alias for +ln+.
|
|
|
|
*
|
|
|
|
* == Examples
|
|
|
|
* With the Exception of OpenSSL::ASN1::EndOfContent, each Primitive class
|
2011-05-17 01:51:12 +04:00
|
|
|
* constructor takes at least one parameter, the +value+.
|
2011-05-16 00:15:38 +04:00
|
|
|
*
|
|
|
|
* === Creating EndOfContent
|
2011-05-17 01:51:12 +04:00
|
|
|
* eoc = OpenSSL::ASN1::EndOfContent.new
|
2011-05-16 00:15:38 +04:00
|
|
|
*
|
|
|
|
* === Creating any other Primitive
|
2011-05-17 01:51:12 +04:00
|
|
|
* prim = <class>.new(value) # <class> being one of the sub-classes except EndOfContent
|
|
|
|
* prim_zero_tagged_implicit = <class>.new(value, 0, :IMPLICIT)
|
|
|
|
* prim_zero_tagged_explicit = <class>.new(value, 0, :EXPLICIT)
|
2011-05-16 00:15:38 +04:00
|
|
|
*/
|
2003-10-03 12:58:01 +04:00
|
|
|
cASN1Primitive = rb_define_class_under(mASN1, "Primitive", cASN1Data);
|
2011-05-17 01:51:12 +04:00
|
|
|
/*
|
|
|
|
* May be used as a hint for encoding a value either implicitly or
|
|
|
|
* explicitly by setting it either to +:IMPLICIT+ or to +:EXPLICIT+.
|
|
|
|
* +tagging+ is not set when a ASN.1 structure is parsed using
|
|
|
|
* OpenSSL::ASN1.decode.
|
|
|
|
*/
|
2003-10-03 12:58:01 +04:00
|
|
|
rb_attr(cASN1Primitive, rb_intern("tagging"), 1, 1, Qtrue);
|
2010-12-11 20:45:42 +03:00
|
|
|
rb_undef_method(cASN1Primitive, "infinite_length=");
|
2003-10-03 12:58:01 +04:00
|
|
|
rb_define_method(cASN1Primitive, "initialize", ossl_asn1_initialize, -1);
|
|
|
|
rb_define_method(cASN1Primitive, "to_der", ossl_asn1prim_to_der, 0);
|
|
|
|
|
2011-05-16 00:15:38 +04:00
|
|
|
/* Document-class: OpenSSL::ASN1::Constructive
|
|
|
|
*
|
|
|
|
* The parent class for all constructed encodings. The +value+ attribute
|
|
|
|
* of a Constructive is always an +Array+. Attributes are the same as
|
2011-05-17 01:51:12 +04:00
|
|
|
* for ASN1Data, with the addition of +tagging+.
|
2011-05-16 00:15:38 +04:00
|
|
|
*
|
|
|
|
* == SET and SEQUENCE
|
|
|
|
*
|
|
|
|
* Most constructed encodings come in the form of a SET or a SEQUENCE.
|
|
|
|
* These encodings are represented by one of the two sub-classes of
|
|
|
|
* Constructive:
|
|
|
|
* * OpenSSL::ASN1::Set
|
|
|
|
* * OpenSSL::ASN1::Sequence
|
|
|
|
* Please note that tagged sequences and sets are still parsed as
|
|
|
|
* instances of ASN1Data. Find further details on tagged values
|
|
|
|
* there.
|
|
|
|
*
|
|
|
|
* === Example - constructing a SEQUENCE
|
2011-05-17 01:51:12 +04:00
|
|
|
* int = OpenSSL::ASN1::Integer.new(1)
|
|
|
|
* str = OpenSSL::ASN1::PrintableString.new('abc')
|
|
|
|
* sequence = OpenSSL::ASN1::Sequence.new( [ int, str ] )
|
2011-05-16 00:15:38 +04:00
|
|
|
*
|
|
|
|
* === Example - constructing a SET
|
2011-05-17 01:51:12 +04:00
|
|
|
* int = OpenSSL::ASN1::Integer.new(1)
|
|
|
|
* str = OpenSSL::ASN1::PrintableString.new('abc')
|
|
|
|
* set = OpenSSL::ASN1::Set.new( [ int, str ] )
|
2011-05-16 00:15:38 +04:00
|
|
|
*
|
|
|
|
* == Infinite length primitive values
|
|
|
|
*
|
2011-05-16 16:20:53 +04:00
|
|
|
* The only case where Constructive is used directly is for infinite
|
2011-05-16 00:15:38 +04:00
|
|
|
* length encodings of primitive values. These encodings are always
|
|
|
|
* constructed, with the contents of the +value+ +Array+ being either
|
|
|
|
* UNIVERSAL non-infinite length partial encodings of the actual value
|
|
|
|
* or again constructive encodings with infinite length (i.e. infinite
|
|
|
|
* length primitive encodings may be constructed recursively with another
|
|
|
|
* infinite length value within an already infinite length value). Each
|
|
|
|
* partial encoding must be of the same UNIVERSAL type as the overall
|
2011-05-16 16:20:53 +04:00
|
|
|
* encoding. The value of the overall encoding consists of the
|
2011-05-16 00:15:38 +04:00
|
|
|
* concatenation of each partial encoding taken in sequence. The +value+
|
2011-05-16 16:20:53 +04:00
|
|
|
* array of the outer infinite length value must end with a
|
2011-05-16 00:15:38 +04:00
|
|
|
* OpenSSL::ASN1::EndOfContent instance.
|
|
|
|
*
|
2011-05-23 01:34:28 +04:00
|
|
|
* Please note that it is not possible to encode Constructive without
|
|
|
|
* the +infinite_length+ attribute being set to +true+, use
|
|
|
|
* OpenSSL::ASN1::Sequence or OpenSSL::ASN1::Set in these cases instead.
|
|
|
|
*
|
2011-05-16 00:15:38 +04:00
|
|
|
* === Example - Infinite length OCTET STRING
|
2011-05-17 01:51:12 +04:00
|
|
|
* partial1 = OpenSSL::ASN1::OctetString.new("\x01")
|
|
|
|
* partial2 = OpenSSL::ASN1::OctetString.new("\x02")
|
|
|
|
* inf_octets = OpenSSL::ASN1::Constructive.new( [ partial1,
|
|
|
|
* partial2,
|
|
|
|
* OpenSSL::ASN1::EndOfContent.new ],
|
|
|
|
* OpenSSL::ASN1::OCTET_STRING,
|
|
|
|
* nil,
|
|
|
|
* :UNIVERSAL )
|
|
|
|
* # The real value of inf_octets is "\x01\x02", i.e. the concatenation
|
|
|
|
* # of partial1 and partial2
|
|
|
|
* inf_octets.infinite_length = true
|
|
|
|
* der = inf_octets.to_der
|
|
|
|
* asn1 = OpenSSL::ASN1.decode(der)
|
|
|
|
* puts asn1.infinite_length # => true
|
2011-05-16 00:15:38 +04:00
|
|
|
*/
|
2003-10-03 12:58:01 +04:00
|
|
|
cASN1Constructive = rb_define_class_under(mASN1,"Constructive", cASN1Data);
|
|
|
|
rb_include_module(cASN1Constructive, rb_mEnumerable);
|
2011-05-17 01:51:12 +04:00
|
|
|
/*
|
|
|
|
* May be used as a hint for encoding a value either implicitly or
|
|
|
|
* explicitly by setting it either to +:IMPLICIT+ or to +:EXPLICIT+.
|
|
|
|
* +tagging+ is not set when a ASN.1 structure is parsed using
|
|
|
|
* OpenSSL::ASN1.decode.
|
|
|
|
*/
|
2003-10-03 12:58:01 +04:00
|
|
|
rb_attr(cASN1Constructive, rb_intern("tagging"), 1, 1, Qtrue);
|
|
|
|
rb_define_method(cASN1Constructive, "initialize", ossl_asn1_initialize, -1);
|
|
|
|
rb_define_method(cASN1Constructive, "to_der", ossl_asn1cons_to_der, 0);
|
|
|
|
rb_define_method(cASN1Constructive, "each", ossl_asn1cons_each, 0);
|
|
|
|
|
|
|
|
#define OSSL_ASN1_DEFINE_CLASS(name, super) \
|
|
|
|
do{\
|
|
|
|
cASN1##name = rb_define_class_under(mASN1, #name, cASN1##super);\
|
|
|
|
rb_define_module_function(mASN1, #name, ossl_asn1_##name, -1);\
|
|
|
|
}while(0)
|
|
|
|
|
|
|
|
OSSL_ASN1_DEFINE_CLASS(Boolean, Primitive);
|
|
|
|
OSSL_ASN1_DEFINE_CLASS(Integer, Primitive);
|
|
|
|
OSSL_ASN1_DEFINE_CLASS(Enumerated, Primitive);
|
|
|
|
OSSL_ASN1_DEFINE_CLASS(BitString, Primitive);
|
|
|
|
OSSL_ASN1_DEFINE_CLASS(OctetString, Primitive);
|
|
|
|
OSSL_ASN1_DEFINE_CLASS(UTF8String, Primitive);
|
|
|
|
OSSL_ASN1_DEFINE_CLASS(NumericString, Primitive);
|
|
|
|
OSSL_ASN1_DEFINE_CLASS(PrintableString, Primitive);
|
|
|
|
OSSL_ASN1_DEFINE_CLASS(T61String, Primitive);
|
|
|
|
OSSL_ASN1_DEFINE_CLASS(VideotexString, Primitive);
|
|
|
|
OSSL_ASN1_DEFINE_CLASS(IA5String, Primitive);
|
|
|
|
OSSL_ASN1_DEFINE_CLASS(GraphicString, Primitive);
|
|
|
|
OSSL_ASN1_DEFINE_CLASS(ISO64String, Primitive);
|
|
|
|
OSSL_ASN1_DEFINE_CLASS(GeneralString, Primitive);
|
|
|
|
OSSL_ASN1_DEFINE_CLASS(UniversalString, Primitive);
|
|
|
|
OSSL_ASN1_DEFINE_CLASS(BMPString, Primitive);
|
|
|
|
OSSL_ASN1_DEFINE_CLASS(Null, Primitive);
|
|
|
|
OSSL_ASN1_DEFINE_CLASS(ObjectId, Primitive);
|
|
|
|
OSSL_ASN1_DEFINE_CLASS(UTCTime, Primitive);
|
|
|
|
OSSL_ASN1_DEFINE_CLASS(GeneralizedTime, Primitive);
|
|
|
|
|
|
|
|
OSSL_ASN1_DEFINE_CLASS(Sequence, Constructive);
|
|
|
|
OSSL_ASN1_DEFINE_CLASS(Set, Constructive);
|
|
|
|
|
2010-12-11 20:45:42 +03:00
|
|
|
OSSL_ASN1_DEFINE_CLASS(EndOfContent, Data);
|
|
|
|
|
2003-10-03 12:58:01 +04:00
|
|
|
rb_define_singleton_method(cASN1ObjectId, "register", ossl_asn1obj_s_register, 3);
|
2003-10-06 20:55:00 +04:00
|
|
|
rb_define_method(cASN1ObjectId, "sn", ossl_asn1obj_get_sn, 0);
|
|
|
|
rb_define_method(cASN1ObjectId, "ln", ossl_asn1obj_get_ln, 0);
|
|
|
|
rb_define_method(cASN1ObjectId, "oid", ossl_asn1obj_get_oid, 0);
|
|
|
|
rb_define_alias(cASN1ObjectId, "short_name", "sn");
|
|
|
|
rb_define_alias(cASN1ObjectId, "long_name", "ln");
|
2007-01-04 11:05:08 +03:00
|
|
|
rb_attr(cASN1BitString, rb_intern("unused_bits"), 1, 1, 0);
|
2010-12-11 20:45:42 +03:00
|
|
|
|
|
|
|
rb_define_method(cASN1EndOfContent, "initialize", ossl_asn1eoc_initialize, 0);
|
2011-05-22 04:01:06 +04:00
|
|
|
|
|
|
|
class_tag_map = rb_hash_new();
|
2011-05-22 16:58:23 +04:00
|
|
|
rb_hash_aset(class_tag_map, cASN1EndOfContent, INT2NUM(V_ASN1_EOC));
|
|
|
|
rb_hash_aset(class_tag_map, cASN1Boolean, INT2NUM(V_ASN1_BOOLEAN));
|
|
|
|
rb_hash_aset(class_tag_map, cASN1Integer, INT2NUM(V_ASN1_INTEGER));
|
|
|
|
rb_hash_aset(class_tag_map, cASN1BitString, INT2NUM(V_ASN1_BIT_STRING));
|
|
|
|
rb_hash_aset(class_tag_map, cASN1OctetString, INT2NUM(V_ASN1_OCTET_STRING));
|
|
|
|
rb_hash_aset(class_tag_map, cASN1Null, INT2NUM(V_ASN1_NULL));
|
|
|
|
rb_hash_aset(class_tag_map, cASN1ObjectId, INT2NUM(V_ASN1_OBJECT));
|
|
|
|
rb_hash_aset(class_tag_map, cASN1Enumerated, INT2NUM(V_ASN1_ENUMERATED));
|
|
|
|
rb_hash_aset(class_tag_map, cASN1UTF8String, INT2NUM(V_ASN1_UTF8STRING));
|
|
|
|
rb_hash_aset(class_tag_map, cASN1Sequence, INT2NUM(V_ASN1_SEQUENCE));
|
|
|
|
rb_hash_aset(class_tag_map, cASN1Set, INT2NUM(V_ASN1_SET));
|
|
|
|
rb_hash_aset(class_tag_map, cASN1NumericString, INT2NUM(V_ASN1_NUMERICSTRING));
|
|
|
|
rb_hash_aset(class_tag_map, cASN1PrintableString, INT2NUM(V_ASN1_PRINTABLESTRING));
|
|
|
|
rb_hash_aset(class_tag_map, cASN1T61String, INT2NUM(V_ASN1_T61STRING));
|
|
|
|
rb_hash_aset(class_tag_map, cASN1VideotexString, INT2NUM(V_ASN1_VIDEOTEXSTRING));
|
|
|
|
rb_hash_aset(class_tag_map, cASN1IA5String, INT2NUM(V_ASN1_IA5STRING));
|
|
|
|
rb_hash_aset(class_tag_map, cASN1UTCTime, INT2NUM(V_ASN1_UTCTIME));
|
|
|
|
rb_hash_aset(class_tag_map, cASN1GeneralizedTime, INT2NUM(V_ASN1_GENERALIZEDTIME));
|
|
|
|
rb_hash_aset(class_tag_map, cASN1GraphicString, INT2NUM(V_ASN1_GRAPHICSTRING));
|
|
|
|
rb_hash_aset(class_tag_map, cASN1ISO64String, INT2NUM(V_ASN1_ISO64STRING));
|
|
|
|
rb_hash_aset(class_tag_map, cASN1GeneralString, INT2NUM(INT2NUM(V_ASN1_GENERALSTRING)));
|
|
|
|
rb_hash_aset(class_tag_map, cASN1UniversalString, INT2NUM(V_ASN1_UNIVERSALSTRING));
|
|
|
|
rb_hash_aset(class_tag_map, cASN1BMPString, INT2NUM(V_ASN1_BMPSTRING));
|
2011-05-22 04:01:06 +04:00
|
|
|
rb_global_variable(&class_tag_map);
|
2003-10-03 12:58:01 +04:00
|
|
|
}
|