acme v0.4.0 Acme
Acme
Installation
Add acme
to your list of dependencies in mix.exs
:
def deps do
[{:acme, "~> 0.3.0"}]
end
How to connect to an Acme server
All client request are called with a connection. I chose to do it this way (over something like hard coded config) so you have the flexability to run multiple connections to many Acme servers at the same time or fetch certificates for different accounts.
To connect to an Acme server, you want use the &Acme.Client.start_link/1 function that takes these options:
server_url
- The Acme server urlprivate_key
- A private_key either in PEM format or as a JWK map, this is required unless you use theprivate_key_file
optionprivate_key_file
- Instead of a private key map/pem value, you can also pass a private key file path
{:ok, conn} = Acme.Client.start_link([
server: "https://acme-v01.api.letsencrypt.org",
private_key_file: "path/to/key.pem"
])
Examples
Staring a connection
Acme.Client.start_link(server: ..., private_key: ...)
#=> {:ok, conn}
We are going to reuse this connection for all examples below
Register an account
Acme.register("mailto:acme@example.com") |> Acme.request(conn)
#=> {:ok, %Registration{...}}
# Agree to terms
Acme.agree_terms(registration) |> Acme.request(conn)
Get new authorization for domain
Acme.authorize("yourdomain.com") |> Acme.request(conn)
#=> {:ok, %Authorization{
status: "pending",
challanges: [
%Acme.Challenge{
type: "http-01",
token "..."
}
]
}}
Respond to a challenge
challenge = %Acme.Challenge{type: "http-01", token: ...}
Acme.respond_challenge(challenge) |> Acme.request(conn)
#=> {:ok, %Challenge{status: "pending", ...}}
Request a certificate
Generate a CSR, we provide OpenSSL helpers to help you with this in Acme.OpenSSL
# Certificate subject informations (only common_name is required)
subject = %{
common_name: "example.acme.com",
organization_name: "Acme INC.",
organizational_unit: "HR",
locality_name: "Philadelphia",
state_or_province: "Pennsylvania",
country_name: "US"
}
# Generate a CSR in DER format
{:ok, csr} = Acme.OpenSSL.generate_csr("/path/to/your/private_key.pem", subject)
# Request a certificate and get back its URL
Acme.new_certificate(csr) |> Acme.request(conn)
#=> {:ok, "https://example.com/acme/cert/asdf"}
# Fetch the certificate using the URL
Acme.get_certificate("https://example.com/acme/cert/asdf")
|> Acme.request(conn)
#=> {:ok, <<DER-encoded certificate>>}
Revoke a certificate
You can revoke a certificate by passing the certificate in DER format and specify a reason code. You can see all possible reason codes at: https://tools.ietf.org/html/rfc5280#section-5.3.1
Acme.revoke_certificate(<<DER-encoded certificate>>, 0)
#=> :ok
License
The MIT License (MIT)
Copyright (c) 2017-present, Sikan He
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Summary
Functions
Builds an %Acme.Request{} for Agree to the TOS after registration.
Takes am
%Acme.Registration{}` struct often received from calling
&Acme.register/1
Builds an %Acme.Request{}
for requesting an authorization
for a domain
Builds an %Acme.Request{}
for Refetch a registration by its uri
Get a certificate by its URL
Takes an CSR in DER format and builds an %Acme.Request{}
for a new certificate
Builds an %Acme.Request{}
for registering an account on the Acme server
Takes an %Acme.Challenge{}
struct and builds an %Acme.ChallengeRequest{}
to
respond to a specific challenge
Takes a certificate in DER format and a reason code and builds
an %Acme.Request{}
to revoke a certificate. When called
with &Acme.request/1
, it returns either :ok
or
{:error, %Acme.Error{}}
Functions
Builds an %Acme.Request{} for Agree to the TOS after registration.
Takes am
%Acme.Registration{}struct often received from calling
&Acme.register/1.
When called with
&Acme.request/1, it returns a
}or
}` tuple.
## Example:
{:ok, conn} = Acme.Client.start_link(server: …, private_key: …)
{:ok, registration} = Acme.register(“mailto:acme@example.com”) |> Acme.request(conn)
Acme.agree_terms(registration) |> Acme.request(conn)
#=> {:ok, %Registration{…}}
Builds an %Acme.Request{}
for requesting an authorization
for a domain.
When called with &Acme.request/1
, it returns a {:ok, %Acme.Authorization{}}
or {:error, %Acme.Error{}}
tuple.
Example:
{:ok, conn} = Acme.Client.start_link(server: ..., private_key: ...)
Acme.authorize("quick@example.com") |> Acme.request(conn)
#=> {:ok, %Authorization{status: "pending", challenges: [...], ...}}
Builds an %Acme.Request{}
for Refetch a registration by its uri.
When called with with &Acme.request/1
, it returns a {:ok, %Acme.Registration{}}
or {:error, %Acme.Error{}}
tuple.
Example:
We have a Registration struct from before:
registration = %Registration{uri:...}
And we want to refetch registation again:
{:ok, conn} = Acme.Client.start_link(server: ..., private_key: ...)
Acme.fetch_registration(registration.uri) |> Acme.request(conn)
#=> {:ok, %Registraction{...}}
Get a certificate by its URL
Example:
{:ok, conn} = Acme.Client.start_link(server: ..., private_key: ...)
Acme.get_certificate("https://example.com/acme/cert/asdf")
|> Acme.request(conn)
#=> {:ok, <<DER-encoded certificate>>}
Takes an CSR in DER format and builds an %Acme.Request{}
for a new certificate
When called with &Acme.request/1
, it returns a {:ok, certificate_url}
or {:error, %Acme.Error{}}
tuple.
Example:
{:ok, conn} = Acme.Client.start_link(server: ..., private_key: ...)
Acme.new_certificate("5jNudRx6Ye4HzKEqT5...FS6aKdZeGsysoCo4H9P")
|> Acme.request(conn)
#=> {:ok, "https://example.com/acme/cert/asdf"}
Builds an %Acme.Request{}
for registering an account on the Acme server.
When called with &Acme.request/1
, it returns a {:ok, %Acme.Registration{}}
or {:error, %Acme.Error{}}
tuple.
Example:
{:ok, conn} = Acme.Client.start_link(server: ..., private_key: ...)
Acme.register("mailto:acme@example.com") |> Acme.request(conn)
#=> {:ok, %Registration{...}}
Takes an %Acme.Challenge{}
struct and builds an %Acme.ChallengeRequest{}
to
respond to a specific challenge.
When called with &Acme.request/1
, it returns a {:ok, %Acme.Chellenge{}}
or {:error, %Acme.Error{}}
tuple.
Example:
challenge = %Acme.Challenge{type: "http-01", token: ...}
{:ok, conn} = Acme.Client.start_link(server: ..., private_key: ...)
Acme.respond_challenge(challenge) |> Acme.request(conn)
#=> {:ok, %Challenge{status: "pending", ...}}
Takes a certificate in DER format and a reason code and builds
an %Acme.Request{}
to revoke a certificate. When called
with &Acme.request/1
, it returns either :ok
or
{:error, %Acme.Error{}}
.
Possible reason codes:
unspecified (0) <-- default,
keyCompromise (1),
cACompromise (2),
affiliationChanged (3),
superseded (4),
cessationOfOperation (5),
certificateHold (6),
-- value 7 is not used
removeFromCRL (8),
privilegeWithdrawn (9),
aACompromise (10)
More information about reason at: https://tools.ietf.org/html/rfc5280#section-5.3.1
Example:
{:ok, conn} = Acme.Client.start_link(server: ..., private_key: ...)
Acme.revoke_certificate("5jNudRx6Ye4HzKEqT5...FS6aKdZeGsysoCo4H9P", 1)
|> Acme.request(conn)
#=> {:ok, "https://example.com/acme/cert/asdf"}