Skip to content

Commit

Permalink
Updated RSA multiformat
Browse files Browse the repository at this point in the history
  • Loading branch information
expede committed Sep 10, 2021
1 parent 522ea05 commit e30022c
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,17 @@ module Fission.Internal.Orphanage.RSA2048.Public () where

import qualified System.IO.Unsafe as Unsafe

import qualified RIO.ByteString as BS

import qualified Crypto.PubKey.RSA as RSA
import qualified Crypto.Store.X509 as X509

import qualified Data.ByteString.Builder as Builder

import qualified Data.ASN1.BinaryEncoding as ASN1
import qualified Data.ASN1.Encoding as ASN1
import qualified Data.ASN1.Types as ASN1

import qualified Data.PEM as PEM

import qualified Data.ByteString.Base64 as BS64
import qualified Data.X509 as X509

Expand All @@ -32,9 +34,8 @@ instance Arbitrary RSA.PublicKey where

instance Display RSA.PublicKey where
textDisplay pk =
X509.PubKeyRSA pk
|> X509.pubKeyToPEM
|> PEM.pemWriteBS
[X509.PubKeyRSA pk]
|> X509.writePubKeyFileToMemory
|> decodeUtf8Lenient
|> Text.strip
|> Text.dropPrefix pemHeader
Expand Down
4 changes: 3 additions & 1 deletion fission-core/library/Fission/Key/Asymmetric/Public/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ data Public
deriving Eq

instance Show Public where
show = Text.unpack . textDisplay
show = \case
Ed25519PublicKey ed -> Text.unpack $ textDisplay ed
RSAPublicKey pk -> show pk

instance Display Public where
textDisplay (Ed25519PublicKey pk) = textDisplay pk
Expand Down
20 changes: 13 additions & 7 deletions fission-core/library/Fission/User/DID/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@ module Fission.User.DID.Types
, module Fission.User.DID.Method.Types
) where

import qualified RIO.ByteString.Lazy as Lazy

import qualified Data.Aeson.Types as JSON
import Data.Swagger

import Data.Base58String.Bitcoin as BS58.BTC
import Data.Binary hiding (encode)
import qualified Data.ByteArray as BA
import qualified Data.ByteString.Base64 as BS64
import qualified Data.ByteString.Builder as Builder
import Data.Hashable (Hashable (..))

import Crypto.Error
Expand Down Expand Up @@ -113,13 +116,11 @@ instance Display DID where -- NOTE `pk` here is base2, not base58
multicodecW8 :: [Word8]
multicodecW8 =
case pk of
Ed25519PublicKey ed -> 0xed : 0x01 : BS.unpack (BA.convert ed)
RSAPublicKey rsa -> 0x00 : 0xF5 : 0x02 : BS.unpack (BS64.decodeLenient . encodeUtf8 $ textDisplay rsa)
{- ^ ^ ^
| | |
| "expect 373 Bytes", encoded in the mixed-endian format
"raw"
-}
Ed25519PublicKey ed ->
0xed : 0x01 : BS.unpack (BA.convert ed)

RSAPublicKey rsa ->
0x12 : 0x05 : BS.unpack (Lazy.toStrict . Builder.toLazyByteString . getUtf8Builder $ display rsa)

instance ToJSON DID where
toJSON = String . textDisplay
Expand Down Expand Up @@ -158,6 +159,11 @@ parseText txt =
CryptoFailed cryptoError -> fail $ "Unable to parse Ed25519 key: " <> show cryptoError
CryptoPassed edPK -> return $ Ed25519PublicKey edPK

-- Multiformats rsa-x509-pub
(0x12 : 0x05 : rsaKeyW8s) ->
RSAPublicKey <$> parseKeyW8s (BS.pack rsaKeyW8s)

-- Legacy encoding for backward compatability
(0x00 : 0xF5 : 0x02 : rsaKeyW8s) ->
RSAPublicKey <$> parseKeyW8s (BS64.encode $ BS.pack rsaKeyW8s)

Expand Down
2 changes: 1 addition & 1 deletion fission-core/test/Fission/Test/User/DID.hs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ spec =
it "serializes to a well-known value"
let
expected :: Lazy.ByteString
expected = "did:key:z13V3Sog2YaUKhdGCmgx9UZuW1o1ShFJYc6DvGYe7NTt689NoL2RtpVs65Zw899YrTN9WuxdEEDm54YxWuQHQvcKfkZwa8HTgokHxGDPEmNLhvh69zUMEP4zjuARQ3T8bMUumkSLGpxNe1bfQX624ef45GhWb3S9HM3gvAJ7Qftm8iqnDQVcxwKHjmkV4hveKMTix4bTRhieVHi1oqU4QCVy4QPWpAAympuCP9dAoJFxSP6TNBLY9vPKLazsg7XcFov6UuLWsEaxJ5SomCpDx181mEgW2qTug5oQbrJwExbD9CMgXHLVDE2QgLoQMmgsrPevX57dH715NXC2uY6vo2mYCzRY4KuDRUsrkuYCkewL8q2oK1BEDVvi3Sg8pbC9QYQ5mMiHf8uxiHxTAmPedv8"
expected = "did:key:z6QpGjYPVzCGb5LgDoRzmYPiuusJxNfPySt8uiyVfyKqbJHkXGKmhP8q6tWvjmUFxQdW16z1zNPbSvefQ15k9VKURuVy7JX6WF1nA3FmfNNjds8bgRMJYTLLTw4GQGYxE8juG9vFRVkigz5g5PozyTgjqcCgcWVXTaDRW3kLDrtZgzj9J3Z2kthgx1HFsFt3bZCCZ8qZ23bryGDCbVg7z5NCP7SQqoMU9zbS7YygVPrayNuKhaibJkYLDpCbcncenn9eAtjpAtXbuWbwuet1CJB6T25xmfCFhq9nTbwKCXvnboLRa4MPTbpuHYyoiiJiZRUb5v7ut6Gcbr9YHYN7E7QMUjs2xAZHHk2f1xBqdKfrAi6qrxkMQqJaNexogYsHkCTNE43wVT87kDt92DyhbVupyBgMTi3jy3otFzbBYoE3213wzvnG5fVnTKNn4g1FLCmcv8XYRHD6tvPsbMDCA2xVaqKaFnN4nZhDVKK2RnATSDCbHLrAAresS365zMrARDtdJRvYcjULR1bhhmQ58k8J64h"
in
encode (DID Key rsaKey) `shouldBe` "\"" <> expected <> "\""

Expand Down

0 comments on commit e30022c

Please sign in to comment.