proxygen
TicketCodec.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018-present, Facebook, Inc.
3  * All rights reserved.
4  *
5  * This source code is licensed under the BSD-style license found in the
6  * LICENSE file in the root directory of this source tree.
7  */
8 
10 
11 namespace fizz {
14  switch (storage) {
16  return "None";
17  case CertificateStorage::X509:
18  return "X509";
19  case CertificateStorage::IdentityOnly:
20  return "IdentityOnly";
21  default:
22  return "Unknown storage";
23  }
24 }
25 namespace server {
27  CertificateStorage storage,
28  const std::shared_ptr<const Cert>& cert,
29  folly::io::Appender& appender) {
30  Buf clientCertBuf = folly::IOBuf::create(0);
31  CertificateStorage selectedStorage;
32  if (!cert || storage == CertificateStorage::None) {
33  selectedStorage = CertificateStorage::None;
34  } else if (storage == CertificateStorage::X509 && cert->getX509()) {
35  selectedStorage = CertificateStorage::X509;
36  clientCertBuf = folly::ssl::OpenSSLCertUtils::derEncode(*cert->getX509());
37  } else {
38  selectedStorage = CertificateStorage::IdentityOnly;
39  clientCertBuf = folly::IOBuf::copyBuffer(cert->getIdentity());
40  }
41  fizz::detail::write(selectedStorage, appender);
42  if (selectedStorage != CertificateStorage::None) {
43  fizz::detail::writeBuf<uint16_t>(clientCertBuf, appender);
44  }
45 }
46 
47 std::shared_ptr<const Cert> readClientCertificate(folly::io::Cursor& cursor) {
48  CertificateStorage storage;
49  fizz::detail::read(storage, cursor);
50  switch (storage) {
52  return nullptr;
54  Buf clientCertBuf;
55  fizz::detail::readBuf<uint16_t>(clientCertBuf, cursor);
56  return CertUtils::makePeerCert(std::move(clientCertBuf));
57  }
59  Buf ident;
60  fizz::detail::readBuf<uint16_t>(ident, cursor);
61  return std::make_shared<const IdentityCert>(
62  ident->moveToFbString().toStdString());
63  }
64  }
65 
66  return nullptr;
67 }
68 } // namespace server
69 } // namespace fizz
folly::StringPiece toString(StateEnum state)
Definition: State.cpp:16
void write(const T &in, folly::io::Appender &appender)
Definition: Types-inl.h:112
static std::unique_ptr< IOBuf > create(std::size_t capacity)
Definition: IOBuf.cpp:229
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
std::shared_ptr< const Cert > readClientCertificate(folly::io::Cursor &cursor)
Definition: TicketCodec.cpp:47
size_t read(T &out, folly::io::Cursor &cursor)
Definition: Types-inl.h:258
Definition: Actions.h:16
static std::unique_ptr< PeerCert > makePeerCert(Buf certData)
Definition: Certificate.cpp:87
const char * string
Definition: Conv.cpp:212
std::unique_ptr< folly::IOBuf > Buf
Definition: Types.h:22
static std::unique_ptr< IOBuf > derEncode(X509 &)
void appendClientCertificate(CertificateStorage storage, const std::shared_ptr< const Cert > &cert, folly::io::Appender &appender)
Definition: TicketCodec.cpp:26
static std::unique_ptr< IOBuf > copyBuffer(const void *buf, std::size_t size, std::size_t headroom=0, std::size_t minTailroom=0)
Definition: IOBuf.h:1587