~ubuntu-branches/ubuntu/hardy/ruby1.8/hardy-updates

« back to all changes in this revision

Viewing changes to ext/openssl/ossl_x509ext.c

  • Committer: Bazaar Package Importer
  • Author(s): akira yamada
  • Date: 2007-03-13 22:11:58 UTC
  • mfrom: (1.1.5 upstream)
  • Revision ID: james.westby@ubuntu.com-20070313221158-h3oql37brlaf2go2
Tags: 1.8.6-1
* new upstream version, 1.8.6.
* libruby1.8 conflicts with libopenssl-ruby1.8 (< 1.8.6) (closes: #410018)
* changed packaging style to cdbs from dbs.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * $Id: ossl_x509ext.c 11708 2007-02-12 23:01:19Z shyouhei $
 
3
 * 'OpenSSL for Ruby' project
 
4
 * Copyright (C) 2001-2002  Michal Rokos <m.rokos@sh.cvut.cz>
 
5
 * All rights reserved.
 
6
 */
 
7
/*
 
8
 * This program is licenced under the same licence as Ruby.
 
9
 * (See the file 'LICENCE'.)
 
10
 */
 
11
#include "ossl.h"
 
12
 
 
13
#define WrapX509Ext(klass, obj, ext) do { \
 
14
    if (!ext) { \
 
15
        ossl_raise(rb_eRuntimeError, "EXT wasn't initialized!"); \
 
16
    } \
 
17
    obj = Data_Wrap_Struct(klass, 0, X509_EXTENSION_free, ext); \
 
18
} while (0)
 
19
#define GetX509Ext(obj, ext) do { \
 
20
    Data_Get_Struct(obj, X509_EXTENSION, ext); \
 
21
    if (!ext) { \
 
22
        ossl_raise(rb_eRuntimeError, "EXT wasn't initialized!"); \
 
23
    } \
 
24
} while (0)
 
25
#define SafeGetX509Ext(obj, ext) do { \
 
26
    OSSL_Check_Kind(obj, cX509Ext); \
 
27
    GetX509Ext(obj, ext); \
 
28
} while (0)
 
29
#define MakeX509ExtFactory(klass, obj, ctx) do { \
 
30
    if (!(ctx = OPENSSL_malloc(sizeof(X509V3_CTX)))) \
 
31
        ossl_raise(rb_eRuntimeError, "CTX wasn't allocated!"); \
 
32
    X509V3_set_ctx(ctx, NULL, NULL, NULL, NULL, 0); \
 
33
    obj = Data_Wrap_Struct(klass, 0, ossl_x509extfactory_free, ctx); \
 
34
} while (0)
 
35
#define GetX509ExtFactory(obj, ctx) do { \
 
36
    Data_Get_Struct(obj, X509V3_CTX, ctx); \
 
37
    if (!ctx) { \
 
38
        ossl_raise(rb_eRuntimeError, "CTX wasn't initialized!"); \
 
39
    } \
 
40
} while (0)
 
41
 
 
42
/*
 
43
 * Classes
 
44
 */
 
45
VALUE cX509Ext;
 
46
VALUE cX509ExtFactory;
 
47
VALUE eX509ExtError;
 
48
 
 
49
/*
 
50
 * Public
 
51
 */
 
52
VALUE 
 
53
ossl_x509ext_new(X509_EXTENSION *ext)
 
54
{
 
55
    X509_EXTENSION *new;
 
56
    VALUE obj;
 
57
 
 
58
    if (!ext) {
 
59
        new = X509_EXTENSION_new();
 
60
    } else {
 
61
        new = X509_EXTENSION_dup(ext);
 
62
    }
 
63
    if (!new) {
 
64
        ossl_raise(eX509ExtError, NULL);
 
65
    }
 
66
    WrapX509Ext(cX509Ext, obj, new);
 
67
        
 
68
    return obj;
 
69
}
 
70
 
 
71
X509_EXTENSION *
 
72
GetX509ExtPtr(VALUE obj)
 
73
{
 
74
    X509_EXTENSION *ext;
 
75
 
 
76
    SafeGetX509Ext(obj, ext);
 
77
 
 
78
    return ext;
 
79
}
 
80
 
 
81
X509_EXTENSION *
 
82
DupX509ExtPtr(VALUE obj)
 
83
{
 
84
    X509_EXTENSION *ext, *new;
 
85
 
 
86
    SafeGetX509Ext(obj, ext);
 
87
    if (!(new = X509_EXTENSION_dup(ext))) {
 
88
        ossl_raise(eX509ExtError, NULL);
 
89
    }
 
90
 
 
91
    return new;
 
92
}
 
93
 
 
94
/*
 
95
 * Private
 
96
 */
 
97
/*
 
98
 * Ext factory
 
99
 */
 
100
static void
 
101
ossl_x509extfactory_free(X509V3_CTX *ctx)
 
102
{
 
103
    OPENSSL_free(ctx);
 
104
}
 
105
 
 
106
static VALUE 
 
107
ossl_x509extfactory_alloc(VALUE klass)
 
108
{
 
109
    X509V3_CTX *ctx;
 
110
    VALUE obj;
 
111
 
 
112
    MakeX509ExtFactory(klass, obj, ctx);
 
113
 
 
114
    return obj;
 
115
}
 
116
 
 
117
static VALUE 
 
118
ossl_x509extfactory_set_issuer_cert(VALUE self, VALUE cert)
 
119
{
 
120
    X509V3_CTX *ctx;
 
121
 
 
122
    GetX509ExtFactory(self, ctx);
 
123
    rb_iv_set(self, "@issuer_certificate", cert);
 
124
    ctx->issuer_cert = GetX509CertPtr(cert); /* NO DUP NEEDED */
 
125
 
 
126
    return cert;
 
127
}
 
128
 
 
129
static VALUE 
 
130
ossl_x509extfactory_set_subject_cert(VALUE self, VALUE cert)
 
131
{
 
132
    X509V3_CTX *ctx;
 
133
 
 
134
    GetX509ExtFactory(self, ctx);
 
135
    rb_iv_set(self, "@subject_certificate", cert);
 
136
    ctx->subject_cert = GetX509CertPtr(cert); /* NO DUP NEEDED */
 
137
 
 
138
    return cert;
 
139
}
 
140
 
 
141
static VALUE 
 
142
ossl_x509extfactory_set_subject_req(VALUE self, VALUE req)
 
143
{
 
144
    X509V3_CTX *ctx;
 
145
 
 
146
    GetX509ExtFactory(self, ctx);
 
147
    rb_iv_set(self, "@subject_request", req);
 
148
    ctx->subject_req = GetX509ReqPtr(req); /* NO DUP NEEDED */
 
149
 
 
150
    return req;
 
151
}
 
152
 
 
153
static VALUE 
 
154
ossl_x509extfactory_set_crl(VALUE self, VALUE crl)
 
155
{
 
156
    X509V3_CTX *ctx;
 
157
 
 
158
    GetX509ExtFactory(self, ctx);
 
159
    rb_iv_set(self, "@crl", crl);
 
160
    ctx->crl = GetX509CRLPtr(crl); /* NO DUP NEEDED */
 
161
 
 
162
    return crl;
 
163
}
 
164
 
 
165
static VALUE
 
166
ossl_x509extfactory_set_config(VALUE self, VALUE config)
 
167
{
 
168
#ifdef HAVE_X509V3_SET_NCONF
 
169
    X509V3_CTX *ctx;
 
170
    CONF *conf;
 
171
 
 
172
    GetX509ExtFactory(self, ctx);
 
173
    rb_iv_set(self, "@config", config);
 
174
    conf = GetConfigPtr(config);  /* NO DUP NEEDED */
 
175
    X509V3_set_nconf(ctx, conf);
 
176
 
 
177
    return config;
 
178
#else
 
179
    rb_notimplement();
 
180
#endif
 
181
}
 
182
 
 
183
static VALUE 
 
184
ossl_x509extfactory_initialize(int argc, VALUE *argv, VALUE self)
 
185
{
 
186
    /*X509V3_CTX *ctx;*/
 
187
    VALUE issuer_cert, subject_cert, subject_req, crl;
 
188
        
 
189
    /*GetX509ExtFactory(self, ctx);*/
 
190
 
 
191
    rb_scan_args(argc, argv, "04",
 
192
                 &issuer_cert, &subject_cert, &subject_req, &crl);
 
193
    if (!NIL_P(issuer_cert))
 
194
        ossl_x509extfactory_set_issuer_cert(self, issuer_cert);
 
195
    if (!NIL_P(subject_cert))
 
196
        ossl_x509extfactory_set_subject_cert(self, subject_cert);
 
197
    if (!NIL_P(subject_req))
 
198
        ossl_x509extfactory_set_subject_req(self, subject_req);
 
199
    if (!NIL_P(crl))
 
200
        ossl_x509extfactory_set_crl(self, crl);
 
201
 
 
202
    return self;
 
203
}
 
204
 
 
205
/*
 
206
 * Array to X509_EXTENSION
 
207
 * Structure:
 
208
 * ["ln", "value", bool_critical] or
 
209
 * ["sn", "value", bool_critical] or
 
210
 * ["ln", "critical,value"] or the same for sn
 
211
 * ["ln", "value"] => not critical
 
212
 */
 
213
static VALUE 
 
214
ossl_x509extfactory_create_ext(int argc, VALUE *argv, VALUE self)
 
215
{
 
216
    X509V3_CTX *ctx;
 
217
    X509_EXTENSION *ext;
 
218
    VALUE oid, value, critical, valstr, obj;
 
219
    int nid;
 
220
#ifdef HAVE_X509V3_EXT_NCONF_NID
 
221
    VALUE rconf;
 
222
    CONF *conf;
 
223
#else
 
224
    static LHASH *empty_lhash;
 
225
#endif
 
226
 
 
227
    rb_scan_args(argc, argv, "21", &oid, &value, &critical);
 
228
    StringValue(oid);
 
229
    StringValue(value);
 
230
    if(NIL_P(critical)) critical = Qfalse;
 
231
 
 
232
    nid = OBJ_ln2nid(RSTRING(oid)->ptr);
 
233
    if(!nid) nid = OBJ_sn2nid(RSTRING(oid)->ptr);
 
234
    if(!nid) ossl_raise(eX509ExtError, "unknown OID `%s'", RSTRING(oid)->ptr);
 
235
    valstr = rb_str_new2(RTEST(critical) ? "critical," : "");
 
236
    rb_str_append(valstr, value);
 
237
    GetX509ExtFactory(self, ctx);
 
238
#ifdef HAVE_X509V3_EXT_NCONF_NID
 
239
    rconf = rb_iv_get(self, "@config");
 
240
    conf = NIL_P(rconf) ? NULL : GetConfigPtr(rconf);
 
241
    ext = X509V3_EXT_nconf_nid(conf, ctx, nid, RSTRING(valstr)->ptr);
 
242
#else
 
243
    if (!empty_lhash) empty_lhash = lh_new(NULL, NULL);
 
244
    ext = X509V3_EXT_conf_nid(empty_lhash, ctx, nid, RSTRING(valstr)->ptr);
 
245
#endif
 
246
    if (!ext){
 
247
        ossl_raise(eX509ExtError, "%s = %s",
 
248
                   RSTRING(oid)->ptr, RSTRING(value)->ptr);
 
249
    }
 
250
    WrapX509Ext(cX509Ext, obj, ext);
 
251
 
 
252
    return obj;
 
253
}
 
254
 
 
255
/*
 
256
 * Ext
 
257
 */
 
258
static VALUE
 
259
ossl_x509ext_alloc(VALUE klass)
 
260
{
 
261
    X509_EXTENSION *ext;
 
262
    VALUE obj;
 
263
 
 
264
    if(!(ext = X509_EXTENSION_new())){
 
265
        ossl_raise(eX509ExtError, NULL);
 
266
    }
 
267
    WrapX509Ext(klass, obj, ext);
 
268
 
 
269
    return obj;
 
270
}
 
271
 
 
272
static VALUE
 
273
ossl_x509ext_initialize(int argc, VALUE *argv, VALUE self)
 
274
{
 
275
    VALUE oid, value, critical;
 
276
    unsigned char *p;
 
277
    X509_EXTENSION *ext;
 
278
 
 
279
    GetX509Ext(self, ext);
 
280
    if(rb_scan_args(argc, argv, "12", &oid, &value, &critical) == 1){
 
281
        oid = ossl_to_der_if_possible(oid);
 
282
        StringValue(oid);
 
283
        p  = RSTRING(oid)->ptr;
 
284
        if(!d2i_X509_EXTENSION((X509_EXTENSION**)&DATA_PTR(self),
 
285
                               &p, RSTRING(oid)->len))
 
286
            ossl_raise(eX509ExtError, NULL);
 
287
        return self;
 
288
    }
 
289
    rb_funcall(self, rb_intern("oid="), 1, oid);
 
290
    rb_funcall(self, rb_intern("value="), 1, value);
 
291
    if(argc > 2) rb_funcall(self, rb_intern("critical="), 1, critical);
 
292
 
 
293
    return self;
 
294
}
 
295
 
 
296
static VALUE
 
297
ossl_x509ext_set_oid(VALUE self, VALUE oid)
 
298
{
 
299
    X509_EXTENSION *ext;
 
300
    ASN1_OBJECT *obj;
 
301
    char *s;
 
302
 
 
303
    s = StringValuePtr(oid);
 
304
    obj = OBJ_txt2obj(s, 0);
 
305
    if(!obj) obj = OBJ_txt2obj(s, 1);
 
306
    if(!obj) ossl_raise(eX509ExtError, NULL);
 
307
    GetX509Ext(self, ext);
 
308
    X509_EXTENSION_set_object(ext, obj);
 
309
 
 
310
    return oid;
 
311
}
 
312
 
 
313
static VALUE
 
314
ossl_x509ext_set_value(VALUE self, VALUE data)
 
315
{
 
316
    X509_EXTENSION *ext;
 
317
    ASN1_OCTET_STRING *asn1s;
 
318
    char *s;
 
319
 
 
320
    data = ossl_to_der_if_possible(data);
 
321
    StringValue(data);
 
322
    if(!(s = OPENSSL_malloc(RSTRING(data)->len)))
 
323
        ossl_raise(eX509ExtError, "malloc error");
 
324
    memcpy(s, RSTRING(data)->ptr, RSTRING(data)->len);
 
325
    if(!(asn1s = ASN1_OCTET_STRING_new())){
 
326
        free(s);
 
327
        ossl_raise(eX509ExtError, NULL);
 
328
    }
 
329
    if(!M_ASN1_OCTET_STRING_set(asn1s, s, RSTRING(data)->len)){
 
330
        free(s);
 
331
        ASN1_OCTET_STRING_free(asn1s);
 
332
        ossl_raise(eX509ExtError, NULL);
 
333
    }
 
334
    GetX509Ext(self, ext);
 
335
    X509_EXTENSION_set_data(ext, asn1s);
 
336
 
 
337
    return data;
 
338
}
 
339
 
 
340
static VALUE
 
341
ossl_x509ext_set_critical(VALUE self, VALUE flag)
 
342
{
 
343
    X509_EXTENSION *ext;
 
344
 
 
345
    GetX509Ext(self, ext);
 
346
    X509_EXTENSION_set_critical(ext, RTEST(flag) ? 1 : 0);
 
347
 
 
348
    return flag;
 
349
}
 
350
 
 
351
static VALUE 
 
352
ossl_x509ext_get_oid(VALUE obj)
 
353
{
 
354
    X509_EXTENSION *ext;
 
355
    ASN1_OBJECT *extobj;
 
356
    BIO *out;
 
357
    VALUE ret;
 
358
    int nid;
 
359
 
 
360
    GetX509Ext(obj, ext);
 
361
    extobj = X509_EXTENSION_get_object(ext);
 
362
    if ((nid = OBJ_obj2nid(extobj)) != NID_undef)
 
363
        ret = rb_str_new2(OBJ_nid2sn(nid));
 
364
    else{
 
365
        if (!(out = BIO_new(BIO_s_mem())))
 
366
            ossl_raise(eX509ExtError, NULL);
 
367
        i2a_ASN1_OBJECT(out, extobj);
 
368
        ret = ossl_membio2str(out);
 
369
    }
 
370
 
 
371
    return ret;
 
372
}
 
373
 
 
374
static VALUE
 
375
ossl_x509ext_get_value(VALUE obj)
 
376
{
 
377
    X509_EXTENSION *ext;
 
378
    BIO *out;
 
379
    VALUE ret;
 
380
 
 
381
    GetX509Ext(obj, ext);
 
382
    if (!(out = BIO_new(BIO_s_mem())))
 
383
        ossl_raise(eX509ExtError, NULL);
 
384
    if (!X509V3_EXT_print(out, ext, 0, 0))
 
385
        M_ASN1_OCTET_STRING_print(out, ext->value);
 
386
    ret = ossl_membio2str(out);
 
387
 
 
388
    return ret;
 
389
}
 
390
 
 
391
static VALUE
 
392
ossl_x509ext_get_critical(VALUE obj)
 
393
{
 
394
    X509_EXTENSION *ext;
 
395
 
 
396
    GetX509Ext(obj, ext);
 
397
    return X509_EXTENSION_get_critical(ext) ? Qtrue : Qfalse;
 
398
}
 
399
 
 
400
static VALUE
 
401
ossl_x509ext_to_der(VALUE obj)
 
402
{
 
403
    X509_EXTENSION *ext;
 
404
    unsigned char *p;
 
405
    long len;
 
406
    VALUE str;
 
407
 
 
408
    GetX509Ext(obj, ext);
 
409
    if((len = i2d_X509_EXTENSION(ext, NULL)) <= 0)
 
410
        ossl_raise(eX509ExtError, NULL);
 
411
    str = rb_str_new(0, len);
 
412
    p = RSTRING(str)->ptr;
 
413
    if(i2d_X509_EXTENSION(ext, &p) < 0)
 
414
        ossl_raise(eX509ExtError, NULL);
 
415
    ossl_str_adjust(str, p);
 
416
 
 
417
    return str;
 
418
}
 
419
 
 
420
/*
 
421
 * INIT
 
422
 */
 
423
void
 
424
Init_ossl_x509ext()
 
425
{
 
426
    eX509ExtError = rb_define_class_under(mX509, "ExtensionError", eOSSLError);
 
427
    
 
428
    cX509ExtFactory = rb_define_class_under(mX509, "ExtensionFactory", rb_cObject);
 
429
        
 
430
    rb_define_alloc_func(cX509ExtFactory, ossl_x509extfactory_alloc);
 
431
    rb_define_method(cX509ExtFactory, "initialize", ossl_x509extfactory_initialize, -1);
 
432
        
 
433
    rb_attr(cX509ExtFactory, rb_intern("issuer_certificate"), 1, 0, Qfalse);
 
434
    rb_attr(cX509ExtFactory, rb_intern("subject_certificate"), 1, 0, Qfalse);
 
435
    rb_attr(cX509ExtFactory, rb_intern("subject_request"), 1, 0, Qfalse);
 
436
    rb_attr(cX509ExtFactory, rb_intern("crl"), 1, 0, Qfalse);
 
437
    rb_attr(cX509ExtFactory, rb_intern("config"), 1, 0, Qfalse);
 
438
 
 
439
    rb_define_method(cX509ExtFactory, "issuer_certificate=", ossl_x509extfactory_set_issuer_cert, 1);
 
440
    rb_define_method(cX509ExtFactory, "subject_certificate=", ossl_x509extfactory_set_subject_cert, 1);
 
441
    rb_define_method(cX509ExtFactory, "subject_request=", ossl_x509extfactory_set_subject_req, 1);
 
442
    rb_define_method(cX509ExtFactory, "crl=", ossl_x509extfactory_set_crl, 1);
 
443
    rb_define_method(cX509ExtFactory, "config=", ossl_x509extfactory_set_config, 1);
 
444
    rb_define_method(cX509ExtFactory, "create_ext", ossl_x509extfactory_create_ext, -1);
 
445
        
 
446
    cX509Ext = rb_define_class_under(mX509, "Extension", rb_cObject);
 
447
    rb_define_alloc_func(cX509Ext, ossl_x509ext_alloc);
 
448
    rb_define_method(cX509Ext, "initialize", ossl_x509ext_initialize, -1);
 
449
    rb_define_method(cX509Ext, "oid=", ossl_x509ext_set_oid, 1);
 
450
    rb_define_method(cX509Ext, "value=", ossl_x509ext_set_value, 1);
 
451
    rb_define_method(cX509Ext, "critical=", ossl_x509ext_set_critical, 1);
 
452
    rb_define_method(cX509Ext, "oid", ossl_x509ext_get_oid, 0);
 
453
    rb_define_method(cX509Ext, "value", ossl_x509ext_get_value, 0);
 
454
    rb_define_method(cX509Ext, "critical?", ossl_x509ext_get_critical, 0);
 
455
    rb_define_method(cX509Ext, "to_der", ossl_x509ext_to_der, 0);
 
456
}