/
DelegatedCredentialFactory.cpp
81 lines (72 loc) · 2.75 KB
/
DelegatedCredentialFactory.cpp
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
/*
* Copyright (c) 2019-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree.
*/
#include <fizz/extensions/delegatedcred/DelegatedCredentialFactory.h>
#include <fizz/extensions/delegatedcred/PeerDelegatedCredential.h>
#include <folly/portability/OpenSSL.h>
namespace fizz {
namespace extensions {
namespace {
std::unique_ptr<PeerCert> makeCredential(
DelegatedCredential&& credential,
folly::ssl::X509UniquePtr cert) {
VLOG(4) << "Making delegated credential";
// Parse pubkey
auto pubKeyRange = credential.public_key->coalesce();
auto addr = pubKeyRange.data();
folly::ssl::EvpPkeyUniquePtr pubKey(
d2i_PUBKEY(nullptr, &addr, pubKeyRange.size()));
if (!pubKey) {
throw FizzException(
"failed to create credential pubkey",
AlertDescription::illegal_parameter);
}
switch (CertUtils::getKeyType(pubKey)) {
case KeyType::RSA:
return std::make_unique<PeerDelegatedCredentialImpl<KeyType::RSA>>(
std::move(cert), std::move(pubKey), std::move(credential));
case KeyType::P256:
return std::make_unique<PeerDelegatedCredentialImpl<KeyType::P256>>(
std::move(cert), std::move(pubKey), std::move(credential));
case KeyType::P384:
return std::make_unique<PeerDelegatedCredentialImpl<KeyType::P384>>(
std::move(cert), std::move(pubKey), std::move(credential));
case KeyType::P521:
return std::make_unique<PeerDelegatedCredentialImpl<KeyType::P521>>(
std::move(cert), std::move(pubKey), std::move(credential));
case KeyType::ED25519:
return std::make_unique<PeerDelegatedCredentialImpl<KeyType::ED25519>>(
std::move(cert), std::move(pubKey), std::move(credential));
}
throw FizzException(
"unknown cert type for delegated credential",
AlertDescription::illegal_parameter);
}
} // namespace
std::unique_ptr<PeerCert> DelegatedCredentialFactory::makePeerCertStatic(
CertificateEntry entry,
bool leaf) {
if (!leaf || entry.extensions.empty()) {
return CertUtils::makePeerCert(std::move(entry.cert_data));
}
auto parentCert = CertUtils::makePeerCert(entry.cert_data->clone());
auto parentX509 = parentCert->getX509();
auto credential = getExtension<DelegatedCredential>(entry.extensions);
// No credential, just leave as is
if (!credential) {
return parentCert;
}
// Create credential
return makeCredential(std::move(credential.value()), std::move(parentX509));
}
std::unique_ptr<PeerCert> DelegatedCredentialFactory::makePeerCert(
CertificateEntry entry,
bool leaf) const {
return makePeerCertStatic(std::move(entry), leaf);
}
} // namespace extensions
} // namespace fizz