Click here to Skip to main content
13,706,244 members
Click here to Skip to main content
Add your own
alternative version

Stats

22.2K views
544 downloads
21 bookmarked
Posted 23 Jul 2016
Licenced CPOL

C++ 11 S/MIME: A Simple MIME Parser and Builder with Security Features

, 3 Sep 2018
Rate this:
Please Sign up or sign in to vote.
Easy to use simple parser with S/MIME Support. Version 2.

Introduction

This is version 2 of my simple MIME parser and builder which uses C++ 11 elements and Win32 API for quick stuff.

For S/MIME, the library now uses my AdES.

Includes a QP decoder from here.

Single Message Builder

// Single Message
MIME2::CONTENT c;

c["MIME-Version"] = "1.0";
c["Content-Type"] = "text/plain";
c.SetData("Hello");

auto str = c.SerializeToVector();

Result:

MIME-Version: 1.0
Content-Type: text/plain

Hello

Some Binary Message

MIME2::CONTENT c;
c["MIME-Version"] = "1.0";
c["Content-Type"] = "application/octet-stream";
c["Content-Transfer-Encoding"] = "base64";
c["Content-Disposition"] = "attachment; filename=\"hello.txt\"";
string out = MIME2::Char2Base64("Hello", 5);
c.SetData(out.c_str());

auto str = c.SerializeToVector();

Result:

MIME-Version: 1.0
Content-Type: application/octet-stream
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="hello.txt"

SGVsbG8=

Multipart Builder

MIME2::CONTENTBUILDER cb;
MIME2::CONTENT e1;
MIME2::CONTENT e2;
e1["Content-Type"] = "text/plain";
e1.SetData("Hello\r\n\r\n");
e2["Content-Type"] = "text/html";
e2.SetData("<b>Hello</b>");

cb.Add(e1);
cb.Add(e2);

MIME2::CONTENT cc;
cb.Build(cc, "multipart/alternative");

auto str = cc.SerializeToVector();

Result:

MIME-Version: 1.0
Content-Type: multipart/alternative; boundary="{79EAC9E2-BAF9-11CE-8C82-00AA004BA90B}"

--{79EAC9E2-BAF9-11CE-8C82-00AA004BA90B}
Content-Type: text/plain

Hello

--{79EAC9E2-BAF9-11CE-8C82-00AA004BA90B}
Content-Type: text/html

<b>Hello</b>

--{79EAC9E2-BAF9-11CE-8C82-00AA004BA90B}--

Parse Simple

string str = 
R"(MIME-Version: 1.0
Content-Type: text/plain

Hello)";

    MIME2::CONTENT c;
    if (c.Parse(str.c_str()) != MIME2::MIMEERR::OK)
        return;

    auto a1 = c.hval("Content-Type"); // a1 = "text/plain"
    auto a2 = c.GetData(); // vector<char> with the data

Parse Multiple

string str = "MIME-Version: 1.0\r\n\
Content-Type: multipart/alternative; boundary=\"{79EAC9E2-BAF9-11CE-8C82-00AA004BA90B}\"\r\n\r\n\
\r\n\
--{79EAC9E2-BAF9-11CE-8C82-00AA004BA90B}\r\n\
Content-Type: text/plain\r\n\
\r\n\
Hello\r\n\
\r\n\
--{79EAC9E2-BAF9-11CE-8C82-00AA004BA90B}\r\n\
Content-Type: text/html\r\n\
\r\n\
<b>Hello</b>\r\n\
\r\n\
--{79EAC9E2-BAF9-11CE-8C82-00AA004BA90B}--";

    MIME2::CONTENT c;
    if (c.Parse(str.c_str()) != MIME2::MIMEERR::OK)
        return;

    auto a1 = c.hval("Content-Type","boundary"); // a1 = the boundary
    if (a1.empty())
        return;

    vector<MIME2::CONTENT> Contents;
    MIMELIB::ParseMultipleContent(str.c_str(), a1.c_str(), Contents);

    // Should have 2
    vector<char> d;
    Contents[1].DecodeData(d); // Decodes from Base64 or Quoted-Printable
    // d = "<b>Hello</b>"

S/MIME

For S/MIME, the library now uses AdES. You must #define MIME_CMS to use S/MIME.

MIMEERR Encrypt(CONTENT& c, std::vector<PCCERT_CONTEXT> certs, bool BinaryOutput = false);
MIMEERR Decrypt(CONTENT& c);
MIMEERR Sign(CONTENT& co, std::vector<PCCERT_CONTEXT> certs,
      std::vector<PCCERT_CONTEXT> addcerts, const wchar_t* TimeStampServer = 0,
      bool Attached = true, bool BinaryOutput = false);
MIMEERR Verify(vector<PCCERT_CONTEXT>* Certs = 0, AdES::CLEVEL* plev = 0);

HTTP Support

vector<char> data = "HTTP 1/1 200 OK\r\n...."
c.Parse(data.data(),true); // true indicates a possible HTTP header
auto mh = c1.httphdr();    // Gets the header

Or build:

c.AddHTTPHeader("HTTP 1/1 200 OK");

Have fun with it!

History

  • 03-09-2018: Version 2, cleaner interface and S/MIME usage through AdES
  • 02-08-2016: Added binary support for all sort of operations
  • 30-07-2016: Added Timestamp support and low level functions, builder for detached signatures
  • 23-07-2016: First release

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Michael Chourdakis
Engineer
Greece Greece
I'm working in C++, PHP , Java, Windows, iOS and Android.

I 've a PhD in Digital Signal Processing and Artificial Intelligence and I specialize in Pro Audio and AI applications.

My home page: http://www.michaelchourdakis.com

You may also be interested in...

Pro

Comments and Discussions

 
GeneralMessage Closed Pin
5-Sep-18 1:13
member5-Sep-18 1:13 
GeneralMessage Closed Pin
5-Sep-18 1:12
member5-Sep-18 1:12 
QuestionWhat about UNICODE? Pin
Michael Haephrati8-Jan-18 8:35
mvpMichael Haephrati8-Jan-18 8:35 
AnswerRe: What about UNICODE? Pin
Michael Chourdakis8-Jan-18 11:40
mvpMichael Chourdakis8-Jan-18 11:40 
GeneralRe: What about UNICODE? Pin
Michael Haephrati8-Jan-18 11:49
mvpMichael Haephrati8-Jan-18 11:49 
GeneralRe: What about UNICODE? Pin
Michael Chourdakis8-Jan-18 11:52
mvpMichael Chourdakis8-Jan-18 11:52 
AnswerRe: What about UNICODE? Pin
Michael Haephrati8-Jan-18 11:58
mvpMichael Haephrati8-Jan-18 11:58 
GeneralRe: What about UNICODE? Pin
Michael Chourdakis8-Jan-18 12:19
mvpMichael Chourdakis8-Jan-18 12:19 
GeneralRe: What about UNICODE? Pin
Michael Haephrati9-Jan-18 7:45
mvpMichael Haephrati9-Jan-18 7:45 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web01-2016 | 2.8.180920.1 | Last Updated 3 Sep 2018
Article Copyright 2016 by Michael Chourdakis
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid