retgoat
Hello,
I have a PEM certificate issued for my CSR: my-cert.crt, my private key: private key
I can create a PKCS7 file with that command
openssl smime -sign \
-nodetach \
-in test.txt \
-out signed_data \
-outform PEM \
-signer my-cert.crt \
-inkey private key
My question is: how can I create the same in elixir?
I can sign the file in openssl like that:
openssl dgst -sha256 -sign $privatekey -out /tmp/$filename.sha256 $filename
openssl base64 -in /tmp/$filename.sha256 -out signature.sha256
and verify the signature in elixir:
{:ok, signature} = File.read!("signature.sha256") |> Base.decode64(ignore: :whitespace)
pub = File.read!("my-cert.crt") |> X509.Certificate.from_pem! |> X509.Certificate.public_key
data = File.read!("test.txt")
:public_key.verify(data, :sha256, signature, pub)
true
But I need to create PKCS7 file.
I’m not good at cryptography and maybe lacking some base knowledge ¯_(ツ)_/¯. Sorry for maybe dump question.
Thank you for your help!
Trending in Questions
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
Hello,
I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
So my question is quite simple and i have found no conclusive answer on forum, google or AI.
Should we use :erlang.float for Integer to ...
New
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New
Documentation
While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
Other Trending Topics
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hi there! We created Gust: A task orchestrator inspired by Airflow.
For those who have never heard about Aiflow, it’s a Python-based wor...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
Aludel - LLM Evaluation Workbench
Aludel is an embeddable Phoenix LiveView dashboard for evaluating and comparing LLM prompts across mult...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #ai
- #elixirconf-us
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
voltone
Well… OTP’s
:public_keydoes include the ASN.1 definitions for PKCS#7, but it does not offer any higher-level API for working with those data structures.Here’s a little something I put together that will generate output similar to your OpenSSL example:
At this point it only supports RSA and SHA-1. The certificate needs to be passed in as a
Certificaterecord, rather than anOTPCertificaterecord. So if you useX509.Certificate.from_pemto read it, make sure to pass:Certificateas the second argument.You can convert the output to DER format using
:public_key.der_encode(:ContentInfo, ci), or to PEM format with:public_key.pem_entry_encode(:ContentInfo, ci) |> List.wrap() |> :public_key.pem_encode().retgoat
Many thanks @voltone! You’re a lifesaver!
Yeah, I thought that there is no straightforward implementation and you just proofed it.
I also need an smime encryption, and it seems I need to do a kind a same manipulations to construct encrypted PKCS#7 file. Could you please point me a source I can refer, to construct such file?
Here is openssl command I use:
I also understand that my ask could be too much for you. Anyways, many thanks for your help!
P.S. Maybe it worth to be included into your X509 library? I’m just asking
voltone
Encryption is a bit harder. You’d need to import and populate the
:EnvelopedDatarecord and its sub-records.That would require support for additional algorithms (ECDSA and SHA256 in particular), and also for verification and decryption. Those latter operations would be harder to implement, since they’d have to handle all possible variants of record values and algorithms that other implementations might use. If and when someone wants to implement full PKCS7/SMIME support, that would probably belong in a separate package.
retgoat
Yep, makes total sense to me!
Thank you for your help!
voltone
As for encryption, this seems to work…
retgoat
@voltone, you’re my hero! Thank you so much!
mtodescato
Hi, I’m trying to implement a Signature wrapped in PKCS7, I’ve followed you example converting it to erlang but for the type of signature I want to use it’s necessary to add SigningCertificateV2 to the authenticatedAttributes.
Unforunately it doesn’t behave like the 3 you added as it requires to use an asn1_OPENTYPE to encode it.
So far i’ve implemented it like this, but I’m not able complete the encode as the der_encode function return an error.
Do you have any suggestion on how this could be done? As I haven’t found any examples or documentation for this.
benwilson512
Hi @mtodescato if you are trying to convert it TO erlang you may have more success on the erlang forums.
mtodescato
No the point is not converting it to erlang, is actually making it work. I can accept a response in elixir and i will convert it myself, in fact i used this post to understand how to make it in erlang. The point is that this post seems the only point you can find an example to work on regardless of the language you are trying to use.
voltone
An ASN.1 OPENTYPE value is represented in Erlang as
{asn1_OPENTYPE, DER}, where DER is the binary DER encoded value of the object that needs to be embedded.If you have a complete encoded signature (or you can produce one with e.g. OpenSSL) you can have
public_keydecode it and you’ll see the Erlang record structure you’d have the pass in to the encoder.