zig_example 0.3.0 → 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (63) hide show
  1. checksums.yaml +4 -4
  2. data/ext/mkmf.rb +2734 -0
  3. data/ext/openssl/openssl_missing.c +40 -0
  4. data/ext/openssl/openssl_missing.h +238 -0
  5. data/ext/openssl/ossl.c +1295 -0
  6. data/ext/openssl/ossl.h +201 -0
  7. data/ext/openssl/ossl_asn1.c +1891 -0
  8. data/ext/openssl/ossl_asn1.h +62 -0
  9. data/ext/openssl/ossl_bio.c +42 -0
  10. data/ext/openssl/ossl_bio.h +16 -0
  11. data/ext/openssl/ossl_bn.c +1344 -0
  12. data/ext/openssl/ossl_bn.h +26 -0
  13. data/ext/openssl/ossl_cipher.c +1074 -0
  14. data/ext/openssl/ossl_cipher.h +20 -0
  15. data/ext/openssl/ossl_config.c +460 -0
  16. data/ext/openssl/ossl_config.h +16 -0
  17. data/ext/openssl/ossl_digest.c +425 -0
  18. data/ext/openssl/ossl_digest.h +20 -0
  19. data/ext/openssl/ossl_engine.c +568 -0
  20. data/ext/openssl/ossl_engine.h +19 -0
  21. data/ext/openssl/ossl_hmac.c +310 -0
  22. data/ext/openssl/ossl_hmac.h +18 -0
  23. data/ext/openssl/ossl_kdf.c +311 -0
  24. data/ext/openssl/ossl_kdf.h +6 -0
  25. data/ext/openssl/ossl_ns_spki.c +405 -0
  26. data/ext/openssl/ossl_ns_spki.h +19 -0
  27. data/ext/openssl/ossl_ocsp.c +1965 -0
  28. data/ext/openssl/ossl_ocsp.h +23 -0
  29. data/ext/openssl/ossl_pkcs12.c +275 -0
  30. data/ext/openssl/ossl_pkcs12.h +13 -0
  31. data/ext/openssl/ossl_pkcs7.c +1081 -0
  32. data/ext/openssl/ossl_pkcs7.h +36 -0
  33. data/ext/openssl/ossl_pkey.c +1624 -0
  34. data/ext/openssl/ossl_pkey.h +204 -0
  35. data/ext/openssl/ossl_pkey_dh.c +440 -0
  36. data/ext/openssl/ossl_pkey_dsa.c +359 -0
  37. data/ext/openssl/ossl_pkey_ec.c +1655 -0
  38. data/ext/openssl/ossl_pkey_rsa.c +579 -0
  39. data/ext/openssl/ossl_rand.c +200 -0
  40. data/ext/openssl/ossl_rand.h +18 -0
  41. data/ext/openssl/ossl_ssl.c +3142 -0
  42. data/ext/openssl/ossl_ssl.h +36 -0
  43. data/ext/openssl/ossl_ssl_session.c +331 -0
  44. data/ext/openssl/ossl_ts.c +1539 -0
  45. data/ext/openssl/ossl_ts.h +16 -0
  46. data/ext/openssl/ossl_x509.c +256 -0
  47. data/ext/openssl/ossl_x509.h +115 -0
  48. data/ext/openssl/ossl_x509attr.c +324 -0
  49. data/ext/openssl/ossl_x509cert.c +1002 -0
  50. data/ext/openssl/ossl_x509crl.c +545 -0
  51. data/ext/openssl/ossl_x509ext.c +490 -0
  52. data/ext/openssl/ossl_x509name.c +597 -0
  53. data/ext/openssl/ossl_x509req.c +444 -0
  54. data/ext/openssl/ossl_x509revoked.c +300 -0
  55. data/ext/openssl/ossl_x509store.c +986 -0
  56. data/ext/zigrb_100doors/build.zig +0 -12
  57. data/ext/zigrb_100doors/extconf.rb +2 -19
  58. data/ext/zigrb_ackermann/build.zig +0 -12
  59. data/ext/zigrb_ackermann/extconf.rb +2 -19
  60. data/ext/zigrb_lucas_lehmer/build.zig +0 -12
  61. data/ext/zigrb_lucas_lehmer/extconf.rb +2 -19
  62. data/lib/zig_example/version.rb +1 -1
  63. metadata +56 -2
@@ -0,0 +1,1891 @@
1
+ /*
2
+ * 'OpenSSL for Ruby' team members
3
+ * Copyright (C) 2003
4
+ * All rights reserved.
5
+ */
6
+ /*
7
+ * This program is licensed under the same licence as Ruby.
8
+ * (See the file 'LICENCE'.)
9
+ */
10
+ #include "ossl.h"
11
+
12
+ static VALUE ossl_asn1_decode0(unsigned char **pp, long length, long *offset,
13
+ int depth, int yield, long *num_read);
14
+ static VALUE ossl_asn1_initialize(int argc, VALUE *argv, VALUE self);
15
+
16
+ /*
17
+ * DATE conversion
18
+ */
19
+ VALUE
20
+ asn1time_to_time(const ASN1_TIME *time)
21
+ {
22
+ struct tm tm;
23
+ VALUE argv[6];
24
+ int count;
25
+
26
+ memset(&tm, 0, sizeof(struct tm));
27
+
28
+ switch (time->type) {
29
+ case V_ASN1_UTCTIME:
30
+ count = sscanf((const char *)time->data, "%2d%2d%2d%2d%2d%2dZ",
31
+ &tm.tm_year, &tm.tm_mon, &tm.tm_mday, &tm.tm_hour, &tm.tm_min,
32
+ &tm.tm_sec);
33
+
34
+ if (count == 5) {
35
+ tm.tm_sec = 0;
36
+ } else if (count != 6) {
37
+ ossl_raise(rb_eTypeError, "bad UTCTIME format: \"%s\"",
38
+ time->data);
39
+ }
40
+ if (tm.tm_year < 69) {
41
+ tm.tm_year += 2000;
42
+ } else {
43
+ tm.tm_year += 1900;
44
+ }
45
+ break;
46
+ case V_ASN1_GENERALIZEDTIME:
47
+ count = sscanf((const char *)time->data, "%4d%2d%2d%2d%2d%2dZ",
48
+ &tm.tm_year, &tm.tm_mon, &tm.tm_mday, &tm.tm_hour, &tm.tm_min,
49
+ &tm.tm_sec);
50
+ if (count == 5) {
51
+ tm.tm_sec = 0;
52
+ }
53
+ else if (count != 6) {
54
+ ossl_raise(rb_eTypeError, "bad GENERALIZEDTIME format: \"%s\"",
55
+ time->data);
56
+ }
57
+ break;
58
+ default:
59
+ rb_warning("unknown time format");
60
+ return Qnil;
61
+ }
62
+ argv[0] = INT2NUM(tm.tm_year);
63
+ argv[1] = INT2NUM(tm.tm_mon);
64
+ argv[2] = INT2NUM(tm.tm_mday);
65
+ argv[3] = INT2NUM(tm.tm_hour);
66
+ argv[4] = INT2NUM(tm.tm_min);
67
+ argv[5] = INT2NUM(tm.tm_sec);
68
+
69
+ return rb_funcall2(rb_cTime, rb_intern("utc"), 6, argv);
70
+ }
71
+
72
+ static VALUE
73
+ asn1time_to_time_i(VALUE arg)
74
+ {
75
+ return asn1time_to_time((ASN1_TIME *)arg);
76
+ }
77
+
78
+ void
79
+ ossl_time_split(VALUE time, time_t *sec, int *days)
80
+ {
81
+ VALUE num = rb_Integer(time);
82
+
83
+ if (FIXNUM_P(num)) {
84
+ time_t t = FIX2LONG(num);
85
+ *sec = t % 86400;
86
+ *days = rb_long2int(t / 86400);
87
+ }
88
+ else {
89
+ *days = NUM2INT(rb_funcall(num, rb_intern("/"), 1, INT2FIX(86400)));
90
+ *sec = NUM2TIMET(rb_funcall(num, rb_intern("%"), 1, INT2FIX(86400)));
91
+ }
92
+ }
93
+
94
+ /*
95
+ * STRING conversion
96
+ */
97
+ VALUE
98
+ asn1str_to_str(const ASN1_STRING *str)
99
+ {
100
+ return rb_str_new((const char *)str->data, str->length);
101
+ }
102
+
103
+ /*
104
+ * ASN1_INTEGER conversions
105
+ */
106
+ VALUE
107
+ asn1integer_to_num(const ASN1_INTEGER *ai)
108
+ {
109
+ BIGNUM *bn;
110
+ VALUE num;
111
+
112
+ if (!ai) {
113
+ ossl_raise(rb_eTypeError, "ASN1_INTEGER is NULL!");
114
+ }
115
+ if (ai->type == V_ASN1_ENUMERATED)
116
+ /* const_cast: workaround for old OpenSSL */
117
+ bn = ASN1_ENUMERATED_to_BN((ASN1_ENUMERATED *)ai, NULL);
118
+ else
119
+ bn = ASN1_INTEGER_to_BN(ai, NULL);
120
+
121
+ if (!bn)
122
+ ossl_raise(eOSSLError, NULL);
123
+ num = ossl_bn_new(bn);
124
+ BN_free(bn);
125
+
126
+ return num;
127
+ }
128
+
129
+ ASN1_INTEGER *
130
+ num_to_asn1integer(VALUE obj, ASN1_INTEGER *ai)
131
+ {
132
+ BIGNUM *bn;
133
+
134
+ if (NIL_P(obj))
135
+ ossl_raise(rb_eTypeError, "Can't convert nil into Integer");
136
+
137
+ bn = GetBNPtr(obj);
138
+
139
+ if (!(ai = BN_to_ASN1_INTEGER(bn, ai)))
140
+ ossl_raise(eOSSLError, NULL);
141
+
142
+ return ai;
143
+ }
144
+
145
+ static VALUE
146
+ asn1integer_to_num_i(VALUE arg)
147
+ {
148
+ return asn1integer_to_num((ASN1_INTEGER *)arg);
149
+ }
150
+
151
+ /********/
152
+ /*
153
+ * ASN1 module
154
+ */
155
+ #define ossl_asn1_get_value(o) rb_attr_get((o),sivVALUE)
156
+ #define ossl_asn1_get_tag(o) rb_attr_get((o),sivTAG)
157
+ #define ossl_asn1_get_tagging(o) rb_attr_get((o),sivTAGGING)
158
+ #define ossl_asn1_get_tag_class(o) rb_attr_get((o),sivTAG_CLASS)
159
+ #define ossl_asn1_get_indefinite_length(o) rb_attr_get((o),sivINDEFINITE_LENGTH)
160
+
161
+ #define ossl_asn1_set_value(o,v) rb_ivar_set((o),sivVALUE,(v))
162
+ #define ossl_asn1_set_tag(o,v) rb_ivar_set((o),sivTAG,(v))
163
+ #define ossl_asn1_set_tagging(o,v) rb_ivar_set((o),sivTAGGING,(v))
164
+ #define ossl_asn1_set_tag_class(o,v) rb_ivar_set((o),sivTAG_CLASS,(v))
165
+ #define ossl_asn1_set_indefinite_length(o,v) rb_ivar_set((o),sivINDEFINITE_LENGTH,(v))
166
+
167
+ VALUE mASN1;
168
+ VALUE eASN1Error;
169
+
170
+ VALUE cASN1Data;
171
+ VALUE cASN1Primitive;
172
+ VALUE cASN1Constructive;
173
+
174
+ VALUE cASN1EndOfContent;
175
+ VALUE cASN1Boolean; /* BOOLEAN */
176
+ VALUE cASN1Integer, cASN1Enumerated; /* INTEGER */
177
+ VALUE cASN1BitString; /* BIT STRING */
178
+ VALUE cASN1OctetString, cASN1UTF8String; /* STRINGs */
179
+ VALUE cASN1NumericString, cASN1PrintableString;
180
+ VALUE cASN1T61String, cASN1VideotexString;
181
+ VALUE cASN1IA5String, cASN1GraphicString;
182
+ VALUE cASN1ISO64String, cASN1GeneralString;
183
+ VALUE cASN1UniversalString, cASN1BMPString;
184
+ VALUE cASN1Null; /* NULL */
185
+ VALUE cASN1ObjectId; /* OBJECT IDENTIFIER */
186
+ VALUE cASN1UTCTime, cASN1GeneralizedTime; /* TIME */
187
+ VALUE cASN1Sequence, cASN1Set; /* CONSTRUCTIVE */
188
+
189
+ static VALUE sym_IMPLICIT, sym_EXPLICIT;
190
+ static VALUE sym_UNIVERSAL, sym_APPLICATION, sym_CONTEXT_SPECIFIC, sym_PRIVATE;
191
+ static ID sivVALUE, sivTAG, sivTAG_CLASS, sivTAGGING, sivINDEFINITE_LENGTH, sivUNUSED_BITS;
192
+ static ID id_each;
193
+
194
+ /*
195
+ * Ruby to ASN1 converters
196
+ */
197
+ static ASN1_BOOLEAN
198
+ obj_to_asn1bool(VALUE obj)
199
+ {
200
+ if (NIL_P(obj))
201
+ ossl_raise(rb_eTypeError, "Can't convert nil into Boolean");
202
+
203
+ return RTEST(obj) ? 0xff : 0x0;
204
+ }
205
+
206
+ static ASN1_INTEGER*
207
+ obj_to_asn1int(VALUE obj)
208
+ {
209
+ return num_to_asn1integer(obj, NULL);
210
+ }
211
+
212
+ static ASN1_BIT_STRING*
213
+ obj_to_asn1bstr(VALUE obj, long unused_bits)
214
+ {
215
+ ASN1_BIT_STRING *bstr;
216
+
217
+ if (unused_bits < 0 || unused_bits > 7)
218
+ ossl_raise(eASN1Error, "unused_bits for a bitstring value must be in "\
219
+ "the range 0 to 7");
220
+ StringValue(obj);
221
+ if(!(bstr = ASN1_BIT_STRING_new()))
222
+ ossl_raise(eASN1Error, NULL);
223
+ ASN1_BIT_STRING_set(bstr, (unsigned char *)RSTRING_PTR(obj), RSTRING_LENINT(obj));
224
+ bstr->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT|0x07); /* clear */
225
+ bstr->flags |= ASN1_STRING_FLAG_BITS_LEFT | unused_bits;
226
+
227
+ return bstr;
228
+ }
229
+
230
+ static ASN1_STRING*
231
+ obj_to_asn1str(VALUE obj)
232
+ {
233
+ ASN1_STRING *str;
234
+
235
+ StringValue(obj);
236
+ if(!(str = ASN1_STRING_new()))
237
+ ossl_raise(eASN1Error, NULL);
238
+ ASN1_STRING_set(str, RSTRING_PTR(obj), RSTRING_LENINT(obj));
239
+
240
+ return str;
241
+ }
242
+
243
+ static ASN1_NULL*
244
+ obj_to_asn1null(VALUE obj)
245
+ {
246
+ ASN1_NULL *null;
247
+
248
+ if(!NIL_P(obj))
249
+ ossl_raise(eASN1Error, "nil expected");
250
+ if(!(null = ASN1_NULL_new()))
251
+ ossl_raise(eASN1Error, NULL);
252
+
253
+ return null;
254
+ }
255
+
256
+ static ASN1_OBJECT*
257
+ obj_to_asn1obj(VALUE obj)
258
+ {
259
+ ASN1_OBJECT *a1obj;
260
+
261
+ StringValueCStr(obj);
262
+ a1obj = OBJ_txt2obj(RSTRING_PTR(obj), 0);
263
+ if(!a1obj) a1obj = OBJ_txt2obj(RSTRING_PTR(obj), 1);
264
+ if(!a1obj) ossl_raise(eASN1Error, "invalid OBJECT ID %"PRIsVALUE, obj);
265
+
266
+ return a1obj;
267
+ }
268
+
269
+ static ASN1_UTCTIME *
270
+ obj_to_asn1utime(VALUE time)
271
+ {
272
+ time_t sec;
273
+ ASN1_UTCTIME *t;
274
+
275
+ int off_days;
276
+
277
+ ossl_time_split(time, &sec, &off_days);
278
+ if (!(t = ASN1_UTCTIME_adj(NULL, sec, off_days, 0)))
279
+ ossl_raise(eASN1Error, NULL);
280
+
281
+ return t;
282
+ }
283
+
284
+ static ASN1_GENERALIZEDTIME *
285
+ obj_to_asn1gtime(VALUE time)
286
+ {
287
+ time_t sec;
288
+ ASN1_GENERALIZEDTIME *t;
289
+
290
+ int off_days;
291
+
292
+ ossl_time_split(time, &sec, &off_days);
293
+ if (!(t = ASN1_GENERALIZEDTIME_adj(NULL, sec, off_days, 0)))
294
+ ossl_raise(eASN1Error, NULL);
295
+
296
+ return t;
297
+ }
298
+
299
+ static ASN1_STRING*
300
+ obj_to_asn1derstr(VALUE obj)
301
+ {
302
+ ASN1_STRING *a1str;
303
+ VALUE str;
304
+
305
+ str = ossl_to_der(obj);
306
+ if(!(a1str = ASN1_STRING_new()))
307
+ ossl_raise(eASN1Error, NULL);
308
+ ASN1_STRING_set(a1str, RSTRING_PTR(str), RSTRING_LENINT(str));
309
+
310
+ return a1str;
311
+ }
312
+
313
+ /*
314
+ * DER to Ruby converters
315
+ */
316
+ static VALUE
317
+ decode_bool(unsigned char* der, long length)
318
+ {
319
+ const unsigned char *p = der;
320
+
321
+ if (length != 3)
322
+ ossl_raise(eASN1Error, "invalid length for BOOLEAN");
323
+ if (p[0] != 1 || p[1] != 1)
324
+ ossl_raise(eASN1Error, "invalid BOOLEAN");
325
+
326
+ return p[2] ? Qtrue : Qfalse;
327
+ }
328
+
329
+ static VALUE
330
+ decode_int(unsigned char* der, long length)
331
+ {
332
+ ASN1_INTEGER *ai;
333
+ const unsigned char *p;
334
+ VALUE ret;
335
+ int status = 0;
336
+
337
+ p = der;
338
+ if(!(ai = d2i_ASN1_INTEGER(NULL, &p, length)))
339
+ ossl_raise(eASN1Error, NULL);
340
+ ret = rb_protect(asn1integer_to_num_i,
341
+ (VALUE)ai, &status);
342
+ ASN1_INTEGER_free(ai);
343
+ if(status) rb_jump_tag(status);
344
+
345
+ return ret;
346
+ }
347
+
348
+ static VALUE
349
+ decode_bstr(unsigned char* der, long length, long *unused_bits)
350
+ {
351
+ ASN1_BIT_STRING *bstr;
352
+ const unsigned char *p;
353
+ long len;
354
+ VALUE ret;
355
+
356
+ p = der;
357
+ if(!(bstr = d2i_ASN1_BIT_STRING(NULL, &p, length)))
358
+ ossl_raise(eASN1Error, NULL);
359
+ len = bstr->length;
360
+ *unused_bits = 0;
361
+ if(bstr->flags & ASN1_STRING_FLAG_BITS_LEFT)
362
+ *unused_bits = bstr->flags & 0x07;
363
+ ret = rb_str_new((const char *)bstr->data, len);
364
+ ASN1_BIT_STRING_free(bstr);
365
+
366
+ return ret;
367
+ }
368
+
369
+ static VALUE
370
+ decode_enum(unsigned char* der, long length)
371
+ {
372
+ ASN1_ENUMERATED *ai;
373
+ const unsigned char *p;
374
+ VALUE ret;
375
+ int status = 0;
376
+
377
+ p = der;
378
+ if(!(ai = d2i_ASN1_ENUMERATED(NULL, &p, length)))
379
+ ossl_raise(eASN1Error, NULL);
380
+ ret = rb_protect(asn1integer_to_num_i,
381
+ (VALUE)ai, &status);
382
+ ASN1_ENUMERATED_free(ai);
383
+ if(status) rb_jump_tag(status);
384
+
385
+ return ret;
386
+ }
387
+
388
+ static VALUE
389
+ decode_null(unsigned char* der, long length)
390
+ {
391
+ ASN1_NULL *null;
392
+ const unsigned char *p;
393
+
394
+ p = der;
395
+ if(!(null = d2i_ASN1_NULL(NULL, &p, length)))
396
+ ossl_raise(eASN1Error, NULL);
397
+ ASN1_NULL_free(null);
398
+
399
+ return Qnil;
400
+ }
401
+
402
+ static VALUE
403
+ decode_obj(unsigned char* der, long length)
404
+ {
405
+ ASN1_OBJECT *obj;
406
+ const unsigned char *p;
407
+ VALUE ret;
408
+ int nid;
409
+ BIO *bio;
410
+
411
+ p = der;
412
+ if(!(obj = d2i_ASN1_OBJECT(NULL, &p, length)))
413
+ ossl_raise(eASN1Error, NULL);
414
+ if((nid = OBJ_obj2nid(obj)) != NID_undef){
415
+ ASN1_OBJECT_free(obj);
416
+ ret = rb_str_new2(OBJ_nid2sn(nid));
417
+ }
418
+ else{
419
+ if(!(bio = BIO_new(BIO_s_mem()))){
420
+ ASN1_OBJECT_free(obj);
421
+ ossl_raise(eASN1Error, NULL);
422
+ }
423
+ i2a_ASN1_OBJECT(bio, obj);
424
+ ASN1_OBJECT_free(obj);
425
+ ret = ossl_membio2str(bio);
426
+ }
427
+
428
+ return ret;
429
+ }
430
+
431
+ static VALUE
432
+ decode_time(unsigned char* der, long length)
433
+ {
434
+ ASN1_TIME *time;
435
+ const unsigned char *p;
436
+ VALUE ret;
437
+ int status = 0;
438
+
439
+ p = der;
440
+ if(!(time = d2i_ASN1_TIME(NULL, &p, length)))
441
+ ossl_raise(eASN1Error, NULL);
442
+ ret = rb_protect(asn1time_to_time_i,
443
+ (VALUE)time, &status);
444
+ ASN1_TIME_free(time);
445
+ if(status) rb_jump_tag(status);
446
+
447
+ return ret;
448
+ }
449
+
450
+ static VALUE
451
+ decode_eoc(unsigned char *der, long length)
452
+ {
453
+ if (length != 2 || !(der[0] == 0x00 && der[1] == 0x00))
454
+ ossl_raise(eASN1Error, NULL);
455
+
456
+ return rb_str_new("", 0);
457
+ }
458
+
459
+ /********/
460
+
461
+ typedef struct {
462
+ const char *name;
463
+ VALUE *klass;
464
+ } ossl_asn1_info_t;
465
+
466
+ static const ossl_asn1_info_t ossl_asn1_info[] = {
467
+ { "EOC", &cASN1EndOfContent, }, /* 0 */
468
+ { "BOOLEAN", &cASN1Boolean, }, /* 1 */
469
+ { "INTEGER", &cASN1Integer, }, /* 2 */
470
+ { "BIT_STRING", &cASN1BitString, }, /* 3 */
471
+ { "OCTET_STRING", &cASN1OctetString, }, /* 4 */
472
+ { "NULL", &cASN1Null, }, /* 5 */
473
+ { "OBJECT", &cASN1ObjectId, }, /* 6 */
474
+ { "OBJECT_DESCRIPTOR", NULL, }, /* 7 */
475
+ { "EXTERNAL", NULL, }, /* 8 */
476
+ { "REAL", NULL, }, /* 9 */
477
+ { "ENUMERATED", &cASN1Enumerated, }, /* 10 */
478
+ { "EMBEDDED_PDV", NULL, }, /* 11 */
479
+ { "UTF8STRING", &cASN1UTF8String, }, /* 12 */
480
+ { "RELATIVE_OID", NULL, }, /* 13 */
481
+ { "[UNIVERSAL 14]", NULL, }, /* 14 */
482
+ { "[UNIVERSAL 15]", NULL, }, /* 15 */
483
+ { "SEQUENCE", &cASN1Sequence, }, /* 16 */
484
+ { "SET", &cASN1Set, }, /* 17 */
485
+ { "NUMERICSTRING", &cASN1NumericString, }, /* 18 */
486
+ { "PRINTABLESTRING", &cASN1PrintableString, }, /* 19 */
487
+ { "T61STRING", &cASN1T61String, }, /* 20 */
488
+ { "VIDEOTEXSTRING", &cASN1VideotexString, }, /* 21 */
489
+ { "IA5STRING", &cASN1IA5String, }, /* 22 */
490
+ { "UTCTIME", &cASN1UTCTime, }, /* 23 */
491
+ { "GENERALIZEDTIME", &cASN1GeneralizedTime, }, /* 24 */
492
+ { "GRAPHICSTRING", &cASN1GraphicString, }, /* 25 */
493
+ { "ISO64STRING", &cASN1ISO64String, }, /* 26 */
494
+ { "GENERALSTRING", &cASN1GeneralString, }, /* 27 */
495
+ { "UNIVERSALSTRING", &cASN1UniversalString, }, /* 28 */
496
+ { "CHARACTER_STRING", NULL, }, /* 29 */
497
+ { "BMPSTRING", &cASN1BMPString, }, /* 30 */
498
+ };
499
+
500
+ enum {ossl_asn1_info_size = (sizeof(ossl_asn1_info)/sizeof(ossl_asn1_info[0]))};
501
+
502
+ static VALUE class_tag_map;
503
+
504
+ static int ossl_asn1_default_tag(VALUE obj);
505
+
506
+ ASN1_TYPE*
507
+ ossl_asn1_get_asn1type(VALUE obj)
508
+ {
509
+ ASN1_TYPE *ret;
510
+ VALUE value, rflag;
511
+ void *ptr;
512
+ typedef void free_func_type(void *);
513
+ free_func_type *free_func;
514
+ int tag;
515
+
516
+ tag = ossl_asn1_default_tag(obj);
517
+ value = ossl_asn1_get_value(obj);
518
+ switch(tag){
519
+ case V_ASN1_BOOLEAN:
520
+ ptr = (void*)(VALUE)obj_to_asn1bool(value);
521
+ free_func = NULL;
522
+ break;
523
+ case V_ASN1_INTEGER: /* FALLTHROUGH */
524
+ case V_ASN1_ENUMERATED:
525
+ ptr = obj_to_asn1int(value);
526
+ free_func = (free_func_type *)ASN1_INTEGER_free;
527
+ break;
528
+ case V_ASN1_BIT_STRING:
529
+ rflag = rb_attr_get(obj, sivUNUSED_BITS);
530
+ ptr = obj_to_asn1bstr(value, NUM2INT(rflag));
531
+ free_func = (free_func_type *)ASN1_BIT_STRING_free;
532
+ break;
533
+ case V_ASN1_NULL:
534
+ ptr = obj_to_asn1null(value);
535
+ free_func = (free_func_type *)ASN1_NULL_free;
536
+ break;
537
+ case V_ASN1_OCTET_STRING: /* FALLTHROUGH */
538
+ case V_ASN1_UTF8STRING: /* FALLTHROUGH */
539
+ case V_ASN1_NUMERICSTRING: /* FALLTHROUGH */
540
+ case V_ASN1_PRINTABLESTRING: /* FALLTHROUGH */
541
+ case V_ASN1_T61STRING: /* FALLTHROUGH */
542
+ case V_ASN1_VIDEOTEXSTRING: /* FALLTHROUGH */
543
+ case V_ASN1_IA5STRING: /* FALLTHROUGH */
544
+ case V_ASN1_GRAPHICSTRING: /* FALLTHROUGH */
545
+ case V_ASN1_ISO64STRING: /* FALLTHROUGH */
546
+ case V_ASN1_GENERALSTRING: /* FALLTHROUGH */
547
+ case V_ASN1_UNIVERSALSTRING: /* FALLTHROUGH */
548
+ case V_ASN1_BMPSTRING:
549
+ ptr = obj_to_asn1str(value);
550
+ free_func = (free_func_type *)ASN1_STRING_free;
551
+ break;
552
+ case V_ASN1_OBJECT:
553
+ ptr = obj_to_asn1obj(value);
554
+ free_func = (free_func_type *)ASN1_OBJECT_free;
555
+ break;
556
+ case V_ASN1_UTCTIME:
557
+ ptr = obj_to_asn1utime(value);
558
+ free_func = (free_func_type *)ASN1_TIME_free;
559
+ break;
560
+ case V_ASN1_GENERALIZEDTIME:
561
+ ptr = obj_to_asn1gtime(value);
562
+ free_func = (free_func_type *)ASN1_TIME_free;
563
+ break;
564
+ case V_ASN1_SET: /* FALLTHROUGH */
565
+ case V_ASN1_SEQUENCE:
566
+ ptr = obj_to_asn1derstr(obj);
567
+ free_func = (free_func_type *)ASN1_STRING_free;
568
+ break;
569
+ default:
570
+ ossl_raise(eASN1Error, "unsupported ASN.1 type");
571
+ }
572
+ if(!(ret = OPENSSL_malloc(sizeof(ASN1_TYPE)))){
573
+ if(free_func) free_func(ptr);
574
+ ossl_raise(eASN1Error, "ASN1_TYPE alloc failure");
575
+ }
576
+ memset(ret, 0, sizeof(ASN1_TYPE));
577
+ ASN1_TYPE_set(ret, tag, ptr);
578
+
579
+ return ret;
580
+ }
581
+
582
+ static int
583
+ ossl_asn1_default_tag(VALUE obj)
584
+ {
585
+ VALUE tmp_class, tag;
586
+
587
+ tmp_class = CLASS_OF(obj);
588
+ while (!NIL_P(tmp_class)) {
589
+ tag = rb_hash_lookup(class_tag_map, tmp_class);
590
+ if (tag != Qnil)
591
+ return NUM2INT(tag);
592
+ tmp_class = rb_class_superclass(tmp_class);
593
+ }
594
+
595
+ return -1;
596
+ }
597
+
598
+ static int
599
+ ossl_asn1_tag(VALUE obj)
600
+ {
601
+ VALUE tag;
602
+
603
+ tag = ossl_asn1_get_tag(obj);
604
+ if(NIL_P(tag))
605
+ ossl_raise(eASN1Error, "tag number not specified");
606
+
607
+ return NUM2INT(tag);
608
+ }
609
+
610
+ static int
611
+ ossl_asn1_tag_class(VALUE obj)
612
+ {
613
+ VALUE s;
614
+
615
+ s = ossl_asn1_get_tag_class(obj);
616
+ if (NIL_P(s) || s == sym_UNIVERSAL)
617
+ return V_ASN1_UNIVERSAL;
618
+ else if (s == sym_APPLICATION)
619
+ return V_ASN1_APPLICATION;
620
+ else if (s == sym_CONTEXT_SPECIFIC)
621
+ return V_ASN1_CONTEXT_SPECIFIC;
622
+ else if (s == sym_PRIVATE)
623
+ return V_ASN1_PRIVATE;
624
+ else
625
+ ossl_raise(eASN1Error, "invalid tag class");
626
+ }
627
+
628
+ static VALUE
629
+ ossl_asn1_class2sym(int tc)
630
+ {
631
+ if((tc & V_ASN1_PRIVATE) == V_ASN1_PRIVATE)
632
+ return sym_PRIVATE;
633
+ else if((tc & V_ASN1_CONTEXT_SPECIFIC) == V_ASN1_CONTEXT_SPECIFIC)
634
+ return sym_CONTEXT_SPECIFIC;
635
+ else if((tc & V_ASN1_APPLICATION) == V_ASN1_APPLICATION)
636
+ return sym_APPLICATION;
637
+ else
638
+ return sym_UNIVERSAL;
639
+ }
640
+
641
+ /*
642
+ * call-seq:
643
+ * OpenSSL::ASN1::ASN1Data.new(value, tag, tag_class) => ASN1Data
644
+ *
645
+ * _value_: Please have a look at Constructive and Primitive to see how Ruby
646
+ * types are mapped to ASN.1 types and vice versa.
647
+ *
648
+ * _tag_: An Integer indicating the tag number.
649
+ *
650
+ * _tag_class_: A Symbol indicating the tag class. Please cf. ASN1 for
651
+ * possible values.
652
+ *
653
+ * == Example
654
+ * asn1_int = OpenSSL::ASN1Data.new(42, 2, :UNIVERSAL) # => Same as OpenSSL::ASN1::Integer.new(42)
655
+ * tagged_int = OpenSSL::ASN1Data.new(42, 0, :CONTEXT_SPECIFIC) # implicitly 0-tagged INTEGER
656
+ */
657
+ static VALUE
658
+ ossl_asn1data_initialize(VALUE self, VALUE value, VALUE tag, VALUE tag_class)
659
+ {
660
+ if(!SYMBOL_P(tag_class))
661
+ ossl_raise(eASN1Error, "invalid tag class");
662
+ ossl_asn1_set_tag(self, tag);
663
+ ossl_asn1_set_value(self, value);
664
+ ossl_asn1_set_tag_class(self, tag_class);
665
+ ossl_asn1_set_indefinite_length(self, Qfalse);
666
+
667
+ return self;
668
+ }
669
+
670
+ static VALUE
671
+ to_der_internal(VALUE self, int constructed, int indef_len, VALUE body)
672
+ {
673
+ int encoding = constructed ? indef_len ? 2 : 1 : 0;
674
+ int tag_class = ossl_asn1_tag_class(self);
675
+ int tag_number = ossl_asn1_tag(self);
676
+ int default_tag_number = ossl_asn1_default_tag(self);
677
+ int body_length, total_length;
678
+ VALUE str;
679
+ unsigned char *p;
680
+
681
+ body_length = RSTRING_LENINT(body);
682
+ if (ossl_asn1_get_tagging(self) == sym_EXPLICIT) {
683
+ int inner_length, e_encoding = indef_len ? 2 : 1;
684
+
685
+ if (default_tag_number == -1)
686
+ ossl_raise(eASN1Error, "explicit tagging of unknown tag");
687
+
688
+ inner_length = ASN1_object_size(encoding, body_length, default_tag_number);
689
+ total_length = ASN1_object_size(e_encoding, inner_length, tag_number);
690
+ str = rb_str_new(NULL, total_length);
691
+ p = (unsigned char *)RSTRING_PTR(str);
692
+ /* Put explicit tag */
693
+ ASN1_put_object(&p, e_encoding, inner_length, tag_number, tag_class);
694
+ /* Append inner object */
695
+ ASN1_put_object(&p, encoding, body_length, default_tag_number, V_ASN1_UNIVERSAL);
696
+ memcpy(p, RSTRING_PTR(body), body_length);
697
+ p += body_length;
698
+ if (indef_len) {
699
+ ASN1_put_eoc(&p); /* For inner object */
700
+ ASN1_put_eoc(&p); /* For wrapper object */
701
+ }
702
+ }
703
+ else {
704
+ total_length = ASN1_object_size(encoding, body_length, tag_number);
705
+ str = rb_str_new(NULL, total_length);
706
+ p = (unsigned char *)RSTRING_PTR(str);
707
+ ASN1_put_object(&p, encoding, body_length, tag_number, tag_class);
708
+ memcpy(p, RSTRING_PTR(body), body_length);
709
+ p += body_length;
710
+ if (indef_len)
711
+ ASN1_put_eoc(&p);
712
+ }
713
+ assert(p - (unsigned char *)RSTRING_PTR(str) == total_length);
714
+ return str;
715
+ }
716
+
717
+ static VALUE ossl_asn1prim_to_der(VALUE);
718
+ static VALUE ossl_asn1cons_to_der(VALUE);
719
+ /*
720
+ * call-seq:
721
+ * asn1.to_der => DER-encoded String
722
+ *
723
+ * Encodes this ASN1Data into a DER-encoded String value. The result is
724
+ * DER-encoded except for the possibility of indefinite length forms.
725
+ * Indefinite length forms are not allowed in strict DER, so strictly speaking
726
+ * the result of such an encoding would be a BER-encoding.
727
+ */
728
+ static VALUE
729
+ ossl_asn1data_to_der(VALUE self)
730
+ {
731
+ VALUE value = ossl_asn1_get_value(self);
732
+
733
+ if (rb_obj_is_kind_of(value, rb_cArray))
734
+ return ossl_asn1cons_to_der(self);
735
+ else {
736
+ if (RTEST(ossl_asn1_get_indefinite_length(self)))
737
+ ossl_raise(eASN1Error, "indefinite length form cannot be used " \
738
+ "with primitive encoding");
739
+ return ossl_asn1prim_to_der(self);
740
+ }
741
+ }
742
+
743
+ static VALUE
744
+ int_ossl_asn1_decode0_prim(unsigned char **pp, long length, long hlen, int tag,
745
+ VALUE tc, long *num_read)
746
+ {
747
+ VALUE value, asn1data;
748
+ unsigned char *p;
749
+ long flag = 0;
750
+
751
+ p = *pp;
752
+
753
+ if(tc == sym_UNIVERSAL && tag < ossl_asn1_info_size) {
754
+ switch(tag){
755
+ case V_ASN1_EOC:
756
+ value = decode_eoc(p, hlen+length);
757
+ break;
758
+ case V_ASN1_BOOLEAN:
759
+ value = decode_bool(p, hlen+length);
760
+ break;
761
+ case V_ASN1_INTEGER:
762
+ value = decode_int(p, hlen+length);
763
+ break;
764
+ case V_ASN1_BIT_STRING:
765
+ value = decode_bstr(p, hlen+length, &flag);
766
+ break;
767
+ case V_ASN1_NULL:
768
+ value = decode_null(p, hlen+length);
769
+ break;
770
+ case V_ASN1_ENUMERATED:
771
+ value = decode_enum(p, hlen+length);
772
+ break;
773
+ case V_ASN1_OBJECT:
774
+ value = decode_obj(p, hlen+length);
775
+ break;
776
+ case V_ASN1_UTCTIME: /* FALLTHROUGH */
777
+ case V_ASN1_GENERALIZEDTIME:
778
+ value = decode_time(p, hlen+length);
779
+ break;
780
+ default:
781
+ /* use original value */
782
+ p += hlen;
783
+ value = rb_str_new((const char *)p, length);
784
+ break;
785
+ }
786
+ }
787
+ else {
788
+ p += hlen;
789
+ value = rb_str_new((const char *)p, length);
790
+ }
791
+
792
+ *pp += hlen + length;
793
+ *num_read = hlen + length;
794
+
795
+ if (tc == sym_UNIVERSAL &&
796
+ tag < ossl_asn1_info_size && ossl_asn1_info[tag].klass) {
797
+ VALUE klass = *ossl_asn1_info[tag].klass;
798
+ VALUE args[4];
799
+ args[0] = value;
800
+ args[1] = INT2NUM(tag);
801
+ args[2] = Qnil;
802
+ args[3] = tc;
803
+ asn1data = rb_obj_alloc(klass);
804
+ ossl_asn1_initialize(4, args, asn1data);
805
+ if(tag == V_ASN1_BIT_STRING){
806
+ rb_ivar_set(asn1data, sivUNUSED_BITS, LONG2NUM(flag));
807
+ }
808
+ }
809
+ else {
810
+ asn1data = rb_obj_alloc(cASN1Data);
811
+ ossl_asn1data_initialize(asn1data, value, INT2NUM(tag), tc);
812
+ }
813
+
814
+ return asn1data;
815
+ }
816
+
817
+ static VALUE
818
+ int_ossl_asn1_decode0_cons(unsigned char **pp, long max_len, long length,
819
+ long *offset, int depth, int yield, int j,
820
+ int tag, VALUE tc, long *num_read)
821
+ {
822
+ VALUE value, asn1data, ary;
823
+ int indefinite;
824
+ long available_len, off = *offset;
825
+
826
+ indefinite = (j == 0x21);
827
+ ary = rb_ary_new();
828
+
829
+ available_len = indefinite ? max_len : length;
830
+ while (available_len > 0) {
831
+ long inner_read = 0;
832
+ value = ossl_asn1_decode0(pp, available_len, &off, depth + 1, yield, &inner_read);
833
+ *num_read += inner_read;
834
+ available_len -= inner_read;
835
+
836
+ if (indefinite &&
837
+ ossl_asn1_tag(value) == V_ASN1_EOC &&
838
+ ossl_asn1_get_tag_class(value) == sym_UNIVERSAL) {
839
+ break;
840
+ }
841
+ rb_ary_push(ary, value);
842
+ }
843
+
844
+ if (tc == sym_UNIVERSAL) {
845
+ VALUE args[4];
846
+ if (tag == V_ASN1_SEQUENCE || tag == V_ASN1_SET)
847
+ asn1data = rb_obj_alloc(*ossl_asn1_info[tag].klass);
848
+ else
849
+ asn1data = rb_obj_alloc(cASN1Constructive);
850
+ args[0] = ary;
851
+ args[1] = INT2NUM(tag);
852
+ args[2] = Qnil;
853
+ args[3] = tc;
854
+ ossl_asn1_initialize(4, args, asn1data);
855
+ }
856
+ else {
857
+ asn1data = rb_obj_alloc(cASN1Data);
858
+ ossl_asn1data_initialize(asn1data, ary, INT2NUM(tag), tc);
859
+ }
860
+
861
+ if (indefinite)
862
+ ossl_asn1_set_indefinite_length(asn1data, Qtrue);
863
+ else
864
+ ossl_asn1_set_indefinite_length(asn1data, Qfalse);
865
+
866
+ *offset = off;
867
+ return asn1data;
868
+ }
869
+
870
+ static VALUE
871
+ ossl_asn1_decode0(unsigned char **pp, long length, long *offset, int depth,
872
+ int yield, long *num_read)
873
+ {
874
+ unsigned char *start, *p;
875
+ const unsigned char *p0;
876
+ long len = 0, inner_read = 0, off = *offset, hlen;
877
+ int tag, tc, j;
878
+ VALUE asn1data, tag_class;
879
+
880
+ p = *pp;
881
+ start = p;
882
+ p0 = p;
883
+ j = ASN1_get_object(&p0, &len, &tag, &tc, length);
884
+ p = (unsigned char *)p0;
885
+ if(j & 0x80) ossl_raise(eASN1Error, NULL);
886
+ if(len > length) ossl_raise(eASN1Error, "value is too short");
887
+ if((tc & V_ASN1_PRIVATE) == V_ASN1_PRIVATE)
888
+ tag_class = sym_PRIVATE;
889
+ else if((tc & V_ASN1_CONTEXT_SPECIFIC) == V_ASN1_CONTEXT_SPECIFIC)
890
+ tag_class = sym_CONTEXT_SPECIFIC;
891
+ else if((tc & V_ASN1_APPLICATION) == V_ASN1_APPLICATION)
892
+ tag_class = sym_APPLICATION;
893
+ else
894
+ tag_class = sym_UNIVERSAL;
895
+
896
+ hlen = p - start;
897
+
898
+ if(yield) {
899
+ VALUE arg = rb_ary_new();
900
+ rb_ary_push(arg, LONG2NUM(depth));
901
+ rb_ary_push(arg, LONG2NUM(*offset));
902
+ rb_ary_push(arg, LONG2NUM(hlen));
903
+ rb_ary_push(arg, LONG2NUM(len));
904
+ rb_ary_push(arg, (j & V_ASN1_CONSTRUCTED) ? Qtrue : Qfalse);
905
+ rb_ary_push(arg, ossl_asn1_class2sym(tc));
906
+ rb_ary_push(arg, INT2NUM(tag));
907
+ rb_yield(arg);
908
+ }
909
+
910
+ if(j & V_ASN1_CONSTRUCTED) {
911
+ *pp += hlen;
912
+ off += hlen;
913
+ asn1data = int_ossl_asn1_decode0_cons(pp, length - hlen, len, &off, depth, yield, j, tag, tag_class, &inner_read);
914
+ inner_read += hlen;
915
+ }
916
+ else {
917
+ if ((j & 0x01) && (len == 0))
918
+ ossl_raise(eASN1Error, "indefinite length for primitive value");
919
+ asn1data = int_ossl_asn1_decode0_prim(pp, len, hlen, tag, tag_class, &inner_read);
920
+ off += hlen + len;
921
+ }
922
+ if (num_read)
923
+ *num_read = inner_read;
924
+ if (len != 0 && inner_read != hlen + len) {
925
+ ossl_raise(eASN1Error,
926
+ "Type mismatch. Bytes read: %ld Bytes available: %ld",
927
+ inner_read, hlen + len);
928
+ }
929
+
930
+ *offset = off;
931
+ return asn1data;
932
+ }
933
+
934
+ static void
935
+ int_ossl_decode_sanity_check(long len, long read, long offset)
936
+ {
937
+ if (len != 0 && (read != len || offset != len)) {
938
+ ossl_raise(eASN1Error,
939
+ "Type mismatch. Total bytes read: %ld Bytes available: %ld Offset: %ld",
940
+ read, len, offset);
941
+ }
942
+ }
943
+
944
+ /*
945
+ * call-seq:
946
+ * OpenSSL::ASN1.traverse(asn1) -> nil
947
+ *
948
+ * If a block is given, it prints out each of the elements encountered.
949
+ * Block parameters are (in that order):
950
+ * * depth: The recursion depth, plus one with each constructed value being encountered (Integer)
951
+ * * offset: Current byte offset (Integer)
952
+ * * header length: Combined length in bytes of the Tag and Length headers. (Integer)
953
+ * * length: The overall remaining length of the entire data (Integer)
954
+ * * constructed: Whether this value is constructed or not (Boolean)
955
+ * * tag_class: Current tag class (Symbol)
956
+ * * tag: The current tag number (Integer)
957
+ *
958
+ * == Example
959
+ * der = File.binread('asn1data.der')
960
+ * OpenSSL::ASN1.traverse(der) do | depth, offset, header_len, length, constructed, tag_class, tag|
961
+ * puts "Depth: #{depth} Offset: #{offset} Length: #{length}"
962
+ * puts "Header length: #{header_len} Tag: #{tag} Tag class: #{tag_class} Constructed: #{constructed}"
963
+ * end
964
+ */
965
+ static VALUE
966
+ ossl_asn1_traverse(VALUE self, VALUE obj)
967
+ {
968
+ unsigned char *p;
969
+ VALUE tmp;
970
+ long len, read = 0, offset = 0;
971
+
972
+ obj = ossl_to_der_if_possible(obj);
973
+ tmp = rb_str_new4(StringValue(obj));
974
+ p = (unsigned char *)RSTRING_PTR(tmp);
975
+ len = RSTRING_LEN(tmp);
976
+ ossl_asn1_decode0(&p, len, &offset, 0, 1, &read);
977
+ RB_GC_GUARD(tmp);
978
+ int_ossl_decode_sanity_check(len, read, offset);
979
+ return Qnil;
980
+ }
981
+
982
+ /*
983
+ * call-seq:
984
+ * OpenSSL::ASN1.decode(der) -> ASN1Data
985
+ *
986
+ * Decodes a BER- or DER-encoded value and creates an ASN1Data instance. _der_
987
+ * may be a String or any object that features a +.to_der+ method transforming
988
+ * it into a BER-/DER-encoded String+
989
+ *
990
+ * == Example
991
+ * der = File.binread('asn1data')
992
+ * asn1 = OpenSSL::ASN1.decode(der)
993
+ */
994
+ static VALUE
995
+ ossl_asn1_decode(VALUE self, VALUE obj)
996
+ {
997
+ VALUE ret;
998
+ unsigned char *p;
999
+ VALUE tmp;
1000
+ long len, read = 0, offset = 0;
1001
+
1002
+ obj = ossl_to_der_if_possible(obj);
1003
+ tmp = rb_str_new4(StringValue(obj));
1004
+ p = (unsigned char *)RSTRING_PTR(tmp);
1005
+ len = RSTRING_LEN(tmp);
1006
+ ret = ossl_asn1_decode0(&p, len, &offset, 0, 0, &read);
1007
+ RB_GC_GUARD(tmp);
1008
+ int_ossl_decode_sanity_check(len, read, offset);
1009
+ return ret;
1010
+ }
1011
+
1012
+ /*
1013
+ * call-seq:
1014
+ * OpenSSL::ASN1.decode_all(der) -> Array of ASN1Data
1015
+ *
1016
+ * Similar to #decode with the difference that #decode expects one
1017
+ * distinct value represented in _der_. #decode_all on the contrary
1018
+ * decodes a sequence of sequential BER/DER values lined up in _der_
1019
+ * and returns them as an array.
1020
+ *
1021
+ * == Example
1022
+ * ders = File.binread('asn1data_seq')
1023
+ * asn1_ary = OpenSSL::ASN1.decode_all(ders)
1024
+ */
1025
+ static VALUE
1026
+ ossl_asn1_decode_all(VALUE self, VALUE obj)
1027
+ {
1028
+ VALUE ary, val;
1029
+ unsigned char *p;
1030
+ long len, tmp_len = 0, read = 0, offset = 0;
1031
+ VALUE tmp;
1032
+
1033
+ obj = ossl_to_der_if_possible(obj);
1034
+ tmp = rb_str_new4(StringValue(obj));
1035
+ p = (unsigned char *)RSTRING_PTR(tmp);
1036
+ len = RSTRING_LEN(tmp);
1037
+ tmp_len = len;
1038
+ ary = rb_ary_new();
1039
+ while (tmp_len > 0) {
1040
+ long tmp_read = 0;
1041
+ val = ossl_asn1_decode0(&p, tmp_len, &offset, 0, 0, &tmp_read);
1042
+ rb_ary_push(ary, val);
1043
+ read += tmp_read;
1044
+ tmp_len -= tmp_read;
1045
+ }
1046
+ RB_GC_GUARD(tmp);
1047
+ int_ossl_decode_sanity_check(len, read, offset);
1048
+ return ary;
1049
+ }
1050
+
1051
+ /*
1052
+ * call-seq:
1053
+ * OpenSSL::ASN1::Primitive.new(value [, tag, tagging, tag_class ]) => Primitive
1054
+ *
1055
+ * _value_: is mandatory.
1056
+ *
1057
+ * _tag_: optional, may be specified for tagged values. If no _tag_ is
1058
+ * specified, the UNIVERSAL tag corresponding to the Primitive sub-class
1059
+ * is used by default.
1060
+ *
1061
+ * _tagging_: may be used as an encoding hint to encode a value either
1062
+ * explicitly or implicitly, see ASN1 for possible values.
1063
+ *
1064
+ * _tag_class_: if _tag_ and _tagging_ are +nil+ then this is set to
1065
+ * +:UNIVERSAL+ by default. If either _tag_ or _tagging_ are set then
1066
+ * +:CONTEXT_SPECIFIC+ is used as the default. For possible values please
1067
+ * cf. ASN1.
1068
+ *
1069
+ * == Example
1070
+ * int = OpenSSL::ASN1::Integer.new(42)
1071
+ * zero_tagged_int = OpenSSL::ASN1::Integer.new(42, 0, :IMPLICIT)
1072
+ * private_explicit_zero_tagged_int = OpenSSL::ASN1::Integer.new(42, 0, :EXPLICIT, :PRIVATE)
1073
+ */
1074
+ static VALUE
1075
+ ossl_asn1_initialize(int argc, VALUE *argv, VALUE self)
1076
+ {
1077
+ VALUE value, tag, tagging, tag_class;
1078
+ int default_tag;
1079
+
1080
+ rb_scan_args(argc, argv, "13", &value, &tag, &tagging, &tag_class);
1081
+ default_tag = ossl_asn1_default_tag(self);
1082
+
1083
+ if (default_tag == -1 || argc > 1) {
1084
+ if(NIL_P(tag))
1085
+ ossl_raise(eASN1Error, "must specify tag number");
1086
+ if(!NIL_P(tagging) && !SYMBOL_P(tagging))
1087
+ ossl_raise(eASN1Error, "invalid tagging method");
1088
+ if(NIL_P(tag_class)) {
1089
+ if (NIL_P(tagging))
1090
+ tag_class = sym_UNIVERSAL;
1091
+ else
1092
+ tag_class = sym_CONTEXT_SPECIFIC;
1093
+ }
1094
+ if(!SYMBOL_P(tag_class))
1095
+ ossl_raise(eASN1Error, "invalid tag class");
1096
+ }
1097
+ else{
1098
+ tag = INT2NUM(default_tag);
1099
+ tagging = Qnil;
1100
+ tag_class = sym_UNIVERSAL;
1101
+ }
1102
+ ossl_asn1_set_tag(self, tag);
1103
+ ossl_asn1_set_value(self, value);
1104
+ ossl_asn1_set_tagging(self, tagging);
1105
+ ossl_asn1_set_tag_class(self, tag_class);
1106
+ ossl_asn1_set_indefinite_length(self, Qfalse);
1107
+ if (default_tag == V_ASN1_BIT_STRING)
1108
+ rb_ivar_set(self, sivUNUSED_BITS, INT2FIX(0));
1109
+
1110
+ return self;
1111
+ }
1112
+
1113
+ static VALUE
1114
+ ossl_asn1eoc_initialize(VALUE self) {
1115
+ VALUE tag, tagging, tag_class, value;
1116
+ tag = INT2FIX(0);
1117
+ tagging = Qnil;
1118
+ tag_class = sym_UNIVERSAL;
1119
+ value = rb_str_new("", 0);
1120
+ ossl_asn1_set_tag(self, tag);
1121
+ ossl_asn1_set_value(self, value);
1122
+ ossl_asn1_set_tagging(self, tagging);
1123
+ ossl_asn1_set_tag_class(self, tag_class);
1124
+ ossl_asn1_set_indefinite_length(self, Qfalse);
1125
+ return self;
1126
+ }
1127
+
1128
+ static VALUE
1129
+ ossl_asn1eoc_to_der(VALUE self)
1130
+ {
1131
+ return rb_str_new("\0\0", 2);
1132
+ }
1133
+
1134
+ /*
1135
+ * call-seq:
1136
+ * asn1.to_der => DER-encoded String
1137
+ *
1138
+ * See ASN1Data#to_der for details.
1139
+ */
1140
+ static VALUE
1141
+ ossl_asn1prim_to_der(VALUE self)
1142
+ {
1143
+ ASN1_TYPE *asn1;
1144
+ long alllen, bodylen;
1145
+ unsigned char *p0, *p1;
1146
+ int j, tag, tc, state;
1147
+ VALUE str;
1148
+
1149
+ if (ossl_asn1_default_tag(self) == -1) {
1150
+ str = ossl_asn1_get_value(self);
1151
+ return to_der_internal(self, 0, 0, StringValue(str));
1152
+ }
1153
+
1154
+ asn1 = ossl_asn1_get_asn1type(self);
1155
+ alllen = i2d_ASN1_TYPE(asn1, NULL);
1156
+ if (alllen < 0) {
1157
+ ASN1_TYPE_free(asn1);
1158
+ ossl_raise(eASN1Error, "i2d_ASN1_TYPE");
1159
+ }
1160
+ str = ossl_str_new(NULL, alllen, &state);
1161
+ if (state) {
1162
+ ASN1_TYPE_free(asn1);
1163
+ rb_jump_tag(state);
1164
+ }
1165
+ p0 = p1 = (unsigned char *)RSTRING_PTR(str);
1166
+ i2d_ASN1_TYPE(asn1, &p0);
1167
+ ASN1_TYPE_free(asn1);
1168
+ assert(p0 - p1 == alllen);
1169
+
1170
+ /* Strip header since to_der_internal() wants only the payload */
1171
+ j = ASN1_get_object((const unsigned char **)&p1, &bodylen, &tag, &tc, alllen);
1172
+ if (j & 0x80)
1173
+ ossl_raise(eASN1Error, "ASN1_get_object"); /* should not happen */
1174
+
1175
+ return to_der_internal(self, 0, 0, rb_str_drop_bytes(str, alllen - bodylen));
1176
+ }
1177
+
1178
+ /*
1179
+ * call-seq:
1180
+ * asn1.to_der => DER-encoded String
1181
+ *
1182
+ * See ASN1Data#to_der for details.
1183
+ */
1184
+ static VALUE
1185
+ ossl_asn1cons_to_der(VALUE self)
1186
+ {
1187
+ VALUE ary, str;
1188
+ long i;
1189
+ int indef_len;
1190
+
1191
+ indef_len = RTEST(ossl_asn1_get_indefinite_length(self));
1192
+ ary = rb_convert_type(ossl_asn1_get_value(self), T_ARRAY, "Array", "to_a");
1193
+ str = rb_str_new(NULL, 0);
1194
+ for (i = 0; i < RARRAY_LEN(ary); i++) {
1195
+ VALUE item = RARRAY_AREF(ary, i);
1196
+
1197
+ if (indef_len && rb_obj_is_kind_of(item, cASN1EndOfContent)) {
1198
+ if (i != RARRAY_LEN(ary) - 1)
1199
+ ossl_raise(eASN1Error, "illegal EOC octets in value");
1200
+
1201
+ /*
1202
+ * EOC is not really part of the content, but we required to add one
1203
+ * at the end in the past.
1204
+ */
1205
+ break;
1206
+ }
1207
+
1208
+ item = ossl_to_der_if_possible(item);
1209
+ StringValue(item);
1210
+ rb_str_append(str, item);
1211
+ }
1212
+
1213
+ return to_der_internal(self, 1, indef_len, str);
1214
+ }
1215
+
1216
+ /*
1217
+ * call-seq:
1218
+ * asn1_ary.each { |asn1| block } => asn1_ary
1219
+ *
1220
+ * Calls the given block once for each element in self, passing that element
1221
+ * as parameter _asn1_. If no block is given, an enumerator is returned
1222
+ * instead.
1223
+ *
1224
+ * == Example
1225
+ * asn1_ary.each do |asn1|
1226
+ * puts asn1
1227
+ * end
1228
+ */
1229
+ static VALUE
1230
+ ossl_asn1cons_each(VALUE self)
1231
+ {
1232
+ rb_block_call(ossl_asn1_get_value(self), id_each, 0, 0, 0, 0);
1233
+
1234
+ return self;
1235
+ }
1236
+
1237
+ /*
1238
+ * call-seq:
1239
+ * OpenSSL::ASN1::ObjectId.register(object_id, short_name, long_name)
1240
+ *
1241
+ * This adds a new ObjectId to the internal tables. Where _object_id_ is the
1242
+ * numerical form, _short_name_ is the short name, and _long_name_ is the long
1243
+ * name.
1244
+ *
1245
+ * Returns +true+ if successful. Raises an OpenSSL::ASN1::ASN1Error if it fails.
1246
+ *
1247
+ */
1248
+ static VALUE
1249
+ ossl_asn1obj_s_register(VALUE self, VALUE oid, VALUE sn, VALUE ln)
1250
+ {
1251
+ StringValueCStr(oid);
1252
+ StringValueCStr(sn);
1253
+ StringValueCStr(ln);
1254
+
1255
+ if(!OBJ_create(RSTRING_PTR(oid), RSTRING_PTR(sn), RSTRING_PTR(ln)))
1256
+ ossl_raise(eASN1Error, NULL);
1257
+
1258
+ return Qtrue;
1259
+ }
1260
+
1261
+ /*
1262
+ * call-seq:
1263
+ * oid.sn -> string
1264
+ * oid.short_name -> string
1265
+ *
1266
+ * The short name of the ObjectId, as defined in <openssl/objects.h>.
1267
+ */
1268
+ static VALUE
1269
+ ossl_asn1obj_get_sn(VALUE self)
1270
+ {
1271
+ VALUE val, ret = Qnil;
1272
+ int nid;
1273
+
1274
+ val = ossl_asn1_get_value(self);
1275
+ if ((nid = OBJ_txt2nid(StringValueCStr(val))) != NID_undef)
1276
+ ret = rb_str_new2(OBJ_nid2sn(nid));
1277
+
1278
+ return ret;
1279
+ }
1280
+
1281
+ /*
1282
+ * call-seq:
1283
+ * oid.ln -> string
1284
+ * oid.long_name -> string
1285
+ *
1286
+ * The long name of the ObjectId, as defined in <openssl/objects.h>.
1287
+ */
1288
+ static VALUE
1289
+ ossl_asn1obj_get_ln(VALUE self)
1290
+ {
1291
+ VALUE val, ret = Qnil;
1292
+ int nid;
1293
+
1294
+ val = ossl_asn1_get_value(self);
1295
+ if ((nid = OBJ_txt2nid(StringValueCStr(val))) != NID_undef)
1296
+ ret = rb_str_new2(OBJ_nid2ln(nid));
1297
+
1298
+ return ret;
1299
+ }
1300
+
1301
+ /*
1302
+ * call-seq:
1303
+ * oid == other_oid => true or false
1304
+ *
1305
+ * Returns +true+ if _other_oid_ is the same as _oid_
1306
+ */
1307
+ static VALUE
1308
+ ossl_asn1obj_eq(VALUE self, VALUE other)
1309
+ {
1310
+ VALUE valSelf, valOther;
1311
+ int nidSelf, nidOther;
1312
+
1313
+ valSelf = ossl_asn1_get_value(self);
1314
+ valOther = ossl_asn1_get_value(other);
1315
+
1316
+ if ((nidSelf = OBJ_txt2nid(StringValueCStr(valSelf))) == NID_undef)
1317
+ ossl_raise(eASN1Error, "OBJ_txt2nid");
1318
+
1319
+ if ((nidOther = OBJ_txt2nid(StringValueCStr(valOther))) == NID_undef)
1320
+ ossl_raise(eASN1Error, "OBJ_txt2nid");
1321
+
1322
+ return nidSelf == nidOther ? Qtrue : Qfalse;
1323
+ }
1324
+
1325
+ static VALUE
1326
+ asn1obj_get_oid_i(VALUE vobj)
1327
+ {
1328
+ ASN1_OBJECT *a1obj = (void *)vobj;
1329
+ VALUE str;
1330
+ int len;
1331
+
1332
+ str = rb_usascii_str_new(NULL, 127);
1333
+ len = OBJ_obj2txt(RSTRING_PTR(str), RSTRING_LENINT(str), a1obj, 1);
1334
+ if (len <= 0 || len == INT_MAX)
1335
+ ossl_raise(eASN1Error, "OBJ_obj2txt");
1336
+ if (len > RSTRING_LEN(str)) {
1337
+ /* +1 is for the \0 terminator added by OBJ_obj2txt() */
1338
+ rb_str_resize(str, len + 1);
1339
+ len = OBJ_obj2txt(RSTRING_PTR(str), len + 1, a1obj, 1);
1340
+ if (len <= 0)
1341
+ ossl_raise(eASN1Error, "OBJ_obj2txt");
1342
+ }
1343
+ rb_str_set_len(str, len);
1344
+ return str;
1345
+ }
1346
+
1347
+ /*
1348
+ * call-seq:
1349
+ * oid.oid -> string
1350
+ *
1351
+ * Returns a String representing the Object Identifier in the dot notation,
1352
+ * e.g. "1.2.3.4.5"
1353
+ */
1354
+ static VALUE
1355
+ ossl_asn1obj_get_oid(VALUE self)
1356
+ {
1357
+ VALUE str;
1358
+ ASN1_OBJECT *a1obj;
1359
+ int state;
1360
+
1361
+ a1obj = obj_to_asn1obj(ossl_asn1_get_value(self));
1362
+ str = rb_protect(asn1obj_get_oid_i, (VALUE)a1obj, &state);
1363
+ ASN1_OBJECT_free(a1obj);
1364
+ if (state)
1365
+ rb_jump_tag(state);
1366
+ return str;
1367
+ }
1368
+
1369
+ #define OSSL_ASN1_IMPL_FACTORY_METHOD(klass) \
1370
+ static VALUE ossl_asn1_##klass(int argc, VALUE *argv, VALUE self)\
1371
+ { return rb_funcall3(cASN1##klass, rb_intern("new"), argc, argv); }
1372
+
1373
+ OSSL_ASN1_IMPL_FACTORY_METHOD(Boolean)
1374
+ OSSL_ASN1_IMPL_FACTORY_METHOD(Integer)
1375
+ OSSL_ASN1_IMPL_FACTORY_METHOD(Enumerated)
1376
+ OSSL_ASN1_IMPL_FACTORY_METHOD(BitString)
1377
+ OSSL_ASN1_IMPL_FACTORY_METHOD(OctetString)
1378
+ OSSL_ASN1_IMPL_FACTORY_METHOD(UTF8String)
1379
+ OSSL_ASN1_IMPL_FACTORY_METHOD(NumericString)
1380
+ OSSL_ASN1_IMPL_FACTORY_METHOD(PrintableString)
1381
+ OSSL_ASN1_IMPL_FACTORY_METHOD(T61String)
1382
+ OSSL_ASN1_IMPL_FACTORY_METHOD(VideotexString)
1383
+ OSSL_ASN1_IMPL_FACTORY_METHOD(IA5String)
1384
+ OSSL_ASN1_IMPL_FACTORY_METHOD(GraphicString)
1385
+ OSSL_ASN1_IMPL_FACTORY_METHOD(ISO64String)
1386
+ OSSL_ASN1_IMPL_FACTORY_METHOD(GeneralString)
1387
+ OSSL_ASN1_IMPL_FACTORY_METHOD(UniversalString)
1388
+ OSSL_ASN1_IMPL_FACTORY_METHOD(BMPString)
1389
+ OSSL_ASN1_IMPL_FACTORY_METHOD(Null)
1390
+ OSSL_ASN1_IMPL_FACTORY_METHOD(ObjectId)
1391
+ OSSL_ASN1_IMPL_FACTORY_METHOD(UTCTime)
1392
+ OSSL_ASN1_IMPL_FACTORY_METHOD(GeneralizedTime)
1393
+ OSSL_ASN1_IMPL_FACTORY_METHOD(Sequence)
1394
+ OSSL_ASN1_IMPL_FACTORY_METHOD(Set)
1395
+ OSSL_ASN1_IMPL_FACTORY_METHOD(EndOfContent)
1396
+
1397
+ void
1398
+ Init_ossl_asn1(void)
1399
+ {
1400
+ #undef rb_intern
1401
+ VALUE ary;
1402
+ int i;
1403
+
1404
+ #if 0
1405
+ mOSSL = rb_define_module("OpenSSL");
1406
+ eOSSLError = rb_define_class_under(mOSSL, "OpenSSLError", rb_eStandardError);
1407
+ #endif
1408
+
1409
+ sym_UNIVERSAL = ID2SYM(rb_intern_const("UNIVERSAL"));
1410
+ sym_CONTEXT_SPECIFIC = ID2SYM(rb_intern_const("CONTEXT_SPECIFIC"));
1411
+ sym_APPLICATION = ID2SYM(rb_intern_const("APPLICATION"));
1412
+ sym_PRIVATE = ID2SYM(rb_intern_const("PRIVATE"));
1413
+ sym_EXPLICIT = ID2SYM(rb_intern_const("EXPLICIT"));
1414
+ sym_IMPLICIT = ID2SYM(rb_intern_const("IMPLICIT"));
1415
+
1416
+ sivVALUE = rb_intern("@value");
1417
+ sivTAG = rb_intern("@tag");
1418
+ sivTAGGING = rb_intern("@tagging");
1419
+ sivTAG_CLASS = rb_intern("@tag_class");
1420
+ sivINDEFINITE_LENGTH = rb_intern("@indefinite_length");
1421
+ sivUNUSED_BITS = rb_intern("@unused_bits");
1422
+
1423
+ /*
1424
+ * Document-module: OpenSSL::ASN1
1425
+ *
1426
+ * Abstract Syntax Notation One (or ASN.1) is a notation syntax to
1427
+ * describe data structures and is defined in ITU-T X.680. ASN.1 itself
1428
+ * does not mandate any encoding or parsing rules, but usually ASN.1 data
1429
+ * structures are encoded using the Distinguished Encoding Rules (DER) or
1430
+ * less often the Basic Encoding Rules (BER) described in ITU-T X.690. DER
1431
+ * and BER encodings are binary Tag-Length-Value (TLV) encodings that are
1432
+ * quite concise compared to other popular data description formats such
1433
+ * as XML, JSON etc.
1434
+ * ASN.1 data structures are very common in cryptographic applications,
1435
+ * e.g. X.509 public key certificates or certificate revocation lists
1436
+ * (CRLs) are all defined in ASN.1 and DER-encoded. ASN.1, DER and BER are
1437
+ * the building blocks of applied cryptography.
1438
+ * The ASN1 module provides the necessary classes that allow generation
1439
+ * of ASN.1 data structures and the methods to encode them using a DER
1440
+ * encoding. The decode method allows parsing arbitrary BER-/DER-encoded
1441
+ * data to a Ruby object that can then be modified and re-encoded at will.
1442
+ *
1443
+ * == ASN.1 class hierarchy
1444
+ *
1445
+ * The base class representing ASN.1 structures is ASN1Data. ASN1Data offers
1446
+ * attributes to read and set the _tag_, the _tag_class_ and finally the
1447
+ * _value_ of a particular ASN.1 item. Upon parsing, any tagged values
1448
+ * (implicit or explicit) will be represented by ASN1Data instances because
1449
+ * their "real type" can only be determined using out-of-band information
1450
+ * from the ASN.1 type declaration. Since this information is normally
1451
+ * known when encoding a type, all sub-classes of ASN1Data offer an
1452
+ * additional attribute _tagging_ that allows to encode a value implicitly
1453
+ * (+:IMPLICIT+) or explicitly (+:EXPLICIT+).
1454
+ *
1455
+ * === Constructive
1456
+ *
1457
+ * Constructive is, as its name implies, the base class for all
1458
+ * constructed encodings, i.e. those that consist of several values,
1459
+ * opposed to "primitive" encodings with just one single value. The value of
1460
+ * an Constructive is always an Array.
1461
+ *
1462
+ * ==== ASN1::Set and ASN1::Sequence
1463
+ *
1464
+ * The most common constructive encodings are SETs and SEQUENCEs, which is
1465
+ * why there are two sub-classes of Constructive representing each of
1466
+ * them.
1467
+ *
1468
+ * === Primitive
1469
+ *
1470
+ * This is the super class of all primitive values. Primitive
1471
+ * itself is not used when parsing ASN.1 data, all values are either
1472
+ * instances of a corresponding sub-class of Primitive or they are
1473
+ * instances of ASN1Data if the value was tagged implicitly or explicitly.
1474
+ * Please cf. Primitive documentation for details on sub-classes and
1475
+ * their respective mappings of ASN.1 data types to Ruby objects.
1476
+ *
1477
+ * == Possible values for _tagging_
1478
+ *
1479
+ * When constructing an ASN1Data object the ASN.1 type definition may
1480
+ * require certain elements to be either implicitly or explicitly tagged.
1481
+ * This can be achieved by setting the _tagging_ attribute manually for
1482
+ * sub-classes of ASN1Data. Use the symbol +:IMPLICIT+ for implicit
1483
+ * tagging and +:EXPLICIT+ if the element requires explicit tagging.
1484
+ *
1485
+ * == Possible values for _tag_class_
1486
+ *
1487
+ * It is possible to create arbitrary ASN1Data objects that also support
1488
+ * a PRIVATE or APPLICATION tag class. Possible values for the _tag_class_
1489
+ * attribute are:
1490
+ * * +:UNIVERSAL+ (the default for untagged values)
1491
+ * * +:CONTEXT_SPECIFIC+ (the default for tagged values)
1492
+ * * +:APPLICATION+
1493
+ * * +:PRIVATE+
1494
+ *
1495
+ * == Tag constants
1496
+ *
1497
+ * There is a constant defined for each universal tag:
1498
+ * * OpenSSL::ASN1::EOC (0)
1499
+ * * OpenSSL::ASN1::BOOLEAN (1)
1500
+ * * OpenSSL::ASN1::INTEGER (2)
1501
+ * * OpenSSL::ASN1::BIT_STRING (3)
1502
+ * * OpenSSL::ASN1::OCTET_STRING (4)
1503
+ * * OpenSSL::ASN1::NULL (5)
1504
+ * * OpenSSL::ASN1::OBJECT (6)
1505
+ * * OpenSSL::ASN1::ENUMERATED (10)
1506
+ * * OpenSSL::ASN1::UTF8STRING (12)
1507
+ * * OpenSSL::ASN1::SEQUENCE (16)
1508
+ * * OpenSSL::ASN1::SET (17)
1509
+ * * OpenSSL::ASN1::NUMERICSTRING (18)
1510
+ * * OpenSSL::ASN1::PRINTABLESTRING (19)
1511
+ * * OpenSSL::ASN1::T61STRING (20)
1512
+ * * OpenSSL::ASN1::VIDEOTEXSTRING (21)
1513
+ * * OpenSSL::ASN1::IA5STRING (22)
1514
+ * * OpenSSL::ASN1::UTCTIME (23)
1515
+ * * OpenSSL::ASN1::GENERALIZEDTIME (24)
1516
+ * * OpenSSL::ASN1::GRAPHICSTRING (25)
1517
+ * * OpenSSL::ASN1::ISO64STRING (26)
1518
+ * * OpenSSL::ASN1::GENERALSTRING (27)
1519
+ * * OpenSSL::ASN1::UNIVERSALSTRING (28)
1520
+ * * OpenSSL::ASN1::BMPSTRING (30)
1521
+ *
1522
+ * == UNIVERSAL_TAG_NAME constant
1523
+ *
1524
+ * An Array that stores the name of a given tag number. These names are
1525
+ * the same as the name of the tag constant that is additionally defined,
1526
+ * e.g. <tt>UNIVERSAL_TAG_NAME[2] = "INTEGER"</tt> and <tt>OpenSSL::ASN1::INTEGER = 2</tt>.
1527
+ *
1528
+ * == Example usage
1529
+ *
1530
+ * === Decoding and viewing a DER-encoded file
1531
+ * require 'openssl'
1532
+ * require 'pp'
1533
+ * der = File.binread('data.der')
1534
+ * asn1 = OpenSSL::ASN1.decode(der)
1535
+ * pp der
1536
+ *
1537
+ * === Creating an ASN.1 structure and DER-encoding it
1538
+ * require 'openssl'
1539
+ * version = OpenSSL::ASN1::Integer.new(1)
1540
+ * # Explicitly 0-tagged implies context-specific tag class
1541
+ * serial = OpenSSL::ASN1::Integer.new(12345, 0, :EXPLICIT, :CONTEXT_SPECIFIC)
1542
+ * name = OpenSSL::ASN1::PrintableString.new('Data 1')
1543
+ * sequence = OpenSSL::ASN1::Sequence.new( [ version, serial, name ] )
1544
+ * der = sequence.to_der
1545
+ */
1546
+ mASN1 = rb_define_module_under(mOSSL, "ASN1");
1547
+
1548
+ /* Document-class: OpenSSL::ASN1::ASN1Error
1549
+ *
1550
+ * Generic error class for all errors raised in ASN1 and any of the
1551
+ * classes defined in it.
1552
+ */
1553
+ eASN1Error = rb_define_class_under(mASN1, "ASN1Error", eOSSLError);
1554
+ rb_define_module_function(mASN1, "traverse", ossl_asn1_traverse, 1);
1555
+ rb_define_module_function(mASN1, "decode", ossl_asn1_decode, 1);
1556
+ rb_define_module_function(mASN1, "decode_all", ossl_asn1_decode_all, 1);
1557
+ ary = rb_ary_new();
1558
+
1559
+ /*
1560
+ * Array storing tag names at the tag's index.
1561
+ */
1562
+ rb_define_const(mASN1, "UNIVERSAL_TAG_NAME", ary);
1563
+ for(i = 0; i < ossl_asn1_info_size; i++){
1564
+ if(ossl_asn1_info[i].name[0] == '[') continue;
1565
+ rb_define_const(mASN1, ossl_asn1_info[i].name, INT2NUM(i));
1566
+ rb_ary_store(ary, i, rb_str_new2(ossl_asn1_info[i].name));
1567
+ }
1568
+
1569
+ /* Document-class: OpenSSL::ASN1::ASN1Data
1570
+ *
1571
+ * The top-level class representing any ASN.1 object. When parsed by
1572
+ * ASN1.decode, tagged values are always represented by an instance
1573
+ * of ASN1Data.
1574
+ *
1575
+ * == The role of ASN1Data for parsing tagged values
1576
+ *
1577
+ * When encoding an ASN.1 type it is inherently clear what original
1578
+ * type (e.g. INTEGER, OCTET STRING etc.) this value has, regardless
1579
+ * of its tagging.
1580
+ * But opposed to the time an ASN.1 type is to be encoded, when parsing
1581
+ * them it is not possible to deduce the "real type" of tagged
1582
+ * values. This is why tagged values are generally parsed into ASN1Data
1583
+ * instances, but with a different outcome for implicit and explicit
1584
+ * tagging.
1585
+ *
1586
+ * === Example of a parsed implicitly tagged value
1587
+ *
1588
+ * An implicitly 1-tagged INTEGER value will be parsed as an
1589
+ * ASN1Data with
1590
+ * * _tag_ equal to 1
1591
+ * * _tag_class_ equal to +:CONTEXT_SPECIFIC+
1592
+ * * _value_ equal to a String that carries the raw encoding
1593
+ * of the INTEGER.
1594
+ * This implies that a subsequent decoding step is required to
1595
+ * completely decode implicitly tagged values.
1596
+ *
1597
+ * === Example of a parsed explicitly tagged value
1598
+ *
1599
+ * An explicitly 1-tagged INTEGER value will be parsed as an
1600
+ * ASN1Data with
1601
+ * * _tag_ equal to 1
1602
+ * * _tag_class_ equal to +:CONTEXT_SPECIFIC+
1603
+ * * _value_ equal to an Array with one single element, an
1604
+ * instance of OpenSSL::ASN1::Integer, i.e. the inner element
1605
+ * is the non-tagged primitive value, and the tagging is represented
1606
+ * in the outer ASN1Data
1607
+ *
1608
+ * == Example - Decoding an implicitly tagged INTEGER
1609
+ * int = OpenSSL::ASN1::Integer.new(1, 0, :IMPLICIT) # implicit 0-tagged
1610
+ * seq = OpenSSL::ASN1::Sequence.new( [int] )
1611
+ * der = seq.to_der
1612
+ * asn1 = OpenSSL::ASN1.decode(der)
1613
+ * # pp asn1 => #<OpenSSL::ASN1::Sequence:0x87326e0
1614
+ * # @indefinite_length=false,
1615
+ * # @tag=16,
1616
+ * # @tag_class=:UNIVERSAL,
1617
+ * # @tagging=nil,
1618
+ * # @value=
1619
+ * # [#<OpenSSL::ASN1::ASN1Data:0x87326f4
1620
+ * # @indefinite_length=false,
1621
+ * # @tag=0,
1622
+ * # @tag_class=:CONTEXT_SPECIFIC,
1623
+ * # @value="\x01">]>
1624
+ * raw_int = asn1.value[0]
1625
+ * # manually rewrite tag and tag class to make it an UNIVERSAL value
1626
+ * raw_int.tag = OpenSSL::ASN1::INTEGER
1627
+ * raw_int.tag_class = :UNIVERSAL
1628
+ * int2 = OpenSSL::ASN1.decode(raw_int)
1629
+ * puts int2.value # => 1
1630
+ *
1631
+ * == Example - Decoding an explicitly tagged INTEGER
1632
+ * int = OpenSSL::ASN1::Integer.new(1, 0, :EXPLICIT) # explicit 0-tagged
1633
+ * seq = OpenSSL::ASN1::Sequence.new( [int] )
1634
+ * der = seq.to_der
1635
+ * asn1 = OpenSSL::ASN1.decode(der)
1636
+ * # pp asn1 => #<OpenSSL::ASN1::Sequence:0x87326e0
1637
+ * # @indefinite_length=false,
1638
+ * # @tag=16,
1639
+ * # @tag_class=:UNIVERSAL,
1640
+ * # @tagging=nil,
1641
+ * # @value=
1642
+ * # [#<OpenSSL::ASN1::ASN1Data:0x87326f4
1643
+ * # @indefinite_length=false,
1644
+ * # @tag=0,
1645
+ * # @tag_class=:CONTEXT_SPECIFIC,
1646
+ * # @value=
1647
+ * # [#<OpenSSL::ASN1::Integer:0x85bf308
1648
+ * # @indefinite_length=false,
1649
+ * # @tag=2,
1650
+ * # @tag_class=:UNIVERSAL
1651
+ * # @tagging=nil,
1652
+ * # @value=1>]>]>
1653
+ * int2 = asn1.value[0].value[0]
1654
+ * puts int2.value # => 1
1655
+ */
1656
+ cASN1Data = rb_define_class_under(mASN1, "ASN1Data", rb_cObject);
1657
+ /*
1658
+ * Carries the value of a ASN.1 type.
1659
+ * Please confer Constructive and Primitive for the mappings between
1660
+ * ASN.1 data types and Ruby classes.
1661
+ */
1662
+ rb_attr(cASN1Data, rb_intern("value"), 1, 1, 0);
1663
+ /*
1664
+ * An Integer representing the tag number of this ASN1Data. Never +nil+.
1665
+ */
1666
+ rb_attr(cASN1Data, rb_intern("tag"), 1, 1, 0);
1667
+ /*
1668
+ * A Symbol representing the tag class of this ASN1Data. Never +nil+.
1669
+ * See ASN1Data for possible values.
1670
+ */
1671
+ rb_attr(cASN1Data, rb_intern("tag_class"), 1, 1, 0);
1672
+ /*
1673
+ * Never +nil+. A boolean value indicating whether the encoding uses
1674
+ * indefinite length (in the case of parsing) or whether an indefinite
1675
+ * length form shall be used (in the encoding case).
1676
+ * In DER, every value uses definite length form. But in scenarios where
1677
+ * large amounts of data need to be transferred it might be desirable to
1678
+ * have some kind of streaming support available.
1679
+ * For example, huge OCTET STRINGs are preferably sent in smaller-sized
1680
+ * chunks, each at a time.
1681
+ * This is possible in BER by setting the length bytes of an encoding
1682
+ * to zero and by this indicating that the following value will be
1683
+ * sent in chunks. Indefinite length encodings are always constructed.
1684
+ * The end of such a stream of chunks is indicated by sending a EOC
1685
+ * (End of Content) tag. SETs and SEQUENCEs may use an indefinite length
1686
+ * encoding, but also primitive types such as e.g. OCTET STRINGS or
1687
+ * BIT STRINGS may leverage this functionality (cf. ITU-T X.690).
1688
+ */
1689
+ rb_attr(cASN1Data, rb_intern("indefinite_length"), 1, 1, 0);
1690
+ rb_define_alias(cASN1Data, "infinite_length", "indefinite_length");
1691
+ rb_define_alias(cASN1Data, "infinite_length=", "indefinite_length=");
1692
+ rb_define_method(cASN1Data, "initialize", ossl_asn1data_initialize, 3);
1693
+ rb_define_method(cASN1Data, "to_der", ossl_asn1data_to_der, 0);
1694
+
1695
+ /* Document-class: OpenSSL::ASN1::Primitive
1696
+ *
1697
+ * The parent class for all primitive encodings. Attributes are the same as
1698
+ * for ASN1Data, with the addition of _tagging_.
1699
+ * Primitive values can never be encoded with indefinite length form, thus
1700
+ * it is not possible to set the _indefinite_length_ attribute for Primitive
1701
+ * and its sub-classes.
1702
+ *
1703
+ * == Primitive sub-classes and their mapping to Ruby classes
1704
+ * * OpenSSL::ASN1::EndOfContent <=> _value_ is always +nil+
1705
+ * * OpenSSL::ASN1::Boolean <=> _value_ is +true+ or +false+
1706
+ * * OpenSSL::ASN1::Integer <=> _value_ is an OpenSSL::BN
1707
+ * * OpenSSL::ASN1::BitString <=> _value_ is a String
1708
+ * * OpenSSL::ASN1::OctetString <=> _value_ is a String
1709
+ * * OpenSSL::ASN1::Null <=> _value_ is always +nil+
1710
+ * * OpenSSL::ASN1::Object <=> _value_ is a String
1711
+ * * OpenSSL::ASN1::Enumerated <=> _value_ is an OpenSSL::BN
1712
+ * * OpenSSL::ASN1::UTF8String <=> _value_ is a String
1713
+ * * OpenSSL::ASN1::NumericString <=> _value_ is a String
1714
+ * * OpenSSL::ASN1::PrintableString <=> _value_ is a String
1715
+ * * OpenSSL::ASN1::T61String <=> _value_ is a String
1716
+ * * OpenSSL::ASN1::VideotexString <=> _value_ is a String
1717
+ * * OpenSSL::ASN1::IA5String <=> _value_ is a String
1718
+ * * OpenSSL::ASN1::UTCTime <=> _value_ is a Time
1719
+ * * OpenSSL::ASN1::GeneralizedTime <=> _value_ is a Time
1720
+ * * OpenSSL::ASN1::GraphicString <=> _value_ is a String
1721
+ * * OpenSSL::ASN1::ISO64String <=> _value_ is a String
1722
+ * * OpenSSL::ASN1::GeneralString <=> _value_ is a String
1723
+ * * OpenSSL::ASN1::UniversalString <=> _value_ is a String
1724
+ * * OpenSSL::ASN1::BMPString <=> _value_ is a String
1725
+ *
1726
+ * == OpenSSL::ASN1::BitString
1727
+ *
1728
+ * === Additional attributes
1729
+ * _unused_bits_: if the underlying BIT STRING's
1730
+ * length is a multiple of 8 then _unused_bits_ is 0. Otherwise
1731
+ * _unused_bits_ indicates the number of bits that are to be ignored in
1732
+ * the final octet of the BitString's _value_.
1733
+ *
1734
+ * == OpenSSL::ASN1::ObjectId
1735
+ *
1736
+ * NOTE: While OpenSSL::ASN1::ObjectId.new will allocate a new ObjectId,
1737
+ * it is not typically allocated this way, but rather that are received from
1738
+ * parsed ASN1 encodings.
1739
+ *
1740
+ * === Additional attributes
1741
+ * * _sn_: the short name as defined in <openssl/objects.h>.
1742
+ * * _ln_: the long name as defined in <openssl/objects.h>.
1743
+ * * _oid_: the object identifier as a String, e.g. "1.2.3.4.5"
1744
+ * * _short_name_: alias for _sn_.
1745
+ * * _long_name_: alias for _ln_.
1746
+ *
1747
+ * == Examples
1748
+ * With the Exception of OpenSSL::ASN1::EndOfContent, each Primitive class
1749
+ * constructor takes at least one parameter, the _value_.
1750
+ *
1751
+ * === Creating EndOfContent
1752
+ * eoc = OpenSSL::ASN1::EndOfContent.new
1753
+ *
1754
+ * === Creating any other Primitive
1755
+ * prim = <class>.new(value) # <class> being one of the sub-classes except EndOfContent
1756
+ * prim_zero_tagged_implicit = <class>.new(value, 0, :IMPLICIT)
1757
+ * prim_zero_tagged_explicit = <class>.new(value, 0, :EXPLICIT)
1758
+ */
1759
+ cASN1Primitive = rb_define_class_under(mASN1, "Primitive", cASN1Data);
1760
+ /*
1761
+ * May be used as a hint for encoding a value either implicitly or
1762
+ * explicitly by setting it either to +:IMPLICIT+ or to +:EXPLICIT+.
1763
+ * _tagging_ is not set when a ASN.1 structure is parsed using
1764
+ * OpenSSL::ASN1.decode.
1765
+ */
1766
+ rb_attr(cASN1Primitive, rb_intern("tagging"), 1, 1, Qtrue);
1767
+ rb_undef_method(cASN1Primitive, "indefinite_length=");
1768
+ rb_undef_method(cASN1Primitive, "infinite_length=");
1769
+ rb_define_method(cASN1Primitive, "initialize", ossl_asn1_initialize, -1);
1770
+ rb_define_method(cASN1Primitive, "to_der", ossl_asn1prim_to_der, 0);
1771
+
1772
+ /* Document-class: OpenSSL::ASN1::Constructive
1773
+ *
1774
+ * The parent class for all constructed encodings. The _value_ attribute
1775
+ * of a Constructive is always an Array. Attributes are the same as
1776
+ * for ASN1Data, with the addition of _tagging_.
1777
+ *
1778
+ * == SET and SEQUENCE
1779
+ *
1780
+ * Most constructed encodings come in the form of a SET or a SEQUENCE.
1781
+ * These encodings are represented by one of the two sub-classes of
1782
+ * Constructive:
1783
+ * * OpenSSL::ASN1::Set
1784
+ * * OpenSSL::ASN1::Sequence
1785
+ * Please note that tagged sequences and sets are still parsed as
1786
+ * instances of ASN1Data. Find further details on tagged values
1787
+ * there.
1788
+ *
1789
+ * === Example - constructing a SEQUENCE
1790
+ * int = OpenSSL::ASN1::Integer.new(1)
1791
+ * str = OpenSSL::ASN1::PrintableString.new('abc')
1792
+ * sequence = OpenSSL::ASN1::Sequence.new( [ int, str ] )
1793
+ *
1794
+ * === Example - constructing a SET
1795
+ * int = OpenSSL::ASN1::Integer.new(1)
1796
+ * str = OpenSSL::ASN1::PrintableString.new('abc')
1797
+ * set = OpenSSL::ASN1::Set.new( [ int, str ] )
1798
+ */
1799
+ cASN1Constructive = rb_define_class_under(mASN1,"Constructive", cASN1Data);
1800
+ rb_include_module(cASN1Constructive, rb_mEnumerable);
1801
+ /*
1802
+ * May be used as a hint for encoding a value either implicitly or
1803
+ * explicitly by setting it either to +:IMPLICIT+ or to +:EXPLICIT+.
1804
+ * _tagging_ is not set when a ASN.1 structure is parsed using
1805
+ * OpenSSL::ASN1.decode.
1806
+ */
1807
+ rb_attr(cASN1Constructive, rb_intern("tagging"), 1, 1, Qtrue);
1808
+ rb_define_method(cASN1Constructive, "initialize", ossl_asn1_initialize, -1);
1809
+ rb_define_method(cASN1Constructive, "to_der", ossl_asn1cons_to_der, 0);
1810
+ rb_define_method(cASN1Constructive, "each", ossl_asn1cons_each, 0);
1811
+
1812
+ #define OSSL_ASN1_DEFINE_CLASS(name, super) \
1813
+ do{\
1814
+ cASN1##name = rb_define_class_under(mASN1, #name, cASN1##super);\
1815
+ rb_define_module_function(mASN1, #name, ossl_asn1_##name, -1);\
1816
+ }while(0)
1817
+
1818
+ OSSL_ASN1_DEFINE_CLASS(Boolean, Primitive);
1819
+ OSSL_ASN1_DEFINE_CLASS(Integer, Primitive);
1820
+ OSSL_ASN1_DEFINE_CLASS(Enumerated, Primitive);
1821
+ OSSL_ASN1_DEFINE_CLASS(BitString, Primitive);
1822
+ OSSL_ASN1_DEFINE_CLASS(OctetString, Primitive);
1823
+ OSSL_ASN1_DEFINE_CLASS(UTF8String, Primitive);
1824
+ OSSL_ASN1_DEFINE_CLASS(NumericString, Primitive);
1825
+ OSSL_ASN1_DEFINE_CLASS(PrintableString, Primitive);
1826
+ OSSL_ASN1_DEFINE_CLASS(T61String, Primitive);
1827
+ OSSL_ASN1_DEFINE_CLASS(VideotexString, Primitive);
1828
+ OSSL_ASN1_DEFINE_CLASS(IA5String, Primitive);
1829
+ OSSL_ASN1_DEFINE_CLASS(GraphicString, Primitive);
1830
+ OSSL_ASN1_DEFINE_CLASS(ISO64String, Primitive);
1831
+ OSSL_ASN1_DEFINE_CLASS(GeneralString, Primitive);
1832
+ OSSL_ASN1_DEFINE_CLASS(UniversalString, Primitive);
1833
+ OSSL_ASN1_DEFINE_CLASS(BMPString, Primitive);
1834
+ OSSL_ASN1_DEFINE_CLASS(Null, Primitive);
1835
+ OSSL_ASN1_DEFINE_CLASS(ObjectId, Primitive);
1836
+ OSSL_ASN1_DEFINE_CLASS(UTCTime, Primitive);
1837
+ OSSL_ASN1_DEFINE_CLASS(GeneralizedTime, Primitive);
1838
+
1839
+ OSSL_ASN1_DEFINE_CLASS(Sequence, Constructive);
1840
+ OSSL_ASN1_DEFINE_CLASS(Set, Constructive);
1841
+
1842
+ OSSL_ASN1_DEFINE_CLASS(EndOfContent, Data);
1843
+
1844
+
1845
+ /* Document-class: OpenSSL::ASN1::ObjectId
1846
+ *
1847
+ * Represents the primitive object id for OpenSSL::ASN1
1848
+ */
1849
+ #if 0
1850
+ cASN1ObjectId = rb_define_class_under(mASN1, "ObjectId", cASN1Primitive); /* let rdoc know */
1851
+ #endif
1852
+ rb_define_singleton_method(cASN1ObjectId, "register", ossl_asn1obj_s_register, 3);
1853
+ rb_define_method(cASN1ObjectId, "sn", ossl_asn1obj_get_sn, 0);
1854
+ rb_define_method(cASN1ObjectId, "ln", ossl_asn1obj_get_ln, 0);
1855
+ rb_define_method(cASN1ObjectId, "oid", ossl_asn1obj_get_oid, 0);
1856
+ rb_define_alias(cASN1ObjectId, "short_name", "sn");
1857
+ rb_define_alias(cASN1ObjectId, "long_name", "ln");
1858
+ rb_define_method(cASN1ObjectId, "==", ossl_asn1obj_eq, 1);
1859
+ rb_attr(cASN1BitString, rb_intern("unused_bits"), 1, 1, 0);
1860
+
1861
+ rb_define_method(cASN1EndOfContent, "initialize", ossl_asn1eoc_initialize, 0);
1862
+ rb_define_method(cASN1EndOfContent, "to_der", ossl_asn1eoc_to_der, 0);
1863
+
1864
+ class_tag_map = rb_hash_new();
1865
+ rb_gc_register_mark_object(class_tag_map);
1866
+ rb_hash_aset(class_tag_map, cASN1EndOfContent, INT2NUM(V_ASN1_EOC));
1867
+ rb_hash_aset(class_tag_map, cASN1Boolean, INT2NUM(V_ASN1_BOOLEAN));
1868
+ rb_hash_aset(class_tag_map, cASN1Integer, INT2NUM(V_ASN1_INTEGER));
1869
+ rb_hash_aset(class_tag_map, cASN1BitString, INT2NUM(V_ASN1_BIT_STRING));
1870
+ rb_hash_aset(class_tag_map, cASN1OctetString, INT2NUM(V_ASN1_OCTET_STRING));
1871
+ rb_hash_aset(class_tag_map, cASN1Null, INT2NUM(V_ASN1_NULL));
1872
+ rb_hash_aset(class_tag_map, cASN1ObjectId, INT2NUM(V_ASN1_OBJECT));
1873
+ rb_hash_aset(class_tag_map, cASN1Enumerated, INT2NUM(V_ASN1_ENUMERATED));
1874
+ rb_hash_aset(class_tag_map, cASN1UTF8String, INT2NUM(V_ASN1_UTF8STRING));
1875
+ rb_hash_aset(class_tag_map, cASN1Sequence, INT2NUM(V_ASN1_SEQUENCE));
1876
+ rb_hash_aset(class_tag_map, cASN1Set, INT2NUM(V_ASN1_SET));
1877
+ rb_hash_aset(class_tag_map, cASN1NumericString, INT2NUM(V_ASN1_NUMERICSTRING));
1878
+ rb_hash_aset(class_tag_map, cASN1PrintableString, INT2NUM(V_ASN1_PRINTABLESTRING));
1879
+ rb_hash_aset(class_tag_map, cASN1T61String, INT2NUM(V_ASN1_T61STRING));
1880
+ rb_hash_aset(class_tag_map, cASN1VideotexString, INT2NUM(V_ASN1_VIDEOTEXSTRING));
1881
+ rb_hash_aset(class_tag_map, cASN1IA5String, INT2NUM(V_ASN1_IA5STRING));
1882
+ rb_hash_aset(class_tag_map, cASN1UTCTime, INT2NUM(V_ASN1_UTCTIME));
1883
+ rb_hash_aset(class_tag_map, cASN1GeneralizedTime, INT2NUM(V_ASN1_GENERALIZEDTIME));
1884
+ rb_hash_aset(class_tag_map, cASN1GraphicString, INT2NUM(V_ASN1_GRAPHICSTRING));
1885
+ rb_hash_aset(class_tag_map, cASN1ISO64String, INT2NUM(V_ASN1_ISO64STRING));
1886
+ rb_hash_aset(class_tag_map, cASN1GeneralString, INT2NUM(V_ASN1_GENERALSTRING));
1887
+ rb_hash_aset(class_tag_map, cASN1UniversalString, INT2NUM(V_ASN1_UNIVERSALSTRING));
1888
+ rb_hash_aset(class_tag_map, cASN1BMPString, INT2NUM(V_ASN1_BMPSTRING));
1889
+
1890
+ id_each = rb_intern_const("each");
1891
+ }