Showing posts with label openssl. Show all posts
Showing posts with label openssl. Show all posts

Wednesday, 23 July 2025

open ssl check cert purpose

 openssl x509 -in 57EMM020001.cer -noout -purpose 

Certificate purposes:
SSL client : No
SSL client CA : No
SSL server : No
SSL server CA : No
Netscape SSL server : No
Netscape SSL server CA : No
S/MIME signing : Yes
S/MIME signing CA : No
S/MIME encryption : No
S/MIME encryption CA : No
CRL signing : No
CRL signing CA : No
Any Purpose : Yes
Any Purpose CA : Yes
OCSP helper : Yes
OCSP helper CA : No
Time Stamp signing : No
Time Stamp signing CA : No
https://serverfault.com/questions/1035044/overriding-ssl-client-no-for-a-specific-nginx-vitual-server

Monday, 26 August 2024

Certificate bag attributes in PEM

 https://stackoverflow.com/questions/72866563/what-are-bag-attributes-in-pemfiles-and-do-they-matter-in-the-file-i-mean-can


ag attributes are generated by OpenSSL during PFX conversion to PKCS#1/PKCS#8 PEM private key files. These attributes are not signed and are completely optional. See this thread for more information: What are bag attributes and how can i generate them?

Thursday, 22 August 2024

Certificate - CSR, CA Signing , how it works -2026 use same ca to renew certificate

 https://www.globalsign.com/en-sg/blog/what-is-a-certificate-signing-request-csr

https://en.wikipedia.org/wiki/Certificate_signing_request

What is certificate sign request(CSR)

Procedure

[edit]

Before creating a CSR for an X.509 certificate, the applicant first generates a key pair, keeping the private key of that pair secret, e.g.:

# https://www.openssl.org/docs/manmaster/man1/openssl-genrsa.html
# "openssl genrsa" creates an RSA private key:

$ openssl genrsa -out 2024_wikipedia.org.key

The CSR contains information identifying the applicant (such as a distinguished name), the public key chosen by the applicant, and possibly further information. When using the PKCS #10 format, the request must be self-signed using the applicant's private key, which provides proof-of-possession of the private key but limits the use of this format to keys that can be used for (some form of) signing. The CSR should be accompanied by a proof of origin (i.e., proof of identity of the applicant) that is required by the certificate authority, and the certificate authority may contact the applicant for further information.

Typical information required in a CSR (sample column from sample X.509 certificate). Note that there are often alternatives for the Distinguished Names (DN), the preferred value is listed.

DN[2]InformationDescriptionSample
CNCommon NameThis is fully qualified domain name that you wish to secure*.wikipedia.org
OOrganization NameUsually the legal name of a company or entity and should include any suffixes such as Ltd., Inc., or Corp.Wikimedia Foundation, Inc.
OUOrganizational UnitInternal organization department/division nameIT
LLocalityTown, city, village, etc. nameSan Francisco
STStateProvince, region, county or state. This should not be abbreviated (e.g. West Sussex, Normandy, New Jersey).California
CCountryThe two-letter ISO code for the country where your organization is locatedUS
EMAILEmail AddressThe organization contact, usually of the certificate administrator or IT department

This sample command line uses the details as listed in the table above:

# https://www.openssl.org/docs/manmaster/man1/openssl-req.html
# "openssl req" creates a signing request:

$ openssl req -sha512 -new -subj "/C=US/ST=California/L=San Francisco/O=Wikimedia Foundation, Inc./CN=*.wikipedia.org" -key 2024_wikipedia.org.key -out 2024_wikipedia.org.csr



This CSR(cert) need to signed by CA's private key , so installed browser has CA' public key to verify CA, then browser send receive server certificate, create a session key using server public key:
https://www.blogger.com/blog/post/edit/2746942211977437381/9146858904040583558

The client sends session key information, encrypted with the server's public key.

The server decrypts the data sent using its private key. The data is used to create a symmetric key.

The client sends its digital certificate, which includes its public key.

The client sends "Certificate Verify" message, which is signed using its private key.

The server verifies the client's certificate and "Certificate Verify" message using the client's public key.

The client and the server exchange "Finished" messages, encrypted with the symmetric key.



CA sign CSR

https://www.ibm.com/docs/en/license-metric-tool?topic=certificate-step-2-signing-certificates


openssl x509 -signkey path_to_CA_key.key -days 

number_of_days -req -in path_to_CA_csr.csr 

-out CA_certificate_name.arm -sha256


---------------------------
renewal

Most common/simple renewal:

existing files:
server.key
server.crt
ca.crt
ca.key
server.ext

Option 1: Renew using the same private key

This keeps the same server.key.

1. Create a new CSR from the existing private key

openssl req -new -key server.key -out server-renew.csr

Use the same Common Name as before, for example:

mydomain.com

2. Sign it again with your CA

openssl x509 -req \
-in server-renew.csr \
-CA ca.crt \
-CAkey ca.key \
-CAcreateserial \
-out server-renew.crt \
-days 365 \
-sha256 \
-extfile server.ext

Now replace the old cert:

cp server.crt server.crt.bak
cp server-renew.crt server.crt

Your pair is still:

server.crt
server.key

Then restart/reload your server.


------need a server.ext


server.ext can be just:

authorityKeyIdentifier=keyid,issuer
basicConstraints=CA:FALSE
keyUsage=digitalSignature,keyEncipherment
extendedKeyUsage=serverAuth
subjectAltName=@alt_names

[alt_names]
IP.1=160.223.160.145

You only add DNS lines when clients connect using a domain name, for example:

[alt_names]
DNS.1=example.com
DNS.2=www.example.com
IP.1=160.223.160.145

So the rule is:

Client connects to https://160.223.160.145  -> use IP.1
Client connects to https://example.com -> use DNS.1

make p12:

openssl pkcs12 -export -in jxiangclientdev.crt -inkey jxiangclientdev.key -out jxiangclientdev.key.p12

Wednesday, 14 August 2024

openssl get private and public key from pk12

 

https://stackoverflow.com/questions/9497719/extract-public-private-key-from-pkcs12-file-for-later-use-in-ssh-pk-authenticati

open ssl use pk12 to get pem and privat key 


PKCS#1 Private key


openssl pkcs12 -in yourP12File.pfx -nocerts -out privateKey.pem




Certificates:


openssl pkcs12 -in yourP12File.pfx -clcerts -nokeys -out publicCert.pem

Friday, 31 May 2024

servicenow MTLS KIX path building failed || fatal alert: bad_certificate

  PKIX path building failed:

https://support.servicenow.com/kb?id=kb_article_view&sysparm_article=KB0521761


check server cert returned

openssl s_client -connect IP:port -status -showcerts </dev/null


For new root CA: 

Create protocol profile


add root CA certificate(subca2001)


addpk12 signed by above rootCA 

link protocol profile with pk12

            reassocated protocol porfile with mtls rest end point



https://stackoverflow.com/questions/11799733/received-fatal-alert-bad-certificate

Received fatal alert: bad_certificate


check openssl s_client -connect IP:port -status -showcerts </dev/null
acceptable client CA

its likely sever rejects client pk12 u present becaue CA signed pk12 is not in the acceptable client ca

Thursday, 16 May 2024

openssl cms verfifcation using ca public key && truncate 16 bytes

https://security.stackexchange.com/questions/273870/openssl-cms-sign-and-verify

 openssl cms -verify -nosmimecap -CAfile <rootCA.pem> -in <signature> -content <content> -inform DER -binary| grep Verification


// Truncate 16 bytes

https://stackoverflow.com/questions/60658407/bash-command-to-truncate-bytes-off-end-of-file

// changes original file

truncate -s -16 myfile.json

Monday, 13 May 2024

Open ssl how to create and verify signature, also how to use go lang to verify

create private key:

openssl genpkey -algorithm RSA -out private_key.pem

 

create signature with prviate key:

openssl dgst -sha256 -sign private_key.pem -out file.sig data_to_sign.txt


verify signature with public key:
openssl dgst -sha256 -verify public_key.pem -signature file.sig data_to_sign.txt


simple golang function to do the above:
package main

import (
    "fmt"
    "os/exec"
)

func verifySignature(publicKeyFile, signatureFile, dataFile string) error {
    cmd := exec.Command("openssl", "dgst", "-sha256", "-verify", publicKeyFile, "-signature", signatureFile, dataFile)
    out, err := cmd.CombinedOutput()
    if err != nil {
        return fmt.Errorf("error verifying signature: %v\nOutput: %s", err, out)
    }
    // Check if the output contains "OK"
    if bytes.Contains(out, []byte("OK")) {
        return true, nil
    }
    // If "OK" is not found in the output, return false
    return false, nil

    fmt.Println("Signature verification result:", string(out))
    return nil
}

func main() {
    publicKeyFile := "public_key.pem"
    signatureFile := "file.sig"
    dataFile := "data_to_verify.txt"

    if err := verifySignature(publicKeyFile, signatureFile, dataFile); err != nil {
        fmt.Println("Error:", err)
        return
    }
    fmt.Println("Signature verification successful.")
}


Saturday, 23 March 2024

Create ca and generate ca signed (self singed cert) - create certs for server and client


https://stackoverflow.com/questions/10175812/how-to-generate-a-self-signed-ssl-certificate-using-openssl

Modern browsers now throw a security error for otherwise well-formed self-signed certificates if they are missing a SAN (Subject Alternate Name)

OpenSSL does not provide a command-line way to specify this, so many developers' tutorials and bookmarks are suddenly outdated.

The quickest way to get running again is a short, stand-alone conf file:

  1. Create an OpenSSL config file (example: req.cnf)

    [req]
    distinguished_name = req_distinguished_name
    x509_extensions = v3_req
    prompt = no
    [req_distinguished_name]
    C = US
    ST = VA
    L = SomeCity
    O = MyCompany
    OU = MyDivision
    CN = www.company.com
    [v3_req]
    keyUsage = critical, digitalSignature, keyAgreement
    extendedKeyUsage = serverAuth
    subjectAltName = @alt_names
    [alt_names]
    DNS.1 = www.company.com
    DNS.2 = company.com
    DNS.3 = company.net
    
  2. Create the certificate referencing this config file

    openssl req -x509 -nodes -days 730 -newkey rsa:2048 \
     -keyout cert.key -out cert.pem -config req.cnf -sha256

https://www.openssl.org/docs/man1.0.2/man1/x509.html


-CAcreateserial

with this option the CA serial number file is created if it does not exist: it will contain the serial number "02" and the certificate being signed will have the 1 as its serial number. Normally if the -CA option is specified and the serial number file does not exist it is an error.




 https://arminreiter.com/2022/01/create-your-own-certificate-authority-ca-using-openssl/


Using OpenSSL to create our CA

Step 1: Create a private key for the CA

Note: we will encrypt the key with AES because if anyone gets access to the key this person can create signed, trusted certificates. Encrypting the key adds some protection (use a 20+ password).

CANAME=MyOrg-RootCA
# optional
mkdir $CANAME
cd $CANAME
# generate aes encrypted private key
openssl genrsa -aes256 -out $CANAME.key 4096

Step 2: Create Certificate of the CA

# create certificate, 1826 days = 5 years
# the following will ask for common name, country, ...
openssl req -x509 -new -nodes -key $CANAME.key -sha256 -days 1826 -out $CANAME.crt
# ... or you provide common name, country etc. via:
openssl req -x509 -new -nodes -key $CANAME.key -sha256 -days 1826 -out $CANAME.crt -subj '/CN=MyOrg Root CA/C=AT/ST=Vienna/L=Vienna/O=MyOrg'

Step 3: Add the CA certificate to the trusted root certificates

For Windows: Open the .crt file and install it for all users to “Trusted Root Certificate Authorities” (verify it by running certmgr.msc)
if you use Intune: Go to Devices > Configuration Profiles > Create profile > Windows 10 and later, Templates, Trusted certificate > upload the .crt file

For Linux (Ubuntu):

sudo apt install -y ca-certificates
sudo cp $CANAME.crt /usr/local/share/ca-certificates
sudo update-ca-certificates

Linux (Fedora/CentOS):

sudo cp $CANAME.crt /etc/pki/ca-trust/source/anchors/$CANAME.crt
sudo update-ca-trust

is by sure also possible for Android, iOS, macOS, … => internet will help ðŸ˜‰

Step 4: Create a certificate for the webserver

MYCERT=myserver
openssl req -new -nodes -out $MYCERT.csr -newkey rsa:4096 -keyout $MYCERT.key -subj '/CN=My Firewall/C=AT/ST=Vienna/L=Vienna/O=MyOrg'
# create a v3 ext file for SAN properties
cat > $MYCERT.v3.ext << EOF
authorityKeyIdentifier=keyid,issuer
basicConstraints=CA:FALSE
keyUsage = digitalSignature, nonRepudiation, keyEncipherment, dataEncipherment
subjectAltName = @alt_names
[alt_names]
DNS.1 = myserver.local
DNS.2 = myserver1.local
IP.1 = 192.168.1.1
IP.2 = 192.168.2.1
EOF

Note: the v3.ext file contains the properties of the v3 extension of certificates. This includes especially the SAN (subject alternative names) which contains the information about DNS or IP, which the browser needs to trust the certificate (you somehow need to make sure, that mysite.local uses the certificate that was issued for mysite.local)

Step 5: Sign the certificate

openssl x509 -req -in $MYCERT.csr -CA $CANAME.crt -CAkey $CANAME.key -CAcreateserial -out $MYCERT.crt -days 730 -sha256 -extfile $MYCERT.v3.ext

Step 6: Deploy the certificate

no explanation here, as it depends on the server.

Source/Command Recap

All commands collected in one code block:

CANAME=MyOrg-RootCA
# optional, create a directory
mkdir $CANAME
cd $CANAME
# generate aes encrypted private key
openssl genrsa -aes256 -out $CANAME.key 4096
# create certificate, 1826 days = 5 years
openssl req -x509 -new -nodes -key $CANAME.key -sha256 -days 1826 -out $CANAME.crt -subj '/CN=My Root CA/C=AT/ST=Vienna/L=Vienna/O=MyOrganisation'
# create certificate for service
MYCERT=myserver.local
openssl req -new -nodes -out $MYCERT.csr -newkey rsa:4096 -keyout $MYCERT.key -subj '/CN=My Firewall/C=AT/ST=Vienna/L=Vienna/O=MyOrganisation'
# create a v3 ext file for SAN properties
cat > $MYCERT.v3.ext << EOF
authorityKeyIdentifier=keyid,issuer
basicConstraints=CA:FALSE
keyUsage = digitalSignature, nonRepudiation, keyEncipherment, dataEncipherment
subjectAltName = @alt_names
[alt_names]
DNS.1 = myserver.local
DNS.2 = myserver1.local
IP.1 = 192.168.1.1
IP.2 = 192.168.2.1
EOF
openssl x509 -req -in $MYCERT.csr -CA $CANAME.crt -CAkey $CANAME.key -CAcreateserial -out $MYCERT.crt -days 730 -sha256 -extfile $MYCERT.v3.ext