* io.c (rb_open_file): encoding in mode string was ignored if perm is
[ruby-svn.git] / ext / openssl / ossl_asn1.c
blobe6d09fbca1deac41c15fe8457bc312c8c6606c64
1 /*
2 * $Id$
3 * 'OpenSSL for Ruby' team members
4 * Copyright (C) 2003
5 * All rights reserved.
6 */
7 /*
8 * This program is licenced under the same licence as Ruby.
9 * (See the file 'LICENCE'.)
11 #include "ossl.h"
13 #if defined(HAVE_SYS_TIME_H)
14 # include <sys/time.h>
15 #elif !defined(NT) && !defined(_WIN32)
16 struct timeval {
17 long tv_sec; /* seconds */
18 long tv_usec; /* and microseconds */
20 #endif
23 * DATE conversion
25 VALUE
26 asn1time_to_time(ASN1_TIME *time)
28 struct tm tm;
29 VALUE argv[6];
31 if (!time || !time->data) return Qnil;
32 memset(&tm, 0, sizeof(struct tm));
34 switch (time->type) {
35 case V_ASN1_UTCTIME:
36 if (sscanf((const char *)time->data, "%2d%2d%2d%2d%2d%2dZ", &tm.tm_year, &tm.tm_mon,
37 &tm.tm_mday, &tm.tm_hour, &tm.tm_min, &tm.tm_sec) != 6) {
38 ossl_raise(rb_eTypeError, "bad UTCTIME format");
40 if (tm.tm_year < 69) {
41 tm.tm_year += 2000;
42 } else {
43 tm.tm_year += 1900;
45 break;
46 case V_ASN1_GENERALIZEDTIME:
47 if (sscanf((const char *)time->data, "%4d%2d%2d%2d%2d%2dZ", &tm.tm_year, &tm.tm_mon,
48 &tm.tm_mday, &tm.tm_hour, &tm.tm_min, &tm.tm_sec) != 6) {
49 ossl_raise(rb_eTypeError, "bad GENERALIZEDTIME format" );
51 break;
52 default:
53 rb_warning("unknown time format");
54 return Qnil;
56 argv[0] = INT2NUM(tm.tm_year);
57 argv[1] = INT2NUM(tm.tm_mon);
58 argv[2] = INT2NUM(tm.tm_mday);
59 argv[3] = INT2NUM(tm.tm_hour);
60 argv[4] = INT2NUM(tm.tm_min);
61 argv[5] = INT2NUM(tm.tm_sec);
63 return rb_funcall2(rb_cTime, rb_intern("utc"), 6, argv);
67 * This function is not exported in Ruby's *.h
69 extern struct timeval rb_time_timeval(VALUE);
71 time_t
72 time_to_time_t(VALUE time)
74 return (time_t)NUM2LONG(rb_Integer(time));
78 * STRING conversion
80 VALUE
81 asn1str_to_str(ASN1_STRING *str)
83 return rb_str_new((const char *)str->data, str->length);
87 * ASN1_INTEGER conversions
88 * TODO: Make a decision what's the right way to do this.
90 #define DO_IT_VIA_RUBY 0
91 VALUE
92 asn1integer_to_num(ASN1_INTEGER *ai)
94 BIGNUM *bn;
95 #if DO_IT_VIA_RUBY
96 char *txt;
97 #endif
98 VALUE num;
100 if (!ai) {
101 ossl_raise(rb_eTypeError, "ASN1_INTEGER is NULL!");
103 if (!(bn = ASN1_INTEGER_to_BN(ai, NULL))) {
104 ossl_raise(eOSSLError, NULL);
106 #if DO_IT_VIA_RUBY
107 if (!(txt = BN_bn2dec(bn))) {
108 BN_free(bn);
109 ossl_raise(eOSSLError, NULL);
111 num = rb_cstr_to_inum(txt, 10, Qtrue);
112 OPENSSL_free(txt);
113 #else
114 num = ossl_bn_new(bn);
115 #endif
116 BN_free(bn);
118 return num;
121 #if DO_IT_VIA_RUBY
122 ASN1_INTEGER *
123 num_to_asn1integer(VALUE obj, ASN1_INTEGER *ai)
125 BIGNUM *bn = NULL;
127 if (RTEST(rb_obj_is_kind_of(obj, cBN))) {
128 bn = GetBNPtr(obj);
129 } else {
130 obj = rb_String(obj);
131 if (!BN_dec2bn(&bn, StringValuePtr(obj))) {
132 ossl_raise(eOSSLError, NULL);
135 if (!(ai = BN_to_ASN1_INTEGER(bn, ai))) {
136 BN_free(bn);
137 ossl_raise(eOSSLError, NULL);
139 BN_free(bn);
140 return ai;
142 #else
143 ASN1_INTEGER *
144 num_to_asn1integer(VALUE obj, ASN1_INTEGER *ai)
146 BIGNUM *bn = GetBNPtr(obj);
148 if (!(ai = BN_to_ASN1_INTEGER(bn, ai))) {
149 ossl_raise(eOSSLError, NULL);
151 return ai;
153 #endif
155 /********/
157 * ASN1 module
159 #define ossl_asn1_get_value(o) rb_attr_get((o),rb_intern("@value"))
160 #define ossl_asn1_get_tag(o) rb_attr_get((o),rb_intern("@tag"))
161 #define ossl_asn1_get_tagging(o) rb_attr_get((o),rb_intern("@tagging"))
162 #define ossl_asn1_get_tag_class(o) rb_attr_get((o),rb_intern("@tag_class"))
164 #define ossl_asn1_set_value(o,v) rb_iv_set((o),"@value",(v))
165 #define ossl_asn1_set_tag(o,v) rb_iv_set((o),"@tag",(v))
166 #define ossl_asn1_set_tagging(o,v) rb_iv_set((o),"@tagging",(v))
167 #define ossl_asn1_set_tag_class(o,v) rb_iv_set((o),"@tag_class",(v))
169 VALUE mASN1;
170 VALUE eASN1Error;
172 VALUE cASN1Data;
173 VALUE cASN1Primitive;
174 VALUE cASN1Constructive;
176 VALUE cASN1Boolean; /* BOOLEAN */
177 VALUE cASN1Integer, cASN1Enumerated; /* INTEGER */
178 VALUE cASN1BitString; /* BIT STRING */
179 VALUE cASN1OctetString, cASN1UTF8String; /* STRINGs */
180 VALUE cASN1NumericString, cASN1PrintableString;
181 VALUE cASN1T61String, cASN1VideotexString;
182 VALUE cASN1IA5String, cASN1GraphicString;
183 VALUE cASN1ISO64String, cASN1GeneralString;
184 VALUE cASN1UniversalString, cASN1BMPString;
185 VALUE cASN1Null; /* NULL */
186 VALUE cASN1ObjectId; /* OBJECT IDENTIFIER */
187 VALUE cASN1UTCTime, cASN1GeneralizedTime; /* TIME */
188 VALUE cASN1Sequence, cASN1Set; /* CONSTRUCTIVE */
190 static ID sIMPLICIT, sEXPLICIT;
191 static ID sUNIVERSAL, sAPPLICATION, sCONTEXT_SPECIFIC, sPRIVATE;
194 * Ruby to ASN1 converters
196 static ASN1_BOOLEAN
197 obj_to_asn1bool(VALUE obj)
199 return RTEST(obj) ? 0xff : 0x100;
202 static ASN1_INTEGER*
203 obj_to_asn1int(VALUE obj)
205 return num_to_asn1integer(obj, NULL);
208 static ASN1_BIT_STRING*
209 obj_to_asn1bstr(VALUE obj, long unused_bits)
211 ASN1_BIT_STRING *bstr;
213 if(unused_bits < 0) unused_bits = 0;
214 StringValue(obj);
215 if(!(bstr = ASN1_BIT_STRING_new()))
216 ossl_raise(eASN1Error, NULL);
217 ASN1_BIT_STRING_set(bstr, (unsigned char *)RSTRING_PTR(obj), RSTRING_LEN(obj));
218 bstr->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT|0x07); /* clear */
219 bstr->flags |= ASN1_STRING_FLAG_BITS_LEFT|(unused_bits&0x07);
221 return bstr;
224 static ASN1_STRING*
225 obj_to_asn1str(VALUE obj)
227 ASN1_STRING *str;
229 StringValue(obj);
230 if(!(str = ASN1_STRING_new()))
231 ossl_raise(eASN1Error, NULL);
232 ASN1_STRING_set(str, RSTRING_PTR(obj), RSTRING_LEN(obj));
234 return str;
237 static ASN1_NULL*
238 obj_to_asn1null(VALUE obj)
240 ASN1_NULL *null;
242 if(!NIL_P(obj))
243 ossl_raise(eASN1Error, "nil expected");
244 if(!(null = ASN1_NULL_new()))
245 ossl_raise(eASN1Error, NULL);
247 return null;
250 static ASN1_OBJECT*
251 obj_to_asn1obj(VALUE obj)
253 ASN1_OBJECT *a1obj;
255 StringValue(obj);
256 a1obj = OBJ_txt2obj(RSTRING_PTR(obj), 0);
257 if(!a1obj) a1obj = OBJ_txt2obj(RSTRING_PTR(obj), 1);
258 if(!a1obj) ossl_raise(eASN1Error, "invalid OBJECT ID");
260 return a1obj;
263 static ASN1_UTCTIME*
264 obj_to_asn1utime(VALUE time)
266 time_t sec;
267 ASN1_UTCTIME *t;
269 sec = time_to_time_t(time);
270 if(!(t = ASN1_UTCTIME_set(NULL, sec)))
271 ossl_raise(eASN1Error, NULL);
273 return t;
276 static ASN1_GENERALIZEDTIME*
277 obj_to_asn1gtime(VALUE time)
279 time_t sec;
280 ASN1_GENERALIZEDTIME *t;
282 sec = time_to_time_t(time);
283 if(!(t =ASN1_GENERALIZEDTIME_set(NULL, sec)))
284 ossl_raise(eASN1Error, NULL);
286 return t;
289 static ASN1_STRING*
290 obj_to_asn1derstr(VALUE obj)
292 ASN1_STRING *a1str;
293 VALUE str;
295 str = ossl_to_der(obj);
296 if(!(a1str = ASN1_STRING_new()))
297 ossl_raise(eASN1Error, NULL);
298 ASN1_STRING_set(a1str, RSTRING_PTR(str), RSTRING_LEN(str));
300 return a1str;
304 * DER to Ruby converters
306 static VALUE
307 decode_bool(unsigned char* der, int length)
309 int bool;
310 const unsigned char *p;
312 p = der;
313 if((bool = d2i_ASN1_BOOLEAN(NULL, &p, length)) < 0)
314 ossl_raise(eASN1Error, NULL);
316 return bool ? Qtrue : Qfalse;
319 static VALUE
320 decode_int(unsigned char* der, int length)
322 ASN1_INTEGER *ai;
323 const unsigned char *p;
324 VALUE ret;
325 int status = 0;
327 p = der;
328 if(!(ai = d2i_ASN1_INTEGER(NULL, &p, length)))
329 ossl_raise(eASN1Error, NULL);
330 ret = rb_protect((VALUE(*)_((VALUE)))asn1integer_to_num,
331 (VALUE)ai, &status);
332 ASN1_INTEGER_free(ai);
333 if(status) rb_jump_tag(status);
335 return ret;
338 static VALUE
339 decode_bstr(unsigned char* der, int length, long *unused_bits)
341 ASN1_BIT_STRING *bstr;
342 const unsigned char *p;
343 long len;
344 VALUE ret;
346 p = der;
347 if(!(bstr = d2i_ASN1_BIT_STRING(NULL, &p, length)))
348 ossl_raise(eASN1Error, NULL);
349 len = bstr->length;
350 *unused_bits = 0;
351 if(bstr->flags & ASN1_STRING_FLAG_BITS_LEFT)
352 *unused_bits = bstr->flags & 0x07;
353 ret = rb_str_new((const char *)bstr->data, len);
354 ASN1_BIT_STRING_free(bstr);
356 return ret;
359 static VALUE
360 decode_enum(unsigned char* der, int length)
362 ASN1_ENUMERATED *ai;
363 const unsigned char *p;
364 VALUE ret;
365 int status = 0;
367 p = der;
368 if(!(ai = d2i_ASN1_ENUMERATED(NULL, &p, length)))
369 ossl_raise(eASN1Error, NULL);
370 ret = rb_protect((VALUE(*)_((VALUE)))asn1integer_to_num,
371 (VALUE)ai, &status);
372 ASN1_ENUMERATED_free(ai);
373 if(status) rb_jump_tag(status);
375 return ret;
378 static VALUE
379 decode_null(unsigned char* der, int length)
381 ASN1_NULL *null;
382 const unsigned char *p;
384 p = der;
385 if(!(null = d2i_ASN1_NULL(NULL, &p, length)))
386 ossl_raise(eASN1Error, NULL);
387 ASN1_NULL_free(null);
389 return Qnil;
392 static VALUE
393 decode_obj(unsigned char* der, int length)
395 ASN1_OBJECT *obj;
396 const unsigned char *p;
397 VALUE ret;
398 int nid;
399 BIO *bio;
401 p = der;
402 if(!(obj = d2i_ASN1_OBJECT(NULL, &p, length)))
403 ossl_raise(eASN1Error, NULL);
404 if((nid = OBJ_obj2nid(obj)) != NID_undef){
405 ASN1_OBJECT_free(obj);
406 ret = rb_str_new2(OBJ_nid2sn(nid));
408 else{
409 if(!(bio = BIO_new(BIO_s_mem()))){
410 ASN1_OBJECT_free(obj);
411 ossl_raise(eASN1Error, NULL);
413 i2a_ASN1_OBJECT(bio, obj);
414 ASN1_OBJECT_free(obj);
415 ret = ossl_membio2str(bio);
418 return ret;
421 static VALUE
422 decode_time(unsigned char* der, int length)
424 ASN1_TIME *time;
425 const unsigned char *p;
426 VALUE ret;
427 int status = 0;
429 p = der;
430 if(!(time = d2i_ASN1_TIME(NULL, &p, length)))
431 ossl_raise(eASN1Error, NULL);
432 ret = rb_protect((VALUE(*)_((VALUE)))asn1time_to_time,
433 (VALUE)time, &status);
434 ASN1_TIME_free(time);
435 if(status) rb_jump_tag(status);
437 return ret;
440 /********/
442 typedef struct {
443 const char *name;
444 VALUE *klass;
445 } ossl_asn1_info_t;
447 static ossl_asn1_info_t ossl_asn1_info[] = {
448 { "EOC", NULL, }, /* 0 */
449 { "BOOLEAN", &cASN1Boolean, }, /* 1 */
450 { "INTEGER", &cASN1Integer, }, /* 2 */
451 { "BIT_STRING", &cASN1BitString, }, /* 3 */
452 { "OCTET_STRING", &cASN1OctetString, }, /* 4 */
453 { "NULL", &cASN1Null, }, /* 5 */
454 { "OBJECT", &cASN1ObjectId, }, /* 6 */
455 { "OBJECT_DESCRIPTOR", NULL, }, /* 7 */
456 { "EXTERNAL", NULL, }, /* 8 */
457 { "REAL", NULL, }, /* 9 */
458 { "ENUMERATED", &cASN1Enumerated, }, /* 10 */
459 { "EMBEDDED_PDV", NULL, }, /* 11 */
460 { "UTF8STRING", &cASN1UTF8String, }, /* 12 */
461 { "RELATIVE_OID", NULL, }, /* 13 */
462 { "[UNIVERSAL 14]", NULL, }, /* 14 */
463 { "[UNIVERSAL 15]", NULL, }, /* 15 */
464 { "SEQUENCE", &cASN1Sequence, }, /* 16 */
465 { "SET", &cASN1Set, }, /* 17 */
466 { "NUMERICSTRING", &cASN1NumericString, }, /* 18 */
467 { "PRINTABLESTRING", &cASN1PrintableString, }, /* 19 */
468 { "T61STRING", &cASN1T61String, }, /* 20 */
469 { "VIDEOTEXSTRING", &cASN1VideotexString, }, /* 21 */
470 { "IA5STRING", &cASN1IA5String, }, /* 22 */
471 { "UTCTIME", &cASN1UTCTime, }, /* 23 */
472 { "GENERALIZEDTIME", &cASN1GeneralizedTime, }, /* 24 */
473 { "GRAPHICSTRING", &cASN1GraphicString, }, /* 25 */
474 { "ISO64STRING", &cASN1ISO64String, }, /* 26 */
475 { "GENERALSTRING", &cASN1GeneralString, }, /* 27 */
476 { "UNIVERSALSTRING", &cASN1UniversalString, }, /* 28 */
477 { "CHARACTER_STRING", NULL, }, /* 29 */
478 { "BMPSTRING", &cASN1BMPString, }, /* 30 */
481 int ossl_asn1_info_size = (sizeof(ossl_asn1_info)/sizeof(ossl_asn1_info[0]));
483 static int ossl_asn1_default_tag(VALUE obj);
485 ASN1_TYPE*
486 ossl_asn1_get_asn1type(VALUE obj)
488 ASN1_TYPE *ret;
489 VALUE value, rflag;
490 void *ptr;
491 void (*free_func)();
492 long tag, flag;
494 tag = ossl_asn1_default_tag(obj);
495 value = ossl_asn1_get_value(obj);
496 switch(tag){
497 case V_ASN1_BOOLEAN:
498 ptr = (void*)(VALUE)obj_to_asn1bool(value);
499 free_func = NULL;
500 break;
501 case V_ASN1_INTEGER: /* FALLTHROUGH */
502 case V_ASN1_ENUMERATED:
503 ptr = obj_to_asn1int(value);
504 free_func = ASN1_INTEGER_free;
505 break;
506 case V_ASN1_BIT_STRING:
507 rflag = rb_attr_get(obj, rb_intern("@unused_bits"));
508 flag = NIL_P(rflag) ? -1 : NUM2INT(rflag);
509 ptr = obj_to_asn1bstr(value, flag);
510 free_func = ASN1_BIT_STRING_free;
511 break;
512 case V_ASN1_NULL:
513 ptr = obj_to_asn1null(value);
514 free_func = ASN1_NULL_free;
515 break;
516 case V_ASN1_OCTET_STRING: /* FALLTHROUGH */
517 case V_ASN1_UTF8STRING: /* FALLTHROUGH */
518 case V_ASN1_NUMERICSTRING: /* FALLTHROUGH */
519 case V_ASN1_PRINTABLESTRING: /* FALLTHROUGH */
520 case V_ASN1_T61STRING: /* FALLTHROUGH */
521 case V_ASN1_VIDEOTEXSTRING: /* FALLTHROUGH */
522 case V_ASN1_IA5STRING: /* FALLTHROUGH */
523 case V_ASN1_GRAPHICSTRING: /* FALLTHROUGH */
524 case V_ASN1_ISO64STRING: /* FALLTHROUGH */
525 case V_ASN1_GENERALSTRING: /* FALLTHROUGH */
526 case V_ASN1_UNIVERSALSTRING: /* FALLTHROUGH */
527 case V_ASN1_BMPSTRING:
528 ptr = obj_to_asn1str(value);
529 free_func = ASN1_STRING_free;
530 break;
531 case V_ASN1_OBJECT:
532 ptr = obj_to_asn1obj(value);
533 free_func = ASN1_OBJECT_free;
534 break;
535 case V_ASN1_UTCTIME:
536 ptr = obj_to_asn1utime(value);
537 free_func = ASN1_TIME_free;
538 break;
539 case V_ASN1_GENERALIZEDTIME:
540 ptr = obj_to_asn1gtime(value);
541 free_func = ASN1_TIME_free;
542 break;
543 case V_ASN1_SET: /* FALLTHROUGH */
544 case V_ASN1_SEQUENCE:
545 ptr = obj_to_asn1derstr(obj);
546 free_func = ASN1_STRING_free;
547 break;
548 default:
549 ossl_raise(eASN1Error, "unsupported ASN.1 type");
551 if(!(ret = OPENSSL_malloc(sizeof(ASN1_TYPE)))){
552 if(free_func) free_func(ptr);
553 ossl_raise(eASN1Error, "ASN1_TYPE alloc failure");
555 memset(ret, 0, sizeof(ASN1_TYPE));
556 ASN1_TYPE_set(ret, tag, ptr);
558 return ret;
561 static int
562 ossl_asn1_default_tag(VALUE obj)
564 int i;
566 for(i = 0; i < ossl_asn1_info_size; i++){
567 if(ossl_asn1_info[i].klass &&
568 rb_obj_is_kind_of(obj, *ossl_asn1_info[i].klass)){
569 return i;
572 ossl_raise(eASN1Error, "universal tag for %s not found",
573 rb_class2name(CLASS_OF(obj)));
575 return -1; /* dummy */
578 static int
579 ossl_asn1_tag(VALUE obj)
581 VALUE tag;
583 tag = ossl_asn1_get_tag(obj);
584 if(NIL_P(tag))
585 ossl_raise(eASN1Error, "tag number not specified");
587 return NUM2INT(tag);
590 static int
591 ossl_asn1_is_explicit(VALUE obj)
593 VALUE s;
594 int ret = -1;
596 s = ossl_asn1_get_tagging(obj);
597 if(NIL_P(s)) return 0;
598 else if(SYMBOL_P(s)){
599 if (SYM2ID(s) == sIMPLICIT)
600 ret = 0;
601 else if (SYM2ID(s) == sEXPLICIT)
602 ret = 1;
604 if(ret < 0){
605 ossl_raise(eASN1Error, "invalid tag default");
608 return ret;
611 static int
612 ossl_asn1_tag_class(VALUE obj)
614 VALUE s;
615 int ret = -1;
617 s = ossl_asn1_get_tag_class(obj);
618 if(NIL_P(s)) ret = V_ASN1_UNIVERSAL;
619 else if(SYMBOL_P(s)){
620 if (SYM2ID(s) == sUNIVERSAL)
621 ret = V_ASN1_UNIVERSAL;
622 else if (SYM2ID(s) == sAPPLICATION)
623 ret = V_ASN1_APPLICATION;
624 else if (SYM2ID(s) == sCONTEXT_SPECIFIC)
625 ret = V_ASN1_CONTEXT_SPECIFIC;
626 else if (SYM2ID(s) == sPRIVATE)
627 ret = V_ASN1_PRIVATE;
629 if(ret < 0){
630 ossl_raise(eASN1Error, "invalid tag class");
633 return ret;
636 static VALUE
637 ossl_asn1_class2sym(int tc)
639 if((tc & V_ASN1_PRIVATE) == V_ASN1_PRIVATE)
640 return ID2SYM(sPRIVATE);
641 else if((tc & V_ASN1_CONTEXT_SPECIFIC) == V_ASN1_CONTEXT_SPECIFIC)
642 return ID2SYM(sCONTEXT_SPECIFIC);
643 else if((tc & V_ASN1_APPLICATION) == V_ASN1_APPLICATION)
644 return ID2SYM(sAPPLICATION);
645 else
646 return ID2SYM(sUNIVERSAL);
649 static VALUE
650 ossl_asn1data_initialize(VALUE self, VALUE value, VALUE tag, VALUE tag_class)
652 if(!SYMBOL_P(tag_class))
653 ossl_raise(eASN1Error, "invalid tag class");
654 if((SYM2ID(tag_class) == sUNIVERSAL) && NUM2INT(tag) > 31)
655 ossl_raise(eASN1Error, "tag number for Universal too large");
656 ossl_asn1_set_tag(self, tag);
657 ossl_asn1_set_value(self, value);
658 ossl_asn1_set_tag_class(self, tag_class);
660 return self;
663 static VALUE
664 join_der_i(VALUE i, VALUE str)
666 i = ossl_to_der_if_possible(i);
667 StringValue(i);
668 rb_str_append(str, i);
669 return Qnil;
672 static VALUE
673 join_der(VALUE enumerable)
675 VALUE str = rb_str_new(0, 0);
676 rb_block_call(enumerable, rb_intern("each"), 0, 0, join_der_i, str);
677 return str;
680 static VALUE
681 ossl_asn1data_to_der(VALUE self)
683 VALUE value, der;
684 int tag, tag_class, is_cons = 0;
685 long length;
686 unsigned char *p;
688 value = ossl_asn1_get_value(self);
689 if(rb_obj_is_kind_of(value, rb_cArray)){
690 is_cons = 1;
691 value = join_der(value);
693 StringValue(value);
695 tag = ossl_asn1_tag(self);
696 tag_class = ossl_asn1_tag_class(self);
697 if((length = ASN1_object_size(1, RSTRING_LEN(value), tag)) <= 0)
698 ossl_raise(eASN1Error, NULL);
699 der = rb_str_new(0, length);
700 p = (unsigned char *)RSTRING_PTR(der);
701 ASN1_put_object(&p, is_cons, RSTRING_LEN(value), tag, tag_class);
702 memcpy(p, RSTRING_PTR(value), RSTRING_LEN(value));
703 p += RSTRING_LEN(value);
704 ossl_str_adjust(der, p);
706 return der;
709 static VALUE
710 ossl_asn1_decode0(unsigned char **pp, long length, long *offset, long depth,
711 int once, int yield)
713 unsigned char *start, *p;
714 const unsigned char *p0;
715 long len, off = *offset;
716 int hlen, tag, tc, j;
717 VALUE ary, asn1data, value, tag_class;
719 ary = rb_ary_new();
720 p = *pp;
721 while(length > 0){
722 start = p;
723 p0 = p;
724 j = ASN1_get_object(&p0, &len, &tag, &tc, length);
725 p = (unsigned char *)p0;
726 if(j & 0x80) ossl_raise(eASN1Error, NULL);
727 hlen = p - start;
728 if(yield){
729 VALUE arg = rb_ary_new();
730 rb_ary_push(arg, LONG2NUM(depth));
731 rb_ary_push(arg, LONG2NUM(off));
732 rb_ary_push(arg, LONG2NUM(hlen));
733 rb_ary_push(arg, LONG2NUM(len));
734 rb_ary_push(arg, (j & V_ASN1_CONSTRUCTED) ? Qtrue : Qfalse);
735 rb_ary_push(arg, ossl_asn1_class2sym(tc));
736 rb_ary_push(arg, INT2NUM(tag));
737 rb_yield(arg);
739 length -= hlen;
740 off += hlen;
741 if(len > length) ossl_raise(eASN1Error, "value is too short");
742 if((tc & V_ASN1_PRIVATE) == V_ASN1_PRIVATE)
743 tag_class = sPRIVATE;
744 else if((tc & V_ASN1_CONTEXT_SPECIFIC) == V_ASN1_CONTEXT_SPECIFIC)
745 tag_class = sCONTEXT_SPECIFIC;
746 else if((tc & V_ASN1_APPLICATION) == V_ASN1_APPLICATION)
747 tag_class = sAPPLICATION;
748 else
749 tag_class = sUNIVERSAL;
750 if(j & V_ASN1_CONSTRUCTED){
751 /* TODO: if j == 0x21 it is indefinite length object. */
752 if((j == 0x21) && (len == 0)){
753 long lastoff = off;
754 value = ossl_asn1_decode0(&p, length, &off, depth+1, 0, yield);
755 len = off - lastoff;
757 else value = ossl_asn1_decode0(&p, len, &off, depth+1, 0, yield);
759 else{
760 value = rb_str_new((const char *)p, len);
761 p += len;
762 off += len;
764 if(tag_class == sUNIVERSAL &&
765 tag < ossl_asn1_info_size && ossl_asn1_info[tag].klass){
766 VALUE klass = *ossl_asn1_info[tag].klass;
767 long flag = 0;
768 if(!rb_obj_is_kind_of(value, rb_cArray)){
769 switch(tag){
770 case V_ASN1_BOOLEAN:
771 value = decode_bool(start, hlen+len);
772 break;
773 case V_ASN1_INTEGER:
774 value = decode_int(start, hlen+len);
775 break;
776 case V_ASN1_BIT_STRING:
777 value = decode_bstr(start, hlen+len, &flag);
778 break;
779 case V_ASN1_NULL:
780 value = decode_null(start, hlen+len);
781 break;
782 case V_ASN1_ENUMERATED:
783 value = decode_enum(start, hlen+len);
784 break;
785 case V_ASN1_OBJECT:
786 value = decode_obj(start, hlen+len);
787 break;
788 case V_ASN1_UTCTIME: /* FALLTHROUGH */
789 case V_ASN1_GENERALIZEDTIME:
790 value = decode_time(start, hlen+len);
791 break;
792 default:
793 /* use original value */
794 break;
797 asn1data = rb_funcall(klass, rb_intern("new"), 1, value);
798 if(tag == V_ASN1_BIT_STRING){
799 rb_iv_set(asn1data, "@unused_bits", LONG2NUM(flag));
802 else{
803 asn1data = rb_funcall(cASN1Data, rb_intern("new"), 3,
804 value, INT2NUM(tag), ID2SYM(tag_class));
806 rb_ary_push(ary, asn1data);
807 length -= len;
808 if(once) break;
810 *pp = p;
811 *offset = off;
813 return ary;
816 static VALUE
817 ossl_asn1_traverse(VALUE self, VALUE obj)
819 unsigned char *p;
820 long offset = 0;
821 volatile VALUE tmp;
823 obj = ossl_to_der_if_possible(obj);
824 tmp = rb_str_new4(StringValue(obj));
825 p = (unsigned char *)RSTRING_PTR(tmp);
826 ossl_asn1_decode0(&p, RSTRING_LEN(tmp), &offset, 0, 0, 1);
828 return Qnil;
831 static VALUE
832 ossl_asn1_decode(VALUE self, VALUE obj)
834 VALUE ret, ary;
835 unsigned char *p;
836 long offset = 0;
837 volatile VALUE tmp;
839 obj = ossl_to_der_if_possible(obj);
840 tmp = rb_str_new4(StringValue(obj));
841 p = (unsigned char *)RSTRING_PTR(tmp);
842 ary = ossl_asn1_decode0(&p, RSTRING_LEN(tmp), &offset, 0, 1, 0);
843 ret = rb_ary_entry(ary, 0);
845 return ret;
848 static VALUE
849 ossl_asn1_decode_all(VALUE self, VALUE obj)
851 VALUE ret;
852 unsigned char *p;
853 long offset = 0;
854 volatile VALUE tmp;
856 obj = ossl_to_der_if_possible(obj);
857 tmp = rb_str_new4(StringValue(obj));
858 p = (unsigned char *)RSTRING_PTR(tmp);
859 ret = ossl_asn1_decode0(&p, RSTRING_LEN(tmp), &offset, 0, 0, 0);
861 return ret;
864 static VALUE
865 ossl_asn1_initialize(int argc, VALUE *argv, VALUE self)
867 VALUE value, tag, tagging, tag_class;
869 rb_scan_args(argc, argv, "13", &value, &tag, &tagging, &tag_class);
870 if(argc > 1){
871 if(NIL_P(tag))
872 ossl_raise(eASN1Error, "must specify tag number");
873 if(NIL_P(tagging))
874 tagging = ID2SYM(sEXPLICIT);
875 if(!SYMBOL_P(tagging))
876 ossl_raise(eASN1Error, "invalid tag default");
877 if(NIL_P(tag_class))
878 tag_class = ID2SYM(sCONTEXT_SPECIFIC);
879 if(!SYMBOL_P(tag_class))
880 ossl_raise(eASN1Error, "invalid tag class");
881 if(SYM2ID(tagging) == sIMPLICIT && NUM2INT(tag) > 31)
882 ossl_raise(eASN1Error, "tag number for Universal too large");
884 else{
885 tag = INT2NUM(ossl_asn1_default_tag(self));
886 tagging = Qnil;
887 tag_class = ID2SYM(sUNIVERSAL);
889 ossl_asn1_set_tag(self, tag);
890 ossl_asn1_set_value(self, value);
891 ossl_asn1_set_tagging(self, tagging);
892 ossl_asn1_set_tag_class(self, tag_class);
894 return self;
897 static int
898 ossl_i2d_ASN1_TYPE(ASN1_TYPE *a, unsigned char **pp)
900 #if OPENSSL_VERSION_NUMBER < 0x00907000L
901 if(!a) return 0;
902 if(a->type == V_ASN1_BOOLEAN)
903 return i2d_ASN1_BOOLEAN(a->value.boolean, pp);
904 #endif
905 return i2d_ASN1_TYPE(a, pp);
908 static void
909 ossl_ASN1_TYPE_free(ASN1_TYPE *a)
911 #if OPENSSL_VERSION_NUMBER < 0x00907000L
912 if(!a) return;
913 if(a->type == V_ASN1_BOOLEAN){
914 OPENSSL_free(a);
915 return;
917 #endif
918 ASN1_TYPE_free(a);
921 static VALUE
922 ossl_asn1prim_to_der(VALUE self)
924 ASN1_TYPE *asn1;
925 int tn, tc, explicit;
926 long len, reallen;
927 unsigned char *buf, *p;
928 VALUE str;
930 tn = NUM2INT(ossl_asn1_get_tag(self));
931 tc = ossl_asn1_tag_class(self);
932 explicit = ossl_asn1_is_explicit(self);
933 asn1 = ossl_asn1_get_asn1type(self);
935 len = ASN1_object_size(1, ossl_i2d_ASN1_TYPE(asn1, NULL), tn);
936 if(!(buf = OPENSSL_malloc(len))){
937 ossl_ASN1_TYPE_free(asn1);
938 ossl_raise(eASN1Error, "cannot alloc buffer");
940 p = buf;
941 if (tc == V_ASN1_UNIVERSAL) {
942 ossl_i2d_ASN1_TYPE(asn1, &p);
943 } else if (explicit) {
944 ASN1_put_object(&p, 1, ossl_i2d_ASN1_TYPE(asn1, NULL), tn, tc);
945 ossl_i2d_ASN1_TYPE(asn1, &p);
946 } else {
947 ossl_i2d_ASN1_TYPE(asn1, &p);
948 *buf = tc | tn | (*buf & V_ASN1_CONSTRUCTED);
950 ossl_ASN1_TYPE_free(asn1);
951 reallen = p - buf;
952 assert(reallen <= len);
953 str = ossl_buf2str((char *)buf, reallen); /* buf will be free in ossl_buf2str */
955 return str;
958 static VALUE
959 ossl_asn1cons_to_der(VALUE self)
961 int tag, tn, tc, explicit;
962 long seq_len, length;
963 unsigned char *p;
964 VALUE value, str;
966 tag = ossl_asn1_default_tag(self);
967 tn = NUM2INT(ossl_asn1_get_tag(self));
968 tc = ossl_asn1_tag_class(self);
969 explicit = ossl_asn1_is_explicit(self);
970 value = join_der(ossl_asn1_get_value(self));
972 seq_len = ASN1_object_size(1, RSTRING_LEN(value), tag);
973 length = ASN1_object_size(1, seq_len, tn);
974 str = rb_str_new(0, length);
975 p = (unsigned char *)RSTRING_PTR(str);
976 if(tc == V_ASN1_UNIVERSAL)
977 ASN1_put_object(&p, 1, RSTRING_LEN(value), tn, tc);
978 else{
979 if(explicit){
980 ASN1_put_object(&p, 1, seq_len, tn, tc);
981 ASN1_put_object(&p, 1, RSTRING_LEN(value), tag, V_ASN1_UNIVERSAL);
983 else ASN1_put_object(&p, 1, RSTRING_LEN(value), tn, tc);
985 memcpy(p, RSTRING_PTR(value), RSTRING_LEN(value));
986 p += RSTRING_LEN(value);
987 ossl_str_adjust(str, p);
989 return str;
992 static VALUE
993 ossl_asn1cons_each(VALUE self)
995 rb_ary_each(ossl_asn1_get_value(self));
996 return self;
999 static VALUE
1000 ossl_asn1obj_s_register(VALUE self, VALUE oid, VALUE sn, VALUE ln)
1002 StringValue(oid);
1003 StringValue(sn);
1004 StringValue(ln);
1006 if(!OBJ_create(RSTRING_PTR(oid), RSTRING_PTR(sn), RSTRING_PTR(ln)))
1007 ossl_raise(eASN1Error, NULL);
1009 return Qtrue;
1012 static VALUE
1013 ossl_asn1obj_get_sn(VALUE self)
1015 VALUE val, ret = Qnil;
1016 int nid;
1018 val = ossl_asn1_get_value(self);
1019 if ((nid = OBJ_txt2nid(StringValuePtr(val))) != NID_undef)
1020 ret = rb_str_new2(OBJ_nid2sn(nid));
1022 return ret;
1025 static VALUE
1026 ossl_asn1obj_get_ln(VALUE self)
1028 VALUE val, ret = Qnil;
1029 int nid;
1031 val = ossl_asn1_get_value(self);
1032 if ((nid = OBJ_txt2nid(StringValuePtr(val))) != NID_undef)
1033 ret = rb_str_new2(OBJ_nid2ln(nid));
1035 return ret;
1038 static VALUE
1039 ossl_asn1obj_get_oid(VALUE self)
1041 VALUE val;
1042 ASN1_OBJECT *a1obj;
1043 char buf[128];
1045 val = ossl_asn1_get_value(self);
1046 a1obj = obj_to_asn1obj(val);
1047 OBJ_obj2txt(buf, sizeof(buf), a1obj, 1);
1048 ASN1_OBJECT_free(a1obj);
1050 return rb_str_new2(buf);
1053 #define OSSL_ASN1_IMPL_FACTORY_METHOD(klass) \
1054 static VALUE ossl_asn1_##klass(int argc, VALUE *argv, VALUE self)\
1055 { return rb_funcall3(cASN1##klass, rb_intern("new"), argc, argv); }
1057 OSSL_ASN1_IMPL_FACTORY_METHOD(Boolean)
1058 OSSL_ASN1_IMPL_FACTORY_METHOD(Integer)
1059 OSSL_ASN1_IMPL_FACTORY_METHOD(Enumerated)
1060 OSSL_ASN1_IMPL_FACTORY_METHOD(BitString)
1061 OSSL_ASN1_IMPL_FACTORY_METHOD(OctetString)
1062 OSSL_ASN1_IMPL_FACTORY_METHOD(UTF8String)
1063 OSSL_ASN1_IMPL_FACTORY_METHOD(NumericString)
1064 OSSL_ASN1_IMPL_FACTORY_METHOD(PrintableString)
1065 OSSL_ASN1_IMPL_FACTORY_METHOD(T61String)
1066 OSSL_ASN1_IMPL_FACTORY_METHOD(VideotexString)
1067 OSSL_ASN1_IMPL_FACTORY_METHOD(IA5String)
1068 OSSL_ASN1_IMPL_FACTORY_METHOD(GraphicString)
1069 OSSL_ASN1_IMPL_FACTORY_METHOD(ISO64String)
1070 OSSL_ASN1_IMPL_FACTORY_METHOD(GeneralString)
1071 OSSL_ASN1_IMPL_FACTORY_METHOD(UniversalString)
1072 OSSL_ASN1_IMPL_FACTORY_METHOD(BMPString)
1073 OSSL_ASN1_IMPL_FACTORY_METHOD(Null)
1074 OSSL_ASN1_IMPL_FACTORY_METHOD(ObjectId)
1075 OSSL_ASN1_IMPL_FACTORY_METHOD(UTCTime)
1076 OSSL_ASN1_IMPL_FACTORY_METHOD(GeneralizedTime)
1077 OSSL_ASN1_IMPL_FACTORY_METHOD(Sequence)
1078 OSSL_ASN1_IMPL_FACTORY_METHOD(Set)
1080 void
1081 Init_ossl_asn1()
1083 VALUE ary;
1084 int i;
1086 #if 0 /* let rdoc know about mOSSL */
1087 mOSSL = rb_define_module("OpenSSL");
1088 #endif
1090 sUNIVERSAL = rb_intern("UNIVERSAL");
1091 sCONTEXT_SPECIFIC = rb_intern("CONTEXT_SPECIFIC");
1092 sAPPLICATION = rb_intern("APPLICATION");
1093 sPRIVATE = rb_intern("PRIVATE");
1094 sEXPLICIT = rb_intern("EXPLICIT");
1095 sIMPLICIT = rb_intern("IMPLICIT");
1097 mASN1 = rb_define_module_under(mOSSL, "ASN1");
1098 eASN1Error = rb_define_class_under(mASN1, "ASN1Error", eOSSLError);
1099 rb_define_module_function(mASN1, "traverse", ossl_asn1_traverse, 1);
1100 rb_define_module_function(mASN1, "decode", ossl_asn1_decode, 1);
1101 rb_define_module_function(mASN1, "decode_all", ossl_asn1_decode_all, 1);
1102 ary = rb_ary_new();
1103 rb_define_const(mASN1, "UNIVERSAL_TAG_NAME", ary);
1104 for(i = 0; i < ossl_asn1_info_size; i++){
1105 if(ossl_asn1_info[i].name[0] == '[') continue;
1106 rb_define_const(mASN1, ossl_asn1_info[i].name, INT2NUM(i));
1107 rb_ary_store(ary, i, rb_str_new2(ossl_asn1_info[i].name));
1110 cASN1Data = rb_define_class_under(mASN1, "ASN1Data", rb_cObject);
1111 rb_attr(cASN1Data, rb_intern("value"), 1, 1, 0);
1112 rb_attr(cASN1Data, rb_intern("tag"), 1, 1, 0);
1113 rb_attr(cASN1Data, rb_intern("tag_class"), 1, 1, 0);
1114 rb_define_method(cASN1Data, "initialize", ossl_asn1data_initialize, 3);
1115 rb_define_method(cASN1Data, "to_der", ossl_asn1data_to_der, 0);
1117 cASN1Primitive = rb_define_class_under(mASN1, "Primitive", cASN1Data);
1118 rb_attr(cASN1Primitive, rb_intern("tagging"), 1, 1, Qtrue);
1119 rb_define_method(cASN1Primitive, "initialize", ossl_asn1_initialize, -1);
1120 rb_define_method(cASN1Primitive, "to_der", ossl_asn1prim_to_der, 0);
1122 cASN1Constructive = rb_define_class_under(mASN1,"Constructive", cASN1Data);
1123 rb_include_module(cASN1Constructive, rb_mEnumerable);
1124 rb_attr(cASN1Constructive, rb_intern("tagging"), 1, 1, Qtrue);
1125 rb_define_method(cASN1Constructive, "initialize", ossl_asn1_initialize, -1);
1126 rb_define_method(cASN1Constructive, "to_der", ossl_asn1cons_to_der, 0);
1127 rb_define_method(cASN1Constructive, "each", ossl_asn1cons_each, 0);
1129 #define OSSL_ASN1_DEFINE_CLASS(name, super) \
1130 do{\
1131 cASN1##name = rb_define_class_under(mASN1, #name, cASN1##super);\
1132 rb_define_module_function(mASN1, #name, ossl_asn1_##name, -1);\
1133 }while(0)
1135 OSSL_ASN1_DEFINE_CLASS(Boolean, Primitive);
1136 OSSL_ASN1_DEFINE_CLASS(Integer, Primitive);
1137 OSSL_ASN1_DEFINE_CLASS(Enumerated, Primitive);
1138 OSSL_ASN1_DEFINE_CLASS(BitString, Primitive);
1139 OSSL_ASN1_DEFINE_CLASS(OctetString, Primitive);
1140 OSSL_ASN1_DEFINE_CLASS(UTF8String, Primitive);
1141 OSSL_ASN1_DEFINE_CLASS(NumericString, Primitive);
1142 OSSL_ASN1_DEFINE_CLASS(PrintableString, Primitive);
1143 OSSL_ASN1_DEFINE_CLASS(T61String, Primitive);
1144 OSSL_ASN1_DEFINE_CLASS(VideotexString, Primitive);
1145 OSSL_ASN1_DEFINE_CLASS(IA5String, Primitive);
1146 OSSL_ASN1_DEFINE_CLASS(GraphicString, Primitive);
1147 OSSL_ASN1_DEFINE_CLASS(ISO64String, Primitive);
1148 OSSL_ASN1_DEFINE_CLASS(GeneralString, Primitive);
1149 OSSL_ASN1_DEFINE_CLASS(UniversalString, Primitive);
1150 OSSL_ASN1_DEFINE_CLASS(BMPString, Primitive);
1151 OSSL_ASN1_DEFINE_CLASS(Null, Primitive);
1152 OSSL_ASN1_DEFINE_CLASS(ObjectId, Primitive);
1153 OSSL_ASN1_DEFINE_CLASS(UTCTime, Primitive);
1154 OSSL_ASN1_DEFINE_CLASS(GeneralizedTime, Primitive);
1156 OSSL_ASN1_DEFINE_CLASS(Sequence, Constructive);
1157 OSSL_ASN1_DEFINE_CLASS(Set, Constructive);
1159 rb_define_singleton_method(cASN1ObjectId, "register", ossl_asn1obj_s_register, 3);
1160 rb_define_method(cASN1ObjectId, "sn", ossl_asn1obj_get_sn, 0);
1161 rb_define_method(cASN1ObjectId, "ln", ossl_asn1obj_get_ln, 0);
1162 rb_define_method(cASN1ObjectId, "oid", ossl_asn1obj_get_oid, 0);
1163 rb_define_alias(cASN1ObjectId, "short_name", "sn");
1164 rb_define_alias(cASN1ObjectId, "long_name", "ln");
1165 rb_attr(cASN1BitString, rb_intern("unused_bits"), 1, 1, 0);