-
Notifications
You must be signed in to change notification settings - Fork 33
/
X509.xs
1626 lines (1222 loc) · 30.4 KB
/
X509.xs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
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
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
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
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"
#include <openssl/asn1.h>
#include <openssl/objects.h>
#include <openssl/bio.h>
#include <openssl/crypto.h>
#include <openssl/err.h>
#include <openssl/evp.h>
#include <openssl/pem.h>
#include <openssl/x509.h>
#include <openssl/x509v3.h>
#include <openssl/opensslconf.h>
#if OPENSSL_VERSION_NUMBER >= 0x10100000
#include <openssl/rsa.h>
#include <openssl/dsa.h>
#include <openssl/bn.h>
#endif
#ifndef OPENSSL_NO_EC
# include <openssl/ec.h>
#endif
/* from openssl/apps/apps.h */
#define FORMAT_UNDEF 0
#define FORMAT_ASN1 1
#define FORMAT_TEXT 2
#define FORMAT_PEM 3
#define FORMAT_PKCS12 5
#define FORMAT_SMIME 6
#define FORMAT_ENGINE 7
#define FORMAT_IISSGC 8
/* fake our package name */
typedef X509* Crypt__OpenSSL__X509;
typedef X509_EXTENSION* Crypt__OpenSSL__X509__Extension;
typedef ASN1_OBJECT* Crypt__OpenSSL__X509__ObjectID;
typedef X509_NAME* Crypt__OpenSSL__X509__Name;
typedef X509_NAME_ENTRY* Crypt__OpenSSL__X509__Name_Entry;
typedef X509_CRL* Crypt__OpenSSL__X509__CRL;
/* 1.0 backwards compat */
#if OPENSSL_VERSION_NUMBER < 0x10100000
#define const_ossl11
#ifndef sk_OPENSSL_STRING_num
#define sk_OPENSSL_STRING_num sk_num
#endif
#ifndef sk_OPENSSL_STRING_value
#define sk_OPENSSL_STRING_value sk_value
#endif
static ASN1_INTEGER *X509_get0_serialNumber(const X509 *a)
{
return a->cert_info->serialNumber;
}
static void RSA_get0_key(const RSA *r,
const BIGNUM **n, const BIGNUM **e, const BIGNUM **d)
{
if (n != NULL)
*n = r->n;
if (e != NULL)
*e = r->e;
if (d != NULL)
*d = r->d;
}
static RSA *EVP_PKEY_get0_RSA(EVP_PKEY *pkey)
{
if (pkey->type != EVP_PKEY_RSA)
return NULL;
return pkey->pkey.rsa;
}
static void X509_CRL_get0_signature(const X509_CRL *crl, const ASN1_BIT_STRING **psig,
X509_ALGOR **palg)
{
if (psig != NULL)
*psig = crl->signature;
if (palg != NULL)
*palg = crl->sig_alg;
}
#if OPENSSL_VERSION_NUMBER < 0x10002000
static void X509_get0_signature(const_ossl11 ASN1_BIT_STRING **psig, const_ossl11 X509_ALGOR **palg,
const X509 *x)
{
if (psig != NULL)
*psig = x->signature;
if (palg != NULL)
*palg = x->sig_alg;
}
#endif
static void DSA_get0_pqg(const DSA *d,
const BIGNUM **p, const BIGNUM **q, const BIGNUM **g)
{
if (p != NULL)
*p = d->p;
if (q != NULL)
*q = d->q;
if (g != NULL)
*g = d->g;
}
static void DSA_get0_key(const DSA *d,
const BIGNUM **pub_key, const BIGNUM **priv_key)
{
if (pub_key != NULL)
*pub_key = d->pub_key;
if (priv_key != NULL)
*priv_key = d->priv_key;
}
static DSA *EVP_PKEY_get0_DSA(EVP_PKEY *pkey)
{
if (pkey->type != EVP_PKEY_DSA)
return NULL;
return pkey->pkey.dsa;
}
static EC_KEY *EVP_PKEY_get0_EC_KEY(EVP_PKEY *pkey)
{
if (pkey->type != EVP_PKEY_EC)
return NULL;
return pkey->pkey.ec;
}
#else
#define const_ossl11 const
#endif
/* Unicode 0xfffd */
static U8 utf8_substitute_char[3] = { 0xef, 0xbf, 0xbd };
/* stolen from OpenSSL.xs */
long bio_write_cb(struct bio_st *bm, int m, const char *ptr, int l, long x, long y) {
if (m == BIO_CB_WRITE) {
SV *sv = (SV *) BIO_get_callback_arg(bm);
sv_catpvn(sv, ptr, l);
}
if (m == BIO_CB_PUTS) {
SV *sv = (SV *) BIO_get_callback_arg(bm);
l = strlen(ptr);
sv_catpvn(sv, ptr, l);
}
return l;
}
static BIO* sv_bio_create(void) {
SV *sv = newSVpvn("", 0);
/* create an in-memory BIO abstraction and callbacks */
BIO *bio = BIO_new(BIO_s_mem());
BIO_set_callback(bio, bio_write_cb);
BIO_set_callback_arg(bio, (void *)sv);
return bio;
}
static SV* sv_bio_final(BIO *bio) {
SV* sv;
(void)BIO_flush(bio);
sv = (SV *)BIO_get_callback_arg(bio);
BIO_set_callback_arg(bio, (void *)NULL);
BIO_set_callback(bio, (void *)NULL);
BIO_free_all(bio);
if (!sv) sv = &PL_sv_undef;
return sv;
}
/* call this just before sv_bio_final if the BIO got an UTF8 encoded text and you want native perl utf-8 strings. */
static SV* sv_bio_utf8_on(BIO *bio) {
SV* sv = (SV *)BIO_get_callback_arg(bio);
/* Illegal utf-8 in the string */
if (!sv_utf8_decode(sv)) {
STRLEN len;
SV *nsv = newSVpvn("", 0);
const U8* start = (U8 *) SvPV(sv, len);
const U8* end = start + len;
const U8* cur;
while ((start < end) && !is_utf8_string_loclen(start, len, &cur, 0)) {
sv_catpvn(nsv, (const char*)start, (cur - start) + 1); /* text that was ok */
sv_catpvn(nsv, (const char*)utf8_substitute_char, 3); /* insert \x{fffd} */
start = cur + 1;
len = end - cur;
}
if (start < end) {
sv_catpvn(nsv, (const char*)start, (cur - start) - 1); /* rest of the string */
}
sv_copypv(sv, nsv);
SvREFCNT_dec(nsv);
sv_utf8_decode(sv); /* should be ok now */
}
return sv;
}
/*
static void sv_bio_error(BIO *bio) {
SV* sv = (SV *)BIO_get_callback_arg(bio);
if (sv) sv_free(sv);
BIO_free_all (bio);
}
*/
static const char *ssl_error(void) {
BIO *bio;
SV *sv;
STRLEN l;
bio = sv_bio_create();
ERR_print_errors(bio);
sv = sv_bio_final(bio);
ERR_clear_error();
return SvPV(sv, l);
}
/* Make a scalar ref to a class object */
static SV* sv_make_ref(const char* class, void* object) {
SV* rv;
rv = newSV(0);
sv_setref_pv(rv, class, (void*) object);
if (! sv_isa(rv, class) ) {
croak("Error creating reference to %s", class);
}
return rv;
}
/*
* hash of extensions from x509.
* no_name can be
* 0: index by long name,
* 1: index by oid string,
* 2: index by short name
*/
static HV* hv_exts(X509* x509, int no_name) {
X509_EXTENSION *ext;
int i, c, r;
size_t len = 128;
char* key = NULL;
SV* rv;
HV* RETVAL = newHV();
sv_2mortal((SV*)RETVAL);
c = X509_get_ext_count(x509);
if ( !(c > 0) ) {
croak("No extensions found\n");
}
for (i = 0; i < c; i++) {
r = 0;
ext = X509_get_ext(x509, i);
if (ext == NULL) croak("Extension %d unavailable\n", i);
rv = sv_make_ref("Crypt::OpenSSL::X509::Extension", (void*)ext);
if (no_name == 0 || no_name == 1) {
key = malloc(sizeof(char) * (len + 1)); /*FIXME will it leak?*/
r = OBJ_obj2txt(key, len, X509_EXTENSION_get_object(ext), no_name);
} else if (no_name == 2) {
key = (char*)OBJ_nid2sn(OBJ_obj2nid(X509_EXTENSION_get_object(ext)));
r = strlen(key);
}
if (! hv_store(RETVAL, key, r, rv, 0) ) croak("Error storing extension in hash\n");
}
return RETVAL;
}
MODULE = Crypt::OpenSSL::X509 PACKAGE = Crypt::OpenSSL::X509
PROTOTYPES: DISABLE
BOOT:
{
HV *stash = gv_stashpvn("Crypt::OpenSSL::X509", 20, TRUE);
struct { char *n; I32 v; } Crypt__OpenSSL__X509__const[] = {
{"OPENSSL_VERSION_NUMBER", OPENSSL_VERSION_NUMBER},
{"FORMAT_UNDEF", FORMAT_UNDEF},
{"FORMAT_ASN1", FORMAT_ASN1},
{"FORMAT_TEXT", FORMAT_TEXT},
{"FORMAT_PEM", FORMAT_PEM},
{"FORMAT_PKCS12", FORMAT_PKCS12},
{"FORMAT_SMIME", FORMAT_SMIME},
{"FORMAT_ENGINE", FORMAT_ENGINE},
{"FORMAT_IISSGC", FORMAT_IISSGC},
{"V_ASN1_PRINTABLESTRING", V_ASN1_PRINTABLESTRING},
{"V_ASN1_UTF8STRING", V_ASN1_UTF8STRING},
{"V_ASN1_IA5STRING", V_ASN1_IA5STRING},
{Nullch,0}};
char *name;
int i;
for (i = 0; (name = Crypt__OpenSSL__X509__const[i].n); i++) {
newCONSTSUB(stash, name, newSViv(Crypt__OpenSSL__X509__const[i].v));
}
#if OPENSSL_VERSION_NUMBER < 0x10100000
ERR_load_crypto_strings();
OPENSSL_add_all_algorithms_conf();
#endif
}
Crypt::OpenSSL::X509
new(class)
SV *class
CODE:
if ((RETVAL = X509_new()) == NULL) {
croak("X509_new");
}
if (!X509_set_version(RETVAL, 2)) {
X509_free(RETVAL);
croak ("%s - can't X509_set_version()", SvPV_nolen(class));
}
ASN1_INTEGER_set(X509_get_serialNumber(RETVAL), 0L);
OUTPUT:
RETVAL
Crypt::OpenSSL::X509
new_from_string(class, string, format = FORMAT_PEM)
SV *class
SV *string
int format
ALIAS:
new_from_file = 1
PREINIT:
BIO *bio;
STRLEN len;
char *cert;
CODE:
cert = SvPV(string, len);
if (ix == 1) {
bio = BIO_new_file(cert, "r");
} else {
bio = BIO_new_mem_buf(cert, len);
}
if (!bio) croak("%s: Failed to create BIO", SvPV_nolen(class));
/* this can come in any number of ways */
if (format == FORMAT_ASN1) {
RETVAL = (X509*)d2i_X509_bio(bio, NULL);
} else {
RETVAL = (X509*)PEM_read_bio_X509(bio, NULL, NULL, NULL);
}
BIO_free_all(bio);
if (!RETVAL) croak("%s: failed to read X509 certificate.", SvPV_nolen(class));
OUTPUT:
RETVAL
void
DESTROY(x509)
Crypt::OpenSSL::X509 x509;
PPCODE:
if (x509) X509_free(x509); x509 = 0;
# This is called via an END block in the Perl module to clean up initialization that happened in BOOT.
void
__X509_cleanup(void)
PPCODE:
#if OPENSSL_VERSION_NUMBER < 0x10100000
CRYPTO_cleanup_all_ex_data();
ERR_free_strings();
ERR_remove_state(0);
EVP_cleanup();
#endif
SV*
accessor(x509)
Crypt::OpenSSL::X509 x509;
ALIAS:
subject = 1
issuer = 2
serial = 3
hash = 4
subject_hash = 4
notBefore = 5
notAfter = 6
email = 7
version = 8
sig_alg_name = 9
key_alg_name = 10
issuer_hash = 11
PREINIT:
BIO *bio;
X509_NAME *name;
CODE:
bio = sv_bio_create();
/* this includes both subject and issuer since they are so much alike */
if (ix == 1 || ix == 2) {
if (ix == 1) {
name = X509_get_subject_name(x509);
} else {
name = X509_get_issuer_name(x509);
}
/* this is prefered over X509_NAME_oneline() */
X509_NAME_print_ex(bio, name, 0, (XN_FLAG_SEP_CPLUS_SPC | ASN1_STRFLGS_UTF8_CONVERT) & ~ASN1_STRFLGS_ESC_MSB);
/* this need not be pure ascii, try to get a native perl character string with * utf8 */
sv_bio_utf8_on(bio);
} else if (ix == 3) {
i2a_ASN1_INTEGER(bio, X509_get0_serialNumber(x509));
} else if (ix == 4) {
BIO_printf(bio, "%08lx", X509_subject_name_hash(x509));
} else if (ix == 5) {
#if OPENSSL_VERSION_NUMBER < 0x10100000
ASN1_TIME_print(bio, X509_get_notBefore(x509));
#else
ASN1_TIME_print(bio, X509_get0_notBefore(x509));
#endif
} else if (ix == 6) {
#if OPENSSL_VERSION_NUMBER < 0x10100000
ASN1_TIME_print(bio, X509_get_notAfter(x509));
#else
ASN1_TIME_print(bio, X509_get0_notAfter(x509));
#endif
} else if (ix == 7) {
int j;
STACK_OF(OPENSSL_STRING) *emlst = X509_get1_email(x509);
for (j = 0; j < sk_OPENSSL_STRING_num(emlst); j++) {
BIO_printf(bio, "%s%s", (j ? " " : ""), sk_OPENSSL_STRING_value(emlst, j));
}
X509_email_free(emlst);
} else if (ix == 8) {
BIO_printf(bio, "%02ld", X509_get_version(x509));
} else if (ix == 9) {
const_ossl11 X509_ALGOR *palg;
const_ossl11 ASN1_OBJECT *paobj;
X509_get0_signature(NULL, &palg, x509);
X509_ALGOR_get0(&paobj, NULL, NULL, palg);
i2a_ASN1_OBJECT(bio, paobj);
} else if ( ix == 10 ) {
X509_PUBKEY *pkey;
ASN1_OBJECT *ppkalg;
pkey = X509_get_X509_PUBKEY(x509);
X509_PUBKEY_get0_param(&ppkalg, NULL, NULL, NULL, pkey);
i2a_ASN1_OBJECT(bio, ppkalg);
} else if ( ix == 11 ) {
BIO_printf(bio, "%08lx", X509_issuer_name_hash(x509));
}
RETVAL = sv_bio_final(bio);
OUTPUT:
RETVAL
Crypt::OpenSSL::X509::Name
subject_name(x509)
Crypt::OpenSSL::X509 x509;
ALIAS:
subject_name = 1
issuer_name = 2
CODE:
if (ix == 1) {
RETVAL = X509_get_subject_name(x509);
} else {
RETVAL = X509_get_issuer_name(x509);
}
OUTPUT:
RETVAL
SV*
sig_print(x509)
Crypt::OpenSSL::X509 x509;
PREINIT:
BIO *bio;
unsigned char *s;
const_ossl11 ASN1_BIT_STRING *psig;
int n,i;
CODE:
X509_get0_signature(&psig, NULL, x509);
n = psig->length;
s = psig->data;
bio = sv_bio_create();
for (i=0; i<n; i++) {
BIO_printf(bio, "%02x", s[i]);
}
RETVAL = sv_bio_final(bio);
OUTPUT:
RETVAL
SV*
as_string(x509, format = FORMAT_PEM)
Crypt::OpenSSL::X509 x509;
int format;
PREINIT:
BIO *bio;
CODE:
bio = sv_bio_create();
/* get the certificate back out in a specified format. */
if (format == FORMAT_PEM) {
PEM_write_bio_X509(bio, x509);
} else if (format == FORMAT_ASN1) {
i2d_X509_bio(bio, x509);
}
RETVAL = sv_bio_final(bio);
OUTPUT:
RETVAL
SV*
bit_length(x509)
Crypt::OpenSSL::X509 x509;
PREINIT:
EVP_PKEY *pkey;
const DSA *dsa_pkey;
const RSA *rsa_pkey;
const EC_KEY *ec_pkey;
const BIGNUM *p;
const BIGNUM *n;
int length;
CODE:
pkey = X509_extract_key(x509);
if (pkey == NULL) {
EVP_PKEY_free(pkey);
croak("Public key is unavailable\n");
}
switch(EVP_PKEY_base_id(pkey)) {
case EVP_PKEY_RSA:
rsa_pkey = EVP_PKEY_get0_RSA(pkey);
RSA_get0_key(rsa_pkey, &n, NULL, NULL);
length = BN_num_bits(n);
break;
case EVP_PKEY_DSA:
dsa_pkey = EVP_PKEY_get0_DSA(pkey);
DSA_get0_pqg(dsa_pkey, &p, NULL, NULL);
length = BN_num_bits(p);
break;
#ifndef OPENSSL_NO_EC
case EVP_PKEY_EC:
{
const EC_GROUP *group;
BIGNUM* ec_order;
ec_order = BN_new();
if ( !ec_order ) {
EVP_PKEY_free(pkey);
croak("Could not malloc bignum");
}
ec_pkey = EVP_PKEY_get0_EC_KEY(pkey);
if ( (group = EC_KEY_get0_group(ec_pkey)) == NULL) {
EVP_PKEY_free(pkey);
croak("No EC group");
}
/* */
if (!EC_GROUP_get_order(group, ec_order, NULL)) {
EVP_PKEY_free(pkey);
croak("Could not get ec-group order");
}
length = BN_num_bits(ec_order);
/* */
BN_free(ec_order);
break;
}
#endif
default:
EVP_PKEY_free(pkey);
croak("Unknown public key type");
}
RETVAL = newSVuv(length);
OUTPUT:
RETVAL
const char*
curve(x509)
Crypt::OpenSSL::X509 x509;
PREINIT:
#ifndef OPENSSL_NO_EC
EVP_PKEY *pkey;
#endif
CODE:
#ifdef OPENSSL_NO_EC
if ( x509 ) {} /* fix unused variable warning. */
croak("OpenSSL without EC-support");
#else
pkey = X509_extract_key(x509);
if (pkey == NULL) {
EVP_PKEY_free(pkey);
croak("Public key is unavailable\n");
}
if ( EVP_PKEY_base_id(pkey) == EVP_PKEY_EC ) {
const EC_GROUP *group;
const EC_KEY *ec_pkey;
int nid;
ec_pkey = EVP_PKEY_get0_EC_KEY(pkey);
if ( (group = EC_KEY_get0_group(ec_pkey)) == NULL) {
EVP_PKEY_free(pkey);
croak("No EC group");
}
nid = EC_GROUP_get_curve_name(group);
if ( nid == 0 ) {
EVP_PKEY_free(pkey);
croak("invalid nid");
}
RETVAL = OBJ_nid2sn(nid);
} else {
EVP_PKEY_free(pkey);
croak("Wrong Algorithm type\n");
}
EVP_PKEY_free(pkey);
#endif
OUTPUT:
RETVAL
SV*
modulus(x509)
Crypt::OpenSSL::X509 x509;
PREINIT:
EVP_PKEY *pkey;
BIO *bio;
int pkey_id;
CODE:
pkey = X509_extract_key(x509);
bio = sv_bio_create();
if (pkey == NULL) {
BIO_free_all(bio);
EVP_PKEY_free(pkey);
croak("Modulus is unavailable\n");
}
pkey_id = EVP_PKEY_base_id(pkey);
if (pkey_id == EVP_PKEY_RSA) {
const RSA *rsa_pkey;
const BIGNUM *n;
rsa_pkey = EVP_PKEY_get0_RSA(pkey);
RSA_get0_key(rsa_pkey, &n, NULL, NULL);
BN_print(bio, n);
} else if (pkey_id == EVP_PKEY_DSA) {
const DSA *dsa_pkey;
const BIGNUM *pub_key;
dsa_pkey = EVP_PKEY_get0_DSA(pkey);
DSA_get0_key(dsa_pkey, &pub_key, NULL);
BN_print(bio, pub_key);
#ifndef OPENSSL_NO_EC
} else if ( pkey_id == EVP_PKEY_EC ) {
const EC_POINT *public_key;
const EC_GROUP *group;
const EC_KEY *ec_pkey;
BIGNUM *pub_key=NULL;
ec_pkey = EVP_PKEY_get0_EC_KEY(pkey);
if ( (group = EC_KEY_get0_group(ec_pkey)) == NULL) {
BIO_free_all(bio);
EVP_PKEY_free(pkey);
croak("No EC group");
}
public_key = EC_KEY_get0_public_key(ec_pkey);
if ((pub_key = EC_POINT_point2bn(group, public_key, EC_KEY_get_conv_form(ec_pkey), NULL, NULL)) == NULL) {
BIO_free_all(bio);
EVP_PKEY_free(pkey);
croak("EC library error");
}
BN_print(bio, pub_key);
#endif
} else {
BIO_free_all(bio);
EVP_PKEY_free(pkey);
croak("Wrong Algorithm type\n");
}
RETVAL = sv_bio_final(bio);
EVP_PKEY_free(pkey);
OUTPUT:
RETVAL
SV*
exponent(x509)
Crypt::OpenSSL::X509 x509;
PREINIT:
EVP_PKEY *pkey;
BIO *bio;
ALIAS:
pub_exponent = 1
CODE:
pkey = X509_get_pubkey(x509);
bio = sv_bio_create();
/* Silence warning */
if (ix)
if (pkey == NULL) {
BIO_free_all(bio);
EVP_PKEY_free(pkey);
croak("Exponent is unavailable\n");
}
if (EVP_PKEY_base_id(pkey) == EVP_PKEY_RSA) {
const RSA *rsa_pkey;
const BIGNUM *e;
rsa_pkey = EVP_PKEY_get0_RSA(pkey);
RSA_get0_key(rsa_pkey, NULL, &e, NULL);
BN_print(bio, e);
} else {
BIO_free_all(bio);
EVP_PKEY_free(pkey);
croak("Wrong Algorithm type -- exponent only available with RSA\n");
}
RETVAL = sv_bio_final(bio);
EVP_PKEY_free(pkey);
OUTPUT:
RETVAL
SV*
fingerprint_md5(x509)
Crypt::OpenSSL::X509 x509;
ALIAS:
fingerprint_sha1 = 1
fingerprint_sha224 = 2
fingerprint_sha256 = 3
fingerprint_sha384 = 4
fingerprint_sha512 = 5
PREINIT:
const EVP_MD *mds[] = { EVP_md5(), EVP_sha1(), EVP_sha224(), EVP_sha256(), EVP_sha384(), EVP_sha512() };
unsigned char md[EVP_MAX_MD_SIZE];
int i;
unsigned int n;
BIO *bio;
CODE:
bio = sv_bio_create();
if (!X509_digest(x509, mds[ix], md, &n)) {
BIO_free_all(bio);
croak("Digest error: %s", ssl_error());
}
BIO_printf(bio, "%02X", md[0]);
for (i = 1; i < n; i++) {
BIO_printf(bio, ":%02X", md[i]);
}
RETVAL = sv_bio_final(bio);
OUTPUT:
RETVAL
SV*
checkend(x509, checkoffset)
Crypt::OpenSSL::X509 x509;
IV checkoffset;
PREINIT:
time_t now;
CODE:
now = time(NULL);
/* given an offset in seconds, will the certificate be expired? */
#if OPENSSL_VERSION_NUMBER < 0x10100000
if (ASN1_UTCTIME_cmp_time_t(X509_get_notAfter(x509), now + (int)checkoffset) == -1) {
#else
if (ASN1_UTCTIME_cmp_time_t(X509_get0_notAfter(x509), now + (int)checkoffset) == -1) {
#endif
RETVAL = &PL_sv_yes;
} else {
RETVAL = &PL_sv_no;
}
OUTPUT:
RETVAL
SV*
pubkey(x509)
Crypt::OpenSSL::X509 x509;
PREINIT:
EVP_PKEY *pkey;
BIO *bio;
int pkey_id;
CODE:
pkey = X509_get_pubkey(x509);
bio = sv_bio_create();
if (pkey == NULL) {
BIO_free_all(bio);
EVP_PKEY_free(pkey);
croak("Public Key is unavailable\n");
}
pkey_id = EVP_PKEY_base_id(pkey);
if (pkey_id == EVP_PKEY_RSA) {
const RSA *rsa_pkey;
rsa_pkey = EVP_PKEY_get0_RSA(pkey);
PEM_write_bio_RSAPublicKey(bio, rsa_pkey);
} else if (pkey_id == EVP_PKEY_DSA) {
const DSA *dsa_pkey;
dsa_pkey = EVP_PKEY_get0_DSA(pkey);
/* In openssl v3, EVP_PKEY_get0_DSA came to return "const DSA*" type.
* However PEM_write_bio_DSA_PUBKEY still needs non-const DSA*.
* So we should force to cast dsa_pkey to "DSA*" here.
*/
PEM_write_bio_DSA_PUBKEY(bio, (DSA*)dsa_pkey);
#ifndef OPENSSL_NO_EC
} else if (pkey_id == EVP_PKEY_EC ) {
const EC_KEY *ec_pkey;
ec_pkey = EVP_PKEY_get0_EC_KEY(pkey);
/* In openssl v3, EVP_PKEY_get0_EC_KEY came to return "const EC_KEY*" type.
* However PEM_write_bio_EC_PUBKEY still needs non-const EC_KEY*.
* So we should force to cast ec_pkey to "EC_KEY*" here.
*/
PEM_write_bio_EC_PUBKEY(bio, (EC_KEY*)ec_pkey);
#endif
} else {
BIO_free_all(bio);
EVP_PKEY_free(pkey);
croak("Wrong Algorithm type\n");
}
EVP_PKEY_free(pkey);
RETVAL = sv_bio_final(bio);
OUTPUT:
RETVAL
char*
pubkey_type(x509)
Crypt::OpenSSL::X509 x509;
PREINIT:
EVP_PKEY *pkey;
int pkey_id;
CODE:
RETVAL=NULL;
pkey = X509_get_pubkey(x509);
if(!pkey)
XSRETURN_UNDEF;
pkey_id = EVP_PKEY_base_id(pkey);
if (pkey_id == EVP_PKEY_DSA) {
RETVAL="dsa";
} else if (pkey_id == EVP_PKEY_RSA) {
RETVAL="rsa";
#ifndef OPENSSL_NO_EC
} else if (pkey_id == EVP_PKEY_EC ) {
RETVAL="ec";
#endif
}
OUTPUT:
RETVAL
int
num_extensions(x509)
Crypt::OpenSSL::X509 x509;
CODE:
RETVAL = X509_get_ext_count(x509);
OUTPUT:
RETVAL
Crypt::OpenSSL::X509::Extension
extension(x509, i)
Crypt::OpenSSL::X509 x509;
int i;
PREINIT:
X509_EXTENSION *ext;
int c;
CODE:
ext = NULL;
c = X509_get_ext_count(x509);