Permalink
Browse files

use parse_RSA in X509/Cert.hs

  • Loading branch information...
1 parent ad59e4c commit faebf3c3d2f0336eb279b7c5c7c6c99222b97713 @rekado rekado committed Apr 22, 2012
Showing with 14 additions and 22 deletions.
  1. +6 −16 Data/Certificate/KeyRSA.hs
  2. +8 −6 Data/Certificate/X509/Cert.hs
@@ -17,28 +17,19 @@ module Data.Certificate.KeyRSA
import Data.ASN1.DER (encodeASN1Stream, ASN1(..), ASN1ConstructionType(..))
import Data.ASN1.BER (decodeASN1Stream)
import Data.ASN1.BitArray
+import Data.Certificate.X509.Cert
import qualified Data.ByteString.Lazy as L
import qualified Crypto.Types.PubKey.RSA as RSA
parsePublic :: [ASN1] -> Either String RSA.PublicKey
parsePublic
[ Start Sequence
, Start Sequence
- , OID [1,2,840,113549,1,1,1] -- stands for RSA crypto method?
+ , OID [1,2,840,113549,1,1,1] -- PubKeyALG_RSA
, Null
, End Sequence
, BitString (BitArray _ as1n)
- , End Sequence ] = case (decodeASN1Stream as1n) of
- Left _ -> Left "could not decode inner ASN1 stream"
- Right [ Start Sequence
- , IntVal p_modulus
- , IntVal p_exponent
- , End Sequence ] -> Right $
- RSA.PublicKey
- { RSA.public_size = calculate_modulus p_modulus 1
- , RSA.public_n = p_modulus
- , RSA.public_e = p_exponent }
- Right _ -> Left "stream does not contain an RSA public key"
+ , End Sequence ] = parse_RSA as1n
parsePublic _ = Left "unexpected format"
decodePublic :: L.ByteString -> Either String RSA.PublicKey
@@ -67,6 +58,9 @@ parsePrivate
, RSA.public_n = p_modulus
, RSA.public_e = pub_exp
}
+ calculate_modulus n i = if (2 ^ (i * 8)) > n
+ then i
+ else calculate_modulus n (i+1)
parsePrivate (Start Sequence : IntVal n : _)
| n == 0 = Left "RSA key format: not recognized"
| otherwise = Left ("RSA key format: unknown version " ++ show n)
@@ -93,7 +87,3 @@ encodePrivate (pubkey, privkey) =
, IntVal $ fromIntegral $ RSA.private_qinv privkey
, End Sequence
]
-
-calculate_modulus n i = if (2 ^ (i * 8)) > n
- then i
- else calculate_modulus n (i+1)
@@ -23,6 +23,9 @@ module Data.Certificate.X509.Cert
, parseCertificate
, encodeCertificateHeader
+ -- public key parser
+ , parse_RSA
+
-- * extensions
, module Data.Certificate.X509.Ext
) where
@@ -111,18 +114,17 @@ oidOrganization = [2,5,4,10]
oidOrganizationUnit = [2,5,4,11]
{- | parse a RSA pubkeys from ASN1 encoded bits.
- - return PubKeyRSA (len-modulus, modulus, e) if successful -}
-parse_RSA :: ByteString -> ParseASN1 PubKey
+ - return RSA.PublicKey (len-modulus, modulus, e) if successful -}
+parse_RSA :: ByteString -> Either String RSA.PublicKey
parse_RSA bits =
case decodeASN1Stream $ bits of
Right [Start Sequence, IntVal modulus, IntVal pubexp, End Sequence] ->
- return $ PubKeyRSA $ RSA.PublicKey
+ Right $ RSA.PublicKey
{ RSA.public_size = calculate_modulus modulus 1
, RSA.public_n = modulus
, RSA.public_e = pubexp
}
- _ ->
- throwError ("bad RSA format")
+ _ -> Left "bad RSA format"
where
calculate_modulus n i = if (2 ^ (i * 8)) > n then i else calculate_modulus n (i+1)
@@ -249,7 +251,7 @@ parseCertHeaderSubjectPK = onNextContainer Sequence $ do
[OID pkalg,Null] -> do
let sig = oidPubKey pkalg
case sig of
- PubKeyALG_RSA -> parse_RSA bits
+ PubKeyALG_RSA -> either (throwError) (return . PubKeyRSA) (parse_RSA bits)
_ -> return $ PubKeyUnknown pkalg $ L.unpack bits
[OID pkalg,OID _] -> do
let sig = oidPubKey pkalg

0 comments on commit faebf3c

Please sign in to comment.