-
Notifications
You must be signed in to change notification settings - Fork 217
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Split authentication policies into public definition, private impleme…
…ntation (#3536)
- Loading branch information
1 parent
7ef8cec
commit 26b8b30
Showing
30 changed files
with
771 additions
and
626 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the Apache 2.0 License. | ||
#pragma once | ||
|
||
#include "ds/json.h" | ||
|
||
namespace crypto | ||
{ | ||
enum class MDType | ||
{ | ||
NONE = 0, | ||
SHA1, | ||
SHA256, | ||
SHA384, | ||
SHA512 | ||
}; | ||
|
||
DECLARE_JSON_ENUM( | ||
MDType, | ||
{{MDType::NONE, "NONE"}, | ||
{MDType::SHA1, "SHA1"}, | ||
{MDType::SHA256, "SHA256"}, | ||
{MDType::SHA384, "SHA384"}, | ||
{MDType::SHA512, "SHA512"}}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
include/ccf/endpoints/authentication/authentication_types.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the Apache 2.0 License. | ||
#pragma once | ||
|
||
#include "ccf/tx.h" | ||
|
||
#include <memory> | ||
#include <nlohmann/json.hpp> | ||
#include <string> | ||
|
||
namespace enclave | ||
{ | ||
class RpcContext; | ||
} | ||
|
||
namespace ccf | ||
{ | ||
struct AuthnIdentity | ||
{ | ||
virtual ~AuthnIdentity() = default; | ||
}; | ||
|
||
using OpenAPISecuritySchema = std::pair<std::string, nlohmann::json>; | ||
static const OpenAPISecuritySchema unauthenticated_schema = | ||
std::make_pair("", nlohmann::json()); | ||
|
||
class AuthnPolicy | ||
{ | ||
public: | ||
virtual ~AuthnPolicy() = default; | ||
|
||
virtual std::unique_ptr<AuthnIdentity> authenticate( | ||
kv::ReadOnlyTx& tx, | ||
const std::shared_ptr<enclave::RpcContext>& ctx, | ||
std::string& error_reason) = 0; | ||
|
||
virtual void set_unauthenticated_error( | ||
std::shared_ptr<enclave::RpcContext>& ctx, std::string&& error_reason); | ||
|
||
virtual std::optional<OpenAPISecuritySchema> get_openapi_security_schema() | ||
const = 0; | ||
}; | ||
|
||
using AuthnPolicies = std::vector<std::shared_ptr<AuthnPolicy>>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the Apache 2.0 License. | ||
#pragma once | ||
|
||
#include "ccf/endpoints/authentication/authentication_types.h" | ||
#include "ccf/entity_id.h" | ||
|
||
namespace ccf | ||
{ | ||
namespace | ||
{ | ||
std::optional<OpenAPISecuritySchema> get_cert_based_security_schema() | ||
{ | ||
// There is currently no OpenAPI-compliant way to describe cert-based TLS | ||
// auth, so this policy is not documented. This should change in | ||
// OpenAPI3.1: https://github.com/OAI/OpenAPI-Specification/pull/1764 | ||
return std::nullopt; | ||
} | ||
} | ||
|
||
struct UserCertAuthnIdentity : public AuthnIdentity | ||
{ | ||
/** CCF user ID */ | ||
UserId user_id; | ||
}; | ||
|
||
class UserCertAuthnPolicy : public AuthnPolicy | ||
{ | ||
public: | ||
static constexpr auto SECURITY_SCHEME_NAME = "user_cert"; | ||
|
||
std::unique_ptr<AuthnIdentity> authenticate( | ||
kv::ReadOnlyTx& tx, | ||
const std::shared_ptr<enclave::RpcContext>& ctx, | ||
std::string& error_reason) override; | ||
|
||
std::optional<OpenAPISecuritySchema> get_openapi_security_schema() | ||
const override | ||
{ | ||
return get_cert_based_security_schema(); | ||
} | ||
}; | ||
|
||
struct MemberCertAuthnIdentity : public AuthnIdentity | ||
{ | ||
/** CCF member ID */ | ||
MemberId member_id; | ||
}; | ||
|
||
class MemberCertAuthnPolicy : public AuthnPolicy | ||
{ | ||
public: | ||
static constexpr auto SECURITY_SCHEME_NAME = "member_cert"; | ||
|
||
std::unique_ptr<AuthnIdentity> authenticate( | ||
kv::ReadOnlyTx& tx, | ||
const std::shared_ptr<enclave::RpcContext>& ctx, | ||
std::string& error_reason) override; | ||
|
||
std::optional<OpenAPISecuritySchema> get_openapi_security_schema() | ||
const override | ||
{ | ||
return get_cert_based_security_schema(); | ||
} | ||
}; | ||
|
||
struct NodeCertAuthnIdentity : public AuthnIdentity | ||
{ | ||
ccf::NodeId node_id; | ||
}; | ||
|
||
class NodeCertAuthnPolicy : public AuthnPolicy | ||
{ | ||
public: | ||
std::unique_ptr<AuthnIdentity> authenticate( | ||
kv::ReadOnlyTx& tx, | ||
const std::shared_ptr<enclave::RpcContext>& ctx, | ||
std::string& error_reason) override; | ||
|
||
std::optional<OpenAPISecuritySchema> get_openapi_security_schema() | ||
const override | ||
{ | ||
return get_cert_based_security_schema(); | ||
} | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the Apache 2.0 License. | ||
#pragma once | ||
|
||
#include "ccf/endpoints/authentication/authentication_types.h" | ||
|
||
namespace ccf | ||
{ | ||
// To make authentication _optional_, no-auth can be listed as one of several | ||
// specified policies | ||
struct EmptyAuthnIdentity : public AuthnIdentity | ||
{}; | ||
|
||
class EmptyAuthnPolicy : public AuthnPolicy | ||
{ | ||
public: | ||
static constexpr auto SECURITY_SCHEME_NAME = "no_auth"; | ||
|
||
std::unique_ptr<AuthnIdentity> authenticate( | ||
kv::ReadOnlyTx&, | ||
const std::shared_ptr<enclave::RpcContext>&, | ||
std::string&) override; | ||
|
||
void set_unauthenticated_error( | ||
std::shared_ptr<enclave::RpcContext>&, std::string&&) override; | ||
|
||
std::optional<OpenAPISecuritySchema> get_openapi_security_schema() | ||
const override | ||
{ | ||
return unauthenticated_schema; | ||
} | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the Apache 2.0 License. | ||
#pragma once | ||
|
||
#include "ccf/endpoints/authentication/authentication_types.h" | ||
|
||
namespace ccf | ||
{ | ||
struct JwtAuthnIdentity : public AuthnIdentity | ||
{ | ||
/** JWT key issuer, as defined in @c | ||
* public:ccf.gov.jwt_public_signing_key_issuer */ | ||
std::string key_issuer; | ||
/** JWT header */ | ||
nlohmann::json header; | ||
/** JWT payload */ | ||
nlohmann::json payload; | ||
}; | ||
|
||
class JwtAuthnPolicy : public AuthnPolicy | ||
{ | ||
protected: | ||
static const OpenAPISecuritySchema security_schema; | ||
|
||
public: | ||
static constexpr auto SECURITY_SCHEME_NAME = "jwt"; | ||
|
||
std::unique_ptr<AuthnIdentity> authenticate( | ||
kv::ReadOnlyTx& tx, | ||
const std::shared_ptr<enclave::RpcContext>& ctx, | ||
std::string& error_reason) override; | ||
|
||
void set_unauthenticated_error( | ||
std::shared_ptr<enclave::RpcContext>& ctx, | ||
std::string&& error_reason) override; | ||
|
||
std::optional<OpenAPISecuritySchema> get_openapi_security_schema() | ||
const override | ||
{ | ||
return security_schema; | ||
} | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the Apache 2.0 License. | ||
#pragma once | ||
|
||
#include "ccf/endpoints/authentication/authentication_types.h" | ||
#include "ccf/entity_id.h" | ||
#include "ccf/service/signed_req.h" | ||
|
||
namespace ccf | ||
{ | ||
struct UserSignatureAuthnIdentity : public AuthnIdentity | ||
{ | ||
/** CCF user ID */ | ||
UserId user_id; | ||
/** User certificate, used to sign this request, described by keyId */ | ||
crypto::Pem user_cert; | ||
/** Canonicalised request and associated signature */ | ||
SignedReq signed_request; | ||
}; | ||
|
||
struct VerifierCache; | ||
|
||
class UserSignatureAuthnPolicy : public AuthnPolicy | ||
{ | ||
protected: | ||
static const OpenAPISecuritySchema security_schema; | ||
std::unique_ptr<VerifierCache> verifiers; | ||
|
||
public: | ||
static constexpr auto SECURITY_SCHEME_NAME = "user_signature"; | ||
|
||
UserSignatureAuthnPolicy(); | ||
~UserSignatureAuthnPolicy(); | ||
|
||
std::unique_ptr<AuthnIdentity> authenticate( | ||
kv::ReadOnlyTx& tx, | ||
const std::shared_ptr<enclave::RpcContext>& ctx, | ||
std::string& error_reason) override; | ||
|
||
void set_unauthenticated_error( | ||
std::shared_ptr<enclave::RpcContext>& ctx, | ||
std::string&& error_reason) override; | ||
|
||
std::optional<OpenAPISecuritySchema> get_openapi_security_schema() | ||
const override | ||
{ | ||
return security_schema; | ||
} | ||
}; | ||
|
||
struct MemberSignatureAuthnIdentity : public AuthnIdentity | ||
{ | ||
/** CCF member ID */ | ||
MemberId member_id; | ||
|
||
/** Member certificate, used to sign this request, described by keyId */ | ||
crypto::Pem member_cert; | ||
|
||
/** Canonicalised request and associated signature */ | ||
SignedReq signed_request; | ||
|
||
/** Digest of request */ | ||
std::vector<uint8_t> request_digest; | ||
}; | ||
|
||
class MemberSignatureAuthnPolicy : public AuthnPolicy | ||
{ | ||
protected: | ||
static const OpenAPISecuritySchema security_schema; | ||
std::unique_ptr<VerifierCache> verifiers; | ||
|
||
public: | ||
static constexpr auto SECURITY_SCHEME_NAME = "member_signature"; | ||
|
||
MemberSignatureAuthnPolicy(); | ||
~MemberSignatureAuthnPolicy(); | ||
|
||
std::unique_ptr<AuthnIdentity> authenticate( | ||
kv::ReadOnlyTx& tx, | ||
const std::shared_ptr<enclave::RpcContext>& ctx, | ||
std::string& error_reason) override; | ||
|
||
void set_unauthenticated_error( | ||
std::shared_ptr<enclave::RpcContext>& ctx, | ||
std::string&& error_reason) override; | ||
|
||
std::optional<OpenAPISecuritySchema> get_openapi_security_schema() | ||
const override | ||
{ | ||
return security_schema; | ||
} | ||
}; | ||
} |
Oops, something went wrong.