Imported Upstream version 0.17
Salvatore Bonaccorso
7 years ago
0 | 0 | Revision history for Crypt::SMIME |
1 | ||
2 | 0.17 Tue Jun 21 12:24:18 JST 2016 | |
3 | - New function: setPrivateKeyPkcs12(), Patch by MIK [...] | |
4 | cpan.org: | |
5 | https://rt.cpan.org/Public/Bug/Display.html?id=115490 | |
1 | 6 | |
2 | 7 | 0.16 Mon Oct 5 10:57:44 JST 2015 |
3 | 8 | - Constants FORMAT_ASN1, FORMAT_PEM, and FORMAT_SMIME can now |
3 | 3 | "Ymirlink <tl@tripletail.jp>" |
4 | 4 | ], |
5 | 5 | "dynamic_config" : 1, |
6 | "generated_by" : "ExtUtils::MakeMaker version 6.98, CPAN::Meta::Converter version 2.150005", | |
6 | "generated_by" : "ExtUtils::MakeMaker version 7.0401, CPAN::Meta::Converter version 2.150001", | |
7 | 7 | "license" : [ |
8 | 8 | "unknown" |
9 | 9 | ], |
41 | 41 | } |
42 | 42 | }, |
43 | 43 | "release_status" : "stable", |
44 | "version" : "0.16", | |
45 | "x_serialization_backend" : "JSON::PP version 2.27203" | |
44 | "version" : "0.17" | |
46 | 45 | } |
11 | 11 | ExtUtils::MakeMaker: '0' |
12 | 12 | ExtUtils::PkgConfig: '0' |
13 | 13 | dynamic_config: 1 |
14 | generated_by: 'ExtUtils::MakeMaker version 6.98, CPAN::Meta::Converter version 2.150005' | |
14 | generated_by: 'ExtUtils::MakeMaker version 7.0401, CPAN::Meta::Converter version 2.150001' | |
15 | 15 | license: unknown |
16 | 16 | meta-spec: |
17 | 17 | url: http://module-build.sourceforge.net/META-spec-v1.4.html |
23 | 23 | - inc |
24 | 24 | requires: |
25 | 25 | XSLoader: '0' |
26 | version: '0.16' | |
27 | x_serialization_backend: 'CPAN::Meta::YAML version 0.012' | |
26 | version: '0.17' |
100 | 100 | if it fails to load the key. |
101 | 101 | J<< ja; |
102 | 102 | 対応しているフォーマットは PEM のみ。鍵の読み込みに失敗した場合はdieする。 |
103 | >> | |
104 | ||
105 | =item setPrivateKeyPkcs12() | |
106 | ||
107 | $smime->setPrivateKeyPkcs12($key, $pkcs12); | |
108 | $smime->setPrivateKeyPkcs12($key, $pkcs12, $password); | |
109 | ||
110 | Load a private key and its X.509 certificate from PKCS#12 into the instance. | |
111 | The private key will be used for signing and decryption. The method dies if | |
112 | it fails to load PKCS12. | |
113 | J<< ja; | |
114 | 秘密鍵およびその X.509 証明書を PKCS#12 から読み込んで設定する。秘密鍵は署名と復号の際に用いられる。 | |
115 | 読み込みに失敗した場合は die する。 | |
103 | 116 | >> |
104 | 117 | |
105 | 118 | =item setPublicKey() |
14 | 14 | ); |
15 | 15 | Exporter::export_ok_tags('constants'); |
16 | 16 | |
17 | our $VERSION = '0.16'; | |
17 | our $VERSION = '0.17'; | |
18 | 18 | |
19 | 19 | XSLoader::load(__PACKAGE__, $VERSION); |
20 | 20 |
81 | 81 | |
82 | 82 | The private key and certificate must be encoded in PEM format. The method dies |
83 | 83 | if it fails to load the key. |
84 | ||
85 | ||
86 | =item setPrivateKeyPkcs12() | |
87 | ||
88 | $smime->setPrivateKeyPkcs12($key, $pkcs12); | |
89 | $smime->setPrivateKeyPkcs12($key, $pkcs12, $password); | |
90 | ||
91 | Load a private key and its X.509 certificate from PKCS#12 into the instance. | |
92 | The private key will be used for signing and decryption. The method dies if | |
93 | it fails to load PKCS12. | |
84 | 94 | |
85 | 95 | |
86 | 96 | =item setPublicKey() |
2 | 2 | #if defined(HAVE_SYS_TIME_H) |
3 | 3 | # include <sys/time.h> |
4 | 4 | #endif |
5 | #include <openssl/rand.h> | |
5 | 6 | #include <openssl/crypto.h> |
6 | 7 | #include <openssl/pem.h> |
7 | 8 | #include <openssl/err.h> |
481 | 482 | SvREFCNT_inc(ST(0)); |
482 | 483 | RETVAL = ST(0); |
483 | 484 | |
485 | OUTPUT: | |
486 | RETVAL | |
487 | ||
488 | SV* | |
489 | setPrivateKeyPkcs12(Crypt_SMIME this, SV* pkcs12, char* password = "") | |
490 | PROTOTYPE: $$$;$ | |
491 | PREINIT: | |
492 | BIO *bio; | |
493 | PKCS12 *p12; | |
494 | int success = 0; | |
495 | CODE: | |
496 | if (this->priv_cert) { | |
497 | X509_free(this->priv_cert); | |
498 | this->priv_cert = NULL; | |
499 | } | |
500 | if (this->priv_key) { | |
501 | EVP_PKEY_free(this->priv_key); | |
502 | this->priv_key = NULL; | |
503 | } | |
504 | ||
505 | if (SvOK(pkcs12)) { | |
506 | if (bio = BIO_new_mem_buf(SvPV_nolen(pkcs12), SvCUR(pkcs12))) { | |
507 | if (p12 = d2i_PKCS12_bio(bio, NULL)) { | |
508 | success = PKCS12_parse(p12, password, &this->priv_key, &this->priv_cert, NULL); | |
509 | } | |
510 | BIO_free(bio); | |
511 | } | |
512 | } | |
513 | ||
514 | if (!success || this->priv_key == NULL || this->priv_cert == NULL) { | |
515 | OPENSSL_CROAK("Crypt::SMIME#setPrivateKeyPkcs12: failed"); | |
516 | } | |
517 | this->priv_key_is_tainted = SvTAINTED(ST(1)); | |
518 | this->priv_cert_is_tainted = SvTAINTED(ST(1)); | |
519 | ||
520 | SvREFCNT_inc(ST(0)); | |
521 | RETVAL = ST(0); | |
484 | 522 | OUTPUT: |
485 | 523 | RETVAL |
486 | 524 |
70 | 70 | ファイル名ではなく、鍵本体を渡す。 |
71 | 71 | |
72 | 72 | 対応しているフォーマットは PEM のみ。鍵の読み込みに失敗した場合はdieする。 |
73 | ||
74 | =item setPrivateKeyPkcs12() | |
75 | ||
76 | $smime->setPrivateKeyPkcs12($key, $pkcs12); | |
77 | $smime->setPrivateKeyPkcs12($key, $pkcs12, $password); | |
78 | ||
79 | 秘密鍵およびその X.509 証明書を PKCS#12 から読み込んで設定する。秘密鍵は署名と復号の際に用いられる。 | |
80 | 読み込みに失敗した場合は die する。 | |
73 | 81 | |
74 | 82 | =item setPublicKey() |
75 | 83 |
16 | 16 | ); |
17 | 17 | Exporter::export_ok_tags('constants'); |
18 | 18 | |
19 | our $VERSION = '0.16'; | |
19 | our $VERSION = '0.17'; | |
20 | 20 | |
21 | 21 | XSLoader::load(__PACKAGE__, $VERSION); |
22 | 22 | |
272 | 272 | if it fails to load the key. |
273 | 273 | |
274 | 274 | |
275 | =item setPrivateKeyPkcs12() | |
276 | ||
277 | $smime->setPrivateKeyPkcs12($key, $pkcs12); | |
278 | $smime->setPrivateKeyPkcs12($key, $pkcs12, $password); | |
279 | ||
280 | Load a private key and its X.509 certificate from PKCS#12 into the instance. | |
281 | The private key will be used for signing and decryption. The method dies if | |
282 | it fails to load PKCS12. | |
283 | ||
284 | ||
275 | 285 | =item setPublicKey() |
276 | 286 | |
277 | 287 | $smime->setPublicKey($crt); |
7 | 7 | use Test::Exception; |
8 | 8 | use Config; |
9 | 9 | |
10 | my (%key, %csr, %crt); | |
10 | my (%key, %csr, %crt, %p12); | |
11 | 11 | do { |
12 | 12 | my $OPENSSL = do { |
13 | 13 | if (defined(my $prefix = ExtUtils::PkgConfig->variable('openssl', 'prefix'))) { |
48 | 48 | (undef, $key{$i}) = tempfile(UNLINK => 1); |
49 | 49 | (undef, $csr{$i}) = tempfile(UNLINK => 1); |
50 | 50 | (undef, $crt{$i}) = tempfile(UNLINK => 1); |
51 | (undef, $p12{$i}) = tempfile(UNLINK => 1); | |
51 | 52 | |
52 | 53 | system(qq{$OPENSSL genrsa -out $key{$i} >$DEVNULL 2>&1}) and die $!; |
53 | 54 | system(qq{$OPENSSL req -new -key $key{$i} -out $csr{$i} -config $conf_file >$DEVNULL 2>&1}) and die $!; |
54 | 55 | system(qq{$OPENSSL x509 -in $csr{$i} -out $crt{$i} -req -signkey $key{$i} -set_serial $i >$DEVNULL 2>&1}) and die $!; |
56 | system(qq{$OPENSSL pkcs12 -export -out $p12{$i} -inkey $key{$i} -in $crt{$i} -passout pass:Secret123 >$DEVNULL 2>&1}) and die $!; | |
55 | 57 | } |
56 | 58 | }; |
57 | 59 | |
71 | 73 | return scalar <$fh>; |
72 | 74 | } |
73 | 75 | |
76 | sub p12 { | |
77 | my $i = shift; | |
78 | ||
79 | local $/; | |
80 | open my $fh, '<', $p12{$i} or die $!; | |
81 | binmode $fh; | |
82 | return scalar <$fh>; | |
83 | } | |
84 | ||
74 | 85 | my $plain = q{From: alice@example.org |
75 | 86 | To: bob@example.org |
76 | 87 | Subject: Crypt::SMIME test |
85 | 96 | $verify =~ s/\r?\n|\r/\r\n/g; |
86 | 97 | |
87 | 98 | #----------------------- |
88 | plan tests => 24; | |
99 | plan tests => 25; | |
89 | 100 | use_ok('Crypt::SMIME'); |
90 | 101 | |
91 | 102 | my $smime; |
127 | 138 | $smime->setPrivateKey(key(2), crt(2)); |
128 | 139 | ok($decrypted = $smime->decrypt($encrypted), 'decrypt (by recipient\'s key)'); |
129 | 140 | |
141 | $smime->setPrivateKeyPkcs12(p12(2), 'Secret123'); | |
142 | ok($decrypted = $smime->decrypt($encrypted), 'decrypt (by recipient\'s PKCS12 key)'); | |
143 | ||
130 | 144 | 1; |