~ubuntu-branches/ubuntu/quantal/ruby1.9.1/quantal

« back to all changes in this revision

Viewing changes to ext/openssl/ossl_x509cert.c

  • Committer: Bazaar Package Importer
  • Author(s): Lucas Nussbaum
  • Date: 2010-07-31 17:08:39 UTC
  • mfrom: (1.1.4 upstream) (8.1.2 sid)
  • Revision ID: james.westby@ubuntu.com-20100731170839-j034dmpdqt1cc4p6
Tags: 1.9.2~svn28788-1
* New release based on upstream snapshot from the 1.9.2 branch,
  after 1.9.2 RC2. That branch is (supposed to be) binary-compatible
  with the 1.9.1 branch.
  + Builds fine on i386. Closes: #580852.
* Upgrade to Standards-Version: 3.9.1. No changes needed.
* Updated generated incs.
* Patches that still need work:
  + Unclear status, need more investigation:
   090729_fix_Makefile_deps.dpatch
   090803_exclude_rdoc.dpatch
   203_adjust_base_of_search_path.dpatch
   902_define_YAML_in_yaml_stringio.rb.dpatch
   919_common.mk_tweaks.dpatch
   931_libruby_suffix.dpatch
   940_test_thread_mutex_sync_shorter.dpatch
  + Maybe not needed anymore, keeping but not applying.
   102_skip_test_copy_stream.dpatch (test doesn't block anymore?)
   104_skip_btest_io.dpatch (test doesn't block anymore?)
   201_gem_prelude.dpatch (we don't use that rubygems anyway?)
   202_gem_default_dir.dpatch (we don't use that rubygems anyway?)
   940_test_file_exhaustive_fails_as_root.dpatch
   940_test_priority_fails.dpatch
   100518_load_libc_libm.dpatch
* Add disable-tests.diff: disable some tests that cause failures on FreeBSD.
  Closes: #590002, #543805, #542927.
* However, many new failures on FreeBSD. Since that version is still an
  improvement, add the check that makes test suite failures non-fatal on
  FreeBSD again. That still needs to be investigated.
* Re-add 903_skip_base_ruby_check.dpatch
* Add build-dependency on ruby1.8 and drop all pre-generated files.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*
2
 
 * $Id: ossl_x509cert.c 18168 2008-07-22 15:34:23Z nobu $
 
2
 * $Id: ossl_x509cert.c 27440 2010-04-22 08:21:01Z nobu $
3
3
 * 'OpenSSL for Ruby' project
4
4
 * Copyright (C) 2001-2002  Michal Rokos <m.rokos@sh.cvut.cz>
5
5
 * All rights reserved.
51
51
        ossl_raise(eX509CertError, NULL);
52
52
    }
53
53
    WrapX509(cX509Cert, obj, new);
54
 
        
 
54
 
55
55
    return obj;
56
56
}
57
57
 
58
 
VALUE 
 
58
VALUE
59
59
ossl_x509_new_from_file(VALUE filename)
60
60
{
61
61
    X509 *x509;
90
90
GetX509CertPtr(VALUE obj)
91
91
{
92
92
    X509 *x509;
93
 
        
 
93
 
94
94
    SafeGetX509(obj, x509);
95
 
        
 
95
 
96
96
    return x509;
97
97
}
98
98
 
100
100
DupX509CertPtr(VALUE obj)
101
101
{
102
102
    X509 *x509;
103
 
        
 
103
 
104
104
    SafeGetX509(obj, x509);
105
 
        
 
105
 
106
106
    CRYPTO_add(&x509->references, 1, CRYPTO_LOCK_X509);
107
 
        
 
107
 
108
108
    return x509;
109
109
}
110
110
 
111
111
/*
112
112
 * Private
113
113
 */
114
 
static VALUE 
 
114
static VALUE
115
115
ossl_x509_alloc(VALUE klass)
116
116
{
117
117
    X509 *x509;
130
130
 *    Certificate.new => cert
131
131
 *    Certificate.new(string) => cert
132
132
 */
133
 
static VALUE 
 
133
static VALUE
134
134
ossl_x509_initialize(int argc, VALUE *argv, VALUE self)
135
135
{
136
136
    BIO *in;
152
152
    }
153
153
    BIO_free(in);
154
154
    if (!x509) ossl_raise(eX509CertError, NULL);
155
 
    
 
155
 
156
156
    return self;
157
157
}
158
158
 
160
160
ossl_x509_copy(VALUE self, VALUE other)
161
161
{
162
162
    X509 *a, *b, *x509;
163
 
        
 
163
 
164
164
    rb_check_frozen(self);
165
165
    if (self == other) return self;
166
166
 
169
169
 
170
170
    x509 = X509_dup(b);
171
171
    if (!x509) ossl_raise(eX509CertError, NULL);
172
 
    
 
172
 
173
173
    DATA_PTR(self) = x509;
174
174
    X509_free(a);
175
175
 
180
180
 * call-seq:
181
181
 *    cert.to_der => string
182
182
 */
183
 
static VALUE 
 
183
static VALUE
184
184
ossl_x509_to_der(VALUE self)
185
185
{
186
186
    X509 *x509;
196
196
    if (i2d_X509(x509, &p) <= 0)
197
197
        ossl_raise(eX509CertError, NULL);
198
198
    ossl_str_adjust(str, p);
199
 
    
 
199
 
200
200
    return str;
201
201
}
202
202
 
204
204
 * call-seq:
205
205
 *    cert.to_pem => string
206
206
 */
207
 
static VALUE 
 
207
static VALUE
208
208
ossl_x509_to_pem(VALUE self)
209
209
{
210
210
    X509 *x509;
211
211
    BIO *out;
212
212
    VALUE str;
213
 
        
 
213
 
214
214
    GetX509(self, x509);
215
215
    out = BIO_new(BIO_s_mem());
216
216
    if (!out) ossl_raise(eX509CertError, NULL);
234
234
    X509 *x509;
235
235
    BIO *out;
236
236
    VALUE str;
237
 
        
 
237
 
238
238
    GetX509(self, x509);
239
239
 
240
240
    out = BIO_new(BIO_s_mem());
253
253
/*
254
254
 * Makes from X509 X509_REQuest
255
255
 */
256
 
static VALUE 
 
256
static VALUE
257
257
ossl_x509_to_req(VALUE self)
258
258
{
259
259
    X509 *x509;
275
275
 * call-seq:
276
276
 *    cert.version => integer
277
277
 */
278
 
static VALUE 
 
278
static VALUE
279
279
ossl_x509_get_version(VALUE self)
280
280
{
281
281
    X509 *x509;
282
282
 
283
283
    GetX509(self, x509);
284
 
        
 
284
 
285
285
    return LONG2NUM(X509_get_version(x509));
286
286
}
287
287
 
289
289
 * call-seq:
290
290
 *    cert.version = integer => integer
291
291
 */
292
 
static VALUE 
 
292
static VALUE
293
293
ossl_x509_set_version(VALUE self, VALUE version)
294
294
{
295
295
    X509 *x509;
310
310
 * call-seq:
311
311
 *    cert.serial => integer
312
312
 */
313
 
static VALUE 
 
313
static VALUE
314
314
ossl_x509_get_serial(VALUE self)
315
315
{
316
316
    X509 *x509;
317
317
 
318
318
    GetX509(self, x509);
319
 
        
 
319
 
320
320
    return asn1integer_to_num(X509_get_serialNumber(x509));
321
321
}
322
322
 
324
324
 * call-seq:
325
325
 *    cert.serial = integer => integer
326
326
 */
327
 
static VALUE 
 
327
static VALUE
328
328
ossl_x509_set_serial(VALUE self, VALUE num)
329
329
{
330
330
    X509 *x509;
333
333
 
334
334
    x509->cert_info->serialNumber =
335
335
        num_to_asn1integer(num, X509_get_serialNumber(x509));
336
 
        
 
336
 
337
337
    return num;
338
338
}
339
339
 
341
341
 * call-seq:
342
342
 *    cert.signature_algorithm => string
343
343
 */
344
 
static VALUE 
 
344
static VALUE
345
345
ossl_x509_get_signature_algorithm(VALUE self)
346
346
{
347
347
    X509 *x509;
365
365
 * call-seq:
366
366
 *    cert.subject => name
367
367
 */
368
 
static VALUE 
 
368
static VALUE
369
369
ossl_x509_get_subject(VALUE self)
370
370
{
371
371
    X509 *x509;
372
372
    X509_NAME *name;
373
 
        
 
373
 
374
374
    GetX509(self, x509);
375
375
    if (!(name = X509_get_subject_name(x509))) { /* NO DUP - don't free! */
376
376
        ossl_raise(eX509CertError, NULL);
383
383
 * call-seq:
384
384
 *    cert.subject = name => name
385
385
 */
386
 
static VALUE 
 
386
static VALUE
387
387
ossl_x509_set_subject(VALUE self, VALUE subject)
388
388
{
389
389
    X509 *x509;
390
 
        
 
390
 
391
391
    GetX509(self, x509);
392
392
    if (!X509_set_subject_name(x509, GetX509NamePtr(subject))) { /* DUPs name */
393
393
        ossl_raise(eX509CertError, NULL);
400
400
 * call-seq:
401
401
 *    cert.issuer => name
402
402
 */
403
 
static VALUE 
 
403
static VALUE
404
404
ossl_x509_get_issuer(VALUE self)
405
405
{
406
406
    X509 *x509;
418
418
 * call-seq:
419
419
 *    cert.issuer = name => name
420
420
 */
421
 
static VALUE 
 
421
static VALUE
422
422
ossl_x509_set_issuer(VALUE self, VALUE issuer)
423
423
{
424
424
    X509 *x509;
435
435
 * call-seq:
436
436
 *    cert.not_before => time
437
437
 */
438
 
static VALUE 
 
438
static VALUE
439
439
ossl_x509_get_not_before(VALUE self)
440
440
{
441
441
    X509 *x509;
453
453
 * call-seq:
454
454
 *    cert.not_before = time => time
455
455
 */
456
 
static VALUE 
 
456
static VALUE
457
457
ossl_x509_set_not_before(VALUE self, VALUE time)
458
458
{
459
459
    X509 *x509;
460
460
    time_t sec;
461
 
        
 
461
 
462
462
    sec = time_to_time_t(time);
463
463
    GetX509(self, x509);
464
464
    if (!X509_time_adj(X509_get_notBefore(x509), 0, &sec)) {
472
472
 * call-seq:
473
473
 *    cert.not_after => time
474
474
 */
475
 
static VALUE 
 
475
static VALUE
476
476
ossl_x509_get_not_after(VALUE self)
477
477
{
478
478
    X509 *x509;
490
490
 * call-seq:
491
491
 *    cert.not_before = time => time
492
492
 */
493
 
static VALUE 
 
493
static VALUE
494
494
ossl_x509_set_not_after(VALUE self, VALUE time)
495
495
{
496
496
    X509 *x509;
497
497
    time_t sec;
498
 
        
 
498
 
499
499
    sec = time_to_time_t(time);
500
500
    GetX509(self, x509);
501
501
    if (!X509_time_adj(X509_get_notAfter(x509), 0, &sec)) {
509
509
 * call-seq:
510
510
 *    cert.public_key => key
511
511
 */
512
 
static VALUE 
 
512
static VALUE
513
513
ossl_x509_get_public_key(VALUE self)
514
514
{
515
515
    X509 *x509;
527
527
 * call-seq:
528
528
 *    cert.public_key = key => key
529
529
 */
530
 
static VALUE 
 
530
static VALUE
531
531
ossl_x509_set_public_key(VALUE self, VALUE key)
532
532
{
533
533
    X509 *x509;
544
544
 * call-seq:
545
545
 *    cert.sign(key, digest) => self
546
546
 */
547
 
static VALUE 
 
547
static VALUE
548
548
ossl_x509_sign(VALUE self, VALUE key, VALUE digest)
549
549
{
550
550
    X509 *x509;
567
567
 *
568
568
 * Checks that cert signature is made with PRIVversion of this PUBLIC 'key'
569
569
 */
570
 
static VALUE 
 
570
static VALUE
571
571
ossl_x509_verify(VALUE self, VALUE key)
572
572
{
573
573
    X509 *x509;
578
578
    GetX509(self, x509);
579
579
    if ((i = X509_verify(x509, pkey)) < 0) {
580
580
        ossl_raise(eX509CertError, NULL);
581
 
    } 
 
581
    }
582
582
    if (i > 0) {
583
583
        return Qtrue;
584
584
    }
592
592
 *
593
593
 * Checks if 'key' is PRIV key for this cert
594
594
 */
595
 
static VALUE 
 
595
static VALUE
596
596
ossl_x509_check_private_key(VALUE self, VALUE key)
597
597
{
598
598
    X509 *x509;
599
599
    EVP_PKEY *pkey;
600
 
        
 
600
 
601
601
    /* not needed private key, but should be */
602
602
    pkey = GetPrivPKeyPtr(key); /* NO NEED TO DUP */
603
603
    GetX509(self, x509);
613
613
 * call-seq:
614
614
 *    cert.extensions => [extension...]
615
615
 */
616
 
static VALUE 
 
616
static VALUE
617
617
ossl_x509_get_extensions(VALUE self)
618
618
{
619
619
    X509 *x509;
639
639
 * call-seq:
640
640
 *    cert.extensions = [ext...] => [ext...]
641
641
 */
642
 
static VALUE 
 
642
static VALUE
643
643
ossl_x509_set_extensions(VALUE self, VALUE ary)
644
644
{
645
645
    X509 *x509;
646
646
    X509_EXTENSION *ext;
647
647
    int i;
648
 
        
 
648
 
649
649
    Check_Type(ary, T_ARRAY);
650
650
    /* All ary's members should be X509Extension */
651
651
    for (i=0; i<RARRAY_LEN(ary); i++) {
656
656
    x509->cert_info->extensions = NULL;
657
657
    for (i=0; i<RARRAY_LEN(ary); i++) {
658
658
        ext = DupX509ExtPtr(RARRAY_PTR(ary)[i]);
659
 
        
 
659
 
660
660
        if (!X509_add_ext(x509, ext, -1)) { /* DUPs ext - FREE it */
661
661
            X509_EXTENSION_free(ext);
662
662
            ossl_raise(eX509CertError, NULL);
671
671
 * call-seq:
672
672
 *    cert.add_extension(extension) => extension
673
673
 */
674
 
static VALUE 
 
674
static VALUE
675
675
ossl_x509_add_extension(VALUE self, VALUE extension)
676
676
{
677
677
    X509 *x509;
678
678
    X509_EXTENSION *ext;
679
 
        
 
679
 
680
680
    GetX509(self, x509);
681
681
    ext = DupX509ExtPtr(extension);
682
682
    if (!X509_add_ext(x509, ext, -1)) { /* DUPs ext - FREE it */
725
725
/*
726
726
 * INIT
727
727
 */
728
 
void 
 
728
void
729
729
Init_ossl_x509cert()
730
730
{
731
731
    eX509CertError = rb_define_class_under(mX509, "CertificateError", eOSSLError);
732
 
        
 
732
 
733
733
    cX509Cert = rb_define_class_under(mX509, "Certificate", rb_cObject);
734
 
        
 
734
 
735
735
    rb_define_alloc_func(cX509Cert, ossl_x509_alloc);
736
736
    rb_define_method(cX509Cert, "initialize", ossl_x509_initialize, -1);
737
737
    rb_define_copy_func(cX509Cert, ossl_x509_copy);
738
 
    
 
738
 
739
739
    rb_define_method(cX509Cert, "to_der", ossl_x509_to_der, 0);
740
740
    rb_define_method(cX509Cert, "to_pem", ossl_x509_to_pem, 0);
741
741
    rb_define_alias(cX509Cert, "to_s", "to_pem");