1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
|
/**
* @file
*
* GPGME Key Sorting
*
* @authors
* Copyright (C) 2024 Richard Russon <rich@flatcap.org>
*
* @copyright
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation, either version 2 of the License, or (at your option) any later
* version.
*
* This program 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 General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* @page crypt_sort_gpgme GPGME Key Sorting
*
* GPGME Key Sorting
*/
#include "config.h"
#include <locale.h>
#include <stdbool.h>
#include "mutt/lib.h"
#include "config/lib.h"
#include "core/lib.h"
#include "lib.h"
#include "crypt_gpgme.h"
#include "sort.h"
/**
* crypt_sort_address - Compare two keys by their addresses - Implements ::sort_t - @ingroup sort_api
*/
static int crypt_sort_address(const void *a, const void *b, void *sdata)
{
struct CryptKeyInfo *s = *(struct CryptKeyInfo **) a;
struct CryptKeyInfo *t = *(struct CryptKeyInfo **) b;
const bool sort_reverse = *(bool *) sdata;
int rc = mutt_istr_cmp(s->uid, t->uid);
if (rc != 0)
goto done;
rc = mutt_istr_cmp(crypt_fpr_or_lkeyid(s), crypt_fpr_or_lkeyid(t));
done:
return sort_reverse ? -rc : rc;
}
/**
* crypt_sort_keyid - Compare two keys by their IDs - Implements ::sort_t - @ingroup sort_api
*/
static int crypt_sort_keyid(const void *a, const void *b, void *sdata)
{
struct CryptKeyInfo *s = *(struct CryptKeyInfo **) a;
struct CryptKeyInfo *t = *(struct CryptKeyInfo **) b;
const bool sort_reverse = *(bool *) sdata;
int rc = mutt_istr_cmp(crypt_fpr_or_lkeyid(s), crypt_fpr_or_lkeyid(t));
if (rc != 0)
goto done;
rc = mutt_istr_cmp(s->uid, t->uid);
done:
return sort_reverse ? -rc : rc;
}
/**
* crypt_sort_date - Compare two keys by their dates - Implements ::sort_t - @ingroup sort_api
*/
static int crypt_sort_date(const void *a, const void *b, void *sdata)
{
struct CryptKeyInfo *s = *(struct CryptKeyInfo **) a;
struct CryptKeyInfo *t = *(struct CryptKeyInfo **) b;
const bool sort_reverse = *(bool *) sdata;
unsigned long ts = 0;
unsigned long tt = 0;
int rc = 0;
if (s->kobj->subkeys && (s->kobj->subkeys->timestamp > 0))
ts = s->kobj->subkeys->timestamp;
if (t->kobj->subkeys && (t->kobj->subkeys->timestamp > 0))
tt = t->kobj->subkeys->timestamp;
if (ts > tt)
{
rc = 1;
goto done;
}
if (ts < tt)
{
rc = -1;
goto done;
}
rc = mutt_istr_cmp(s->uid, t->uid);
done:
return sort_reverse ? -rc : rc;
}
/**
* crypt_sort_trust - Compare two keys by their trust levels - Implements ::sort_t - @ingroup sort_api
*/
static int crypt_sort_trust(const void *a, const void *b, void *sdata)
{
struct CryptKeyInfo *s = *(struct CryptKeyInfo **) a;
struct CryptKeyInfo *t = *(struct CryptKeyInfo **) b;
const bool sort_reverse = *(bool *) sdata;
unsigned long ts = 0;
unsigned long tt = 0;
int rc = mutt_numeric_cmp(s->flags & KEYFLAG_RESTRICTIONS, t->flags & KEYFLAG_RESTRICTIONS);
if (rc != 0)
goto done;
// Note: reversed
rc = mutt_numeric_cmp(t->validity, s->validity);
if (rc != 0)
return rc;
ts = 0;
tt = 0;
if (s->kobj->subkeys)
ts = s->kobj->subkeys->length;
if (t->kobj->subkeys)
tt = t->kobj->subkeys->length;
// Note: reversed
rc = mutt_numeric_cmp(tt, ts);
if (rc != 0)
goto done;
ts = 0;
tt = 0;
if (s->kobj->subkeys && (s->kobj->subkeys->timestamp > 0))
ts = s->kobj->subkeys->timestamp;
if (t->kobj->subkeys && (t->kobj->subkeys->timestamp > 0))
tt = t->kobj->subkeys->timestamp;
// Note: reversed
rc = mutt_numeric_cmp(tt, ts);
if (rc != 0)
goto done;
rc = mutt_istr_cmp(s->uid, t->uid);
if (rc != 0)
goto done;
rc = mutt_istr_cmp(crypt_fpr_or_lkeyid(s), crypt_fpr_or_lkeyid(t));
done:
return sort_reverse ? -rc : rc;
}
/**
* gpgme_sort_keys - Sort an array of GPGME keys
* @param ckia Array to sort
*
* Sort GPGME keys according to `$pgp_sort_keys`
*/
void gpgme_sort_keys(struct CryptKeyInfoArray *ckia)
{
const short c_pgp_sort_keys = cs_subset_sort(NeoMutt->sub, "pgp_key_sort");
sort_t fn = NULL;
switch (c_pgp_sort_keys & SORT_MASK)
{
case KEY_SORT_ADDRESS:
fn = crypt_sort_address;
break;
case KEY_SORT_DATE:
fn = crypt_sort_date;
break;
case KEY_SORT_KEYID:
fn = crypt_sort_keyid;
break;
case KEY_SORT_TRUST:
default:
fn = crypt_sort_trust;
break;
}
if (ARRAY_SIZE(ckia) > 1)
{
bool sort_reverse = c_pgp_sort_keys & SORT_REVERSE;
ARRAY_SORT(ckia, fn, &sort_reverse);
}
}
|