diff options
author | Michal Rokos <m.rokos@sh.cvut.cz> | 2001-11-16 11:20:13 +0000 |
---|---|---|
committer | Michal Rokos <m.rokos@sh.cvut.cz> | 2001-11-16 11:20:13 +0000 |
commit | 8903e757c052d3a99aada758bb628ec135360e60 (patch) | |
tree | 5f0e99803c1fc5c6909fd7d325d0f279265b97cf /ossl_x509attr.c | |
download | ruby-openssl-history-8903e757c052d3a99aada758bb628ec135360e60.tar.gz |
Initial revision
Diffstat (limited to 'ossl_x509attr.c')
-rw-r--r-- | ossl_x509attr.c | 164 |
1 files changed, 164 insertions, 0 deletions
diff --git a/ossl_x509attr.c b/ossl_x509attr.c new file mode 100644 index 0000000..c72e794 --- /dev/null +++ b/ossl_x509attr.c @@ -0,0 +1,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); + */ +} + |