pkcs7: better handling of signed attributes
Subsystems (3)
v4 cover pkcs7: better handling of signed attributes
v4: the patch set got much smaller thanks to dumping the ability to verify only the signature itself without having to supply the buffer for verification (all potential consumers confirmed they have the buffer and it's not a huge overhead). So the use flow now is parse the pkcs7, call the existing verify_pkcs7_message_sig to get the trust for the signed attributes and then extract them.
v3 updates for the now-upstream ml-dsa. The ml-dsa patches actually changed the definition of authattrs in struct pkcs7_signed_info, which means I now don't have to justify stepping one back in patch 4 (thanks!). Just in case I checked that nothing else was affected by this change.
Original cover letter:
Although the biggest use of signed attributes is PKCS#7 and X509 specific data, they can be added to a signature to support arbitrary and verifiable objects. This makes them particularly useful when you want to take an existing signature scheme and extend it with additional (but always verified) data in such a way that it still looks valid to both the old and new schemes.
To use a scheme like this to extend signatures requires that the authenticated attribute only be pulled out of a signer info that can be verified by one of the trusted keys, so the implementation loops over all signer infos, discarding those that haven't been verified and returns the first OID match it finds in the verified ones. Note that if you reparse a pkcs7 it starts out with no trusted signer infos, and you must anchor trust by calling validate_pkcs7_trust() with the trusted keyring.
The first three patches in this series are new to v2. They add the new validate_pkcs7_trust() call, thread a verified flag through struct pkcs7_signer_info so we can tell which signers have been validated against the trusted keyring. And finally thread pkcs7_digest through the pkcs7_validate functions so they can operate on a plain parsed pkcs7 structure that hasn't gone through pkcs7_verify. Note we could simply drop the last patch and insist that the pkcs7 be re-verified; it just looked a bit inefficient, especially as the default way of doing this (verify_pkcs7_signature() frees the pkcs7 structure before returning.
The final two patches search for the authenticated attribute by OID, stopping at the first one it finds belonging to a verified signer info. The final patch demonstrates how to use it. I've added a check to show that if you don't in any way validate the pkcs7 then no signed attributes get returned.
Regards,
James
---
James Bottomley (3):
crypto: pkcs7: add flag for validated trust on a signed info block
crypto: pkcs7: add ability to extract signed attributes by OID
crypto: pkcs7: add tests for pkcs7_get_authattr
certs/system_keyring.c | 1 + crypto/asymmetric_keys/Makefile | 4 +- crypto/asymmetric_keys/pkcs7_aa.asn1 | 18 ++++++ crypto/asymmetric_keys/pkcs7_key_type.c | 44 +++++++++++++- crypto/asymmetric_keys/pkcs7_parser.c | 81 +++++++++++++++++++++++++ crypto/asymmetric_keys/pkcs7_parser.h | 1 + crypto/asymmetric_keys/pkcs7_trust.c | 1 + include/crypto/pkcs7.h | 4 ++ 8 files changed, 152 insertions(+), 2 deletions(-) create mode 100644 crypto/asymmetric_keys/pkcs7_aa.asn1
Signature · 1 line
2.51.0
PATCH v4 1/3 crypto: pkcs7: add flag for validated trust on a signed info block
Allow consumers of struct pkcs7_message to tell if any of the sinfo fields has passed a trust validation. Note that this does not happen in parsing, pkcs7_validate_trust() must be explicitly called or called via validate_pkcs7_trust(). Since the way to get this trusted pkcs7 object is via verify_pkcs7_message_sig, export that so modules can use it.
--- certs/system_keyring.c | 1 + crypto/asymmetric_keys/pkcs7_parser.h | 1 + crypto/asymmetric_keys/pkcs7_trust.c | 1 + 3 files changed, 3 insertions(+)
certs/system_keyring.c +1 -0
@@ -380,6 +380,7 @@ int verify_pkcs7_message_sig(const void *data, size_t len,pr_devel ("<==%s() = %d\n" , __func__, ret);return ret; } +EXPORT_SYMBOL (verify_pkcs7_message_sig);/** * verify_pkcs7_signature - Verify a PKCS#7-based signature on system data.
crypto/asymmetric_keys/pkcs7_parser.h +1 -0
@@ -20,6 +20,7 @@ struct pkcs7_signed_info {unsigned index;bool unsupported_crypto;/* T if not usable due to missing crypto */ bool blacklisted; +bool verified;/* T if this signer has validated trust */ /* Message digest - the digest of the Content Data (or NULL) */ const void *msgdigest;
crypto/asymmetric_keys/pkcs7_trust.c +1 -0
@@ -127,6 +127,7 @@ static int pkcs7_validate_trust_one(struct pkcs7_message *pkcs7,for (p = sinfo->signer; p != x509; p = p->signer) p->verified = true; } + sinfo->verified = true;kleave (" = 0" );return 0 ; }
Signature · 1 line
2.51.0
PATCH v4 2/3 crypto: pkcs7: add ability to extract signed attributes by OID
In reply to James BottomleySigners may add any information they like in signed attributes and sometimes this information turns out to be relevant to specific signing cases, so add an api pkcs7_get_authattr() to extract the value of an authenticated attribute by specific OID. The current implementation is designed for the single signer use case and simply terminates the search when it finds the relevant OID.
---
v2: fix docbook
--- crypto/asymmetric_keys/Makefile | 4 +- crypto/asymmetric_keys/pkcs7_aa.asn1 | 18 ++++++ crypto/asymmetric_keys/pkcs7_parser.c | 81 +++++++++++++++++++++++++++ include/crypto/pkcs7.h | 4 ++ 4 files changed, 106 insertions(+), 1 deletion(-) create mode 100644 crypto/asymmetric_keys/pkcs7_aa.asn1
crypto/asymmetric_keys/Makefile +3 -1
@@ -53,12 +53,14 @@ clean-files += pkcs8.asn1.c pkcs8.asn1.h obj-$(CONFIG_PKCS7_MESSAGE_PARSER) += pkcs7_message.o pkcs7_message-y := \ pkcs7.asn1.o \ + pkcs7_aa.asn1.o \ pkcs7_parser.o \ pkcs7_trust.o \ pkcs7_verify.o -$(obj)/pkcs7_parser.o: $(obj)/pkcs7.asn1.h +$(obj)/pkcs7_parser.o: $(obj)/pkcs7.asn1.h $(obj)/pkcs7_aa.asn1.h $(obj)/pkcs7.asn1.o: $(obj)/pkcs7.asn1.c $(obj)/pkcs7.asn1.h +$(obj)/pkcs7_aa.asn1.o: $(obj)/pkcs7_aa.asn1.c $(obj)/pkcs7_aa.asn1.h # # PKCS#7 parser testing key
crypto/asymmetric_keys/pkcs7_aa.asn1 new file +18 -0
@@ -0,0 +1,18 @@ +-- SPDX-License-Identifier: BSD-3-Clause +-- +-- Copyright (C) 2009 IETF Trust and the persons identified as authors +-- of the code +-- +-- https://www.rfc-editor.org/rfc/rfc5652#section-3 + +AA ::= CHOICE { + aaSet [0] IMPLICIT AASet, + aaSequence [2] EXPLICIT SEQUENCE OF AuthenticatedAttribute +} + +AASet ::= SET OF AuthenticatedAttribute + +AuthenticatedAttribute ::= SEQUENCE { + type OBJECT IDENTIFIER ({ pkcs7_aa_note_OID }), + values SET OF ANY ({ pkcs7_aa_note_attr }) +}
crypto/asymmetric_keys/pkcs7_parser.c +81 -0
93 lines not on this page · raw message
include/crypto/pkcs7.h +4 -0
@@ -25,6 +25,10 @@ extern void pkcs7_free_message(struct pkcs7_message *pkcs7);extern int pkcs7_get_content_data (const struct pkcs7_message *pkcs7,const void **_data,size_t *_datalen,size_t *_headerlen); +extern int pkcs7_get_authattr (const struct pkcs7_message *pkcs7, +enum OID oid, +const void **_data,size_t *_len); +/* * pkcs7_trust.c
Signature · 1 line
2.51.0
PATCH v4 3/3 crypto: pkcs7: add tests for pkcs7_get_authattr
In reply to James BottomleyAdd example code to the test module pkcs7_key_type.c that verifies a message and then pulls out a known authenticated attribute.
---
v2: add ack
v4: adjust format specifier for size_t
I'm not convinced this needs adding, but it provided a convenient mechanism for testing the pcks7_get_authattr() call so I added it in case others find it useful.
--- crypto/asymmetric_keys/pkcs7_key_type.c | 44 ++++++++++++++++++++++++- 1 file changed, 43 insertions(+), 1 deletion(-)
crypto/asymmetric_keys/pkcs7_key_type.c +43 -1
@@ -12,6 +12,7 @@#include <linux/verification.h> #include <linux/key-type.h> #include <keys/user-type.h> +#include <crypto/pkcs7.h> MODULE_LICENSE ("GPL" );MODULE_DESCRIPTION ("PKCS#7 testing key type" );
@@ -51,16 +52,57 @@ static int pkcs7_view_content(void *ctx, const void *data, size_t len,static int pkcs7_preparse (struct key_preparsed_payload *prep) {enum key_being_used_for usage = pkcs7_usage; +int ret; +struct pkcs7_message *pkcs7; +const void *data; +size_t len;if (usage >=NR__KEY_BEING_USED_FOR ) {pr_err ("Invalid usage type %d\n" , usage);return -EINVAL ; } -return verify_pkcs7_signature (NULL ,0 , + ret =verify_pkcs7_signature (NULL ,0 , prep->data, prep->datalen,VERIFY_USE_SECONDARY_KEYRING , usage, pkcs7_view_content, prep); +if (ret) +return ret; + + pkcs7 =pkcs7_parse_message (prep->data, prep->datalen); +if (IS_ERR (pkcs7)) { +pr_err ("pkcs7 parse error\n" ); +return PTR_ERR (pkcs7); + } + +/* +* the parsed message has no trusted signer, so nothing should +* be returned here +*/ + ret =pkcs7_get_authattr (pkcs7, OID_messageDigest, &data, &len); +if (ret ==0 ) { +pr_err ("OID returned when no trust in signer\n" ); + goto out; + } +/* add trust and check again */ + ret =verify_pkcs7_message_sig (NULL ,0 , pkcs7, +VERIFY_USE_SECONDARY_KEYRING , usage, +NULL ,NULL ); +if (ret) { +pr_err ("verify_pkcs7_message_sig failed!!\n" ); + goto out; + } +/* now we should find the OID */ + ret =pkcs7_get_authattr (pkcs7, OID_messageDigest, &data, &len); +if (ret) { +pr_err ("Failed to get message digest\n" ); + goto out; + } +pr_info ("Correctly Got message hash, size=%zu\n" , len); + + out: +pkcs7_free_message (pkcs7); +return 0 ; }/*
Signature · 1 line
2.51.0