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 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526
|
{-# LANGUAGE OverloadedStrings #-}
{-# OPTIONS_GHC -fno-warn-orphans #-}
-- | Orphan instances.
module CMS.Instances
( arbitraryPassword
, arbitraryAttributes
, arbitraryIntegrityDigest
, arbitrarySigVer
, arbitraryEnvDev
) where
import Data.ASN1.Types
import qualified Data.ByteArray as B
import Data.ByteString (ByteString)
import Data.X509
import GHC.TypeLits
import Test.Tasty.QuickCheck
import Crypto.Cipher.Types
import Crypto.Store.CMS
import Crypto.Store.Error
import X509.Instances
arbitrarySmall :: Gen ByteString
arbitrarySmall = resize 10 (B.pack <$> arbitrary)
arbitraryPassword :: Gen ByteString
arbitraryPassword = resize 16 (B.pack <$> asciiChar)
where asciiChar = listOf $ choose (0x20,0x7f)
instance Arbitrary ContentInfo where
arbitrary = sized $ \n ->
if n == 0
then DataCI <$> arbitraryMessage
else oneof [ DataCI <$> arbitraryMessage
, arbitraryMode <*> arbitrarySignedData
, arbitraryMode <*> arbitraryEnvelopedData
, arbitraryMode <*> arbitraryDigestedData
, arbitraryMode <*> arbitraryEncryptedData
, arbitraryMode <*> arbitraryAuthenticatedData
, arbitraryMode <*> arbitraryAuthEnvelopedData
]
where
arbitraryMessage :: Gen ByteString
arbitraryMessage = resize 2048 (B.pack <$> arbitrary)
arbitraryMode :: Encapsulates struct => Gen (struct ByteString -> ContentInfo)
arbitraryMode = elements [ toAttachedCI, snd . toDetachedCI ]
arbitrarySignedData :: Gen (SignedData EncapsulatedContent)
arbitrarySignedData = do
alg <- arbitrary
(sigFns, _) <- arbitrarySigVer alg
inner <- scale (subtract $ length sigFns) arbitrary
signData sigFns inner >>= failIfError
arbitraryEnvelopedData :: Gen (EnvelopedData EncryptedContent)
arbitraryEnvelopedData = do
oinfo <- arbitrary
(alg, key, attrs) <- getCommon
(envFns, _) <- arbitraryEnvDev key
inner <- scale (subtract $ length envFns) arbitrary
envelopData oinfo key alg envFns attrs inner >>= failIfError
arbitraryDigestedData :: Gen (DigestedData EncapsulatedContent)
arbitraryDigestedData = do
inner <- scale pred arbitrary
dt <- arbitrary
return $ digestData dt inner
arbitraryEncryptedData :: Gen (EncryptedData EncryptedContent)
arbitraryEncryptedData = do
(alg, key, attrs) <- getCommon
inner <- scale pred arbitrary
failIfError $ encryptData key alg attrs inner
arbitraryAuthenticatedData :: Gen (AuthenticatedData EncapsulatedContent)
arbitraryAuthenticatedData = do
(oinfo, alg, key, envFns, aAttrs, uAttrs) <- getCommonAuth
dig <- arbitrary
inner <- scale (subtract $ length envFns) arbitrary
generateAuthenticatedData oinfo key alg dig envFns aAttrs uAttrs inner
>>= failIfError
arbitraryAuthEnvelopedData :: Gen (AuthEnvelopedData EncryptedContent)
arbitraryAuthEnvelopedData = do
(oinfo, alg, key, envFns, aAttrs, uAttrs) <- getCommonAuth
inner <- scale (subtract $ length envFns) arbitrary
authEnvelopData oinfo key alg envFns aAttrs uAttrs inner
>>= failIfError
getCommonAuth :: (HasKeySize params, Arbitrary params)
=> Gen ( OriginatorInfo, params, ContentEncryptionKey
, [ProducerOfRI Gen], [Attribute], [Attribute]
)
getCommonAuth = do
oinfo <- arbitrary
(alg, key, uAttrs) <- getCommon
aAttrs <- arbitraryAttributes
(envFns, _) <- arbitraryEnvDev key
return (oinfo, alg, key, envFns, aAttrs, uAttrs)
getCommon :: (HasKeySize params, Arbitrary params, B.ByteArray key)
=> Gen (params, key, [Attribute])
getCommon = do
alg <- arbitrary
key <- generateKey alg
attrs <- arbitraryAttributes
return (alg, key, attrs)
failIfError :: Either StoreError a -> Gen a
failIfError = either (error . show) return
instance Arbitrary Attribute where
arbitrary = do
oid <- arbitraryOID
vals <- resize 3 $ listOf1 (OctetString <$> arbitrarySmall)
return Attribute { attrType = oid, attrValues = vals }
arbitraryAttributes :: Gen [Attribute]
arbitraryAttributes = resize 3 arbitrary
arbitraryNat :: Gen SomeNat
arbitraryNat = unwrap <$> arbitrary
where unwrap (Positive i) = let Just n = someNatVal (127 + i) in n
instance Arbitrary DigestAlgorithm where
arbitrary = oneof
[ pure $ DigestAlgorithm MD2
, pure $ DigestAlgorithm MD4
, pure $ DigestAlgorithm MD5
, pure $ DigestAlgorithm SHA1
, pure $ DigestAlgorithm SHA224
, pure $ DigestAlgorithm SHA256
, pure $ DigestAlgorithm SHA384
, pure $ DigestAlgorithm SHA512
, pure $ DigestAlgorithm SHAKE128_256
, pure $ DigestAlgorithm SHAKE256_512
, (\(SomeNat p) -> DigestAlgorithm (SHAKE128 p)) <$> arbitraryNat
, (\(SomeNat p) -> DigestAlgorithm (SHAKE256 p)) <$> arbitraryNat
]
arbitraryIntegrityDigest :: Gen DigestAlgorithm
arbitraryIntegrityDigest = elements
[ DigestAlgorithm MD5
, DigestAlgorithm SHA1
, DigestAlgorithm SHA224
, DigestAlgorithm SHA256
, DigestAlgorithm SHA384
, DigestAlgorithm SHA512
]
instance Arbitrary MACAlgorithm where
arbitrary = oneof
[ (\(DigestAlgorithm alg) -> HMAC alg) <$> arbitraryIntegrityDigest
, (\(SomeNat p) -> KMAC_SHAKE128 p) <$> arbitraryNat <*> arbitraryCtx
, (\(SomeNat p) -> KMAC_SHAKE256 p) <$> arbitraryNat <*> arbitraryCtx
]
where arbitraryCtx = elements [ B.empty , "ctx" , "custom string" ]
instance Arbitrary OAEPParams where
arbitrary = do
alg <- arbitrary
mga <- MGF1 <$> arbitrary
return OAEPParams { oaepHashAlgorithm = alg
, oaepMaskGenAlgorithm = mga
}
instance Arbitrary PSSParams where
arbitrary = do
alg <- arbitrary
mga <- MGF1 <$> arbitrary
len <- choose (1, 30)
return PSSParams { pssHashAlgorithm = alg
, pssMaskGenAlgorithm = mga
, pssSaltLength = len
}
instance Arbitrary SignatureAlg where
arbitrary = oneof
[ pure RSAAnyHash
, pure $ RSA (DigestAlgorithm MD2)
, pure $ RSA (DigestAlgorithm MD5)
, pure $ RSA (DigestAlgorithm SHA1)
, pure $ RSA (DigestAlgorithm SHA224)
, pure $ RSA (DigestAlgorithm SHA256)
, pure $ RSA (DigestAlgorithm SHA384)
, pure $ RSA (DigestAlgorithm SHA512)
, RSAPSS <$> arbitrary
, pure $ DSA (DigestAlgorithm SHA1)
, pure $ DSA (DigestAlgorithm SHA224)
, pure $ DSA (DigestAlgorithm SHA256)
, pure $ ECDSA (DigestAlgorithm SHA1)
, pure $ ECDSA (DigestAlgorithm SHA224)
, pure $ ECDSA (DigestAlgorithm SHA256)
, pure $ ECDSA (DigestAlgorithm SHA384)
, pure $ ECDSA (DigestAlgorithm SHA512)
, pure Ed25519
, pure Ed448
]
arbitraryKeyPair :: SignatureAlg -> Gen (PubKey, PrivKey)
arbitraryKeyPair RSAAnyHash = do
(pub, priv) <- arbitraryRSA
return (PubKeyRSA pub, PrivKeyRSA priv)
arbitraryKeyPair (RSA _) = do
(pub, priv) <- arbitraryRSA
return (PubKeyRSA pub, PrivKeyRSA priv)
arbitraryKeyPair (RSAPSS _) = do
(pub, priv) <- arbitraryRSA
return (PubKeyRSA pub, PrivKeyRSA priv)
arbitraryKeyPair (DSA _) = do
(pub, priv) <- arbitraryDSA
return (PubKeyDSA pub, PrivKeyDSA priv)
arbitraryKeyPair (ECDSA _) = do
(pub, priv) <- arbitraryNamedEC
return (PubKeyEC pub, PrivKeyEC priv)
arbitraryKeyPair Ed25519 = do
(pub, priv) <- arbitraryEd25519
return (PubKeyEd25519 pub, PrivKeyEd25519 priv)
arbitraryKeyPair Ed448 = do
(pub, priv) <- arbitraryEd448
return (PubKeyEd448 pub, PrivKeyEd448 priv)
arbitrarySigVer :: SignatureAlg -> Gen ([ProducerOfSI Gen], ConsumerOfSI Gen)
arbitrarySigVer alg = sized $ \n -> do
(sigFn, verFn) <- onePair
otherPairs <- resize (min (pred n) 3) $ listOf onePair
sigFns <- shuffle (sigFn : map fst otherPairs)
return (sigFns, verFn)
where
onePair = do
(pub, priv) <- arbitraryKeyPair alg
chain <- arbitraryCertificateChain pub
sAttrs <- oneof [ pure Nothing, Just <$> arbitraryAttributes ]
uAttrs <- arbitraryAttributes
return (certSigner alg priv chain sAttrs uAttrs, withPublicKey pub)
instance Arbitrary PBKDF2_PRF where
arbitrary = elements
[ PBKDF2_SHA1
, PBKDF2_SHA256
, PBKDF2_SHA512
]
instance Arbitrary ContentEncryptionAlg where
arbitrary = elements
[ CBC DES
, CBC DES_EDE3
, CBC AES128
, CBC AES192
, CBC AES256
, CBC CAST5
, CBC Camellia128
, CBC_RC2
, ECB DES
, ECB AES128
, ECB AES192
, ECB AES256
, ECB Camellia128
, CFB DES
, CFB AES128
, CFB AES192
, CFB AES256
, CFB Camellia128
, CTR Camellia128
]
instance Arbitrary ContentEncryptionParams where
arbitrary = arbitrary >>= gen
where
gen CBC_RC2 = choose (24, 512) >>= generateRC2EncryptionParams
gen alg = generateEncryptionParams alg
instance Arbitrary AuthContentEncryptionAlg where
arbitrary = elements
[ AUTH_ENC_128
, AUTH_ENC_256
, CHACHA20_POLY1305
, CCM AES128
, CCM AES192
, CCM AES256
, GCM AES128
, GCM AES192
, GCM AES256
]
instance Arbitrary AuthContentEncryptionParams where
arbitrary = do
alg <- arbitrary
case alg of
AUTH_ENC_128 -> arb3 generateAuthEnc128Params
AUTH_ENC_256 -> arb3 generateAuthEnc256Params
CHACHA20_POLY1305 -> generateChaChaPoly1305Params
CCM c -> do m <- arbitraryM
l <- arbitraryL
generateCCMParams c m l
GCM c -> choose (12,16) >>= generateGCMParams c
where arb3 fn = do
a <- arbitrary; b <- arbitrary; c <- arbitrary
fn a b c
arbitraryM :: Gen CCM_M
arbitraryM = elements
[ CCM_M4
, CCM_M6
, CCM_M8
, CCM_M10
, CCM_M12
, CCM_M14
, CCM_M16
]
arbitraryL :: Gen CCM_L
arbitraryL = elements [ CCM_L2, CCM_L3, CCM_L4 ]
instance Arbitrary KeyDerivationFunc where
arbitrary = do
salt <- generateSalt 8
oneof [ pbkdf2 salt , scrypt salt ]
where
pbkdf2 salt = do
iters <- choose (1,512)
pf <- arbitrary
return PBKDF2 { pbkdf2Salt = salt
, pbkdf2IterationCount = iters
, pbkdf2KeyLength = Nothing
, pbkdf2Prf = pf
}
scrypt salt = do
(n, r, p) <- elements [ (16, 1, 1) , (1024, 8, 16) ]
return Scrypt { scryptSalt = salt
, scryptN = n
, scryptR = r
, scryptP = p
, scryptKeyLength = Nothing
}
instance Arbitrary KeyTransportParams where
arbitrary = oneof
[ pure RSAES
, RSAESOAEP <$> arbitrary
]
instance Arbitrary KeyEncryptionParams where
arbitrary = oneof
[ PWRIKEK <$> arbitrary
, return AES128_WRAP
, return AES192_WRAP
, return AES256_WRAP
, return AES128_WRAP_PAD
, return AES192_WRAP_PAD
, return AES256_WRAP_PAD
, return DES_EDE3_WRAP
, RC2_WRAP <$> choose (1, 1024)
]
instance Arbitrary OtherKeyAttribute where
arbitrary = do
oid <- arbitraryOID
vals <- resize 3 $ listOf1 (OctetString <$> arbitrarySmall)
return OtherKeyAttribute { keyAttrId = oid, keyAttr = vals }
instance Arbitrary KeyIdentifier where
arbitrary = do
kid <- arbitrarySmall
KeyIdentifier kid Nothing <$> arbitrary
arbitraryAgreeParams :: Bool -> KeyEncryptionParams -> Gen KeyAgreementParams
arbitraryAgreeParams allowCofactorDH alg
| allowCofactorDH = oneof
[ flip StdDH alg <$> elements stdKDFs
, flip CofactorDH alg <$> elements cofactorKDFs
]
| otherwise = flip StdDH alg <$> elements stdKDFs
where
cofactorKDFs =
[ KA_X963_KDF SHA1
, KA_X963_KDF SHA224
, KA_X963_KDF SHA256
, KA_X963_KDF SHA384
, KA_X963_KDF SHA512
]
stdKDFs = cofactorKDFs ++
[ KA_HKDF SHA256
, KA_HKDF SHA384
, KA_HKDF SHA512
]
arbitraryEnvDev :: ContentEncryptionKey
-> Gen ([ProducerOfRI Gen], ConsumerOfRI Gen)
arbitraryEnvDev cek = sized $ \n -> do
(envFn, devFn) <- onePair
otherPairs <- resize (min (pred n) 3) $ listOf onePair
envFns <- shuffle (envFn : map fst otherPairs)
return (envFns, devFn)
where
len = B.length cek
onePair = oneof [ arbitraryKT, arbitraryKA, arbitraryKEK, arbitraryPW ]
arbitraryKT = do
(pub, priv) <- arbitraryLargeRSA
cert <- arbitrarySignedCertificate (PubKeyRSA pub)
ktp <- arbitrary
let envFn = forKeyTransRecipient cert ktp
devFn = withRecipientKeyTrans (PrivKeyRSA priv)
return (envFn, devFn)
arbitraryKA = do
(cert, priv) <- arbitraryDHParams
let allowCofactorDH =
case priv of
PrivKeyEC _ -> True
_ -> False
kap <- arbitraryAlg >>= arbitraryAgreeParams allowCofactorDH
let envFn = forKeyAgreeRecipient cert kap
devFn = withRecipientKeyAgree priv cert
return (envFn, devFn)
arbitraryKEK = do
kid <- arbitrary
es <- arbitraryAlg
key <- generateKey es
return (forKeyRecipient key kid es, withRecipientKey key)
arbitraryPW = do
pwd <- arbitraryPassword
kdf <- arbitrary
cea <- arbitrary `suchThat` notModeCTR
let es = PWRIKEK cea
return (forPasswordRecipient pwd kdf es, withRecipientPassword pwd)
arbitraryAlg
| len == 24 = oneof [ return AES128_WRAP
, return AES192_WRAP
, return AES256_WRAP
, return AES128_WRAP_PAD
, return AES192_WRAP_PAD
, return AES256_WRAP_PAD
, return DES_EDE3_WRAP
, RC2_WRAP <$> choose (1, 1024)
]
| mod len 8 == 0 = oneof [ return AES128_WRAP
, return AES192_WRAP
, return AES256_WRAP
, return AES128_WRAP_PAD
, return AES192_WRAP_PAD
, return AES256_WRAP_PAD
, RC2_WRAP <$> choose (1, 1024)
]
| otherwise = oneof [ return AES128_WRAP_PAD
, return AES192_WRAP_PAD
, return AES256_WRAP_PAD
, RC2_WRAP <$> choose (1, 1024)
]
arbitraryDHParams = oneof [ arbitraryCredNamedEC
, arbitraryCredX25519
, arbitraryCredX448
]
arbitraryCredNamedEC = do
(pub, priv) <- arbitraryNamedEC
cert <- arbitrarySignedCertificate (PubKeyEC pub)
return (cert, PrivKeyEC priv)
arbitraryCredX25519 = do
(pub, priv) <- arbitraryX25519
cert <- arbitrarySignedCertificate (PubKeyX25519 pub)
return (cert, PrivKeyX25519 priv)
arbitraryCredX448 = do
(pub, priv) <- arbitraryX448
cert <- arbitrarySignedCertificate (PubKeyX448 pub)
return (cert, PrivKeyX448 priv)
-- key wrapping in PWRIKEK is incompatible with CTR mode so we must never
-- generate this combination
notModeCTR params =
case getContentEncryptionAlg params of
CTR _ -> False
_ -> True
instance Arbitrary OriginatorInfo where
arbitrary = OriginatorInfo <$> arbitrary <*> arbitrary
instance Arbitrary CertificateChoice where
arbitrary = oneof [ CertificateCertificate <$> arbitrary
, CertificateOther <$> arbitrary
]
instance Arbitrary RevocationInfoChoice where
arbitrary = oneof [ RevocationInfoCRL <$> arbitrary
, RevocationInfoOther <$> arbitrary
]
instance Arbitrary OtherCertificateFormat where
arbitrary = do
oid <- arbitraryOID
vals <- resize 3 $ listOf1 (OctetString <$> arbitrarySmall)
return OtherCertificateFormat { otherCertFormat = oid
, otherCertValues = vals
}
instance Arbitrary OtherRevocationInfoFormat where
arbitrary = do
oid <- arbitraryOID
vals <- resize 3 $ listOf1 (OctetString <$> arbitrarySmall)
return OtherRevocationInfoFormat { otherRevInfoFormat = oid
, otherRevInfoValues = vals
}
|