[pacman-dev] [PATCH 3/8 v2] Add function to extract key id from signatures
This does not support all possibilities of RFC4880, but it does cover every key currently used in Arch Linux. Signed-off-by: Allan McRae <allan@archlinux.org> --- lib/libalpm/signing.c | 113 ++++++++++++++++++++++++++++++++++++++++++++++++++ lib/libalpm/signing.h | 3 ++ 2 files changed, 116 insertions(+) diff --git a/lib/libalpm/signing.c b/lib/libalpm/signing.c index 9d56aba..6ad0c5e 100644 --- a/lib/libalpm/signing.c +++ b/lib/libalpm/signing.c @@ -948,4 +948,117 @@ int SYMEXPORT alpm_siglist_cleanup(alpm_siglist_t *siglist) return 0; } +/** + * Extract the Issuer Key ID from a signature + * @param sig PGP signature + * @param len length of signature + * @param keys a pointer to storage for key IDs + * @return 0 on success, -1 on error + */ +int _alpm_extract_keyid(alpm_handle_t *handle, const char *identifier, + const unsigned char *sig, const size_t len, alpm_list_t **keys) +{ + size_t pos, spos, blen, hlen, ulen, slen; + pos = 0; + + while(pos < len) { + if(!(sig[pos] & 0x80)) { + _alpm_log(handle, ALPM_LOG_ERROR, + _("%s: signature format error"), identifier); + return -1; + } + + if(sig[pos] & 0x40) { + /* "new" packet format is not supported */ + _alpm_log(handle, ALPM_LOG_ERROR, + _("%s: unsupported signature format"), identifier); + return -1; + } + + if(((sig[pos] & 0x3f) >> 2) != 2) { + /* signature is not a "Signature Packet" */ + _alpm_log(handle, ALPM_LOG_ERROR, + _("%s: signature format error"), identifier); + return -1; + } + + switch(sig[pos] & 0x03) { + case 0: + blen = sig[pos + 1]; + pos = pos + 2; + break; + + case 1: + blen = (sig[pos + 1] << 8) | sig[pos + 2]; + pos = pos + 3; + break; + + case 2: + blen = (sig[pos + 1] << 24) | (sig[pos + 2] << 16) | (sig[pos + 3] << 8) | sig[pos + 4]; + pos = pos + 5; + break; + + case 3: + /* partial body length not supported */ + _alpm_log(handle, ALPM_LOG_ERROR, + _("%s: unsupported signature format"), identifier); + return -1; + } + + if(sig[pos] != 4) { + /* only support version 4 signature packet format */ + _alpm_log(handle, ALPM_LOG_ERROR, + _("%s: unsupported signature format"), identifier); + return -1; + } + + if(sig[pos + 1] != 0x00) { + /* not a signature of a binary document */ + _alpm_log(handle, ALPM_LOG_ERROR, + _("%s: signature format error"), identifier); + return -1; + } + + pos = pos + 4; + + hlen = (sig[pos] << 8) | sig[pos + 1]; + pos = pos + hlen + 2; + + ulen = (sig[pos] << 8) | sig[pos + 1]; + pos = pos + 2; + + spos = pos; + + while(spos < pos + ulen) { + if(sig[spos] < 192) { + slen = sig[spos]; + spos = spos + 1; + } else if(sig[spos] < 255) { + slen = (sig[spos] << 8) | sig[spos + 1]; + spos = spos + 2; + } else { + slen = (sig[spos + 1] << 24) | (sig[spos + 2] << 16) | (sig[spos + 3] << 8) | sig[spos + 4]; + spos = spos + 5; + } + + if(sig[spos] == 16) { + /* issuer key ID */ + char key[16]; + size_t i; + for (i = 0; i < 8; i++) { + sprintf(&key[i * 2], "%02X", sig[spos + i + 1]); + } + *keys = alpm_list_add(*keys, strdup(key)); + break; + } + + spos = spos + slen; + } + + pos = pos + (blen - hlen - 8); + } + + return 0; +} + /* vim: set ts=2 sw=2 noet: */ diff --git a/lib/libalpm/signing.h b/lib/libalpm/signing.h index a78e4b7..d3e72bc 100644 --- a/lib/libalpm/signing.h +++ b/lib/libalpm/signing.h @@ -34,6 +34,9 @@ int _alpm_process_siglist(alpm_handle_t *handle, const char *identifier, int _alpm_key_in_keychain(alpm_handle_t *handle, const char *fpr); int _alpm_key_import(alpm_handle_t *handle, const char *fpr); +int _alpm_extract_keyid(alpm_handle_t *handle, const char *identifier, + const unsigned char *sig, const size_t len, alpm_list_t **keys); + #endif /* _ALPM_SIGNING_H */ /* vim: set ts=2 sw=2 noet: */ -- 1.8.0
When installing a package with "pacman -U" that has a detached signature, check if the needed key is in the keyring and download if necessary. Signed-off-by: Allan McRae <allan@archlinux.org> --- lib/libalpm/be_package.c | 66 ++++++++++++++++++++++++++++++++++++++++++++++++ lib/libalpm/signing.c | 5 ++++ 2 files changed, 71 insertions(+) diff --git a/lib/libalpm/be_package.c b/lib/libalpm/be_package.c index 8c5b2d1..d088820 100644 --- a/lib/libalpm/be_package.c +++ b/lib/libalpm/be_package.c @@ -511,6 +511,34 @@ error: return NULL; } +static int read_sigfile(const char *sigpath, unsigned char **sig) +{ + struct stat st; + FILE *fp; + + if(stat(sigpath, &st) != 0) { + return -1; + } + + *sig = malloc(st.st_size); + if(!(*sig)) { + return -1; + } + + if((fp = fopen(sigpath, "rb")) == NULL) { + free(*sig); + return -1; + } + + if(fread(*sig, st.st_size, 1, fp) != 1) { + free(*sig); + fclose(fp); + return -1; + } + + return st.st_size; +} + int SYMEXPORT alpm_pkg_load(alpm_handle_t *handle, const char *filename, int full, alpm_siglevel_t level, alpm_pkg_t **pkg) { @@ -519,6 +547,44 @@ int SYMEXPORT alpm_pkg_load(alpm_handle_t *handle, const char *filename, int ful CHECK_HANDLE(handle, return -1); ASSERT(pkg != NULL, RET_ERR(handle, ALPM_ERR_WRONG_ARGS, -1)); + char *sigpath = _alpm_sigpath(handle, filename); + if(sigpath && !_alpm_access(handle, NULL, sigpath, R_OK)) { + if(level & ALPM_SIG_PACKAGE) { + alpm_list_t *keys = NULL; + int fail = 0; + unsigned char *sig = NULL; + + int len = read_sigfile(sigpath, &sig); + if(len == -1) { + _alpm_log(handle, ALPM_LOG_ERROR, + _("failed to read signature file: %s\n"), sigpath); + free(sigpath); + return -1; + } + + if(_alpm_extract_keyid(handle, filename, sig, len, &keys) == 0) { + alpm_list_t *k; + for(k = keys; k; k = k->next) { + char *key = k->data; + if(_alpm_key_in_keychain(handle, key) == 0) { + if(_alpm_key_import(handle, key) == -1) { + fail = 1; + } + } + } + FREELIST(keys); + } + + free(sig); + + if(fail) { + _alpm_log(handle, ALPM_LOG_ERROR, _("required key missing from keyring\n")); + return -1; + } + } + } + free(sigpath); + if(_alpm_pkg_validate_internal(handle, filename, NULL, level, NULL, &validation) == -1) { /* pm_errno is set by pkg_validate */ diff --git a/lib/libalpm/signing.c b/lib/libalpm/signing.c index 4e59e0a..8d70b61 100644 --- a/lib/libalpm/signing.c +++ b/lib/libalpm/signing.c @@ -192,6 +192,11 @@ int _alpm_key_in_keychain(alpm_handle_t *handle, const char *fpr) gpgme_key_t key; int ret = -1; + if(init_gpgme(handle)) { + /* pm_errno was set in gpgme_init() */ + goto error; + } + memset(&ctx, 0, sizeof(ctx)); err = gpgme_new(&ctx); CHECK_ERR(); -- 1.8.0
On 08/11/12 23:57, Allan McRae wrote:
This does not support all possibilities of RFC4880, but it does cover every key currently used in Arch Linux.
Signed-off-by: Allan McRae <allan@archlinux.org> --- lib/libalpm/signing.c | 113 ++++++++++++++++++++++++++++++++++++++++++++++++++ lib/libalpm/signing.h | 3 ++ 2 files changed, 116 insertions(+)
diff --git a/lib/libalpm/signing.c b/lib/libalpm/signing.c index 9d56aba..6ad0c5e 100644 --- a/lib/libalpm/signing.c +++ b/lib/libalpm/signing.c @@ -948,4 +948,117 @@ int SYMEXPORT alpm_siglist_cleanup(alpm_siglist_t *siglist) return 0; }
+/** + * Extract the Issuer Key ID from a signature + * @param sig PGP signature + * @param len length of signature + * @param keys a pointer to storage for key IDs + * @return 0 on success, -1 on error + */ +int _alpm_extract_keyid(alpm_handle_t *handle, const char *identifier, + const unsigned char *sig, const size_t len, alpm_list_t **keys) +{ + size_t pos, spos, blen, hlen, ulen, slen; + pos = 0; + + while(pos < len) { + if(!(sig[pos] & 0x80)) { + _alpm_log(handle, ALPM_LOG_ERROR, + _("%s: signature format error"), identifier); + return -1; + } + + if(sig[pos] & 0x40) { + /* "new" packet format is not supported */ + _alpm_log(handle, ALPM_LOG_ERROR, + _("%s: unsupported signature format"), identifier); + return -1; + } + + if(((sig[pos] & 0x3f) >> 2) != 2) { + /* signature is not a "Signature Packet" */ + _alpm_log(handle, ALPM_LOG_ERROR, + _("%s: signature format error"), identifier); + return -1; + } + + switch(sig[pos] & 0x03) { + case 0: + blen = sig[pos + 1]; + pos = pos + 2; + break; + + case 1: + blen = (sig[pos + 1] << 8) | sig[pos + 2]; + pos = pos + 3; + break; + + case 2: + blen = (sig[pos + 1] << 24) | (sig[pos + 2] << 16) | (sig[pos + 3] << 8) | sig[pos + 4]; + pos = pos + 5; + break; + + case 3: + /* partial body length not supported */ + _alpm_log(handle, ALPM_LOG_ERROR, + _("%s: unsupported signature format"), identifier); + return -1; + } + + if(sig[pos] != 4) { + /* only support version 4 signature packet format */ + _alpm_log(handle, ALPM_LOG_ERROR, + _("%s: unsupported signature format"), identifier); + return -1; + } + + if(sig[pos + 1] != 0x00) { + /* not a signature of a binary document */ + _alpm_log(handle, ALPM_LOG_ERROR, + _("%s: signature format error"), identifier); + return -1; + } + + pos = pos + 4; + + hlen = (sig[pos] << 8) | sig[pos + 1]; + pos = pos + hlen + 2; + + ulen = (sig[pos] << 8) | sig[pos + 1]; + pos = pos + 2; + + spos = pos; + + while(spos < pos + ulen) { + if(sig[spos] < 192) { + slen = sig[spos]; + spos = spos + 1; + } else if(sig[spos] < 255) { + slen = (sig[spos] << 8) | sig[spos + 1]; + spos = spos + 2; + } else { + slen = (sig[spos + 1] << 24) | (sig[spos + 2] << 16) | (sig[spos + 3] << 8) | sig[spos + 4]; + spos = spos + 5; + } + + if(sig[spos] == 16) { + /* issuer key ID */ + char key[16];
Buffer overflow! Detected with -O3 and -D_FORTIFY_SOURCE=2 char key[17];
+ size_t i; + for (i = 0; i < 8; i++) { + sprintf(&key[i * 2], "%02X", sig[spos + i + 1]); + } + *keys = alpm_list_add(*keys, strdup(key)); + break; + } + + spos = spos + slen; + } + + pos = pos + (blen - hlen - 8); + } + + return 0; +} + /* vim: set ts=2 sw=2 noet: */ diff --git a/lib/libalpm/signing.h b/lib/libalpm/signing.h index a78e4b7..d3e72bc 100644 --- a/lib/libalpm/signing.h +++ b/lib/libalpm/signing.h @@ -34,6 +34,9 @@ int _alpm_process_siglist(alpm_handle_t *handle, const char *identifier, int _alpm_key_in_keychain(alpm_handle_t *handle, const char *fpr); int _alpm_key_import(alpm_handle_t *handle, const char *fpr);
+int _alpm_extract_keyid(alpm_handle_t *handle, const char *identifier, + const unsigned char *sig, const size_t len, alpm_list_t **keys); + #endif /* _ALPM_SIGNING_H */
/* vim: set ts=2 sw=2 noet: */
participants (1)
-
Allan McRae