Converting ACME Spec DER Certificates to PEM Format with CURL

Hello guys.
I have a certbot and boulder copy running in 2 local VM and I m using Ubuntu server trusty. Everything works fine. However, now im trying to get the certificate after the challenges are completed using curl. So after doing all the challenges and getting a new certificate I do this:
curl -i -H "Accept: application/crt" -H "Content-Type: application/json" http://172.17.0.4:4000/acme/cert/ff1ffe82ed...#certId

172.17.0.4:4000 Btw, this is where my Boulder is listening :grinning:

I was expecting to get the same certificate but I get some cyphered text, I guess it is the certificate, so how can I read that? Or im getting it all wrong and that cant be done?
Thanks in advance!!

Hi @Chopi64

This is the DER representation of the certificate

If you have a look at one of mine: https://acme-v01.api.letsencrypt.org/acme/cert/0472f4a7c14409e446e72fa2f06db7334b52

You will see the same thing

Section 6.6 describes this: draft-ietf-acme-acme-01

GET /acme/cert/asdf HTTP/1.1
Host: example.com
Accept: application/pkix-cert

HTTP/1.1 202 Accepted
Retry-After: 120

The default format of the certificate is DER (application/pkix-cert).
The client may request other formats by including an Accept header in
its request.

The server provides metadata about the certificate in HTTP headers.
In particular, the server MUST include a Link relation header field
[RFC5988] with relation "up" to provide a certificate under which
this certificate was issued, and one with relation "author" to
indicate the registration under which this certicate was issued. The
server MAY also include an Expires header as a hint to the client
about when to renew the certificate. (Of course, the real expiration
of the certificate is controlled by the notAfter time in the
certificate itself.)

You will need to grab the DER and Conver it to PEM.

An example from the letsencryptshell client letsencryptshell

def sslutils_x509_dertopem(x = ""):
"""
Conversion from DER to PEM format
XXX TODO - remove dependency on openssl
"""

    if x.find("BEGIN CERTIFICATE") != -1:
            return x
    cmd = ['openssl', 'x509', '-inform', 'der', '-outform', 'pem']
    p = subprocess.Popen(cmd, stdout = subprocess.PIPE, stdin = subprocess.PIPE)
    ret = p.communicate(x)[0]
    if (p.returncode != 0):
            raise Exception("%s: failed" % (" ".join(cmd)))
    return ret

Andrei

Well, you donā€™t have to use all of that Python wrapping around the openssl program. You can use it directly:

openssl x509 -in mycert.der -out mycert.pem -inform der -outform pem

Thank you all, after @ahaw021 comment I realized that it wasnt base64 encoded so of course I couldnt view the contents. However, I still dont get the .pem certificate after saving it in a file and then using the openssl commandā€¦what I get is a malformed der encoded file, so I think I ll have to rethink the curl approach, I ll keep working around it, thank you all guys!:grin:

I'm afraid that doesn't make sense to me. Would you be willing to post the original DER file and the OpenSSL commands that you're running?

I cant till Friday morning :sweat:

Hello, I m back here with my computer. After seeing the DER file carefully I have realized that the certificate i am getting when doing the curl petition comes from the fake CA and it is not the actual certificate that im trying to get with curl. I guess this is because after Certbot ends, then the certificate isnt available again at the the uri, right?
Commands:
curl -i -H "Accept: application/json" -H "Content-Type: application/json" http://172.17.0.4:4000/acme/cert/ff1ffe82edc1c851ca6c6e60ea171e18574c > myDer.cer

Content of the DER file:

HTTP/1.1 200 OK
Boulder-Request-Id: ZtXs6FJ1b0yrNXltb9uAk-c1kESeyV0X6Wijm9qhG44
Cache-Control: public, max-age=0, no-cache
Content-Type: application/pkix-cert
Link: <http://172.17.0.4:4000/acme/issuer-cert>;rel="up"
Replay-Nonce: IeNwJQJTSl5c3EqwraRY0eO5yjItyG4eVo5JoMExr4M
Date: Fri, 09 Jun 2017 07:43:40 GMT
Content-Length: 1156
0<82>^D<80>0<82>^Ch<A0>^C^B^A^B^B^S^@<FF>^B<F4>
<DF>y<EF><FB><FA><FA><B2><E6><A8><EC>^K<BA>J<A2>0^M^F   *<86>H<86><F7>^M^A^A^K^E^@0^_1^]0ESC^F^CU^D^C^L^Th2ppy h2cker fake CA0^^^W
^M170609063800Z^W^M170907063800Z0E1^T0^R^F^CU^D^C^S^Kwww.bye.com1-0+^F^CU^D^E^S$ff02f40adf79effbfafab2e6a8ec0bba4aa20<82>^A"0^M^F
 *<86>H<86><F7>^M^A^A^A^E^@^C<82>^A^O^@0<82>^A
^B<82>^A^A^@<9A>n<F8><FC>
-<90>1<B3><B1><D5>l<9F><E5>ng<81>G[o<A5>^?q<EB>-<C0><E5><A5>a<EF>^F]<D6>H<B8>6L<91><D7>v_^E<E8>kH<DF>^Ou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^B0^S^L^QDo What Thou Wilt0^M^F       *<86>H<86><F7>^M^A^A^K^E^@^C<82>^A^A^@*^R#<C6>w<B9>j<A6><97><D7>
<D1>ZQ^<CC>^M|y^^^_<F2>T^M^Zd<E7>C\d<C6>Óæ}qŁ©^Oj<E7><92>^F<8D><C0><AC>D<83>^S"<<F1><FF>r<F1>^O<EE><DD>+<96>      <F9><AE>^SŲ³Y<E4>/
<CC>gĪ©<BD>D^V<C5><D6>L<C5>]<B0>^@M~<B2><F7>    <C0>C)M<97><FB><DD>m<AA>Ѭ#b<8C>l^U,q:<EC>g^\L^X<B9><D8>ll<F4><94>f^\<F5>\<B2>k*^Pex
<C1><A7><CF>gp<AF>?}?o<D2>`\<DF><C6>qESC<8B>_Q<EF><DF>oC<9A>S*T<A7>RSd<A0><BE><AF><DD>B~E%Z<89><E1>^W<99><A2>h~<D4>^D^K]<E9><F5>c
<DD>6^Rz<EC>S<E8>;<94>^?^A<C5>p<E8><86>^_<DB><C4>#<CE><C9><E3>0n}2<98>b`^_<EC><E5><C5><^C;^M<8F><8F>D<AD>^Y<8C>>c^G#<D6>^Y^F<FE>ߔ^^<D1><C8>3e<F0><8C><CD><E1><F1>   ^L<F6>f<B4>Ė›<A4>[<9B>
myDer.cer (END)

I havenā€™t thought about the other possible part of the problem, but the reason your DER file is corrupt is that you used curl -i. The -i option includes web headers in the output, yet they are not part of the file sent by the web server and hence your output is a ā€œweb transaction that includes a DER fileā€ rather than ā€œa DER fileā€. If you leave off the -i option, you can at least fix the file corruption concern.

You were absolutely right!!! I was modifying the der file myself to remove the headers but it was corrupted anyway, I removed the -i field and it worked perfect so I got the certificate just like I wanted :grin:
About the h2ppy h2cker fake CA I first checked if it was because I was passing the -csr myself but removing that field didnā€™t change the CA. Then I thought that it may just be because I am using my own Boulder copy and doing the challenge to a server that I am running but it is not accessible from the Internet. In fact, all the other certifiactes that I have asked for using the full certbot procedure have the the same CA but the message after the challenge says Congratulations!!
So, am I right or am I f***?

Thanks again for the the help, guys.

Iā€™m not sure if I understood your most recent question, but if you run your own copy of Boulder, the certificates that it issues are signed by itself, not by Letā€™s Encrypt. Only Letā€™s Encrypt itself can issue publicly-trusted Letā€™s Encrypt certificates. Running your own copy of Boulder is appropriate only for testing purposes, or when you want to create your own internal organizational CA, or when you want to go through the auditing and approval process to become a publicly-trusted CA.

It is just for testing purposes, so I guess that ends it, thank you all :smile_cat:

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.