$Id: USAGE,v 1.19 2006/07/10 16:06:26 sinoue Exp $

This document describes the basic usage to run the Racoon2 system.

1. Recommended system configuration
== ================================

Both NetBSD and FreeBSD have the kernel state, "net.key.blockacq_count" to setup
the behavior how many packet the kernel will block until the suitable SA
will be installed.  The state sometimes disturbs retransmission of the key
exchange message.  We recommend you to set it to zero.

	# sysctl -w net.key.blockacq_count=0

And FreeBSD also has the kernel state, "net.key.preferred_old" to use an
old SA preferred to a new SA.  The state sometimes disturbs interoperability.
We recommend you to set it to zero.

	# sysctl -w net.key.preferred_oldsa=0

2. spmd(8)
== =======

2-1. Setting up spmd
---- ---------------

spmd(8) operates the SPD (Security Policy Database), and receives
various request from other racoon2 key management daemons.  So you must run 
spmd(8) before you run other racoon2 key management daemons.  If spmd(8) 
terminates for some reason, other daemons will terminate.

To setup the configuration of spmd(8), "interface" and "selector" directives
are important.

To specify the I/F between spmd(8) and others daemons, you have to setup
"interface" directive like following:

	interface {
		# specify the I/F
		spmd { unix "/usr/local/run/spmif"; };

		# specify the password file in order to communicate
		# between spmd(8) and otheres.
		# create it by 'pskgen -r -o FILE-NAME' comand and
		# make sure the file permission is 600 and the owner is root.
		spmd_password "/usr/local/etc/secret/spmd.psk";
	};

To specify a security policy, you have to setup a "selector" directive.

	selector A-B-any {
		direction outbound;
		src 10.1.1.1;
		dst 10.2.2.2;
		upper_layer_protocol "any";
		policy_index A-B;
	};
	selector B-A-any {
		direction inbound;
		src 10.2.2.2;
		dst 10.1.1.1;
		upper_layer_protocol "any";
		policy_index A-B;
	};

If you want spmd(8) to behave a DNS proxy in your platform, you have to
setup "resolver" section like following:

	resolver {
		# make spmd(8) as a DNS proxy.
		resolver on;

		# specify the name server address to be sent the query.
		nameserver { 203.119.1.1 port 53; };

		# specify the local address and the port to be listened.
		dns_query { 127.0.0.1 port 53; };
	};

and don't forget to edit /etc/resolv.conf to direct queries from applications
to spmd(8).

3. iked(8)
== =======

3-1. Setting up iked
---- ---------------

o Edit /usr/local/etc/racoon2.conf

  Minimum required information for iked is (replace [...] with
  appropriate string):

  interface {
       ike { [IP_ADDRESSES_LIST_TO_USE_FOR_IKE]; };
  };
  remote [REMOTE_SECTION_INDEX_STRING] {
      ikev2 {
          peers_ipaddr	[PEER_IP_ADDRESS];
	  peers_id	[ID_TYPE] [ID_STRING];
	  my_id		[ID_TYPE] [ID_STRING];
	  kmp_enc_alg	{ [ENCR_ALGS]; };	# 3des_cbc, aes128_cbc, aes192_cbc, aes256_cbc
	  kmp_prf_alg	{ [PRF_ALGS]; };	# hmac_md5, hmac_sha1, aes_xcbc
	  kmp_hash_alg	{ [INTEGRITY_ALGS]; };	# hmac_md5, hmac_sha1, aes_xcbc
	  kmp_dh_group	{ [DH_GROUP_NUMBERS]; }; # modp768, modp1024, modp1536,
						# modp2048, modp3072, modp4096,
						# modp6144, modp8192
						# (or corresponding numbers)
	  kmp_auth_method { [AUTHENTICATION_METHOD]; }; # psk, rsasig, dss

	  # if kmp_auth_method is psk, specify pre_shared_key
	  pre_shared_key  [PRE_SHARED_KEY_FILE_NAME];

	  # if kmp_auth_method is rsasig or dss,
	  # specify my_public_key and peers_public_key
	  my_public_key	   x509pem [X509_PUBKEY_FILE_PATH] [PRIVKEY_FILE_PATH];
	  peers_public_key x509pem [X509_PUBKEY_FILE_PATH] "";
      };
  };

  Also other sections (selector, policy, ipsec, sa) in racoon2.conf
  should be written appropriately.

  Currently, iked cannot handle FQDN for any IP address fields.  You
  can, though, use "fqdn" for peers_id or my_id fields, since they are
  handled as opaque strings.

o Create pre-shared key file for IKE authentication (if necessary)

  If you specified psk for kmp_auth_method, you need to store
  pre-shared key in a file and specify the file name in pre_shared_key
  field of racoon2.conf.  Note that the whole data is used as the pre-shared
  key.  In particular, you should take attention to a newline in the file.
  Currently, you need to specify the file in full path.
  It must be set that the root only access the file.

o Set up certificates and private keys for IKE authentication (if necessary)

  If you specified rsasig or dss for kmp_auth_method, you need to
  specify certificates and private key in my_public_key and
  peers_public_key fields of racoon2.conf.  Currently, you need to
  specify the file in full path.

  Currently, only x509pem type certificate can be used.

o Using NAT-T for IKEv2 (if necessary)

  If you want to use NAT-T for IKEv2, you have to specify the port 4500
  in "interface" directive as follows.

	interface {
		ike {
			MY_IP port 500;
			MY_IP port 4500;
		};
	};

o Using policy generation functionality for IKEv2 (if necessary)

  If you want to generate policy dynamically for IKEv2 responder,
  specify peer addresses as follows.

	remote remote-rw {
		acceptable_kmp { ikev2; };
		ikev2 {
			peers_ipaddr IP_RW;
			...
		}
	}

	policy policy-rw {
		action auto_ipsec;
		remote_index remote-rw;
		peers_sa_ipaddr IP_RW;
		...
	}

  Note that the "default" directive is required too, for policy
  generation to work.

3-2. Starting iked
---- -------------

  spmd must be started before iked.

  To run iked, type iked in command line.  You need a superuser
  privilege.

  Command line options of iked are:
    -f [file]	specifies the configuration file path.
    -p [num]	specifies the port number of isakmp socket.
    -4		restricts to IPv4 only.
    -6		restricts to IPv6 only.
    -d		increases debugging flag
    -D [num]	specifies debugging flag
    -F		foreground mode
    -v		specifies to output log messages to standard error in addition to syslog
    -l [file]	specifies to output log messages to the file instead of syslog
    -h		outputs the list of command line options to standard error, and exits
    -V		outputs the version number to stdandard error

  The parameter for -D option is a number, where each bit represents
  debugging flag as following:
    1		specifies to log debugging messages
    2		specifies to log internal tracing messages
    4		specifies to log configuration parser debugging message
    8		specifies not to call kernel PF_KEY

  Each of command line option -d is equivalent to increasing a debug
  flag from LSB.  For example, -ddd is equivalent to -D 7

3-3. How to create certificate
---- -------------------------

Also see openssl documentation. (openssl/doc/HOWTO/)

3-3-1. Make the PKCS#10 request for getting your certificate, which will
   be sent to the CA.  For example, to create a request with new key
   of RSA 1024 bits key and SHA1 as the signature algorithm:

  % openssl req -new -nodes -newkey rsa:1024 -sha1 -keyform PEM -keyout privkey.pem -outform PEM -out request.pem
  Using configuration from /usr/local/openssl/openssl.cnf
  Generating a 1024 bit RSA private key
  .........................+++++
  ........+++++
  writing new private key to 'privkey.pem'
  -----
  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]:JP
  State or Province Name (full name) [Some-State]:Kanagawa
  Locality Name (eg, city) []:Fujisawa
  Organization Name (eg, company) [Internet Widgits Pty Ltd]:WIDE Project
  Organizational Unit Name (eg, section) []:KAME Project
  Common Name (eg, YOUR name) []:Shoichi Sakane
  Email Address []:sakane@ydc.co.jp

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

3-3-2. Send the PKCS#10 request to the CA. Then retrieve your authorized
   certificate and store it to the certificate repository.

3-3-3. Alternatively, you can sign your certificate by yourself. For
   example, reqest.pem is the PKCS#10 request file in PEM format.

  % openssl x509 -req -in request.pem -signkey privkey.pem -out cert.pem

3-3-4. Issuer's certificate

   You have to put the issuer's certificates into CERT directory. It
   is a unique method to save them in OpenSSL.  For example, suppose
   the filename of the certificate is ssh-ca1.pem.  You have to type
   the following magic word after changing CERT directory.

  % ln -s ssh-ca1.pem `openssl x509 -noout -hash -in ssh-ca1.pem`.0

4. kinkd(8)
== ========

This document assumes you have some knowledge on Kerberos and
you have a working Kerberos environment.  Otherwise you should
consult the documentations of the Kerberos system you are using.

4-1. Setting up as a Kerberos5 principal
---- -----------------------------------

[This section is beyond the scope of this document and the examples here
is just for your convenience.  Please refer the manuals of your
Kerberos5 system.]

o Prepare /etc/krb5.conf. (on all your hosts including KDCs)

  sample /etc/krb5.conf:
	[libdefaults]
		default_realm = EXAMPLE.ORG
	[realms]
		EXAMPLE.ORG = {
			kdc = yourkdc.example.org
		}

o Prepare a working Kerberos5 environment and a KDC.

  example with Heimdal (on your KDC host):
   - create a database and stash file, and set the master key
	# kstash
   - initialize a realm named EXAMPLE.ORG
	# kadmin -l
	kadmin> init EXAMPLE.ORG
   - add admin principal
	# kadmin -l
	kadmin> add root/admin
	# echo "root/admin@EXAMPLE.ORG all" >> /var/heimdal/kadmind.acl

  example with MIT krb5 (on your KDC host):
   - create a database and stash file
	# kdb5_util create -r EXAMPLE.ORG
		(you will be asked master password)
	# kdb5_util stash
   - edit access control list (the file is specified in your kdc.conf)
	# echo "*/admin@EXAMPLE.ORG *" >> /etc/krb5kdc/kadm5.acl
	  Note: this permits the principals of */admin to do everything.
   - add administrative principal
	# kadmin.local
	kadmin.local: addprinc admin/admin@EXAMPLE.ORG

o Prepare a principal and a shared key

  example with Heimdal (on host1.example.org, on which kinkd will run):
	# kadmin -a yourkdc.example.org
	kadmin> add --random-key kink/host1.example.org
	kadmin> ext kink/host1.example.org

  example with MIT krb5 (on host1.example.org):
   - add principals for KINK
	# kadmin -p admin/admin -s kdc.example.org
	kadmin: addprinc -randkey kink/host1.example.org
	kadmin: ktadd kink/host1.example.org

4-2. Setting up racoon2
---- ------------------

o /usr/local/etc/racoon2.conf

  Copy racoon2/samples/racoon2.conf to an appropriate directory
  (default is /usr/local/etc).

  Global settings
   - Your own principal name
	Specify your principal name in default{remote{kink{my_principal}}}.
	If your realm matches the default realm specified in krb5.conf,
	realm name can be omitted.

   - Your recursive DNS resolver
	Specify your recursive DNS resolver in resolver{nameserver}.

  Per-peer settings
   - Traffic selectors
	Specify them in selector{src} and selector{dst}.
	You usually need two selectors, one for inbound SA and one for
	outbound SA.

   - KINK peer's principal name.
	Specify peer's principal name in remote{kink{peers_principal}}.
	If (1) its FQDN is specified in selectors, (2) its form is
	"kink/f.q.d.n", and (3) its realm is the default realm,
	then remote{kink{peers_principal}} can be omitted.

   - selector_index in remote
	If you are the KINK responder, you have to specify a corresponding
	selector_index in remote section.

o /etc/resolv.conf

  Point nameserver to spmd. (specified in resolver{dns_query} section in
  racoon2.conf).
  This change should be performed just before starting spmd, or
  your applications may fail to resolve names.

4-3. Starting programs
---- -----------------

o spmd (Security Policy Management Daemon)

  spmd must be started by root.
  spmd must be started before other key management daemons.

o kinkd (KINK Daemon)

  kinkd must be started by root.
  kinkd must be started after spmd.
  kinkd is terminated by SIGINT or SIGTERM.
  kinkd will reload racoon2.conf by SIGHUP.

4-4. Quick configuration guide
---- -------------------------

 ***************************************************************
  SECURITY WARNING
 ***************************************************************
  Don't use the configuration type "C) With DNS FQDN names",
  unless you know what you are doing and accept security risks.
  There is some situations where even DNSSEC is _not_ enough to
  be safe. (even if spmd supported DNSSEC)
 ***************************************************************

A) With (numeric) IP addresses

  - resolver{resolver off;}.
  - Specify numeric IP addresses in selector{}.
  - Specify peer's principal name in remote{}.
  - Specify one of corresponding selector indexes in remote{} in the
    responder's config.

B) With static FQDN names

  - Add your FQDNs in /etc/hosts.
  - resolver{resolver off;}.
  - Specify FQDNs in selector{}.
  - Specify one of corresponding selector indexes in remote{} in the
    responder's config.

C) With DNS FQDN names

  - Make your FQDNs resolvable via DNS.
  - Specify a recursive DNS server in resolver{nameserver;}.
  - Add IP addresses, on which spmd serves as a DNS proxy,
    to resolver{dns_query}, and specify them in /etc/resolv.conf.
  - resolver{resolver on;}.
  - Specify FQDNs in selector{}.
  - Specify one of corresponding selector indexes in remote{} in the
    responder's config.

5. Configuration file
== ==================

All of daemons can use a identical configuration file.  The following is
supposes that a initiator A (IP address is 10.1.1.1) will try to setup SAs
to communicate with a responder B (IP addresss is 10.2.2.2) by using IKEv2,
and the SAs is ESP transport mode.

A sample at the A is:

	interface {
	    # specify the I/F.
	    # make sure the directory permission is 700 and the owner is root.
	    spmd { unix "/var/run/racoon/spmif"; };

	    # specify the password file in order to communicate
	    # between spmd(8) and otheres.
	    # create it by 'pskgen -r -o FILE-NAME' comand and
	    # make sure the file permission is 600 and the owner is root.
	    spmd_password "/usr/local/etc/spmd.pwd";

	    # listen to all addresses.
	    ike { MY_IP; };
	};

	selector slt-A-B-any {
	    direction outbound;             # set "inbound" at B
	    src 10.1.1.1;
	    dst 10.2.2.2;
	    upper_layer_protocol "any";
	    policy_index policy-A-B;
	};
	selector slt-B-A-any {
	    direction inbound;              # set "outbound" at B
	    src 10.2.2.2;
	    dst 10.1.1.1;
	    upper_layer_protocol "any";
	    policy_index policy-A-B;
	};

	remote remote-B {
	    acceptable_kmp { ikev2; };

	    # ikev2 configuration
	    ikev2 {
		peers_ipaddr    10.2.2.2;               # 10.1.1.1 at B
		peers_id        ipaddr 10.2.2.2;        # 10.1.1.1 at B
		my_id           ipaddr 10.1.1.1;        # 10.2.2.2 at B
		kmp_enc_alg     { aes192_cbc; 3des_cbc; };
		kmp_prf_alg     { hmac_sha1; hmac_md5; };
		kmp_hash_alg    { hmac_sha1; hmac_md5; };
		kmp_dh_group    { 2; 1; };
		kmp_auth_method { psk; };

	        # make sure the file permission is 600 and the owner is root.
		pre_shared_key  "/usr/local/etc/psk/AB.psk";
	    };
	};

	policy policy-A-B {
	    action auto_ipsec;
	    remote_index remote-B;
	    ipsec_mode transport;
	    ipsec_index { ipsec-esp-a-b; };
	    ipsec_level require;
	    peers_sa_ipaddr 10.2.2.2;       # 10.1.1.1 at B
	    my_sa_ipaddr 10.1.1.1;          # 10.2.2.2 at B
	};

	ipsec ipsec-esp-a-b {
	    sa_index { sa-esp-1; };
	};

	sa sa-esp-1 {
	    sa_protocol esp;
	    esp_enc_alg { aes128_cbc; 3des_cbc; };
	    esp_auth_alg { hmac_sha1; hmac_md5; };
	};

6. Starting daemons
== ================

To run the daemons (for example, spmd and iked), just type as follows.

	# spmd
	# iked

The program commands look terminating silently.  You can show information
in the system log, typically /var/log/messages.

	# tail -f /var/log/messages

If you want to show more information, you should use the -d option and
the -F option (for more information, please read '-h' option output).

	# spmd -ddd
	# iked -ddd
