2 * $Id: ossl_x509crl.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>
8 * This program is licenced under the same licence as Ruby.
9 * (See the file 'LICENCE'.)
13 #define WrapX509CRL(klass, obj, crl) do { \
15 ossl_raise(rb_eRuntimeError, "CRL wasn't initialized!"); \
17 obj = Data_Wrap_Struct(klass, 0, X509_CRL_free, crl); \
19 #define GetX509CRL(obj, crl) do { \
20 Data_Get_Struct(obj, X509_CRL, crl); \
22 ossl_raise(rb_eRuntimeError, "CRL wasn't initialized!"); \
25 #define SafeGetX509CRL(obj, crl) do { \
26 OSSL_Check_Kind(obj, cX509CRL); \
27 GetX509CRL(obj, crl); \
40 GetX509CRLPtr(VALUE obj
)
44 SafeGetX509CRL(obj
, crl
);
50 DupX509CRLPtr(VALUE obj
)
54 SafeGetX509CRL(obj
, crl
);
55 CRYPTO_add(&crl
->references
, 1, CRYPTO_LOCK_X509_CRL
);
61 ossl_x509crl_new(X509_CRL
*crl
)
66 tmp
= crl
? X509_CRL_dup(crl
) : X509_CRL_new();
67 if(!tmp
) ossl_raise(eX509CRLError
, NULL
);
68 WrapX509CRL(cX509CRL
, obj
, tmp
);
77 ossl_x509crl_alloc(VALUE klass
)
82 if (!(crl
= X509_CRL_new())) {
83 ossl_raise(eX509CRLError
, NULL
);
85 WrapX509CRL(klass
, obj
, crl
);
91 ossl_x509crl_initialize(int argc
, VALUE
*argv
, VALUE self
)
97 if (rb_scan_args(argc
, argv
, "01", &arg
) == 0) {
100 arg
= ossl_to_der_if_possible(arg
);
101 in
= ossl_obj2bio(arg
);
102 crl
= PEM_read_bio_X509_CRL(in
, (X509_CRL
**)&DATA_PTR(self
), NULL
, NULL
);
105 crl
= d2i_X509_CRL_bio(in
, (X509_CRL
**)&DATA_PTR(self
));
108 if (!crl
) ossl_raise(eX509CRLError
, NULL
);
114 ossl_x509crl_copy(VALUE self
, VALUE other
)
116 X509_CRL
*a
, *b
, *crl
;
118 rb_check_frozen(self
);
119 if (self
== other
) return self
;
121 SafeGetX509CRL(other
, b
);
122 if (!(crl
= X509_CRL_dup(b
))) {
123 ossl_raise(eX509CRLError
, NULL
);
126 DATA_PTR(self
) = crl
;
132 ossl_x509crl_get_version(VALUE self
)
137 GetX509CRL(self
, crl
);
138 ver
= X509_CRL_get_version(crl
);
140 return LONG2NUM(ver
);
144 ossl_x509crl_set_version(VALUE self
, VALUE version
)
149 if ((ver
= NUM2LONG(version
)) < 0) {
150 ossl_raise(eX509CRLError
, "version must be >= 0!");
152 GetX509CRL(self
, crl
);
153 if (!X509_CRL_set_version(crl
, ver
)) {
154 ossl_raise(eX509CRLError
, NULL
);
161 ossl_x509crl_get_signature_algorithm(VALUE self
)
168 GetX509CRL(self
, crl
);
169 if (!(out
= BIO_new(BIO_s_mem()))) {
170 ossl_raise(eX509CRLError
, NULL
);
172 if (!i2a_ASN1_OBJECT(out
, crl
->sig_alg
->algorithm
)) {
174 ossl_raise(eX509CRLError
, NULL
);
176 BIO_get_mem_ptr(out
, &buf
);
177 str
= rb_str_new(buf
->data
, buf
->length
);
183 ossl_x509crl_get_issuer(VALUE self
)
187 GetX509CRL(self
, crl
);
189 return ossl_x509name_new(X509_CRL_get_issuer(crl
)); /* NO DUP - don't free */
193 ossl_x509crl_set_issuer(VALUE self
, VALUE issuer
)
197 GetX509CRL(self
, crl
);
199 if (!X509_CRL_set_issuer_name(crl
, GetX509NamePtr(issuer
))) { /* DUPs name */
200 ossl_raise(eX509CRLError
, NULL
);
206 ossl_x509crl_get_last_update(VALUE self
)
210 GetX509CRL(self
, crl
);
212 return asn1time_to_time(X509_CRL_get_lastUpdate(crl
));
216 ossl_x509crl_set_last_update(VALUE self
, VALUE time
)
221 sec
= time_to_time_t(time
);
222 GetX509CRL(self
, crl
);
223 if (!X509_time_adj(crl
->crl
->lastUpdate
, 0, &sec
)) {
224 ossl_raise(eX509CRLError
, NULL
);
231 ossl_x509crl_get_next_update(VALUE self
)
235 GetX509CRL(self
, crl
);
237 return asn1time_to_time(X509_CRL_get_nextUpdate(crl
));
241 ossl_x509crl_set_next_update(VALUE self
, VALUE time
)
246 sec
= time_to_time_t(time
);
247 GetX509CRL(self
, crl
);
248 /* This must be some thinko in OpenSSL */
249 if (!(crl
->crl
->nextUpdate
= X509_time_adj(crl
->crl
->nextUpdate
, 0, &sec
))){
250 ossl_raise(eX509CRLError
, NULL
);
257 ossl_x509crl_get_revoked(VALUE self
)
264 GetX509CRL(self
, crl
);
265 num
= sk_X509_CRL_num(X509_CRL_get_REVOKED(crl
));
267 OSSL_Debug("num < 0???");
270 ary
= rb_ary_new2(num
);
271 for(i
=0; i
<num
; i
++) {
272 /* NO DUP - don't free! */
273 rev
= (X509_REVOKED
*)sk_X509_CRL_value(X509_CRL_get_REVOKED(crl
), i
);
274 revoked
= ossl_x509revoked_new(rev
);
275 rb_ary_push(ary
, revoked
);
282 ossl_x509crl_set_revoked(VALUE self
, VALUE ary
)
288 Check_Type(ary
, T_ARRAY
);
289 /* All ary members should be X509 Revoked */
290 for (i
=0; i
<RARRAY(ary
)->len
; i
++) {
291 OSSL_Check_Kind(RARRAY(ary
)->ptr
[i
], cX509Rev
);
293 GetX509CRL(self
, crl
);
294 sk_X509_REVOKED_pop_free(crl
->crl
->revoked
, X509_REVOKED_free
);
295 crl
->crl
->revoked
= NULL
;
296 for (i
=0; i
<RARRAY(ary
)->len
; i
++) {
297 rev
= DupX509RevokedPtr(RARRAY(ary
)->ptr
[i
]);
298 if (!X509_CRL_add0_revoked(crl
, rev
)) { /* NO DUP - don't free! */
299 ossl_raise(eX509CRLError
, NULL
);
308 ossl_x509crl_add_revoked(VALUE self
, VALUE revoked
)
313 GetX509CRL(self
, crl
);
314 rev
= DupX509RevokedPtr(revoked
);
315 if (!X509_CRL_add0_revoked(crl
, rev
)) { /* NO DUP - don't free! */
316 ossl_raise(eX509CRLError
, NULL
);
324 ossl_x509crl_sign(VALUE self
, VALUE key
, VALUE digest
)
330 GetX509CRL(self
, crl
);
331 pkey
= GetPrivPKeyPtr(key
); /* NO NEED TO DUP */
332 md
= GetDigestPtr(digest
);
333 if (!X509_CRL_sign(crl
, pkey
, md
)) {
334 ossl_raise(eX509CRLError
, NULL
);
341 ossl_x509crl_verify(VALUE self
, VALUE key
)
346 GetX509CRL(self
, crl
);
347 if ((ret
= X509_CRL_verify(crl
, GetPKeyPtr(key
))) < 0) {
348 ossl_raise(eX509CRLError
, NULL
);
358 ossl_x509crl_to_der(VALUE self
)
365 GetX509CRL(self
, crl
);
366 if (!(out
= BIO_new(BIO_s_mem()))) {
367 ossl_raise(eX509CRLError
, NULL
);
369 if (!i2d_X509_CRL_bio(out
, crl
)) {
371 ossl_raise(eX509CRLError
, NULL
);
373 BIO_get_mem_ptr(out
, &buf
);
374 str
= rb_str_new(buf
->data
, buf
->length
);
381 ossl_x509crl_to_pem(VALUE self
)
388 GetX509CRL(self
, crl
);
389 if (!(out
= BIO_new(BIO_s_mem()))) {
390 ossl_raise(eX509CRLError
, NULL
);
392 if (!PEM_write_bio_X509_CRL(out
, crl
)) {
394 ossl_raise(eX509CRLError
, NULL
);
396 BIO_get_mem_ptr(out
, &buf
);
397 str
= rb_str_new(buf
->data
, buf
->length
);
404 ossl_x509crl_to_text(VALUE self
)
411 GetX509CRL(self
, crl
);
412 if (!(out
= BIO_new(BIO_s_mem()))) {
413 ossl_raise(eX509CRLError
, NULL
);
415 if (!X509_CRL_print(out
, crl
)) {
417 ossl_raise(eX509CRLError
, NULL
);
419 BIO_get_mem_ptr(out
, &buf
);
420 str
= rb_str_new(buf
->data
, buf
->length
);
427 * Gets X509v3 extensions as array of X509Ext objects
430 ossl_x509crl_get_extensions(VALUE self
)
437 GetX509CRL(self
, crl
);
438 count
= X509_CRL_get_ext_count(crl
);
440 OSSL_Debug("count < 0???");
443 ary
= rb_ary_new2(count
);
444 for (i
=0; i
<count
; i
++) {
445 ext
= X509_CRL_get_ext(crl
, i
); /* NO DUP - don't free! */
446 rb_ary_push(ary
, ossl_x509ext_new(ext
));
453 * Sets X509_EXTENSIONs
456 ossl_x509crl_set_extensions(VALUE self
, VALUE ary
)
462 Check_Type(ary
, T_ARRAY
);
463 /* All ary members should be X509 Extensions */
464 for (i
=0; i
<RARRAY(ary
)->len
; i
++) {
465 OSSL_Check_Kind(RARRAY(ary
)->ptr
[i
], cX509Ext
);
467 GetX509CRL(self
, crl
);
468 sk_X509_EXTENSION_pop_free(crl
->crl
->extensions
, X509_EXTENSION_free
);
469 crl
->crl
->extensions
= NULL
;
470 for (i
=0; i
<RARRAY(ary
)->len
; i
++) {
471 ext
= DupX509ExtPtr(RARRAY(ary
)->ptr
[i
]);
472 if(!X509_CRL_add_ext(crl
, ext
, -1)) { /* DUPs ext - FREE it */
473 X509_EXTENSION_free(ext
);
474 ossl_raise(eX509CRLError
, NULL
);
476 X509_EXTENSION_free(ext
);
483 ossl_x509crl_add_extension(VALUE self
, VALUE extension
)
488 GetX509CRL(self
, crl
);
489 ext
= DupX509ExtPtr(extension
);
490 if (!X509_CRL_add_ext(crl
, ext
, -1)) { /* DUPs ext - FREE it */
491 X509_EXTENSION_free(ext
);
492 ossl_raise(eX509CRLError
, NULL
);
494 X509_EXTENSION_free(ext
);
505 eX509CRLError
= rb_define_class_under(mX509
, "CRLError", eOSSLError
);
507 cX509CRL
= rb_define_class_under(mX509
, "CRL", rb_cObject
);
509 rb_define_alloc_func(cX509CRL
, ossl_x509crl_alloc
);
510 rb_define_method(cX509CRL
, "initialize", ossl_x509crl_initialize
, -1);
511 rb_define_copy_func(cX509CRL
, ossl_x509crl_copy
);
513 rb_define_method(cX509CRL
, "version", ossl_x509crl_get_version
, 0);
514 rb_define_method(cX509CRL
, "version=", ossl_x509crl_set_version
, 1);
515 rb_define_method(cX509CRL
, "signature_algorithm", ossl_x509crl_get_signature_algorithm
, 0);
516 rb_define_method(cX509CRL
, "issuer", ossl_x509crl_get_issuer
, 0);
517 rb_define_method(cX509CRL
, "issuer=", ossl_x509crl_set_issuer
, 1);
518 rb_define_method(cX509CRL
, "last_update", ossl_x509crl_get_last_update
, 0);
519 rb_define_method(cX509CRL
, "last_update=", ossl_x509crl_set_last_update
, 1);
520 rb_define_method(cX509CRL
, "next_update", ossl_x509crl_get_next_update
, 0);
521 rb_define_method(cX509CRL
, "next_update=", ossl_x509crl_set_next_update
, 1);
522 rb_define_method(cX509CRL
, "revoked", ossl_x509crl_get_revoked
, 0);
523 rb_define_method(cX509CRL
, "revoked=", ossl_x509crl_set_revoked
, 1);
524 rb_define_method(cX509CRL
, "add_revoked", ossl_x509crl_add_revoked
, 1);
525 rb_define_method(cX509CRL
, "sign", ossl_x509crl_sign
, 2);
526 rb_define_method(cX509CRL
, "verify", ossl_x509crl_verify
, 1);
527 rb_define_method(cX509CRL
, "to_der", ossl_x509crl_to_der
, 0);
528 rb_define_method(cX509CRL
, "to_pem", ossl_x509crl_to_pem
, 0);
529 rb_define_alias(cX509CRL
, "to_s", "to_pem");
530 rb_define_method(cX509CRL
, "to_text", ossl_x509crl_to_text
, 0);
531 rb_define_method(cX509CRL
, "extensions", ossl_x509crl_get_extensions
, 0);
532 rb_define_method(cX509CRL
, "extensions=", ossl_x509crl_set_extensions
, 1);
533 rb_define_method(cX509CRL
, "add_extension", ossl_x509crl_add_extension
, 1);