Skapa självsignerat certifikat

From Linuxwiki
Jump to navigation Jump to search

Självsignerat certifikat

Denna information kommer delvis från http://apache-ssl.org

Börja med att skapa nyckel och request:

openssl req -new > new.cert.csr
Generating a 1024 bit RSA private key
....++++++
.++++++
writing new private key to 'privkey.pem'
Enter PEM pass phrase: <måste vara minst 4 tecken>
Verifying - Enter PEM pass phrase:
-----
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [AU]:SE
State or Province Name (full name) [Some-State]:Kalmar
Locality Name (eg, city) []:Kalmar
Organization Name (eg, company) [Internet Widgits Pty Ltd]:ECS
Organizational Unit Name (eg, section) []:ECS
Common Name (eg, YOUR name) []:server.se
Email Address []:

Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:

Nu är nyckeln skapad men det lösenord som man var tvungen att ange ska nu tas bort:

openssl rsa -in privkey.pem -out new.cert.key
Enter pass phrase for privkey.pem:Samma lösen som ovan
writing RSA key

Konvertera detta request till ett självsignerat certifikat:

openssl x509 -in new.cert.csr -out new.cert -req -signkey new.cert.key -days 365
Signature ok
subject=/C=SE/ST=Kalmar/L=Kalmar/O=ECS/OU=ECS/CN=server.se
Getting Private key

I apache måste man använda dessa parametrar för att peka ut sitt certifikat (visar mer om det nedan):

SSLCertificateFile /path/to/certs/new.cert.cert
SSLCertificateKeyFile /path/to/certs/new.cert.key

Exemplet nedan visar hur detta kan göras för en virtuell host, men först kan det vara bra att byta namn på certifikatet till något bättre om man använde new.nånting enligt ovan. Förslag kan vara att kalla filerna för servernamn.nånting. Så här kan det se ut efter byte av namnen:

server.se.cert.cert
server.se.cert.key

Lägg dessa förslagsvis i apaches ssl-katalog vilket t ex kan vara /etc/apache2/ssl.

Sedan kan man inkludera dessa i en virtuell host eller ha som standard för hela servern. Detta är för en virtuell host. Notera att det är här viktigt att namnet stämmer med servernamnet i konfig. Annars ges en varning för det med, förutom att certfikatet inte är utfärdat av behörig godkännare.

<VirtualHost *:443>
 ServerName server.se
 DocumentRoot /var/www/sslmapp/
 ErrorLog /var/log/apache2/sslmapp.log
 CustomLog /var/log/apache2/sslmapp.log combined
 SSLEngine on
 SSLCertificateFile /etc/apache2/ssl/avdelning.se.cert.cert
 SSLCertificateKeyFile /etc/apache2/ssl/avdelning.se.cert.key
<Directory "/var/www/sslmapp">
   Options ExecCGI Includes
   AllowOverride None
   Order allow,deny
   allow from all
</Directory>
</VirtualHost>

Ovanstående är ett exempel där det fetmarkerade är sånt som kan vara bra att ändra på.






Taget från gamla wikiprog

Apache - Skapa egen nyckel

Hur man skapar sin egen SSL-nyckel för apache.

Beskrivning

Direkt från http://apache-ssl.org

Now I've got my server installed, how do I create a test certificate?

Step one - create the key and request:

 openssl req -new > new.cert.csr

Step two - remove the passphrase from the key (optional):

 openssl rsa -in privkey.pem -out new.cert.key

Step three - convert request into signed cert:

  openssl x509 -in new.cert.csr -out new.cert.cert -req -signkey new.cert.key -days 365

The Apache-SSL directives that you need to use the resulting cert are:

 SSLCertificateFile /path/to/certs/new.cert.cert
 SSLCertificateKeyFile /path/to/certs/new.cert.key

Efter att det är klart, infoga konfigurationen i VirtualHost. Det behövs alltså en nyckel för varje VirtualHost.

<VirtualHost *:443>
  ServerName avdelning.se
  DocumentRoot /var/www/html/avdelning
  ErrorLog /var/log/apache2/avdelning-error.log
  CustomLog /var/log/apache2/avdelning-access.log combined
  SSLEngine on
  SSLCertificateFile /etc/apache2/ssl/avdelning.se.cert.cert
  SSLCertificateKeyFile /etc/apache2/ssl/avdelning.se.cert.key
</VirtualHost>

Nedan är för Certifikat för klient How do I create a client certificate?

Step one - create a CA certificate/key pair, as above.

Step two - sign the client request with the CA key:

 openssl x509 -req -in client.cert.csr -out client.cert.cert -signkey my.CA.key -CA my.CA.cert -CAkey my.CA.key -CAcreateserial -days 365

Step three - issue the file 'client.cert.cert' to the requester.

The Apache-SSL directives that you need to validate against this cert are:

 SSLCACertificateFile /path/to/certs/my.CA.cert
 SSLVerifyClient 2

Example

root@hapenny:/etc/apache2# openssl req -new > avdelning.se
Generating a 1024 bit RSA private key
...++++++
.........................................................................................++++++
writing new private key to 'privkey.pem'
Enter PEM pass phrase:
Verifying - Enter PEM pass phrase:
-----
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [AU]:SE
State or Province Name (full name) [Some-State]:SE
Locality Name (eg, city) []:SE
Organization Name (eg, company) [Internet Widgits Pty Ltd]:SE
Organizational Unit Name (eg, section) []:SE
Common Name (eg, YOUR name) []:avdelning.se
Email Address []:

Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:
root@hapenny:/etc/apache2# openssl rsa -in privkey.pem -out avdelning.se.cert.key
Enter pass phrase for privkey.pem:
unable to load Private Key
13949:error:06065064:digital envelope routines:EVP_DecryptFinal_ex:bad decrypt:evp_enc.c:454:
13949:error:0906A065:PEM routines:PEM_do_header:bad decrypt:pem_lib.c:425:
root@hapenny:/etc/apache2# openssl rsa -in privkey.pem -out avdelning.se.cert.key
Enter pass phrase for privkey.pem:
writing RSA key
root@hapenny:/etc/apache2# openssl x509 -in avdelning.se.csr -out avdelning.se.cert.cert -req -signkey avdelning.se.cert.key -days 365
Signature ok
subject=/C=SE/ST=SE/L=SE/O=SE/OU=SE/CN=avdelning.se
Getting Private key
root@hapenny:/etc/apache2#