Ruby  2.4.2p198(2017-09-14revision59899)
ossl_x509revoked.c
Go to the documentation of this file.
1 /*
2  * 'OpenSSL for Ruby' project
3  * Copyright (C) 2001-2002 Michal Rokos <m.rokos@sh.cvut.cz>
4  * All rights reserved.
5  */
6 /*
7  * This program is licensed under the same licence as Ruby.
8  * (See the file 'LICENCE'.)
9  */
10 #include "ossl.h"
11 
12 #define NewX509Rev(klass) \
13  TypedData_Wrap_Struct((klass), &ossl_x509rev_type, 0)
14 #define SetX509Rev(obj, rev) do { \
15  if (!(rev)) { \
16  ossl_raise(rb_eRuntimeError, "REV wasn't initialized!"); \
17  } \
18  RTYPEDDATA_DATA(obj) = (rev); \
19 } while (0)
20 #define GetX509Rev(obj, rev) do { \
21  TypedData_Get_Struct((obj), X509_REVOKED, &ossl_x509rev_type, (rev)); \
22  if (!(rev)) { \
23  ossl_raise(rb_eRuntimeError, "REV wasn't initialized!"); \
24  } \
25 } while (0)
26 #define SafeGetX509Rev(obj, rev) do { \
27  OSSL_Check_Kind((obj), cX509Rev); \
28  GetX509Rev((obj), (rev)); \
29 } while (0)
30 
31 /*
32  * Classes
33  */
36 
37 static void
39 {
40  X509_REVOKED_free(ptr);
41 }
42 
44  "OpenSSL/X509/REV",
45  {
47  },
49 };
50 
51 /*
52  * PUBLIC
53  */
54 VALUE
55 ossl_x509revoked_new(X509_REVOKED *rev)
56 {
57  X509_REVOKED *new;
58  VALUE obj;
59 
60  obj = NewX509Rev(cX509Rev);
61  if (!rev) {
62  new = X509_REVOKED_new();
63  } else {
64  new = X509_REVOKED_dup(rev);
65  }
66  if (!new) {
68  }
69  SetX509Rev(obj, new);
70 
71  return obj;
72 }
73 
74 X509_REVOKED *
76 {
77  X509_REVOKED *rev, *new;
78 
79  SafeGetX509Rev(obj, rev);
80  if (!(new = X509_REVOKED_dup(rev))) {
82  }
83 
84  return new;
85 }
86 
87 /*
88  * PRIVATE
89  */
90 static VALUE
92 {
93  X509_REVOKED *rev;
94  VALUE obj;
95 
96  obj = NewX509Rev(klass);
97  if (!(rev = X509_REVOKED_new())) {
99  }
100  SetX509Rev(obj, rev);
101 
102  return obj;
103 }
104 
105 static VALUE
107 {
108  /* EMPTY */
109  return self;
110 }
111 
112 static VALUE
114 {
115  X509_REVOKED *rev, *rev_other, *rev_new;
116 
117  rb_check_frozen(self);
118  GetX509Rev(self, rev);
119  SafeGetX509Rev(other, rev_other);
120 
121  rev_new = X509_REVOKED_dup(rev_other);
122  if (!rev_new)
123  ossl_raise(eX509RevError, "X509_REVOKED_dup");
124 
125  SetX509Rev(self, rev_new);
126  X509_REVOKED_free(rev);
127 
128  return self;
129 }
130 
131 static VALUE
133 {
134  X509_REVOKED *rev;
135 
136  GetX509Rev(self, rev);
137 
139 }
140 
141 static VALUE
143 {
144  X509_REVOKED *rev;
145  ASN1_INTEGER *asn1int;
146 
147  GetX509Rev(self, rev);
148  asn1int = num_to_asn1integer(num, NULL);
149  if (!X509_REVOKED_set_serialNumber(rev, asn1int)) {
150  ASN1_INTEGER_free(asn1int);
151  ossl_raise(eX509RevError, "X509_REVOKED_set_serialNumber");
152  }
153  ASN1_INTEGER_free(asn1int);
154 
155  return num;
156 }
157 
158 static VALUE
160 {
161  X509_REVOKED *rev;
162 
163  GetX509Rev(self, rev);
164 
166 }
167 
168 static VALUE
170 {
171  X509_REVOKED *rev;
172  ASN1_TIME *asn1time;
173 
174  GetX509Rev(self, rev);
175  asn1time = ossl_x509_time_adjust(NULL, time);
176  if (!X509_REVOKED_set_revocationDate(rev, asn1time)) {
177  ASN1_TIME_free(asn1time);
178  ossl_raise(eX509RevError, "X509_REVOKED_set_revocationDate");
179  }
180  ASN1_TIME_free(asn1time);
181 
182  return time;
183 }
184 /*
185  * Gets X509v3 extensions as array of X509Ext objects
186  */
187 static VALUE
189 {
190  X509_REVOKED *rev;
191  int count, i;
192  X509_EXTENSION *ext;
193  VALUE ary;
194 
195  GetX509Rev(self, rev);
196  count = X509_REVOKED_get_ext_count(rev);
197  if (count < 0) {
198  OSSL_Debug("count < 0???");
199  return rb_ary_new();
200  }
201  ary = rb_ary_new2(count);
202  for (i=0; i<count; i++) {
203  ext = X509_REVOKED_get_ext(rev, i);
204  rb_ary_push(ary, ossl_x509ext_new(ext));
205  }
206 
207  return ary;
208 }
209 
210 /*
211  * Sets X509_EXTENSIONs
212  */
213 static VALUE
215 {
216  X509_REVOKED *rev;
217  X509_EXTENSION *ext;
218  long i;
219  VALUE item;
220 
221  Check_Type(ary, T_ARRAY);
222  for (i=0; i<RARRAY_LEN(ary); i++) {
224  }
225  GetX509Rev(self, rev);
226  while ((ext = X509_REVOKED_delete_ext(rev, 0)))
227  X509_EXTENSION_free(ext);
228  for (i=0; i<RARRAY_LEN(ary); i++) {
229  item = RARRAY_AREF(ary, i);
230  ext = GetX509ExtPtr(item);
231  if(!X509_REVOKED_add_ext(rev, ext, -1)) {
233  }
234  }
235 
236  return ary;
237 }
238 
239 static VALUE
241 {
242  X509_REVOKED *rev;
243 
244  GetX509Rev(self, rev);
245  if (!X509_REVOKED_add_ext(rev, GetX509ExtPtr(ext), -1)) {
247  }
248 
249  return ext;
250 }
251 
252 /*
253  * INIT
254  */
255 void
257 {
258 #if 0
259  mOSSL = rb_define_module("OpenSSL");
262 #endif
263 
265 
267 
271 
279 }
static VALUE ossl_x509revoked_add_extension(VALUE self, VALUE ext)
VALUE rb_eStandardError
Definition: error.c:760
VALUE mOSSL
Definition: ossl.c:213
#define SetX509Rev(obj, rev)
#define RARRAY_LEN(a)
Definition: ruby.h:1026
static VALUE ossl_x509revoked_get_serial(VALUE self)
#define RUBY_TYPED_FREE_IMMEDIATELY
Definition: ruby.h:1145
int count
Definition: encoding.c:56
static VALUE ossl_x509revoked_initialize_copy(VALUE self, VALUE other)
static VALUE ossl_x509revoked_set_time(VALUE self, VALUE time)
VALUE rb_ary_push(VALUE ary, VALUE item)
Definition: array.c:905
ASN1_TIME * ossl_x509_time_adjust(ASN1_TIME *s, VALUE time)
Definition: ossl_x509.c:19
VALUE rb_define_class_under(VALUE outer, const char *name, VALUE super)
Defines a class under the namespace of outer.
Definition: class.c:693
#define Check_Type(v, t)
Definition: ruby.h:562
static VALUE ossl_x509revoked_initialize(int argc, VALUE *argv, VALUE self)
VALUE ossl_x509revoked_new(X509_REVOKED *rev)
void rb_define_alloc_func(VALUE, rb_alloc_func_t)
VALUE cX509Ext
Definition: ossl_x509ext.c:47
static void ossl_x509rev_free(void *ptr)
#define T_ARRAY
Definition: ruby.h:498
X509_REVOKED * DupX509RevokedPtr(VALUE obj)
VALUE asn1integer_to_num(const ASN1_INTEGER *ai)
Definition: ossl_asn1.c:112
#define rb_ary_new2
Definition: intern.h:90
#define GetX509Rev(obj, rev)
static VALUE ossl_x509revoked_set_serial(VALUE self, VALUE num)
#define rb_define_copy_func(klass, func)
Definition: ruby_missing.h:13
#define X509_REVOKED_get0_serialNumber(x)
RUBY_EXTERN VALUE rb_cObject
Definition: ruby.h:1872
VALUE rb_ary_new(void)
Definition: array.c:493
#define SafeGetX509Rev(obj, rev)
VALUE eOSSLError
Definition: ossl.c:218
int argc
Definition: ruby.c:183
VALUE ossl_x509ext_new(X509_EXTENSION *)
Definition: ossl_x509ext.c:69
#define X509_REVOKED_get0_revocationDate(x)
#define NewX509Rev(klass)
#define X509_REVOKED_dup(rev)
VALUE eX509RevError
unsigned long VALUE
Definition: ruby.h:85
static const rb_data_type_t ossl_x509rev_type
static VALUE ossl_x509revoked_set_extensions(VALUE self, VALUE ary)
VALUE mX509
Definition: ossl_x509.c:12
#define OSSL_Debug
Definition: ossl.h:155
static VALUE ossl_x509revoked_get_time(VALUE self)
VALUE rb_define_module_under(VALUE outer, const char *name)
Definition: class.c:790
X509_EXTENSION * GetX509ExtPtr(VALUE)
Definition: ossl_x509ext.c:89
#define RARRAY_AREF(a, i)
Definition: ruby.h:1040
VALUE asn1time_to_time(const ASN1_TIME *time)
Definition: ossl_asn1.c:22
#define OSSL_Check_Kind(obj, klass)
Definition: ossl.h:52
void ossl_raise(VALUE exc, const char *fmt,...)
Definition: ossl.c:278
void Init_ossl_x509revoked(void)
VALUE cX509Rev
static VALUE ossl_x509revoked_alloc(VALUE klass)
static VALUE ossl_x509revoked_get_extensions(VALUE self)
#define rb_check_frozen(obj)
Definition: intern.h:276
VALUE rb_define_module(const char *name)
Definition: class.c:768
#define NULL
Definition: _sdbm.c:102
void rb_define_method(VALUE klass, const char *name, VALUE(*func)(ANYARGS), int argc)
Definition: class.c:1515
char ** argv
Definition: ruby.c:184
ASN1_INTEGER * num_to_asn1integer(VALUE obj, ASN1_INTEGER *ai)
Definition: ossl_asn1.c:135