

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 ( /* C=CH, O=strongSwan Project, CN=strongSwan Root CA */
  9, X'3047310b3009060355040613024348311b3019060355040a13127374726f6e675377616e2050726f6a656374311b3019060355040313127374726f6e675377616e20526f6f74204341'
);

INSERT INTO identities (
  type, data
) VALUES ( /* subjkey of 'C=CH, O=strongSwan Project, CN=strongSwan Root CA' */
  11, X'288a0bd0d28511dae59ffa160ab021d708922d67'
);

INSERT INTO identities (
  type, data
) VALUES ( /* keyid of 'C=CH, O=strongSwan Project, CN=strongSwan Root CA' */
  11, X'91f8e4475f0bc75a388624bcf0a56fe6a45f0c2a'
);

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

INSERT INTO identities (
  type, data
) VALUES ( /* sun.strongswan.org */
  2, X'73756e2e7374726f6e677377616e2e6f7267'
 );

INSERT INTO identities (
  type, data
) VALUES ( /* subjkey of 'C=CH, O=strongSwan Project, CN=moon.strongswan.org' */
  11, X'4b712ee9ad4bb4d4ce6d5c52eff8074be41ec9e9'
 );

/* Certificates */

INSERT INTO certificates (
   type, keytype, data
) VALUES ( /* C=CH, O=strongSwan Project, CN=strongSwan Root CA */
  1, 1, X'2d2d2d2d2d424547494e2043455254494649434154452d2d2d2d2d0a4d4949455654434341723267417749424167494950704e47303745745a3449774451594a4b6f5a496876634e4151454c42514177527a454c4d416b47413155450a42684d4351306778477a415a42674e5642416f54456e4e30636d39755a314e335957346755484a76616d566a644445624d426b474131554541784d53633352790a6232356e55336468626942536232393049454e424d423458445449314d5445774e6a41774d544d794e6c6f5844544d314d5445774e7a41774d544d794e6c6f770a527a454c4d416b474131554542684d4351306778477a415a42674e5642416f54456e4e30636d39755a314e335957346755484a76616d566a644445624d426b470a4131554541784d53633352796232356e55336468626942536232393049454e424d4949426f6a414e42676b71686b6947397730424151454641414f43415938410a4d49494269674b43415945416d58702b65514b5773642f6d6c776b4e53414244713148573372436b5156496a6f747a6466503770387175696365307050784a700a473662394f2f4f30642b6666704f5633654e796a466764584a515673436a584d4a7a727456424777526d4f5a6a7754737934426d4343316858785867574952700a705a5773726671507a2b716c7573774a4753784b544556354a6e4c696b61546152584346596178726a46375671765769563248474e583059683173446b6376550a78695a5455636755767154387966765435675a3554774c596b7959574b7268756b5a547a43454470494d475377746c784b656a70506768554f366a45797977460a6535356b4956746579547063324f3845437a354e39333568664c54486d7671554f4634736b4e35456b5131506f524c625065373442596d4647364351514678770a6f47477364534f526c6a36326a494e372b37696c3674587a433154486c316b6353725170424d5354435a52464b35743362356738666a33723562484f57774b650a586171323579446e764654553273646163437939674a35705732325a467372434a357063424c7075717263475269644f2b356c674267526753344e6c5168514c0a5177494b31724c4450432b444a76314b4e446b654942454a4c544965684949454a62746c596b4b424f4b316a44504573356d33693246737a46523745506b55450a78794c784f453641724d757841674d424141476a525442444d42494741315564457745422f7751494d415942416638434151457744675944565230504151482f0a42415144416745474d42304741315564446751574242516f69677651306f5552327557662b68594b73434858434a49745a7a414e42676b71686b6947397730420a4151734641414f4341594541664d725a79434549416c4d724745356676534f6858506144444b6544786337394649587166646e6c6f7a6b48544734496b5a50550a485551433878386d432b554471315371583863514a68796e4a465844727358325969544d7243504665354161723433303248675645494a3257756274685549450a37474e43335062416b59557443557a72556f767264706d68355930543877637170444c6e536e4c4f376b3731665733725757506472753856792b776d636b65670a58427a79676b7061643076504d65646653413458736b566f756c5239424d71616b4665587a2f37672b6a4351524469625869504377737349775939436f456d310a3968503578317963744331583343474268333349397a556162537761324236364574567255766c6d43626157753234423365774f2f596b38796b452f61356b680a61544373547734564b7346335078674259794f30434c6a385867734b7930616c465752382f6d5a344a6f376f566a633448496f456c31575359654179426542580a2f4d4d775039444a793547447978336f6e47366c35383256727269317045453049414e66386a5059647452494441494352694745716152697238316c417430620a473633395751594a4b422f576a673276577459754a346e4a4a32506f512f444c6a50583979703645645450486b61564b3558513350683950764a5a746b5066380a795a53466e486c784251392f0a2d2d2d2d2d454e442043455254494649434154452d2d2d2d2d0a'
);

INSERT INTO certificates (
   type, keytype, data
) VALUES ( /* C=CH, O=strongSwan Project, CN=moon.strongswan.org */
  1, 1, X'2d2d2d2d2d424547494e2043455254494649434154452d2d2d2d2d0a4d49494569444343417643674177494241674942417a414e42676b71686b69473977304241517346414442484d517377435159445651514745774a44534445620a4d426b474131554543684d53633352796232356e5533646862694251636d39715a574e304d527377475159445651514445784a7a64484a76626d6454643246750a49464a7662335167513045774868634e4d6a55784d5441324d4441784d7a49325768634e4d7a4d784d5441324d4441784d7a4932576a42494d517377435159440a5651514745774a44534445624d426b474131554543684d53633352796232356e5533646862694251636d39715a574e304d527777476759445651514445784e740a623239754c6e4e30636d39755a334e335957347562334a6e4d4949426f6a414e42676b71686b6947397730424151454641414f43415938414d49494269674b430a415945417657514d3667717579796471356d526f705268562f4a4a79637576503633737434416332704c2f3172316862364e742b6c783539376b737464592f610a37425036794575366c794b2f41466546596e44516c636e4756564766447366504b73524335465766715a6143656d2b777052386f6c5a635a48694f795037426d0a794a4d5068567671314f765753515970587346675938384c716471475a50476f71306748435a507568382b397464437174674472396b73534d52794b636b46630a6e426d7130554631686856566d315973696a4b4c55767547745044622f6a464359304839635871764b352f684e506e3063613741722f2b4c386f634c4a7048530a5a78526b416a62586c65794e7757426135764f6b4d424937387a4f2f305050522f2b58716d4f5370535633564167504c6647794b436f4b79392f7037764f6d300a3954686c4375436638705a32645a6c4b48516675422b6f4755623071682b714f35616349756e6462724f504e466d717243514e326f2f6f54324737666b542f4a0a74675448337274504a496a2f4868726f38526f32564b3353737656685047346c496b496762476869517847736247304e5175336764452b614536596e39792b570a6277336b626c4c734c5754516c4b7430504c5951504f3858326d5359626f334c30686d344d4773444578665946734d34792b496e64727978384d613431386b6b0a6635717841674d424141476a666a42384d42384741315564497751594d4261414643694b4339445368524861355a2f3646677177496463496b69316e4d4234470a41315564455151584d4257434532317662323475633352796232356e6333646862693576636d63774f51594456523066424449774d4441756f4379674b6f596f0a6148523063446f764c324e796243357a64484a76626d647a643246754c6d39795a79397a64484a76626d647a643246754c6d4e796244414e42676b71686b69470a397730424151734641414f43415945414569325a73505343504e745270614656446754385669464566434c5358366a5333726e3755445848584f32357445344a0a767533554f4e5067435a5a6474336374446766742f306776376537625547652f424832364a52576f7a5149684d6d7457676636586f6b36657534422f6e62784e0a3958564c70395956564574416379504f35433050522b6d6f50656f77465142704d63785268335a6f686c54696c4b78454c464153785858302b414a6e78357a410a64544a7a624e37636154596535686f4466424b31442f513857565354694e566437365a5a7467472b43714e6362636c4e517031507878464d42686d5a657262790a35626951524a496c3852496a77414447685950517377545a4f7a6e624935446d486f6f766562385544474238467372542f6c5974754a425a4174325a436b65430a395a2f704368634d6a4c4e56626d52536b596b33355878585973386a6835555a396173756c596f5a6a596648677572426b4741516c6b6a35714f6d7a705749690a777955562b6b716437466742364c5a66305a77576663705a56514f4832594e4979594d3039493556434936705279665332483531435853654c454b58337777430a674466575a534c30336a4644664f616e32303467644b353769364c6835702b586a614b336a6956477a4344386538755874616e7430766c6c305244376c3569650a66777876317a3833547569746a4b6d390a2d2d2d2d2d454e442043455254494649434154452d2d2d2d2d0a'
);

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

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

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

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

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

/* Private Keys */

INSERT INTO private_keys (
   type, data
) VALUES ( /* key of 'C=CH, O=strongSwan Project, CN=moon.strongswan.org' */
  1, X'[encoded data, 4910 characters]'
);

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

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

/* Proposals */

INSERT INTO proposals (
  proposal
) VALUES (
  'aes128-aes192-aes256-sha256-sha384-sha512-modp3072-modp4096-modp8192'
);

/* Configurations */

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

INSERT INTO ike_config_proposal (
  ike_cfg, prio, prop
) VALUES (
  1, 1, 1
);

INSERT INTO peer_configs (
  name, ike_cfg, local_id, remote_id, mobike, dpd_delay
) VALUES (
  'net-net', 1, 4, 5, 0, 30
);

INSERT INTO child_configs (
  name, updown, start_action, dpd_action
) VALUES (
  'net-1', '/usr/local/libexec/ipsec/_updown iptables', 1, 1
);

INSERT INTO child_configs (
  name, updown, start_action, dpd_action
) VALUES (
  'net-2', '/usr/local/libexec/ipsec/_updown iptables', 1, 1
);

INSERT INTO child_configs (
  name, updown, start_action, dpd_action
) VALUES (
  'net-3', '/usr/local/libexec/ipsec/_updown iptables', 0, 0
);

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

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

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

INSERT INTO child_config_proposal (
  child_cfg, prio, prop
) VALUES (
  1, 1, 1
);

INSERT INTO child_config_proposal (
  child_cfg, prio, prop
) VALUES (
  2, 1, 1
);

INSERT INTO child_config_proposal (
  child_cfg, prio, prop
) VALUES (
  3, 1, 1
);

INSERT INTO traffic_selectors (
  type, start_addr, end_addr
) VALUES (
  7, X'0a010000', X'0a01000f'
);

INSERT INTO traffic_selectors (
  type, start_addr, end_addr
) VALUES (
  7, X'0a010010', X'0a01001f'
);

INSERT INTO traffic_selectors (
  type, start_addr, end_addr
) VALUES (
  7, X'0a010200', X'0a0103ff'
);

INSERT INTO traffic_selectors (
  type, start_addr, end_addr
) VALUES (
  7, X'0a020000', X'0a0201ff'
);

INSERT INTO traffic_selectors (
  type, start_addr, end_addr
) VALUES (
  7, X'0a020200', X'0a0203ff'
);

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, 4, 1
);

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

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

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

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