Class: OpenSSL::X509::Certificate
- Inherits:
-
Object
- Object
- OpenSSL::X509::Certificate
- Defined in:
- ossl_x509cert.c
Instance Method Summary collapse
- #add_extension(extension) ⇒ Object
-
#check_private_key(key) ⇒ Object
Checks if ‘key’ is PRIV key for this cert.
- #extensions ⇒ Array
- #extensions=(ary) ⇒ Object
- #initialize(*args) ⇒ Object constructor
- #inspect ⇒ Object
- #issuer ⇒ Object
- #issuer=(name) ⇒ Object
- #not_after ⇒ Time
- #not_before=(time) ⇒ Time
- #not_before ⇒ Time
- #not_before=(time) ⇒ Time
- #public_key ⇒ Object
- #public_key=(key) ⇒ Object
- #serial ⇒ Integer
- #serial=(integer) ⇒ Integer
- #sign(key, digest) ⇒ self
- #signature_algorithm ⇒ String
- #subject ⇒ Object
- #subject=(name) ⇒ Object
- #to_der ⇒ String
- #to_pem ⇒ String (also: #to_s)
- #to_text ⇒ String
-
#verify(key) ⇒ Object
Checks that cert signature is made with PRIVversion of this PUBLIC ‘key’.
- #version ⇒ Integer
- #version=(integer) ⇒ Integer
Constructor Details
#new ⇒ Object #new(string) ⇒ Object
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 |
# File 'ossl_x509cert.c', line 133
static VALUE
ossl_x509_initialize(int argc, VALUE *argv, VALUE self)
{
BIO *in;
X509 *x509;
VALUE arg;
if (rb_scan_args(argc, argv, "01", &arg) == 0) {
/* create just empty X509Cert */
return self;
}
arg = ossl_to_der_if_possible(arg);
in = ossl_obj2bio(arg);
x509 = PEM_read_bio_X509(in, (X509 **)&DATA_PTR(self), NULL, NULL);
if (!x509) {
BIO_reset(in);
x509 = d2i_X509_bio(in, (X509 **)&DATA_PTR(self));
}
BIO_free(in);
if (!x509) ossl_raise(eX509CertError, NULL);
return self;
}
|
Instance Method Details
#add_extension(extension) ⇒ Object
672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 |
# File 'ossl_x509cert.c', line 672
static VALUE
ossl_x509_add_extension(VALUE self, VALUE extension)
{
X509 *x509;
X509_EXTENSION *ext;
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;
}
|
#check_private_key(key) ⇒ Object
Checks if ‘key’ is PRIV key for this cert
593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 |
# File 'ossl_x509cert.c', line 593
static VALUE
ossl_x509_check_private_key(VALUE self, VALUE key)
{
X509 *x509;
EVP_PKEY *pkey;
/* not needed private key, but should be */
pkey = GetPrivPKeyPtr(key); /* NO NEED TO DUP */
GetX509(self, x509);
if (!X509_check_private_key(x509, pkey)) {
OSSL_Warning("Check private key:%s", OSSL_ErrMsg());
return Qfalse;
}
return Qtrue;
}
|
#extensions ⇒ Array
614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 |
# File 'ossl_x509cert.c', line 614
static VALUE
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;
}
|
#extensions=(ary) ⇒ Object
640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 |
# File 'ossl_x509cert.c', line 640
static VALUE
ossl_x509_set_extensions(VALUE self, VALUE ary)
{
X509 *x509;
X509_EXTENSION *ext;
int i;
Check_Type(ary, T_ARRAY);
/* All ary's members should be X509Extension */
for (i=0; i<RARRAY_LEN(ary); i++) {
OSSL_Check_Kind(RARRAY_PTR(ary)[i], cX509Ext);
}
GetX509(self, x509);
sk_X509_EXTENSION_pop_free(x509->cert_info->extensions, X509_EXTENSION_free);
x509->cert_info->extensions = NULL;
for (i=0; i<RARRAY_LEN(ary); i++) {
ext = DupX509ExtPtr(RARRAY_PTR(ary)[i]);
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;
}
|
#inspect ⇒ Object
689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 |
# File 'ossl_x509cert.c', line 689
static VALUE
ossl_x509_inspect(VALUE self)
{
VALUE str;
char *cname = rb_class2name(rb_obj_class(self));
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;
}
|
#issuer ⇒ Object
401 402 403 404 405 406 407 408 409 410 411 412 413 |
# File 'ossl_x509cert.c', line 401
static VALUE
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);
}
|
#issuer=(name) ⇒ Object
419 420 421 422 423 424 425 426 427 428 429 430 |
# File 'ossl_x509cert.c', line 419
static VALUE
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;
}
|
#not_after ⇒ Time
473 474 475 476 477 478 479 480 481 482 483 484 485 |
# File 'ossl_x509cert.c', line 473
static VALUE
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);
}
|
#not_before=(time) ⇒ Time
491 492 493 494 495 496 497 498 499 500 501 502 503 504 |
# File 'ossl_x509cert.c', line 491
static VALUE
ossl_x509_set_not_after(VALUE self, VALUE time)
{
X509 *x509;
time_t sec;
sec = time_to_time_t(time);
GetX509(self, x509);
if (!X509_time_adj(X509_get_notAfter(x509), 0, &sec)) {
ossl_raise(eX509CertError, NULL);
}
return time;
}
|
#not_before ⇒ Time
436 437 438 439 440 441 442 443 444 445 446 447 448 |
# File 'ossl_x509cert.c', line 436
static VALUE
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);
}
|
#not_before=(time) ⇒ Time
454 455 456 457 458 459 460 461 462 463 464 465 466 467 |
# File 'ossl_x509cert.c', line 454
static VALUE
ossl_x509_set_not_before(VALUE self, VALUE time)
{
X509 *x509;
time_t sec;
sec = time_to_time_t(time);
GetX509(self, x509);
if (!X509_time_adj(X509_get_notBefore(x509), 0, &sec)) {
ossl_raise(eX509CertError, NULL);
}
return time;
}
|
#public_key ⇒ Object
510 511 512 513 514 515 516 517 518 519 520 521 522 |
# File 'ossl_x509cert.c', line 510
static VALUE
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 */
}
|
#public_key=(key) ⇒ Object
528 529 530 531 532 533 534 535 536 537 538 539 |
# File 'ossl_x509cert.c', line 528
static VALUE
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;
}
|
#serial ⇒ Integer
311 312 313 314 315 316 317 318 319 |
# File 'ossl_x509cert.c', line 311
static VALUE
ossl_x509_get_serial(VALUE self)
{
X509 *x509;
GetX509(self, x509);
return asn1integer_to_num(X509_get_serialNumber(x509));
}
|
#serial=(integer) ⇒ Integer
325 326 327 328 329 330 331 332 333 334 335 336 |
# File 'ossl_x509cert.c', line 325
static VALUE
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));
return num;
}
|
#sign(key, digest) ⇒ self
545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 |
# File 'ossl_x509cert.c', line 545
static VALUE
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);
GetX509(self, x509);
if (!X509_sign(x509, pkey, md)) {
ossl_raise(eX509CertError, NULL);
}
return self;
}
|
#signature_algorithm ⇒ String
342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 |
# File 'ossl_x509cert.c', line 342
static VALUE
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);
}
str = ossl_membio2str(out);
return str;
}
|
#subject ⇒ Object
366 367 368 369 370 371 372 373 374 375 376 377 378 |
# File 'ossl_x509cert.c', line 366
static VALUE
ossl_x509_get_subject(VALUE self)
{
X509 *x509;
X509_NAME *name;
GetX509(self, x509);
if (!(name = X509_get_subject_name(x509))) { /* NO DUP - don't free! */
ossl_raise(eX509CertError, NULL);
}
return ossl_x509name_new(name);
}
|
#subject=(name) ⇒ Object
384 385 386 387 388 389 390 391 392 393 394 395 |
# File 'ossl_x509cert.c', line 384
static VALUE
ossl_x509_set_subject(VALUE self, VALUE subject)
{
X509 *x509;
GetX509(self, x509);
if (!X509_set_subject_name(x509, GetX509NamePtr(subject))) { /* DUPs name */
ossl_raise(eX509CertError, NULL);
}
return subject;
}
|
#to_der ⇒ String
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 |
# File 'ossl_x509cert.c', line 181
static VALUE
ossl_x509_to_der(VALUE self)
{
X509 *x509;
VALUE str;
long len;
unsigned char *p;
GetX509(self, x509);
if ((len = i2d_X509(x509, NULL)) <= 0)
ossl_raise(eX509CertError, NULL);
str = rb_str_new(0, len);
p = RSTRING_PTR(str);
if (i2d_X509(x509, &p) <= 0)
ossl_raise(eX509CertError, NULL);
ossl_str_adjust(str, p);
return str;
}
|
#to_pem ⇒ String Also known as: to_s
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 |
# File 'ossl_x509cert.c', line 205
static VALUE
ossl_x509_to_pem(VALUE self)
{
X509 *x509;
BIO *out;
VALUE str;
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);
}
str = ossl_membio2str(out);
return str;
}
|
#to_text ⇒ String
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 |
# File 'ossl_x509cert.c', line 229
static VALUE
ossl_x509_to_text(VALUE self)
{
X509 *x509;
BIO *out;
VALUE str;
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);
}
str = ossl_membio2str(out);
return str;
}
|
#verify(key) ⇒ Object
Checks that cert signature is made with PRIVversion of this PUBLIC ‘key’
568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 |
# File 'ossl_x509cert.c', line 568
static VALUE
ossl_x509_verify(VALUE self, VALUE key)
{
X509 *x509;
EVP_PKEY *pkey;
int i;
pkey = GetPKeyPtr(key); /* NO NEED TO DUP */
GetX509(self, x509);
if ((i = X509_verify(x509, pkey)) < 0) {
ossl_raise(eX509CertError, NULL);
}
if (i > 0) {
return Qtrue;
}
return Qfalse;
}
|
#version ⇒ Integer
276 277 278 279 280 281 282 283 284 |
# File 'ossl_x509cert.c', line 276
static VALUE
ossl_x509_get_version(VALUE self)
{
X509 *x509;
GetX509(self, x509);
return LONG2NUM(X509_get_version(x509));
}
|
#version=(integer) ⇒ Integer
290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 |
# File 'ossl_x509cert.c', line 290
static VALUE
ossl_x509_set_version(VALUE self, VALUE version)
{
X509 *x509;
long ver;
if ((ver = NUM2LONG(version)) < 0) {
ossl_raise(eX509CertError, "version must be >= 0!");
}
GetX509(self, x509);
if (!X509_set_version(x509, ver)) {
ossl_raise(eX509CertError, NULL);
}
return version;
}
|