Skip to content

Instantly share code, notes, and snippets.

@troykelly
Created June 23, 2023 08:15
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save troykelly/7d62c69b2f98a9a9abdc37e6e68a512f to your computer and use it in GitHub Desktop.
Save troykelly/7d62c69b2f98a9a9abdc37e6e68a512f to your computer and use it in GitHub Desktop.
Converting PEM to P12

How to Convert a PEM Certificate to P12 using Docker on Windows and MacOS

Step 1: Start the Docker container

For MacOS:

$ docker run --rm -it -v /absolute/path/to/your/certificates:/certificates alpine /bin/sh -c "apk add --no-cache openssl && sh"

For Windows (assuming you are using PowerShell):

PS> docker run --rm -it -v C:\absolute\path\to\your\certificates:/certificates alpine /bin/sh -c "apk add --no-cache openssl && sh"

**Remember to replace '/absolute/path/to/your/certificates' (or 'C:\absolute\path\to\your\certificates' for Windows) with the absolute path to the folder that contains your pem certificate.


Step 2: Convert the PEM certificate to P12

Once you are in the Docker container, run the following command:

/ # openssl pkcs12 -export -out /certificates/certificate.p12 -inkey /certificates/YOUR_KEY.pem -in /certificates/YOUR_CERT.pem

Don't forget to replace YOUR_KEY.pem and YOUR_CERT.pem with their specific names.

You will be asked to enter the passphrase for your PEM files should they be protected. You'll also need to set a password for the P12 certificate.


Step 3: Exit the Docker container

After the conversion is done, you can exit the Docker container by typing exit:

/ # exit

The Docker container will automatically be removed, courtesy of the --rm flag we added at the beginning.


Step 4: Verify Results and Cleanup

Your .p12 file will get saved in your local folder on the host machine, that you mounted while running the Docker container. Verify its presence.

For cleanup, you can optionally remove the Docker Images to free up disk space:

MacOS/Linux:

$ docker rmi -f alpine:latest
$ docker rmi $(docker images -f "dangling=true" -q)
$ docker network prune

Windows (PowerShell):

PS> docker rmi -f alpine:latest
PS> docker rmi $(docker images -f "dangling=true" -q)
PS> docker network prune
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment