aboutsummaryrefslogtreecommitdiffstats
path: root/ossl_x509attr.c
blob: c72e7944411df74fc2054ff0087576bd056a0b2b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/*
 * $Id$
 * 'OpenSSL for Ruby' project
 * Copyright (C) 2001 Michal Rokos <m.rokos@sh.cvut.cz>
 * All rights reserved.
 */
/*
 * This program is licenced under the same licence as Ruby.
 * (See the file 'LICENCE'.)
 */
#include "ossl.h"

#define MakeX509Attr(obj, attrp) {\
	obj = Data_Make_Struct(cX509Attribute, ossl_x509attr, 0, ossl_x509attr_free, attrp);\
}

#define GetX509Attr(obj, attrp) {\
	Data_Get_Struct(obj, ossl_x509attr, attrp);\
}

/*
 * Classes
 */
VALUE cX509Attribute;
VALUE eX509AttributeError;

/*
 * Struct
 */
typedef struct ossl_x509attr_st {
	X509_ATTRIBUTE *attribute;
} ossl_x509attr;


static void ossl_x509attr_free(ossl_x509attr *attrp)
{
	if (attrp) {
		if (attrp->attribute) X509_ATTRIBUTE_free(attrp->attribute);
		free(attrp);
	}
}

/*
 * public
 */
VALUE ossl_x509attr_new2(X509_ATTRIBUTE *attr)
{
	ossl_x509attr *attrp = NULL;
	VALUE obj;

	MakeX509Attr(obj, attrp);

	if (!(attrp->attribute = X509_ATTRIBUTE_dup(attr))) {
		rb_raise(eX509AttributeError, "%s", ossl_error());
	}

	return obj;
}

X509_ATTRIBUTE *ossl_x509attr_get_X509_ATTRIBUTE(VALUE self)
{
	ossl_x509attr *attrp = NULL;
	X509_ATTRIBUTE *attr = NULL;

	GetX509Attr(self, attrp);

	if (!(attr = X509_ATTRIBUTE_dup(attrp->attribute))) {
		rb_raise(eX509AttributeError, "%s", ossl_error());
	}
	return attr;
}

/*
 * private
 */
static VALUE ossl_x509attr_s_new(int argc, VALUE *argv, VALUE klass)
{
	ossl_x509attr *attrp = NULL;
	VALUE obj;

	MakeX509Attr(obj, attrp);
	rb_obj_call_init(obj, argc, argv);
	return obj;
}

X509_ATTRIBUTE *ary_to_x509attr(VALUE ary)
{
	X509_ATTRIBUTE *attr = NULL;
	int nid = NID_undef;
	VALUE item;

	Check_Type(ary, T_ARRAY);

	if (RARRAY(ary)->len != 2) {
		rb_raise(eX509AttributeError, "unsupported ary structure");
	}

	/* key [0] */
	item = RARRAY(ary)->ptr[0];
	Check_Type(item, T_STRING);
	if (!(nid = OBJ_ln2nid(RSTRING(item)->ptr)))
		if (!(nid = OBJ_sn2nid(RSTRING(item)->ptr)))
			rb_raise(eX509AttributeError, "%s", ossl_error());

	/* data [1] */
	item = RARRAY(ary)->ptr[1];
	Check_Type(item, T_STRING);

	if (!(attr = X509_ATTRIBUTE_create(nid, MBSTRING_ASC, RSTRING(item)->ptr))) {
		rb_raise(eX509AttributeError, "%s", ossl_error());
	}

	return attr;
}

static VALUE ossl_x509attr_initialize(int argc, VALUE *argv, VALUE self)
{
	ossl_x509attr *attrp = NULL;
	X509_ATTRIBUTE *attr = NULL;
	VALUE arg1, arg2, ary;

	switch (rb_scan_args(argc, argv, "02", &arg1, &arg2)) {
		case 0:
			if (!(attr = X509_ATTRIBUTE_new())) {
				rb_raise(eX509AttributeError, "%s", ossl_error());
			}
			break;
		case 1:
			Check_Type(arg1, T_ARRAY);
			attr = ary_to_x509attr(ary);
			break;
		case 2:
			ary = rb_ary_new2(2);
			rb_ary_push(ary, arg1);
			rb_ary_push(ary, arg2);
			attr = ary_to_x509attr(ary);
			break;
		default:
			rb_raise(rb_eTypeError, "unsupported type");
	}

	GetX509Attr(self, attrp);
	attrp->attribute = attr;

	return self;
}

/*
 * X509_ATTRIBUTE init
 */
void Init_ossl_x509attr(VALUE mX509)
{
	eX509AttributeError = rb_define_class_under(mX509, "AttributeError", rb_eStandardError);

	cX509Attribute = rb_define_class_under(mX509, "Attribute", rb_cObject);
	rb_define_singleton_method(cX509Attribute, "new", ossl_x509attr_s_new, -1);
	rb_define_method(cX509Attribute, "initialize", ossl_x509attr_initialize, -1);
/*
 * TODO:
	rb_define_method(cX509Attribute, "to_str", ossl_x509attr_to_str, 0);
	rb_define_method(cX509Attribute, "to_a", ossl_x509attr_to_a, 0);
 */
}