2003-07-23 12:12:24 -04:00
|
|
|
/*
|
|
|
|
* $Id$
|
|
|
|
* 'OpenSSL for Ruby' project
|
|
|
|
* Copyright (C) 2001-2002 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 WrapX509(klass, obj, x509) do { \
|
2011-03-14 06:55:05 -04:00
|
|
|
if (!(x509)) { \
|
2003-07-23 12:12:24 -04:00
|
|
|
ossl_raise(rb_eRuntimeError, "CERT wasn't initialized!"); \
|
|
|
|
} \
|
2011-03-14 06:55:05 -04:00
|
|
|
(obj) = Data_Wrap_Struct((klass), 0, X509_free, (x509)); \
|
2003-07-23 12:12:24 -04:00
|
|
|
} while (0)
|
|
|
|
#define GetX509(obj, x509) do { \
|
2011-03-14 06:55:05 -04:00
|
|
|
Data_Get_Struct((obj), X509, (x509)); \
|
|
|
|
if (!(x509)) { \
|
2003-07-23 12:12:24 -04:00
|
|
|
ossl_raise(rb_eRuntimeError, "CERT wasn't initialized!"); \
|
|
|
|
} \
|
|
|
|
} while (0)
|
|
|
|
#define SafeGetX509(obj, x509) do { \
|
2011-03-14 06:55:05 -04:00
|
|
|
OSSL_Check_Kind((obj), cX509Cert); \
|
|
|
|
GetX509((obj), (x509)); \
|
2003-07-23 12:12:24 -04:00
|
|
|
} while (0)
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Classes
|
|
|
|
*/
|
|
|
|
VALUE cX509Cert;
|
|
|
|
VALUE eX509CertError;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Public
|
|
|
|
*/
|
|
|
|
VALUE
|
|
|
|
ossl_x509_new(X509 *x509)
|
|
|
|
{
|
|
|
|
X509 *new;
|
|
|
|
VALUE obj;
|
|
|
|
|
|
|
|
if (!x509) {
|
|
|
|
new = X509_new();
|
|
|
|
} else {
|
|
|
|
new = X509_dup(x509);
|
|
|
|
}
|
|
|
|
if (!new) {
|
|
|
|
ossl_raise(eX509CertError, NULL);
|
|
|
|
}
|
|
|
|
WrapX509(cX509Cert, obj, new);
|
2010-04-22 04:21:01 -04:00
|
|
|
|
2003-07-23 12:12:24 -04:00
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
2010-04-22 04:04:13 -04:00
|
|
|
VALUE
|
2003-07-23 12:12:24 -04:00
|
|
|
ossl_x509_new_from_file(VALUE filename)
|
|
|
|
{
|
|
|
|
X509 *x509;
|
|
|
|
FILE *fp;
|
|
|
|
VALUE obj;
|
|
|
|
|
|
|
|
SafeStringValue(filename);
|
2006-08-31 06:30:33 -04:00
|
|
|
if (!(fp = fopen(RSTRING_PTR(filename), "r"))) {
|
2003-07-23 12:12:24 -04:00
|
|
|
ossl_raise(eX509CertError, "%s", strerror(errno));
|
|
|
|
}
|
|
|
|
x509 = PEM_read_X509(fp, NULL, NULL, NULL);
|
|
|
|
/*
|
|
|
|
* prepare for DER...
|
|
|
|
#if !defined(OPENSSL_NO_FP_API)
|
|
|
|
if (!x509) {
|
|
|
|
rewind(fp);
|
|
|
|
|
|
|
|
x509 = d2i_X509_fp(fp, NULL);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
*/
|
|
|
|
fclose(fp);
|
|
|
|
if (!x509) {
|
|
|
|
ossl_raise(eX509CertError, NULL);
|
|
|
|
}
|
|
|
|
WrapX509(cX509Cert, obj, x509);
|
|
|
|
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
X509 *
|
|
|
|
GetX509CertPtr(VALUE obj)
|
|
|
|
{
|
|
|
|
X509 *x509;
|
2010-04-22 04:21:01 -04:00
|
|
|
|
2003-07-23 12:12:24 -04:00
|
|
|
SafeGetX509(obj, x509);
|
2010-04-22 04:21:01 -04:00
|
|
|
|
2003-07-23 12:12:24 -04:00
|
|
|
return x509;
|
|
|
|
}
|
|
|
|
|
|
|
|
X509 *
|
|
|
|
DupX509CertPtr(VALUE obj)
|
|
|
|
{
|
|
|
|
X509 *x509;
|
2010-04-22 04:21:01 -04:00
|
|
|
|
2003-07-23 12:12:24 -04:00
|
|
|
SafeGetX509(obj, x509);
|
2010-04-22 04:21:01 -04:00
|
|
|
|
2003-07-23 12:12:24 -04:00
|
|
|
CRYPTO_add(&x509->references, 1, CRYPTO_LOCK_X509);
|
2010-04-22 04:21:01 -04:00
|
|
|
|
2003-07-23 12:12:24 -04:00
|
|
|
return x509;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Private
|
|
|
|
*/
|
2010-04-22 04:04:13 -04:00
|
|
|
static VALUE
|
2003-07-23 12:12:24 -04:00
|
|
|
ossl_x509_alloc(VALUE klass)
|
|
|
|
{
|
|
|
|
X509 *x509;
|
|
|
|
VALUE obj;
|
|
|
|
|
|
|
|
x509 = X509_new();
|
|
|
|
if (!x509) ossl_raise(eX509CertError, NULL);
|
|
|
|
|
|
|
|
WrapX509(klass, obj, x509);
|
|
|
|
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
2007-04-05 15:03:28 -04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* Certificate.new => cert
|
|
|
|
* Certificate.new(string) => cert
|
|
|
|
*/
|
2010-04-22 04:04:13 -04:00
|
|
|
static VALUE
|
2003-07-23 12:12:24 -04:00
|
|
|
ossl_x509_initialize(int argc, VALUE *argv, VALUE self)
|
|
|
|
{
|
|
|
|
BIO *in;
|
2008-07-22 11:34:23 -04:00
|
|
|
X509 *x509, *x = DATA_PTR(self);
|
2003-07-23 12:12:24 -04:00
|
|
|
VALUE arg;
|
|
|
|
|
|
|
|
if (rb_scan_args(argc, argv, "01", &arg) == 0) {
|
|
|
|
/* create just empty X509Cert */
|
|
|
|
return self;
|
|
|
|
}
|
2003-09-17 05:05:02 -04:00
|
|
|
arg = ossl_to_der_if_possible(arg);
|
2003-07-23 12:12:24 -04:00
|
|
|
in = ossl_obj2bio(arg);
|
2008-07-22 11:34:23 -04:00
|
|
|
x509 = PEM_read_bio_X509(in, &x, NULL, NULL);
|
|
|
|
DATA_PTR(self) = x;
|
2003-07-23 12:12:24 -04:00
|
|
|
if (!x509) {
|
2008-07-22 11:34:23 -04:00
|
|
|
(void)BIO_reset(in);
|
|
|
|
x509 = d2i_X509_bio(in, &x);
|
|
|
|
DATA_PTR(self) = x;
|
2003-07-23 12:12:24 -04:00
|
|
|
}
|
|
|
|
BIO_free(in);
|
|
|
|
if (!x509) ossl_raise(eX509CertError, NULL);
|
2010-04-22 04:04:13 -04:00
|
|
|
|
2003-07-23 12:12:24 -04:00
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
ossl_x509_copy(VALUE self, VALUE other)
|
|
|
|
{
|
|
|
|
X509 *a, *b, *x509;
|
2010-04-22 04:21:01 -04:00
|
|
|
|
2003-07-23 12:12:24 -04:00
|
|
|
rb_check_frozen(self);
|
|
|
|
if (self == other) return self;
|
|
|
|
|
|
|
|
GetX509(self, a);
|
|
|
|
SafeGetX509(other, b);
|
|
|
|
|
|
|
|
x509 = X509_dup(b);
|
|
|
|
if (!x509) ossl_raise(eX509CertError, NULL);
|
2010-04-22 04:04:13 -04:00
|
|
|
|
2003-07-23 12:12:24 -04:00
|
|
|
DATA_PTR(self) = x509;
|
|
|
|
X509_free(a);
|
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2007-04-05 15:03:28 -04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* cert.to_der => string
|
|
|
|
*/
|
2010-04-22 04:04:13 -04:00
|
|
|
static VALUE
|
2003-07-23 12:12:24 -04:00
|
|
|
ossl_x509_to_der(VALUE self)
|
|
|
|
{
|
|
|
|
X509 *x509;
|
|
|
|
VALUE str;
|
2003-09-17 05:05:02 -04:00
|
|
|
long len;
|
|
|
|
unsigned char *p;
|
2003-07-23 12:12:24 -04:00
|
|
|
|
|
|
|
GetX509(self, x509);
|
2003-09-17 05:05:02 -04:00
|
|
|
if ((len = i2d_X509(x509, NULL)) <= 0)
|
2003-07-23 12:12:24 -04:00
|
|
|
ossl_raise(eX509CertError, NULL);
|
2003-09-17 05:05:02 -04:00
|
|
|
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(x509, &p) <= 0)
|
|
|
|
ossl_raise(eX509CertError, NULL);
|
|
|
|
ossl_str_adjust(str, p);
|
2010-04-22 04:04:13 -04:00
|
|
|
|
2003-07-23 12:12:24 -04:00
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
2007-04-05 15:03:28 -04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* cert.to_pem => string
|
|
|
|
*/
|
2010-04-22 04:04:13 -04:00
|
|
|
static VALUE
|
2003-07-23 12:12:24 -04:00
|
|
|
ossl_x509_to_pem(VALUE self)
|
|
|
|
{
|
|
|
|
X509 *x509;
|
|
|
|
BIO *out;
|
|
|
|
VALUE str;
|
2010-04-22 04:21:01 -04:00
|
|
|
|
2003-07-23 12:12:24 -04:00
|
|
|
GetX509(self, x509);
|
|
|
|
out = BIO_new(BIO_s_mem());
|
|
|
|
if (!out) ossl_raise(eX509CertError, NULL);
|
|
|
|
|
|
|
|
if (!PEM_write_bio_X509(out, x509)) {
|
|
|
|
BIO_free(out);
|
|
|
|
ossl_raise(eX509CertError, NULL);
|
|
|
|
}
|
2003-09-17 05:05:02 -04:00
|
|
|
str = ossl_membio2str(out);
|
2003-07-23 12:12:24 -04:00
|
|
|
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
2007-04-05 15:03:28 -04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* cert.to_text => string
|
|
|
|
*/
|
2003-07-23 12:12:24 -04:00
|
|
|
static VALUE
|
|
|
|
ossl_x509_to_text(VALUE self)
|
|
|
|
{
|
|
|
|
X509 *x509;
|
|
|
|
BIO *out;
|
|
|
|
VALUE str;
|
2010-04-22 04:21:01 -04:00
|
|
|
|
2003-07-23 12:12:24 -04:00
|
|
|
GetX509(self, x509);
|
|
|
|
|
|
|
|
out = BIO_new(BIO_s_mem());
|
|
|
|
if (!out) ossl_raise(eX509CertError, NULL);
|
|
|
|
|
|
|
|
if (!X509_print(out, x509)) {
|
|
|
|
BIO_free(out);
|
|
|
|
ossl_raise(eX509CertError, NULL);
|
|
|
|
}
|
2003-09-17 05:05:02 -04:00
|
|
|
str = ossl_membio2str(out);
|
2003-07-23 12:12:24 -04:00
|
|
|
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
/*
|
|
|
|
* Makes from X509 X509_REQuest
|
|
|
|
*/
|
2010-04-22 04:04:13 -04:00
|
|
|
static VALUE
|
2003-07-23 12:12:24 -04:00
|
|
|
ossl_x509_to_req(VALUE self)
|
|
|
|
{
|
|
|
|
X509 *x509;
|
|
|
|
X509_REQ *req;
|
|
|
|
VALUE obj;
|
|
|
|
|
|
|
|
GetX509(self, x509);
|
|
|
|
if (!(req = X509_to_X509_REQ(x509, NULL, EVP_md5()))) {
|
|
|
|
ossl_raise(eX509CertError, NULL);
|
|
|
|
}
|
|
|
|
obj = ossl_x509req_new(req);
|
|
|
|
X509_REQ_free(req);
|
|
|
|
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2007-04-05 15:03:28 -04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* cert.version => integer
|
|
|
|
*/
|
2010-04-22 04:04:13 -04:00
|
|
|
static VALUE
|
2003-07-23 12:12:24 -04:00
|
|
|
ossl_x509_get_version(VALUE self)
|
|
|
|
{
|
|
|
|
X509 *x509;
|
|
|
|
|
|
|
|
GetX509(self, x509);
|
2010-04-22 04:21:01 -04:00
|
|
|
|
2003-07-23 12:12:24 -04:00
|
|
|
return LONG2NUM(X509_get_version(x509));
|
|
|
|
}
|
|
|
|
|
2007-04-05 15:03:28 -04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* cert.version = integer => integer
|
|
|
|
*/
|
2010-04-22 04:04:13 -04:00
|
|
|
static VALUE
|
2003-07-23 12:12:24 -04:00
|
|
|
ossl_x509_set_version(VALUE self, VALUE version)
|
|
|
|
{
|
|
|
|
X509 *x509;
|
|
|
|
long ver;
|
|
|
|
|
|
|
|
if ((ver = NUM2LONG(version)) < 0) {
|
|
|
|
ossl_raise(eX509CertError, "version must be >= 0!");
|
|
|
|
}
|
2004-12-15 01:35:55 -05:00
|
|
|
GetX509(self, x509);
|
2003-07-23 12:12:24 -04:00
|
|
|
if (!X509_set_version(x509, ver)) {
|
|
|
|
ossl_raise(eX509CertError, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
return version;
|
|
|
|
}
|
|
|
|
|
2007-04-05 15:03:28 -04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* cert.serial => integer
|
|
|
|
*/
|
2010-04-22 04:04:13 -04:00
|
|
|
static VALUE
|
2003-07-23 12:12:24 -04:00
|
|
|
ossl_x509_get_serial(VALUE self)
|
|
|
|
{
|
|
|
|
X509 *x509;
|
|
|
|
|
|
|
|
GetX509(self, x509);
|
2010-04-22 04:21:01 -04:00
|
|
|
|
2003-07-23 12:12:24 -04:00
|
|
|
return asn1integer_to_num(X509_get_serialNumber(x509));
|
|
|
|
}
|
|
|
|
|
2007-04-05 15:03:28 -04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* cert.serial = integer => integer
|
|
|
|
*/
|
2010-04-22 04:04:13 -04:00
|
|
|
static VALUE
|
2003-07-23 12:12:24 -04:00
|
|
|
ossl_x509_set_serial(VALUE self, VALUE num)
|
|
|
|
{
|
|
|
|
X509 *x509;
|
|
|
|
|
|
|
|
GetX509(self, x509);
|
|
|
|
|
|
|
|
x509->cert_info->serialNumber =
|
|
|
|
num_to_asn1integer(num, X509_get_serialNumber(x509));
|
2010-04-22 04:21:01 -04:00
|
|
|
|
2003-07-23 12:12:24 -04:00
|
|
|
return num;
|
|
|
|
}
|
|
|
|
|
2007-04-05 15:03:28 -04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* cert.signature_algorithm => string
|
|
|
|
*/
|
2010-04-22 04:04:13 -04:00
|
|
|
static VALUE
|
2003-07-23 12:12:24 -04:00
|
|
|
ossl_x509_get_signature_algorithm(VALUE self)
|
|
|
|
{
|
|
|
|
X509 *x509;
|
|
|
|
BIO *out;
|
|
|
|
VALUE str;
|
|
|
|
|
|
|
|
GetX509(self, x509);
|
|
|
|
out = BIO_new(BIO_s_mem());
|
|
|
|
if (!out) ossl_raise(eX509CertError, NULL);
|
|
|
|
|
|
|
|
if (!i2a_ASN1_OBJECT(out, x509->cert_info->signature->algorithm)) {
|
|
|
|
BIO_free(out);
|
|
|
|
ossl_raise(eX509CertError, NULL);
|
|
|
|
}
|
2003-09-17 05:05:02 -04:00
|
|
|
str = ossl_membio2str(out);
|
2003-07-23 12:12:24 -04:00
|
|
|
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
2007-04-05 15:03:28 -04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* cert.subject => name
|
|
|
|
*/
|
2010-04-22 04:04:13 -04:00
|
|
|
static VALUE
|
2003-07-23 12:12:24 -04:00
|
|
|
ossl_x509_get_subject(VALUE self)
|
|
|
|
{
|
|
|
|
X509 *x509;
|
|
|
|
X509_NAME *name;
|
2010-04-22 04:21:01 -04:00
|
|
|
|
2003-07-23 12:12:24 -04:00
|
|
|
GetX509(self, x509);
|
|
|
|
if (!(name = X509_get_subject_name(x509))) { /* NO DUP - don't free! */
|
|
|
|
ossl_raise(eX509CertError, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ossl_x509name_new(name);
|
|
|
|
}
|
|
|
|
|
2007-04-05 15:03:28 -04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* cert.subject = name => name
|
|
|
|
*/
|
2010-04-22 04:04:13 -04:00
|
|
|
static VALUE
|
2003-07-23 12:12:24 -04:00
|
|
|
ossl_x509_set_subject(VALUE self, VALUE subject)
|
|
|
|
{
|
|
|
|
X509 *x509;
|
2010-04-22 04:21:01 -04:00
|
|
|
|
2003-07-23 12:12:24 -04:00
|
|
|
GetX509(self, x509);
|
|
|
|
if (!X509_set_subject_name(x509, GetX509NamePtr(subject))) { /* DUPs name */
|
|
|
|
ossl_raise(eX509CertError, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
return subject;
|
|
|
|
}
|
|
|
|
|
2007-04-05 15:03:28 -04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* cert.issuer => name
|
|
|
|
*/
|
2010-04-22 04:04:13 -04:00
|
|
|
static VALUE
|
2003-07-23 12:12:24 -04:00
|
|
|
ossl_x509_get_issuer(VALUE self)
|
|
|
|
{
|
|
|
|
X509 *x509;
|
|
|
|
X509_NAME *name;
|
|
|
|
|
|
|
|
GetX509(self, x509);
|
|
|
|
if(!(name = X509_get_issuer_name(x509))) { /* NO DUP - don't free! */
|
|
|
|
ossl_raise(eX509CertError, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ossl_x509name_new(name);
|
|
|
|
}
|
|
|
|
|
2007-04-05 15:03:28 -04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* cert.issuer = name => name
|
|
|
|
*/
|
2010-04-22 04:04:13 -04:00
|
|
|
static VALUE
|
2003-07-23 12:12:24 -04:00
|
|
|
ossl_x509_set_issuer(VALUE self, VALUE issuer)
|
|
|
|
{
|
|
|
|
X509 *x509;
|
|
|
|
|
|
|
|
GetX509(self, x509);
|
|
|
|
if (!X509_set_issuer_name(x509, GetX509NamePtr(issuer))) { /* DUPs name */
|
|
|
|
ossl_raise(eX509CertError, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
return issuer;
|
|
|
|
}
|
|
|
|
|
2007-04-05 15:03:28 -04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* cert.not_before => time
|
|
|
|
*/
|
2010-04-22 04:04:13 -04:00
|
|
|
static VALUE
|
2003-07-23 12:12:24 -04:00
|
|
|
ossl_x509_get_not_before(VALUE self)
|
|
|
|
{
|
|
|
|
X509 *x509;
|
|
|
|
ASN1_UTCTIME *asn1time;
|
|
|
|
|
|
|
|
GetX509(self, x509);
|
|
|
|
if (!(asn1time = X509_get_notBefore(x509))) { /* NO DUP - don't free! */
|
|
|
|
ossl_raise(eX509CertError, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
return asn1time_to_time(asn1time);
|
|
|
|
}
|
|
|
|
|
2007-04-05 15:03:28 -04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* cert.not_before = time => time
|
|
|
|
*/
|
2010-04-22 04:04:13 -04:00
|
|
|
static VALUE
|
2003-07-23 12:12:24 -04:00
|
|
|
ossl_x509_set_not_before(VALUE self, VALUE time)
|
|
|
|
{
|
|
|
|
X509 *x509;
|
|
|
|
time_t sec;
|
2010-04-22 04:21:01 -04:00
|
|
|
|
2003-07-23 12:12:24 -04:00
|
|
|
sec = time_to_time_t(time);
|
2004-12-15 01:35:55 -05:00
|
|
|
GetX509(self, x509);
|
2003-07-23 12:12:24 -04:00
|
|
|
if (!X509_time_adj(X509_get_notBefore(x509), 0, &sec)) {
|
|
|
|
ossl_raise(eX509CertError, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
return time;
|
|
|
|
}
|
|
|
|
|
2007-04-05 15:03:28 -04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* cert.not_after => time
|
|
|
|
*/
|
2010-04-22 04:04:13 -04:00
|
|
|
static VALUE
|
2003-07-23 12:12:24 -04:00
|
|
|
ossl_x509_get_not_after(VALUE self)
|
|
|
|
{
|
|
|
|
X509 *x509;
|
|
|
|
ASN1_TIME *asn1time;
|
|
|
|
|
|
|
|
GetX509(self, x509);
|
|
|
|
if (!(asn1time = X509_get_notAfter(x509))) { /* NO DUP - don't free! */
|
|
|
|
ossl_raise(eX509CertError, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
return asn1time_to_time(asn1time);
|
|
|
|
}
|
|
|
|
|
2007-04-05 15:03:28 -04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* cert.not_before = time => time
|
|
|
|
*/
|
2010-04-22 04:04:13 -04:00
|
|
|
static VALUE
|
2003-07-23 12:12:24 -04:00
|
|
|
ossl_x509_set_not_after(VALUE self, VALUE time)
|
|
|
|
{
|
|
|
|
X509 *x509;
|
|
|
|
time_t sec;
|
2010-04-22 04:21:01 -04:00
|
|
|
|
2003-07-23 12:12:24 -04:00
|
|
|
sec = time_to_time_t(time);
|
2004-12-15 01:35:55 -05:00
|
|
|
GetX509(self, x509);
|
2003-07-23 12:12:24 -04:00
|
|
|
if (!X509_time_adj(X509_get_notAfter(x509), 0, &sec)) {
|
|
|
|
ossl_raise(eX509CertError, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
return time;
|
|
|
|
}
|
|
|
|
|
2007-04-05 15:03:28 -04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* cert.public_key => key
|
|
|
|
*/
|
2010-04-22 04:04:13 -04:00
|
|
|
static VALUE
|
2003-07-23 12:12:24 -04:00
|
|
|
ossl_x509_get_public_key(VALUE self)
|
|
|
|
{
|
|
|
|
X509 *x509;
|
|
|
|
EVP_PKEY *pkey;
|
|
|
|
|
|
|
|
GetX509(self, x509);
|
|
|
|
if (!(pkey = X509_get_pubkey(x509))) { /* adds an reference */
|
|
|
|
ossl_raise(eX509CertError, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ossl_pkey_new(pkey); /* NO DUP - OK */
|
|
|
|
}
|
|
|
|
|
2007-04-05 15:03:28 -04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* cert.public_key = key => key
|
|
|
|
*/
|
2010-04-22 04:04:13 -04:00
|
|
|
static VALUE
|
2003-07-23 12:12:24 -04:00
|
|
|
ossl_x509_set_public_key(VALUE self, VALUE key)
|
|
|
|
{
|
|
|
|
X509 *x509;
|
|
|
|
|
|
|
|
GetX509(self, x509);
|
|
|
|
if (!X509_set_pubkey(x509, GetPKeyPtr(key))) { /* DUPs pkey */
|
|
|
|
ossl_raise(eX509CertError, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
return key;
|
|
|
|
}
|
|
|
|
|
2007-04-05 15:03:28 -04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* cert.sign(key, digest) => self
|
|
|
|
*/
|
2010-04-22 04:04:13 -04:00
|
|
|
static VALUE
|
2003-07-23 12:12:24 -04:00
|
|
|
ossl_x509_sign(VALUE self, VALUE key, VALUE digest)
|
|
|
|
{
|
|
|
|
X509 *x509;
|
|
|
|
EVP_PKEY *pkey;
|
|
|
|
const EVP_MD *md;
|
|
|
|
|
|
|
|
pkey = GetPrivPKeyPtr(key); /* NO NEED TO DUP */
|
|
|
|
md = GetDigestPtr(digest);
|
2004-12-15 01:35:55 -05:00
|
|
|
GetX509(self, x509);
|
2003-07-23 12:12:24 -04:00
|
|
|
if (!X509_sign(x509, pkey, md)) {
|
|
|
|
ossl_raise(eX509CertError, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2007-04-05 15:03:28 -04:00
|
|
|
* call-seq:
|
|
|
|
* cert.verify(key) => true | false
|
|
|
|
*
|
2003-07-23 12:12:24 -04:00
|
|
|
* Checks that cert signature is made with PRIVversion of this PUBLIC 'key'
|
|
|
|
*/
|
2010-04-22 04:04:13 -04:00
|
|
|
static VALUE
|
2003-07-23 12:12:24 -04:00
|
|
|
ossl_x509_verify(VALUE self, VALUE key)
|
|
|
|
{
|
|
|
|
X509 *x509;
|
|
|
|
EVP_PKEY *pkey;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
pkey = GetPKeyPtr(key); /* NO NEED TO DUP */
|
2004-12-15 01:35:55 -05:00
|
|
|
GetX509(self, x509);
|
2003-07-23 12:12:24 -04:00
|
|
|
if ((i = X509_verify(x509, pkey)) < 0) {
|
|
|
|
ossl_raise(eX509CertError, NULL);
|
2010-04-22 04:04:13 -04:00
|
|
|
}
|
2003-07-23 12:12:24 -04:00
|
|
|
if (i > 0) {
|
|
|
|
return Qtrue;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Qfalse;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2007-04-05 15:03:28 -04:00
|
|
|
* call-seq:
|
|
|
|
* cert.check_private_key(key)
|
|
|
|
*
|
2003-07-23 12:12:24 -04:00
|
|
|
* Checks if 'key' is PRIV key for this cert
|
|
|
|
*/
|
2010-04-22 04:04:13 -04:00
|
|
|
static VALUE
|
2003-07-23 12:12:24 -04:00
|
|
|
ossl_x509_check_private_key(VALUE self, VALUE key)
|
|
|
|
{
|
|
|
|
X509 *x509;
|
|
|
|
EVP_PKEY *pkey;
|
2010-04-22 04:21:01 -04:00
|
|
|
|
2003-07-23 12:12:24 -04:00
|
|
|
/* not needed private key, but should be */
|
|
|
|
pkey = GetPrivPKeyPtr(key); /* NO NEED TO DUP */
|
2004-12-15 01:35:55 -05:00
|
|
|
GetX509(self, x509);
|
2003-07-23 12:12:24 -04:00
|
|
|
if (!X509_check_private_key(x509, pkey)) {
|
|
|
|
OSSL_Warning("Check private key:%s", OSSL_ErrMsg());
|
|
|
|
return Qfalse;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Qtrue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2007-04-05 15:03:28 -04:00
|
|
|
* call-seq:
|
|
|
|
* cert.extensions => [extension...]
|
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_x509_get_extensions(VALUE self)
|
|
|
|
{
|
|
|
|
X509 *x509;
|
|
|
|
int count, i;
|
|
|
|
X509_EXTENSION *ext;
|
|
|
|
VALUE ary;
|
|
|
|
|
|
|
|
GetX509(self, x509);
|
|
|
|
count = X509_get_ext_count(x509);
|
|
|
|
if (count < 0) {
|
|
|
|
return rb_ary_new();
|
|
|
|
}
|
|
|
|
ary = rb_ary_new2(count);
|
|
|
|
for (i=0; i<count; i++) {
|
|
|
|
ext = X509_get_ext(x509, i); /* NO DUP - don't free! */
|
|
|
|
rb_ary_push(ary, ossl_x509ext_new(ext));
|
|
|
|
}
|
|
|
|
|
|
|
|
return ary;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2007-04-05 15:03:28 -04:00
|
|
|
* call-seq:
|
|
|
|
* cert.extensions = [ext...] => [ext...]
|
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_x509_set_extensions(VALUE self, VALUE ary)
|
|
|
|
{
|
|
|
|
X509 *x509;
|
|
|
|
X509_EXTENSION *ext;
|
|
|
|
int i;
|
2010-04-22 04:21:01 -04:00
|
|
|
|
2003-07-23 12:12:24 -04:00
|
|
|
Check_Type(ary, T_ARRAY);
|
|
|
|
/* All ary's members should be X509Extension */
|
2006-09-02 10:42:08 -04:00
|
|
|
for (i=0; i<RARRAY_LEN(ary); i++) {
|
|
|
|
OSSL_Check_Kind(RARRAY_PTR(ary)[i], cX509Ext);
|
2003-07-23 12:12:24 -04:00
|
|
|
}
|
2004-12-15 01:35:55 -05:00
|
|
|
GetX509(self, x509);
|
2003-07-23 12:12:24 -04:00
|
|
|
sk_X509_EXTENSION_pop_free(x509->cert_info->extensions, X509_EXTENSION_free);
|
|
|
|
x509->cert_info->extensions = NULL;
|
2006-09-02 10:42:08 -04:00
|
|
|
for (i=0; i<RARRAY_LEN(ary); i++) {
|
|
|
|
ext = DupX509ExtPtr(RARRAY_PTR(ary)[i]);
|
2010-04-22 04:21:01 -04:00
|
|
|
|
2003-07-23 12:12:24 -04:00
|
|
|
if (!X509_add_ext(x509, ext, -1)) { /* DUPs ext - FREE it */
|
|
|
|
X509_EXTENSION_free(ext);
|
|
|
|
ossl_raise(eX509CertError, NULL);
|
|
|
|
}
|
|
|
|
X509_EXTENSION_free(ext);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ary;
|
|
|
|
}
|
|
|
|
|
2007-04-05 15:03:28 -04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* cert.add_extension(extension) => extension
|
|
|
|
*/
|
2010-04-22 04:04:13 -04:00
|
|
|
static VALUE
|
2003-07-23 12:12:24 -04:00
|
|
|
ossl_x509_add_extension(VALUE self, VALUE extension)
|
|
|
|
{
|
|
|
|
X509 *x509;
|
|
|
|
X509_EXTENSION *ext;
|
2010-04-22 04:21:01 -04:00
|
|
|
|
2003-07-23 12:12:24 -04:00
|
|
|
GetX509(self, x509);
|
|
|
|
ext = DupX509ExtPtr(extension);
|
|
|
|
if (!X509_add_ext(x509, ext, -1)) { /* DUPs ext - FREE it */
|
|
|
|
X509_EXTENSION_free(ext);
|
|
|
|
ossl_raise(eX509CertError, NULL);
|
|
|
|
}
|
|
|
|
X509_EXTENSION_free(ext);
|
|
|
|
|
|
|
|
return extension;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
ossl_x509_inspect(VALUE self)
|
|
|
|
{
|
|
|
|
VALUE str;
|
2008-07-22 11:34:23 -04:00
|
|
|
const char *cname = rb_class2name(rb_obj_class(self));
|
2003-07-23 12:12:24 -04:00
|
|
|
|
|
|
|
str = rb_str_new2("#<");
|
|
|
|
rb_str_cat2(str, cname);
|
|
|
|
rb_str_cat2(str, " ");
|
|
|
|
|
|
|
|
rb_str_cat2(str, "subject=");
|
|
|
|
rb_str_append(str, rb_inspect(ossl_x509_get_subject(self)));
|
|
|
|
rb_str_cat2(str, ", ");
|
|
|
|
|
|
|
|
rb_str_cat2(str, "issuer=");
|
|
|
|
rb_str_append(str, rb_inspect(ossl_x509_get_issuer(self)));
|
|
|
|
rb_str_cat2(str, ", ");
|
|
|
|
|
|
|
|
rb_str_cat2(str, "serial=");
|
|
|
|
rb_str_append(str, rb_inspect(ossl_x509_get_serial(self)));
|
|
|
|
rb_str_cat2(str, ", ");
|
|
|
|
|
|
|
|
rb_str_cat2(str, "not_before=");
|
|
|
|
rb_str_append(str, rb_inspect(ossl_x509_get_not_before(self)));
|
|
|
|
rb_str_cat2(str, ", ");
|
|
|
|
|
|
|
|
rb_str_cat2(str, "not_after=");
|
|
|
|
rb_str_append(str, rb_inspect(ossl_x509_get_not_after(self)));
|
|
|
|
|
|
|
|
str = rb_str_cat2(str, ">");
|
|
|
|
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* INIT
|
|
|
|
*/
|
2010-04-22 04:04:13 -04:00
|
|
|
void
|
2003-07-23 12:12:24 -04:00
|
|
|
Init_ossl_x509cert()
|
|
|
|
{
|
|
|
|
eX509CertError = rb_define_class_under(mX509, "CertificateError", eOSSLError);
|
2010-04-22 04:21:01 -04:00
|
|
|
|
2003-07-23 12:12:24 -04:00
|
|
|
cX509Cert = rb_define_class_under(mX509, "Certificate", rb_cObject);
|
2010-04-22 04:21:01 -04:00
|
|
|
|
2003-07-23 12:12:24 -04:00
|
|
|
rb_define_alloc_func(cX509Cert, ossl_x509_alloc);
|
|
|
|
rb_define_method(cX509Cert, "initialize", ossl_x509_initialize, -1);
|
|
|
|
rb_define_copy_func(cX509Cert, ossl_x509_copy);
|
2010-04-22 04:04:13 -04:00
|
|
|
|
2003-07-23 12:12:24 -04:00
|
|
|
rb_define_method(cX509Cert, "to_der", ossl_x509_to_der, 0);
|
|
|
|
rb_define_method(cX509Cert, "to_pem", ossl_x509_to_pem, 0);
|
|
|
|
rb_define_alias(cX509Cert, "to_s", "to_pem");
|
|
|
|
rb_define_method(cX509Cert, "to_text", ossl_x509_to_text, 0);
|
|
|
|
rb_define_method(cX509Cert, "version", ossl_x509_get_version, 0);
|
|
|
|
rb_define_method(cX509Cert, "version=", ossl_x509_set_version, 1);
|
|
|
|
rb_define_method(cX509Cert, "signature_algorithm", ossl_x509_get_signature_algorithm, 0);
|
|
|
|
rb_define_method(cX509Cert, "serial", ossl_x509_get_serial, 0);
|
|
|
|
rb_define_method(cX509Cert, "serial=", ossl_x509_set_serial, 1);
|
|
|
|
rb_define_method(cX509Cert, "subject", ossl_x509_get_subject, 0);
|
|
|
|
rb_define_method(cX509Cert, "subject=", ossl_x509_set_subject, 1);
|
|
|
|
rb_define_method(cX509Cert, "issuer", ossl_x509_get_issuer, 0);
|
|
|
|
rb_define_method(cX509Cert, "issuer=", ossl_x509_set_issuer, 1);
|
|
|
|
rb_define_method(cX509Cert, "not_before", ossl_x509_get_not_before, 0);
|
|
|
|
rb_define_method(cX509Cert, "not_before=", ossl_x509_set_not_before, 1);
|
|
|
|
rb_define_method(cX509Cert, "not_after", ossl_x509_get_not_after, 0);
|
|
|
|
rb_define_method(cX509Cert, "not_after=", ossl_x509_set_not_after, 1);
|
|
|
|
rb_define_method(cX509Cert, "public_key", ossl_x509_get_public_key, 0);
|
|
|
|
rb_define_method(cX509Cert, "public_key=", ossl_x509_set_public_key, 1);
|
|
|
|
rb_define_method(cX509Cert, "sign", ossl_x509_sign, 2);
|
|
|
|
rb_define_method(cX509Cert, "verify", ossl_x509_verify, 1);
|
|
|
|
rb_define_method(cX509Cert, "check_private_key", ossl_x509_check_private_key, 1);
|
|
|
|
rb_define_method(cX509Cert, "extensions", ossl_x509_get_extensions, 0);
|
|
|
|
rb_define_method(cX509Cert, "extensions=", ossl_x509_set_extensions, 1);
|
|
|
|
rb_define_method(cX509Cert, "add_extension", ossl_x509_add_extension, 1);
|
|
|
|
rb_define_method(cX509Cert, "inspect", ossl_x509_inspect, 0);
|
|
|
|
}
|
|
|
|
|