aboutsummaryrefslogtreecommitdiffstats
path: root/kbx/keybox-dump.c
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--kbx/keybox-dump.c157
1 files changed, 148 insertions, 9 deletions
diff --git a/kbx/keybox-dump.c b/kbx/keybox-dump.c
index 2177bedae..d28611377 100644
--- a/kbx/keybox-dump.c
+++ b/kbx/keybox-dump.c
@@ -15,7 +15,8 @@
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
+ * USA.
*/
#include <config.h>
@@ -78,9 +79,22 @@ print_string (FILE *fp, const byte *p, size_t n, int delim)
static int
dump_header_blob (const byte *buffer, size_t length, FILE *fp)
{
+ unsigned long n;
+
+ if (length < 32)
+ {
+ fprintf (fp, "[blob too short]\n");
+ return -1;
+ }
fprintf (fp, "Version: %d\n", buffer[5]);
if ( memcmp (buffer+8, "KBXf", 4))
fprintf (fp, "[Error: invalid magic number]\n");
+
+ n = get32 (buffer+16);
+ fprintf( fp, "created-at: %lu\n", n );
+ n = get32 (buffer+20);
+ fprintf( fp, "last-maint: %lu\n", n );
+
return 0;
}
@@ -101,7 +115,7 @@ _keybox_dump_blob (KEYBOXBLOB blob, FILE *fp)
buffer = _keybox_get_blob_image (blob, &length);
- if (length < 40)
+ if (length < 32)
{
fprintf (fp, "[blob too short]\n");
return -1;
@@ -136,6 +150,12 @@ _keybox_dump_blob (KEYBOXBLOB blob, FILE *fp)
return 0;
}
fprintf (fp, "Version: %d\n", buffer[5]);
+
+ if (length < 40)
+ {
+ fprintf (fp, "[blob too short]\n");
+ return -1;
+ }
n = get16 (buffer + 6);
fprintf( fp, "Blob-Flags: %04lX", n);
@@ -290,11 +310,11 @@ _keybox_dump_blob (KEYBOXBLOB blob, FILE *fp)
fprintf (fp, "All-Validity: %d\n", p[1] );
p += 4;
n = get32 (p); p += 4;
- fprintf (fp, "Recheck-After: %s\n", /*n? strtimestamp(n) :*/ "0" );
+ fprintf (fp, "Recheck-After: %lu\n", n );
n = get32 (p ); p += 4;
- fprintf( fp, "Latest-Timestamp: %s\n", "0"/*strtimestamp(n)*/ );
+ fprintf( fp, "Latest-Timestamp: %lu\n", n );
n = get32 (p ); p += 4;
- fprintf (fp, "Created-At: %s\n", "0"/*strtimestamp(n)*/ );
+ fprintf (fp, "Created-At: %lu\n", n );
n = get32 (p ); p += 4;
fprintf (fp, "Reserved-Space: %lu\n", n );
@@ -304,14 +324,95 @@ _keybox_dump_blob (KEYBOXBLOB blob, FILE *fp)
}
+struct file_stats_s
+{
+ unsigned long too_short_blobs;
+ unsigned long too_large_blobs;
+ unsigned long total_blob_count;
+ unsigned long empty_blob_count;
+ unsigned long header_blob_count;
+ unsigned long pgp_blob_count;
+ unsigned long x509_blob_count;
+ unsigned long unknown_blob_count;
+ unsigned long non_flagged;
+ unsigned long secret_flagged;
+ unsigned long ephemeral_flagged;
+};
+
+static int
+update_stats (KEYBOXBLOB blob, struct file_stats_s *s)
+{
+ const unsigned char *buffer;
+ size_t length;
+ int type;
+ unsigned long n;
+
+ buffer = _keybox_get_blob_image (blob, &length);
+ if (length < 32)
+ {
+ s->too_short_blobs++;
+ return -1;
+ }
+
+ n = get32( buffer );
+ if (n > length)
+ s->too_large_blobs++;
+ else
+ length = n; /* ignore the rest */
+
+ s->total_blob_count++;
+ type = buffer[4];
+ switch (type)
+ {
+ case BLOBTYPE_EMPTY:
+ s->empty_blob_count++;
+ return 0;
+ case BLOBTYPE_HEADER:
+ s->header_blob_count++;
+ return 0;
+ case BLOBTYPE_PGP:
+ s->pgp_blob_count++;
+ break;
+ case BLOBTYPE_X509:
+ s->x509_blob_count++;
+ break;
+ default:
+ s->unknown_blob_count++;
+ return 0;
+ }
+
+ if (length < 40)
+ {
+ s->too_short_blobs++;
+ return -1;
+ }
+
+ n = get16 (buffer + 6);
+ if (n)
+ {
+ if ((n & 1))
+ s->secret_flagged++;
+ if ((n & 2))
+ s->ephemeral_flagged++;
+ }
+ else
+ s->non_flagged++;
+
+ return 0;
+}
+
+
int
-_keybox_dump_file (const char *filename, FILE *outfp)
+_keybox_dump_file (const char *filename, int stats_only, FILE *outfp)
{
FILE *fp;
KEYBOXBLOB blob;
int rc;
unsigned long count = 0;
+ struct file_stats_s stats;
+
+ memset (&stats, 0, sizeof stats);
if (!filename)
{
@@ -329,10 +430,17 @@ _keybox_dump_file (const char *filename, FILE *outfp)
while ( !(rc = _keybox_read_blob (&blob, fp)) )
{
- fprintf (outfp, "BEGIN-RECORD: %lu\n", count );
- _keybox_dump_blob (blob, outfp);
+ if (stats_only)
+ {
+ update_stats (blob, &stats);
+ }
+ else
+ {
+ fprintf (outfp, "BEGIN-RECORD: %lu\n", count );
+ _keybox_dump_blob (blob, outfp);
+ fprintf (outfp, "END-RECORD\n");
+ }
_keybox_release_blob (blob);
- fprintf (outfp, "END-RECORD\n");
count++;
}
if (rc == -1)
@@ -342,5 +450,36 @@ _keybox_dump_file (const char *filename, FILE *outfp)
if (fp != stdin)
fclose (fp);
+
+ if (stats_only)
+ {
+ fprintf (outfp,
+ "Total number of blobs: %8lu\n"
+ " header: %8lu\n"
+ " empty: %8lu\n"
+ " openpgp: %8lu\n"
+ " x509: %8lu\n"
+ " non flagged: %8lu\n"
+ " secret flagged: %8lu\n"
+ " ephemeral flagged: %8lu\n",
+ stats.total_blob_count,
+ stats.header_blob_count,
+ stats.empty_blob_count,
+ stats.pgp_blob_count,
+ stats.x509_blob_count,
+ stats.non_flagged,
+ stats.secret_flagged,
+ stats.ephemeral_flagged);
+ if (stats.unknown_blob_count)
+ fprintf (outfp, " unknown blob types: %8lu\n",
+ stats.unknown_blob_count);
+ if (stats.too_short_blobs)
+ fprintf (outfp, " too short blobs: %8lu\n",
+ stats.too_short_blobs);
+ if (stats.too_large_blobs)
+ fprintf (outfp, " too large blobs: %8lu\n",
+ stats.too_large_blobs);
+ }
+
return rc;
}