[Solved] IPSec config with certificates

I would like to setup a RB493 with IPSec tunnels using certificates rather than shared keys. One end of the tunnel will be a SmoothWall firewall (linux based) that I have configured several different vendors IPSec products to connect to. All of the other tunnels are using pre-shared keys.

So far this is what I have:

SmoothWall has a CA that I used to sign a new key.
cacert.pem, mikrotik_cert.pem, and mikrotik_key.pem created
All .pem files FTPed to routerboard.
/certificate import - cacert.pem imported as cert1, mikrotik_cert.pem and mikrotik_key.pem imported as cert2
/ip ipsec peer created with public IP of SmoothWall and auth-method=rsa-signature, certificate=cert2, remote-certificate=cert1

It will be a day or two before I can connect this RB to the internet to test my setup, but can someone who has IPSec with certificates setup tell me if I missed any important steps.

Thanks!

Hello,

You have to create a proposal in which you specify which encryption type you want to use and also a policy, so the router will know which traffic should pass through IPSec.

Also, keep in mind your natting rules… take a look to Mikrotik’s packet flow if you need some help with that:

http://www.mikrotik.com/testdocs/ros/3.0/qos/flow.php

Bye

I did not mention it, but I do have a proposal and a policy. I will check into the NAT rules. From searching the forums some comments would imply that the IPSec layer operates before routing. I took this to mean that it operates before NAT as well. I will check out the link and see if I can figure it out.

Thanks!

Ipsec encryption is after NAT, look at this diagram.
http://wiki.mikrotik.com/wiki/Packet_Flow#IPsec_encryption

Great!

I didn’t know about all those flow diagrams!! :open_mouth:

Thanks!

I need more help getting my IPSec going.

From my SmoothWall I can see the connection start, but it never completes.

Apr 27 09:28:05 s_sys@smoothwall pluto[5756] added connection description conn242
Apr 27 09:28:23 s_sys@smoothwall pluto[5756] conn242[1] xxx.xxx.xxx.5 #430291: responding to Main Mode from unknown peer xxx.xxx.xxx.5
Apr 27 09:28:23 s_sys@smoothwall pluto[5756] conn242[1] xxx.xxx.xxx.5 #430291: transition from state STATE_MAIN_R0 to state STATE_MAIN_R1
Apr 27 09:28:23 s_sys@smoothwall pluto[5756] conn242[1] xxx.xxx.xxx.5 #430291: STATE_MAIN_R1: sent MR1, expecting MI2
Apr 27 09:28:23 s_sys@smoothwall pluto[5756] conn242[1] xxx.xxx.xxx.5 #430291: transition from state STATE_MAIN_R1 to state STATE_MAIN_R2
Apr 27 09:28:23 s_sys@smoothwall pluto[5756] conn242[1] xxx.xxx.xxx.5 #430291: STATE_MAIN_R2: sent MR2, expecting MI3
Apr 27 09:29:33 s_sys@smoothwall pluto[5756] conn242[1] xxx.xxx.xxx.5 #430291: max number of retransmissions (2) reached STATE_MAIN_R2
Apr 27 09:29:33 s_sys@smoothwall pluto[5756] conn242[1] xxx.xxx.xxx.5: deleting connection "conn242" instance with peer 72.155.176.5 {isakmp=#0/ipsec=#0}

On the RB I see the connection attempt start:

[admin@test] /ip ipsec> installed-sa print
Flags: A - AH, E - ESP, P - pfs 
 0 E  spi=0 src-address=xxx.xxx.xxx.5 dst-address=xxx.xxx.xxx.11 auth-algorithm=none enc-algorithm=none replay=0 state=larval add-lifetime=0s/30s 
      use-lifetime=0s/0s lifebytes=0/0

Here are the IPSec settings:

[admin@test] /ip ipsec> policy print
Flags: X - disabled, D - dynamic, I - inactive 
 0   src-address=192.168.253.0/24:any dst-address=192.168.1.0/24:any protocol=all action=encrypt level=require ipsec-protocols=esp tunnel=yes 
     sa-src-address=0.0.0.0 sa-dst-address=xxx.xxx.xxx.11 proposal=default priority=0



[admin@test] /ip ipsec> peer print
Flags: X - disabled 
 0   address=xxx.xxx.xxx.11/32:500 auth-method=rsa-signature certificate=cert2 remote-certificate=cert1 generate-policy=no exchange-mode=main 
     send-initial-contact=yes nat-traversal=no proposal-check=obey hash-algorithm=sha1 enc-algorithm=aes-256 dh-group=modp1024 lifetime=1h 
     lifebytes=0 dpd-interval=disable-dpd dpd-maximum-failures=5

Can someone help me find my problem?

Thanks!

I had to sort out a RouterOS scripting issue before continuing with the IPSec config.

Due to the lack of information on running an IPSec VPN with RouterOS and x.509 certificates I decided to take a step back and see if I could get things setup with pre-shared keys.

After changing my /ip ipsec peer configuration to use pre-shared keys I was able to bring the tunnel up and ping through the tunnel to hosts at each end. Ok. So I know I can setup PSK IPSec tunnels between the SmoothWall and RouterOS. Next is moving the setup to x.509. I am still stumped. The configuration is identical to what I posted earlier in this thread. If anyone has any hints or ideas I am open to hearing them.

Thanks!

I have been reading up on the OpenSSL and converting certificates between formats. I think the problem I am having is that I am unable to decrypt my private key for the RouterOS cert.

I have my ca public key in file cacert.pem. This is imported into the router.

I have the public cert of my firewall (remote end of IPSec tunnel) in pem format. File-name: smoothwall.pem

I have a signed certificate and key in pkcs12 format. File-name: mikrotik.p12 I also have this in separate pem format cert and key files. File-names: mikrotik_cert.pem, mikrotik_key.pem

I can import the ca cert, the smoothwall cert, and the mikrotik cert and key. I have been unable to decrypt the key. I have tried both 3des and des encryption on the key. Both fail.

Can someone tell me the correct format of the certificate and key for RouterOS?

Thanks!

Hi!

Both the CA cert and MK cert’s that I’ve used for a MK to MK IPSec VPN where created with XCA or easy-rsa (from the OpenVPN package) and exported in PEM format (base64 encoded files).

After importing the certificates in the MK, you have to decrypt them with the decrypt command and the password you set when exporting the Priv key. If everything is ok, “certificate print” will show a KR for the CA cert and a D for the MK cert.

bye!

I think I have followed those steps except I used our company’s CA from the SmoothWall firewall. Here are the exact steps I used:

  1. Create a new signed certificate from the company’s CA.
  2. Export certificate and key as pkcs12 file
  3. Use OpenSSL to convert pkcs12 to pem format
  4. Import pem file into RouterOS
  5. Try to decrypt private key and it fails

I can decrypt the private key with OpenSSL without a problem. I have tried with the key encrypted with both des and 3des.

Does anyone know how to generate some sort of logging for the certificate operations? When I run decrypt it gives me almost no output.

Thanks!

This is the output from RouterOS when I try to import and decrypt my signed certificate and key. Maybe someone can see my problem.

[admin@test] /certificate> print
Flags: K - decrypted-private-key, Q - private-key, R - rsa, D - dsa 
[admin@test] /certificate> import
passphrase: *********
     certificates-imported: 1
     private-keys-imported: 1
            files-imported: 1
       decryption-failures: 0
  keys-with-no-certificate: 0

[admin@test] /certificate> print
Flags: K - decrypted-private-key, Q - private-key, R - rsa, D - dsa 
 0 QR name="cert1" subject=***,CN=mikrotik1 
      issuer=CN=SmoothWall,***
      serial-number="1C" email=mikrotik1@example.com 
      invalid-before=apr/29/2009 18:57:29 invalid-after=apr/28/2013 18:57:29 ca=yes 
[admin@test] /certificate> decrypt
passphrase: **********
  keys-decrypted: 0

[admin@test] /certificate>

are you running v3.23 ? I think there was a fix related to this in v3.23

I am running 3.22. I will troll the docs and see if there is a way to do a remote upgrade. If not then it will be a day or two until I can get upgrade done.

Thanks!

http://wiki.mikrotik.com/wiki/Upgrading_RouterOS

After the upgrade to 3.23 things are looking up!

[admin@test] /certificate> print
Flags: K - decrypted-private-key, Q - private-key, R - rsa, D - dsa 
 0 QR name="cert1" subject=***,CN=mikrotik1 
      issuer=CN=SmoothWall,***
      serial-number="1C" email=mikrotik1@example.com 
      invalid-before=apr/29/2009 18:57:29 invalid-after=apr/28/2013 18:57:29 ca=yes 
[admin@test] /certificate> remove 0
[admin@test] /certificate> import
passphrase: *********
     certificates-imported: 1
     private-keys-imported: 1
            files-imported: 1
       decryption-failures: 0
  keys-with-no-certificate: 0

[admin@test] /certificate> print
Flags: K - decrypted-private-key, Q - private-key, R - rsa, D - dsa 
 0 KR name="cert1" subject=***,CN=mikrotik1 
      issuer=CN=SmoothWall,***
      serial-number="1C" email=mikrotik1@example.com 
      invalid-before=apr/29/2009 18:57:29 invalid-after=apr/28/2013 18:57:29 ca=yes 
[admin@test] /certificate> set 0 ca=no
[admin@test] /certificate>

Now RouterOS thinks the private key is decrypted. I am going to try setting up the IPSec tunnel again.

Thanks!

I am still having some issues, but I think they may be firewall related. Here is the log from the SmoothWall end:

Apr 30 16:31:55 s_sys@smoothwall pluto[6191] conn242[2] 65.12.104.225 #5820: max number of retransmissions (2) reached STATE_MAIN_R2
Apr 30 16:31:55 s_sys@smoothwall pluto[6191] conn242[2] 65.12.104.225: deleting connection "conn242" instance with peer 65.12.104.225 {isakmp=#0/ipsec=#0}
Apr 30 16:34:44 s_sys@smoothwall pluto[6191] conn242[3] 65.12.104.225 #5840: responding to Main Mode from unknown peer 65.12.104.225
Apr 30 16:34:44 s_sys@smoothwall pluto[6191] conn242[3] 65.12.104.225 #5840: transition from state STATE_MAIN_R0 to state STATE_MAIN_R1
Apr 30 16:34:44 s_sys@smoothwall pluto[6191] conn242[3] 65.12.104.225 #5840: STATE_MAIN_R1: sent MR1, expecting MI2
Apr 30 16:34:45 s_sys@smoothwall pluto[6191] conn242[3] 65.12.104.225 #5840: transition from state STATE_MAIN_R1 to state STATE_MAIN_R2
Apr 30 16:34:45 s_sys@smoothwall pluto[6191] conn242[3] 65.12.104.225 #5840: STATE_MAIN_R2: sent MR2, expecting MI3
Apr 30 16:35:16 s_sys@smoothwall pluto[6191] conn242[3] 65.12.104.225 #5840: ERROR: asynchronous network error report on ethB (sport=500) for message to 65.12.104.225 port 500, complainant 65.12.104.225: Connection refused [errno 111, origin ICMP type 3 code 3 (not authenticated)]
Apr 30 16:35:55 s_sys@smoothwall pluto[6191] conn242[3] 65.12.104.225 #5840: max number of retransmissions (2) reached STATE_MAIN_R2
Apr 30 16:35:55 s_sys@smoothwall pluto[6191] conn242[3] 65.12.104.225: deleting connection "conn242" instance with peer 65.12.104.225 {isakmp=#0/ipsec=#0}

Tomorrow I will start Googling for this error. I have the first firewall rule set to accept any packets from the remote ipsec ip to the input chain. Beyond that I am not sure what might be causing my problem.

If anyone has any ideas I would appreciate it.

Thanks!

I now have an IPSec tunnel between a SmoothWall firewall and a routerboard. The setup is not yet workable for me though. I created two new signed certificates. For each certificate I used the public IP. I then setup those two certificates in RouterOS and the SmoothWall. Once the setup was complete I simply started a ping from one end of the tunnel to the other. The tunnel came up quickly and it has been up for a little over an hour.

The problem with this setup is that my DSL IP changes frequently. I wish the docs for mikrotik said what it presented as an ID during the IPSec negotiation. I think it just uses the subject of the certificate.

I will be trying some other options for the certificate.

Thanks!

I have made more progress.

One end of the tunnel (SmoothWall) is on a public static ip that has forward and reverse DNS set correctly. The RouterOS end is on a DSL connection with an IP that changes frequently.

I created a new certificate for the SmoothWall that used its public IP as the ID and CN. I already had a certificate for RouterOS that used mikrotik as the ID and CN. With these two certificates I am able to bring the tunnel up.

For the SmoothWall end, I set it to accept a user input certificate subject and used “mikrotik” to match the certificate in RouterOS. I imported both the mikrotik certificate and the certificate form the smoothwall.

This morning I tried a new test and created a certificate that used the FQDN of my smoothwall as the CN and ID of the certificate. I am able to bring the tunnel up with that certificate. The certificate for the mikrotik does not include or need its IP address.

I now have the IPSec tunnel configured and working 100% with x.509 certificates.

Thanks everyone for your help.

Could you please give us some more details to your setup?
Please post the proposal and the policy.
I currently stuck in transforming freeswan VPN with certificates to Mikrotik Certificate VPN.
The certs are all imported.

Thanks for your help in advance.