2003-07-23 12:12:24 -04:00
|
|
|
/*
|
|
|
|
* 'OpenSSL for Ruby' project
|
|
|
|
* Copyright (C) 2001-2002 Michal Rokos <m.rokos@sh.cvut.cz>
|
|
|
|
* All rights reserved.
|
|
|
|
*/
|
|
|
|
/*
|
2015-04-19 23:55:09 -04:00
|
|
|
* This program is licensed under the same licence as Ruby.
|
2003-07-23 12:12:24 -04:00
|
|
|
* (See the file 'LICENCE'.)
|
|
|
|
*/
|
|
|
|
#include "ossl.h"
|
|
|
|
|
2015-05-29 01:55:02 -04:00
|
|
|
#define NewX509Rev(klass) \
|
|
|
|
TypedData_Wrap_Struct((klass), &ossl_x509rev_type, 0)
|
|
|
|
#define SetX509Rev(obj, rev) do { \
|
2011-03-18 23:30:59 -04:00
|
|
|
if (!(rev)) { \
|
2003-07-23 12:12:24 -04:00
|
|
|
ossl_raise(rb_eRuntimeError, "REV wasn't initialized!"); \
|
|
|
|
} \
|
2015-05-29 01:55:02 -04:00
|
|
|
RTYPEDDATA_DATA(obj) = (rev); \
|
2003-07-23 12:12:24 -04:00
|
|
|
} while (0)
|
|
|
|
#define GetX509Rev(obj, rev) do { \
|
2014-12-12 18:59:36 -05:00
|
|
|
TypedData_Get_Struct((obj), X509_REVOKED, &ossl_x509rev_type, (rev)); \
|
2011-03-18 23:30:59 -04:00
|
|
|
if (!(rev)) { \
|
2003-07-23 12:12:24 -04:00
|
|
|
ossl_raise(rb_eRuntimeError, "REV wasn't initialized!"); \
|
|
|
|
} \
|
|
|
|
} while (0)
|
|
|
|
#define SafeGetX509Rev(obj, rev) do { \
|
2011-03-18 23:30:59 -04:00
|
|
|
OSSL_Check_Kind((obj), cX509Rev); \
|
|
|
|
GetX509Rev((obj), (rev)); \
|
2003-07-23 12:12:24 -04:00
|
|
|
} while (0)
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Classes
|
|
|
|
*/
|
|
|
|
VALUE cX509Rev;
|
|
|
|
VALUE eX509RevError;
|
|
|
|
|
2014-12-12 18:59:36 -05:00
|
|
|
static void
|
|
|
|
ossl_x509rev_free(void *ptr)
|
|
|
|
{
|
|
|
|
X509_REVOKED_free(ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const rb_data_type_t ossl_x509rev_type = {
|
|
|
|
"OpenSSL/X509/REV",
|
|
|
|
{
|
|
|
|
0, ossl_x509rev_free,
|
|
|
|
},
|
|
|
|
0, 0, RUBY_TYPED_FREE_IMMEDIATELY,
|
|
|
|
};
|
|
|
|
|
2003-07-23 12:12:24 -04:00
|
|
|
/*
|
|
|
|
* PUBLIC
|
|
|
|
*/
|
2010-04-22 04:04:13 -04:00
|
|
|
VALUE
|
2003-07-23 12:12:24 -04:00
|
|
|
ossl_x509revoked_new(X509_REVOKED *rev)
|
|
|
|
{
|
|
|
|
X509_REVOKED *new;
|
|
|
|
VALUE obj;
|
|
|
|
|
2015-05-29 01:55:02 -04:00
|
|
|
obj = NewX509Rev(cX509Rev);
|
2003-07-23 12:12:24 -04:00
|
|
|
if (!rev) {
|
|
|
|
new = X509_REVOKED_new();
|
|
|
|
} else {
|
|
|
|
new = X509_REVOKED_dup(rev);
|
|
|
|
}
|
|
|
|
if (!new) {
|
|
|
|
ossl_raise(eX509RevError, NULL);
|
|
|
|
}
|
2015-05-29 01:55:02 -04:00
|
|
|
SetX509Rev(obj, new);
|
2003-07-23 12:12:24 -04:00
|
|
|
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
X509_REVOKED *
|
|
|
|
DupX509RevokedPtr(VALUE obj)
|
|
|
|
{
|
|
|
|
X509_REVOKED *rev, *new;
|
|
|
|
|
|
|
|
SafeGetX509Rev(obj, rev);
|
|
|
|
if (!(new = X509_REVOKED_dup(rev))) {
|
|
|
|
ossl_raise(eX509RevError, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
return new;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* PRIVATE
|
|
|
|
*/
|
2010-04-22 04:04:13 -04:00
|
|
|
static VALUE
|
2003-07-23 12:12:24 -04:00
|
|
|
ossl_x509revoked_alloc(VALUE klass)
|
|
|
|
{
|
|
|
|
X509_REVOKED *rev;
|
|
|
|
VALUE obj;
|
|
|
|
|
2015-05-29 01:55:02 -04:00
|
|
|
obj = NewX509Rev(klass);
|
2003-07-23 12:12:24 -04:00
|
|
|
if (!(rev = X509_REVOKED_new())) {
|
|
|
|
ossl_raise(eX509RevError, NULL);
|
|
|
|
}
|
2015-05-29 01:55:02 -04:00
|
|
|
SetX509Rev(obj, rev);
|
2003-07-23 12:12:24 -04:00
|
|
|
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
2010-04-22 04:04:13 -04:00
|
|
|
static VALUE
|
2003-07-23 12:12:24 -04:00
|
|
|
ossl_x509revoked_initialize(int argc, VALUE *argv, VALUE self)
|
|
|
|
{
|
|
|
|
/* EMPTY */
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2016-08-29 01:47:09 -04:00
|
|
|
static VALUE
|
|
|
|
ossl_x509revoked_initialize_copy(VALUE self, VALUE other)
|
|
|
|
{
|
|
|
|
X509_REVOKED *rev, *rev_other, *rev_new;
|
|
|
|
|
|
|
|
rb_check_frozen(self);
|
|
|
|
GetX509Rev(self, rev);
|
|
|
|
SafeGetX509Rev(other, rev_other);
|
|
|
|
|
|
|
|
rev_new = X509_REVOKED_dup(rev_other);
|
|
|
|
if (!rev_new)
|
|
|
|
ossl_raise(eX509RevError, "X509_REVOKED_dup");
|
|
|
|
|
|
|
|
SetX509Rev(self, rev_new);
|
|
|
|
X509_REVOKED_free(rev);
|
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2010-04-22 04:04:13 -04:00
|
|
|
static VALUE
|
2003-07-23 12:12:24 -04:00
|
|
|
ossl_x509revoked_get_serial(VALUE self)
|
|
|
|
{
|
|
|
|
X509_REVOKED *rev;
|
|
|
|
|
|
|
|
GetX509Rev(self, rev);
|
|
|
|
|
2016-06-05 11:35:12 -04:00
|
|
|
return asn1integer_to_num(X509_REVOKED_get0_serialNumber(rev));
|
2003-07-23 12:12:24 -04:00
|
|
|
}
|
|
|
|
|
2010-04-22 04:04:13 -04:00
|
|
|
static VALUE
|
2003-07-23 12:12:24 -04:00
|
|
|
ossl_x509revoked_set_serial(VALUE self, VALUE num)
|
|
|
|
{
|
|
|
|
X509_REVOKED *rev;
|
2016-08-29 01:47:09 -04:00
|
|
|
ASN1_INTEGER *asn1int;
|
2003-07-23 12:12:24 -04:00
|
|
|
|
|
|
|
GetX509Rev(self, rev);
|
2016-08-29 01:47:09 -04:00
|
|
|
asn1int = num_to_asn1integer(num, NULL);
|
|
|
|
if (!X509_REVOKED_set_serialNumber(rev, asn1int)) {
|
|
|
|
ASN1_INTEGER_free(asn1int);
|
|
|
|
ossl_raise(eX509RevError, "X509_REVOKED_set_serialNumber");
|
|
|
|
}
|
|
|
|
ASN1_INTEGER_free(asn1int);
|
2003-07-23 12:12:24 -04:00
|
|
|
|
|
|
|
return num;
|
|
|
|
}
|
|
|
|
|
2010-04-22 04:04:13 -04:00
|
|
|
static VALUE
|
2003-07-23 12:12:24 -04:00
|
|
|
ossl_x509revoked_get_time(VALUE self)
|
|
|
|
{
|
|
|
|
X509_REVOKED *rev;
|
2010-04-22 04:21:01 -04:00
|
|
|
|
2003-07-23 12:12:24 -04:00
|
|
|
GetX509Rev(self, rev);
|
|
|
|
|
2016-06-05 11:35:12 -04:00
|
|
|
return asn1time_to_time(X509_REVOKED_get0_revocationDate(rev));
|
2003-07-23 12:12:24 -04:00
|
|
|
}
|
|
|
|
|
2010-04-22 04:04:13 -04:00
|
|
|
static VALUE
|
2003-07-23 12:12:24 -04:00
|
|
|
ossl_x509revoked_set_time(VALUE self, VALUE time)
|
|
|
|
{
|
|
|
|
X509_REVOKED *rev;
|
2016-08-29 01:47:09 -04:00
|
|
|
ASN1_TIME *asn1time;
|
2003-07-23 12:12:24 -04:00
|
|
|
|
2004-12-15 01:35:55 -05:00
|
|
|
GetX509Rev(self, rev);
|
2016-08-29 01:47:09 -04:00
|
|
|
asn1time = ossl_x509_time_adjust(NULL, time);
|
|
|
|
if (!X509_REVOKED_set_revocationDate(rev, asn1time)) {
|
|
|
|
ASN1_TIME_free(asn1time);
|
|
|
|
ossl_raise(eX509RevError, "X509_REVOKED_set_revocationDate");
|
|
|
|
}
|
|
|
|
ASN1_TIME_free(asn1time);
|
2003-07-23 12:12:24 -04:00
|
|
|
|
|
|
|
return time;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Gets X509v3 extensions as array of X509Ext objects
|
|
|
|
*/
|
2010-04-22 04:04:13 -04:00
|
|
|
static VALUE
|
2003-07-23 12:12:24 -04:00
|
|
|
ossl_x509revoked_get_extensions(VALUE self)
|
|
|
|
{
|
|
|
|
X509_REVOKED *rev;
|
|
|
|
int count, i;
|
|
|
|
X509_EXTENSION *ext;
|
|
|
|
VALUE ary;
|
|
|
|
|
|
|
|
GetX509Rev(self, rev);
|
|
|
|
count = X509_REVOKED_get_ext_count(rev);
|
|
|
|
if (count < 0) {
|
|
|
|
OSSL_Debug("count < 0???");
|
|
|
|
return rb_ary_new();
|
|
|
|
}
|
|
|
|
ary = rb_ary_new2(count);
|
|
|
|
for (i=0; i<count; i++) {
|
|
|
|
ext = X509_REVOKED_get_ext(rev, i);
|
|
|
|
rb_ary_push(ary, ossl_x509ext_new(ext));
|
|
|
|
}
|
|
|
|
|
|
|
|
return ary;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Sets X509_EXTENSIONs
|
|
|
|
*/
|
2010-04-22 04:04:13 -04:00
|
|
|
static VALUE
|
2003-07-23 12:12:24 -04:00
|
|
|
ossl_x509revoked_set_extensions(VALUE self, VALUE ary)
|
|
|
|
{
|
|
|
|
X509_REVOKED *rev;
|
|
|
|
X509_EXTENSION *ext;
|
2015-11-04 02:18:01 -05:00
|
|
|
long i;
|
2003-07-23 12:12:24 -04:00
|
|
|
VALUE item;
|
|
|
|
|
|
|
|
Check_Type(ary, T_ARRAY);
|
2006-09-02 10:42:08 -04:00
|
|
|
for (i=0; i<RARRAY_LEN(ary); i++) {
|
2015-11-04 20:09:17 -05:00
|
|
|
OSSL_Check_Kind(RARRAY_AREF(ary, i), cX509Ext);
|
2003-07-23 12:12:24 -04:00
|
|
|
}
|
2004-12-15 01:35:55 -05:00
|
|
|
GetX509Rev(self, rev);
|
2016-06-05 11:35:12 -04:00
|
|
|
while ((ext = X509_REVOKED_delete_ext(rev, 0)))
|
|
|
|
X509_EXTENSION_free(ext);
|
2006-09-02 10:42:08 -04:00
|
|
|
for (i=0; i<RARRAY_LEN(ary); i++) {
|
2015-11-04 20:09:17 -05:00
|
|
|
item = RARRAY_AREF(ary, i);
|
2016-08-29 01:47:09 -04:00
|
|
|
ext = GetX509ExtPtr(item);
|
2003-07-23 12:12:24 -04:00
|
|
|
if(!X509_REVOKED_add_ext(rev, ext, -1)) {
|
|
|
|
ossl_raise(eX509RevError, NULL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ary;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
ossl_x509revoked_add_extension(VALUE self, VALUE ext)
|
|
|
|
{
|
|
|
|
X509_REVOKED *rev;
|
2010-04-22 04:04:13 -04:00
|
|
|
|
2003-07-23 12:12:24 -04:00
|
|
|
GetX509Rev(self, rev);
|
2016-08-29 01:47:09 -04:00
|
|
|
if (!X509_REVOKED_add_ext(rev, GetX509ExtPtr(ext), -1)) {
|
2003-07-23 12:12:24 -04:00
|
|
|
ossl_raise(eX509RevError, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ext;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* INIT
|
|
|
|
*/
|
|
|
|
void
|
2014-09-30 01:25:32 -04:00
|
|
|
Init_ossl_x509revoked(void)
|
2003-07-23 12:12:24 -04:00
|
|
|
{
|
2016-08-29 01:47:09 -04:00
|
|
|
#if 0
|
|
|
|
mOSSL = rb_define_module("OpenSSL");
|
|
|
|
eOSSLError = rb_define_class_under(mOSSL, "OpenSSLError", rb_eStandardError);
|
|
|
|
mX509 = rb_define_module_under(mOSSL, "X509");
|
|
|
|
#endif
|
|
|
|
|
2003-07-23 12:12:24 -04:00
|
|
|
eX509RevError = rb_define_class_under(mX509, "RevokedError", eOSSLError);
|
|
|
|
|
|
|
|
cX509Rev = rb_define_class_under(mX509, "Revoked", rb_cObject);
|
2010-04-22 04:21:01 -04:00
|
|
|
|
2003-07-23 12:12:24 -04:00
|
|
|
rb_define_alloc_func(cX509Rev, ossl_x509revoked_alloc);
|
|
|
|
rb_define_method(cX509Rev, "initialize", ossl_x509revoked_initialize, -1);
|
2016-08-29 01:47:09 -04:00
|
|
|
rb_define_copy_func(cX509Rev, ossl_x509revoked_initialize_copy);
|
2010-04-22 04:21:01 -04:00
|
|
|
|
2003-07-23 12:12:24 -04:00
|
|
|
rb_define_method(cX509Rev, "serial", ossl_x509revoked_get_serial, 0);
|
|
|
|
rb_define_method(cX509Rev, "serial=", ossl_x509revoked_set_serial, 1);
|
|
|
|
rb_define_method(cX509Rev, "time", ossl_x509revoked_get_time, 0);
|
|
|
|
rb_define_method(cX509Rev, "time=", ossl_x509revoked_set_time, 1);
|
|
|
|
rb_define_method(cX509Rev, "extensions", ossl_x509revoked_get_extensions, 0);
|
|
|
|
rb_define_method(cX509Rev, "extensions=", ossl_x509revoked_set_extensions, 1);
|
|
|
|
rb_define_method(cX509Rev, "add_extension", ossl_x509revoked_add_extension, 1);
|
|
|
|
}
|