1/* 2 * $Id: ossl_x509revoked.c 31128 2011-03-19 03:30:59Z akr $ 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 WrapX509Rev(klass, obj, rev) do { \ 14 if (!(rev)) { \ 15 ossl_raise(rb_eRuntimeError, "REV wasn't initialized!"); \ 16 } \ 17 (obj) = Data_Wrap_Struct((klass), 0, X509_REVOKED_free, (rev)); \ 18} while (0) 19#define GetX509Rev(obj, rev) do { \ 20 Data_Get_Struct((obj), X509_REVOKED, (rev)); \ 21 if (!(rev)) { \ 22 ossl_raise(rb_eRuntimeError, "REV wasn't initialized!"); \ 23 } \ 24} while (0) 25#define SafeGetX509Rev(obj, rev) do { \ 26 OSSL_Check_Kind((obj), cX509Rev); \ 27 GetX509Rev((obj), (rev)); \ 28} while (0) 29 30/* 31 * Classes 32 */ 33VALUE cX509Rev; 34VALUE eX509RevError; 35 36/* 37 * PUBLIC 38 */ 39VALUE 40ossl_x509revoked_new(X509_REVOKED *rev) 41{ 42 X509_REVOKED *new; 43 VALUE obj; 44 45 if (!rev) { 46 new = X509_REVOKED_new(); 47 } else { 48 new = X509_REVOKED_dup(rev); 49 } 50 if (!new) { 51 ossl_raise(eX509RevError, NULL); 52 } 53 WrapX509Rev(cX509Rev, obj, new); 54 55 return obj; 56} 57 58X509_REVOKED * 59DupX509RevokedPtr(VALUE obj) 60{ 61 X509_REVOKED *rev, *new; 62 63 SafeGetX509Rev(obj, rev); 64 if (!(new = X509_REVOKED_dup(rev))) { 65 ossl_raise(eX509RevError, NULL); 66 } 67 68 return new; 69} 70 71/* 72 * PRIVATE 73 */ 74static VALUE 75ossl_x509revoked_alloc(VALUE klass) 76{ 77 X509_REVOKED *rev; 78 VALUE obj; 79 80 if (!(rev = X509_REVOKED_new())) { 81 ossl_raise(eX509RevError, NULL); 82 } 83 WrapX509Rev(klass, obj, rev); 84 85 return obj; 86} 87 88static VALUE 89ossl_x509revoked_initialize(int argc, VALUE *argv, VALUE self) 90{ 91 /* EMPTY */ 92 return self; 93} 94 95static VALUE 96ossl_x509revoked_get_serial(VALUE self) 97{ 98 X509_REVOKED *rev; 99 100 GetX509Rev(self, rev); 101 102 return asn1integer_to_num(rev->serialNumber); 103} 104 105static VALUE 106ossl_x509revoked_set_serial(VALUE self, VALUE num) 107{ 108 X509_REVOKED *rev; 109 110 GetX509Rev(self, rev); 111 rev->serialNumber = num_to_asn1integer(num, rev->serialNumber); 112 113 return num; 114} 115 116static VALUE 117ossl_x509revoked_get_time(VALUE self) 118{ 119 X509_REVOKED *rev; 120 121 GetX509Rev(self, rev); 122 123 return asn1time_to_time(rev->revocationDate); 124} 125 126static VALUE 127ossl_x509revoked_set_time(VALUE self, VALUE time) 128{ 129 X509_REVOKED *rev; 130 time_t sec; 131 132 sec = time_to_time_t(time); 133 GetX509Rev(self, rev); 134 if (!X509_time_adj(rev->revocationDate, 0, &sec)) { 135 ossl_raise(eX509RevError, NULL); 136 } 137 138 return time; 139} 140/* 141 * Gets X509v3 extensions as array of X509Ext objects 142 */ 143static VALUE 144ossl_x509revoked_get_extensions(VALUE self) 145{ 146 X509_REVOKED *rev; 147 int count, i; 148 X509_EXTENSION *ext; 149 VALUE ary; 150 151 GetX509Rev(self, rev); 152 count = X509_REVOKED_get_ext_count(rev); 153 if (count < 0) { 154 OSSL_Debug("count < 0???"); 155 return rb_ary_new(); 156 } 157 ary = rb_ary_new2(count); 158 for (i=0; i<count; i++) { 159 ext = X509_REVOKED_get_ext(rev, i); 160 rb_ary_push(ary, ossl_x509ext_new(ext)); 161 } 162 163 return ary; 164} 165 166/* 167 * Sets X509_EXTENSIONs 168 */ 169static VALUE 170ossl_x509revoked_set_extensions(VALUE self, VALUE ary) 171{ 172 X509_REVOKED *rev; 173 X509_EXTENSION *ext; 174 int i; 175 VALUE item; 176 177 Check_Type(ary, T_ARRAY); 178 for (i=0; i<RARRAY_LEN(ary); i++) { 179 OSSL_Check_Kind(RARRAY_PTR(ary)[i], cX509Ext); 180 } 181 GetX509Rev(self, rev); 182 sk_X509_EXTENSION_pop_free(rev->extensions, X509_EXTENSION_free); 183 rev->extensions = NULL; 184 for (i=0; i<RARRAY_LEN(ary); i++) { 185 item = RARRAY_PTR(ary)[i]; 186 ext = DupX509ExtPtr(item); 187 if(!X509_REVOKED_add_ext(rev, ext, -1)) { 188 ossl_raise(eX509RevError, NULL); 189 } 190 } 191 192 return ary; 193} 194 195static VALUE 196ossl_x509revoked_add_extension(VALUE self, VALUE ext) 197{ 198 X509_REVOKED *rev; 199 200 GetX509Rev(self, rev); 201 if(!X509_REVOKED_add_ext(rev, DupX509ExtPtr(ext), -1)) { 202 ossl_raise(eX509RevError, NULL); 203 } 204 205 return ext; 206} 207 208/* 209 * INIT 210 */ 211void 212Init_ossl_x509revoked() 213{ 214 eX509RevError = rb_define_class_under(mX509, "RevokedError", eOSSLError); 215 216 cX509Rev = rb_define_class_under(mX509, "Revoked", rb_cObject); 217 218 rb_define_alloc_func(cX509Rev, ossl_x509revoked_alloc); 219 rb_define_method(cX509Rev, "initialize", ossl_x509revoked_initialize, -1); 220 221 rb_define_method(cX509Rev, "serial", ossl_x509revoked_get_serial, 0); 222 rb_define_method(cX509Rev, "serial=", ossl_x509revoked_set_serial, 1); 223 rb_define_method(cX509Rev, "time", ossl_x509revoked_get_time, 0); 224 rb_define_method(cX509Rev, "time=", ossl_x509revoked_set_time, 1); 225 rb_define_method(cX509Rev, "extensions", ossl_x509revoked_get_extensions, 0); 226 rb_define_method(cX509Rev, "extensions=", ossl_x509revoked_set_extensions, 1); 227 rb_define_method(cX509Rev, "add_extension", ossl_x509revoked_add_extension, 1); 228} 229 230