2 * key-store.c - High level routines to load + save OpenPGP packets/keys
4 * This program is free software: you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the Free
6 * Software Foundation; version 2 of the License.
8 * This program is distributed in the hope that it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * You should have received a copy of the GNU General Public License along with
14 * this program. If not, see <https://www.gnu.org/licenses/>.
21 #include "build-config.h"
24 #include "charfuncs.h"
25 #include "key-store.h"
26 #include "keystructs.h"
31 * onak_read_openpgp_file - Reads a set of OpenPGP packets from a file
32 * @file: The file to open and read
33 * @packets: The returned packet list
35 * This function opens the supplied file and tries to parse it as a set
36 * of OpenPGP packets. It will attempt to autodetect if the file is ASCII
37 * armored, or binary packets, and adapt accordingly. The packets read are
38 * returned in the packets parameter. It is the callers responsbility to
39 * free the packet memory when it is no longe required, e.g. using
42 * Returns a status code indicating any error.
44 onak_status_t onak_read_openpgp_file(const char *file,
45 struct openpgp_packet_list **packets)
51 fd = open(file, O_RDONLY);
53 return (errno == ENOENT) ? ONAK_E_NOT_FOUND : ONAK_E_IO_ERROR;
56 /* Peek at the first byte in the file */
57 ret = read(fd, &c, 1);
59 return ONAK_E_IO_ERROR;
61 lseek(fd, 0, SEEK_SET);
64 * A binary OpenPGP packet will have the top bit set on its first byte,
65 * so we use that to determine if we should try to process the stream
66 * as binary or ASCII armored data.
69 res = read_openpgp_stream(file_fetchchar, &fd,
72 res = dearmor_openpgp_stream(file_fetchchar, &fd, packets);