Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Integralist/94be4633641bc644ef3b9cb50d8926fb to your computer and use it in GitHub Desktop.
Save Integralist/94be4633641bc644ef3b9cb50d8926fb to your computer and use it in GitHub Desktop.
[OpenSSL Generate Certificate for Code Signing] #openssl #cert #codesign #AI

NOTE: The explanation of the code was auto-generated by AI (so some of the explanations could be incorrect).

openssl genrsa -des3 -out rootCA.key 4096

This code generates a new RSA private key with a length of 4096 bits and encrypts it using the Triple DES algorithm with a passphrase. The private key is saved in a file named rootCA.key. This command is commonly used to generate a root certificate authority (CA) key, which is used to sign and issue digital certificates for other entities.

openssl req -x509 -new -nodes -key rootCA.key -sha256 -days 1024 -out rootCA.crt

This code generates a self-signed X.509 certificate using the RSA private key stored in the rootCA.key file. The certificate is valid for 1024 days and is saved in the rootCA.crt file. The -x509 option specifies that a self-signed certificate should be generated, while the -new option indicates that a new certificate request should be created. The -nodes option specifies that the private key should not be encrypted. The -sha256 option specifies the hash algorithm to use for the certificate. This command is commonly used to generate a root certificate authority (CA) certificate, which is used to sign and issue digital certificates for other entities.

cat << EOF > code_sign_csr.conf
[ req ]
default_bits  = 2048             # RSA key size
encrypt_key   = yes              # Protect private key
default_md    = sha256           # MD to use
utf8          = yes              # Input is UTF-8
string_mask   = utf8only         # Emit UTF-8 strings
prompt        = yes              # Prompt for DN
distinguished_name = codesign_dn # DN template
req_extensions = codesign_reqext # Desired extensions

[ codesign_dn ]
commonName      = the-company.com
commonName_max  = 64

[ codesign_reqext ]
keyUsage        = critical,digitalSignature
extendedKeyUsage = critical,codeSigning
subjectKeyIdentifier = hash
EOF

This code creates a configuration file named code_sign_csr.conf with settings for generating a certificate signing request (CSR) for code signing purposes. The configuration file specifies the default RSA key size of 2048 bits, the SHA-256 message digest algorithm, and UTF-8 encoding. It also prompts for the distinguished name (DN) and specifies the desired extensions for the CSR. The codesign_dn section specifies the common name for the certificate as the-company.com, while the codesign_reqext section specifies the key usage, extended key usage, and subject key identifier for the certificate. This configuration file can be used with the openssl req command to generate a CSR for code signing purposes.

openssl req -new -newkey rsa:2048 -keyout testsign.key -sha256 -nodes -out testsign.csr -subj "/CN=The Company Engineering Code Sign Cert" -config code_sign_csr.conf

This code generates a new RSA private key with a length of 2048 bits and a new certificate signing request (CSR) using the openssl req command. The private key is saved in a file named testsign.key, and the CSR is saved in a file named testsign.csr. The -new option specifies that a new CSR should be created, while the -newkey option specifies that a new private key should be generated. The -nodes option specifies that the private key should not be encrypted. The -sha256 option specifies the hash algorithm to use for the CSR. The -subj option specifies the subject of the CSR, which includes the common name The Company Engineering Code Sign Cert. The -config option specifies the configuration file to use for the CSR, which is code_sign_csr.conf. This command is commonly used to generate a CSR for code signing purposes.

cat << EOF > code_sign_cert.conf
authorityKeyIdentifier=keyid,issuer
basicConstraints=CA:FALSE
subjectAltName = @alt_names
[alt_names]
DNS.1 = fastly.com
EOF

This code creates a configuration file named code_sign_cert.conf with settings for generating a code signing certificate. The configuration file specifies the authority key identifier, basic constraints, and subject alternative name (SAN) for the certificate. The authorityKeyIdentifier option specifies the key identifier and issuer of the certificate authority (CA) that issued the certificate. The basicConstraints option specifies that the certificate is not a CA. The subjectAltName option specifies the SAN for the certificate, which is defined in the alt_names section. In this case, the SAN is a DNS name fastly.com. This configuration file can be used with the openssl x509 command to generate a code signing certificate.

openssl x509 -req -CA rootCA.crt -CAkey rootCA.key -in testsign.csr -out testsign.crt -days 365 -CAcreateserial -extfile code_sign_cert.conf

This code generates a code signing certificate using the openssl x509 command. The certificate is generated by signing the certificate signing request (CSR) stored in the testsign.csr file with the root certificate authority (CA) key stored in the rootCA.key file. The resulting certificate is saved in the testsign.crt file. The -req option specifies that a CSR is being used as input. The -CA and -CAkey options specify the root CA certificate and key to use for signing the CSR. The -days option specifies the validity period of the certificate, which is set to 365 days. The -CAcreateserial option specifies that a serial number file should be created for the CA. The -extfile option specifies the configuration file to use for the certificate, which is code_sign_cert.conf. This command is commonly used to generate a code signing certificate for signing software or code.

openssl x509 -in testsign.crt -noout -text
openssl x509 -in rootCA.crt -noout -text

These commands display the details of the X.509 certificates stored in the testsign.crt and rootCA.crt files, respectively. The -in option specifies the input file containing the certificate. The -noout option specifies that no output should be generated other than the certificate details. The -text option specifies that the certificate should be displayed in human-readable text format. The output includes information such as the certificate version, serial number, signature algorithm, issuer and subject distinguished names, validity period, public key information, and any extensions included in the certificate. These commands are commonly used to inspect the details of X.509 certificates.

openssl pkcs12 -export -out testsign.p12 -inkey testsign.key -in testsign.crt
openssl pkcs12 -export -out rootCA.p12 -inkey rootCA.key -in rootCA.crt

These commands create PKCS#12 files containing the private key and certificate for the testsign and rootCA entities, respectively. The -export option specifies that a PKCS#12 file should be created. The -out option specifies the output file name. The -inkey option specifies the input file containing the private key. The -in option specifies the input file containing the certificate. The resulting PKCS#12 file contains the private key and certificate in a password-protected format. These commands are commonly used to export a private key and certificate for use in other systems or applications.

UPDATE

I asked AI to generate code to produce a Software Publishing Certificate (spc) and it gave me back...

openssl req -x509 -newkey rsa:2048 -keyout private_key.pem -out spc.pem -days 365 -nodes

This code generates a self-signed X.509 certificate using the RSA private key generated by the openssl req command. The private key is saved in a file named private_key.pem, and the certificate is saved in a file named spc.pem. The -x509 option specifies that a self-signed certificate should be generated, while the -newkey option specifies that a new private key should be generated. The -nodes option specifies that the private key should not be encrypted. The -days option specifies the validity period of the certificate, which is set to 365 days. This command is commonly used to generate a self-signed certificate for testing or development purposes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment