rjw | 1f88458 | 2022-01-06 17:20:42 +0800 | [diff] [blame^] | 1 | |
| 2 | #include <stdio.h> |
| 3 | #include <stdlib.h> |
| 4 | #include <string.h> |
| 5 | #include <ctype.h> |
| 6 | #include <unistd.h> |
| 7 | #include <errno.h> |
| 8 | #include <netdb.h> |
| 9 | #include <fcntl.h> |
| 10 | #include <dirent.h> |
| 11 | #include <time.h> |
| 12 | #include <linux/capability.h> |
| 13 | #include <sys/capability.h> |
| 14 | #include <cutils/properties.h> |
| 15 | #include "setkey_fileio.h" |
| 16 | #include "utils_xfrm.h" |
| 17 | #include "setkey_xfrm_parse.h" |
| 18 | #define LOG_TAG "setkey" |
| 19 | #include <log/log.h> |
| 20 | #include <cutils/log.h> |
| 21 | |
| 22 | |
| 23 | const struct typeent xfrmproto_types[]= { |
| 24 | { "esp", IPPROTO_ESP }, { "ah", IPPROTO_AH }, { "comp", IPPROTO_COMP }, |
| 25 | { "route2", IPPROTO_ROUTING }, { "hao", IPPROTO_DSTOPTS }, |
| 26 | { "ipsec-any", IPSEC_PROTO_ANY }, |
| 27 | { NULL, -1 } |
| 28 | }; |
| 29 | |
| 30 | |
| 31 | int xfrm_xfrmproto_getbyname(char *name) |
| 32 | { |
| 33 | int i; |
| 34 | |
| 35 | for (i = 0; ; i++) { |
| 36 | const struct typeent *t = &xfrmproto_types[i]; |
| 37 | if (!t->t_name || t->t_type == -1) |
| 38 | break; |
| 39 | |
| 40 | if (strcmp(t->t_name, name) == 0) |
| 41 | return t->t_type; |
| 42 | } |
| 43 | |
| 44 | return -1; |
| 45 | } |
| 46 | |
| 47 | |
| 48 | int xfrm_id_parse(xfrm_address_t *saddr_xfrm, struct xfrm_id *id, __u16 *family, |
| 49 | char * src,char * dst,char * ipsec_type) |
| 50 | { |
| 51 | |
| 52 | inet_prefix dst_prefix; |
| 53 | inet_prefix src_prefix; |
| 54 | |
| 55 | memset(&dst_prefix, 0, sizeof(dst_prefix)); |
| 56 | memset(&src_prefix, 0, sizeof(src_prefix)); |
| 57 | |
| 58 | get_prefix(&src_prefix, src, AF_UNSPEC); |
| 59 | if (family) |
| 60 | *family = src_prefix.family; |
| 61 | memcpy(saddr_xfrm, &src_prefix.data, sizeof(*saddr_xfrm)); |
| 62 | get_prefix(&dst_prefix, dst, AF_UNSPEC); |
| 63 | memcpy(&id->daddr, &dst_prefix.data, sizeof(id->daddr)); |
| 64 | |
| 65 | int ret = xfrm_xfrmproto_getbyname(ipsec_type); |
| 66 | if(ret<0) |
| 67 | { |
| 68 | ALOGD("xfrm_id_parse %s is wrong\n",ipsec_type); |
| 69 | return -1; |
| 70 | } |
| 71 | id->proto = (__u8)ret; |
| 72 | |
| 73 | |
| 74 | return 0; |
| 75 | } |
| 76 | |
| 77 | void xfrm_encry_algo_parse(char * encry_src, char *name) |
| 78 | { |
| 79 | if(encry_src == NULL) |
| 80 | memcpy(name,"ecb(cipher_null)",strlen("ecb(cipher_null)")); |
| 81 | else if(strcmp(encry_src,"des-cbc")==0) |
| 82 | memcpy(name,"cbc(des)",strlen("cbc(des)")); |
| 83 | else if(strcmp(encry_src,"des-ede3-cbc")==0) |
| 84 | memcpy(name,"cbc(des3_ede)",strlen("cbc(des3_ede)")); |
| 85 | else if(strcmp(encry_src,"3des-cbc")==0) |
| 86 | memcpy(name,"cbc(des3_ede)",strlen("cbc(des3_ede)")); |
| 87 | else if(strcmp(encry_src,"cast5-cbc")==0) |
| 88 | memcpy(name,"cbc(cast5)",strlen("cbc(cast5)")); |
| 89 | else if(strcmp(encry_src,"blowfish-cbc")==0) |
| 90 | memcpy(name,"cbc(blowfish)",strlen("cbc(blowfish)")); |
| 91 | else if(strcmp(encry_src,"aes-cbc")==0) |
| 92 | memcpy(name,"cbc(aes)",strlen("cbc(aes)")); |
| 93 | else if(strcmp(encry_src,"serpent-cbc")==0) |
| 94 | memcpy(name,"cbc(serpent)",strlen("cbc(serpent)")); |
| 95 | else if(strcmp(encry_src,"camellia-cbc")==0) |
| 96 | memcpy(name,"cbc(camellia)",strlen("cbc(camellia)")); |
| 97 | else if(strcmp(encry_src,"twofish-cbc")==0) |
| 98 | memcpy(name,"cbc(twofish)",strlen("cbc(twofish)")); |
| 99 | else if(strcmp(encry_src,"aes-ctr-rfc3686")==0) |
| 100 | memcpy(name,"rfc3686(ctr(aes))",strlen("rfc3686(ctr(aes))")); |
| 101 | else if(strcmp(encry_src,"null")==0) |
| 102 | memcpy(name,"ecb(cipher_null)",strlen("ecb(cipher_null)")); |
| 103 | else |
| 104 | { |
| 105 | memcpy(name,"not-supported",strlen("not-supported")); |
| 106 | ALOGD("xfrm_encry_algo_parse not supported algorithm--%s\n",encry_src); |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | void xfrm_interg_algo_parse(char * interg_src, char *name) |
| 111 | { |
| 112 | if(interg_src == NULL) |
| 113 | memcpy(name,"digest_null",strlen("digest_null")); |
| 114 | else if(strcmp(interg_src,"hmac-md5")==0) |
| 115 | memcpy(name,"hmac(md5)",strlen("hmac(md5)")); |
| 116 | else if(strcmp(interg_src,"hmac-sha1")==0) |
| 117 | memcpy(name,"hmac(sha1)",strlen("hmac(sha1)")); |
| 118 | else if(strcmp(interg_src,"hmac-sha256")==0) |
| 119 | memcpy(name,"hmac(sha256)",strlen("hmac(sha256)")); |
| 120 | else if(strcmp(interg_src,"hmac-sha384)")==0) |
| 121 | memcpy(name,"hmac(sha384)",strlen("hmac(sha384)")); |
| 122 | else if(strcmp(interg_src,"hmac-sha512")==0) |
| 123 | memcpy(name,"hmac(sha512)",strlen("hmac(sha512)")); |
| 124 | else if(strcmp(interg_src,"hmac-rmd160")==0) |
| 125 | memcpy(name,"hmac(rmd160)",strlen("hmac(rmd160)")); |
| 126 | else if(strcmp(interg_src,"aes-xcbc")==0) |
| 127 | memcpy(name,"xcbc(aes)",strlen("xcbc(aes)")); |
| 128 | else if(strcmp(interg_src,"cmac(aes)")==0) |
| 129 | memcpy(name,"aes-cmac",strlen("aes-cmac")); |
| 130 | else if(strcmp(interg_src,"null")==0) |
| 131 | memcpy(name,"digest_null",strlen("digest_null")); |
| 132 | else |
| 133 | { |
| 134 | memcpy(name,"not-supported",strlen("not-supported")); |
| 135 | ALOGD("xfrm_interg_algo_parse not supported algorithm--%s\n",interg_src); |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | |
| 140 | int xfrm_algo_parse(struct xfrm_algo *alg, char *name, char *key, char *buf, int max) |
| 141 | { |
| 142 | int len; |
| 143 | int slen = strlen(key); |
| 144 | |
| 145 | strncpy(alg->alg_name, name, sizeof(alg->alg_name)); |
| 146 | |
| 147 | if (slen > 2 && strncmp(key, "0x", 2) == 0) { |
| 148 | /* split two chars "0x" from the top */ |
| 149 | char *p = key + 2; |
| 150 | int plen = slen - 2; |
| 151 | int i; |
| 152 | int j; |
| 153 | |
| 154 | /* Converting hexadecimal numbered string into real key; |
| 155 | * Convert each two chars into one char(value). If number |
| 156 | * of the length is odd, add zero on the top for rounding. |
| 157 | */ |
| 158 | |
| 159 | /* calculate length of the converted values(real key) */ |
| 160 | len = (plen + 1) / 2; |
| 161 | if (len > max) |
| 162 | { |
| 163 | ALOGD("xfrm_algo_parse key(len:%d) makes buffer overflow\n",len); |
| 164 | return -1; |
| 165 | } |
| 166 | |
| 167 | for (i = - (plen % 2), j = 0; j < len; i += 2, j++) { |
| 168 | char vbuf[3]; |
| 169 | __u8 val; |
| 170 | |
| 171 | vbuf[0] = i >= 0 ? p[i] : '0'; |
| 172 | vbuf[1] = p[i + 1]; |
| 173 | vbuf[2] = '\0'; |
| 174 | |
| 175 | if (get_u8(&val, vbuf, 16)) |
| 176 | { |
| 177 | ALOGD("xfrm_algo_parse key(len:%s) is invalid\n",key); |
| 178 | return -1; |
| 179 | } |
| 180 | buf[j] = val; |
| 181 | } |
| 182 | } else { |
| 183 | len = slen; |
| 184 | if (len > 0) { |
| 185 | if (len > max) |
| 186 | { |
| 187 | ALOGD("xfrm_algo_parse key(len:%d) makes buffer overflow\n",len); |
| 188 | return -1; |
| 189 | } |
| 190 | |
| 191 | strncpy(buf, key, len); |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | alg->alg_key_len = len * 8; |
| 196 | |
| 197 | return 0; |
| 198 | } |
| 199 | |
| 200 | __u8 xfrm_dir_parse(char * dir_str) |
| 201 | { |
| 202 | __u8 dir; |
| 203 | if(strcmp(dir_str,"out")==0) |
| 204 | dir = XFRM_POLICY_OUT; |
| 205 | else if(strcmp(dir_str,"in")==0) |
| 206 | dir = XFRM_POLICY_IN; |
| 207 | else if(strcmp(dir_str,"fwd")==0) |
| 208 | dir = XFRM_POLICY_FWD; |
| 209 | else |
| 210 | dir = XFRM_POLICY_ERROR; |
| 211 | return dir; |
| 212 | } |
| 213 | |
| 214 | int xfrm_mode_parse(__u8 *mode, char * mode_str) |
| 215 | { |
| 216 | |
| 217 | |
| 218 | if (strcmp(mode_str, "transport") == 0) |
| 219 | *mode = XFRM_MODE_TRANSPORT; |
| 220 | else if (strcmp(mode_str, "tunnel") == 0) |
| 221 | *mode = XFRM_MODE_TUNNEL; |
| 222 | else if (strcmp(mode_str, "ro") == 0) |
| 223 | *mode = XFRM_MODE_ROUTEOPTIMIZATION; |
| 224 | else if (strcmp(mode_str, "in_trigger") == 0) |
| 225 | *mode = XFRM_MODE_IN_TRIGGER; |
| 226 | else if (strcmp(mode_str, "beet") == 0) |
| 227 | *mode = XFRM_MODE_BEET; |
| 228 | else |
| 229 | return -1; |
| 230 | |
| 231 | |
| 232 | return 0; |
| 233 | } |
| 234 | |
| 235 | void xfrm_selector_parse(struct xfrm_selector *sel, char * src,char * dst,enum PROTOCOL_TYPE protocol,char * src_port,char * dst_port) |
| 236 | { |
| 237 | |
| 238 | inet_prefix dst_prefix; |
| 239 | inet_prefix src_prefix; |
| 240 | |
| 241 | memset(&dst_prefix, 0, sizeof(dst_prefix)); |
| 242 | memset(&src_prefix, 0, sizeof(src_prefix)); |
| 243 | |
| 244 | |
| 245 | get_prefix(&src_prefix, src, AF_UNSPEC); |
| 246 | memcpy(&sel->saddr, &src_prefix.data, sizeof(sel->saddr)); |
| 247 | sel->prefixlen_s = src_prefix.bitlen; |
| 248 | |
| 249 | get_prefix(&dst_prefix, dst, AF_UNSPEC); |
| 250 | memcpy(&sel->daddr, &dst_prefix.data, sizeof(sel->daddr)); |
| 251 | sel->prefixlen_d = dst_prefix.bitlen; |
| 252 | |
| 253 | sel->family = dst_prefix.family; |
| 254 | |
| 255 | sel->sport = htons(atoi(src_port)); |
| 256 | sel->dport = htons(atoi(dst_port)); |
| 257 | sel->dport_mask = ~((__u16)0); |
| 258 | sel->sport_mask = ~((__u16)0); |
| 259 | |
| 260 | sel->user = getpid(); |
| 261 | |
| 262 | sel->proto = protocol; |
| 263 | #ifdef INIT_ENG_BUILD |
| 264 | if(sel->family == AF_INET) |
| 265 | ALOGD("xfrm_selector_parse family:%u,prefix_d:%u,prefix_s:%u,daddr:0x%x,saddr:0x%x,sel->sport:%d,sel->dport:%d,proto:%u,user:%u\n",sel->family,sel->prefixlen_d,sel->prefixlen_s,sel->daddr.a4,sel->daddr.a4,sel->sport,sel->dport,sel->proto,sel->user); |
| 266 | else |
| 267 | ALOGD("xfrm_selector_parse family:%u,prefix_d:%u,prefix_s:%u,daddr:0x%x %x %x %x,saddr:0x%x %x %x %x ,sel->sport:%d,sel->dport:%d,proto:%u,user:%u\n",sel->family,sel->prefixlen_d,sel->prefixlen_s,sel->daddr.a6[0],sel->daddr.a6[1],sel->daddr.a6[2],sel->daddr.a6[3],sel->saddr.a6[0],sel->saddr.a6[1],sel->saddr.a6[2],sel->saddr.a6[3],sel->sport,sel->dport,sel->proto,sel->user); |
| 268 | #endif |
| 269 | } |
| 270 | |
| 271 | |
| 272 | |