Poly1305 command line in OpenSSL - hash

I am trying to execute basic ChaCha20-Poly1305 cipher suite using OpenSSL without any TLS or any Apache server. I have succesfully encrypted my text with Chacha20 openssl command but I am unable to produce Poly1305 MAC authentication. I am using dgst command and it seems it only generates SHA algorithm. I am using 1.1.1 openssl version. Can somebody tell me which command shall I use?

If you have a file F you can compute the Poly1305 with the following command:
openssl dgst -mac poly1305 -macopt key:abcd1234abcd1234abcd1234abcd1234 <PATH_TO_F>
The key may also be a hex value with 32 bytes:
openssl dgst -mac poly1305 -macopt hexkey:ee00000000000000000000000000000000000000000000ffffffffffffffffff <PATH_TO_F>

Related

How do I execute a command in cmder using powershell?

as part of an onboarding process I write a .ps1 script that installs every neccessary program, sets up the dev environments,...
Also I create SSH keys, therefore I have to use openSSL. The command openssl genrsa 4096 | openssl pkcs8 -topk8 -inform PEM -out key.p8 works fine using cmder (and the pub key as well).
But:
The onboarding script is a .ps1 script. Inside this script I install the CMDER (which is used later on in VS Code). I can't create the openssl key using PowerShell. Is there a way to open the CMDER window in the .ps1 script?
So far the Start-Process C:\cmder\cmder.exe -ArgumentList "\task 'openssl genrsa 4096 | openssl pkcs8 -topk8 -inform PEM -out key.p8'" creates an error. Either the command is not recognized or I use the wrong parameter for executing a command (see this link to the CMDER gitlab project: https://github.com/cmderdev/cmder#cmderexe-command-line-arguments).

Is there a way to verify a signed digest within Postgres?

The following command was run and the content of content_file, signature_file and id_rsa.pub (or pem) are inserted into a Postgres database.
openssl dgst -sign id_rsa content_file > signature_file
Is there any way to verify that the signature corresponds with the content/public key within Postgres?
I have looked at the pgcrypto functions however the only relevant function seems to be pgp_pub_decrypt which requires the secret key.
Basically I am looking to perform the following in Postgres:
openssl dgst -verify .\id_rsa.pem -signature .\signature_file .\content_file
As per Craig's suggestion, I ended up using plpythonu to solve this.
CREATE OR REPLACE FUNCTION api.verify(
p_data text,
p_signature text,
p_publickey text
)
RETURNS boolean AS
$$
try:
import rsa
pubkey = rsa.PublicKey.load_pkcs1(p_publickey)
signature = bytearray.fromhex(p_signature)
verified = rsa.verify(p_data, signature, pubkey)
return verified
except:
return False
$$ LANGUAGE plpythonu VOLATILE
SECURITY DEFINER;
With my lack of python knowledge, the hardest part of this was actually setting up the required python packages (Docker environment in my case). Here is the relevant excerpt from the Dockerfile:
FROM postgres:9.6
# Install necessary python packages to work with postgres
RUN apt-get update \
&& apt-get install -y --no-install-recommends \
"postgresql-plpython-$PG_MAJOR" \
&& apt-get install -y python-pip python-dev
# Install python rsa module for signature verification
RUN pip install rsa
The function worked given the following parameters:
-- Generate private key. Provide secure passphrase when prompted.
openssl genrsa -aes256 -out private.pem 4096
--Export public KEY
openssl rsa -in private.pem -RSAPublicKey_out -out public.pem
--Sign data. Provide secure passphrase when prompted.
--Remove first line (RSA-SHA256(data.txt)=) when passing into database verify function.
openssl dgst -hex -sign private.pem data.txt > signature.txt

I can't run postgresql server after I change ssl = on in postgresql.conf in Windows Server 2012.

After changing ssl = on in postgresql.conf, postgresql server can't run anymore. What settings are needed more to enable SSL. I am using postgresql on Windows Server 2012.
Here is my research
https://www.postgresql.org/docs/current/static/ssl-tcp.html#SSL-FILE-USAGE
As in the link, you need to create a Self-signed Certificate first.
To do that you need OPENSSL. I use apache 2.4.12 and it has OpenSSL 1.01.1m ver.
You can also download and install easily from internet.
Then go to the folder that Openssl install from cmd. In my case, "cd C:\Program Files\Apache Software Foundation\Apache 24\bin".
Then execute the command as in the above link says.
openssl req -new -text -out server.req
openssl rsa -in privkey.pem -out server.key
openssl req -x509 -in server.req -text -key server.key -out server.crt (In Windows OS you can only do those steps.)
Then copy server.key C:\ (Anywhere you want..)
copy server.crt C:\
Look for the files you copy (server.key & server.crt) and copy them to PostgreSQL data folder.
Then change ssl = on in postgresql.conf file.
Restart the postgresql service.
Done!!!

help in APNs pem creation

after exporting to p.12 in MacOSX, can i run the following 3 step in Linux? Or i must get it done in the same machine where i export to P.12 before i upload to Linux server to use with my php script?
openssl pkcs12 -clcerts -nokeys -out apns-dev-cert.pem -in apns-dev-cert.p12
openssl pkcs12 -nocerts -out apns-dev-key.pem -in apns-dev-key.p12
openssl rsa -in apns-dev-key.pem -out apns-dev-key-noenc.pem
I am not sure but this might be helpful:
1) CertificateCreation
2) apple-push-notification-service-tutorial
I think there is no problem in use the openssl in a linux machine. The algorithm is the same.
I used this tutorial and works great: http://blog.boxedice.com/2009/07/10/how-to-build-an-apple-push-notification-provider-server-tutorial/

Joining GoDaddy-issued .spc and .key files into a complete .pfx / .cer certificate

I have a GoDaddy-issued code signing certificate in a .spc file. Also, I have a private key in .key file. The code signing has been issued some 13 months ago, then it expired and was renewed with GoDaddy. During the renewal process no private key was requested and just a new .spc file was issues.
Now I'm facing the problem of joining the original private key file with the issues certificate to form a .pfx (or .cer?) file suitable for installation into the Windows certificate store.
The command I'm trying is:
openssl.exe pkcs12 -inkey my.key -in my.spc -out my.pfx -export
However, I'm getting an error message that reads “No certificate matches private key”.
I've followed this answer on SO to verify the .key file is a valid private key. However, when I try to verify that .spc is a valid certificate, I just get
unable to load certificate
5436:error:0906D06C:PEM routines:PEM_read_bio:no start line:.\crypto\pem\pem_lib.c:650:Expecting: TRUSTED CERTIFICATE
What's the correct way of producing an .pfx file from my inputs? I'm using OpenSSL 0.9.8k.
In the end I managed to figure out a procedure that works. Here are the steps to generate a new PFX and CER code signing certificate from SPC and KEY files:
Obtain your new CodeSign.spc certificate from GoDaddy.
Export a PEM-formatted private key from the expired PFX:
openssl.exe pkcs12 -in CodeSign.pfx -nocerts -out CodeSign.pem
Convert the PEM-formatted private key into the PVK format:
pvk.exe -in CodeSign.pem -topvk -strong -out CodeSign.pvk
Combine the PVK and SPC into PFX:
pvk2pfx.exe -pvk CodeSign.pvk -pi <passphrase> -spc CodeSign.spc -pfx CodeSign.pfx -po <passphrase> -f
Import the resulting PFX file into Windows certificate store. Remember to make it exportable.
Export it from the certificate store into the binary CER format as CodeSign.cer.
Optionally delete the certificate from the Windows certificate store.
In case you are renewing your certificate periodically you can store the PVK file and skip steps (2) and (3).
UPDATE: In case you happen to have the certificate in CRT instead of SPC format, do the following to covert it into SPC:
openssl crl2pkcs7 -nocrl -certfile CodeSign.crt -outform DER -out CodeSign.spc
Sources:
http://www.tech-pro.net/export-to-pvk-spc.html
http://ellisweb.net/2008/08/signing-code-using-pvk-and-spc-files/
http://www.drh-consultancy.demon.co.uk/
https://www.sslshopper.com/ssl-converter.html
http://russenreaktor.wordpress.com/2010/06/10/solved-convert-signing-certificate-crt-to-spc/
The tools you will need:
OpenSSL
pvk.exe — see the download link at the bottom of that page (original location may not be accessible; in such a case see this article with a link to a mirror site or another direct download link here)
pvk2pfx.exe — part of Microsoft SDKs, installs with Visual Studio 2010
I had the similar issue and I spent at least few hours searching around for a solution. GoDaddy provided me with .spc and .pem file and I couldn't create .pfx file out if it using OpenSSL. Finally, I imported .spc file within my local computer using MMC. Once the certificate was imported in my local machine, I noticed that it brought in GoDaddy's chain file along with the Code Sign Cert file itself. MMC View
Now, select both files and right click to export as .pfx file. Supply a password to protect the file and you're done. By far, this is the simplest and straight forward solution. Hope this post helps many people.
You can create PFX with openssl only.
Export a PEM-formatted private key from the expired PFX:
openssl pkcs12 -in CodeSign.pfx -nocerts -out CodeSign.pem
Create PFX
openssl pkcs7 -in CodeSign.spc -inform der -print_certs | openssl pkcs12 -export -inkey CodeSign.pem -out CodeSign.pfx
The current answer post was extremely helpful to me in the final steps of moving from an expired certificate file (.pfx or .p12) to a new one with GoDaddy, but I found it lacking information on the initial steps of how to generate a certificate signing request (CSR) from my original certificate file.
For anyone else looking for similar information, here is what I ended up using...
Get the private key:
openssl pkcs12 -in certs-and-key.p12 -out privateKey.key
Get the certificatate:
Beware: This can give you the CA cert
openssl pkcs12 -in certs-and-key.p12 -out certificate.crt -nokeys
Better: Use this command to print only the client cert
openssl pkcs12 -in MacCossLabUW.p12 -clcerts
Then copy the output between:
-----BEGIN CERTIFICATE-----
-----END CERTIFICATE-----
Save it to a file named certificate.crt
Now check that the private key and certificate match with the commands:
openssl rsa -noout -modulus -in privateKey.key | openssl md5
openssl x509 -noout -modulus -in certificate.crt | openssl md5
Then generate a new CSR:
openssl x509 -x509toreq -in certificate.crt -out CSR.csr -signkey privateKey.key
Use the CSR to Re-Key the certificate.
Download the GoDaddy software publishing certificate (.spc) file.
Verify that the generated certificate matches the request private key:
openssl pkcs7 -inform DER -in certificate.spc -print_certs
Then copy the output between for your certificate (Note: the output will also contain CA certs):
-----BEGIN CERTIFICATE-----
-----END CERTIFICATE-----
Save to a file named certificate-new.crt
And run the command:
openssl x509 -noout -modulus -in certificate-new.crt | openssl md5
The output should match the previous call used with the private key and request certificate.
To finish the process, follow the steps outlined in the answer with pvk2pfx.
I also found the schematic diagram in this post quite helpful:
PVK2PFX Error 0x80070490 - Cannot find certificates that match the key
For anyone still looking for an answer on how to do this, I just spent a day figuring this out and had to do a mix of things listed in this post. If you are using git-bash on windows, I had to add winpty to the beginning of all calls using openssl or else it would get stuck in the void. The pseudo steps were as follows:
Generate a private key and CSR using openssl
openssl req -newkey rsa:2048 -keyout private.key -out my.csr
Generate a PVK using the private key with openssl
openssl rsa -in private.key -outform PVK -pvk-strong -out codesign.pvk
Use the CSR in the generation of the code sign certificate on Godaddy
Download the Zip from Godaddy and extract the SPC file (as mentioned by OP)
Use pvk2pfx to combine the PVK and the SPC file into a code sign certificate. This was included in the same SDK (and directory) as signtool itself:
pvk2pfx.exe -pvk codesign.pvk -spc SPC_FILEPATH_HERE -pfx codesign.pfx -pi PVK_PASSWORD -po PFX_PASSWORD
If you generated your certificate request from IIS (I did this on IIS on windows 2012 Server) follow these steps on the server/pc where you generated the request
- Open IIS
- Click on the top level node (Server node)
- Open the Server Certificates settings
- Click on "Complete certificate request" under actions on the right
- Import your spc file to the server.
From here you can then export to a PFX file