/* * 'OpenSSL for Ruby' project * Copyright (C) 2001-2002 Michal Rokos * All rights reserved. */ /* * This program is licensed under the same licence as Ruby. * (See the file 'LICENCE'.) */ #include "ossl.h" #define NewX509Rev(klass) \ TypedData_Wrap_Struct((klass), &ossl_x509rev_type, 0) #define SetX509Rev(obj, rev) do { \ if (!(rev)) { \ ossl_raise(rb_eRuntimeError, "REV wasn't initialized!"); \ } \ RTYPEDDATA_DATA(obj) = (rev); \ } while (0) #define GetX509Rev(obj, rev) do { \ TypedData_Get_Struct((obj), X509_REVOKED, &ossl_x509rev_type, (rev)); \ if (!(rev)) { \ ossl_raise(rb_eRuntimeError, "REV wasn't initialized!"); \ } \ } while (0) #define SafeGetX509Rev(obj, rev) do { \ OSSL_Check_Kind((obj), cX509Rev); \ GetX509Rev((obj), (rev)); \ } while (0) /* * Classes */ VALUE cX509Rev; VALUE eX509RevError; static void ossl_x509rev_free(void *ptr) { X509_REVOKED_free(ptr); } static const rb_data_type_t ossl_x509rev_type = { "OpenSSL/X509/REV", { 0, ossl_x509rev_free, }, 0, 0, RUBY_TYPED_FREE_IMMEDIATELY, }; /* * PUBLIC */ VALUE ossl_x509revoked_new(X509_REVOKED *rev) { X509_REVOKED *new; VALUE obj; obj = NewX509Rev(cX509Rev); if (!rev) { new = X509_REVOKED_new(); } else { new = X509_REVOKED_dup(rev); } if (!new) { ossl_raise(eX509RevError, NULL); } SetX509Rev(obj, new); return obj; } X509_REVOKED * DupX509RevokedPtr(VALUE obj) { X509_REVOKED *rev, *new; SafeGetX509Rev(obj, rev); if (!(new = X509_REVOKED_dup(rev))) { ossl_raise(eX509RevError, NULL); } return new; } /* * PRIVATE */ static VALUE ossl_x509revoked_alloc(VALUE klass) { X509_REVOKED *rev; VALUE obj; obj = NewX509Rev(klass); if (!(rev = X509_REVOKED_new())) { ossl_raise(eX509RevError, NULL); } SetX509Rev(obj, rev); return obj; } static VALUE ossl_x509revoked_initialize(int argc, VALUE *argv, VALUE self) { /* EMPTY */ return self; } static VALUE ossl_x509revoked_get_serial(VALUE self) { X509_REVOKED *rev; GetX509Rev(self, rev); return asn1integer_to_num(X509_REVOKED_get0_serialNumber(rev)); } static VALUE ossl_x509revoked_set_serial(VALUE self, VALUE num) { X509_REVOKED *rev; ASN1_INTEGER *ai; GetX509Rev(self, rev); ai = X509_REVOKED_get0_serialNumber(rev); X509_REVOKED_set_serialNumber(rev, num_to_asn1integer(num, ai)); return num; } static VALUE ossl_x509revoked_get_time(VALUE self) { X509_REVOKED *rev; GetX509Rev(self, rev); return asn1time_to_time(X509_REVOKED_get0_revocationDate(rev)); } static VALUE ossl_x509revoked_set_time(VALUE self, VALUE time) { X509_REVOKED *rev; GetX509Rev(self, rev); if (!ossl_x509_time_adjust(X509_REVOKED_get0_revocationDate(rev), time)) ossl_raise(eX509RevError, NULL); return time; } /* * Gets X509v3 extensions as array of X509Ext objects */ static VALUE ossl_x509revoked_get_extensions(VALUE self) { X509_REVOKED *rev; int count, i; X509_EXTENSION *ext; VALUE ary; GetX509Rev(self, rev); count = X509_REVOKED_get_ext_count(rev); if (count < 0) { OSSL_Debug("count < 0???"); return rb_ary_new(); } ary = rb_ary_new2(count); for (i=0; i