9
votes

I am generating a self-signed certificate using OpenSSL following the steps here Create PKCS#12 file with self-signed certificate via OpenSSL in Windows for my Android App.

openssl req -x509 -days 365 -subj "/CN=MULTI LINE NEEDED HERE" -newkey rsa:1024 -keyout mycert.pem -out mycert.pem

Here the subject is populated by the prompts by cmd like Country, State etc. I wish to give a multiple line value for CN attribute. How do I do that in command line?

1
"...multiple line value for CN attribute..." - what do you mean here? Do you literally want a CRLF in the Common Name? Or do you want multiple DNS names (example.com, www.example.com, etc) in the Common Name? Or do you want multiple Common Names (2 or 3 instead of 1)?jww
Also, the Common Name should be a "friendly" name like Example, LLC; and not a DNS name like example.com. Using a DNS name in the Common Name field is deprecated by both the IETF and the CA/Browser forums. Put your DNS names in the Subject Alt Names (SAN). There can be multiple SANs.jww
I have edited the question @jww So, CN SHould be like: CN = GOOGLE (ENTER) INCTyler Durden
In that case, probably not with OpenSSL in one command. You can probably do it in three steps: (1) generate the CSR, (2) Edit with Hex Editor, and (3) Sign the CSR. That's an odd requirement. Are you trying to break browsers and other software that presents certificates?jww
@jww what about the case when I want multiple domain names? like www.google.com and www.yahoo.comTyler Durden

1 Answers

30
votes

... what about the case when I want multiple domain names? like www.google.com and www.yahoo.com?

Here's how to add multiple DNS names to a certificate. You have to add them through Subject Alternate Names (SAN).

In your case, add www.google.com and www.yahoo.com under the section alternate_names.

(I'm not sure if this is an answer or a comment. I'm still not clear if you want to attempt to break PKI things by adding the CRLF to the Common Name; or if you only want to add multiple DNS names to a certificate).

First

$ touch example-com.conf

Second

Add the following to the configuration file. Tune it to suit your taste.

[ req ]
default_bits        = 2048
default_keyfile     = server-key.pem
distinguished_name  = subject
req_extensions      = extensions
x509_extensions     = extensions
string_mask         = utf8only

[ subject ]
countryName         = Country Name (2 letter code)
countryName_default     = US

stateOrProvinceName     = State or Province Name (full name)
stateOrProvinceName_default = NY

localityName            = Locality Name (eg, city)
localityName_default        = New York

organizationName         = Organization Name (eg, company)
organizationName_default    = Example, LLC

commonName          = Common Name (e.g. server FQDN or YOUR name)
commonName_default      = Example, LLC

emailAddress            = Email Address
emailAddress_default        = [email protected]

[ extensions ]

subjectKeyIdentifier        = hash
authorityKeyIdentifier  = keyid,issuer

basicConstraints        = CA:FALSE
keyUsage            = nonRepudiation, digitalSignature, keyEncipherment
extendedKeyUsage    = serverAuth
subjectAltName          = @alternate_names
nsComment           = "OpenSSL Generated Certificate"

[ alternate_names ]

DNS.1       = example.com
DNS.2       = www.example.com
DNS.3       = mail.example.com
DNS.4       = ftp.example.com

Third

Generate the certificate with the following. It generates a new key with each request. Tune it to suit your taste. For example, if you omit -x509 you get a CSR rather than a certificate.

$ openssl req -config example-com.conf -new -x509 -newkey rsa:2048 -nodes \
        -keyout example-com.key.pem -days 365 -out example-com.cert.pem

Fourth

Examine the certificate with the following.

$ openssl x509 -in example-com.cert.pem -text -noout

You will see the multiple DNS names in the SAN.

$ openssl x509 -in example-com.cert.pem -text -noout
Certificate:
    Data:
        Version: 3 (0x2)
        Serial Number: 15695764655789201623 (0xd9d28ecb727258d7)
    Signature Algorithm: sha1WithRSAEncryption
        Issuer: C=US, ST=NY, L=New York, CN=Example, LLC/[email protected]
        Validity
            Not Before: May 10 22:34:14 2014 GMT
            Not After : May 10 22:34:14 2015 GMT
        Subject: C=US, ST=NY, L=New York, CN=Example, LLC/[email protected]
        Subject Public Key Info:
            Public Key Algorithm: rsaEncryption
                Public-Key: (2048 bit)
                Modulus:
                    00:9a:a4:93:67:26:79:6f:dd:9a:25:0a:11:66:f6:
                    5a:04:36:66:5f:46:fd:b3:ee:08:ac:e4:92:88:12:
                    92:ea:ec:9b:62:6c:5d:ec:8c:4f:c6:0c:e9:99:c4:
                    77:70:3e:52:fc:25:9d:74:56:2d:49:08:9c:8f:b3:
                    82:ea:9e:b2:60:52:69:59:7f:c8:14:15:74:ef:f6:
                    80:6c:7f:1d:b4:b8:55:89:7f:d7:e4:0a:94:9e:3f:
                    fe:b6:64:fa:7a:9a:f6:43:46:53:f2:e3:b0:8f:92:
                    ca:83:6d:00:1d:57:9a:73:b9:f9:14:11:2b:fc:2d:
                    36:b5:ed:95:73:c3:28:7d:4b:86:97:88:85:cd:d6:
                    32:b7:e3:db:ea:3b:9a:7c:5a:c9:b0:6b:38:ac:e4:
                    44:e6:95:57:6a:f0:05:7a:5c:86:e0:46:4a:83:b2:
                    78:59:38:42:9c:84:c6:8b:ee:04:cc:8b:e5:29:3a:
                    45:6f:0e:8c:cd:6e:35:e9:f8:f7:fb:f2:a5:8a:e2:
                    53:77:b2:59:33:64:9a:1d:98:d6:4e:2d:e6:73:cb:
                    18:fc:86:4f:e7:bf:3b:14:79:92:32:e0:63:40:d3:
                    16:b6:33:f6:42:fc:59:6c:a4:8c:ed:86:68:3e:14:
                    64:da:16:66:85:42:ef:73:8a:c1:f9:6f:a2:b0:92:
                    d0:fb
                Exponent: 65537 (0x10001)
        X509v3 extensions:
            X509v3 Subject Key Identifier: 
                B5:CE:04:1D:D9:10:85:FF:2E:FD:AA:AA:CB:36:7A:1E:14:66:69:EA
            X509v3 Authority Key Identifier: 
                keyid:B5:CE:04:1D:D9:10:85:FF:2E:FD:AA:AA:CB:36:7A:1E:14:66:69:EA
            X509v3 Basic Constraints: 
                CA:FALSE
            X509v3 Key Usage: 
                Digital Signature, Non Repudiation, Key Encipherment
            X509v3 Extended Key Usage: 
                TLS Web Server Authentication
            X509v3 Subject Alternative Name: 
                DNS:example.com, DNS:www.example.com, DNS:mail.example.com, DNS:ftp.example.com
            Netscape Comment: 
                OpenSSL Generated Certificate
    Signature Algorithm: sha1WithRSAEncryption
         32:a0:28:21:8b:a8:46:d5:89:5e:10:b7:b6:35:cb:a8:2e:4a:
         a4:10:3d:4c:1d:3f:8a:b2:7d:2e:53:e5:4f:b5:c7:9c:14:84:
         ad:11:51:01:f2:41:80:6e:23:10:ed:d6:cc:38:48:15:ff:d8:
         17:6c:09:b0:1e:b7:ed:c0:1a:6d:41:74:48:63:05:46:85:61:
         bd:ac:ab:36:0e:70:a1:e5:2b:1d:3b:02:0b:00:31:74:d3:5e:
         0c:88:9b:ac:e8:c7:3d:22:22:90:01:c3:c0:f0:1e:e5:0a:4c:
         b1:0f:ab:6b:39:5c:af:fc:34:53:ef:fd:38:35:9b:15:63:57:
         f9:89:f0:f6:b8:5a:c7:e1:a6:ab:03:b0:b5:5a:c9:f1:b6:02:
         41:ba:f8:5b:58:f4:ed:4c:57:df:69:3f:55:25:57:4d:39:da:
         94:8a:36:27:f1:a8:db:59:c3:47:65:9d:db:7c:5d:0f:39:4a:
         6a:a8:b6:12:7b:2e:41:16:b5:ed:b9:33:aa:a0:74:d3:1c:3f:
         a4:4a:c8:0b:e3:37:13:f0:97:38:c5:4d:6c:62:d5:16:31:c3:
         c9:d9:48:91:d1:e6:3f:33:0e:24:0d:96:80:a3:80:b4:09:32:
         30:4c:e2:c2:d8:d3:3d:76:98:f0:5e:70:e0:ec:4a:ba:2e:97:
         44:4a:75:f2