2 * Copyright (C) 2010 Martin Willi
3 * Copyright (C) 2010 revosec AG
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 #include "eap_simaka_sql_provider.h"
22 typedef struct private_eap_simaka_sql_provider_t private_eap_simaka_sql_provider_t
;
25 * Private data of an eap_simaka_sql_provider_t object.
27 struct private_eap_simaka_sql_provider_t
{
30 * Public eap_simaka_sql_provider_t interface.
32 eap_simaka_sql_provider_t
public;
35 * Triplet/quintuplet database
40 * Remove used triplets/quintuplets from database
45 METHOD(simaka_provider_t
, get_triplet
, bool,
46 private_eap_simaka_sql_provider_t
*this, identification_t
*id
,
47 char rand
[SIM_RAND_LEN
], char sres
[SIM_SRES_LEN
], char kc
[SIM_KC_LEN
])
49 chunk_t rand_chunk
, sres_chunk
, kc_chunk
;
54 snprintf(buf
, sizeof(buf
), "%Y", id
);
55 query
= this->db
->query(this->db
,
56 "select rand, sres, kc from triplets where id = ? order by use",
57 DB_TEXT
, buf
, DB_BLOB
, DB_BLOB
, DB_BLOB
);
60 if (query
->enumerate(query
, &rand_chunk
, &sres_chunk
, &kc_chunk
))
62 if (rand_chunk
.len
== SIM_RAND_LEN
&&
63 sres_chunk
.len
== SIM_SRES_LEN
&&
64 kc_chunk
.len
== SIM_KC_LEN
)
66 memcpy(rand
, rand_chunk
.ptr
, SIM_RAND_LEN
);
67 memcpy(sres
, sres_chunk
.ptr
, SIM_SRES_LEN
);
68 memcpy(kc
, kc_chunk
.ptr
, SIM_KC_LEN
);
72 query
->destroy(query
);
76 if (this->remove_used
)
78 this->db
->execute(this->db
, NULL
,
79 "delete from triplets where id = ? and rand = ?",
80 DB_TEXT
, buf
, DB_BLOB
, chunk_create(rand
, SIM_RAND_LEN
));
84 this->db
->execute(this->db
, NULL
,
85 "update triplets set use = ? where id = ? and rand = ?",
86 DB_UINT
, time(NULL
), DB_TEXT
, buf
,
87 DB_BLOB
, chunk_create(rand
, SIM_RAND_LEN
));
93 METHOD(simaka_provider_t
, get_quintuplet
, bool,
94 private_eap_simaka_sql_provider_t
*this, identification_t
*id
,
95 char rand
[AKA_RAND_LEN
], char xres
[AKA_RES_MAX
], int *xres_len
,
96 char ck
[AKA_CK_LEN
], char ik
[AKA_IK_LEN
], char autn
[AKA_AUTN_LEN
])
98 chunk_t rand_chunk
, xres_chunk
, ck_chunk
, ik_chunk
, autn_chunk
;
103 snprintf(buf
, sizeof(buf
), "%Y", id
);
104 query
= this->db
->query(this->db
, "select rand, res, ck, ik, autn "
105 "from quintuplets where id = ? order by use", DB_TEXT
, buf
,
106 DB_BLOB
, DB_BLOB
, DB_BLOB
, DB_BLOB
, DB_BLOB
);
109 if (query
->enumerate(query
, &rand_chunk
, &xres_chunk
,
110 &ck_chunk
, &ik_chunk
, &autn_chunk
))
112 if (rand_chunk
.len
== AKA_RAND_LEN
&&
113 xres_chunk
.len
<= AKA_RES_MAX
&&
114 ck_chunk
.len
== AKA_CK_LEN
&&
115 ik_chunk
.len
== AKA_IK_LEN
&&
116 autn_chunk
.len
== AKA_AUTN_LEN
)
118 memcpy(rand
, rand_chunk
.ptr
, AKA_RAND_LEN
);
119 memcpy(xres
, xres_chunk
.ptr
, xres_chunk
.len
);
120 *xres_len
= xres_chunk
.len
;
121 memcpy(ck
, ck_chunk
.ptr
, AKA_CK_LEN
);
122 memcpy(ik
, ik_chunk
.ptr
, AKA_IK_LEN
);
123 memcpy(autn
, autn_chunk
.ptr
, AKA_AUTN_LEN
);
127 query
->destroy(query
);
131 if (this->remove_used
)
133 this->db
->execute(this->db
, NULL
,
134 "delete from quintuplets where id = ? and rand = ?",
135 DB_TEXT
, buf
, DB_BLOB
, chunk_create(rand
, SIM_RAND_LEN
));
139 this->db
->execute(this->db
, NULL
,
140 "update quintuplets set use = ? where id = ? and rand = ?",
141 DB_UINT
, time(NULL
), DB_TEXT
, buf
,
142 DB_BLOB
, chunk_create(rand
, AKA_RAND_LEN
));
148 METHOD(eap_simaka_sql_provider_t
, destroy
, void,
149 private_eap_simaka_sql_provider_t
*this)
157 eap_simaka_sql_provider_t
*eap_simaka_sql_provider_create(database_t
*db
,
160 private_eap_simaka_sql_provider_t
*this;
165 .get_triplet
= _get_triplet
,
166 .get_quintuplet
= _get_quintuplet
,
167 .resync
= (void*)return_false
,
168 .is_pseudonym
= (void*)return_null
,
169 .gen_pseudonym
= (void*)return_null
,
170 .is_reauth
= (void*)return_null
,
171 .gen_reauth
= (void*)return_null
,
176 .remove_used
= remove_used
,
179 return &this->public;