2015-09-05 2 views
0

Lorsque je déchiffre les messages signés et cryptés, je décrypte avec succès et obtenir un "MimeEntity", son type de smime est "signé- Les données".comment vérifier le MimeEntity de dncrypted, et le fomart signé en utilisant "" application/pkcs7-mime

mais le format de signature n'est pas "multipart/signed", et le format est "application/pkcs7-mime".

Je le convertis en "ApplicationPkcs7Mime", "multipart", "textpart", "messagepart", mais j'ai la valeur "null".

Mais je peux normalement ouvrir ce message dans Outlook, j'utilise Outlook pour envoyer ce message, le contenu n'est pas "mimikit" généré.

Je vais seulement convertir "message.body" à "ApplicationPkcs7Mime", mais à propos de "decrime MimeEntity" comment dois-je faire?

code:

var parser = new MimeParser(new MemoryStream(content), MimeFormat.Default); 

    MimeMessage message = parser.ParseMessage(); 



     if (message.Body is ApplicationPkcs7Mime) { 

      using (var ctx = new MySecureMimeContext()) { 

       var encrypted = message.Body as ApplicationPkcs7Mime; 

       if (encrypted != null && encrypted.SecureMimeType == SecureMimeType.EnvelopedData){ 

        ctx.Import(new MemoryStream(p12data.blob),p12data.Pwd); 

        MimeEntity decrypted = encrypted.Decrypt(ctx); 

        if (decrypted is MultipartSigned) {       
         var signed = (MultipartSigned)decrypted; 
         var protocol = signed.ContentType.Parameters["protocol"]; 
         if (ctx.Supports(protocol)){ 
          if (signed[0] is TextPart && signed[1] is ApplicationPkcs7Signature) { 
           var extracted = (TextPart)signed[0]; 
           var signatures = signed.Verify(ctx); 

           if (signatures != null && signatures.Count > 0) { 
            foreach (var signature in signatures){ 
             bool valid = signature.Verify(); 
             if (!valid){ 
              isverify = false; 
              return isverify; 
             } 
            } 
           } 
          } 
         } 
        } 
        else { 
         string signType = decrypted.ContentType.Parameters["smime-type"];     
         if (signType == "signed-data"){  
          //what can 1 do? 
          var signed = message.Body as ApplicationPkcs7Mime; 
         }       
        } 
       } 

Répondre

0

Remplacer:

else { 
    string signType = decrypted.ContentType.Parameters["smime-type"];     
    if (signType == "signed-data"){  
     //what can 1 do? 
     var signed = message.Body as ApplicationPkcs7Mime; 
    }       
} 

avec:

else if (decrypted is ApplicationPkcs7Mime) { 
    var signed = (ApplicationPkcs7Mime) decrypted; 
    if (signed.SecureMimeType == SecureMimeType.SignedData) { 
     // extract the original content and get a list of signatures 
     MimeEntity original; 

     // Note: if you are rendering the message, you'll want to render the 
     // original mime part rather than the application/pkcs7-mime part. 
     foreach (var signature in pkcs7.Verify (out original)) { 
      try { 
       bool valid = signature.Verify(); 

       // If valid is true, then it signifies that the signed content 
       // has not been modified since this particular signer signed the 
       // content. 
       // 
       // However, if it is false, then it indicates that the signed 
       // content has been modified. 
      } catch (DigitalSignatureVerifyException) { 
       // There was an error verifying the signature. 
      } 
     } 
    } 
} 

Je viens de copier/coller cet extrait de code de http://www.mimekit.net/docs/html/WorkingWithSMime.htm