@@ -19,16 +19,13 @@ module Wire.API.MLS.Validation
19
19
( -- * Main key package validation function
20
20
validateKeyPackage ,
21
21
validateLeafNode ,
22
+ ValidationError (.. ),
22
23
)
23
24
where
24
25
25
26
import Control.Applicative
26
27
import Control.Error.Util
27
28
import Data.ByteArray qualified as BA
28
- import Data.Text qualified as T
29
- import Data.Text.Lazy qualified as LT
30
- import Data.Text.Lazy.Builder qualified as LT
31
- import Data.Text.Lazy.Builder.Int qualified as LT
32
29
import Data.X509 qualified as X509
33
30
import Imports
34
31
import Wire.API.MLS.Capabilities
@@ -39,20 +36,17 @@ import Wire.API.MLS.LeafNode
39
36
import Wire.API.MLS.Lifetime
40
37
import Wire.API.MLS.ProtocolVersion
41
38
import Wire.API.MLS.Serialisation
39
+ import Wire.API.MLS.Validation.Error
42
40
43
41
validateKeyPackage ::
44
42
Maybe ClientIdentity ->
45
43
KeyPackage ->
46
- Either Text (CipherSuiteTag , Lifetime )
44
+ Either ValidationError (CipherSuiteTag , Lifetime )
47
45
validateKeyPackage mIdentity kp = do
48
46
-- get ciphersuite
49
47
cs <-
50
48
maybe
51
- ( Left
52
- ( " Unsupported ciphersuite 0x"
53
- <> LT. toStrict (LT. toLazyText (LT. hexadecimal kp. cipherSuite. cipherSuiteNumber))
54
- )
55
- )
49
+ (Left (UnsupportedCipherSuite kp. cipherSuite. cipherSuiteNumber))
56
50
pure
57
51
$ cipherSuiteTag kp. cipherSuite
58
52
@@ -65,11 +59,11 @@ validateKeyPackage mIdentity kp = do
65
59
kp. tbs
66
60
kp. signature_
67
61
)
68
- $ Left " Invalid KeyPackage signature "
62
+ $ Left InvalidKeyPackageSignature
69
63
70
64
-- validate protocol version
71
65
maybe
72
- (Left " Unsupported protocol version " )
66
+ (Left UnsupportedProtocolVersion )
73
67
pure
74
68
(pvTag (kp. protocolVersion) >>= guard . (== ProtocolMLS10 ))
75
69
@@ -79,7 +73,7 @@ validateKeyPackage mIdentity kp = do
79
73
lt <- case kp. leafNode. source of
80
74
LeafNodeSourceKeyPackage lt -> pure lt
81
75
-- unreachable
82
- _ -> Left " Unexpected leaf node source "
76
+ _ -> Left UnexpectedLeafNodeSource
83
77
84
78
pure (cs, lt)
85
79
@@ -88,7 +82,7 @@ validateLeafNode ::
88
82
Maybe ClientIdentity ->
89
83
LeafNodeTBSExtra ->
90
84
LeafNode ->
91
- Either Text ()
85
+ Either ValidationError ()
92
86
validateLeafNode cs mIdentity extra leafNode = do
93
87
let tbs = LeafNodeTBS leafNode. core extra
94
88
unless
@@ -99,27 +93,25 @@ validateLeafNode cs mIdentity extra leafNode = do
99
93
(mkRawMLS tbs)
100
94
leafNode. signature_
101
95
)
102
- $ Left " Invalid LeafNode signature "
96
+ $ Left InvalidLeafNodeSignature
103
97
104
98
validateCredential cs leafNode. signatureKey mIdentity leafNode. credential
105
99
validateSource extra. tag leafNode. source
106
100
validateCapabilities (credentialTag leafNode. credential) leafNode. capabilities
107
101
108
- validateCredential :: CipherSuiteTag -> ByteString -> Maybe ClientIdentity -> Credential -> Either Text ()
102
+ validateCredential :: CipherSuiteTag -> ByteString -> Maybe ClientIdentity -> Credential -> Either ValidationError ()
109
103
validateCredential cs pkey mIdentity cred = do
110
104
-- FUTUREWORK: check signature in the case of an x509 credential
111
105
(identity, mkey) <-
112
106
either credentialError pure $
113
107
credentialIdentityAndKey cred
114
108
traverse_ (validateCredentialKey (csSignatureScheme cs) pkey) mkey
115
109
unless (maybe True (identity == ) mIdentity) $
116
- Left " client identity does not match credential identity "
110
+ Left IdentityMismatch
117
111
where
118
- credentialError e =
119
- Left $
120
- " Failed to parse identity: " <> e
112
+ credentialError e = Left $ FailedToParseIdentity e
121
113
122
- validateCredentialKey :: SignatureSchemeTag -> ByteString -> X509. PubKey -> Either Text ()
114
+ validateCredentialKey :: SignatureSchemeTag -> ByteString -> X509. PubKey -> Either ValidationError ()
123
115
validateCredentialKey Ed25519 pk1 (X509. PubKeyEd25519 pk2) = validateCredentialKeyBS pk1 (BA. convert pk2)
124
116
validateCredentialKey Ecdsa_secp256r1_sha256 pk1 (X509. PubKeyEC pk2) =
125
117
case pk2. pubkeyEC_pub of
@@ -131,28 +123,28 @@ validateCredentialKey Ecdsa_secp521r1_sha512 pk1 (X509.PubKeyEC pk2) =
131
123
case pk2. pubkeyEC_pub of
132
124
X509. SerializedPoint bs -> validateCredentialKeyBS pk1 bs
133
125
validateCredentialKey ss _ _ =
134
- Left $
135
- " Certificate signature scheme " <> T. pack (show ss) <> " does not match client's public key"
126
+ Left $ SchemeMismatch ss
136
127
137
- validateCredentialKeyBS :: ByteString -> ByteString -> Either Text ()
128
+ validateCredentialKeyBS :: ByteString -> ByteString -> Either ValidationError ()
138
129
validateCredentialKeyBS pk1 pk2 =
139
- note " Certificate public key does not match client's " $
130
+ note PublicKeyMismatch $
140
131
guard (pk1 == pk2)
141
132
142
- validateSource :: LeafNodeSourceTag -> LeafNodeSource -> Either Text ()
133
+ validateSource :: LeafNodeSourceTag -> LeafNodeSource -> Either ValidationError ()
143
134
validateSource t s = do
144
135
let t' = leafNodeSourceTag s
145
136
if t == t'
146
137
then pure ()
147
138
else
148
139
Left $
149
- " Expected '"
150
- <> t. name
151
- <> " ' source, got '"
152
- <> t'. name
153
- <> " '"
140
+ LeafNodeSourceTagMisMatch $
141
+ " Expected '"
142
+ <> t. name
143
+ <> " ' source, got '"
144
+ <> t'. name
145
+ <> " '"
154
146
155
- validateCapabilities :: CredentialTag -> Capabilities -> Either Text ()
147
+ validateCapabilities :: CredentialTag -> Capabilities -> Either ValidationError ()
156
148
validateCapabilities ctag caps =
157
149
unless (fromMLSEnum ctag `elem` caps. credentials) $
158
- Left " missing BasicCredential capability "
150
+ Left BasicCredentialCapabilityMissing
0 commit comments