iwd-decrypt-profile.c (4912B)
1 /* 2 * 3 * Wireless daemon for Linux 4 * 5 * Copyright (C) 2022 Intel Corporation. All rights reserved. 6 * 7 * This library is free software; you can redistribute it and/or 8 * modify it under the terms of the GNU Lesser General Public 9 * License as published by the Free Software Foundation; either 10 * version 2.1 of the License, or (at your option) any later version. 11 * 12 * This library is distributed in the hope that it will be useful, 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 15 * Lesser General Public License for more details. 16 * 17 * You should have received a copy of the GNU Lesser General Public 18 * License along with this library; if not, write to the Free Software 19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 20 * 21 */ 22 23 #include <stdio.h> 24 #include <unistd.h> 25 #include <fcntl.h> 26 #include <sys/stat.h> 27 #include <sys/mman.h> 28 #include <getopt.h> 29 30 #include <ell/ell.h> 31 32 #include "ell/useful.h" 33 34 #include "src/storage.h" 35 #include "src/common.h" 36 37 static void usage(void) 38 { 39 printf("decrypt-profile - Decrypt a network profile\n" 40 "Usage:\n"); 41 printf("\tdecrypt-profile [--pass | --file] [OPTIONS]\n"); 42 printf("\n\tEither --pass or --file must be provided. The profile\n"); 43 printf("\tshould be supplied using --infile.\n"); 44 printf("\tThe --name argument must be used if the name cannot be\n"); 45 printf("\tinferred from the input file\n\n"); 46 printf("Options:\n" 47 "\t-p, --pass Password/key used to encrypt\n" 48 "\t-f, --file File containing key\n" 49 "\t-s, --name Name for associated profile (will\n" 50 "\t be inferred from --infile if not\n" 51 "\t provided). For non hotspot networks\n" 52 "\t this will be the SSID.\n" 53 "\t-i, --infile Input profile\n" 54 "\t-o, --outfile Output file for decrypted profile\n" 55 "\t-h, --help Show help options\n"); 56 printf("\n"); 57 } 58 59 static const struct option main_options[] = { 60 { "pass", required_argument, NULL, 'p' }, 61 { "file", required_argument, NULL, 'f' }, 62 { "infile", required_argument, NULL, 'i' }, 63 { "outfile", required_argument, NULL, 'o' }, 64 { "name", required_argument, NULL, 'n' }, 65 { "help", no_argument, NULL, 'h' }, 66 { } 67 }; 68 69 static bool secret_from_file(const char *file) 70 { 71 int fd; 72 struct stat st; 73 void *data = NULL; 74 bool r; 75 76 fd = open(file, O_RDONLY, 0); 77 if (fd < 0) { 78 printf("Can't open %s (%d)\n", file, fd); 79 return false; 80 } 81 82 if (fstat(fd, &st) < 0 || st.st_size == 0) { 83 close(fd); 84 return false; 85 } 86 87 data = mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, fd, 0); 88 close(fd); 89 90 if (data == MAP_FAILED) 91 return false; 92 93 r = storage_init(data, st.st_size); 94 95 munmap(data, st.st_size); 96 97 return r; 98 } 99 100 int main(int argc, char *argv[]) 101 { 102 const char *pass = NULL; 103 const char *file = NULL; 104 const char *infile = NULL; 105 const char *outfile = NULL; 106 const char *name = NULL; 107 _auto_(l_free) char *decrypted = NULL; 108 _auto_(l_settings_free) struct l_settings *settings = NULL; 109 enum security sec; 110 int ret = EXIT_FAILURE; 111 ssize_t len = 0; 112 int r; 113 114 for (;;) { 115 int opt; 116 117 opt = getopt_long(argc, argv, "pfhion", main_options, NULL); 118 if (opt < 0) 119 break; 120 121 switch (opt) { 122 case 'p': 123 pass = optarg; 124 break; 125 case 'f': 126 file = optarg; 127 break; 128 case 'i': 129 infile = optarg; 130 break; 131 case 'o': 132 outfile = optarg; 133 break; 134 case 'n': 135 name = optarg; 136 break; 137 case 'h': 138 usage(); 139 return EXIT_SUCCESS; 140 default: 141 goto failed; 142 } 143 } 144 145 if (!file && !pass) { 146 printf("--file or --pass must be supplied\n\n"); 147 goto usage; 148 } 149 150 if (!infile) { 151 printf("--infile must be supplied\n\n"); 152 goto usage; 153 } 154 155 if (!name) { 156 name = storage_network_ssid_from_path(infile, &sec); 157 if (!name) { 158 printf("Can't get name from --infile, use --name\n\n"); 159 goto usage; 160 } 161 } 162 163 settings = l_settings_new(); 164 165 if (!l_settings_load_from_file(settings, infile)) { 166 printf("Profile is not formatted correctly\n"); 167 goto failed; 168 } 169 170 if (pass) { 171 if (!storage_init((const uint8_t *)pass, strlen(pass))) 172 goto failed; 173 } else if (!secret_from_file(file)) 174 goto failed; 175 176 r = __storage_decrypt(settings, name, NULL); 177 if (r < 0) { 178 printf("Unable to decrypt profile (%d)\n", r); 179 goto failed; 180 } 181 182 decrypted = l_settings_to_data(settings, (size_t *)&len); 183 184 if (!outfile) 185 fwrite(decrypted, 1, len, stdout); 186 else { 187 int fd = open(outfile, O_CREAT | O_WRONLY, S_IRUSR | S_IWUSR); 188 189 if (fd < 0) { 190 printf("Unable to open %s (%d)\n", outfile, fd); 191 goto failed; 192 } 193 194 len = write(fd, decrypted, len); 195 196 close(fd); 197 198 if (len < 0) { 199 printf("Unable to write to %s (%zd)\n", outfile, len); 200 goto failed; 201 } 202 } 203 204 ret = EXIT_SUCCESS; 205 206 usage: 207 if (ret != EXIT_SUCCESS) 208 usage(); 209 210 failed: 211 return ret; 212 }