diff --git a/src/data-identify.c b/src/data-identify.c index eb657861..8e0295e7 100644 --- a/src/data-identify.c +++ b/src/data-identify.c @@ -1,517 +1,517 @@ /* data-identify.c - Try to identify the data * Copyright (C) 2013, 2016 g10 Code GmbH * * This file is part of GPGME. * * GPGME is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * GPGME is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this program; if not, see . * SPDX-License-Identifier: LGPL-2.1-or-later */ #if HAVE_CONFIG_H # include #endif #include #include #include "gpgme.h" #include "data.h" #include "util.h" #include "parsetlv.h" /* The size of the sample data we take for detection. */ #define SAMPLE_SIZE 2048 /* OpenPGP packet types. */ enum { PKT_NONE = 0, PKT_PUBKEY_ENC = 1, /* Public key encrypted packet. */ PKT_SIGNATURE = 2, /* Secret key encrypted packet. */ PKT_SYMKEY_ENC = 3, /* Session key packet. */ PKT_ONEPASS_SIG = 4, /* One pass sig packet. */ PKT_SECRET_KEY = 5, /* Secret key. */ PKT_PUBLIC_KEY = 6, /* Public key. */ PKT_SECRET_SUBKEY = 7, /* Secret subkey. */ PKT_COMPRESSED = 8, /* Compressed data packet. */ PKT_ENCRYPTED = 9, /* Conventional encrypted data. */ PKT_MARKER = 10, /* Marker packet. */ PKT_PLAINTEXT = 11, /* Literal data packet. */ PKT_RING_TRUST = 12, /* Keyring trust packet. */ PKT_USER_ID = 13, /* User id packet. */ PKT_PUBLIC_SUBKEY = 14, /* Public subkey. */ PKT_OLD_COMMENT = 16, /* Comment packet from an OpenPGP draft. */ PKT_ATTRIBUTE = 17, /* PGP's attribute packet. */ PKT_ENCRYPTED_MDC = 18, /* Integrity protected encrypted data. */ PKT_MDC = 19, /* Manipulation detection code packet. */ }; static inline unsigned long buf32_to_ulong (const void *buffer) { const unsigned char *p = buffer; return (((unsigned long)p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3]); } /* Parse the next openpgp packet. This function assumes a valid * OpenPGP packet at the address pointed to by BUFPTR which has a * maximum length as stored at BUFLEN. Return the header information * of that packet and advance the pointer stored at BUFPTR to the next * packet; also adjust the length stored at BUFLEN to match the * remaining bytes. If there are no more packets, store NULL at * BUFPTR. Return an non-zero error code on failure or the following * data on success: * * R_PKTTYPE = The packet type. * R_NTOTAL = The total number of bytes of this packet * * If GPG_ERR_TRUNCATED is returned, a packet type is anyway stored at * R_PKTTYPE but R_NOTAL won't have a usable value, */ static gpg_error_t next_openpgp_packet (unsigned char const **bufptr, size_t *buflen, int *r_pkttype, size_t *r_ntotal) { const unsigned char *buf = *bufptr; size_t len = *buflen; int c, ctb, pkttype; unsigned long pktlen; if (!len) return gpg_error (GPG_ERR_NO_DATA); /* First some blacklisting. */ if (len >= 4 && !memcmp (buf, "\x89PNG", 4)) return gpg_error (GPG_ERR_INV_PACKET); /* This is a PNG file. */ /* Start parsing. */ ctb = *buf++; len--; if ( !(ctb & 0x80) ) return gpg_error (GPG_ERR_INV_PACKET); /* Invalid CTB. */ if ((ctb & 0x40)) /* New style (OpenPGP) CTB. */ { pkttype = (ctb & 0x3f); if (!len) return gpg_error (GPG_ERR_INV_PACKET); /* No 1st length byte. */ c = *buf++; len--; if ( c < 192 ) pktlen = c; else if ( c < 224 ) { pktlen = (c - 192) * 256; if (!len) return gpg_error (GPG_ERR_INV_PACKET); /* No 2nd length byte. */ c = *buf++; len--; pktlen += c + 192; } else if (c == 255) { if (len < 4) return gpg_error (GPG_ERR_INV_PACKET); /* No length bytes. */ pktlen = buf32_to_ulong (buf); buf += 4; len -= 4; } else /* Partial length encoding. */ { pktlen = 0; } } else /* Old style CTB. */ { int lenbytes; pktlen = 0; pkttype = (ctb>>2)&0xf; lenbytes = ((ctb&3)==3)? 0 : (1<<(ctb & 3)); if (len < lenbytes) return gpg_error (GPG_ERR_INV_PACKET); /* Not enough length bytes. */ for (; lenbytes; lenbytes--) { pktlen <<= 8; pktlen |= *buf++; len--; } } /* Do some basic sanity check. */ switch (pkttype) { case PKT_PUBKEY_ENC: case PKT_SIGNATURE: case PKT_SYMKEY_ENC: case PKT_ONEPASS_SIG: case PKT_SECRET_KEY: case PKT_PUBLIC_KEY: case PKT_SECRET_SUBKEY: case PKT_COMPRESSED: case PKT_ENCRYPTED: case PKT_MARKER: case PKT_PLAINTEXT: case PKT_RING_TRUST: case PKT_USER_ID: case PKT_PUBLIC_SUBKEY: case PKT_OLD_COMMENT: case PKT_ATTRIBUTE: case PKT_ENCRYPTED_MDC: case PKT_MDC: break; /* Okay these are allowed packets. */ default: return gpg_error (GPG_ERR_UNEXPECTED); } if (pktlen > len) { /* Packet length header too long. This is possible because we * may have only a truncated image. */ *r_pkttype = pkttype; *r_ntotal = 0; *bufptr = NULL; return gpg_error (GPG_ERR_TRUNCATED); } *r_pkttype = pkttype; *r_ntotal = (buf - *bufptr) + pktlen; *bufptr = buf + pktlen; *buflen = len - pktlen; if (!*buflen) *bufptr = NULL; return 0; } /* Detection of PGP binary data. This function parses an OpenPGP * message. This parser is robust enough to work on a truncated * version. Returns a GPGME_DATA_TYPE_. */ static gpgme_data_type_t pgp_binary_detection (const void *image_arg, size_t imagelen) { gpg_error_t err = 0; const unsigned char *image = image_arg; size_t n; int pkttype; int anypacket = 0; int allsignatures = 0; while (!err && image) { err = next_openpgp_packet (&image, &imagelen, &pkttype, &n); if (gpg_err_code (err) == GPG_ERR_TRUNCATED) ; else if (err) break; /* Skip all leading marker packets. */ if (!anypacket && pkttype == PKT_MARKER) continue; if (pkttype == PKT_SIGNATURE) { if (!anypacket) allsignatures = 1; } else allsignatures = 0; switch (pkttype) { case PKT_SIGNATURE: break; /* We decide later. */ case PKT_PLAINTEXT: /* Old style signature format: {sig}+,plaintext */ if (allsignatures) return GPGME_DATA_TYPE_PGP_SIGNED; break; case PKT_ONEPASS_SIG: return GPGME_DATA_TYPE_PGP_SIGNED; case PKT_SECRET_KEY: case PKT_PUBLIC_KEY: return GPGME_DATA_TYPE_PGP_KEY; case PKT_SECRET_SUBKEY: case PKT_PUBLIC_SUBKEY: return GPGME_DATA_TYPE_PGP_OTHER; case PKT_PUBKEY_ENC: case PKT_SYMKEY_ENC: return GPGME_DATA_TYPE_PGP_ENCRYPTED; case PKT_COMPRESSED: /* If this is the first packet we assume that that a signed * packet follows. We do not want to uncompress it here due * to the need of a lot of code and the potential DoS. */ if (!anypacket) return GPGME_DATA_TYPE_PGP_SIGNED; return GPGME_DATA_TYPE_PGP_OTHER; default: return GPGME_DATA_TYPE_PGP_OTHER; } anypacket = 1; } if (allsignatures) return GPGME_DATA_TYPE_PGP_SIGNATURE; return GPGME_DATA_TYPE_UNKNOWN; } /* This is probably an armored "PGP MESSAGE" which can encode * different PGP data types. STRING is modified after a call to this * function. */ static gpgme_data_type_t inspect_pgp_message (char *string) { struct b64state state; size_t nbytes; if (_gpgme_b64dec_start (&state, "")) return GPGME_DATA_TYPE_INVALID; /* oops */ if (_gpgme_b64dec_proc (&state, string, strlen (string), &nbytes)) { _gpgme_b64dec_finish (&state); return GPGME_DATA_TYPE_UNKNOWN; /* bad encoding etc. */ } _gpgme_b64dec_finish (&state); string[nbytes] = 0; /* Better append a Nul. */ return pgp_binary_detection (string, nbytes); } /* Note that DATA may be binary but a final nul is required so that string operations will find a terminator. Returns: GPGME_DATA_TYPE_xxxx */ static gpgme_data_type_t basic_detection (char *data, size_t datalen) { tlvinfo_t ti; const char *s; size_t n; int maybe_p12 = 0; if (datalen < 24) /* Object is probably too short for detection. */ return GPGME_DATA_TYPE_UNKNOWN; /* This is a common example of a CMS object - it is obvious that we only need to read a few bytes to get to the OID: 30 82 0B 59 06 09 2A 86 48 86 F7 0D 01 07 02 A0 82 0B 4A 30 82 0B 46 02 ----------- ++++++++++++++++++++++++++++++++ SEQUENCE OID (signedData) (2 byte len) A PKCS#12 message is: 30 82 08 59 02 01 03 30 82 08 1F 06 09 2A 86 48 86 F7 0D 01 07 01 A0 82 ----------- ++++++++ ----------- ++++++++++++++++++++++++++++++++ SEQUENCE INTEGER SEQUENCE OID (data) A X.509 certificate is: 30 82 05 B8 30 82 04 A0 A0 03 02 01 02 02 07 15 46 A0 BF 30 07 39 30 0D ----------- +++++++++++ ----- ++++++++ -------------------------- SEQUENCE SEQUENCE [0] INTEGER INTEGER SEQU (tbs) (version) (s/n) (Algo) Thus we need to read at least 22 bytes, we add 2 bytes to cope with length headers stored with 4 bytes. */ s = data; n = datalen; if (parse_tlv (&s, &n, &ti)) goto try_pgp; /* Not properly BER encoded. */ if (!(ti.cls == ASN1_CLASS_UNIVERSAL && ti.tag == ASN1_TAG_SEQUENCE && ti.is_cons)) goto try_pgp; /* A CMS object always starts with a sequence. */ if (parse_tlv (&s, &n, &ti)) goto try_pgp; /* Not properly BER encoded. */ if (ti.cls == ASN1_CLASS_UNIVERSAL && ti.tag == ASN1_TAG_SEQUENCE && ti.is_cons && n >= ti.length) { if (parse_tlv (&s, &n, &ti)) goto try_pgp; if (!(ti.cls == ASN1_CLASS_CONTEXT && ti.tag == 0 && ti.is_cons && ti.length == 3 && n >= ti.length)) goto try_pgp; if (parse_tlv (&s, &n, &ti)) goto try_pgp; if (!(ti.cls == ASN1_CLASS_UNIVERSAL && ti.tag == ASN1_TAG_INTEGER && !ti.is_cons && ti.length == 1 && n && (*s == 1 || *s == 2))) goto try_pgp; s++; n--; if (!(ti.cls == ASN1_CLASS_UNIVERSAL && ti.tag == ASN1_TAG_INTEGER && !ti.is_cons)) goto try_pgp; /* Because the now following S/N may be larger than the sample data we have, we stop parsing here and don't check for the algorithm ID. */ return GPGME_DATA_TYPE_X509_CERT; } if (ti.cls == ASN1_CLASS_UNIVERSAL && ti.tag == ASN1_TAG_INTEGER && !ti.is_cons && ti.length == 1 && n && *s == 3) { maybe_p12 = 1; s++; n--; if (parse_tlv (&s, &n, &ti)) goto try_pgp; if (!(ti.cls == ASN1_CLASS_UNIVERSAL && ti.tag == ASN1_TAG_SEQUENCE && ti.is_cons)) goto try_pgp; if (parse_tlv (&s, &n, &ti)) goto try_pgp; } if (ti.cls == ASN1_CLASS_UNIVERSAL && ti.tag == ASN1_TAG_OBJECT_ID && !ti.is_cons && ti.length && n >= ti.length) { if (ti.length == 9) { if (!memcmp (s, "\x2A\x86\x48\x86\xF7\x0D\x01\x07\x01", 9)) { /* Data. */ return (maybe_p12 ? GPGME_DATA_TYPE_PKCS12 /* */ : GPGME_DATA_TYPE_CMS_OTHER); } if (!memcmp (s, "\x2A\x86\x48\x86\xF7\x0D\x01\x07\x02", 9)) { /* Signed Data. */ return (maybe_p12 ? GPGME_DATA_TYPE_PKCS12 /* */ : GPGME_DATA_TYPE_CMS_SIGNED); } if (!memcmp (s, "\x2A\x86\x48\x86\xF7\x0D\x01\x07\x03", 9)) return GPGME_DATA_TYPE_CMS_ENCRYPTED; /* Enveloped Data. */ if (!memcmp (s, "\x2A\x86\x48\x86\xF7\x0D\x01\x07\x05", 9)) return GPGME_DATA_TYPE_CMS_OTHER; /* Digested Data. */ if (!memcmp (s, "\x2A\x86\x48\x86\xF7\x0D\x01\x07\x06", 9)) return GPGME_DATA_TYPE_CMS_OTHER; /* Encrypted Data. */ } else if (ti.length == 11) { - if (!memcmp (s, "\x2A\x86\x48\x86\xF7\x0D\x01\x09\x10\x01\x02", 11)) - return GPGME_DATA_TYPE_CMS_OTHER; /* Auth Data. */ + if (!memcmp (s, "\x2A\x86\x48\x86\xF7\x0D\x01\x09\x10\x01\x17", 11)) + return GPGME_DATA_TYPE_CMS_ENCRYPTED; /* AuthEnveloped Data. */ } } try_pgp: /* Check whether this might be a non-armored PGP message. We need to do this before checking for armor lines, so that we don't get fooled by armored messages inside a signed binary PGP message. */ if ((data[0] & 0x80)) { /* That might be a binary PGP message. At least it is not plain ASCII. Of course this might be certain lead-in text of armored CMS messages. However, I am not sure whether this is at all defined and in any case it is uncommon. Thus we don't do any further plausibility checks but stupidly assume no CMS armored data will follow. */ return pgp_binary_detection (data, datalen); } /* Now check whether there are armor lines. */ for (s = data; s && *s; s = (*s=='\n')?(s+1):((s=strchr (s,'\n'))?(s+1):s)) { if (!strncmp (s, "-----BEGIN ", 11)) { if (!strncmp (s+11, "SIGNED ", 7)) return GPGME_DATA_TYPE_CMS_SIGNED; if (!strncmp (s+11, "ENCRYPTED ", 10)) return GPGME_DATA_TYPE_CMS_ENCRYPTED; if (!strncmp (s+11, "PGP ", 4)) { if (!strncmp (s+15, "SIGNATURE", 9)) return GPGME_DATA_TYPE_PGP_SIGNATURE; if (!strncmp (s+15, "SIGNED MESSAGE", 14)) return GPGME_DATA_TYPE_PGP_SIGNED; if (!strncmp (s+15, "PUBLIC KEY BLOCK", 16)) return GPGME_DATA_TYPE_PGP_KEY; if (!strncmp (s+15, "PRIVATE KEY BLOCK", 17)) return GPGME_DATA_TYPE_PGP_KEY; if (!strncmp (s+15, "SECRET KEY BLOCK", 16)) return GPGME_DATA_TYPE_PGP_KEY; if (!strncmp (s+15, "ARMORED FILE", 12)) return GPGME_DATA_TYPE_UNKNOWN; return inspect_pgp_message (data); } if (!strncmp (s+11, "CERTIFICATE", 11)) return GPGME_DATA_TYPE_X509_CERT; if (!strncmp (s+11, "PKCS12", 6)) return GPGME_DATA_TYPE_PKCS12; return GPGME_DATA_TYPE_CMS_OTHER; /* Not PGP, thus we assume CMS. */ } } return GPGME_DATA_TYPE_UNKNOWN; } /* Try to detect the type of the data. Note that this function works only on seekable data objects. The function tries to reset the file pointer but there is no guarantee that it will work. FIXME: We may want to add internal buffering so that this function can be implemented for almost all kind of data objects. */ gpgme_data_type_t gpgme_data_identify (gpgme_data_t dh, int reserved) { gpgme_data_type_t result; char *sample; int n; gpgme_off_t off; (void)reserved; /* Check whether we can seek the data object. */ off = gpgme_data_seek (dh, 0, SEEK_CUR); if (off == (gpgme_off_t)(-1)) return GPGME_DATA_TYPE_INVALID; /* Allocate a buffer and read the data. */ sample = malloc (SAMPLE_SIZE); if (!sample) return GPGME_DATA_TYPE_INVALID; /* Ooops. */ n = gpgme_data_read (dh, sample, SAMPLE_SIZE - 1); if (n < 0) { free (sample); return GPGME_DATA_TYPE_INVALID; /* Ooops. */ } sample[n] = 0; /* (Required for our string functions.) */ result = basic_detection (sample, n); free (sample); gpgme_data_seek (dh, off, SEEK_SET); return result; } diff --git a/src/data.c b/src/data.c index 70595907..14652938 100644 --- a/src/data.c +++ b/src/data.c @@ -1,663 +1,663 @@ /* data.c - An abstraction for data objects. * Copyright (C) 2002, 2003, 2004, 2005, 2007 g10 Code GmbH * * This file is part of GPGME. * * GPGME is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * GPGME is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this program; if not, see . * SPDX-License-Identifier: LGPL-2.1-or-later */ #if HAVE_CONFIG_H #include #endif #include #ifdef HAVE_UNISTD_H # include #endif #include #include #include #include "gpgme.h" #include "data.h" #include "util.h" #include "ops.h" #include "priv-io.h" #include "debug.h" /* The property table which has an entry for each active data object. * The data object itself uses an index into this table and the table * has a pointer back to the data object. All access to that table is * controlled by the property_table_lock. * * We use a separate table instead of linking all data objects * together for faster locating properties of the data object using * the data objects serial number. We use 64 bit for the serial * number which is good enough to create a new data object every * nanosecond for more than 500 years. Thus no wrap around will ever * happen. */ struct property_s { - gpgme_data_t dh; /* The data objcet or NULL if the slot is not used. */ + gpgme_data_t dh; /* The data object or NULL if the slot is not used. */ uint64_t dserial; /* The serial number of the data object. */ struct { unsigned int blankout : 1; /* Void the held data. */ } flags; }; typedef struct property_s *property_t; static property_t property_table; static unsigned int property_table_size; DEFINE_STATIC_LOCK (property_table_lock); #define PROPERTY_TABLE_ALLOCATION_CHUNK 32 /* Insert the newly created data object DH into the property table and * store the index of it at R_IDX. An error code is returned on error * and the table is not changed. */ static gpg_error_t insert_into_property_table (gpgme_data_t dh, unsigned int *r_idx) { static uint64_t last_dserial; gpg_error_t err; unsigned int idx; LOCK (property_table_lock); if (!property_table) { property_table_size = PROPERTY_TABLE_ALLOCATION_CHUNK; property_table = calloc (property_table_size, sizeof *property_table); if (!property_table) { err = gpg_error_from_syserror (); goto leave; } } /* Find an empty slot. */ for (idx = 0; idx < property_table_size; idx++) if (!property_table[idx].dh) break; if (!(idx < property_table_size)) { /* No empty slot found. Enlarge the table. */ property_t newtbl; unsigned int newsize; newsize = property_table_size + PROPERTY_TABLE_ALLOCATION_CHUNK;; if ((newsize * sizeof *property_table) < (property_table_size * sizeof *property_table)) { err = gpg_error (GPG_ERR_ENOMEM); goto leave; } newtbl = realloc (property_table, newsize * sizeof *property_table); if (!newtbl) { err = gpg_error_from_syserror (); goto leave; } property_table = newtbl; for (idx = property_table_size; idx < newsize; idx++) property_table[idx].dh = NULL; idx = property_table_size; property_table_size = newsize; } /* Slot found. */ property_table[idx].dh = dh; property_table[idx].dserial = ++last_dserial; memset (&property_table[idx].flags, 0, sizeof property_table[idx].flags); *r_idx = idx; err = 0; leave: UNLOCK (property_table_lock); return err; } /* Remove the data object at PROPIDX from the table. DH is only used * for cross checking. */ static void remove_from_property_table (gpgme_data_t dh, unsigned int propidx) { LOCK (property_table_lock); assert (property_table); assert (propidx < property_table_size); assert (property_table[propidx].dh == dh); property_table[propidx].dh = NULL; UNLOCK (property_table_lock); } /* Return the data object's serial number for handle DH. This is a * unique serial number for each created data object. */ uint64_t _gpgme_data_get_dserial (gpgme_data_t dh) { uint64_t dserial; unsigned int idx; if (!dh) return 0; idx = dh->propidx; LOCK (property_table_lock); assert (property_table); assert (idx < property_table_size); assert (property_table[idx].dh == dh); dserial = property_table[idx].dserial; UNLOCK (property_table_lock); return dserial; } /* Set an internal property of a data object. The data object may * either be identified by the usual DH or by using the data serial * number DSERIAL. */ gpg_error_t _gpgme_data_set_prop (gpgme_data_t dh, uint64_t dserial, data_prop_t name, int value) { gpg_error_t err = 0; int idx; TRACE_BEG (DEBUG_DATA, "gpgme_data_set_prop", dh, "dserial=%llu %lu=%d", (unsigned long long)dserial, (unsigned long)name, value); LOCK (property_table_lock); if ((!dh && !dserial) || (dh && dserial)) { err = gpg_error (GPG_ERR_INV_VALUE); goto leave; } if (dh) /* Lookup via handle. */ { idx = dh->propidx; assert (property_table); assert (idx < property_table_size); assert (property_table[idx].dh == dh); } else /* Lookup via DSERIAL. */ { if (!property_table) { err = gpg_error (GPG_ERR_NOT_FOUND); goto leave; } for (idx = 0; idx < property_table_size; idx++) if (property_table[idx].dh && property_table[idx].dserial == dserial) break; if (!(idx < property_table_size)) { err = gpg_error (GPG_ERR_NOT_FOUND); goto leave; } } switch (name) { case DATA_PROP_NONE: /* Nothing to to do. */ break; case DATA_PROP_BLANKOUT: property_table[idx].flags.blankout = !!value; break; default: err = gpg_error (GPG_ERR_UNKNOWN_NAME); break; } leave: UNLOCK (property_table_lock); return TRACE_ERR (err); } /* Get an internal property of a data object. This is the counter * part to _gpgme_data_set_property. The value of the property is * stored at R_VALUE. On error 0 is stored at R_VALUE. */ gpg_error_t _gpgme_data_get_prop (gpgme_data_t dh, uint64_t dserial, data_prop_t name, int *r_value) { gpg_error_t err = 0; int idx; TRACE_BEG (DEBUG_DATA, "gpgme_data_get_prop", dh, "dserial=%llu %lu", (unsigned long long)dserial, (unsigned long)name); *r_value = 0; LOCK (property_table_lock); if ((!dh && !dserial) || (dh && dserial)) { err = gpg_error (GPG_ERR_INV_VALUE); goto leave; } if (dh) /* Lookup via handle. */ { idx = dh->propidx; assert (property_table); assert (idx < property_table_size); assert (property_table[idx].dh == dh); } else /* Lookup via DSERIAL. */ { if (!property_table) { err = gpg_error (GPG_ERR_NOT_FOUND); goto leave; } for (idx = 0; idx < property_table_size; idx++) if (property_table[idx].dh && property_table[idx].dserial == dserial) break; if (!(idx < property_table_size)) { err = gpg_error (GPG_ERR_NOT_FOUND); goto leave; } } switch (name) { case DATA_PROP_NONE: /* Nothing to to do. */ break; case DATA_PROP_BLANKOUT: *r_value = property_table[idx].flags.blankout; break; default: err = gpg_error (GPG_ERR_UNKNOWN_NAME); break; } leave: UNLOCK (property_table_lock); return TRACE_ERR (err); } gpgme_error_t _gpgme_data_new (gpgme_data_t *r_dh, struct _gpgme_data_cbs *cbs) { gpgme_error_t err; gpgme_data_t dh; if (!r_dh) return gpg_error (GPG_ERR_INV_VALUE); *r_dh = NULL; if (_gpgme_selftest) return _gpgme_selftest; dh = calloc (1, sizeof (*dh)); if (!dh) return gpg_error_from_syserror (); dh->cbs = cbs; err = insert_into_property_table (dh, &dh->propidx); if (err) { free (dh); return err; } *r_dh = dh; return 0; } void _gpgme_data_release (gpgme_data_t dh) { if (!dh) return; remove_from_property_table (dh, dh->propidx); if (dh->file_name) free (dh->file_name); free (dh); } /* Read up to SIZE bytes into buffer BUFFER from the data object with the handle DH. Return the number of characters read, 0 on EOF and -1 on error. If an error occurs, errno is set. */ gpgme_ssize_t gpgme_data_read (gpgme_data_t dh, void *buffer, size_t size) { gpgme_ssize_t res; int blankout; TRACE_BEG (DEBUG_DATA, "gpgme_data_read", dh, "buffer=%p, size=%zu", buffer, size); if (!dh) { gpg_err_set_errno (EINVAL); return TRACE_SYSRES (-1); } if (!dh->cbs->read) { gpg_err_set_errno (ENOSYS); return TRACE_SYSRES (-1); } if (_gpgme_data_get_prop (dh, 0, DATA_PROP_BLANKOUT, &blankout) || blankout) res = 0; else { do res = (*dh->cbs->read) (dh, buffer, size); while (res < 0 && errno == EINTR); } return TRACE_SYSRES ((int)res); } /* Write up to SIZE bytes from buffer BUFFER to the data object with the handle DH. Return the number of characters written, or -1 on error. If an error occurs, errno is set. */ gpgme_ssize_t gpgme_data_write (gpgme_data_t dh, const void *buffer, size_t size) { gpgme_ssize_t res; TRACE_BEG (DEBUG_DATA, "gpgme_data_write", dh, "buffer=%p, size=%zu", buffer, size); if (!dh) { gpg_err_set_errno (EINVAL); return TRACE_SYSRES (-1); } if (!dh->cbs->write) { gpg_err_set_errno (ENOSYS); return TRACE_SYSRES (-1); } do res = (*dh->cbs->write) (dh, buffer, size); while (res < 0 && errno == EINTR); return TRACE_SYSRES ((int)res); } /* Set the current position from where the next read or write starts in the data object with the handle DH to OFFSET, relative to WHENCE. */ gpgme_off_t gpgme_data_seek (gpgme_data_t dh, gpgme_off_t offset, int whence) { TRACE_BEG (DEBUG_DATA, "gpgme_data_seek", dh, "offset=%lli, whence=%i", (long long int)offset, whence); if (!dh) { gpg_err_set_errno (EINVAL); return TRACE_SYSRES (-1); } if (!dh->cbs->seek) { gpg_err_set_errno (ENOSYS); return TRACE_SYSRES (-1); } /* For relative movement, we must take into account the actual position of the read counter. */ if (whence == SEEK_CUR) offset -= dh->pending_len; offset = (*dh->cbs->seek) (dh, offset, whence); if (offset >= 0) dh->pending_len = 0; return TRACE_SYSRES ((int)offset); } /* Convenience function to do a gpgme_data_seek (dh, 0, SEEK_SET). */ gpgme_error_t gpgme_data_rewind (gpgme_data_t dh) { gpgme_error_t err; TRACE_BEG (DEBUG_DATA, "gpgme_data_rewind", dh, ""); err = ((gpgme_data_seek (dh, 0, SEEK_SET) == -1) ? gpg_error_from_syserror () : 0); return TRACE_ERR (err); } /* Release the data object with the handle DH. */ void gpgme_data_release (gpgme_data_t dh) { TRACE (DEBUG_DATA, "gpgme_data_release", dh, ""); if (!dh) return; if (dh->cbs->release) (*dh->cbs->release) (dh); _gpgme_data_release (dh); } /* Get the current encoding meta information for the data object with handle DH. */ gpgme_data_encoding_t gpgme_data_get_encoding (gpgme_data_t dh) { TRACE (DEBUG_DATA, "gpgme_data_get_encoding", dh, "dh->encoding=%i", dh ? dh->encoding : GPGME_DATA_ENCODING_NONE); return dh ? dh->encoding : GPGME_DATA_ENCODING_NONE; } /* Set the encoding meta information for the data object with handle DH to ENC. */ gpgme_error_t gpgme_data_set_encoding (gpgme_data_t dh, gpgme_data_encoding_t enc) { TRACE_BEG (DEBUG_DATA, "gpgme_data_set_encoding", dh, "encoding=%i", enc); if (!dh) return TRACE_ERR (gpg_error (GPG_ERR_INV_VALUE)); if (enc < 0 || enc > GPGME_DATA_ENCODING_MIME) return TRACE_ERR (gpg_error (GPG_ERR_INV_VALUE)); dh->encoding = enc; return TRACE_ERR (0); } /* Set the file name associated with the data object with handle DH to FILE_NAME. */ gpgme_error_t gpgme_data_set_file_name (gpgme_data_t dh, const char *file_name) { TRACE_BEG (DEBUG_DATA, "gpgme_data_set_file_name", dh, "file_name=%s", file_name); if (!dh) return TRACE_ERR (gpg_error (GPG_ERR_INV_VALUE)); if (dh->file_name) free (dh->file_name); if (file_name) { dh->file_name = strdup (file_name); if (!dh->file_name) return TRACE_ERR (gpg_error_from_syserror ()); } else dh->file_name = 0; return TRACE_ERR (0); } /* Get the file name associated with the data object with handle DH, or NULL if there is none. */ char * gpgme_data_get_file_name (gpgme_data_t dh) { if (!dh) { TRACE (DEBUG_DATA, "gpgme_data_get_file_name", dh, ""); return NULL; } TRACE (DEBUG_DATA, "gpgme_data_get_file_name", dh, "dh->file_name=%s", dh->file_name); return dh->file_name; } /* Set a flag for the data object DH. See the manual for details. */ gpg_error_t gpgme_data_set_flag (gpgme_data_t dh, const char *name, const char *value) { TRACE_BEG (DEBUG_DATA, "gpgme_data_set_flag", dh, "%s=%s", name, value); if (!dh) return TRACE_ERR (gpg_error (GPG_ERR_INV_VALUE)); if (!strcmp (name, "size-hint")) { dh->size_hint= value? _gpgme_string_to_off (value) : 0; } else return gpg_error (GPG_ERR_UNKNOWN_NAME); return 0; } /* Functions to support the wait interface. */ gpgme_error_t _gpgme_data_inbound_handler (void *opaque, int fd) { struct io_cb_data *data = (struct io_cb_data *) opaque; gpgme_data_t dh = (gpgme_data_t) data->handler_value; char buffer[BUFFER_SIZE]; char *bufp = buffer; gpgme_ssize_t buflen; TRACE_BEG (DEBUG_CTX, "_gpgme_data_inbound_handler", dh, "fd=%d", fd); buflen = _gpgme_io_read (fd, buffer, BUFFER_SIZE); if (buflen < 0) return gpg_error_from_syserror (); if (buflen == 0) { _gpgme_io_close (fd); return TRACE_ERR (0); } do { gpgme_ssize_t amt = gpgme_data_write (dh, bufp, buflen); if (amt == 0 || (amt < 0 && errno != EINTR)) return TRACE_ERR (gpg_error_from_syserror ()); bufp += amt; buflen -= amt; } while (buflen > 0); return TRACE_ERR (0); } gpgme_error_t _gpgme_data_outbound_handler (void *opaque, int fd) { struct io_cb_data *data = (struct io_cb_data *) opaque; gpgme_data_t dh = (gpgme_data_t) data->handler_value; gpgme_ssize_t nwritten; TRACE_BEG (DEBUG_CTX, "_gpgme_data_outbound_handler", dh, "fd=%d", fd); if (!dh->pending_len) { gpgme_ssize_t amt = gpgme_data_read (dh, dh->pending, BUFFER_SIZE); if (amt < 0) return TRACE_ERR (gpg_error_from_syserror ()); if (amt == 0) { _gpgme_io_close (fd); return TRACE_ERR (0); } dh->pending_len = amt; } nwritten = _gpgme_io_write (fd, dh->pending, dh->pending_len); if (nwritten == -1 && errno == EAGAIN) return TRACE_ERR (0); if (nwritten == -1 && errno == EPIPE) { /* Not much we can do. The other end closed the pipe, but we still have data. This should only ever happen if the other end is going to tell us what happened on some other channel. Silently close our end. */ _gpgme_io_close (fd); return TRACE_ERR (0); } if (nwritten <= 0) return TRACE_ERR (gpg_error_from_syserror ()); if (nwritten < dh->pending_len) memmove (dh->pending, dh->pending + nwritten, dh->pending_len - nwritten); dh->pending_len -= nwritten; return TRACE_ERR (0); } /* Get the file descriptor associated with DH, if possible. Otherwise return -1. */ int _gpgme_data_get_fd (gpgme_data_t dh) { if (!dh || !dh->cbs->get_fd) return -1; return (*dh->cbs->get_fd) (dh); } /* Get the size-hint value for DH or 0 if not available. */ gpgme_off_t _gpgme_data_get_size_hint (gpgme_data_t dh) { return dh ? dh->size_hint : 0; }