123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167 |
- /*
- * Copyright (c)2013-2020 ZeroTier, Inc.
- *
- * Use of this software is governed by the Business Source License included
- * in the LICENSE.TXT file in the project's root directory.
- *
- * Change Date: 2024-01-01
- *
- * On the date above, in accordance with the Business Source License, use
- * of this software will be governed by version 2.0 of the Apache License.
- */
- /****/
- #ifndef ZT_CAPABILITY_HPP
- #define ZT_CAPABILITY_HPP
- #include <cstdio>
- #include <cstdlib>
- #include <cstring>
- #include "Constants.hpp"
- #include "Credential.hpp"
- #include "Address.hpp"
- #include "C25519.hpp"
- #include "Utils.hpp"
- #include "Identity.hpp"
- #define ZT_VIRTUALNETWORKRULE_MARSHAL_SIZE_MAX 21
- #define ZT_CAPABILITY_MARSHAL_SIZE_MAX (8 + 8 + 4 + 1 + 2 + (ZT_VIRTUALNETWORKRULE_MARSHAL_SIZE_MAX * ZT_MAX_CAPABILITY_RULES) + 2 + (5 + 5 + 1 + 2 + ZT_SIGNATURE_BUFFER_SIZE))
- namespace ZeroTier {
- class RuntimeEnvironment;
- /**
- * A set of grouped and signed network flow rules for a specific member.
- *
- * On the sending side the sender does the following for each packet:
- *
- * (1) Evaluates its capabilities in ascending order of ID to determine
- * which capability allows it to transmit this packet.
- * (2) If it has not done so lately, it then sends this capability to the
- * receiving peer ("presents" it).
- * (3) The sender then sends the packet.
- *
- * On the receiving side the receiver evaluates the capabilities presented
- * by the sender. If any valid un-expired capability allows this packet it
- * is accepted.
- *
- * Note that this is after evaluation of network scope rules and only if
- * network scope rules do not deliver an explicit match.
- */
- class Capability : public Credential
- {
- friend class Credential;
- public:
- static constexpr ZT_CredentialType credentialType() noexcept { return ZT_CREDENTIAL_TYPE_CAPABILITY; }
- ZT_INLINE Capability() noexcept { memoryZero(this); } // NOLINT(cppcoreguidelines-pro-type-member-init,hicpp-member-init)
- /**
- * @param id Capability ID
- * @param nwid Network ID
- * @param ts Timestamp (at controller)
- * @param mccl Maximum custody chain length (1 to create non-transferable capability)
- * @param rules Network flow rules for this capability
- * @param ruleCount Number of flow rules
- */
- ZT_INLINE Capability(const uint32_t id,const uint64_t nwid,const int64_t ts,const ZT_VirtualNetworkRule *const rules,const unsigned int ruleCount) noexcept : // NOLINT(cppcoreguidelines-pro-type-member-init,hicpp-member-init)
- _nwid(nwid),
- _ts(ts),
- _id(id),
- _ruleCount((ruleCount < ZT_MAX_CAPABILITY_RULES) ? ruleCount : ZT_MAX_CAPABILITY_RULES),
- _signatureLength(0)
- {
- if (_ruleCount > 0)
- Utils::copy(_rules,rules,sizeof(ZT_VirtualNetworkRule) * _ruleCount);
- }
- /**
- * @return Rules -- see ruleCount() for size of array
- */
- ZT_INLINE const ZT_VirtualNetworkRule *rules() const noexcept { return _rules; }
- /**
- * @return Number of rules in rules()
- */
- ZT_INLINE unsigned int ruleCount() const noexcept { return _ruleCount; }
- ZT_INLINE uint32_t id() const noexcept { return _id; }
- ZT_INLINE uint64_t networkId() const noexcept { return _nwid; }
- ZT_INLINE int64_t timestamp() const noexcept { return _ts; }
- ZT_INLINE const Address &issuedTo() const noexcept { return _issuedTo; }
- ZT_INLINE const Address &signer() const noexcept { return _signedBy; }
- ZT_INLINE const uint8_t *signature() const noexcept { return _signature; }
- ZT_INLINE unsigned int signatureLength() const noexcept { return _signatureLength; }
- /**
- * Sign this capability and add signature to its chain of custody
- *
- * If this returns false, this object should be considered to be
- * in an undefined state and should be discarded. False can be returned
- * if there is no more room for signatures (max chain length reached)
- * or if the 'from' identity does not include a secret key to allow
- * it to sign anything.
- *
- * @param from Signing identity (must have secret)
- * @param to Recipient of this signature
- * @return True if signature successful and chain of custody appended
- */
- bool sign(const Identity &from,const Address &to) noexcept;
- /**
- * Verify this capability's chain of custody and signatures
- *
- * @param RR Runtime environment to provide for peer lookup, etc.
- */
- ZT_INLINE Credential::VerifyResult verify(const RuntimeEnvironment *RR,void *tPtr) const noexcept { return _verify(RR,tPtr,*this); }
- static constexpr int marshalSizeMax() noexcept { return ZT_CAPABILITY_MARSHAL_SIZE_MAX; }
- int marshal(uint8_t data[ZT_CAPABILITY_MARSHAL_SIZE_MAX],bool forSign = false) const noexcept;
- int unmarshal(const uint8_t *data,int len) noexcept;
- /**
- * Marshal a set of virtual network rules
- *
- * @param data Buffer to store rules (must be at least ruleCount * ZT_VIRTUALNETWORKRULE_MARSHAL_SIZE_MAX)
- * @param rules Network rules
- * @param ruleCount Number of rules
- * @return Number of bytes written or -1 on error
- */
- static int marshalVirtualNetworkRules(uint8_t *data,const ZT_VirtualNetworkRule *rules,unsigned int ruleCount) noexcept;
- /**
- * Unmarshal a set of virtual network rules
- *
- * @param data Rule set to unmarshal
- * @param len Length of data
- * @param rules Buffer to store rules
- * @param ruleCount Result parameter to set to the number of rules decoded
- * @param maxRuleCount Capacity of rules buffer
- * @return Number of bytes unmarshaled or -1 on error
- */
- static int unmarshalVirtualNetworkRules(const uint8_t *data,int len,ZT_VirtualNetworkRule *rules,unsigned int &ruleCount,unsigned int maxRuleCount) noexcept;
- // Provides natural sort order by ID
- ZT_INLINE bool operator<(const Capability &c) const noexcept { return (_id < c._id); }
- ZT_INLINE bool operator==(const Capability &c) const noexcept { return (memcmp(this,&c,sizeof(Capability)) == 0); }
- ZT_INLINE bool operator!=(const Capability &c) const noexcept { return (memcmp(this,&c,sizeof(Capability)) != 0); }
- private:
- uint64_t _nwid;
- int64_t _ts;
- uint32_t _id;
- unsigned int _ruleCount;
- ZT_VirtualNetworkRule _rules[ZT_MAX_CAPABILITY_RULES];
- Address _issuedTo;
- Address _signedBy;
- unsigned int _signatureLength;
- uint8_t _signature[ZT_SIGNATURE_BUFFER_SIZE];
- };
- } // namespace ZeroTier
- #endif
|