Do not print line number in debug messages
[project/ucert.git] / ucert.c
diff --git a/ucert.c b/ucert.c
index 89bf0c64d4b500e971f23e70125c588a806b0563..5523b02a7eb2b977ce4c28bfd3b4c082845d6513 100644 (file)
--- a/ucert.c
+++ b/ucert.c
@@ -49,10 +49,10 @@ static enum {
 
 static bool quiet;
 #ifndef UCERT_STRIP_MESSAGES
-#define DPRINTF(format, ...)                                                                   \
-       do {                                                                                    \
-               if (!quiet)                                                                     \
-                       fprintf(stderr, "%s(%d): " format, __func__, __LINE__, ## __VA_ARGS__); \
+#define DPRINTF(format, ...)                                                           \
+       do {                                                                            \
+               if (!quiet)                                                             \
+                       fprintf(stderr, "%s: " format, __func__, ## __VA_ARGS__);       \
        } while (0)
 #else
 #define DPRINTF(format, ...) do { } while (0)
@@ -164,9 +164,8 @@ static int cert_load(const char *certfile, struct list_head *chain) {
        struct blob_attr *certtb[CERT_ATTR_MAX];
        struct blob_attr *bufpt;
        struct cert_object *cobj;
-       char filebuf[CERT_BUF_LEN];
-       int ret = 0, pret = 0;
-       size_t pos = 0;
+       char filebuf[CERT_BUF_LEN], *end;
+       int ret = 1;
        ssize_t len;
 
        len = read_file(certfile, filebuf, sizeof(filebuf) - 1, 0);
@@ -177,17 +176,16 @@ static int cert_load(const char *certfile, struct list_head *chain) {
        }
 
        bufpt = (struct blob_attr *)filebuf;
-       do {
-               pret = blob_parse_untrusted(bufpt, len, certtb, cert_policy, CERT_ATTR_MAX);
-               if (pret <= 0)
-                       /* no attributes found */
+       end = filebuf + len;
+
+       while (true) {
+               len = end - (char *)bufpt;
+               if (len <= 0)
                        break;
 
-               if (pos + blob_pad_len(bufpt) > (size_t) len)
-                       /* blob exceeds filebuffer */
+               if (blob_parse_untrusted(bufpt, len, certtb, cert_policy, CERT_ATTR_MAX) <= 0)
+                       /* no attributes found */
                        break;
-               else
-                       pos += blob_pad_len(bufpt);
 
                if (!certtb[CERT_ATTR_SIGNATURE])
                        /* no signature -> drop */
@@ -199,11 +197,17 @@ static int cert_load(const char *certfile, struct list_head *chain) {
                        cobj->cert[CERT_ATTR_PAYLOAD] = blob_memdup(certtb[CERT_ATTR_PAYLOAD]);
 
                list_add_tail(&cobj->list, chain);
-               ret += pret;
-       /* repeat parsing while there is still enough remaining data in buffer */
-       } while((size_t) len > pos + sizeof(struct blob_attr) && (bufpt = blob_next(bufpt)));
+               ret = 0;
+
+               /* Repeat parsing while there is still enough remaining data in buffer
+                *
+                * Note that blob_next() is only valid for untrusted data because blob_parse_untrusted()
+                * verified that the buffer contains at least one blob, and that it is completely contained
+                * in the buffer */
+               bufpt = blob_next(bufpt);
+       }
 
-       return (ret <= 0);
+       return ret;
 }
 
 #ifdef UCERT_FULL
@@ -349,7 +353,7 @@ static int chain_verify(const char *msgfile, const char *pubkeyfile,
                                   blobmsg_data_len(payloadtb[CERT_PL_ATTR_PUBKEY]),
                                   false);
 
-                       if (usign_f_pubkey(chainedfp, chainedpubkey)) {
+                       if (usign_f_pubkey(chainedfp, chainedpubkey, quiet)) {
                                DPRINTF("cannot get fingerprint for chained key\n");
                                ret = 2;
                                goto clean_and_return;
@@ -460,7 +464,7 @@ static int cert_issue(const char *certfile, const char *pubkeyfile, const char *
 
        pkb[pklen] = '\0';
 
-       if (usign_f_pubkey(pkfp, pubkeyfile))
+       if (usign_f_pubkey(pkfp, pubkeyfile, quiet))
                return -1;
 
        gettimeofday(&tv, NULL);