Skip to content

Instantly share code, notes, and snippets.

@powerdefy
Last active June 11, 2021 04:19
Show Gist options
  • Save powerdefy/fa6442c49cc8dad4cef9e4c407b1f47e to your computer and use it in GitHub Desktop.
Save powerdefy/fa6442c49cc8dad4cef9e4c407b1f47e to your computer and use it in GitHub Desktop.
set -e
brew install softhsm opensc # install softhsm2
# clear all tokens
rm -rf /usr/local/var/lib/softhsm/tokens/*
cat > private.pem <<EOL
Bag Attributes
localKeyID: A0 14 BC 15 70 06 95 7D CD A0 48 F8 3B EC CC D3 C6 42 DE FC
Key Attributes: <No Attributes>
-----BEGIN PRIVATE KEY-----
MIIEvwIBADANBgkqhkiG9w0BAQEFAASCBKkwggSlAgEAAoIBAQDr8pmLKHU6dU5H
j1HdPRaaZ1G2DmYOhIt6MfBInha8ltIHnlb+3AEo3eEyekk2+QBEbrCR1DRmNiZy
eemzMZi3GNBVG++/Uch4Njt1fq7BPaJSPqSw2l4Mmbvck31Yi0EAClfRLSsFVhWF
Ns+I5JWhxCiIqmGLAISaGzfYX/4a4pyv8TT4fWirC0/kTGqOsTKp6wuRgJbkI00K
j5hWkC4BWAwffAx/igB+r5rCXsAltVVsc94siB3drNwhf6YCyuHtjHKZMgxZ1INp
qfihPD0VNJoH1nvP++Yyws66DkcOLzt3E5cMm4SoXvQLfF3oRQklpF1VlXL6l8aj
WZEfd/zVAgMBAAECggEBAMrZz9gr+F1B7QLo3ODwXDZzDXUP+/4PRjrQJFzKTZHl
uLyEDRcf3n9cAvh62edBPFGZOISnhEU72OIiniFJSppf4vzI/93kWg2/0m65c1T/
wEYlRUIm+ySqJzEEPIebcU6vlcBorMvxrp7Q+KwgmeRx2MdmdGYlE9vJ+sE0Birl
ciqYADwpcdvEmCh13mr74zxmf5oDPLzjDnlPzJ7QA0dUVynQh6t3wr335fiuy6WU
d0+uo7jt9G3uKQA2J+1M88z/a4fmFR4TMt5XsMqS7SWuooy6FIDcTAjrO53o6ocq
1BmKaYX7HyJtNhl3ZG8xoBuQn3H4uH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----END CERTIFICATE-----
EOL
KEY_ALIAS=BEEF
# init new token
SLOT_ID=$(softhsm2-util --init-token --slot 0 --label "Token 0" --pin 1234 --so-pin 0000 | grep -Eo '[0-9]{1,30}')
# add private key
softhsm2-util --import private.pem --slot $SLOT_ID --label key --id $KEY_ALIAS --pin 1234
# add certificate
pkcs11-tool --module /usr/local/lib/softhsm/libsofthsm2.so -l --id $KEY_ALIAS --label mycert1 -y cert -w cert.pem --pin 1234
echo Your slot id $SLOT_ID
rm private.pem cert.pem
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment