Merging ENGINE-128 into default branch
authorKrista Grothoff <krista@pep-project.org>
Mon, 24 Oct 2016 21:33:05 +0200
changeset 13335f474567e8eb
parent 1330 0c55ede4d522
parent 1332 f4720efd0832
child 1335 31e34b81d431
Merging ENGINE-128 into default branch
     1.1 --- a/src/message_api.c	Mon Oct 24 20:48:00 2016 +0200
     1.2 +++ b/src/message_api.c	Mon Oct 24 21:33:05 2016 +0200
     1.3 @@ -2094,3 +2094,80 @@
     1.4      return status;
     1.5  }
     1.6  
     1.7 +
     1.8 +DYNAMIC_API PEP_STATUS MIME_decrypt_message(
     1.9 +    PEP_SESSION session,
    1.10 +    const char *mimetext,
    1.11 +    size_t size,
    1.12 +    char** mime_plaintext,
    1.13 +    stringlist_t **keylist,
    1.14 +    PEP_rating *rating,
    1.15 +    PEP_decrypt_flags_t *flags
    1.16 +) 
    1.17 +{
    1.18 +    PEP_STATUS status = PEP_STATUS_OK;
    1.19 +    message* tmp_msg = NULL;
    1.20 +    message* dec_msg = NULL;
    1.21 +    
    1.22 +    status = mime_decode_message(mimetext, size, &tmp_msg);
    1.23 +    if (status != PEP_STATUS_OK)
    1.24 +        goto pep_error;
    1.25 +    
    1.26 +    status = decrypt_message(session,
    1.27 +                             tmp_msg,
    1.28 +                             &dec_msg,
    1.29 +                             keylist,
    1.30 +                             rating,
    1.31 +                             flags);
    1.32 +    if (status != PEP_STATUS_OK)
    1.33 +        goto pep_error;
    1.34 +    
    1.35 +    status = mime_encode_message(dec_msg, false, mime_plaintext);
    1.36 +    
    1.37 +pep_error:
    1.38 +    free_message(tmp_msg);
    1.39 +    free_message(dec_msg);
    1.40 +    
    1.41 +    return status;
    1.42 +}
    1.43 +
    1.44 +DYNAMIC_API PEP_STATUS MIME_encrypt_message(
    1.45 +    PEP_SESSION session,
    1.46 +    const char *mimetext,
    1.47 +    size_t size,
    1.48 +    stringlist_t* extra,
    1.49 +    char** mime_ciphertext,
    1.50 +    PEP_enc_format enc_format,
    1.51 +    PEP_encrypt_flags_t flags
    1.52 +) 
    1.53 +{
    1.54 +    PEP_STATUS status = PEP_STATUS_OK;
    1.55 +    message* tmp_msg = NULL;
    1.56 +    message* enc_msg = NULL;
    1.57 +    
    1.58 +    status = mime_decode_message(mimetext, size, &tmp_msg);
    1.59 +    if (status != PEP_STATUS_OK)
    1.60 +        goto pep_error;
    1.61 +    
    1.62 +    // This isn't incoming, though... so we need to reverse the direction
    1.63 +    tmp_msg->dir = PEP_dir_outgoing;
    1.64 +    status = encrypt_message(session,
    1.65 +                             tmp_msg,
    1.66 +                             extra,
    1.67 +                             &enc_msg,
    1.68 +                             enc_format,
    1.69 +                             flags);
    1.70 +    if (status != PEP_STATUS_OK)
    1.71 +        goto pep_error;
    1.72 +        
    1.73 +    status = mime_encode_message(enc_msg, false, mime_ciphertext);
    1.74 +    
    1.75 +pep_error:
    1.76 +    free_message(tmp_msg);
    1.77 +    free_message(enc_msg);
    1.78 +
    1.79 +    return status;
    1.80 +
    1.81 +}
    1.82 +
    1.83 +
     2.1 --- a/src/message_api.h	Mon Oct 24 20:48:00 2016 +0200
     2.2 +++ b/src/message_api.h	Mon Oct 24 21:33:05 2016 +0200
     2.3 @@ -89,6 +89,40 @@
     2.4          PEP_enc_format enc_format
     2.5      );
     2.6  
     2.7 +// MIME_encrypt_message() - encrypt a MIME message, with MIME output
     2.8 +//
     2.9 +//  parameters:
    2.10 +//      session (in)            session handle
    2.11 +//      mimetext (in)           MIME encoded text to encrypt
    2.12 +//      size (in)               size of input mime text
    2.13 +//      extra (in)              extra keys for encryption
    2.14 +//      mime_ciphertext (out)   encrypted, encoded message
    2.15 +//      enc_format (in)         encrypted format
    2.16 +//      flags (in)              flags to set special encryption features
    2.17 +//
    2.18 +//  return value:
    2.19 +//      PEP_STATUS_OK           if everything worked
    2.20 +//      PEP_BUFFER_TOO_SMALL    if encoded message size is too big to handle
    2.21 +//      PEP_CANNOT_CREATE_TEMP_FILE
    2.22 +//                              if there are issues with temp files; in
    2.23 +//                              this case errno will contain the underlying
    2.24 +//                              error
    2.25 +//      PEP_OUT_OF_MEMORY       if not enough memory could be allocated
    2.26 +//
    2.27 +//  caveat:
    2.28 +//      the encrypted, encoded mime text will go to the ownership of the caller; mimetext
    2.29 +//      will remain in the ownership of the caller
    2.30 +
    2.31 +DYNAMIC_API PEP_STATUS MIME_encrypt_message(
    2.32 +    PEP_SESSION session,
    2.33 +    const char *mimetext,
    2.34 +    size_t size,
    2.35 +    stringlist_t* extra,
    2.36 +    char** mime_ciphertext,
    2.37 +    PEP_enc_format enc_format,
    2.38 +    PEP_encrypt_flags_t flags
    2.39 +);
    2.40 +
    2.41  typedef enum _PEP_rating {
    2.42      PEP_rating_undefined = 0,
    2.43      PEP_rating_cannot_decrypt,
    2.44 @@ -157,6 +191,41 @@
    2.45          PEP_decrypt_flags_t *flags
    2.46  );
    2.47  
    2.48 +// MIME_decrypt_message() - decrypt a MIME message, with MIME output
    2.49 +//
    2.50 +//  parameters:
    2.51 +//      session (in)            session handle
    2.52 +//      mimetext (in)           MIME encoded text to decrypt
    2.53 +//      size (in)               size of mime text to decode (in order to decrypt)
    2.54 +//      mime_plaintext (out)    decrypted, encoded message
    2.55 +//      keylist (out)           stringlist with keyids
    2.56 +//      rating (out)            rating for the message
    2.57 +//      flags (out)             flags to signal special decryption features
    2.58 +//
    2.59 +//  return value:
    2.60 +//      PEP_STATUS_OK           if everything worked
    2.61 +//      PEP_BUFFER_TOO_SMALL    if encoded message size is too big to handle
    2.62 +//      PEP_CANNOT_CREATE_TEMP_FILE
    2.63 +//                              if there are issues with temp files; in
    2.64 +//                              this case errno will contain the underlying
    2.65 +//                              error
    2.66 +//      PEP_OUT_OF_MEMORY       if not enough memory could be allocated
    2.67 +//
    2.68 +//  caveat:
    2.69 +//      the decrypted, encoded mime text will go to the ownership of the caller; mimetext
    2.70 +//      will remain in the ownership of the caller
    2.71 +
    2.72 +DYNAMIC_API PEP_STATUS MIME_decrypt_message(
    2.73 +    PEP_SESSION session,
    2.74 +    const char *mimetext,
    2.75 +    size_t size,
    2.76 +    char** mime_plaintext,
    2.77 +    stringlist_t **keylist,
    2.78 +    PEP_rating *rating,
    2.79 +    PEP_decrypt_flags_t *flags
    2.80 +);
    2.81 +
    2.82 +
    2.83  // own_message_private_key_details() - details on own key in own message
    2.84  //
    2.85  //  parameters:
     3.1 --- a/test/message_api_test.cc	Mon Oct 24 20:48:00 2016 +0200
     3.2 +++ b/test/message_api_test.cc	Mon Oct 24 21:33:05 2016 +0200
     3.3 @@ -129,6 +129,42 @@
     3.4      cout << "rating :" << rating2 << "\n";
     3.5      free_stringlist(keylist5);
     3.6  
     3.7 +    cout << "\nTesting MIME_encrypt_message / MIME_decrypt_message...\n\n";
     3.8 +
     3.9 +    cout << "opening alice_bob_encrypt_test_plaintext_mime.eml for reading\n";
    3.10 +    ifstream inFile4 ("test_mails/alice_bob_encrypt_test_plaintext_mime.eml");
    3.11 +    assert(inFile4.is_open());
    3.12 +    
    3.13 +    string text4;
    3.14 +    
    3.15 +    cout << "reading alice_bob_encrypt_test_plaintext_mime.eml sample\n";
    3.16 +    while (!inFile4.eof()) {
    3.17 +        static string line;
    3.18 +        getline(inFile4, line);
    3.19 +        text4 += line + "\r\n";
    3.20 +    }
    3.21 +    inFile4.close();
    3.22 +    
    3.23 +    const char* out_msg_plain = text4.c_str();
    3.24 +    char* enc_msg = NULL;
    3.25 +    char* dec_msg = NULL;
    3.26 +
    3.27 +    PEP_STATUS status7 = MIME_encrypt_message(session, text4.c_str(), text4.length(), NULL, &enc_msg, PEP_enc_PGP_MIME, 0);
    3.28 +    assert(status7 == PEP_STATUS_OK);
    3.29 +    
    3.30 +    cout << enc_msg << endl;
    3.31 +
    3.32 +    string text5 = enc_msg;
    3.33 +    
    3.34 +    PEP_decrypt_flags_t dec_flags;
    3.35 +    stringlist_t* keys_used;
    3.36 +    
    3.37 +    PEP_STATUS status8 = MIME_decrypt_message(session, text5.c_str(), text5.length(), &dec_msg, &keys_used, &rating, &dec_flags);
    3.38 +    assert(status8 == PEP_STATUS_OK);
    3.39 +    
    3.40 +    cout << dec_msg << endl;
    3.41 +    
    3.42 +    
    3.43      cout << "freeing messages…\n";
    3.44      free_message(msg4);
    3.45      free_message(msg3);
    3.46 @@ -138,6 +174,8 @@
    3.47      free_message(msg5);
    3.48      cout << "done.\n";
    3.49  
    3.50 +    free(enc_msg);
    3.51 +    free(dec_msg);
    3.52      cout << "calling release()\n";
    3.53      release(session);
    3.54      return 0;