Ruby  2.0.0p598(2014-11-13revision48408)
ossl_x509attr.c
Go to the documentation of this file.
1 /*
2  * $Id: ossl_x509attr.c 31103 2011-03-13 09:11:45Z akr $
3  * 'OpenSSL for Ruby' project
4  * Copyright (C) 2001 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 WrapX509Attr(klass, obj, attr) do { \
14  if (!(attr)) { \
15  ossl_raise(rb_eRuntimeError, "ATTR wasn't initialized!"); \
16  } \
17  (obj) = Data_Wrap_Struct((klass), 0, X509_ATTRIBUTE_free, (attr)); \
18 } while (0)
19 #define GetX509Attr(obj, attr) do { \
20  Data_Get_Struct((obj), X509_ATTRIBUTE, (attr)); \
21  if (!(attr)) { \
22  ossl_raise(rb_eRuntimeError, "ATTR wasn't initialized!"); \
23  } \
24 } while (0)
25 #define SafeGetX509Attr(obj, attr) do { \
26  OSSL_Check_Kind((obj), cX509Attr); \
27  GetX509Attr((obj), (attr)); \
28 } while (0)
29 
30 /*
31  * Classes
32  */
35 
36 /*
37  * Public
38  */
39 VALUE
40 ossl_x509attr_new(X509_ATTRIBUTE *attr)
41 {
42  X509_ATTRIBUTE *new;
43  VALUE obj;
44 
45  if (!attr) {
46  new = X509_ATTRIBUTE_new();
47  } else {
48  new = X509_ATTRIBUTE_dup(attr);
49  }
50  if (!new) {
52  }
53  WrapX509Attr(cX509Attr, obj, new);
54 
55  return obj;
56 }
57 
58 X509_ATTRIBUTE *
60 {
61  X509_ATTRIBUTE *attr, *new;
62 
63  SafeGetX509Attr(obj, attr);
64  if (!(new = X509_ATTRIBUTE_dup(attr))) {
66  }
67 
68  return new;
69 }
70 
71 /*
72  * Private
73  */
74 static VALUE
76 {
77  X509_ATTRIBUTE *attr;
78  VALUE obj;
79 
80  if (!(attr = X509_ATTRIBUTE_new()))
82  WrapX509Attr(klass, obj, attr);
83 
84  return obj;
85 }
86 
87 /*
88  * call-seq:
89  * Attribute.new(oid [, value]) => attr
90  */
91 static VALUE
93 {
94  VALUE oid, value;
95  X509_ATTRIBUTE *attr, *x;
96  const unsigned char *p;
97 
98  GetX509Attr(self, attr);
99  if(rb_scan_args(argc, argv, "11", &oid, &value) == 1){
100  oid = ossl_to_der_if_possible(oid);
101  StringValue(oid);
102  p = (unsigned char *)RSTRING_PTR(oid);
103  x = d2i_X509_ATTRIBUTE(&attr, &p, RSTRING_LEN(oid));
104  DATA_PTR(self) = attr;
105  if(!x){
107  }
108  return self;
109  }
110  rb_funcall(self, rb_intern("oid="), 1, oid);
111  rb_funcall(self, rb_intern("value="), 1, value);
112 
113  return self;
114 }
115 
116 /*
117  * call-seq:
118  * attr.oid = string => string
119  */
120 static VALUE
122 {
123  X509_ATTRIBUTE *attr;
124  ASN1_OBJECT *obj;
125  char *s;
126 
127  s = StringValuePtr(oid);
128  obj = OBJ_txt2obj(s, 0);
129  if(!obj) obj = OBJ_txt2obj(s, 1);
130  if(!obj) ossl_raise(eX509AttrError, NULL);
131  GetX509Attr(self, attr);
132  X509_ATTRIBUTE_set1_object(attr, obj);
133 
134  return oid;
135 }
136 
137 /*
138  * call-seq:
139  * attr.oid => string
140  */
141 static VALUE
143 {
144  X509_ATTRIBUTE *attr;
145  ASN1_OBJECT *oid;
146  BIO *out;
147  VALUE ret;
148  int nid;
149 
150  GetX509Attr(self, attr);
151  oid = X509_ATTRIBUTE_get0_object(attr);
152  if ((nid = OBJ_obj2nid(oid)) != NID_undef)
153  ret = rb_str_new2(OBJ_nid2sn(nid));
154  else{
155  if (!(out = BIO_new(BIO_s_mem())))
157  i2a_ASN1_OBJECT(out, oid);
158  ret = ossl_membio2str(out);
159  }
160 
161  return ret;
162 }
163 
164 #if defined(HAVE_ST_X509_ATTRIBUTE_SINGLE) || defined(HAVE_ST_SINGLE)
165 # define OSSL_X509ATTR_IS_SINGLE(attr) ((attr)->single)
166 # define OSSL_X509ATTR_SET_SINGLE(attr) ((attr)->single = 1)
167 #else
168 # define OSSL_X509ATTR_IS_SINGLE(attr) (!(attr)->set)
169 # define OSSL_X509ATTR_SET_SINGLE(attr) ((attr)->set = 0)
170 #endif
171 
172 /*
173  * call-seq:
174  * attr.value = asn1 => asn1
175  */
176 static VALUE
178 {
179  X509_ATTRIBUTE *attr;
180  ASN1_TYPE *a1type;
181 
182  if(!(a1type = ossl_asn1_get_asn1type(value)))
183  ossl_raise(eASN1Error, "could not get ASN1_TYPE");
184  if(ASN1_TYPE_get(a1type) == V_ASN1_SEQUENCE){
185  ASN1_TYPE_free(a1type);
186  ossl_raise(eASN1Error, "couldn't set SEQUENCE for attribute value.");
187  }
188  GetX509Attr(self, attr);
189  if(attr->value.set){
190  if(OSSL_X509ATTR_IS_SINGLE(attr)) ASN1_TYPE_free(attr->value.single);
191  else sk_ASN1_TYPE_free(attr->value.set);
192  }
194  attr->value.single = a1type;
195 
196  return value;
197 }
198 
199 /*
200  * call-seq:
201  * attr.value => asn1
202  */
203 static VALUE
205 {
206  X509_ATTRIBUTE *attr;
207  VALUE str, asn1;
208  long length;
209  unsigned char *p;
210 
211  GetX509Attr(self, attr);
212  if(attr->value.ptr == NULL) return Qnil;
213  if(OSSL_X509ATTR_IS_SINGLE(attr)){
214  length = i2d_ASN1_TYPE(attr->value.single, NULL);
215  str = rb_str_new(0, length);
216  p = (unsigned char *)RSTRING_PTR(str);
217  i2d_ASN1_TYPE(attr->value.single, &p);
218  ossl_str_adjust(str, p);
219  }
220  else{
221  length = i2d_ASN1_SET_OF_ASN1_TYPE(attr->value.set,
222  (unsigned char **) NULL, i2d_ASN1_TYPE,
223  V_ASN1_SET, V_ASN1_UNIVERSAL, 0);
224  str = rb_str_new(0, length);
225  p = (unsigned char *)RSTRING_PTR(str);
226  i2d_ASN1_SET_OF_ASN1_TYPE(attr->value.set, &p,
227  i2d_ASN1_TYPE, V_ASN1_SET, V_ASN1_UNIVERSAL, 0);
228  ossl_str_adjust(str, p);
229  }
230  asn1 = rb_funcall(mASN1, rb_intern("decode"), 1, str);
231 
232  return asn1;
233 }
234 
235 /*
236  * call-seq:
237  * attr.to_der => string
238  */
239 static VALUE
241 {
242  X509_ATTRIBUTE *attr;
243  VALUE str;
244  int len;
245  unsigned char *p;
246 
247  GetX509Attr(self, attr);
248  if((len = i2d_X509_ATTRIBUTE(attr, NULL)) <= 0)
250  str = rb_str_new(0, len);
251  p = (unsigned char *)RSTRING_PTR(str);
252  if(i2d_X509_ATTRIBUTE(attr, &p) <= 0)
254  rb_str_set_len(str, p - (unsigned char*)RSTRING_PTR(str));
255 
256  return str;
257 }
258 
259 /*
260  * X509_ATTRIBUTE init
261  */
262 void
264 {
266 
275 }
static VALUE ossl_x509attr_to_der(VALUE self)
static VALUE ossl_x509attr_set_oid(VALUE self, VALUE oid)
Win32OLEIDispatch * p
Definition: win32ole.c:786
static VALUE ossl_x509attr_get_oid(VALUE self)
C_block * out
Definition: crypt.c:308
#define OSSL_X509ATTR_SET_SINGLE(attr)
static VALUE ossl_x509attr_alloc(VALUE klass)
Definition: ossl_x509attr.c:75
#define ossl_str_adjust(str, p)
Definition: ossl.h:138
int ret
Definition: tcltklib.c:280
VALUE mASN1
Definition: ossl_asn1.c:189
void rb_define_alloc_func(VALUE, rb_alloc_func_t)
#define RSTRING_PTR(str)
VALUE rb_funcall(VALUE, ID, int,...)
Calls a method.
Definition: vm_eval.c:773
VALUE rb_define_class_under(VALUE outer, const char *name, VALUE super)
Defines a class under the namespace of outer.
Definition: class.c:549
#define rb_str_new2
VALUE ossl_membio2str(BIO *bio)
Definition: ossl_bio.c:77
#define OSSL_X509ATTR_IS_SINGLE(attr)
VALUE ossl_to_der_if_possible(VALUE obj)
Definition: ossl.c:283
X509_ATTRIBUTE * DupX509AttrPtr(VALUE)
Definition: ossl_x509attr.c:59
#define Qnil
Definition: tcltklib.c:1895
#define StringValuePtr(v)
static VALUE char * str
Definition: tcltklib.c:3546
static VALUE VALUE obj
Definition: tcltklib.c:3157
#define RSTRING_LEN(str)
VALUE eOSSLError
Definition: ossl.c:264
static VALUE ossl_x509attr_set_value(VALUE self, VALUE value)
ASN1_TYPE * ossl_asn1_get_asn1type(VALUE obj)
Definition: ossl_asn1.c:539
VALUE eX509AttrError
Definition: ossl_x509attr.c:34
VALUE * argv
Definition: tcltklib.c:1970
volatile VALUE value
Definition: tcltklib.c:9441
#define StringValue(v)
register char * s
Definition: os2.c:56
int rb_scan_args(int argc, const VALUE *argv, const char *fmt,...)
Definition: class.c:1574
size_t length
Definition: tcltklib.c:4558
int argc
Definition: tcltklib.c:1969
VALUE mX509
Definition: ossl_x509.c:13
VALUE ossl_x509attr_new(X509_ATTRIBUTE *)
Definition: ossl_x509attr.c:40
DATA_PTR(self)
RUBY_EXTERN VALUE rb_cObject
Definition: ripper.y:1426
klass
Definition: tcltklib.c:3503
void ossl_raise(VALUE exc, const char *fmt,...)
Definition: ossl.c:333
void Init_ossl_x509attr(void)
#define WrapX509Attr(klass, obj, attr)
Definition: ossl_x509attr.c:13
static VALUE ossl_x509attr_get_value(VALUE self)
VALUE rb_str_new(const char *, long)
Definition: string.c:425
unsigned long VALUE
Definition: ripper.y:104
static VALUE ossl_x509attr_initialize(int argc, VALUE *argv, VALUE self)
Definition: ossl_x509attr.c:92
#define rb_intern(str)
#define NULL
Definition: _sdbm.c:103
void rb_define_method(VALUE klass, const char *name, VALUE(*func)(ANYARGS), int argc)
Definition: class.c:1348
#define SafeGetX509Attr(obj, attr)
Definition: ossl_x509attr.c:25
VALUE cX509Attr
Definition: ossl_x509attr.c:33
VALUE eASN1Error
Definition: ossl_asn1.c:190
#define GetX509Attr(obj, attr)
Definition: ossl_x509attr.c:19
size_t len
Definition: tcltklib.c:3567
void rb_str_set_len(VALUE, long)
Definition: string.c:1838