j-deng
March 12, 2020, 11:29am
1
I have a requirement that using Certificates begins with -----BEGIN CERTIFICATE----- to verify signatures.
It’s unlike public key that can do with:
[entry] = :public_key.pem_decode(public_key_string)
pub_key = :public_key.pem_entry_decode(entry)
:public_key.verify(...)
I have search to where describe the Certificates Examples — OTP 29.0.2 (public_key 1.21.2) . But cannot figure out how to solve it.
rjk
March 12, 2020, 11:59am
2
You’ll have to extract the public key from the certificate. While it is possible to do that directly using :public_key, with help of some Record imports, you may want to use the x509 package to simplify things:
{:ok, certificate} = X509.Certificate.from_pem(certificate_string)
public_key = X509.Certificate.public_key(certificate)
:public_key.verify(message, :sha256, signature, public_key)
Depending on the signature algorithm (ECDSA, RSASSA-PKCS1_5, RSASSA-PSS, …) it may be necessary to pass in additional options.
j-deng
March 12, 2020, 12:10pm
4
Thanks! @rjk and @voltone