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 NewX509Ext(klass) \
|
|
|
|
TypedData_Wrap_Struct((klass), &ossl_x509ext_type, 0)
|
|
|
|
#define SetX509Ext(obj, ext) do { \
|
2011-03-15 19:41:17 -04:00
|
|
|
if (!(ext)) { \
|
2003-07-23 12:12:24 -04:00
|
|
|
ossl_raise(rb_eRuntimeError, "EXT wasn't initialized!"); \
|
|
|
|
} \
|
2015-05-29 01:55:02 -04:00
|
|
|
RTYPEDDATA_DATA(obj) = (ext); \
|
2003-07-23 12:12:24 -04:00
|
|
|
} while (0)
|
|
|
|
#define GetX509Ext(obj, ext) do { \
|
2014-12-12 18:46:30 -05:00
|
|
|
TypedData_Get_Struct((obj), X509_EXTENSION, &ossl_x509ext_type, (ext)); \
|
2011-03-15 19:41:17 -04:00
|
|
|
if (!(ext)) { \
|
2003-07-23 12:12:24 -04:00
|
|
|
ossl_raise(rb_eRuntimeError, "EXT wasn't initialized!"); \
|
|
|
|
} \
|
|
|
|
} while (0)
|
2005-11-22 17:29:13 -05:00
|
|
|
#define MakeX509ExtFactory(klass, obj, ctx) do { \
|
2015-05-29 01:55:02 -04:00
|
|
|
(obj) = TypedData_Wrap_Struct((klass), &ossl_x509extfactory_type, 0); \
|
2011-03-15 19:41:17 -04:00
|
|
|
if (!((ctx) = OPENSSL_malloc(sizeof(X509V3_CTX)))) \
|
2005-11-22 17:29:13 -05:00
|
|
|
ossl_raise(rb_eRuntimeError, "CTX wasn't allocated!"); \
|
2011-03-15 19:41:17 -04:00
|
|
|
X509V3_set_ctx((ctx), NULL, NULL, NULL, NULL, 0); \
|
2015-05-29 01:55:02 -04:00
|
|
|
RTYPEDDATA_DATA(obj) = (ctx); \
|
2005-11-22 17:29:13 -05:00
|
|
|
} while (0)
|
2003-07-23 12:12:24 -04:00
|
|
|
#define GetX509ExtFactory(obj, ctx) do { \
|
2014-12-12 18:46:42 -05:00
|
|
|
TypedData_Get_Struct((obj), X509V3_CTX, &ossl_x509extfactory_type, (ctx)); \
|
2011-03-15 19:41:17 -04:00
|
|
|
if (!(ctx)) { \
|
2003-07-23 12:12:24 -04:00
|
|
|
ossl_raise(rb_eRuntimeError, "CTX wasn't initialized!"); \
|
|
|
|
} \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Classes
|
|
|
|
*/
|
|
|
|
VALUE cX509Ext;
|
|
|
|
VALUE cX509ExtFactory;
|
|
|
|
VALUE eX509ExtError;
|
|
|
|
|
2014-12-12 18:46:30 -05:00
|
|
|
static void
|
|
|
|
ossl_x509ext_free(void *ptr)
|
|
|
|
{
|
|
|
|
X509_EXTENSION_free(ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const rb_data_type_t ossl_x509ext_type = {
|
|
|
|
"OpenSSL/X509/EXTENSION",
|
|
|
|
{
|
|
|
|
0, ossl_x509ext_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_x509ext_new(X509_EXTENSION *ext)
|
|
|
|
{
|
|
|
|
X509_EXTENSION *new;
|
|
|
|
VALUE obj;
|
|
|
|
|
2015-05-29 01:55:02 -04:00
|
|
|
obj = NewX509Ext(cX509Ext);
|
2003-07-23 12:12:24 -04:00
|
|
|
if (!ext) {
|
|
|
|
new = X509_EXTENSION_new();
|
|
|
|
} else {
|
|
|
|
new = X509_EXTENSION_dup(ext);
|
|
|
|
}
|
|
|
|
if (!new) {
|
|
|
|
ossl_raise(eX509ExtError, NULL);
|
|
|
|
}
|
2015-05-29 01:55:02 -04:00
|
|
|
SetX509Ext(obj, new);
|
2010-04-22 04:21:01 -04:00
|
|
|
|
2003-07-23 12:12:24 -04:00
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
X509_EXTENSION *
|
|
|
|
GetX509ExtPtr(VALUE obj)
|
|
|
|
{
|
|
|
|
X509_EXTENSION *ext;
|
|
|
|
|
2017-09-03 08:35:27 -04:00
|
|
|
GetX509Ext(obj, ext);
|
2003-07-23 12:12:24 -04:00
|
|
|
|
|
|
|
return ext;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Private
|
|
|
|
*/
|
|
|
|
/*
|
|
|
|
* Ext factory
|
|
|
|
*/
|
|
|
|
static void
|
2014-12-12 18:46:42 -05:00
|
|
|
ossl_x509extfactory_free(void *ctx)
|
2003-07-23 12:12:24 -04:00
|
|
|
{
|
2003-09-08 06:31:38 -04:00
|
|
|
OPENSSL_free(ctx);
|
2003-07-23 12:12:24 -04:00
|
|
|
}
|
|
|
|
|
2014-12-12 18:46:42 -05:00
|
|
|
static const rb_data_type_t ossl_x509extfactory_type = {
|
|
|
|
"OpenSSL/X509/EXTENSION/Factory",
|
|
|
|
{
|
|
|
|
0, ossl_x509extfactory_free,
|
|
|
|
},
|
|
|
|
0, 0, RUBY_TYPED_FREE_IMMEDIATELY,
|
|
|
|
};
|
|
|
|
|
2010-04-22 04:04:13 -04:00
|
|
|
static VALUE
|
2003-07-23 12:12:24 -04:00
|
|
|
ossl_x509extfactory_alloc(VALUE klass)
|
|
|
|
{
|
|
|
|
X509V3_CTX *ctx;
|
|
|
|
VALUE obj;
|
|
|
|
|
|
|
|
MakeX509ExtFactory(klass, obj, ctx);
|
2008-04-20 18:32:06 -04:00
|
|
|
rb_iv_set(obj, "@config", Qnil);
|
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_x509extfactory_set_issuer_cert(VALUE self, VALUE cert)
|
|
|
|
{
|
|
|
|
X509V3_CTX *ctx;
|
|
|
|
|
|
|
|
GetX509ExtFactory(self, ctx);
|
2003-09-08 06:31:38 -04:00
|
|
|
rb_iv_set(self, "@issuer_certificate", cert);
|
2003-11-01 04:24:55 -05:00
|
|
|
ctx->issuer_cert = GetX509CertPtr(cert); /* NO DUP NEEDED */
|
2003-07-23 12:12:24 -04:00
|
|
|
|
|
|
|
return cert;
|
|
|
|
}
|
|
|
|
|
2010-04-22 04:04:13 -04:00
|
|
|
static VALUE
|
2003-07-23 12:12:24 -04:00
|
|
|
ossl_x509extfactory_set_subject_cert(VALUE self, VALUE cert)
|
|
|
|
{
|
|
|
|
X509V3_CTX *ctx;
|
|
|
|
|
|
|
|
GetX509ExtFactory(self, ctx);
|
2003-09-08 06:31:38 -04:00
|
|
|
rb_iv_set(self, "@subject_certificate", cert);
|
2003-11-01 04:24:55 -05:00
|
|
|
ctx->subject_cert = GetX509CertPtr(cert); /* NO DUP NEEDED */
|
2003-07-23 12:12:24 -04:00
|
|
|
|
|
|
|
return cert;
|
|
|
|
}
|
|
|
|
|
2010-04-22 04:04:13 -04:00
|
|
|
static VALUE
|
2003-07-23 12:12:24 -04:00
|
|
|
ossl_x509extfactory_set_subject_req(VALUE self, VALUE req)
|
|
|
|
{
|
|
|
|
X509V3_CTX *ctx;
|
|
|
|
|
|
|
|
GetX509ExtFactory(self, ctx);
|
2003-09-08 06:31:38 -04:00
|
|
|
rb_iv_set(self, "@subject_request", req);
|
2003-11-01 04:24:55 -05:00
|
|
|
ctx->subject_req = GetX509ReqPtr(req); /* NO DUP NEEDED */
|
2003-07-23 12:12:24 -04:00
|
|
|
|
|
|
|
return req;
|
|
|
|
}
|
|
|
|
|
2010-04-22 04:04:13 -04:00
|
|
|
static VALUE
|
2003-07-23 12:12:24 -04:00
|
|
|
ossl_x509extfactory_set_crl(VALUE self, VALUE crl)
|
|
|
|
{
|
|
|
|
X509V3_CTX *ctx;
|
|
|
|
|
|
|
|
GetX509ExtFactory(self, ctx);
|
2003-09-08 06:31:38 -04:00
|
|
|
rb_iv_set(self, "@crl", crl);
|
2003-11-01 04:24:55 -05:00
|
|
|
ctx->crl = GetX509CRLPtr(crl); /* NO DUP NEEDED */
|
2003-07-23 12:12:24 -04:00
|
|
|
|
|
|
|
return crl;
|
|
|
|
}
|
|
|
|
|
2010-04-22 04:04:13 -04:00
|
|
|
static VALUE
|
2003-07-23 12:12:24 -04:00
|
|
|
ossl_x509extfactory_initialize(int argc, VALUE *argv, VALUE self)
|
|
|
|
{
|
|
|
|
/*X509V3_CTX *ctx;*/
|
|
|
|
VALUE issuer_cert, subject_cert, subject_req, crl;
|
2010-04-22 04:21:01 -04:00
|
|
|
|
2003-07-23 12:12:24 -04:00
|
|
|
/*GetX509ExtFactory(self, ctx);*/
|
|
|
|
|
2003-09-17 05:05:02 -04:00
|
|
|
rb_scan_args(argc, argv, "04",
|
|
|
|
&issuer_cert, &subject_cert, &subject_req, &crl);
|
|
|
|
if (!NIL_P(issuer_cert))
|
2003-07-23 12:12:24 -04:00
|
|
|
ossl_x509extfactory_set_issuer_cert(self, issuer_cert);
|
2003-09-17 05:05:02 -04:00
|
|
|
if (!NIL_P(subject_cert))
|
2003-07-23 12:12:24 -04:00
|
|
|
ossl_x509extfactory_set_subject_cert(self, subject_cert);
|
2003-09-17 05:05:02 -04:00
|
|
|
if (!NIL_P(subject_req))
|
2003-07-23 12:12:24 -04:00
|
|
|
ossl_x509extfactory_set_subject_req(self, subject_req);
|
2003-09-17 05:05:02 -04:00
|
|
|
if (!NIL_P(crl))
|
2003-07-23 12:12:24 -04:00
|
|
|
ossl_x509extfactory_set_crl(self, crl);
|
2003-09-17 05:05:02 -04:00
|
|
|
|
2003-07-23 12:12:24 -04:00
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2016-08-29 01:47:09 -04:00
|
|
|
* call-seq:
|
|
|
|
* ef.create_ext(ln_or_sn, "value", critical = false) -> X509::Extension
|
|
|
|
* ef.create_ext(ln_or_sn, "critical,value") -> X509::Extension
|
|
|
|
*
|
|
|
|
* Creates a new X509::Extension with passed values. See also x509v3_config(5).
|
2003-07-23 12:12:24 -04:00
|
|
|
*/
|
2010-04-22 04:04:13 -04:00
|
|
|
static VALUE
|
2003-09-17 05:05:02 -04:00
|
|
|
ossl_x509extfactory_create_ext(int argc, VALUE *argv, VALUE self)
|
2003-07-23 12:12:24 -04:00
|
|
|
{
|
|
|
|
X509V3_CTX *ctx;
|
|
|
|
X509_EXTENSION *ext;
|
2003-09-17 05:05:02 -04:00
|
|
|
VALUE oid, value, critical, valstr, obj;
|
2003-07-23 12:12:24 -04:00
|
|
|
int nid;
|
2005-11-22 17:29:13 -05:00
|
|
|
VALUE rconf;
|
|
|
|
CONF *conf;
|
2003-07-23 12:12:24 -04:00
|
|
|
|
2003-09-17 05:05:02 -04:00
|
|
|
rb_scan_args(argc, argv, "21", &oid, &value, &critical);
|
2016-05-23 07:40:07 -04:00
|
|
|
StringValueCStr(oid);
|
2003-09-17 05:05:02 -04:00
|
|
|
StringValue(value);
|
|
|
|
if(NIL_P(critical)) critical = Qfalse;
|
|
|
|
|
2006-08-31 06:30:33 -04:00
|
|
|
nid = OBJ_ln2nid(RSTRING_PTR(oid));
|
|
|
|
if(!nid) nid = OBJ_sn2nid(RSTRING_PTR(oid));
|
2016-05-23 07:40:07 -04:00
|
|
|
if(!nid) ossl_raise(eX509ExtError, "unknown OID `%"PRIsVALUE"'", oid);
|
|
|
|
|
2003-09-17 05:05:02 -04:00
|
|
|
valstr = rb_str_new2(RTEST(critical) ? "critical," : "");
|
|
|
|
rb_str_append(valstr, value);
|
2016-05-23 07:40:07 -04:00
|
|
|
StringValueCStr(valstr);
|
|
|
|
|
2004-12-15 01:35:55 -05:00
|
|
|
GetX509ExtFactory(self, ctx);
|
2015-05-29 01:55:02 -04:00
|
|
|
obj = NewX509Ext(cX509Ext);
|
2005-11-22 17:29:13 -05:00
|
|
|
rconf = rb_iv_get(self, "@config");
|
2016-08-29 01:47:09 -04:00
|
|
|
conf = NIL_P(rconf) ? NULL : DupConfigPtr(rconf);
|
|
|
|
X509V3_set_nconf(ctx, conf);
|
2006-08-31 06:30:33 -04:00
|
|
|
ext = X509V3_EXT_nconf_nid(conf, ctx, nid, RSTRING_PTR(valstr));
|
2016-08-29 01:47:09 -04:00
|
|
|
X509V3_set_ctx_nodb(ctx);
|
|
|
|
NCONF_free(conf);
|
2003-11-15 08:19:17 -05:00
|
|
|
if (!ext){
|
2016-05-23 07:40:07 -04:00
|
|
|
ossl_raise(eX509ExtError, "%"PRIsVALUE" = %"PRIsVALUE, oid, valstr);
|
2003-11-15 08:19:17 -05:00
|
|
|
}
|
2015-05-29 01:55:02 -04:00
|
|
|
SetX509Ext(obj, ext);
|
2003-07-23 12:12:24 -04:00
|
|
|
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Ext
|
|
|
|
*/
|
2003-09-06 04:56:09 -04:00
|
|
|
static VALUE
|
|
|
|
ossl_x509ext_alloc(VALUE klass)
|
|
|
|
{
|
|
|
|
X509_EXTENSION *ext;
|
|
|
|
VALUE obj;
|
|
|
|
|
2015-05-29 01:55:02 -04:00
|
|
|
obj = NewX509Ext(klass);
|
2003-09-06 04:56:09 -04:00
|
|
|
if(!(ext = X509_EXTENSION_new())){
|
|
|
|
ossl_raise(eX509ExtError, NULL);
|
|
|
|
}
|
2015-05-29 01:55:02 -04:00
|
|
|
SetX509Ext(obj, ext);
|
2003-09-06 04:56:09 -04:00
|
|
|
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
2010-12-03 21:41:13 -05:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2017-09-03 08:35:27 -04:00
|
|
|
* OpenSSL::X509::Extension.new(der)
|
|
|
|
* OpenSSL::X509::Extension.new(oid, value)
|
|
|
|
* OpenSSL::X509::Extension.new(oid, value, critical)
|
2010-12-03 21:41:13 -05:00
|
|
|
*
|
|
|
|
* Creates an X509 extension.
|
|
|
|
*
|
2017-09-03 08:35:27 -04:00
|
|
|
* The extension may be created from _der_ data or from an extension _oid_
|
|
|
|
* and _value_. The _oid_ may be either an OID or an extension name. If
|
|
|
|
* _critical_ is +true+ the extension is marked critical.
|
2010-12-03 21:41:13 -05:00
|
|
|
*/
|
2003-09-06 04:56:09 -04:00
|
|
|
static VALUE
|
|
|
|
ossl_x509ext_initialize(int argc, VALUE *argv, VALUE self)
|
|
|
|
{
|
|
|
|
VALUE oid, value, critical;
|
2008-07-22 11:34:23 -04:00
|
|
|
const unsigned char *p;
|
2008-08-30 01:58:38 -04:00
|
|
|
X509_EXTENSION *ext, *x;
|
2003-09-06 04:56:09 -04:00
|
|
|
|
2004-08-23 19:59:44 -04:00
|
|
|
GetX509Ext(self, ext);
|
2003-09-17 05:05:02 -04:00
|
|
|
if(rb_scan_args(argc, argv, "12", &oid, &value, &critical) == 1){
|
|
|
|
oid = ossl_to_der_if_possible(oid);
|
2003-09-20 15:18:22 -04:00
|
|
|
StringValue(oid);
|
2008-07-22 11:34:23 -04:00
|
|
|
p = (unsigned char *)RSTRING_PTR(oid);
|
2008-08-30 01:58:38 -04:00
|
|
|
x = d2i_X509_EXTENSION(&ext, &p, RSTRING_LEN(oid));
|
|
|
|
DATA_PTR(self) = ext;
|
|
|
|
if(!x)
|
2003-09-17 05:05:02 -04:00
|
|
|
ossl_raise(eX509ExtError, NULL);
|
|
|
|
return self;
|
|
|
|
}
|
2003-09-06 04:56:09 -04:00
|
|
|
rb_funcall(self, rb_intern("oid="), 1, oid);
|
|
|
|
rb_funcall(self, rb_intern("value="), 1, value);
|
|
|
|
if(argc > 2) rb_funcall(self, rb_intern("critical="), 1, critical);
|
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2016-08-29 01:47:09 -04:00
|
|
|
static VALUE
|
|
|
|
ossl_x509ext_initialize_copy(VALUE self, VALUE other)
|
|
|
|
{
|
|
|
|
X509_EXTENSION *ext, *ext_other, *ext_new;
|
|
|
|
|
|
|
|
rb_check_frozen(self);
|
|
|
|
GetX509Ext(self, ext);
|
2017-09-03 08:35:27 -04:00
|
|
|
GetX509Ext(other, ext_other);
|
2016-08-29 01:47:09 -04:00
|
|
|
|
|
|
|
ext_new = X509_EXTENSION_dup(ext_other);
|
|
|
|
if (!ext_new)
|
|
|
|
ossl_raise(eX509ExtError, "X509_EXTENSION_dup");
|
|
|
|
|
|
|
|
SetX509Ext(self, ext_new);
|
|
|
|
X509_EXTENSION_free(ext);
|
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2003-09-06 04:56:09 -04:00
|
|
|
static VALUE
|
|
|
|
ossl_x509ext_set_oid(VALUE self, VALUE oid)
|
|
|
|
{
|
|
|
|
X509_EXTENSION *ext;
|
|
|
|
ASN1_OBJECT *obj;
|
|
|
|
|
2004-12-15 01:35:55 -05:00
|
|
|
GetX509Ext(self, ext);
|
2016-08-29 01:47:09 -04:00
|
|
|
obj = OBJ_txt2obj(StringValueCStr(oid), 0);
|
|
|
|
if (!obj)
|
|
|
|
ossl_raise(eX509ExtError, "OBJ_txt2obj");
|
|
|
|
if (!X509_EXTENSION_set_object(ext, obj)) {
|
|
|
|
ASN1_OBJECT_free(obj);
|
|
|
|
ossl_raise(eX509ExtError, "X509_EXTENSION_set_object");
|
|
|
|
}
|
|
|
|
ASN1_OBJECT_free(obj);
|
2003-09-06 04:56:09 -04:00
|
|
|
|
|
|
|
return oid;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
ossl_x509ext_set_value(VALUE self, VALUE data)
|
|
|
|
{
|
|
|
|
X509_EXTENSION *ext;
|
|
|
|
ASN1_OCTET_STRING *asn1s;
|
|
|
|
|
2016-08-29 01:47:09 -04:00
|
|
|
GetX509Ext(self, ext);
|
2003-09-17 05:05:02 -04:00
|
|
|
data = ossl_to_der_if_possible(data);
|
2003-09-20 15:18:22 -04:00
|
|
|
StringValue(data);
|
2016-08-29 01:47:09 -04:00
|
|
|
asn1s = X509_EXTENSION_get_data(ext);
|
|
|
|
|
|
|
|
if (!ASN1_OCTET_STRING_set(asn1s, (unsigned char *)RSTRING_PTR(data),
|
|
|
|
RSTRING_LENINT(data))) {
|
|
|
|
ossl_raise(eX509ExtError, "ASN1_OCTET_STRING_set");
|
2003-09-06 04:56:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
ossl_x509ext_set_critical(VALUE self, VALUE flag)
|
|
|
|
{
|
|
|
|
X509_EXTENSION *ext;
|
|
|
|
|
|
|
|
GetX509Ext(self, ext);
|
|
|
|
X509_EXTENSION_set_critical(ext, RTEST(flag) ? 1 : 0);
|
|
|
|
|
|
|
|
return flag;
|
|
|
|
}
|
|
|
|
|
2010-04-22 04:04:13 -04:00
|
|
|
static VALUE
|
2003-07-23 12:12:24 -04:00
|
|
|
ossl_x509ext_get_oid(VALUE obj)
|
|
|
|
{
|
|
|
|
X509_EXTENSION *ext;
|
|
|
|
ASN1_OBJECT *extobj;
|
|
|
|
BIO *out;
|
|
|
|
VALUE ret;
|
2003-09-17 05:05:02 -04:00
|
|
|
int nid;
|
2003-07-23 12:12:24 -04:00
|
|
|
|
|
|
|
GetX509Ext(obj, ext);
|
|
|
|
extobj = X509_EXTENSION_get_object(ext);
|
|
|
|
if ((nid = OBJ_obj2nid(extobj)) != NID_undef)
|
|
|
|
ret = rb_str_new2(OBJ_nid2sn(nid));
|
|
|
|
else{
|
|
|
|
if (!(out = BIO_new(BIO_s_mem())))
|
|
|
|
ossl_raise(eX509ExtError, NULL);
|
|
|
|
i2a_ASN1_OBJECT(out, extobj);
|
2003-09-17 05:05:02 -04:00
|
|
|
ret = ossl_membio2str(out);
|
2003-07-23 12:12:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
ossl_x509ext_get_value(VALUE obj)
|
|
|
|
{
|
|
|
|
X509_EXTENSION *ext;
|
|
|
|
BIO *out;
|
|
|
|
VALUE ret;
|
|
|
|
|
|
|
|
GetX509Ext(obj, ext);
|
|
|
|
if (!(out = BIO_new(BIO_s_mem())))
|
|
|
|
ossl_raise(eX509ExtError, NULL);
|
|
|
|
if (!X509V3_EXT_print(out, ext, 0, 0))
|
2016-05-24 08:39:57 -04:00
|
|
|
ASN1_STRING_print(out, (ASN1_STRING *)X509_EXTENSION_get_data(ext));
|
2003-09-17 05:05:02 -04:00
|
|
|
ret = ossl_membio2str(out);
|
2003-07-23 12:12:24 -04:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2020-02-16 01:21:29 -05:00
|
|
|
static VALUE
|
|
|
|
ossl_x509ext_get_value_der(VALUE obj)
|
|
|
|
{
|
|
|
|
X509_EXTENSION *ext;
|
|
|
|
ASN1_OCTET_STRING *value;
|
|
|
|
|
|
|
|
GetX509Ext(obj, ext);
|
|
|
|
if ((value = X509_EXTENSION_get_data(ext)) == NULL)
|
|
|
|
ossl_raise(eX509ExtError, NULL);
|
|
|
|
|
|
|
|
return rb_str_new((const char *)value->data, value->length);
|
|
|
|
}
|
|
|
|
|
2003-07-23 12:12:24 -04:00
|
|
|
static VALUE
|
|
|
|
ossl_x509ext_get_critical(VALUE obj)
|
|
|
|
{
|
|
|
|
X509_EXTENSION *ext;
|
2004-12-15 01:35:55 -05:00
|
|
|
|
2003-07-23 12:12:24 -04:00
|
|
|
GetX509Ext(obj, ext);
|
|
|
|
return X509_EXTENSION_get_critical(ext) ? Qtrue : Qfalse;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
ossl_x509ext_to_der(VALUE obj)
|
|
|
|
{
|
|
|
|
X509_EXTENSION *ext;
|
|
|
|
unsigned char *p;
|
2003-09-17 05:05:02 -04:00
|
|
|
long len;
|
2003-07-23 12:12:24 -04:00
|
|
|
VALUE str;
|
|
|
|
|
|
|
|
GetX509Ext(obj, ext);
|
2003-09-17 05:05:02 -04:00
|
|
|
if((len = i2d_X509_EXTENSION(ext, NULL)) <= 0)
|
|
|
|
ossl_raise(eX509ExtError, NULL);
|
|
|
|
str = rb_str_new(0, len);
|
2008-07-22 11:34:23 -04:00
|
|
|
p = (unsigned char *)RSTRING_PTR(str);
|
2003-09-17 05:05:02 -04:00
|
|
|
if(i2d_X509_EXTENSION(ext, &p) < 0)
|
|
|
|
ossl_raise(eX509ExtError, NULL);
|
|
|
|
ossl_str_adjust(str, p);
|
2003-07-23 12:12:24 -04:00
|
|
|
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* INIT
|
|
|
|
*/
|
|
|
|
void
|
2014-09-30 01:25:32 -04:00
|
|
|
Init_ossl_x509ext(void)
|
2003-07-23 12:12:24 -04:00
|
|
|
{
|
2018-02-16 03:39:48 -05:00
|
|
|
#undef rb_intern
|
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
|
|
|
eX509ExtError = rb_define_class_under(mX509, "ExtensionError", eOSSLError);
|
2010-04-22 04:04:13 -04:00
|
|
|
|
2003-07-23 12:12:24 -04:00
|
|
|
cX509ExtFactory = rb_define_class_under(mX509, "ExtensionFactory", rb_cObject);
|
2010-04-22 04:21:01 -04:00
|
|
|
|
2003-07-23 12:12:24 -04:00
|
|
|
rb_define_alloc_func(cX509ExtFactory, ossl_x509extfactory_alloc);
|
|
|
|
rb_define_method(cX509ExtFactory, "initialize", ossl_x509extfactory_initialize, -1);
|
2010-04-22 04:21:01 -04:00
|
|
|
|
2003-09-08 06:31:38 -04:00
|
|
|
rb_attr(cX509ExtFactory, rb_intern("issuer_certificate"), 1, 0, Qfalse);
|
|
|
|
rb_attr(cX509ExtFactory, rb_intern("subject_certificate"), 1, 0, Qfalse);
|
|
|
|
rb_attr(cX509ExtFactory, rb_intern("subject_request"), 1, 0, Qfalse);
|
|
|
|
rb_attr(cX509ExtFactory, rb_intern("crl"), 1, 0, Qfalse);
|
2016-08-29 01:47:09 -04:00
|
|
|
rb_attr(cX509ExtFactory, rb_intern("config"), 1, 1, Qfalse);
|
2003-09-08 06:31:38 -04:00
|
|
|
|
2003-07-23 12:12:24 -04:00
|
|
|
rb_define_method(cX509ExtFactory, "issuer_certificate=", ossl_x509extfactory_set_issuer_cert, 1);
|
|
|
|
rb_define_method(cX509ExtFactory, "subject_certificate=", ossl_x509extfactory_set_subject_cert, 1);
|
|
|
|
rb_define_method(cX509ExtFactory, "subject_request=", ossl_x509extfactory_set_subject_req, 1);
|
|
|
|
rb_define_method(cX509ExtFactory, "crl=", ossl_x509extfactory_set_crl, 1);
|
2003-09-17 05:05:02 -04:00
|
|
|
rb_define_method(cX509ExtFactory, "create_ext", ossl_x509extfactory_create_ext, -1);
|
2010-04-22 04:21:01 -04:00
|
|
|
|
2003-07-23 12:12:24 -04:00
|
|
|
cX509Ext = rb_define_class_under(mX509, "Extension", rb_cObject);
|
2003-09-06 04:56:09 -04:00
|
|
|
rb_define_alloc_func(cX509Ext, ossl_x509ext_alloc);
|
|
|
|
rb_define_method(cX509Ext, "initialize", ossl_x509ext_initialize, -1);
|
2017-09-03 08:35:27 -04:00
|
|
|
rb_define_method(cX509Ext, "initialize_copy", ossl_x509ext_initialize_copy, 1);
|
2003-09-06 04:56:09 -04:00
|
|
|
rb_define_method(cX509Ext, "oid=", ossl_x509ext_set_oid, 1);
|
|
|
|
rb_define_method(cX509Ext, "value=", ossl_x509ext_set_value, 1);
|
|
|
|
rb_define_method(cX509Ext, "critical=", ossl_x509ext_set_critical, 1);
|
2003-07-23 12:12:24 -04:00
|
|
|
rb_define_method(cX509Ext, "oid", ossl_x509ext_get_oid, 0);
|
|
|
|
rb_define_method(cX509Ext, "value", ossl_x509ext_get_value, 0);
|
2020-02-16 01:21:29 -05:00
|
|
|
rb_define_method(cX509Ext, "value_der", ossl_x509ext_get_value_der, 0);
|
2003-07-23 12:12:24 -04:00
|
|
|
rb_define_method(cX509Ext, "critical?", ossl_x509ext_get_critical, 0);
|
|
|
|
rb_define_method(cX509Ext, "to_der", ossl_x509ext_to_der, 0);
|
|
|
|
}
|