Ruby 2.0.0p353(2013-11-22revision43784)

- Main Page
- Modules
- Data Structures
- Files

- File List
- Globals

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 */
33 VALUE cX509Attr;
34 VALUE eX509AttrError;
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) {
51 ossl_raise(eX509AttrError, NULL);
52 }
53 WrapX509Attr(cX509Attr, obj, new);
54
55 return obj;
56 }
57
58 X509_ATTRIBUTE *
59 DupX509AttrPtr(VALUE obj)
60 {
61 X509_ATTRIBUTE *attr, *new;
62
63 SafeGetX509Attr(obj, attr);
64 if (!(new = X509_ATTRIBUTE_dup(attr))) {
65 ossl_raise(eX509AttrError, NULL);
66 }
67
68 return new;
69 }
70
71 /*
72 * Private
73 */
74 static VALUE
75 ossl_x509attr_alloc(VALUE klass)
76 {
77 X509_ATTRIBUTE *attr;
78 VALUE obj;
79
80 if (!(attr = X509_ATTRIBUTE_new()))
81 ossl_raise(eX509AttrError, NULL);
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
92 ossl_x509attr_initialize(int argc, VALUE *argv, VALUE self)
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){
106 ossl_raise(eX509AttrError, NULL);
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
121 ossl_x509attr_set_oid(VALUE self, VALUE oid)
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
142 ossl_x509attr_get_oid(VALUE self)
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())))
156 ossl_raise(eX509AttrError, NULL);
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
177 ossl_x509attr_set_value(VALUE self, VALUE 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 }
193 OSSL_X509ATTR_SET_SINGLE(attr);
194 attr->value.single = a1type;
195
196 return value;
197 }
198
199 /*
200 * call-seq:
201 * attr.value => asn1
202 */
203 static VALUE
204 ossl_x509attr_get_value(VALUE self)
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
240 ossl_x509attr_to_der(VALUE self)
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)
249 ossl_raise(eX509AttrError, NULL);
250 str = rb_str_new(0, len);
251 p = (unsigned char *)RSTRING_PTR(str);
252 if(i2d_X509_ATTRIBUTE(attr, &p) <= 0)
253 ossl_raise(eX509AttrError, NULL);
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
263 Init_ossl_x509attr()
264 {
265 eX509AttrError = rb_define_class_under(mX509, "AttributeError", eOSSLError);
266
267 cX509Attr = rb_define_class_under(mX509, "Attribute", rb_cObject);
268 rb_define_alloc_func(cX509Attr, ossl_x509attr_alloc);
269 rb_define_method(cX509Attr, "initialize", ossl_x509attr_initialize, -1);
270 rb_define_method(cX509Attr, "oid=", ossl_x509attr_set_oid, 1);
271 rb_define_method(cX509Attr, "oid", ossl_x509attr_get_oid, 0);
272 rb_define_method(cX509Attr, "value=", ossl_x509attr_set_value, 1);
273 rb_define_method(cX509Attr, "value", ossl_x509attr_get_value, 0);
274 rb_define_method(cX509Attr, "to_der", ossl_x509attr_to_der, 0);
275 }
ossl_x509attr_to_der
static VALUE ossl_x509attr_to_der(VALUE self)
Definition: ossl_x509attr.c:240
ossl_x509attr_set_oid
static VALUE ossl_x509attr_set_oid(VALUE self, VALUE oid)
Definition: ossl_x509attr.c:121
p
Win32OLEIDispatch * p
Definition: win32ole.c:786
ossl_x509attr_get_oid
static VALUE ossl_x509attr_get_oid(VALUE self)
Definition: ossl_x509attr.c:142
out
C_block * out
Definition: crypt.c:308
OSSL_X509ATTR_SET_SINGLE
#define OSSL_X509ATTR_SET_SINGLE(attr)
Definition: ossl_x509attr.c:169
ossl_x509attr_alloc
static VALUE ossl_x509attr_alloc(VALUE klass)
Definition: ossl_x509attr.c:75
ossl_str_adjust
#define ossl_str_adjust(str, p)
Definition: ossl.h:138
ret
int ret
Definition: tcltklib.c:280
mASN1
VALUE mASN1
Definition: ossl_asn1.c:189
rb_define_alloc_func
void rb_define_alloc_func(VALUE, rb_alloc_func_t)
RSTRING_PTR
#define RSTRING_PTR(str)
rb_funcall
VALUE rb_funcall(VALUE, ID, int,...)
Calls a method.
Definition: vm_eval.c:774
rb_define_class_under
VALUE rb_define_class_under(VALUE outer, const char *name, VALUE super)
Defines a class under the namespace of outer.
Definition: class.c:545
rb_str_new2
#define rb_str_new2
ossl_membio2str
VALUE ossl_membio2str(BIO *bio)
Definition: ossl_bio.c:77
OSSL_X509ATTR_IS_SINGLE
#define OSSL_X509ATTR_IS_SINGLE(attr)
Definition: ossl_x509attr.c:168
ossl_to_der_if_possible
VALUE ossl_to_der_if_possible(VALUE obj)
Definition: ossl.c:283
DupX509AttrPtr
X509_ATTRIBUTE * DupX509AttrPtr(VALUE)
Definition: ossl_x509attr.c:59
Qnil
#define Qnil
Definition: tcltklib.c:1896
StringValuePtr
#define StringValuePtr(v)
str
static VALUE char * str
Definition: tcltklib.c:3547
obj
static VALUE VALUE obj
Definition: tcltklib.c:3158
RSTRING_LEN
#define RSTRING_LEN(str)
eOSSLError
VALUE eOSSLError
Definition: ossl.c:264
ossl_x509attr_set_value
static VALUE ossl_x509attr_set_value(VALUE self, VALUE value)
Definition: ossl_x509attr.c:177
ossl_asn1_get_asn1type
ASN1_TYPE * ossl_asn1_get_asn1type(VALUE obj)
Definition: ossl_asn1.c:539
eX509AttrError
VALUE eX509AttrError
Definition: ossl_x509attr.c:34
argv
VALUE * argv
Definition: tcltklib.c:1971
value
volatile VALUE value
Definition: tcltklib.c:9442
StringValue
#define StringValue(v)
s
register char * s
Definition: os2.c:56
rb_scan_args
int rb_scan_args(int argc, const VALUE *argv, const char *fmt,...)
Definition: class.c:1570
length
size_t length
Definition: tcltklib.c:4559
argc
int argc
Definition: tcltklib.c:1970
mX509
VALUE mX509
Definition: ossl_x509.c:13
ossl_x509attr_new
VALUE ossl_x509attr_new(X509_ATTRIBUTE *)
Definition: ossl_x509attr.c:40
DATA_PTR
DATA_PTR(self)
rb_cObject
RUBY_EXTERN VALUE rb_cObject
Definition: ripper.y:1426
klass
klass
Definition: tcltklib.c:3504
ossl_raise
void ossl_raise(VALUE exc, const char *fmt,...)
Definition: ossl.c:328
Init_ossl_x509attr
void Init_ossl_x509attr(void)
Definition: ossl_x509attr.c:263
WrapX509Attr
#define WrapX509Attr(klass, obj, attr)
Definition: ossl_x509attr.c:13
ossl_x509attr_get_value
static VALUE ossl_x509attr_get_value(VALUE self)
Definition: ossl_x509attr.c:204
rb_str_new
VALUE rb_str_new(const char *, long)
Definition: string.c:425
ossl.h
VALUE
unsigned long VALUE
Definition: ripper.y:104
ossl_x509attr_initialize
static VALUE ossl_x509attr_initialize(int argc, VALUE *argv, VALUE self)
Definition: ossl_x509attr.c:92
rb_intern
#define rb_intern(str)
NULL
#define NULL
Definition: _sdbm.c:103
rb_define_method
void rb_define_method(VALUE klass, const char *name, VALUE(*func)(ANYARGS), int argc)
Definition: class.c:1344
SafeGetX509Attr
#define SafeGetX509Attr(obj, attr)
Definition: ossl_x509attr.c:25
cX509Attr
VALUE cX509Attr
Definition: ossl_x509attr.c:33
eASN1Error
VALUE eASN1Error
Definition: ossl_asn1.c:190
GetX509Attr
#define GetX509Attr(obj, attr)
Definition: ossl_x509attr.c:19
len
size_t len
Definition: tcltklib.c:3568
rb_str_set_len
void rb_str_set_len(VALUE, long)
Definition: string.c:1837