2017-09-27 4 views
1

Je tente de créer un script Perl qui vérifie un message SMIME "manuellement". J'ai vraiment beaucoup essayé mais ça ne marche pas.Vérification d'une signature SMIME avec Perl et OpenSSL

La plupart du temps, la vérification met "Signature plus longue que la clé" ou donne simplement un faux retour.

Si je le fais avec OpenSSL à la console, cela fonctionne comme un charme.

CER=mycert 
KEY=mykey 
MSG=msg.txt 
PLA=plain.txt 

# create keys with: 
# openssl req -x509 -nodes -newkey rsa:1024 -keyout mykey -out mycert 

echo -n "test" > $PLA 

# sign 
openssl cms -sign -md sha1 -subject "test" -from "sam" -to "alice" 
    -signer $CER -inkey $KEY -in $PLA -out $MSG 

# verify 
openssl smime -verify -purpose any -in $MSG -CAfile $CER 
    -CApath /etc/ssl/certs 

>> Verification successful 

qui produit et vérifie msg.txt

To: alice 
From: sam 
Subject: test 
MIME-Version: 1.0 
Content-Type: multipart/signed; protocol="application/pkcs7-signature";  
micalg="sha1"; boundary="----654C4F221B45801BF9249BC8B2EBC320" 

This is an S/MIME signed message 

------654C4F221B45801BF9249BC8B2EBC320 
test 
------654C4F221B45801BF9249BC8B2EBC320 
Content-Type: application/pkcs7-signature; name="smime.p7s" 
Content-Transfer-Encoding: base64 
Content-Disposition: attachment; filename="smime.p7s" 

MIIEZgYJKoZIhvcNAQcCoIIEVzCCBFMCAQExCTAHBgUrDgMCGjALBgkqhkiG9w0B 
BwGgggJfMIICWzCCAcSgAwIBAgIJAJeRsnkW7iHCMA0GCSqGSIb3DQEBCwUAMEUx 

Je réduis ici.

Eep0LlT+ThDmdSWm8OIPA4f5UCI5+jWB91Nf5CqKFhuua2obZmAOqZXcX4E6VdLV 
taorGZL0OCiGuUY94QJEdoJZ7rlpkwFBBVE= 

------654C4F221B45801BF9249BC8B2EBC320-- 

Maintenant, j'essaie de le montrer avec Perl très simple. Je l'ai simplifié pour couper la gestion MIME et juste utiliser les valeurs en ligne de msg.txt.

use strict; 
use warnings; 
use Crypt::OpenSSL::X509; 
use Crypt::OpenSSL::RSA; 
use MIME::Base64; 

my $in = "msg.txt"; 
my $crt = "mycert"; 

my $cert = Crypt::OpenSSL::X509->new_from_file ($crt); 
my $pubkey = $cert->pubkey(); 
my $rsa = Crypt::OpenSSL::RSA->new_public_key ($pubkey); 

my $ct = "test"; 

my $sig = "MIIEZgYJKoZIhvcNAQcCoIIEVzCCBFMCAQExCTAHBgUrDgMCGjALBgkqhkiG9w0B 
BwGgggJfMIICWzCCAcSgAwIBAgIJAJeRsnkW7iHCMA0GCSqGSIb3DQEBCwUAMEUx 

J'ai sauté le reste. Il est équivalent à la chaîne de signature dans msg.txt

Eep0LlT+ThDmdSWm8OIPA4f5UCI5+jWB91Nf5CqKFhuua2obZmAOqZXcX4E6VdLV 
taorGZL0OCiGuUY94QJEdoJZ7rlpkwFBBVE="; 

$sig = decode_base64 ($sig); # make it binary 
if ($rsa->verify ($ct, $sig)) 
{ 
    print "\nverified!\n"; 
} 

# verify fails with "Signature longer than key" or returns false. 

Je ne sais pas pourquoi il ne fonctionne pas. J'apprécierais toute aide, merci!

+0

J'ai eu quelques grands malentendu. SMIME signe avec pkcs7. J'ai donc besoin d'extraire la signature en premier. Si c'est le cas, cela fonctionne bien. La vérification attend donc une signature et non toute la chaîne pkcs7. – chris01

Répondre