Ruby 2.0.0p353(2013-11-22revision43784)

- Main Page
- Modules
- Data Structures
- Files

- File List
- Globals

ossl_x509cert.c
Go to the documentation of this file.
1 /*
2 * $Id: ossl_x509cert.c 35190 2012-03-31 03:07:53Z emboss $
3 * 'OpenSSL for Ruby' project
4 * Copyright (C) 2001-2002 Michal Rokos <m.rokos@sh.cvut.cz>
5 * All rights reserved.
6 */
7 /*
8 * This program is licenced under the same licence as Ruby.
9 * (See the file 'LICENCE'.)
10 */
11 #include "ossl.h"
12
13 #define WrapX509(klass, obj, x509) do { \
14 if (!(x509)) { \
15 ossl_raise(rb_eRuntimeError, "CERT wasn't initialized!"); \
16 } \
17 (obj) = Data_Wrap_Struct((klass), 0, X509_free, (x509)); \
18 } while (0)
19 #define GetX509(obj, x509) do { \
20 Data_Get_Struct((obj), X509, (x509)); \
21 if (!(x509)) { \
22 ossl_raise(rb_eRuntimeError, "CERT wasn't initialized!"); \
23 } \
24 } while (0)
25 #define SafeGetX509(obj, x509) do { \
26 OSSL_Check_Kind((obj), cX509Cert); \
27 GetX509((obj), (x509)); \
28 } while (0)
29
30 /*
31 * Classes
32 */
33 VALUE cX509Cert;
34 VALUE eX509CertError;
35
36 /*
37 * Public
38 */
39 VALUE
40 ossl_x509_new(X509 *x509)
41 {
42 X509 *new;
43 VALUE obj;
44
45 if (!x509) {
46 new = X509_new();
47 } else {
48 new = X509_dup(x509);
49 }
50 if (!new) {
51 ossl_raise(eX509CertError, NULL);
52 }
53 WrapX509(cX509Cert, obj, new);
54
55 return obj;
56 }
57
58 VALUE
59 ossl_x509_new_from_file(VALUE filename)
60 {
61 X509 *x509;
62 FILE *fp;
63 VALUE obj;
64
65 SafeStringValue(filename);
66 if (!(fp = fopen(RSTRING_PTR(filename), "r"))) {
67 ossl_raise(eX509CertError, "%s", strerror(errno));
68 }
69 rb_fd_fix_cloexec(fileno(fp));
70 x509 = PEM_read_X509(fp, NULL, NULL, NULL);
71 /*
72 * prepare for DER...
73 #if !defined(OPENSSL_NO_FP_API)
74 if (!x509) {
75 (void)ERR_get_error();
76 rewind(fp);
77
78 x509 = d2i_X509_fp(fp, NULL);
79 }
80 #endif
81 */
82 fclose(fp);
83 if (!x509) {
84 ossl_raise(eX509CertError, NULL);
85 }
86 WrapX509(cX509Cert, obj, x509);
87
88 return obj;
89 }
90
91 X509 *
92 GetX509CertPtr(VALUE obj)
93 {
94 X509 *x509;
95
96 SafeGetX509(obj, x509);
97
98 return x509;
99 }
100
101 X509 *
102 DupX509CertPtr(VALUE obj)
103 {
104 X509 *x509;
105
106 SafeGetX509(obj, x509);
107
108 CRYPTO_add(&x509->references, 1, CRYPTO_LOCK_X509);
109
110 return x509;
111 }
112
113 /*
114 * Private
115 */
116 static VALUE
117 ossl_x509_alloc(VALUE klass)
118 {
119 X509 *x509;
120 VALUE obj;
121
122 x509 = X509_new();
123 if (!x509) ossl_raise(eX509CertError, NULL);
124
125 WrapX509(klass, obj, x509);
126
127 return obj;
128 }
129
130 /*
131 * call-seq:
132 * Certificate.new => cert
133 * Certificate.new(string) => cert
134 */
135 static VALUE
136 ossl_x509_initialize(int argc, VALUE *argv, VALUE self)
137 {
138 BIO *in;
139 X509 *x509, *x = DATA_PTR(self);
140 VALUE arg;
141
142 if (rb_scan_args(argc, argv, "01", &arg) == 0) {
143 /* create just empty X509Cert */
144 return self;
145 }
146 arg = ossl_to_der_if_possible(arg);
147 in = ossl_obj2bio(arg);
148 x509 = PEM_read_bio_X509(in, &x, NULL, NULL);
149 DATA_PTR(self) = x;
150 if (!x509) {
151 OSSL_BIO_reset(in);
152 x509 = d2i_X509_bio(in, &x);
153 DATA_PTR(self) = x;
154 }
155 BIO_free(in);
156 if (!x509) ossl_raise(eX509CertError, NULL);
157
158 return self;
159 }
160
161 static VALUE
162 ossl_x509_copy(VALUE self, VALUE other)
163 {
164 X509 *a, *b, *x509;
165
166 rb_check_frozen(self);
167 if (self == other) return self;
168
169 GetX509(self, a);
170 SafeGetX509(other, b);
171
172 x509 = X509_dup(b);
173 if (!x509) ossl_raise(eX509CertError, NULL);
174
175 DATA_PTR(self) = x509;
176 X509_free(a);
177
178 return self;
179 }
180
181 /*
182 * call-seq:
183 * cert.to_der => string
184 */
185 static VALUE
186 ossl_x509_to_der(VALUE self)
187 {
188 X509 *x509;
189 VALUE str;
190 long len;
191 unsigned char *p;
192
193 GetX509(self, x509);
194 if ((len = i2d_X509(x509, NULL)) <= 0)
195 ossl_raise(eX509CertError, NULL);
196 str = rb_str_new(0, len);
197 p = (unsigned char *)RSTRING_PTR(str);
198 if (i2d_X509(x509, &p) <= 0)
199 ossl_raise(eX509CertError, NULL);
200 ossl_str_adjust(str, p);
201
202 return str;
203 }
204
205 /*
206 * call-seq:
207 * cert.to_pem => string
208 */
209 static VALUE
210 ossl_x509_to_pem(VALUE self)
211 {
212 X509 *x509;
213 BIO *out;
214 VALUE str;
215
216 GetX509(self, x509);
217 out = BIO_new(BIO_s_mem());
218 if (!out) ossl_raise(eX509CertError, NULL);
219
220 if (!PEM_write_bio_X509(out, x509)) {
221 BIO_free(out);
222 ossl_raise(eX509CertError, NULL);
223 }
224 str = ossl_membio2str(out);
225
226 return str;
227 }
228
229 /*
230 * call-seq:
231 * cert.to_text => string
232 */
233 static VALUE
234 ossl_x509_to_text(VALUE self)
235 {
236 X509 *x509;
237 BIO *out;
238 VALUE str;
239
240 GetX509(self, x509);
241
242 out = BIO_new(BIO_s_mem());
243 if (!out) ossl_raise(eX509CertError, NULL);
244
245 if (!X509_print(out, x509)) {
246 BIO_free(out);
247 ossl_raise(eX509CertError, NULL);
248 }
249 str = ossl_membio2str(out);
250
251 return str;
252 }
253
254 #if 0
255 /*
256 * Makes from X509 X509_REQuest
257 */
258 static VALUE
259 ossl_x509_to_req(VALUE self)
260 {
261 X509 *x509;
262 X509_REQ *req;
263 VALUE obj;
264
265 GetX509(self, x509);
266 if (!(req = X509_to_X509_REQ(x509, NULL, EVP_md5()))) {
267 ossl_raise(eX509CertError, NULL);
268 }
269 obj = ossl_x509req_new(req);
270 X509_REQ_free(req);
271
272 return obj;
273 }
274 #endif
275
276 /*
277 * call-seq:
278 * cert.version => integer
279 */
280 static VALUE
281 ossl_x509_get_version(VALUE self)
282 {
283 X509 *x509;
284
285 GetX509(self, x509);
286
287 return LONG2NUM(X509_get_version(x509));
288 }
289
290 /*
291 * call-seq:
292 * cert.version = integer => integer
293 */
294 static VALUE
295 ossl_x509_set_version(VALUE self, VALUE version)
296 {
297 X509 *x509;
298 long ver;
299
300 if ((ver = NUM2LONG(version)) < 0) {
301 ossl_raise(eX509CertError, "version must be >= 0!");
302 }
303 GetX509(self, x509);
304 if (!X509_set_version(x509, ver)) {
305 ossl_raise(eX509CertError, NULL);
306 }
307
308 return version;
309 }
310
311 /*
312 * call-seq:
313 * cert.serial => integer
314 */
315 static VALUE
316 ossl_x509_get_serial(VALUE self)
317 {
318 X509 *x509;
319
320 GetX509(self, x509);
321
322 return asn1integer_to_num(X509_get_serialNumber(x509));
323 }
324
325 /*
326 * call-seq:
327 * cert.serial = integer => integer
328 */
329 static VALUE
330 ossl_x509_set_serial(VALUE self, VALUE num)
331 {
332 X509 *x509;
333
334 GetX509(self, x509);
335
336 x509->cert_info->serialNumber =
337 num_to_asn1integer(num, X509_get_serialNumber(x509));
338
339 return num;
340 }
341
342 /*
343 * call-seq:
344 * cert.signature_algorithm => string
345 */
346 static VALUE
347 ossl_x509_get_signature_algorithm(VALUE self)
348 {
349 X509 *x509;
350 BIO *out;
351 VALUE str;
352
353 GetX509(self, x509);
354 out = BIO_new(BIO_s_mem());
355 if (!out) ossl_raise(eX509CertError, NULL);
356
357 if (!i2a_ASN1_OBJECT(out, x509->cert_info->signature->algorithm)) {
358 BIO_free(out);
359 ossl_raise(eX509CertError, NULL);
360 }
361 str = ossl_membio2str(out);
362
363 return str;
364 }
365
366 /*
367 * call-seq:
368 * cert.subject => name
369 */
370 static VALUE
371 ossl_x509_get_subject(VALUE self)
372 {
373 X509 *x509;
374 X509_NAME *name;
375
376 GetX509(self, x509);
377 if (!(name = X509_get_subject_name(x509))) { /* NO DUP - don't free! */
378 ossl_raise(eX509CertError, NULL);
379 }
380
381 return ossl_x509name_new(name);
382 }
383
384 /*
385 * call-seq:
386 * cert.subject = name => name
387 */
388 static VALUE
389 ossl_x509_set_subject(VALUE self, VALUE subject)
390 {
391 X509 *x509;
392
393 GetX509(self, x509);
394 if (!X509_set_subject_name(x509, GetX509NamePtr(subject))) { /* DUPs name */
395 ossl_raise(eX509CertError, NULL);
396 }
397
398 return subject;
399 }
400
401 /*
402 * call-seq:
403 * cert.issuer => name
404 */
405 static VALUE
406 ossl_x509_get_issuer(VALUE self)
407 {
408 X509 *x509;
409 X509_NAME *name;
410
411 GetX509(self, x509);
412 if(!(name = X509_get_issuer_name(x509))) { /* NO DUP - don't free! */
413 ossl_raise(eX509CertError, NULL);
414 }
415
416 return ossl_x509name_new(name);
417 }
418
419 /*
420 * call-seq:
421 * cert.issuer = name => name
422 */
423 static VALUE
424 ossl_x509_set_issuer(VALUE self, VALUE issuer)
425 {
426 X509 *x509;
427
428 GetX509(self, x509);
429 if (!X509_set_issuer_name(x509, GetX509NamePtr(issuer))) { /* DUPs name */
430 ossl_raise(eX509CertError, NULL);
431 }
432
433 return issuer;
434 }
435
436 /*
437 * call-seq:
438 * cert.not_before => time
439 */
440 static VALUE
441 ossl_x509_get_not_before(VALUE self)
442 {
443 X509 *x509;
444 ASN1_UTCTIME *asn1time;
445
446 GetX509(self, x509);
447 if (!(asn1time = X509_get_notBefore(x509))) { /* NO DUP - don't free! */
448 ossl_raise(eX509CertError, NULL);
449 }
450
451 return asn1time_to_time(asn1time);
452 }
453
454 /*
455 * call-seq:
456 * cert.not_before = time => time
457 */
458 static VALUE
459 ossl_x509_set_not_before(VALUE self, VALUE time)
460 {
461 X509 *x509;
462 time_t sec;
463
464 sec = time_to_time_t(time);
465 GetX509(self, x509);
466 if (!X509_time_adj(X509_get_notBefore(x509), 0, &sec)) {
467 ossl_raise(eX509CertError, NULL);
468 }
469
470 return time;
471 }
472
473 /*
474 * call-seq:
475 * cert.not_after => time
476 */
477 static VALUE
478 ossl_x509_get_not_after(VALUE self)
479 {
480 X509 *x509;
481 ASN1_TIME *asn1time;
482
483 GetX509(self, x509);
484 if (!(asn1time = X509_get_notAfter(x509))) { /* NO DUP - don't free! */
485 ossl_raise(eX509CertError, NULL);
486 }
487
488 return asn1time_to_time(asn1time);
489 }
490
491 /*
492 * call-seq:
493 * cert.not_after = time => time
494 */
495 static VALUE
496 ossl_x509_set_not_after(VALUE self, VALUE time)
497 {
498 X509 *x509;
499 time_t sec;
500
501 sec = time_to_time_t(time);
502 GetX509(self, x509);
503 if (!X509_time_adj(X509_get_notAfter(x509), 0, &sec)) {
504 ossl_raise(eX509CertError, NULL);
505 }
506
507 return time;
508 }
509
510 /*
511 * call-seq:
512 * cert.public_key => key
513 */
514 static VALUE
515 ossl_x509_get_public_key(VALUE self)
516 {
517 X509 *x509;
518 EVP_PKEY *pkey;
519
520 GetX509(self, x509);
521 if (!(pkey = X509_get_pubkey(x509))) { /* adds an reference */
522 ossl_raise(eX509CertError, NULL);
523 }
524
525 return ossl_pkey_new(pkey); /* NO DUP - OK */
526 }
527
528 /*
529 * call-seq:
530 * cert.public_key = key => key
531 */
532 static VALUE
533 ossl_x509_set_public_key(VALUE self, VALUE key)
534 {
535 X509 *x509;
536
537 GetX509(self, x509);
538 if (!X509_set_pubkey(x509, GetPKeyPtr(key))) { /* DUPs pkey */
539 ossl_raise(eX509CertError, NULL);
540 }
541
542 return key;
543 }
544
545 /*
546 * call-seq:
547 * cert.sign(key, digest) => self
548 */
549 static VALUE
550 ossl_x509_sign(VALUE self, VALUE key, VALUE digest)
551 {
552 X509 *x509;
553 EVP_PKEY *pkey;
554 const EVP_MD *md;
555
556 pkey = GetPrivPKeyPtr(key); /* NO NEED TO DUP */
557 md = GetDigestPtr(digest);
558 GetX509(self, x509);
559 if (!X509_sign(x509, pkey, md)) {
560 ossl_raise(eX509CertError, NULL);
561 }
562
563 return self;
564 }
565
566 /*
567 * call-seq:
568 * cert.verify(key) => true | false
569 *
570 * Checks that cert signature is made with PRIVversion of this PUBLIC 'key'
571 */
572 static VALUE
573 ossl_x509_verify(VALUE self, VALUE key)
574 {
575 X509 *x509;
576 EVP_PKEY *pkey;
577 int i;
578
579 pkey = GetPKeyPtr(key); /* NO NEED TO DUP */
580 GetX509(self, x509);
581 if ((i = X509_verify(x509, pkey)) < 0) {
582 ossl_raise(eX509CertError, NULL);
583 }
584 if (i > 0) {
585 return Qtrue;
586 }
587
588 return Qfalse;
589 }
590
591 /*
592 * call-seq:
593 * cert.check_private_key(key)
594 *
595 * Checks if 'key' is PRIV key for this cert
596 */
597 static VALUE
598 ossl_x509_check_private_key(VALUE self, VALUE key)
599 {
600 X509 *x509;
601 EVP_PKEY *pkey;
602
603 /* not needed private key, but should be */
604 pkey = GetPrivPKeyPtr(key); /* NO NEED TO DUP */
605 GetX509(self, x509);
606 if (!X509_check_private_key(x509, pkey)) {
607 OSSL_Warning("Check private key:%s", OSSL_ErrMsg());
608 return Qfalse;
609 }
610
611 return Qtrue;
612 }
613
614 /*
615 * call-seq:
616 * cert.extensions => [extension...]
617 */
618 static VALUE
619 ossl_x509_get_extensions(VALUE self)
620 {
621 X509 *x509;
622 int count, i;
623 X509_EXTENSION *ext;
624 VALUE ary;
625
626 GetX509(self, x509);
627 count = X509_get_ext_count(x509);
628 if (count < 0) {
629 return rb_ary_new();
630 }
631 ary = rb_ary_new2(count);
632 for (i=0; i<count; i++) {
633 ext = X509_get_ext(x509, i); /* NO DUP - don't free! */
634 rb_ary_push(ary, ossl_x509ext_new(ext));
635 }
636
637 return ary;
638 }
639
640 /*
641 * call-seq:
642 * cert.extensions = [ext...] => [ext...]
643 */
644 static VALUE
645 ossl_x509_set_extensions(VALUE self, VALUE ary)
646 {
647 X509 *x509;
648 X509_EXTENSION *ext;
649 int i;
650
651 Check_Type(ary, T_ARRAY);
652 /* All ary's members should be X509Extension */
653 for (i=0; i<RARRAY_LEN(ary); i++) {
654 OSSL_Check_Kind(RARRAY_PTR(ary)[i], cX509Ext);
655 }
656 GetX509(self, x509);
657 sk_X509_EXTENSION_pop_free(x509->cert_info->extensions, X509_EXTENSION_free);
658 x509->cert_info->extensions = NULL;
659 for (i=0; i<RARRAY_LEN(ary); i++) {
660 ext = DupX509ExtPtr(RARRAY_PTR(ary)[i]);
661
662 if (!X509_add_ext(x509, ext, -1)) { /* DUPs ext - FREE it */
663 X509_EXTENSION_free(ext);
664 ossl_raise(eX509CertError, NULL);
665 }
666 X509_EXTENSION_free(ext);
667 }
668
669 return ary;
670 }
671
672 /*
673 * call-seq:
674 * cert.add_extension(extension) => extension
675 */
676 static VALUE
677 ossl_x509_add_extension(VALUE self, VALUE extension)
678 {
679 X509 *x509;
680 X509_EXTENSION *ext;
681
682 GetX509(self, x509);
683 ext = DupX509ExtPtr(extension);
684 if (!X509_add_ext(x509, ext, -1)) { /* DUPs ext - FREE it */
685 X509_EXTENSION_free(ext);
686 ossl_raise(eX509CertError, NULL);
687 }
688 X509_EXTENSION_free(ext);
689
690 return extension;
691 }
692
693 static VALUE
694 ossl_x509_inspect(VALUE self)
695 {
696 VALUE str;
697 const char *cname = rb_class2name(rb_obj_class(self));
698
699 str = rb_str_new2("#<");
700 rb_str_cat2(str, cname);
701 rb_str_cat2(str, " ");
702
703 rb_str_cat2(str, "subject=");
704 rb_str_append(str, rb_inspect(ossl_x509_get_subject(self)));
705 rb_str_cat2(str, ", ");
706
707 rb_str_cat2(str, "issuer=");
708 rb_str_append(str, rb_inspect(ossl_x509_get_issuer(self)));
709 rb_str_cat2(str, ", ");
710
711 rb_str_cat2(str, "serial=");
712 rb_str_append(str, rb_inspect(ossl_x509_get_serial(self)));
713 rb_str_cat2(str, ", ");
714
715 rb_str_cat2(str, "not_before=");
716 rb_str_append(str, rb_inspect(ossl_x509_get_not_before(self)));
717 rb_str_cat2(str, ", ");
718
719 rb_str_cat2(str, "not_after=");
720 rb_str_append(str, rb_inspect(ossl_x509_get_not_after(self)));
721
722 str = rb_str_cat2(str, ">");
723
724 return str;
725 }
726
727 /*
728 * INIT
729 */
730 void
731 Init_ossl_x509cert()
732 {
733
734 #if 0
735 mOSSL = rb_define_module("OpenSSL"); /* let rdoc know about mOSSL */
736 mX509 = rb_define_module_under(mOSSL, "X509");
737 #endif
738
739 eX509CertError = rb_define_class_under(mX509, "CertificateError", eOSSLError);
740
741 /* Document-class: OpenSSL::X509::Certificate
742 *
743 * Implementation of an X.509 certificate as specified in RFC 5280.
744 * Provides access to a certificate's attributes and allows certificates
745 * to be read from a string, but also supports the creation of new
746 * certificates from scratch.
747 *
748 * === Reading a certificate from a file
749 *
750 * Certificate is capable of handling DER-encoded certificates and
751 * certificates encoded in OpenSSL's PEM format.
752 *
753 * raw = File.read "cert.cer" # DER- or PEM-encoded
754 * certificate = OpenSSL::X509::Certificate.new raw
755 *
756 * === Saving a certificate to a file
757 *
758 * A certificate may be encoded in DER format
759 *
760 * cert = ...
761 * File.open("cert.cer", "wb") { |f| f.print cert.to_der }
762 *
763 * or in PEM format
764 *
765 * cert = ...
766 * File.open("cert.pem", "wb") { |f| f.print cert.to_pem }
767 *
768 * X.509 certificates are associated with a private/public key pair,
769 * typically a RSA, DSA or ECC key (see also OpenSSL::PKey::RSA,
770 * OpenSSL::PKey::DSA and OpenSSL::PKey::EC), the public key itself is
771 * stored within the certificate and can be accessed in form of an
772 * OpenSSL::PKey. Certificates are typically used to be able to associate
773 * some form of identity with a key pair, for example web servers serving
774 * pages over HTTPs use certificates to authenticate themselves to the user.
775 *
776 * The public key infrastructure (PKI) model relies on trusted certificate
777 * authorities ("root CAs") that issue these certificates, so that end
778 * users need to base their trust just on a selected few authorities
779 * that themselves again vouch for subordinate CAs issuing their
780 * certificates to end users.
781 *
782 * The OpenSSL::X509 module provides the tools to set up an independent
783 * PKI, similar to scenarios where the 'openssl' command line tool is
784 * used for issuing certificates in a private PKI.
785 *
786 * === Creating a root CA certificate and an end-entity certificate
787 *
788 * First, we need to create a "self-signed" root certificate. To do so,
789 * we need to generate a key first. Please note that the choice of "1"
790 * as a serial number is considered a security flaw for real certificates.
791 * Secure choices are integers in the two-digit byte range and ideally
792 * not sequential but secure random numbers, steps omitted here to keep
793 * the example concise.
794 *
795 * root_key = OpenSSL::PKey::RSA.new 2048 # the CA's public/private key
796 * root_ca = OpenSSL::X509::Certificate.new
797 * root_ca.version = 2 # cf. RFC 5280 - to make it a "v3" certificate
798 * root_ca.serial = 1
799 * root_ca.subject = OpenSSL::X509::Name.parse "/DC=org/DC=ruby-lang/CN=Ruby CA"
800 * root_ca.issuer = root_ca.subject # root CA's are "self-signed"
801 * root_ca.public_key = root_key.public_key
802 * root_ca.not_before = Time.now
803 * root_ca.not_after = root_ca.not_before + 2 * 365 * 24 * 60 * 60 # 2 years validity
804 * ef = OpenSSL::X509::ExtensionFactory.new
805 * ef.subject_certificate = root_ca
806 * ef.issuer_certificate = root_ca
807 * root_ca.add_extension(ef.create_extension("basicConstraints","CA:TRUE",true))
808 * root_ca.add_extension(ef.create_extension("keyUsage","keyCertSign, cRLSign", true))
809 * root_ca.add_extension(ef.create_extension("subjectKeyIdentifier","hash",false))
810 * root_ca.add_extension(ef.create_extension("authorityKeyIdentifier","keyid:always",false))
811 * root_ca.sign(root_key, OpenSSL::Digest::SHA256.new)
812 *
813 * The next step is to create the end-entity certificate using the root CA
814 * certificate.
815 *
816 * key = OpenSSL::PKey::RSA.new 2048
817 * cert = OpenSSL::X509::Certificate.new
818 * cert.version = 2
819 * cert.serial = 2
820 * cert.subject = OpenSSL::X509::Name.parse "/DC=org/DC=ruby-lang/CN=Ruby certificate"
821 * cert.issuer = root_ca.subject # root CA is the issuer
822 * cert.public_key = key.public_key
823 * cert.not_before = Time.now
824 * cert.not_after = cert.not_before + 1 * 365 * 24 * 60 * 60 # 1 years validity
825 * ef = OpenSSL::X509::ExtensionFactory.new
826 * ef.subject_certificate = cert
827 * ef.issuer_certificate = root_ca
828 * cert.add_extension(ef.create_extension("keyUsage","digitalSignature", true))
829 * cert.add_extension(ef.create_extension("subjectKeyIdentifier","hash",false))
830 * cert.sign(root_key, OpenSSL::Digest::SHA256.new)
831 *
832 */
833 cX509Cert = rb_define_class_under(mX509, "Certificate", rb_cObject);
834
835 rb_define_alloc_func(cX509Cert, ossl_x509_alloc);
836 rb_define_method(cX509Cert, "initialize", ossl_x509_initialize, -1);
837 rb_define_copy_func(cX509Cert, ossl_x509_copy);
838
839 rb_define_method(cX509Cert, "to_der", ossl_x509_to_der, 0);
840 rb_define_method(cX509Cert, "to_pem", ossl_x509_to_pem, 0);
841 rb_define_alias(cX509Cert, "to_s", "to_pem");
842 rb_define_method(cX509Cert, "to_text", ossl_x509_to_text, 0);
843 rb_define_method(cX509Cert, "version", ossl_x509_get_version, 0);
844 rb_define_method(cX509Cert, "version=", ossl_x509_set_version, 1);
845 rb_define_method(cX509Cert, "signature_algorithm", ossl_x509_get_signature_algorithm, 0);
846 rb_define_method(cX509Cert, "serial", ossl_x509_get_serial, 0);
847 rb_define_method(cX509Cert, "serial=", ossl_x509_set_serial, 1);
848 rb_define_method(cX509Cert, "subject", ossl_x509_get_subject, 0);
849 rb_define_method(cX509Cert, "subject=", ossl_x509_set_subject, 1);
850 rb_define_method(cX509Cert, "issuer", ossl_x509_get_issuer, 0);
851 rb_define_method(cX509Cert, "issuer=", ossl_x509_set_issuer, 1);
852 rb_define_method(cX509Cert, "not_before", ossl_x509_get_not_before, 0);
853 rb_define_method(cX509Cert, "not_before=", ossl_x509_set_not_before, 1);
854 rb_define_method(cX509Cert, "not_after", ossl_x509_get_not_after, 0);
855 rb_define_method(cX509Cert, "not_after=", ossl_x509_set_not_after, 1);
856 rb_define_method(cX509Cert, "public_key", ossl_x509_get_public_key, 0);
857 rb_define_method(cX509Cert, "public_key=", ossl_x509_set_public_key, 1);
858 rb_define_method(cX509Cert, "sign", ossl_x509_sign, 2);
859 rb_define_method(cX509Cert, "verify", ossl_x509_verify, 1);
860 rb_define_method(cX509Cert, "check_private_key", ossl_x509_check_private_key, 1);
861 rb_define_method(cX509Cert, "extensions", ossl_x509_get_extensions, 0);
862 rb_define_method(cX509Cert, "extensions=", ossl_x509_set_extensions, 1);
863 rb_define_method(cX509Cert, "add_extension", ossl_x509_add_extension, 1);
864 rb_define_method(cX509Cert, "inspect", ossl_x509_inspect, 0);
865 }
866
RARRAY_PTR
RARRAY_PTR(q->result)[0]
mOSSL
VALUE mOSSL
Definition: ossl.c:259
ossl_x509_get_signature_algorithm
static VALUE ossl_x509_get_signature_algorithm(VALUE self)
Definition: ossl_x509cert.c:347
ary
volatile VALUE ary
Definition: tcltklib.c:9713
ossl_x509_inspect
static VALUE ossl_x509_inspect(VALUE self)
Definition: ossl_x509cert.c:694
p
Win32OLEIDispatch * p
Definition: win32ole.c:786
count
int count
Definition: encoding.c:51
ossl_x509_get_subject
static VALUE ossl_x509_get_subject(VALUE self)
Definition: ossl_x509cert.c:371
ossl_x509_set_serial
static VALUE ossl_x509_set_serial(VALUE self, VALUE num)
Definition: ossl_x509cert.c:330
out
C_block * out
Definition: crypt.c:308
GetPrivPKeyPtr
EVP_PKEY * GetPrivPKeyPtr(VALUE obj)
Definition: ossl_pkey.c:184
ossl_str_adjust
#define ossl_str_adjust(str, p)
Definition: ossl.h:138
i
ssize_t i
Definition: bigdecimal.c:5655
rb_check_frozen
#define rb_check_frozen(obj)
a
Real * a
Definition: bigdecimal.c:1182
rb_define_alloc_func
void rb_define_alloc_func(VALUE, rb_alloc_func_t)
ossl_x509_set_version
static VALUE ossl_x509_set_version(VALUE self, VALUE version)
Definition: ossl_x509cert.c:295
rb_ary_push
VALUE rb_ary_push(VALUE ary, VALUE item)
Definition: array.c:822
ossl_x509_copy
static VALUE ossl_x509_copy(VALUE self, VALUE other)
Definition: ossl_x509cert.c:162
ossl_x509_get_extensions
static VALUE ossl_x509_get_extensions(VALUE self)
Definition: ossl_x509cert.c:619
RSTRING_PTR
#define RSTRING_PTR(str)
T_ARRAY
#define T_ARRAY
in
int const char * in
Definition: crypt.c:639
ossl_x509_add_extension
static VALUE ossl_x509_add_extension(VALUE self, VALUE extension)
Definition: ossl_x509cert.c:677
rb_define_class_under
VALUE rb_define_class_under(VALUE outer, const char *name, VALUE super)
Defines a class under the namespace of outer.
Definition: class.c:545
Qtrue
return Qtrue
Definition: tcltklib.c:9610
rb_obj_class
VALUE rb_obj_class(VALUE)
Definition: object.c:194
ossl_x509_set_not_after
static VALUE ossl_x509_set_not_after(VALUE self, VALUE time)
Definition: ossl_x509cert.c:496
SafeStringValue
#define SafeStringValue(v)
cX509Ext
VALUE cX509Ext
Definition: ossl_x509ext.c:45
asn1time_to_time
VALUE asn1time_to_time(ASN1_TIME *time)
Definition: ossl_asn1.c:32
rb_str_new2
#define rb_str_new2
DupX509ExtPtr
X509_EXTENSION * DupX509ExtPtr(VALUE)
Definition: ossl_x509ext.c:82
ossl_membio2str
VALUE ossl_membio2str(BIO *bio)
Definition: ossl_bio.c:77
ossl_pkey_new
VALUE ossl_pkey_new(EVP_PKEY *pkey)
Definition: ossl_pkey.c:76
GetX509NamePtr
X509_NAME * GetX509NamePtr(VALUE)
Definition: ossl_x509name.c:64
ossl_x509_new_from_file
VALUE ossl_x509_new_from_file(VALUE)
Definition: ossl_x509cert.c:59
LONG2NUM
#define LONG2NUM(x)
rb_str_append
VALUE rb_str_append(VALUE, VALUE)
Definition: string.c:2121
rb_fd_fix_cloexec
void rb_fd_fix_cloexec(int fd)
Definition: io.c:202
__sFILE
Definition: vsnprintf.c:175
time_to_time_t
time_t time_to_time_t(VALUE time)
Definition: ossl_asn1.c:85
rb_define_copy_func
#define rb_define_copy_func(klass, func)
Definition: ruby_missing.h:14
GetX509CertPtr
X509 * GetX509CertPtr(VALUE)
Definition: ossl_x509cert.c:92
OSSL_Warning
#define OSSL_Warning
Definition: ossl.h:212
ossl_x509_set_not_before
static VALUE ossl_x509_set_not_before(VALUE self, VALUE time)
Definition: ossl_x509cert.c:459
GetX509
#define GetX509(obj, x509)
Definition: ossl_x509cert.c:19
ossl_to_der_if_possible
VALUE ossl_to_der_if_possible(VALUE obj)
Definition: ossl.c:283
Qfalse
return Qfalse
Definition: tcltklib.c:6779
cX509Cert
VALUE cX509Cert
Definition: ossl_x509cert.c:33
RARRAY_LEN
#define RARRAY_LEN(a)
eX509CertError
VALUE eX509CertError
Definition: ossl_x509cert.c:34
str
static VALUE char * str
Definition: tcltklib.c:3547
rb_ary_new
VALUE rb_ary_new(void)
Definition: array.c:424
Check_Type
#define Check_Type(v, t)
OSSL_BIO_reset
#define OSSL_BIO_reset(bio)
Definition: ossl.h:155
rb_str_cat2
VALUE rb_str_cat2(VALUE, const char *)
Definition: string.c:1982
GetDigestPtr
const EVP_MD * GetDigestPtr(VALUE obj)
Definition: ossl_digest.c:36
obj
static VALUE VALUE obj
Definition: tcltklib.c:3158
eOSSLError
VALUE eOSSLError
Definition: ossl.c:264
ossl_x509_new
VALUE ossl_x509_new(X509 *)
Definition: ossl_x509cert.c:40
ossl_x509_get_issuer
static VALUE ossl_x509_get_issuer(VALUE self)
Definition: ossl_x509cert.c:406
ossl_x509ext_new
VALUE ossl_x509ext_new(X509_EXTENSION *)
Definition: ossl_x509ext.c:53
key
static int VALUE key
Definition: tkutil.c:265
argv
VALUE * argv
Definition: tcltklib.c:1971
rb_define_alias
void rb_define_alias(VALUE klass, const char *name1, const char *name2)
Defines an alias of a method.
Definition: class.c:1539
errno
int errno
ossl_x509_set_subject
static VALUE ossl_x509_set_subject(VALUE self, VALUE subject)
Definition: ossl_x509cert.c:389
ossl_x509_get_public_key
static VALUE ossl_x509_get_public_key(VALUE self)
Definition: ossl_x509cert.c:515
ossl_obj2bio
BIO * ossl_obj2bio(VALUE obj)
Definition: ossl_bio.c:17
ossl_x509_set_public_key
static VALUE ossl_x509_set_public_key(VALUE self, VALUE key)
Definition: ossl_x509cert.c:533
ossl_x509_set_extensions
static VALUE ossl_x509_set_extensions(VALUE self, VALUE ary)
Definition: ossl_x509cert.c:645
rb_scan_args
int rb_scan_args(int argc, const VALUE *argv, const char *fmt,...)
Definition: class.c:1570
argc
int argc
Definition: tcltklib.c:1970
SafeGetX509
#define SafeGetX509(obj, x509)
Definition: ossl_x509cert.c:25
ossl_x509_sign
static VALUE ossl_x509_sign(VALUE self, VALUE key, VALUE digest)
Definition: ossl_x509cert.c:550
ossl_x509req_new
VALUE ossl_x509req_new(X509_REQ *)
Definition: ossl_x509req.c:40
mX509
VALUE mX509
Definition: ossl_x509.c:13
ossl_x509_check_private_key
static VALUE ossl_x509_check_private_key(VALUE self, VALUE key)
Definition: ossl_x509cert.c:598
b
Real * b
Definition: bigdecimal.c:1182
OSSL_ErrMsg
#define OSSL_ErrMsg()
Definition: ossl.h:161
rb_define_module_under
VALUE rb_define_module_under(VALUE outer, const char *name)
Definition: class.c:637
strerror
RUBY_EXTERN char * strerror(int)
Definition: strerror.c:11
rb_class2name
const char * rb_class2name(VALUE)
Definition: variable.c:389
arg
arg
Definition: ripper.y:1315
NUM2LONG
#define NUM2LONG(x)
ossl_x509_verify
static VALUE ossl_x509_verify(VALUE self, VALUE key)
Definition: ossl_x509cert.c:573
ossl_x509_get_version
static VALUE ossl_x509_get_version(VALUE self)
Definition: ossl_x509cert.c:281
DATA_PTR
DATA_PTR(self)
OSSL_Check_Kind
#define OSSL_Check_Kind(obj, klass)
Definition: ossl.h:96
rb_cObject
RUBY_EXTERN VALUE rb_cObject
Definition: ripper.y:1426
klass
klass
Definition: tcltklib.c:3504
ossl_x509_get_not_before
static VALUE ossl_x509_get_not_before(VALUE self)
Definition: ossl_x509cert.c:441
ossl_x509_get_serial
static VALUE ossl_x509_get_serial(VALUE self)
Definition: ossl_x509cert.c:316
ossl_x509_to_der
static VALUE ossl_x509_to_der(VALUE self)
Definition: ossl_x509cert.c:186
ossl_raise
void ossl_raise(VALUE exc, const char *fmt,...)
Definition: ossl.c:328
ossl_x509_to_text
static VALUE ossl_x509_to_text(VALUE self)
Definition: ossl_x509cert.c:234
GetPKeyPtr
EVP_PKEY * GetPKeyPtr(VALUE obj)
Definition: ossl_pkey.c:174
DupX509CertPtr
X509 * DupX509CertPtr(VALUE)
Definition: ossl_x509cert.c:102
rb_ary_new2
VALUE rb_ary_new2(long capa)
Definition: array.c:417
rb_str_new
VALUE rb_str_new(const char *, long)
Definition: string.c:425
ossl_x509_initialize
static VALUE ossl_x509_initialize(int argc, VALUE *argv, VALUE self)
Definition: ossl_x509cert.c:136
ossl.h
VALUE
unsigned long VALUE
Definition: ripper.y:104
ossl_x509name_new
VALUE ossl_x509name_new(X509_NAME *)
Definition: ossl_x509name.c:45
fileno
#define fileno(p)
Definition: vsnprintf.c:223
version
static void version(void)
Definition: nkf.c:898
asn1integer_to_num
VALUE asn1integer_to_num(ASN1_INTEGER *ai)
Definition: ossl_asn1.c:105
ossl_x509_alloc
static VALUE ossl_x509_alloc(VALUE klass)
Definition: ossl_x509cert.c:117
ossl_x509_set_issuer
static VALUE ossl_x509_set_issuer(VALUE self, VALUE issuer)
Definition: ossl_x509cert.c:424
rb_define_module
VALUE rb_define_module(const char *name)
Definition: class.c:617
ossl_x509_to_pem
static VALUE ossl_x509_to_pem(VALUE self)
Definition: ossl_x509cert.c:210
NULL
#define NULL
Definition: _sdbm.c:103
Init_ossl_x509cert
void Init_ossl_x509cert(void)
Definition: ossl_x509cert.c:731
time
VALUE time
Definition: tcltklib.c:1866
name
const char * name
Definition: nkf.c:208
rb_define_method
void rb_define_method(VALUE klass, const char *name, VALUE(*func)(ANYARGS), int argc)
Definition: class.c:1344
WrapX509
#define WrapX509(klass, obj, x509)
Definition: ossl_x509cert.c:13
rb_inspect
VALUE rb_inspect(VALUE)
Definition: object.c:402
ossl_x509_get_not_after
static VALUE ossl_x509_get_not_after(VALUE self)
Definition: ossl_x509cert.c:478
num_to_asn1integer
ASN1_INTEGER * num_to_asn1integer(VALUE obj, ASN1_INTEGER *ai)
Definition: ossl_asn1.c:157
len
size_t len
Definition: tcltklib.c:3568