

CREATE TABLE identities (
  id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
  type INTEGER NOT NULL,
  data BLOB NOT NULL,
  UNIQUE (type, data)
);


CREATE TABLE child_configs (
  id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
  name TEXT NOT NULL,
  lifetime INTEGER NOT NULL DEFAULT '1500',
  rekeytime INTEGER NOT NULL DEFAULT '1200',
  jitter INTEGER NOT NULL DEFAULT '60',
  updown TEXT DEFAULT NULL,
  hostaccess INTEGER NOT NULL DEFAULT '0',
  mode INTEGER NOT NULL DEFAULT '2',
  start_action INTEGER NOT NULL DEFAULT '0',
  dpd_action INTEGER NOT NULL DEFAULT '0',
  close_action INTEGER NOT NULL DEFAULT '0',
  ipcomp INTEGER NOT NULL DEFAULT '0',
  reqid INTEGER NOT NULL DEFAULT '0'
);
DROP INDEX IF EXISTS child_configs_name;
CREATE INDEX child_configs_name ON child_configs (
  name
);


CREATE TABLE child_config_traffic_selector (
  child_cfg INTEGER NOT NULL,
  traffic_selector INTEGER NOT NULL,
  kind INTEGER NOT NULL
);
DROP INDEX IF EXISTS child_config_traffic_selector_all;
CREATE INDEX child_config_traffic_selector_all ON child_config_traffic_selector (
  child_cfg, traffic_selector
);

CREATE TABLE proposals (
  id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
  proposal TEXT NOT NULL
);


CREATE TABLE child_config_proposal (
  child_cfg INTEGER NOT NULL,
  prio INTEGER NOT NULL,
  prop INTEGER NOT NULL
);


CREATE TABLE ike_configs (
  id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
  certreq INTEGER NOT NULL DEFAULT '1',
  force_encap INTEGER NOT NULL DEFAULT '0',
  local TEXT NOT NULL,
  remote TEXT NOT NULL
);


CREATE TABLE ike_config_proposal (
  ike_cfg INTEGER NOT NULL,
  prio INTEGER NOT NULL,
  prop INTEGER NOT NULL
);


CREATE TABLE peer_configs (
  id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
  name TEXT NOT NULL,
  ike_version INTEGER NOT NULL DEFAULT '2',
  ike_cfg INTEGER NOT NULL,
  local_id TEXT NOT NULL,
  remote_id TEXT NOT NULL,
  cert_policy INTEGER NOT NULL DEFAULT '1',
  uniqueid INTEGER NOT NULL DEFAULT '0',
  auth_method INTEGER NOT NULL DEFAULT '1',
  eap_type INTEGER NOT NULL DEFAULT '0',
  eap_vendor INTEGER NOT NULL DEFAULT '0',
  keyingtries INTEGER NOT NULL DEFAULT '3',
  rekeytime INTEGER NOT NULL DEFAULT '7200',
  reauthtime INTEGER NOT NULL DEFAULT '0',
  jitter INTEGER NOT NULL DEFAULT '180',
  overtime INTEGER NOT NULL DEFAULT '300',
  mobike INTEGER NOT NULL DEFAULT '1',
  dpd_delay INTEGER NOT NULL DEFAULT '120',
  virtual TEXT DEFAULT NULL,
  pool TEXT DEFAULT NULL,
  mediation INTEGER NOT NULL DEFAULT '0',
  mediated_by INTEGER NOT NULL DEFAULT '0',
  peer_id INTEGER NOT NULL DEFAULT '0'
);
DROP INDEX IF EXISTS peer_configs_name;
CREATE INDEX peer_configs_name ON peer_configs (
  name
);


CREATE TABLE peer_config_child_config (
  peer_cfg INTEGER NOT NULL,
  child_cfg INTEGER NOT NULL,
  PRIMARY KEY (peer_cfg, child_cfg)
);


CREATE TABLE traffic_selectors (
  id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
  type INTEGER NOT NULL DEFAULT '7',
  protocol INTEGER NOT NULL DEFAULT '0',
  start_addr BLOB DEFAULT NULL,
  end_addr BLOB DEFAULT NULL,
  start_port INTEGER NOT NULL DEFAULT '0',
  end_port INTEGER NOT NULL DEFAULT '65535'
);


CREATE TABLE certificates (
  id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
  type INTEGER NOT NULL,
  keytype INTEGER NOT NULL,
  data BLOB NOT NULL
);


CREATE TABLE certificate_identity (
  certificate INTEGER NOT NULL,
  identity INTEGER NOT NULL,
  PRIMARY KEY (certificate, identity)
);


CREATE TABLE private_keys (
  id INTEGER NOT NULL  PRIMARY KEY AUTOINCREMENT,
  type INTEGER NOT NULL,
  data BLOB NOT NULL
);


CREATE TABLE private_key_identity (
  private_key INTEGER NOT NULL,
  identity INTEGER NOT NULL,
  PRIMARY KEY (private_key, identity)
);


CREATE TABLE shared_secrets (
  id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
  type INTEGER NOT NULL,
  data BLOB NOT NULL
);


CREATE TABLE shared_secret_identity (
  shared_secret INTEGER NOT NULL,
  identity INTEGER NOT NULL,
  PRIMARY KEY (shared_secret, identity)
);


CREATE TABLE certificate_authorities (
  id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
  certificate INTEGER NOT NULL
);


CREATE TABLE certificate_distribution_points (
  id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
  ca INTEGER NOT NULL,
  type INTEGER NOT NULL,
  uri TEXT NOT NULL
);


CREATE TABLE pools (
  id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
  name TEXT NOT NULL UNIQUE,
  start BLOB NOT NULL,
  end BLOB NOT NULL,
  timeout INTEGER NOT NULL
);

CREATE TABLE addresses (
  id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
  pool INTEGER NOT NULL,
  address BLOB NOT NULL,
  identity INTEGER NOT NULL DEFAULT 0,
  acquired INTEGER NOT NULL DEFAULT 0,
  released INTEGER NOT NULL DEFAULT 1
);
DROP INDEX IF EXISTS addresses_pool;
CREATE INDEX addresses_pool ON addresses (
  pool
);
DROP INDEX IF EXISTS addresses_address;
CREATE INDEX addresses_address ON addresses (
  address
);
DROP INDEX IF EXISTS addresses_identity;
CREATE INDEX addresses_identity ON addresses (
  identity
);

CREATE TABLE leases (
  id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
  address INTEGER NOT NULL,
  identity INTEGER NOT NULL,
  acquired INTEGER NOT NULL,
  released INTEGER NOT NULL
);

CREATE TABLE attribute_pools (
  id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
  name TEXT NOT NULL
);

CREATE TABLE attributes (
  id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
  identity INTEGER NOT NULL DEFAULT 0,
  pool INTEGER NOT NULL DEFAULT 0,
  type INTEGER NOT NULL,
  value BLOB NOT NULL
);
DROP INDEX IF EXISTS attributes_identity;
CREATE INDEX attributes_identity ON attributes (
  identity
);
DROP INDEX IF EXISTS attributes_pool;
CREATE INDEX attributes_pool ON attributes (
  pool
);

CREATE TABLE ike_sas (
  local_spi BLOB NOT NULL PRIMARY KEY,
  remote_spi BLOB NOT NULL,
  id INTEGER NOT NULL,
  initiator INTEGER NOT NULL,
  local_id_type INTEGER NOT NULL,
  local_id_data BLOB DEFAULT NULL,
  remote_id_type INTEGER NOT NULL,
  remote_id_data BLOB DEFAULT NULL,
  host_family INTEGER NOT NULL,
  local_host_data BLOB NOT NULL,
  remote_host_data BLOB NOT NULL,
  created INTEGER NOT NULL DEFAULT CURRENT_TIMESTAMP
);

CREATE TABLE logs (
  id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
  local_spi BLOB NOT NULL,
  signal INTEGER NOT NULL,
  level INTEGER NOT NULL,
  msg TEXT NOT NULL,
  time INTEGER NOT NULL DEFAULT CURRENT_TIMESTAMP
);

/* Identities */

INSERT INTO identities (
  type, data
) VALUES ( /* moon.strongswan.org */
  2, X'6d6f6f6e2e7374726f6e677377616e2e6f7267'
 );

INSERT INTO identities (
  type, data
) VALUES ( /* carol@strongswan.org */
  3, X'6361726f6c407374726f6e677377616e2e6f7267'
 );

INSERT INTO identities (
  type, data
) VALUES ( /* dave@strongswan.org */
  3, X'64617665407374726f6e677377616e2e6f7267'
 );

INSERT INTO identities (
  type, data
) VALUES ( /* %any */
  0, '%any'
);

INSERT INTO identities (
  type, data
) VALUES ( /* subjkey of moon.strongswan.org */
  11, X'4b712ee9ad4bb4d4ce6d5c52eff8074be41ec9e9'
 );

INSERT INTO identities (
  type, data
) VALUES ( /* subjkey of carol@strongswan.org */
  11, X'f95e0a118b82b2dd133ed258f1bde99df277dcb0'
 );

INSERT INTO identities (
  type, data
) VALUES ( /* subjkey of dave@strongswan.org */
  11, X'516f2a5affb28c278a5b3044f84fab6b42e4e8d1'
 );

/* Raw RSA public keys */

INSERT INTO certificates (
   type, keytype, data
) VALUES ( /* moon.strongswan.org */
  6, 1, X'308201a2300d06092a864886f70d01010105000382018f003082018a0282018100bd640cea0aaecb276ae66468a51855fc927272ebcfeb7b2de00736a4bff5af585be8db7e971e7dee4b2d758fdaec13fac84bba9722bf0057856270d095c9c655519f0ec7cf2ac442e4559fa996827a6fb0a51f289597191e23b23fb066c8930f855bead4ebd64906295ec16063cf0ba9da8664f1a8ab48070993ee87cfbdb5d0aab600ebf64b12311c8a72415c9c19aad141758615559b562c8a328b52fb86b4f0dbfe31426341fd717aaf2b9fe134f9f471aec0afff8bf2870b2691d26714640236d795ec8dc1605ae6f3a430123bf333bfd0f3d1ffe5ea98e4a9495dd50203cb7c6c8a0a82b2f7fa7bbce9b4f538650ae09ff2967675994a1d07ee07ea0651bd2a87ea8ee5a708ba775bace3cd166aab090376a3fa13d86edf913fc9b604c7debb4f2488ff1e1ae8f11a3654add2b2f5613c6e252242206c68624311ac6c6d0d42ede0744f9a13a627f72f966f0de46e52ec2d64d094ab743cb6103cef17da64986e8dcbd219b8306b031317d816c338cbe22776bcb1f0c6b8d7c9247f9ab10203010001'
);

INSERT INTO certificates (
   type, keytype, data
) VALUES ( /* carol@strongswan.org */
  6, 1, X'308201a2300d06092a864886f70d01010105000382018f003082018a0282018100d7cbc3a0cbd23c004eef1df6e477f0fc307e9241eac63524e4ff7e799d84096e07d839baf139bad3ac9041eccc724040b012dd52de12f791edbdcb86b9ecc9657961503aef1f8e0fed8385d87368dda333eb69ab4eab75822026cc02891e033203ec12659275cd4aea9962490ffcdc33efc9d2a89c2c4bf15d199ebf675b6bf8cfb2272659ca8c52f50d43ef50fc8fad1ea82b27a4755757aaa321129388b415564cb3f3e955ea45da96512479258e16e513d162b10bcd209eda6069770064011581e9fc62d4777d2e689f57869da0e68f2aeae1cc0b4c730b9dd2f641a9881f7d871e6ea6e4d2533170b8d191f005bbd15dcc319572044343e2f142ddd3167ad307053f8d1b8a0033edb6ed0f94c17a646c84a5bf2361ff7ce9fab83583e61fd1c542c0de300176b4d0943d1c49e15ffb3c10041165dfaaed2ae26ceec79bf537fc58f8cb6360980004f538ce6d3ff3ab9c74f695b2683e34e2053ff7625375c94aa672faf86f56864db4d27b9967dd9449f22ade1ac19342e8cff3082d6c450203010001'
);

INSERT INTO certificates (
   type, keytype, data
) VALUES ( /* dave@strongswan.org */
  6, 1, X'308201a2300d06092a864886f70d01010105000382018f003082018a0282018100ac0930765adb727a1d7f092a8bc02fa88754cd355279c5cf5b1129bf22b1607736a42ad468dec35753669f2672f97b645f00ecd8909675a0d85ace8914073d7f76acf2f8655b212bf05b84dae81218f34c87843a63ff358797eb94c59e291f5daa780d6fe2016bbfcb9a47e1aec2f67f1f45a6fb89478ef3e59f735e0eb42d6700346e57c59baa4f4f8b868552eed0f6a2c29c36d70284ffccdc20db75f66516b0e52666b6a36b673474c252d32c5350ac45a2168ea1d03f4f37801dfc56e9489508d4da92323c3036a80d91ba8b3edeb88ffe06989a8a3906fc1161c14bb306ade5ba3abe61a79e9f6ed5d03995dc2792e5769dcb118b606013ca6a13dff1221f36bddee4ae08191d4f3c5063c970570d0d7018ac158ce9a6f211855feb557f7b8df61287e69deccd92d3e986ab3506beb1111215a5990d60c2f5c2cfcc2fac0a9d30120e306309111f11929f4cc1b5cf480c99598440ebf41a287857d44c01596cded752d38f8465601339793740836530c3530814e61d749a84a9e60dca1d0203010001'
);

INSERT INTO certificate_identity (
  certificate, identity
) VALUES (
  1, 1
);

INSERT INTO certificate_identity (
  certificate, identity
) VALUES (
  1, 5
);

INSERT INTO certificate_identity (
  certificate, identity
) VALUES (
  2, 2
);

INSERT INTO certificate_identity (
  certificate, identity
) VALUES (
  2, 6
);

INSERT INTO certificate_identity (
  certificate, identity
) VALUES (
  3, 3
);

INSERT INTO certificate_identity (
  certificate, identity
) VALUES (
  3, 7
);

/* Private Keys */

INSERT INTO private_keys (
   type, data
) VALUES ( /* key of CN=moon.strongswan.org' */
  1, X'308206e30201000282018100bd640cea0aaecb276ae66468a51855fc927272ebcfeb7b2de00736a4bff5af585be8db7e971e7dee4b2d758fdaec13fac84bba9722bf0057856270d095c9c655519f0ec7cf2ac442e4559fa996827a6fb0a51f289597191e23b23fb066c8930f855bead4ebd64906295ec16063cf0ba9da8664f1a8ab48070993ee87cfbdb5d0aab600ebf64b12311c8a72415c9c19aad141758615559b562c8a328b52fb86b4f0dbfe31426341fd717aaf2b9fe134f9f471aec0afff8bf2870b2691d26714640236d795ec8dc1605ae6f3a430123bf333bfd0f3d1ffe5ea98e4a9495dd50203cb7c6c8a0a82b2f7fa7bbce9b4f538650ae09ff2967675994a1d07ee07ea0651bd2a87ea8ee5a708ba775bace3cd166aab090376a3fa13d86edf913fc9b604c7debb4f2488ff1e1ae8f11a3654add2b2f5613c6e252242206c68624311ac6c6d0d42ede0744f9a13a627f72f966f0de46e52ec2d64d094ab743cb6103cef17da64986e8dcbd219b8306b031317d816c338cbe22776bcb1f0c6b8d7c9247f9ab102030100010282018017679cdcf37304721b98db41bf5d518b040216c7a38b434eb907cb2df2f706b351b06627afbf39eacfe98f44b6d643e5d308e0c73aa905fecae20e75087dc5ffd73563db3773de8b31b28f27a30cfeda7ca6fb41c5b2c58cdef6713e788542ff4108c9ed84339814a5da2ae5a18ffdae00ee68dd206e0259b44a4ec903d83af6cd91ee6d3aa3ae5cd95d169f93354b4abbcf6e19a2db507606cd114ffc724f045f5ba07f2fe81a489ad4817f4fa3c22b3d952af66f09929ee7276e9dc8b5aa6436a55297a793dde103bb38e540dda10d7b49e85edbe5c20977c63e6c84928073355fdbdf81be8732049eb65142e193b7fb18ff02c058a3cf51c324f6c13d63e1ae950cd473a4492a12e7180cdccd0f12d790101c94f75c0eb3bbb98ee0615eaaaa7d523b5349d60e8848dd2b3549abc630886d5f7caeba0485f590713165773ec51151de7adfad61edbaad795c1d17ef99ea27b64652096f0d4dcedb33b724973f835668b8e5c3b1f6d9b885aafdce5ac9f5e0163e4a2dd341c4558709e398dd0281c100fba18186ac767da078696797beb769ce67974b397be9a69e1cc421df7792d44ec23f4f6c2f88781f9fd2dba3fcd4d6195abfdae180622f175ac3f697d6ca835a16248e753fc97b119e5de7e1ce1c8358202044978eb7243fe1ca49749fa11e1a7b9d2a0cea7ab2bf932307fb4ddef767606bfd7dd5644c55ced99b5ee82c01f1dc483ef37c00669278874eca9e47d7e69abca52338b49ae01e3a1c5a63eb01b069d96d274fa06748b2687cc2af60165bda1128a81c8b8395cdc73d876237ba9b0281c100c0ade38285bd83416601df9b7af7b2b896f2c439d67e7ecb35eed5667446c2d8ca8e6762c655f5ebac4840f4010d9ef78d131469b4c65acd3da0dc2c7e4eab0b8d24e80eb93296aa546bcb7a43ffca4bd44ed221e6d47657aa2e6e8f03c391eed002b79c43dba61b2077e42481b4ffa65a4dd9314c29d9bf1c4e70b39db4ddc866de95c5778fc51b7d331042875bc65bd5ff3b0be0cea2fe5c664a268bf96cba6db87703d3021b62ac63d6254f1a7ddf409de555ce6fa2f25415c0f7467afea30281c0048fb4ed600af5d406dac249f9e7113ff098c9804d42bb067ab703762d90b7d085ace81fa9eceb60876127221ddf2dd25fd6c27cfbfacba7bcb978aadff5e3d3aa3b176a00b2024d1b75d052438e38c34c31c98c85c5c41e74555a6786cd40e272033353f09c30c9503d7e538be1d2d248e92c3b2fb715bb2878c15c43112927dd3a6209bfe1b1fa4dbd062c9e19e74c02da109446dedf3fd8cc6cdf269887fb4eafe64ea41ee0406d1b7fc777ae62396006979a148a62e4404ab04ed7f4f2a90281c04349b6c5333d7ea40d22a45b60f335d7dee742fdcbd97b202e628ce5edbf74764f2eb20e3da371716f460060f6293295d30f91b664aa4cb5964b21f8ebd73f46967108157439a582305f40bd1f182a26972b3b99194aa355741a29040ad5769646d05c1cd759d2c5706e4e1f2da8ef0ac248a38be2c44ec5bdf5eac7df50758f71d553ca953981255b70c4c1e71176d145878fb0533ef68f0be7b1078b247d031224347402719eecb3b55de8c39a45d0fc78e7c93281ac0f22ac422a58d3c52f0281c100faebc206ba231be8384f3e5c1dd28b827703956c917f9cdc80594b8dd7fc3389deb31fd0322aed661c73a66dffac45a2829e8e79f87ac8fd2fd5a031b05cc19d0e8568316a7ed9487af702a129dea3dc671c00b372c1a132d15270716c3008fb9aa421fd95b459b60250d98fe32439c0c33f752f2fefdea1450118451856e4de146d3daab9afa0f79dc6aa0632ef125b0b39b5f84ef843b414d72f5e1a0f80d787964213b11858698701cabeefd850c36b70ee4c2e266ebddfc990fb8fadf26e'
);

INSERT INTO private_key_identity (
  private_key, identity
) VALUES (
  1, 1
);

INSERT INTO private_key_identity (
  private_key, identity
) VALUES (
  1, 5
);

/* Configurations */

INSERT INTO ike_configs (
  local, remote
) VALUES (
  '192.168.0.1', '0.0.0.0'
);

INSERT INTO peer_configs (
  name, ike_cfg, local_id, remote_id
) VALUES (
  'rw', 1, 5, 4
);

INSERT INTO child_configs (
  name, updown
) VALUES (
  'rw', '/usr/local/libexec/ipsec/_updown iptables'
);

INSERT INTO peer_config_child_config (
  peer_cfg, child_cfg
) VALUES (
  1, 1
);

INSERT INTO traffic_selectors (
  type, start_addr, end_addr
) VALUES ( /* 10.1.0.0/16 */
  7, X'0a010000', X'0a01ffff'
);

INSERT INTO traffic_selectors (
  type
) VALUES ( /* dynamic/32 */
  7
);

INSERT INTO child_config_traffic_selector (
  child_cfg, traffic_selector, kind
) VALUES (
  1, 1, 0
);

INSERT INTO child_config_traffic_selector (
	child_cfg, traffic_selector, kind
) VALUES (
  1, 2, 3
);

