Ruby
2.0.0p247(2013-06-27revision41674)
|
00001 /* 00002 * $Id: ossl_x509revoked.c 31128 2011-03-19 03:30:59Z akr $ 00003 * 'OpenSSL for Ruby' project 00004 * Copyright (C) 2001-2002 Michal Rokos <m.rokos@sh.cvut.cz> 00005 * All rights reserved. 00006 */ 00007 /* 00008 * This program is licenced under the same licence as Ruby. 00009 * (See the file 'LICENCE'.) 00010 */ 00011 #include "ossl.h" 00012 00013 #define WrapX509Rev(klass, obj, rev) do { \ 00014 if (!(rev)) { \ 00015 ossl_raise(rb_eRuntimeError, "REV wasn't initialized!"); \ 00016 } \ 00017 (obj) = Data_Wrap_Struct((klass), 0, X509_REVOKED_free, (rev)); \ 00018 } while (0) 00019 #define GetX509Rev(obj, rev) do { \ 00020 Data_Get_Struct((obj), X509_REVOKED, (rev)); \ 00021 if (!(rev)) { \ 00022 ossl_raise(rb_eRuntimeError, "REV wasn't initialized!"); \ 00023 } \ 00024 } while (0) 00025 #define SafeGetX509Rev(obj, rev) do { \ 00026 OSSL_Check_Kind((obj), cX509Rev); \ 00027 GetX509Rev((obj), (rev)); \ 00028 } while (0) 00029 00030 /* 00031 * Classes 00032 */ 00033 VALUE cX509Rev; 00034 VALUE eX509RevError; 00035 00036 /* 00037 * PUBLIC 00038 */ 00039 VALUE 00040 ossl_x509revoked_new(X509_REVOKED *rev) 00041 { 00042 X509_REVOKED *new; 00043 VALUE obj; 00044 00045 if (!rev) { 00046 new = X509_REVOKED_new(); 00047 } else { 00048 new = X509_REVOKED_dup(rev); 00049 } 00050 if (!new) { 00051 ossl_raise(eX509RevError, NULL); 00052 } 00053 WrapX509Rev(cX509Rev, obj, new); 00054 00055 return obj; 00056 } 00057 00058 X509_REVOKED * 00059 DupX509RevokedPtr(VALUE obj) 00060 { 00061 X509_REVOKED *rev, *new; 00062 00063 SafeGetX509Rev(obj, rev); 00064 if (!(new = X509_REVOKED_dup(rev))) { 00065 ossl_raise(eX509RevError, NULL); 00066 } 00067 00068 return new; 00069 } 00070 00071 /* 00072 * PRIVATE 00073 */ 00074 static VALUE 00075 ossl_x509revoked_alloc(VALUE klass) 00076 { 00077 X509_REVOKED *rev; 00078 VALUE obj; 00079 00080 if (!(rev = X509_REVOKED_new())) { 00081 ossl_raise(eX509RevError, NULL); 00082 } 00083 WrapX509Rev(klass, obj, rev); 00084 00085 return obj; 00086 } 00087 00088 static VALUE 00089 ossl_x509revoked_initialize(int argc, VALUE *argv, VALUE self) 00090 { 00091 /* EMPTY */ 00092 return self; 00093 } 00094 00095 static VALUE 00096 ossl_x509revoked_get_serial(VALUE self) 00097 { 00098 X509_REVOKED *rev; 00099 00100 GetX509Rev(self, rev); 00101 00102 return asn1integer_to_num(rev->serialNumber); 00103 } 00104 00105 static VALUE 00106 ossl_x509revoked_set_serial(VALUE self, VALUE num) 00107 { 00108 X509_REVOKED *rev; 00109 00110 GetX509Rev(self, rev); 00111 rev->serialNumber = num_to_asn1integer(num, rev->serialNumber); 00112 00113 return num; 00114 } 00115 00116 static VALUE 00117 ossl_x509revoked_get_time(VALUE self) 00118 { 00119 X509_REVOKED *rev; 00120 00121 GetX509Rev(self, rev); 00122 00123 return asn1time_to_time(rev->revocationDate); 00124 } 00125 00126 static VALUE 00127 ossl_x509revoked_set_time(VALUE self, VALUE time) 00128 { 00129 X509_REVOKED *rev; 00130 time_t sec; 00131 00132 sec = time_to_time_t(time); 00133 GetX509Rev(self, rev); 00134 if (!X509_time_adj(rev->revocationDate, 0, &sec)) { 00135 ossl_raise(eX509RevError, NULL); 00136 } 00137 00138 return time; 00139 } 00140 /* 00141 * Gets X509v3 extensions as array of X509Ext objects 00142 */ 00143 static VALUE 00144 ossl_x509revoked_get_extensions(VALUE self) 00145 { 00146 X509_REVOKED *rev; 00147 int count, i; 00148 X509_EXTENSION *ext; 00149 VALUE ary; 00150 00151 GetX509Rev(self, rev); 00152 count = X509_REVOKED_get_ext_count(rev); 00153 if (count < 0) { 00154 OSSL_Debug("count < 0???"); 00155 return rb_ary_new(); 00156 } 00157 ary = rb_ary_new2(count); 00158 for (i=0; i<count; i++) { 00159 ext = X509_REVOKED_get_ext(rev, i); 00160 rb_ary_push(ary, ossl_x509ext_new(ext)); 00161 } 00162 00163 return ary; 00164 } 00165 00166 /* 00167 * Sets X509_EXTENSIONs 00168 */ 00169 static VALUE 00170 ossl_x509revoked_set_extensions(VALUE self, VALUE ary) 00171 { 00172 X509_REVOKED *rev; 00173 X509_EXTENSION *ext; 00174 int i; 00175 VALUE item; 00176 00177 Check_Type(ary, T_ARRAY); 00178 for (i=0; i<RARRAY_LEN(ary); i++) { 00179 OSSL_Check_Kind(RARRAY_PTR(ary)[i], cX509Ext); 00180 } 00181 GetX509Rev(self, rev); 00182 sk_X509_EXTENSION_pop_free(rev->extensions, X509_EXTENSION_free); 00183 rev->extensions = NULL; 00184 for (i=0; i<RARRAY_LEN(ary); i++) { 00185 item = RARRAY_PTR(ary)[i]; 00186 ext = DupX509ExtPtr(item); 00187 if(!X509_REVOKED_add_ext(rev, ext, -1)) { 00188 ossl_raise(eX509RevError, NULL); 00189 } 00190 } 00191 00192 return ary; 00193 } 00194 00195 static VALUE 00196 ossl_x509revoked_add_extension(VALUE self, VALUE ext) 00197 { 00198 X509_REVOKED *rev; 00199 00200 GetX509Rev(self, rev); 00201 if(!X509_REVOKED_add_ext(rev, DupX509ExtPtr(ext), -1)) { 00202 ossl_raise(eX509RevError, NULL); 00203 } 00204 00205 return ext; 00206 } 00207 00208 /* 00209 * INIT 00210 */ 00211 void 00212 Init_ossl_x509revoked() 00213 { 00214 eX509RevError = rb_define_class_under(mX509, "RevokedError", eOSSLError); 00215 00216 cX509Rev = rb_define_class_under(mX509, "Revoked", rb_cObject); 00217 00218 rb_define_alloc_func(cX509Rev, ossl_x509revoked_alloc); 00219 rb_define_method(cX509Rev, "initialize", ossl_x509revoked_initialize, -1); 00220 00221 rb_define_method(cX509Rev, "serial", ossl_x509revoked_get_serial, 0); 00222 rb_define_method(cX509Rev, "serial=", ossl_x509revoked_set_serial, 1); 00223 rb_define_method(cX509Rev, "time", ossl_x509revoked_get_time, 0); 00224 rb_define_method(cX509Rev, "time=", ossl_x509revoked_set_time, 1); 00225 rb_define_method(cX509Rev, "extensions", ossl_x509revoked_get_extensions, 0); 00226 rb_define_method(cX509Rev, "extensions=", ossl_x509revoked_set_extensions, 1); 00227 rb_define_method(cX509Rev, "add_extension", ossl_x509revoked_add_extension, 1); 00228 } 00229 00230