0

我正在编写一个接受 SSL 连接并计算客户端证书的 SHA-1 哈希的 Web 服务器:

import OpenSSL (withOpenSSL)
import OpenSSL.Session as SSL
import OpenSSL.X509 as X509
import OpenSSL.EVP.Digest as EVP

sslStuff :: SSL.SSL -> IO String
sslStuff ssl = withOpenSSL $ do
  x509   <- liftM fromJust $ SSL.getPeerCertificate ssl
  issuer <- X509.getIssuerName x509 False
  subj   <- X509.getSubjectName x509 False
  putStrLn $ "\tsubject: " ++show subj
  putStrLn $ "\tissuer: " ++show issuer
  dg <- liftM fromJust $ EVP.getDigestByName "SHA1"
  cert <- X509.printX509 x509
  putStrLn cert
  let s = EVP.digest dg cert
  putStrLn $ "After Digest: "++s
  return s

我成功获得了证书,但摘要只有 15 个字节而不是 20 个字节。我不确定在将证书传递给 EVP.digest 之前是否正确将其转换为字符串。谁能给我一个如何以正确方式做的例子?

4

1 回答 1

1

我不知道哈斯克尔。但以下代码可能会对您有所帮助。

X509 * x509;
char sha1dig[SHA1_DIGEST_LENGTH];
/*Get X509 certificate in x509*/

//Call X509_check_purpose to set SHA1 hash.
X509_check_purpose (x509, -1, 0);

//Get the SHA1 hash into buffer. Use x509->sha1_hash
memcpy(sha1dig, x509->sha1_hash, SHA1_DIGEST_LENGTH);

我相信我的评论会帮助你将这个程序转换为 Haskell。

于 2013-08-25T05:33:48.267 回答