From b3579cb54bd5cd16e9740404408b2505b4b1e26b Mon Sep 17 00:00:00 2001 From: Stef Walter Date: Fri, 12 Sep 2014 15:37:02 +0200 Subject: trust: Allow 'BEGIN PUBLIC KEY' PEM blocks in .p11-kit files These PEM blocks contribute a CKA_PUBLIC_KEY_INFO to the object being read/written. https://bugs.freedesktop.org/show_bug.cgi?id=83799 --- doc/internal/persist-format.txt | 13 +++++++++---- trust/persist.c | 24 ++++++++++++++++++++++++ trust/test-persist.c | 27 +++++++++++++++++++++++++++ trust/test-trust.h | 22 ++++++++++++++++++++++ 4 files changed, 82 insertions(+), 4 deletions(-) diff --git a/doc/internal/persist-format.txt b/doc/internal/persist-format.txt index a0a3194..cb863be 100644 --- a/doc/internal/persist-format.txt +++ b/doc/internal/persist-format.txt @@ -22,10 +22,15 @@ constants are available. PEM blocks can be present within an object, and these contribute certain PKCS#11 attributes to the object. The attributes that come from PEM blocks -never override those explicitly specified. A 'CERTIFICATE' type PEM block -contributes the 'value', 'class', 'certificate-type', 'subject', 'issuer' -'start-date', 'end-date', 'id', 'certificate-category', 'check-value', -'serial-number' attributes with appropriate values. +never override those explicitly specified. + +A 'CERTIFICATE' type PEM block contributes the 'value', 'class', +'certificate-type', 'subject', 'issuer' 'start-date', 'end-date', 'id', +'certificate-category', 'check-value', 'serial-number', 'public-key-info' +attributes with appropriate values. + +A 'PUBLIC KEY' type PEM block contributes the 'public-key-info' attribute +with an appropriate value. Comments starting with a '#' and blank lines are ignored. diff --git a/trust/persist.c b/trust/persist.c index 1b41568..ae76342 100644 --- a/trust/persist.c +++ b/trust/persist.c @@ -557,6 +557,15 @@ certificate_to_attributes (const unsigned char *der, return p11_attrs_build (NULL, &klass, &certificate_type, &value, NULL); } +static CK_ATTRIBUTE * +public_key_to_attributes (const unsigned char *der, + size_t length) +{ + /* Eventually we might choose to contribute a class here ... */ + CK_ATTRIBUTE public_key = { CKA_PUBLIC_KEY_INFO, (void *)der, length }; + return p11_attrs_build (NULL, &public_key, NULL); +} + typedef struct { p11_lexer *lexer; CK_ATTRIBUTE *attrs; @@ -577,6 +586,11 @@ on_pem_block (const char *type, pb->attrs = p11_attrs_merge (pb->attrs, attrs, false); pb->result = true; + } else if (strcmp (type, "PUBLIC KEY") == 0) { + attrs = public_key_to_attributes (contents, length); + pb->attrs = p11_attrs_merge (pb->attrs, attrs, false); + pb->result = true; + } else { p11_lexer_msg (pb->lexer, "unsupported pem block in store"); pb->result = false; @@ -697,10 +711,12 @@ p11_persist_write (p11_persist *persist, { char string[sizeof (CK_ULONG) * 4]; CK_ATTRIBUTE *cert_value; + CK_ATTRIBUTE *spki_value; const char *nick; int i; cert_value = find_certificate_value (attrs); + spki_value = p11_attrs_find_valid (attrs, CKA_PUBLIC_KEY_INFO); p11_buffer_add (buf, "[" PERSIST_HEADER "]\n", -1); @@ -713,6 +729,11 @@ p11_persist_write (p11_persist *persist, attrs[i].type == CKA_VALUE)) continue; + /* These are written later? */ + if (spki_value != NULL && + attrs[i].type == CKA_PUBLIC_KEY_INFO) + continue; + /* These are never written */ if (attrs[i].type == CKA_TOKEN || attrs[i].type == CKA_X_ORIGIN || @@ -737,6 +758,9 @@ p11_persist_write (p11_persist *persist, if (cert_value != NULL) { if (!p11_pem_write (cert_value->pValue, cert_value->ulValueLen, "CERTIFICATE", buf)) return_val_if_reached (false); + } else if (spki_value != NULL) { + if (!p11_pem_write (spki_value->pValue, spki_value->ulValueLen, "PUBLIC KEY", buf)) + return_val_if_reached (false); } p11_buffer_add (buf, "\n", 1); diff --git a/trust/test-persist.c b/trust/test-persist.c index 68d2033..bb2b630 100644 --- a/trust/test-persist.c +++ b/trust/test-persist.c @@ -399,6 +399,32 @@ test_pem_middle (void) check_read_success (input, (expected, NULL)); } +static void +test_pem_public_key (void) +{ + const char *output = "[p11-kit-object-v1]\n" + "id: \"292c92\"\n" + "-----BEGIN PUBLIC KEY-----\n" + "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAryQICCl6NZ5gDKrnSztO\n" + "3Hy8PEUcuyvg/ikC+VcIo2SFFSf18a3IMYldIugqqqZCs4/4uVW3sbdLs/6PfgdX\n" + "7O9D22ZiFWHPYA2k2N744MNiCD1UE+tJyllUhSblK48bn+v1oZHCM0nYQ2NqUkvS\n" + "j+hwUU3RiWl7x3D2s9wSdNt7XUtW05a/FXehsPSiJfKvHJJnGOX0BgTvkLnkAOTd\n" + "OrUZ/wK69Dzu4IvrN4vs9Nes8vbwPa/ddZEzGR0cQMt0JBkhk9kU/qwqUseP1QRJ\n" + "5I1jR4g8aYPL/ke9K35PxZWuDp3U0UPAZ3PjFAh+5T+fc7gzCs9dPzSHloruU+gl\n" + "FQIDAQAB\n" + "-----END PUBLIC KEY-----\n\n"; + + CK_ATTRIBUTE attrs[] = { + { CKA_ID, "292c92", 6, }, + { CKA_PUBLIC_KEY_INFO, &example_public_key, sizeof (example_public_key) }, + { CKA_INVALID }, + }; + + check_read_success (output, (attrs, NULL)); + check_write_success (output, (attrs, NULL)); +} + + static void test_pem_invalid (void) { @@ -594,6 +620,7 @@ main (int argc, p11_test (test_multiple, "/persist/multiple"); p11_test (test_pem_block, "/persist/pem_block"); p11_test (test_pem_middle, "/persist/pem-middle"); + p11_test (test_pem_public_key, "/persist/pem-public-key"); p11_test (test_pem_invalid, "/persist/pem_invalid"); p11_test (test_pem_unsupported, "/persist/pem_unsupported"); p11_test (test_pem_first, "/persist/pem_first"); diff --git a/trust/test-trust.h b/trust/test-trust.h index b70bbdb..81c779c 100644 --- a/trust/test-trust.h +++ b/trust/test-trust.h @@ -335,6 +335,28 @@ static const unsigned char verisign_v1_ca_public_key[] = { 0x00, 0x01, }; +static const unsigned char example_public_key[] = { + 0x30, 0x82, 0x01, 0x22, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, + 0x01, 0x05, 0x00, 0x03, 0x82, 0x01, 0x0f, 0x00, 0x30, 0x82, 0x01, 0x0a, 0x02, 0x82,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, 0xf0, 0x3d, 0xaf, 0xdd, 0x75, 0x91, 0x33, 0x19, 0x1d, 0x1c, 0x40, 0xcb, + 0x74, 0x24, 0x19, 0x21, 0x93, 0xd9, 0x14, 0xfe, 0xac, 0x2a, 0x52, 0xc7, 0x8f, 0xd5, 0x04, 0x49, + 0xe4, 0x8d, 0x63, 0x47, 0x88, 0x3c, 0x69, 0x83, 0xcb, 0xfe, 0x47, 0xbd, 0x2b, 0x7e, 0x4f, 0xc5, + 0x95, 0xae, 0x0e, 0x9d, 0xd4, 0xd1, 0x43, 0xc0, 0x67, 0x73, 0xe3, 0x14, 0x08, 0x7e, 0xe5, 0x3f, + 0x9f, 0x73, 0xb8, 0x33, 0x0a, 0xcf, 0x5d, 0x3f, 0x34, 0x87, 0x96, 0x8a, 0xee, 0x53, 0xe8, 0x25, + 0x15, 0x02, 0x03, 0x01, 0x00, 0x01 +}; + static const char test_text[] = "This is the file text"; static const char test_eku_server_and_client[] = { -- cgit v1.2.1