summaryrefslogtreecommitdiff
path: root/src/ssl_openssl.cc
blob: 0430f22aa0838d9d557a86f231a0c64fe71805b3 (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
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
// -*- mode: c++; c-basic-offset: 2; -*-

#include "common.hh"

#define SSL oSSL

#include <openssl/bio.h>
#include <openssl/err.h>
#include <openssl/pem.h>
#include <openssl/ssl.h>
#include <openssl/rsa.h>
#include <openssl/x509.h>
#include <openssl/x509v3.h>

#undef SSL

#include <memory>
#include <string.h>

#include "buffer.hh"
#include "logger.hh"
#include "ssl.hh"

namespace {

#if OPENSSL_VERSION_NUMBER < 0x10100000

BIO_METHOD* BIO_meth_new(int type, const char* name) {
  auto bm = new BIO_METHOD;
  memset(bm, 0, sizeof(BIO_METHOD));
  bm->type = type;
  bm->name = name;
  return bm;
}

void BIO_meth_free(BIO_METHOD* bm) {
  delete bm;
}

#define BIO_meth_set_write(bm, f) bm->bwrite = f
#define BIO_meth_set_read(bm, f) bm->bread = f
#define BIO_meth_set_puts(bm, f) bm->bputs = f
#define BIO_meth_set_gets(bm, f) bm->bgets = f
#define BIO_meth_set_ctrl(bm, f) bm->ctrl = f
#define BIO_meth_set_create(bm, f) bm->create = f
#define BIO_meth_set_destroy(bm, f) bm->destroy = f

#define BIO_set_init(b, v) b->init = v
#define BIO_set_data(b, v) b->ptr = v
#define BIO_set_shutdown(b, v) b->shutdown = v
#define BIO_get_data(b) b->ptr

#endif

void check_init() {
#if OPENSSL_VERSION_NUMBER < 0x10100000
  static bool initialized;
  if (initialized) return;
  initialized = true;
  SSL_load_error_strings();
  SSL_library_init();
#endif
}

class SSLEntropyImpl : public SSLEntropy {
public:
  SSLEntropyImpl() {
  }
};

class SSLCertStoreImpl : public SSLCertStore {
public:
  SSLCertStoreImpl(std::string const& filename)
    : file_(filename) {
  }
  ~SSLCertStoreImpl() override {
  }

  std::string const& file() const {
    return file_;
  }
private:
  std::string file_;
};

class SSLKeyImpl : public SSLKey {
public:
  SSLKeyImpl(EVP_PKEY* key)
    : key_(key) {
  }
  ~SSLKeyImpl() override {
    EVP_PKEY_free(key_);
  }

  EVP_PKEY* key() const {
    return key_;
  }

private:
  EVP_PKEY* const key_;
};

class SSLCertImpl : public SSLCert {
public:
  SSLCertImpl(X509* x509)
    : x509_(x509) {
  }
  ~SSLCertImpl() override {
    X509_free(x509_);
  }

  X509* x509() const {
    return x509_;
  }

private:
  X509* const x509_;
};

void logerr(Logger* logger, const char* message) {
  bool any = false;
  while (true) {
    auto err = ERR_get_error();
    if (!err) {
      if (!any) logger->out(Logger::ERR, "%s: Unknown error", message);
      break;
    }
    any = true;
    char buffer[512];
    ERR_error_string_n(err, buffer, sizeof(buffer));
    logger->out(Logger::ERR, "%s: %s", message, buffer);
  }
}

class SSLImpl : public SSL {
public:
  SSLImpl(Logger* logger, uint16_t flags)
    : logger_(logger), flags_(flags),
      bio_meth_(BIO_meth_new(99 | BIO_TYPE_SOURCE_SINK, "SSLImpl")),
      ctx_(nullptr), ssl_(nullptr),
      bio_(nullptr), rbuf_(nullptr), wbuf_(nullptr) {
    BIO_meth_set_write(bio_meth_, bio_write);
    BIO_meth_set_read(bio_meth_, bio_read);
    BIO_meth_set_puts(bio_meth_, bio_puts);
    BIO_meth_set_gets(bio_meth_, bio_gets);
    BIO_meth_set_ctrl(bio_meth_, bio_ctrl);
    BIO_meth_set_create(bio_meth_, bio_create);
    BIO_meth_set_destroy(bio_meth_, bio_destroy);
  }

  ~SSLImpl() override {
    if (ssl_) SSL_free(ssl_);
    if (ctx_) SSL_CTX_free(ctx_);
    if (bio_) BIO_free(bio_);
    if (bio_meth_) BIO_meth_free(bio_meth_);
  }

  bool unsecure() const {
    return flags_ & UNSECURE;
  }

  TransferResult transfer(Buffer* ssl_in, Buffer* ssl_out,
                          Buffer* data_in, Buffer* data_out) override {
    bool want_read = false, want_write = false;
    rbuf_ = ssl_in;
    wbuf_ = ssl_out;
    while (!want_read || !want_write) {
      switch (state_) {
      case HANDSHAKE: {
        auto ret = handshake();
        if (ret == 1) {
          state_ = TRANSFER;
          continue;
        }
        ret = SSL_get_error(ssl_, ret);
        if (ret == SSL_ERROR_WANT_READ || ret == SSL_ERROR_WANT_WRITE) {
          return SSL::NO_ERR;
        }
        logsslerr("Handshake", ret);
        return SSL::ERR;
      }
      case CLOSED: {
        auto ret = SSL_shutdown(ssl_);
        if (ret == 1) {
          return SSL::CLOSED;
        } else if (ret == 0) {
          continue;
        }
        ret = SSL_get_error(ssl_, ret);
        if (ret == SSL_ERROR_WANT_READ || ret == SSL_ERROR_WANT_WRITE) {
          return SSL::NO_ERR;
        }
        logsslerr("Close", ret);
        return SSL::ERR;
      }
      case ERROR:
        return SSL::ERR;
      case TRANSFER: {
        size_t avail;
        auto wptr = data_out->write_ptr(&avail);
        if (avail > 0) {
          auto ret = SSL_read(ssl_, wptr, avail);
          if (ret > 0) {
            data_out->commit(ret);
          } else {
            ret = SSL_get_error(ssl_, ret);
            if (ret == SSL_ERROR_WANT_READ || ret == SSL_ERROR_WANT_WRITE) {
              want_read = true;
            } else if (ret == SSL_ERROR_ZERO_RETURN) {
              return SSL::CLOSED;
            } else {
              logsslerr("SSL_read", ret);
              return SSL::ERR;
            }
          }
        } else {
          assert(false);
          want_read = true;
        }
        auto rptr = data_in->read_ptr(&avail);
        if (avail > 0) {
          auto ret = SSL_write(ssl_, rptr, avail);
          if (ret > 0) {
            data_in->consume(ret);
          } else {
            ret = SSL_get_error(ssl_, ret);
            if (ret == SSL_ERROR_WANT_READ || ret == SSL_ERROR_WANT_WRITE) {
              want_write = true;
            } else if (ret == SSL_ERROR_ZERO_RETURN) {
              return SSL::CLOSED;
            } else {
              logsslerr("SSL_write", ret);
              return SSL::ERR;
            }
          }
        } else {
          want_write = true;
        }
        break;
      }
      }
    }
    rbuf_ = nullptr;
    wbuf_ = nullptr;
    return NO_ERR;
  }

  void close() override {
    switch (state_) {
    case CLOSED:
    case ERROR:
      return;
    default:
      state_ = CLOSED;
    }
  }

protected:
  Logger* const logger_;
  uint16_t const flags_;

  void logsslerr(std::string const& message, int err) {
    state_ = ERROR;
    switch (err) {
    case SSL_ERROR_NONE:
      logger_->out(Logger::ERR, "%s: No error (?)", message.c_str());
      return;
    case SSL_ERROR_ZERO_RETURN:
      logger_->out(Logger::ERR, "%s: Connection closed", message.c_str());
      return;
    case SSL_ERROR_WANT_READ:
    case SSL_ERROR_WANT_WRITE:
    case SSL_ERROR_WANT_CONNECT:
    case SSL_ERROR_WANT_ACCEPT:
      assert(false);
      logger_->out(Logger::ERR, "%s: Non-blocking error", message.c_str());
      return;
    case SSL_ERROR_WANT_X509_LOOKUP:
      assert(false);
      logger_->out(Logger::ERR, "%s: Unknown error", message.c_str());
      return;
    case SSL_ERROR_SYSCALL:
    case SSL_ERROR_SSL:
    default:
      logerr(logger_, message.c_str());
      return;
    }
  }

  bool setup(SSL_CTX* ctx) {
    if (!ctx) return false;
    ctx_ = ctx;
    SSL_CTX_set_mode(ctx_, SSL_MODE_ENABLE_PARTIAL_WRITE);
    ssl_ = SSL_new(ctx_);
    if (!ssl_) {
      logerr(logger_, "Unable to create SSL");
      return false;
    }
    bio_ = BIO_new(bio_meth_);
    BIO_set_data(bio_, this);
    SSL_set_bio(ssl_, bio_, bio_);
    return true;
  }

  oSSL* ssl() {
    return ssl_;
  }

  virtual int handshake() = 0;

private:
  enum State {
    HANDSHAKE,
    CLOSED,
    ERROR,
    TRANSFER
  };

  static int bio_write(BIO* bio, const char* buf, int len) {
    BIO_clear_retry_flags(bio);
    if (len <= 0) return 0;
    auto impl = reinterpret_cast<SSLImpl*>(BIO_get_data(bio));
    if (impl->wbuf_) {
      impl->wbuf_->write(buf, len);
      return len;
    }
    BIO_set_retry_write(bio);
    return -1;
  }
  static int bio_read(BIO* bio, char* buf, int len) {
    BIO_clear_retry_flags(bio);
    if (len <= 0) return 0;
    auto impl = reinterpret_cast<SSLImpl*>(BIO_get_data(bio));
    if (impl->rbuf_) {
      auto ret = impl->rbuf_->read(buf, len);
      if (ret > 0) return ret;
    }
    BIO_set_retry_read(bio);
    return -1;
  }
  static int bio_puts(BIO* bio, const char* str) {
    return bio_write(bio, str, strlen(str));
  }
  static int bio_gets(BIO* UNUSED(bio), char* UNUSED(str), int UNUSED(size)) {
    return -2;
  }
  static long bio_ctrl(BIO* UNUSED(bio), int cmd,
                       long UNUSED(num), void* UNUSED(ptr)) {
    if (cmd == BIO_CTRL_FLUSH) {
      return 1;
    }
    return 0;
  }
  static int bio_create(BIO* bio) {
    BIO_set_shutdown(bio, 0);
    BIO_set_init(bio, 1);
    BIO_set_data(bio, nullptr);
    return 1;
  }
  static int bio_destroy(BIO* bio) {
    if (!bio) return 0;
    BIO_set_init(bio, 0);
    BIO_set_data(bio, nullptr);
    return 1;
  }
  BIO_METHOD* bio_meth_;
  SSL_CTX* ctx_;
  oSSL* ssl_;
  State state_;
  BIO* bio_;
  Buffer* rbuf_;
  Buffer* wbuf_;
};

class SSLServerImpl : public SSLImpl {
public:
  SSLServerImpl(Logger* logger, uint16_t flags)
    : SSLImpl(logger, flags) {
  }

  bool setup(SSLCert* cert, SSLKey* key) {
    auto ctx = SSL_CTX_new(SSLv23_server_method());
    if (!ctx) {
      logerr(logger_, "Unable to create server context");
      return false;
    }
    if (!unsecure()) {
      SSL_CTX_set_options(ctx, SSL_OP_NO_SSLv2 | SSL_OP_NO_SSLv3);
    }
    if (cert) {
      if (SSL_CTX_use_certificate(
              ctx, static_cast<SSLCertImpl*>(cert)->x509()) != 1) {
        logerr(logger_, "Unable to set certificate");
        return false;
      }
    }
    if (key) {
      if (SSL_CTX_use_PrivateKey(
              ctx, static_cast<SSLKeyImpl*>(key)->key()) != 1) {
        logerr(logger_, "Unable to set private key");
        return false;
      }
    }
    return SSLImpl::setup(ctx);
  }

  int handshake() {
    return SSL_accept(ssl());
  }
};

class SSLClientImpl : public SSLImpl {
public:
  SSLClientImpl(Logger* logger, uint16_t flags)
    : SSLImpl(logger, flags) {
  }

  bool setup(SSLCertStore* store, std::string const& host) {
    auto ctx = SSL_CTX_new(SSLv23_client_method());
    if (!ctx) {
      logerr(logger_, "Unable to create client context");
      return false;
    }
    if (!unsecure()) {
      SSL_CTX_set_options(ctx, SSL_OP_NO_SSLv2 | SSL_OP_NO_SSLv3);
    }
    if (SSL_CTX_load_verify_locations(
            ctx, static_cast<SSLCertStoreImpl*>(store)->file().c_str(),
            nullptr) != 1) {
      logerr(logger_, "Unable to load certificate store");
      return false;
    }
    if (!SSLImpl::setup(ctx)) return false;

    // Setup peer verification
    auto param = SSL_get0_param(ssl());
    X509_VERIFY_PARAM_set_hostflags(
        param, X509_CHECK_FLAG_NO_PARTIAL_WILDCARDS);
    X509_VERIFY_PARAM_set1_host(param, host.data(), host.size());
    SSL_set_verify(ssl(), unsecure() ? SSL_VERIFY_NONE : SSL_VERIFY_PEER,
                   nullptr);
    return true;
  }

  int handshake() {
    return SSL_connect(ssl());
  }

protected:
  std::string host_;
};

}  // namespace

// static
SSLEntropy* SSLEntropy::create(Logger* UNUSED(logger)) {
  return new SSLEntropyImpl();
}

// static
SSLCertStore* SSLCertStore::create(Logger* UNUSED(logger),
                                   std::string const& bundle) {
  // TODO(the_jk): Read certificates here and not later when store is used
  return new SSLCertStoreImpl(bundle);
}

// static
bool SSLKey::generate(Logger* logger, SSLEntropy* UNUSED(entropy),
                      std::string* key) {
  check_init();

  RSA* rsa = RSA_new();
  EVP_PKEY* pk = EVP_PKEY_new();
  BIGNUM* exp = BN_new();
  BIO* bio = BIO_new(BIO_s_mem());
  bool ok = false;
  int len;

  if (BN_set_word(exp, 65537) != 1) {
    logerr(logger, "Unable to set exponent");
    goto error;
  }
  if (RSA_generate_key_ex(rsa, 4096, exp, nullptr) != 1) {
    logerr(logger, "Unable to generate key");
    goto error;
  }
  if (!EVP_PKEY_assign_RSA(pk, rsa)) {
    logerr(logger, "Unable to copy key");
    goto error;
  }
  rsa = nullptr;

  if (PEM_write_bio_PKCS8PrivateKey(bio, pk, nullptr, nullptr, 0,
                                    nullptr, nullptr) != 1) {
    logerr(logger, "Error writing key");
    goto error;
  }

  len = BIO_pending(bio);
  key->resize(len);
  BIO_read(bio, &(*key)[0], len);

  ok = true;

 error:
  BIO_free(bio);
  EVP_PKEY_free(pk);
  if (rsa) RSA_free(rsa);
  BN_free(exp);
  return ok;
}

// static
SSLKey* SSLKey::load(Logger* logger, std::string const& data) {
  check_init();

  EVP_PKEY* key = EVP_PKEY_new();
  BIO* bio = BIO_new_mem_buf(data.data(), data.size());
  SSLKey* ret = nullptr;

  if (!PEM_read_bio_PrivateKey(bio, &key, nullptr, nullptr)) {
    logerr(logger, "Error reading key");
    goto error;
  }

  ret = new SSLKeyImpl(key);
  key = nullptr;

 error:
  BIO_free(bio);
  if (key) EVP_PKEY_free(key);
  return ret;
}

// static
bool SSLCert::generate(Logger* logger, SSLEntropy* UNUSED(entropy),
                       SSLCert* issuer_cert, SSLKey* issuer_key,
                       std::string const& host, SSLKey* key,
                       std::string* cert) {
  check_init();

  X509* x509 = X509_new();
  BIO* bio = BIO_new(BIO_s_mem());
  BIGNUM* bn = BN_new();
  ASN1_INTEGER* serial = ASN1_INTEGER_new();
  X509_NAME* name;
  X509_EXTENSION* ext;
  X509V3_CTX ctx;
  EVP_PKEY* sign_key = nullptr;
  bool ok = false;
  int len;
  char const* constraints;
  std::string tmp;
  int ret;

  if (X509_set_version(x509, 2) != 1) {
    logerr(logger, "Unable to set cert version");
    goto error;
  }
  if (BN_pseudo_rand(bn, 32, 0, 0) != 1) {
    logerr(logger, "Unable to generate random serial");
    goto error;
  }
  if (!BN_to_ASN1_INTEGER(bn, serial)) {
    logerr(logger, "Unable to convert serial");
    goto error;
  }
  if (X509_set_serialNumber(x509, serial) != 1) {
    logerr(logger, "Unable to set serial");
    goto error;
  }
  if (!X509_gmtime_adj(X509_get_notBefore(x509), - (24 * 60 * 60 - 1))) {
    logerr(logger, "Unable to not before time");
    goto error;
  }
  if (!X509_gmtime_adj(X509_get_notAfter(x509), 30 * 24 * 60 * 60)) {
    logerr(logger, "Unable to not after time");
    goto error;
  }

  if (key) {
    if (X509_set_pubkey(x509, static_cast<SSLKeyImpl*>(key)->key()) != 1) {
      logerr(logger, "Unable to set public key");
      goto error;
    }
  }

  name = X509_get_subject_name(x509);
  if (X509_NAME_add_entry_by_txt(
          name, "CN", MBSTRING_ASC,
          reinterpret_cast<unsigned char const*>(host.data()), host.size(),
          -1, 0) != 1) {
    logerr(logger, "Unable to set common name");
    goto error;
  }
  X509V3_set_ctx_nodb(&ctx);
  if (issuer_cert) {
    auto issuer = static_cast<SSLCertImpl*>(issuer_cert)->x509();
    X509_set_issuer_name(x509, X509_get_subject_name(issuer));
    X509V3_set_ctx(&ctx, issuer, x509, nullptr, nullptr, 0);
  } else {
    X509_set_issuer_name(x509, name);
    X509V3_set_ctx(&ctx, x509, x509, nullptr, nullptr, 0);
  }

  if (issuer_cert) {
    constraints = "CA:FALSE";
  } else {
    constraints = "CA:TRUE,pathlen:1";
  }
  ext = X509V3_EXT_conf_nid(nullptr, &ctx, NID_basic_constraints,
                            const_cast<char*>(constraints));
  if (!ext) {
    logerr(logger, "Unable to create basic constraints extension");
    goto error;
  }
  ret = X509_add_ext(x509, ext, -1);
  X509_EXTENSION_free(ext);
  if (ret != 1) {
    logerr(logger, "Unable to add basic constraints extension");
    goto error;
  }

  if (issuer_cert) {
    tmp = "DNS:" + host;
    ext = X509V3_EXT_conf_nid(nullptr, &ctx, NID_subject_alt_name,
                              const_cast<char*>(tmp.c_str()));
    if (!ext) {
      logerr(logger, "Unable to create subject alt name extension");
      goto error;
    }
    ret = X509_add_ext(x509, ext, -1);
    X509_EXTENSION_free(ext);
    if (ret != 1) {
      logerr(logger, "Unable to add subject alt name extension");
      goto error;
    }
  }

  if (issuer_key) {
    sign_key = static_cast<SSLKeyImpl*>(issuer_key)->key();
  } else if (key) {
    sign_key = static_cast<SSLKeyImpl*>(key)->key();
  }

  if (!X509_sign(x509, sign_key, EVP_sha256())) {
    logerr(logger, "Error signing cert");
    goto error;
  }

  if (PEM_write_bio_X509(bio, x509) != 1) {
    logerr(logger, "Error writing cert");
    goto error;
  }

  len = BIO_pending(bio);
  cert->resize(len);
  BIO_read(bio, &(*cert)[0], len);

  ok = true;

 error:
  BN_free(bn);
  ASN1_INTEGER_free(serial);
  BIO_free(bio);
  X509_free(x509);
  return ok;
}

// static
SSLCert* SSLCert::load(Logger* logger, std::string const& data) {
  check_init();

  X509* x509 = X509_new();
  BIO* bio = BIO_new_mem_buf(data.data(), data.size());
  SSLCert* ret = nullptr;

  if (!PEM_read_bio_X509(bio, &x509, nullptr, nullptr)) {
    logerr(logger, "Error reading cert");
    goto error;
  }

  ret = new SSLCertImpl(x509);
  x509 = nullptr;

 error:
  BIO_free(bio);
  if (x509) X509_free(x509);
  return ret;
}

// static
const uint16_t SSL::UNSECURE = 0x01;

// static
SSL* SSL::server(Logger* logger, SSLEntropy* UNUSED(entropy),
                 SSLCert* cert, SSLKey* key,
                 uint16_t flags) {
  std::unique_ptr<SSLServerImpl> ret(new SSLServerImpl(logger, flags));
  if (!ret->setup(cert, key)) return nullptr;
  return ret.release();
}

// static
SSL* SSL::client(Logger* logger, SSLEntropy* UNUSED(entropy),
                 SSLCertStore* store, std::string const& host, uint16_t flags) {
  std::unique_ptr<SSLClientImpl> ret(new SSLClientImpl(logger, flags));
  if (!ret->setup(store, host)) return nullptr;
  return ret.release();
}