| 1 | #pragma once |
| 2 | |
| 3 | #include <mls/common.h> |
| 4 | #include <mls/crypto.h> |
| 5 | |
| 6 | namespace mlspp { |
| 7 | |
| 8 | namespace hpke { |
| 9 | struct UserInfoVC; |
| 10 | } |
| 11 | |
| 12 | // struct { |
| 13 | // opaque identity<0..2^16-1>; |
| 14 | // SignaturePublicKey public_key; |
| 15 | // } BasicCredential; |
| 16 | struct BasicCredential |
| 17 | { |
| 18 | BasicCredential() {} |
| 19 | |
| 20 | BasicCredential(bytes identity_in) |
| 21 | : identity(std::move(identity_in)) |
| 22 | { |
| 23 | } |
| 24 | |
| 25 | bytes identity; |
| 26 | |
| 27 | TLS_SERIALIZABLE(identity) |
| 28 | }; |
| 29 | |
| 30 | struct X509Credential |
| 31 | { |
| 32 | struct CertData |
| 33 | { |
| 34 | bytes data; |
| 35 | |
| 36 | TLS_SERIALIZABLE(data) |
| 37 | }; |
| 38 | |
| 39 | X509Credential() = default; |
| 40 | explicit X509Credential(const std::vector<bytes>& der_chain_in); |
| 41 | |
| 42 | SignatureScheme signature_scheme() const; |
| 43 | SignaturePublicKey public_key() const; |
| 44 | bool valid_for(const SignaturePublicKey& pub) const; |
| 45 | |
| 46 | // TODO(rlb) This should be const or exposed via a method |
| 47 | std::vector<CertData> der_chain; |
| 48 | |
| 49 | private: |
| 50 | SignaturePublicKey _public_key; |
| 51 | SignatureScheme _signature_scheme; |
| 52 | }; |
| 53 | |
| 54 | tls::ostream& |
| 55 | operator<<(tls::ostream& str, const X509Credential& obj); |
| 56 | |
| 57 | tls::istream& |
| 58 | operator>>(tls::istream& str, X509Credential& obj); |
| 59 | |
| 60 | struct UserInfoVCCredential |
| 61 | { |
| 62 | UserInfoVCCredential() = default; |
| 63 | explicit UserInfoVCCredential(std::string userinfo_vc_jwt_in); |
| 64 | |
| 65 | std::string userinfo_vc_jwt; |
| 66 | |
| 67 | bool valid_for(const SignaturePublicKey& pub) const; |
| 68 | bool valid_from(const PublicJWK& pub) const; |
| 69 | |
| 70 | friend tls::ostream operator<<(tls::ostream& str, |
| 71 | const UserInfoVCCredential& obj); |
| 72 | friend tls::istream operator>>(tls::istream& str, UserInfoVCCredential& obj); |
| 73 | friend bool operator==(const UserInfoVCCredential& lhs, |
| 74 | const UserInfoVCCredential& rhs); |
| 75 | friend bool operator!=(const UserInfoVCCredential& lhs, |
| 76 | const UserInfoVCCredential& rhs); |
| 77 | |
| 78 | private: |
| 79 | std::shared_ptr<hpke::UserInfoVC> _vc; |
| 80 | }; |
| 81 | |
| 82 | bool |
| 83 | operator==(const X509Credential& lhs, const X509Credential& rhs); |
| 84 | |
| 85 | enum struct CredentialType : uint16_t |
| 86 | { |
| 87 | reserved = 0, |
| 88 | basic = 1, |
| 89 | x509 = 2, |
| 90 | |
| 91 | userinfo_vc_draft_00 = 0xFE00, |
| 92 | multi_draft_00 = 0xFF00, |
| 93 | |
| 94 | // GREASE values, included here mainly so that debugger output looks nice |
| 95 | GREASE_0 = 0x0A0A, |
| 96 | GREASE_1 = 0x1A1A, |
| 97 | GREASE_2 = 0x2A2A, |
| 98 | GREASE_3 = 0x3A3A, |
| 99 | GREASE_4 = 0x4A4A, |
| 100 | GREASE_5 = 0x5A5A, |
| 101 | GREASE_6 = 0x6A6A, |
| 102 | GREASE_7 = 0x7A7A, |
| 103 | GREASE_8 = 0x8A8A, |
| 104 | GREASE_9 = 0x9A9A, |
| 105 | GREASE_A = 0xAAAA, |
| 106 | GREASE_B = 0xBABA, |
| 107 | GREASE_C = 0xCACA, |
| 108 | GREASE_D = 0xDADA, |
| 109 | GREASE_E = 0xEAEA, |
| 110 | }; |
| 111 | |
| 112 | // struct { |
| 113 | // Credential credential; |
| 114 | // SignaturePublicKey credential_key; |
| 115 | // opaque signature<V>; |
| 116 | // } CredentialBinding |
| 117 | // |
| 118 | // struct { |
| 119 | // CredentialBinding bindings<V>; |
| 120 | // } MultiCredential; |
| 121 | struct CredentialBinding; |
| 122 | struct CredentialBindingInput; |
| 123 | |
| 124 | struct MultiCredential |
| 125 | { |
| 126 | MultiCredential() = default; |
| 127 | MultiCredential(const std::vector<CredentialBindingInput>& binding_inputs, |
| 128 | const SignaturePublicKey& signature_key); |
| 129 | |
| 130 | std::vector<CredentialBinding> bindings; |
| 131 | |
| 132 | bool valid_for(const SignaturePublicKey& pub) const; |
| 133 | |
| 134 | TLS_SERIALIZABLE(bindings) |
| 135 | }; |
| 136 | |
| 137 | // struct { |
| 138 | // CredentialType credential_type; |
| 139 | // select (credential_type) { |
| 140 | // case basic: |
| 141 | // BasicCredential; |
| 142 | // |
| 143 | // case x509: |
| 144 | // opaque cert_data<1..2^24-1>; |
| 145 | // }; |
| 146 | // } Credential; |
| 147 | struct Credential |
| 148 | { |
| 149 | Credential() = default; |
| 150 | |
| 151 | CredentialType type() const; |
| 152 | |
| 153 | template<typename T> |
| 154 | const T& get() const |
| 155 | { |
| 156 | return var::get<T>(_cred); |
| 157 | } |
| 158 | |
| 159 | static Credential basic(const bytes& identity); |
| 160 | static Credential x509(const std::vector<bytes>& der_chain); |
| 161 | static Credential userinfo_vc(const std::string& userinfo_vc_jwt); |
| 162 | static Credential multi( |
| 163 | const std::vector<CredentialBindingInput>& binding_inputs, |
| 164 | const SignaturePublicKey& signature_key); |
| 165 | |
| 166 | bool valid_for(const SignaturePublicKey& pub) const; |
| 167 | |
| 168 | TLS_SERIALIZABLE(_cred) |
| 169 | TLS_TRAITS(tls::variant<CredentialType>) |
| 170 | |
| 171 | private: |
| 172 | using SpecificCredential = var::variant<BasicCredential, |
| 173 | X509Credential, |
| 174 | UserInfoVCCredential, |
| 175 | MultiCredential>; |
| 176 | |
| 177 | Credential(SpecificCredential specific); |
| 178 | SpecificCredential _cred; |
| 179 | }; |
| 180 | |
| 181 | // XXX(RLB): This struct needs to appear below Credential so that all types are |
| 182 | // concrete at the appropriate points. |
| 183 | struct CredentialBindingInput |
| 184 | { |
| 185 | CipherSuite cipher_suite; |
| 186 | Credential credential; |
| 187 | const SignaturePrivateKey& credential_priv; |
| 188 | }; |
| 189 | |
| 190 | struct CredentialBinding |
| 191 | { |
| 192 | CipherSuite cipher_suite; |
| 193 | Credential credential; |
| 194 | SignaturePublicKey credential_key; |
| 195 | bytes signature; |
| 196 | |
| 197 | CredentialBinding() = default; |
| 198 | CredentialBinding(CipherSuite suite_in, |
| 199 | Credential credential_in, |
| 200 | const SignaturePrivateKey& credential_priv, |
| 201 | const SignaturePublicKey& signature_key); |
| 202 | |
| 203 | bool valid_for(const SignaturePublicKey& signature_key) const; |
| 204 | |
| 205 | TLS_SERIALIZABLE(cipher_suite, credential, credential_key, signature) |
| 206 | |
| 207 | private: |
| 208 | bytes to_be_signed(const SignaturePublicKey& signature_key) const; |
| 209 | }; |
| 210 | |
| 211 | } // namespace mlspp |
| 212 | |
| 213 | namespace mlspp::tls { |
| 214 | |
| 215 | TLS_VARIANT_MAP(mlspp::CredentialType, |
| 216 | mlspp::BasicCredential, |
| 217 | basic) |
| 218 | TLS_VARIANT_MAP(mlspp::CredentialType, |
| 219 | mlspp::X509Credential, |
| 220 | x509) |
| 221 | TLS_VARIANT_MAP(mlspp::CredentialType, |
| 222 | mlspp::UserInfoVCCredential, |
| 223 | userinfo_vc_draft_00) |
| 224 | TLS_VARIANT_MAP(mlspp::CredentialType, |
| 225 | mlspp::MultiCredential, |
| 226 | multi_draft_00) |
| 227 | |
| 228 | } // namespace mlspp::tls |