summaryrefslogtreecommitdiff
path: root/src/components/security_manager/src/ssl_context_impl.cc
blob: c5f8bed633b0544e8da92511f34d2be9b7b5e492 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
/*
 * Copyright (c) 2014, Ford Motor Company
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice, this
 * list of conditions and the following disclaimer.
 *
 * Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following
 * disclaimer in the documentation and/or other materials provided with the
 * distribution.
 *
 * Neither the name of the Ford Motor Company nor the names of its contributors
 * may be used to endorse or promote products derived from this software
 * without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */
#include "security_manager/crypto_manager_impl.h"

#include <assert.h>
#include <memory.h>
#include <map>
#include <algorithm>
#include <vector>

#include <openssl/bio.h>
#include <openssl/ssl.h>
#include <openssl/err.h>

#include "utils/macro.h"

namespace security_manager {

CREATE_LOGGERPTR_GLOBAL(logger_, "SecurityManager")

CryptoManagerImpl::SSLContextImpl::SSLContextImpl(SSL* conn,
                                                  Mode mode,
                                                  size_t maximum_payload_size)
    : connection_(conn)
    , bioIn_(BIO_new(BIO_s_mem()))
    , bioOut_(BIO_new(BIO_s_mem()))
    , bioFilter_(NULL)
    , buffer_size_(maximum_payload_size)
    , buffer_(new uint8_t[buffer_size_])
    , is_handshake_pending_(false)
    , mode_(mode)
    , max_block_size_(0) {
  SSL_set_bio(connection_, bioIn_, bioOut_);
}

std::string CryptoManagerImpl::SSLContextImpl::LastError() const {
  if (!IsInitCompleted()) {
    return std::string("Initialization is not completed");
  }
  const char* reason = ERR_reason_error_string(ERR_get_error());
  return std::string(reason ? reason : "");
}

bool CryptoManagerImpl::SSLContextImpl::IsInitCompleted() const {
  sync_primitives::AutoLock locker(bio_locker);
  return SSL_is_init_finished(connection_);
}

SSLContext::HandshakeResult CryptoManagerImpl::SSLContextImpl::StartHandshake(
    const uint8_t** const out_data, size_t* out_data_size) {
  LOG4CXX_AUTO_TRACE(logger_);
  is_handshake_pending_ = true;
  return DoHandshakeStep(NULL, 0, out_data, out_data_size);
}

namespace {
size_t aes128_gcm_sha256_max_block_size(size_t mtu) {
  if (mtu < 29)
    return 0;
  return mtu - 29;
}
size_t rc4_md5_max_block_size(size_t mtu) {
  if (mtu < 21)
    return 0;
  return mtu - 21;
}
size_t rc4_sha_max_block_size(size_t mtu) {
  if (mtu < 25)
    return 0;
  return mtu - 25;
}
size_t seed_sha_max_block_size(size_t mtu) {
  if (mtu < 53)
    return 0;
  return ((mtu - 37) & 0xfffffff0) - 5;
}
size_t aes128_sha256_max_block_size(size_t mtu) {
  if (mtu < 69)
    return 0;
  return ((mtu - 53) & 0xfffffff0) - 1;
}
size_t des_cbc3_sha_max_block_size(size_t mtu) {
  if (mtu < 37)
    return 0;
  return ((mtu - 29) & 0xfffffff8) - 5;
}
}  // namespace

std::map<std::string, CryptoManagerImpl::SSLContextImpl::BlockSizeGetter>
CryptoManagerImpl::SSLContextImpl::create_max_block_sizes() {
  std::map<std::string, CryptoManagerImpl::SSLContextImpl::BlockSizeGetter> rc;
  rc.insert(
      std::make_pair("AES128-GCM-SHA256", aes128_gcm_sha256_max_block_size));
  rc.insert(std::make_pair("AES128-SHA256", aes128_sha256_max_block_size));
  rc.insert(std::make_pair("AES128-SHA", seed_sha_max_block_size));
  rc.insert(
      std::make_pair("AES256-GCM-SHA384", aes128_gcm_sha256_max_block_size));
  rc.insert(std::make_pair("AES256-SHA256", aes128_sha256_max_block_size));
  rc.insert(std::make_pair("AES256-SHA", seed_sha_max_block_size));
  rc.insert(std::make_pair("CAMELLIA128-SHA", seed_sha_max_block_size));
  rc.insert(std::make_pair("CAMELLIA256-SHA", seed_sha_max_block_size));
  rc.insert(std::make_pair("DES-CBC3-SHA", des_cbc3_sha_max_block_size));
  rc.insert(std::make_pair("DES-CBC-SHA", des_cbc3_sha_max_block_size));
  rc.insert(std::make_pair("RC4-MD5", rc4_md5_max_block_size));
  rc.insert(std::make_pair("RC4-SHA", rc4_sha_max_block_size));
  rc.insert(std::make_pair("SEED-SHA", seed_sha_max_block_size));
  return rc;
}

std::map<std::string, CryptoManagerImpl::SSLContextImpl::BlockSizeGetter>
    CryptoManagerImpl::SSLContextImpl::max_block_sizes =
        CryptoManagerImpl::SSLContextImpl::create_max_block_sizes();

const std::string CryptoManagerImpl::SSLContextImpl::RemoveDisallowedInfo(
    X509_NAME* in_data) const {
  if (!in_data) {
    return std::string();
  }

  char* tmp_char_str = X509_NAME_oneline(in_data, NULL, 0);
  std::string out_str(tmp_char_str);
  OPENSSL_free(tmp_char_str);

  typedef std::vector<std::string> StringVector;
  StringVector disallowed_params;
  disallowed_params.push_back("CN");
  disallowed_params.push_back("serialNumber");

  const char str_delimiter = '/', param_delimiter = '=';
  for (StringVector::const_iterator it = disallowed_params.begin();
       it != disallowed_params.end();
       ++it) {
    const std::string search_str = str_delimiter + (*it) + param_delimiter;
    const size_t occurence_start = out_str.find(search_str);
    if (std::string::npos == occurence_start) {
      continue;
    }

    const size_t occurence_end =
        out_str.find(str_delimiter, occurence_start + 1);
    out_str.erase(occurence_start, occurence_end - occurence_start);
  }

  return out_str;
}

void CryptoManagerImpl::SSLContextImpl::PrintCertData(
    X509* cert, const std::string& cert_owner) {
  LOG4CXX_AUTO_TRACE(logger_);
  if (!cert) {
    LOG4CXX_DEBUG(logger_, "Empty certificate data");
    return;
  }

  std::string subj = RemoveDisallowedInfo(X509_get_subject_name(cert));
  if (!subj.empty()) {
    std::replace(subj.begin(), subj.end(), '/', ' ');
    LOG4CXX_DEBUG(logger_, cert_owner << " subject:" << subj);
  }

  std::string issuer = RemoveDisallowedInfo(X509_get_issuer_name(cert));
  if (!issuer.empty()) {
    std::replace(issuer.begin(), issuer.end(), '/', ' ');
    LOG4CXX_DEBUG(logger_, cert_owner << " issuer:" << issuer);
  }

  ASN1_TIME* not_before = X509_get_notBefore(cert);
  if (not_before) {
    LOG4CXX_DEBUG(
        logger_,
        "Start date: " << static_cast<unsigned char*>(not_before->data));
  }

  ASN1_TIME* not_after = X509_get_notAfter(cert);
  if (not_after) {
    LOG4CXX_DEBUG(logger_,
                  "End date: " << static_cast<unsigned char*>(not_after->data));
  }
}

void CryptoManagerImpl::SSLContextImpl::PrintCertInfo() {
  LOG4CXX_AUTO_TRACE(logger_);
  PrintCertData(SSL_get_certificate(connection_), "HU's");

  STACK_OF(X509)* peer_certs = SSL_get_peer_cert_chain(connection_);
  while (sk_X509_num(peer_certs) > 0) {
    X509* cert = sk_X509_pop(peer_certs);
    PrintCertData(cert, "SERVERS");
  }
}

SSLContext::HandshakeResult
CryptoManagerImpl::SSLContextImpl::CheckCertContext() {
  LOG4CXX_AUTO_TRACE(logger_);
  X509* cert = SSL_get_peer_certificate(connection_);
  if (!cert) {
    // According to the openssl documentation the peer certificate
    // might be ommitted for the SERVER but required for the cient.
    return CLIENT == mode_ ? Handshake_Result_Fail : Handshake_Result_Success;
  }
  ASN1_TIME* notBefore = X509_get_notBefore(cert);
  ASN1_TIME* notAfter = X509_get_notAfter(cert);

  time_t start = convert_asn1_time_to_time_t(notBefore);
  time_t end = convert_asn1_time_to_time_t(notAfter);

  const double start_seconds = difftime(hsh_context_.system_time, start);
  const double end_seconds = difftime(end, hsh_context_.system_time);

  if (start_seconds < 0) {
    LOG4CXX_ERROR(logger_,
                  "Certificate is not yet valid. Time before validity "
                      << start_seconds << " seconds");
    return Handshake_Result_NotYetValid;
  } else {
    LOG4CXX_DEBUG(logger_,
                  "Time since certificate validity " << start_seconds
                                                     << "seconds");
  }

  if (end_seconds < 0) {
    LOG4CXX_ERROR(logger_,
                  "Certificate already expired. Time after expiration "
                      << end_seconds << " seconds");
    return Handshake_Result_CertExpired;
  } else {
    LOG4CXX_DEBUG(logger_,
                  "Time until expiration " << end_seconds << "seconds");
  }

  X509_NAME* subj_name = X509_get_subject_name(cert);

  const std::string& cn = GetTextBy(subj_name, NID_commonName);
  const std::string& sn = GetTextBy(subj_name, NID_serialNumber);

  if (!(hsh_context_.expected_cn.CompareIgnoreCase(cn.c_str()))) {
    LOG4CXX_ERROR(logger_,
                  "Trying to run handshake with wrong app name: "
                      << cn << ". Expected app name: "
                      << hsh_context_.expected_cn.AsMBString());
    return Handshake_Result_AppNameMismatch;
  }

  if (!(hsh_context_.expected_sn.CompareIgnoreCase(sn.c_str()))) {
    LOG4CXX_ERROR(logger_,
                  "Trying to run handshake with wrong app id: "
                      << sn << ". Expected app id: "
                      << hsh_context_.expected_sn.AsMBString());
    return Handshake_Result_AppIDMismatch;
  }
  return Handshake_Result_Success;
}

time_t CryptoManagerImpl::SSLContextImpl::convert_asn1_time_to_time_t(
    ASN1_TIME* time_to_convert) const {
  struct tm cert_time;
  memset(&cert_time, 0, sizeof(struct tm));
  // the minimum value for day of month is 1, otherwise exception will be thrown
  cert_time.tm_mday = 1;
  char* buf = reinterpret_cast<char*>(time_to_convert->data);
  int index = 0;
  const int year = get_number_from_char_buf(buf, &index);
  if (V_ASN1_GENERALIZEDTIME == time_to_convert->type) {
    cert_time.tm_year =
        (year * 100 - 1900) + get_number_from_char_buf(buf, &index);
  } else {
    cert_time.tm_year = year < 50 ? year + 100 : year;
  }

  const int mon = get_number_from_char_buf(buf, &index);
  const int day = get_number_from_char_buf(buf, &index);
  const int hour = get_number_from_char_buf(buf, &index);
  const int mn = get_number_from_char_buf(buf, &index);

  cert_time.tm_mon = mon - 1;
  cert_time.tm_mday = day;
  cert_time.tm_hour = hour;
  cert_time.tm_min = mn;

  if (buf[index] == 'Z') {
    cert_time.tm_sec = 0;
  }
  if ((buf[index] == '+') || (buf[index] == '-')) {
    const int mn = get_number_from_char_buf(buf, &index);
    const int mn1 = get_number_from_char_buf(buf, &index);
    cert_time.tm_sec = (mn * 3600) + (mn1 * 60);
  } else {
    const int sec = get_number_from_char_buf(buf, &index);
    cert_time.tm_sec = sec;
  }

  return mktime(&cert_time);
}

bool CryptoManagerImpl::SSLContextImpl::ReadHandshakeData(
    const uint8_t** const out_data, size_t* out_data_size) {
  LOG4CXX_AUTO_TRACE(logger_);
  const size_t pend = BIO_ctrl_pending(bioOut_);
  LOG4CXX_DEBUG(logger_, "Available " << pend << " bytes for handshake");

  if (pend > 0) {
    LOG4CXX_DEBUG(logger_, "Reading handshake data");
    EnsureBufferSizeEnough(pend);

    const int read_count = BIO_read(bioOut_, buffer_, pend);
    if (read_count == static_cast<int>(pend)) {
      *out_data_size = read_count;
      *out_data = buffer_;
    } else {
      LOG4CXX_WARN(logger_, "BIO read fail");
      is_handshake_pending_ = false;
      ResetConnection();
      return false;
    }
  }

  return true;
}

bool CryptoManagerImpl::SSLContextImpl::WriteHandshakeData(
    const uint8_t* const in_data, size_t in_data_size) {
  LOG4CXX_AUTO_TRACE(logger_);
  if (in_data && in_data_size) {
    const int ret = BIO_write(bioIn_, in_data, in_data_size);
    if (ret <= 0) {
      is_handshake_pending_ = false;
      ResetConnection();
      return false;
    }
  }
  return true;
}

SSLContext::HandshakeResult
CryptoManagerImpl::SSLContextImpl::PerformHandshake() {
  LOG4CXX_AUTO_TRACE(logger_);
  const int handshake_result = SSL_do_handshake(connection_);
  LOG4CXX_TRACE(logger_, "Handshake result: " << handshake_result);
  if (handshake_result == 1) {
    const HandshakeResult result = CheckCertContext();
    if (result != Handshake_Result_Success) {
      ResetConnection();
      is_handshake_pending_ = false;
      return result;
    }

    LOG4CXX_DEBUG(logger_, "SSL handshake successfully finished");
    // Handshake is successful
    bioFilter_ = BIO_new(BIO_f_ssl());
    BIO_set_ssl(bioFilter_, connection_, BIO_NOCLOSE);

    const SSL_CIPHER* cipher = SSL_get_current_cipher(connection_);
    max_block_size_ = max_block_sizes[SSL_CIPHER_get_name(cipher)];
    is_handshake_pending_ = false;

  } else if (handshake_result == 0) {
    LOG4CXX_DEBUG(logger_, "SSL handshake failed");
    SSL_clear(connection_);
    is_handshake_pending_ = false;
    return Handshake_Result_Fail;
  } else {
    const int error = SSL_get_error(connection_, handshake_result);
    if (error != SSL_ERROR_WANT_READ) {
      const long error = SSL_get_verify_result(connection_);
      SetHandshakeError(error);
      LOG4CXX_WARN(logger_,
                   "Handshake failed with error "
                       << " -> " << SSL_get_error(connection_, error) << " \""
                       << LastError() << '"');
      ResetConnection();
      is_handshake_pending_ = false;

      // In case error happened but ssl verification shows OK
      // method will return AbnormalFail.
      if (X509_V_OK == error) {
        return Handshake_Result_AbnormalFail;
      }
      return openssl_error_convert_to_internal(error);
    }
  }
  return Handshake_Result_Success;
}

SSLContext::HandshakeResult CryptoManagerImpl::SSLContextImpl::DoHandshakeStep(
    const uint8_t* const in_data,
    size_t in_data_size,
    const uint8_t** const out_data,
    size_t* out_data_size) {
  LOG4CXX_AUTO_TRACE(logger_);
  DCHECK(out_data);
  DCHECK(out_data_size);
  *out_data = NULL;
  *out_data_size = 0;

  // TODO(Ezamakhov): add test - hanshake fail -> restart StartHandshake
  {
    sync_primitives::AutoLock locker(bio_locker);

    if (SSL_is_init_finished(connection_)) {
      LOG4CXX_DEBUG(logger_, "SSL initilization is finished");
      is_handshake_pending_ = false;
      return Handshake_Result_Success;
    }
  }

  if (!WriteHandshakeData(in_data, in_data_size)) {
    return Handshake_Result_AbnormalFail;
  }

  PrintCertInfo();

  const HandshakeResult res = PerformHandshake();
  if (res != Handshake_Result_Success) {
    return res;
  }

  if (!ReadHandshakeData(out_data, out_data_size)) {
    return Handshake_Result_AbnormalFail;
  }

  return res;
}

bool CryptoManagerImpl::SSLContextImpl::Encrypt(const uint8_t* const in_data,
                                                size_t in_data_size,
                                                const uint8_t** const out_data,
                                                size_t* out_data_size) {
  sync_primitives::AutoLock locker(bio_locker);
  if (!SSL_is_init_finished(connection_) || !in_data || !in_data_size) {
    return false;
  }

  BIO_write(bioFilter_, in_data, in_data_size);
  const size_t len = BIO_ctrl_pending(bioOut_);

  EnsureBufferSizeEnough(len);
  const int read_size = BIO_read(bioOut_, buffer_, len);
  DCHECK(len == static_cast<size_t>(read_size));
  if (read_size <= 0) {
    // Reset filter and connection deinitilization instead
    BIO_ctrl(bioFilter_, BIO_CTRL_RESET, 0, NULL);
    return false;
  }
  *out_data_size = read_size;
  *out_data = buffer_;

  return true;
}

bool CryptoManagerImpl::SSLContextImpl::Decrypt(const uint8_t* const in_data,
                                                size_t in_data_size,
                                                const uint8_t** const out_data,
                                                size_t* out_data_size) {
  LOG4CXX_AUTO_TRACE(logger_);
  sync_primitives::AutoLock locker(bio_locker);
  if (!SSL_is_init_finished(connection_)) {
    LOG4CXX_ERROR(logger_, "SSL initilization is not finished");
    return false;
  }

  if (!in_data || (0 == in_data_size)) {
    LOG4CXX_ERROR(logger_, "IN data ptr or IN data size is 0");
    return false;
  }

  BIO_write(bioIn_, in_data, in_data_size);
  int len = BIO_ctrl_pending(bioFilter_);

  ptrdiff_t offset = 0;

  *out_data_size = 0;
  *out_data = NULL;
  while (len > 0) {
    EnsureBufferSizeEnough(len + offset);
    len = BIO_read(bioFilter_, buffer_ + offset, len);
    // TODO(EZamakhov): investigate BIO_read return 0, -1 and -2 meanings
    if (len <= 0) {
      // Reset filter and connection deinitilization instead
      LOG4CXX_ERROR(logger_, "Read error occured. Read data lenght : " << len);
      BIO_ctrl(bioFilter_, BIO_CTRL_RESET, 0, NULL);
      return false;
    }
    *out_data_size += len;
    offset += len;
    len = BIO_ctrl_pending(bioFilter_);
  }
  *out_data = buffer_;
  return true;
}

size_t CryptoManagerImpl::SSLContextImpl::get_max_block_size(size_t mtu) const {
  if (!max_block_size_) {
    // FIXME(EZamakhov): add correct logics for TLS1/1.2/SSL3
    // For SSL3.0 set temporary value 90, old TLS1.2 value is 29
    assert(mtu > 90);
    return mtu - 90;
  }
  return max_block_size_(mtu);
}

bool CryptoManagerImpl::SSLContextImpl::IsHandshakePending() const {
  return is_handshake_pending_;
}

bool CryptoManagerImpl::SSLContextImpl::GetCertificateDueDate(
    time_t& due_date) const {
  LOG4CXX_AUTO_TRACE(logger_);

  X509* cert = SSL_get_certificate(connection_);
  if (!cert) {
    LOG4CXX_DEBUG(logger_, "Get certificate failed.");
    return false;
  }

  due_date = convert_asn1_time_to_time_t(X509_get_notAfter(cert));

  return true;
}

bool CryptoManagerImpl::SSLContextImpl::HasCertificate() const {
  return SSL_get_certificate(connection_) != NULL;
}

CryptoManagerImpl::SSLContextImpl::~SSLContextImpl() {
  SSL_shutdown(connection_);
  SSL_free(connection_);
  delete[] buffer_;
}

void CryptoManagerImpl::SSLContextImpl::SetHandshakeError(const int error) {
  const char* error_str = X509_verify_cert_error_string(error);
  if (error_str) {
    last_error_ = error_str;
  } else {
    // Error will be updated with the next LastError call
    last_error_.clear();
  }
}

void CryptoManagerImpl::SSLContextImpl::ResetConnection() {
  LOG4CXX_AUTO_TRACE(logger_);
  const int shutdown_result = SSL_shutdown(connection_);
  if (shutdown_result != 1) {
    const size_t pend = BIO_ctrl_pending(bioOut_);
    LOG4CXX_DEBUG(logger_, "Available " << pend << " bytes for shutdown");
    if (pend > 0) {
      LOG4CXX_DEBUG(logger_, "Reading shutdown data");
      EnsureBufferSizeEnough(pend);
      BIO_read(bioOut_, buffer_, pend);
    }
    SSL_shutdown(connection_);
  }
  LOG4CXX_DEBUG(logger_, "SSL connection recreation");
  SSL_CTX* ssl_context = connection_->ctx;
  SSL_free(connection_);
  connection_ = SSL_new(ssl_context);
  if (mode_ == SERVER) {
    SSL_set_accept_state(connection_);
  } else {
    SSL_set_connect_state(connection_);
  }
  bioIn_ = BIO_new(BIO_s_mem());
  bioOut_ = BIO_new(BIO_s_mem());
  SSL_set_bio(connection_, bioIn_, bioOut_);
}

void CryptoManagerImpl::SSLContextImpl::SetHandshakeContext(
    const SSLContext::HandshakeContext& hsh_ctx) {
  hsh_context_ = hsh_ctx;
}

void CryptoManagerImpl::SSLContextImpl::EnsureBufferSizeEnough(size_t size) {
  if (buffer_size_ < size) {
    delete[] buffer_;
    buffer_ = new (std::nothrow) uint8_t[size];
    if (buffer_) {
      buffer_size_ = size;
    }
  }
}

SSLContext::HandshakeResult
CryptoManagerImpl::SSLContextImpl::openssl_error_convert_to_internal(
    const long error) {
  switch (error) {
    case X509_V_ERR_CERT_HAS_EXPIRED:
      return Handshake_Result_CertExpired;
    case X509_V_ERR_CERT_NOT_YET_VALID:
      return Handshake_Result_NotYetValid;
    case X509_V_ERR_SUBJECT_ISSUER_MISMATCH:
    case X509_V_ERR_CERT_SIGNATURE_FAILURE:
    case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT:
    case X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT:
      return Handshake_Result_CertNotSigned;
    default:
      return Handshake_Result_Fail;
  }
}

std::string CryptoManagerImpl::SSLContextImpl::GetTextBy(X509_NAME* name,
                                                         int object) const {
  const int req_len = X509_NAME_get_text_by_NID(name, object, NULL, 0);

  if (-1 == req_len) {
    LOG4CXX_WARN(logger_,
                 "Unable to obtain object: " << object << " from certificate");
    return std::string();
  }

  std::vector<char> data;
  data.resize(req_len + 1);
  X509_NAME_get_text_by_NID(name, object, &data.front(), data.size());

  std::string str(data.begin(), data.end() - 1);

  std::transform(str.begin(), str.end(), str.begin(), ::tolower);
  return str;
}

}  // namespace security_manager