SOIL C++
C++ Unified Device Interface
Signer.cpp
Go to the documentation of this file.
1#include "Signer.h"
2#include <fstream>
3#include <sstream>
4#include <iostream>
5#include <openssl/rsa.h>
6#include <openssl/ssl.h>
7#include <openssl/err.h>
8#include <openssl/evp.h>
9#include <openssl/opensslv.h>
10
11SIGN::Signer::Signer(std::string filename)
12{
13 _name = filename;
14 std::ifstream keyfile(filename);
15 std::stringstream buffer;
16 buffer << keyfile.rdbuf();
17 std::string key = buffer.str();
18
19 BIO* bio = BIO_new(BIO_s_mem());
20 BIO_write(bio, key.c_str(), static_cast<int>(key.length()));
21
22 private_key = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL);
23
24 BIO_free(bio);
25 context = EVP_MD_CTX_create();
26 EVP_MD_CTX_set_flags(context, EVP_MD_CTX_FLAG_ONESHOT & EVP_MD_CTX_FLAG_FINALISE & EVP_MD_CTX_FLAG_REUSE);
27
28
29 // Make sure Signer gets a warm start
30 std::vector<unsigned char> dummy(256, ' ');
31 this->sign(dummy);
32
33}
34
35
37{
38 EVP_PKEY_free(private_key);
39}
40
41std::vector<unsigned char> SIGN::Signer::sign(std::vector<unsigned char> message)
42{
43 EVP_DigestSignInit(context, NULL, EVP_sha256(), NULL, private_key);
44 EVP_DigestSignUpdate(context, message.data(), message.size());
45
46 size_t length = 512;
47 EVP_DigestSignFinal(context, NULL, &length);
48 unsigned char* buffer = new unsigned char[length];
49 EVP_DigestSignFinal(context, buffer, &length);
50 //EVP_DigestSign(context, buffer, &length, message.data(), message.size());
51 std::vector<unsigned char> result;
52 result.reserve(length);
53 for (unsigned int i = 0; i < length; i++)
54 {
55 result.push_back(buffer[i]);
56 }
57 delete [] buffer;
58 return result;
59}
60
61std::string SIGN::Signer::openssl_version(void) const
62{
63 return OPENSSL_VERSION_TEXT;
64}
std::vector< unsigned char > sign(std::vector< unsigned char > digest)
Sign bytes.
Definition: Signer.cpp:41
std::string openssl_version(void) const
OpenSSL Version.
Definition: Signer.cpp:61
~Signer()
Destructor.
Definition: Signer.cpp:36
Signer(std::string filename)
Constructor.
Definition: Signer.cpp:11