diff options
| author | jsing <> | 2019-11-02 15:35:10 +0000 |
|---|---|---|
| committer | jsing <> | 2019-11-02 15:35:10 +0000 |
| commit | a2eeae557e541b57369692dfc78175fab547e251 (patch) | |
| tree | f88afc2f923782e69f24c022913601cbe59553c0 /src | |
| parent | 8ce3d0166753049e6d22a4aa255ecddf542ccdc8 (diff) | |
| download | openbsd-a2eeae557e541b57369692dfc78175fab547e251.tar.gz openbsd-a2eeae557e541b57369692dfc78175fab547e251.tar.bz2 openbsd-a2eeae557e541b57369692dfc78175fab547e251.zip | |
Initial regress for cms.
Diffstat (limited to 'src')
| -rw-r--r-- | src/regress/lib/libcrypto/cms/Makefile | 9 | ||||
| -rw-r--r-- | src/regress/lib/libcrypto/cms/cmstest.c | 190 |
2 files changed, 199 insertions, 0 deletions
diff --git a/src/regress/lib/libcrypto/cms/Makefile b/src/regress/lib/libcrypto/cms/Makefile new file mode 100644 index 0000000000..048e7aa0fa --- /dev/null +++ b/src/regress/lib/libcrypto/cms/Makefile | |||
| @@ -0,0 +1,9 @@ | |||
| 1 | # $OpenBSD: Makefile,v 1.1 2019/11/02 15:35:10 jsing Exp $ | ||
| 2 | |||
| 3 | PROG= cmstest | ||
| 4 | LDADD= -lcrypto | ||
| 5 | DPADD= ${LIBCRYPTO} | ||
| 6 | WARNINGS= Yes | ||
| 7 | CFLAGS+= -DLIBRESSL_INTERNAL -Werror | ||
| 8 | |||
| 9 | .include <bsd.regress.mk> | ||
diff --git a/src/regress/lib/libcrypto/cms/cmstest.c b/src/regress/lib/libcrypto/cms/cmstest.c new file mode 100644 index 0000000000..40cb611c9a --- /dev/null +++ b/src/regress/lib/libcrypto/cms/cmstest.c | |||
| @@ -0,0 +1,190 @@ | |||
| 1 | /* $OpenBSD: cmstest.c,v 1.1 2019/11/02 15:35:10 jsing Exp $ */ | ||
| 2 | /* | ||
| 3 | * Copyright (c) 2019 Joel Sing <jsing@openbsd.org> | ||
| 4 | * | ||
| 5 | * Permission to use, copy, modify, and distribute this software for any | ||
| 6 | * purpose with or without fee is hereby granted, provided that the above | ||
| 7 | * copyright notice and this permission notice appear in all copies. | ||
| 8 | * | ||
| 9 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | ||
| 10 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | ||
| 11 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | ||
| 12 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | ||
| 13 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | ||
| 14 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | ||
| 15 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | ||
| 16 | */ | ||
| 17 | |||
| 18 | #include <err.h> | ||
| 19 | #include <string.h> | ||
| 20 | |||
| 21 | #include <openssl/bio.h> | ||
| 22 | #include <openssl/err.h> | ||
| 23 | #include <openssl/pem.h> | ||
| 24 | #include <openssl/x509.h> | ||
| 25 | |||
| 26 | #include <openssl/cms.h> | ||
| 27 | |||
| 28 | static int verbose = 0; | ||
| 29 | |||
| 30 | static const char cms_msg[] = "Hello CMS!\r\n"; | ||
| 31 | |||
| 32 | static const char cms_cert_1[] = \ | ||
| 33 | "-----BEGIN CERTIFICATE-----\n" | ||
| 34 | "MIICpDCCAYwCAQMwDQYJKoZIhvcNAQEFBQAwFjEUMBIGA1UEAwwLVGVzdCBDTVMg\n" | ||
| 35 | "Q0EwHhcNMTkwNTExMTU1MzU0WhcNMjkwNTA4MTU1MzU0WjAaMRgwFgYDVQQDDA9U\n" | ||
| 36 | "ZXN0IENNUyBDZXJ0IDEwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQDD\n" | ||
| 37 | "MLSuy+tc0AwfrlszgHJ3z7UEpJSn5mcKxquFnEC5DtchgQJ+cj5VFvB9A9G98ykQ\n" | ||
| 38 | "0IrHXNUTbS2yvf8ac1PlocuA8ggeDK4gPHCe097j0nUphhT0VzhwwFfP6Uo6VaR8\n" | ||
| 39 | "B7Qb3zFTz64bN66V89etZ5NQJKMdrh4oOh5nfxLKvCcTK+9U4ZrgeGVdVXmL6HJp\n" | ||
| 40 | "3m9CPobCBsC8DgI+zF/tg4GjDoVCJd6Tv5MRAmKiBrzTGglVeknkgiyIZ9C7gXU/\n" | ||
| 41 | "7NMUihmLlt+80zr+nL0P+MA924WV4fZJi1wtf6Eioalq6n/9i93nBRCeu8bEOBrT\n" | ||
| 42 | "pAre2oBEoULIJu7Ubx79AgMBAAEwDQYJKoZIhvcNAQEFBQADggEBADnLc6ZzApHq\n" | ||
| 43 | "Z8l4zrFKAG/O/oULPMRTA8/zXNQ60BMV10hVtTCxVNq59d48wEljuUOGLfM91rhj\n" | ||
| 44 | "gId8AOlsQbfRZE94DxlcaaAXaEjbkVSke56yfdLd4NqkIWrXGrFlbepj4b4ORAHh\n" | ||
| 45 | "85kPwDEDnpMgQ63LqNX3gru3xf2AGIa1Fck2ISkVafqW5TH0Y6dCeGGFTtnH/QUT\n" | ||
| 46 | "ofTm8uQ2vG9ERn+C1ooqJ2dyAckXFdmCcpor26vO/ZssMEKSee38ZNWR/01LEkOG\n" | ||
| 47 | "G0+AL7E1mJdlVOtp3DDFN0hoNY7PbVuuzT+mrAwGLhCp2jnf68iNdrIuDdIE6yvi\n" | ||
| 48 | "6WWvmmz+rC0=\n" | ||
| 49 | "-----END CERTIFICATE-----\n"; | ||
| 50 | |||
| 51 | static const char cms_key_1[] = \ | ||
| 52 | "-----BEGIN PRIVATE KEY-----\n" | ||
| 53 | "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQDDMLSuy+tc0Awf\n" | ||
| 54 | "rlszgHJ3z7UEpJSn5mcKxquFnEC5DtchgQJ+cj5VFv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La9Y3hblvNpOU0SoVeMAGQLblEznJAl1nbaWqVY\n" | ||
| 69 | "kPgvtQcTOL/awEB90JklvSRqR82WJchMOHMG5SeqrpUx3Dg+cPH6nId0e8UCt3/U\n" | ||
| 70 | "W/u/5hP+IQKBgQDfReEmxaZ10MIm6P6p24Wm3dEcYBfxEjbEb0HBzspek1u3JWep\n" | ||
| 71 | "PfsuQavTXy/IaKBOENIUgBhjOZssqxnZChgXkD7frtulRNOTW5RuLkRzp3BWWJ1v\n" | ||
| 72 | "VifB3gBYj41d16UH+VnVQbnCEiUCuk5hR4bh8oJaaUV8xvW6ipItHNHErQKBgGoe\n" | ||
| 73 | "2uuj6UkiSbFRNL4z3JFZN6AlvNsOl3imHZ/v8Ou29dwQkVbJuNdckydzVoOwpZ7h\n" | ||
| 74 | "ZY8D3JJHHq3rYv3TqQ86c56MAv8tYbiy5yMrtZHIJMOlSeI4oSa6GZt8Dx5gylO5\n" | ||
| 75 | "JUMxtPrU70u5BiZAwYxsCi0AdYimfXAsqB9hNFUBAoGBAJPT7Xsr7NIkrbv+aYXj\n" | ||
| 76 | "rVVJ1qokUEKT6H1GmFXO3Fkw3kjPKS8VZloKOB7OiBC+AwMEQIflArCZ+PJdnVNO\n" | ||
| 77 | "48ntHnaeaZk8rKXsYdJsqMKgIxZYZuCIazZz9WHeYxn5vkH76Q3DrfqrneJ3HSU/\n" | ||
| 78 | "pFtLoXoGoVXRjAtpNvX7fh/G\n" | ||
| 79 | "-----END PRIVATE KEY-----\n"; | ||
| 80 | |||
| 81 | static void | ||
| 82 | hexdump(const unsigned char *buf, size_t len) | ||
| 83 | { | ||
| 84 | size_t i; | ||
| 85 | |||
| 86 | for (i = 1; i <= len; i++) | ||
| 87 | fprintf(stderr, " 0x%02x,%s", buf[i - 1], i % 8 ? "" : "\n"); | ||
| 88 | if (len % 8 != 0) | ||
| 89 | fprintf(stderr, "\n"); | ||
| 90 | } | ||
| 91 | |||
| 92 | static int | ||
| 93 | test_cms_encrypt_decrypt() | ||
| 94 | { | ||
| 95 | STACK_OF(X509) *certs = NULL; | ||
| 96 | CMS_ContentInfo *ci = NULL; | ||
| 97 | EVP_PKEY *pkey = NULL; | ||
| 98 | BIO *bio_mem = NULL; | ||
| 99 | BIO *bio_out; | ||
| 100 | X509 *cert = NULL; | ||
| 101 | size_t len; | ||
| 102 | char *p; | ||
| 103 | int failed = 1; | ||
| 104 | |||
| 105 | if ((bio_out = BIO_new_fp(stdout, BIO_NOCLOSE)) == NULL) | ||
| 106 | errx(1, "failed to create BIO"); | ||
| 107 | |||
| 108 | if ((certs = sk_X509_new_null()) == NULL) | ||
| 109 | errx(1, "failed to create certs"); | ||
| 110 | if ((bio_mem = BIO_new_mem_buf(cms_cert_1, -1)) == NULL) | ||
| 111 | errx(1, "failed to create BIO for cert"); | ||
| 112 | if ((cert = PEM_read_bio_X509(bio_mem, NULL, NULL, NULL)) == NULL) | ||
| 113 | errx(1, "failed to read cert"); | ||
| 114 | if (!sk_X509_push(certs, cert)) | ||
| 115 | errx(1, "failed to push cert"); | ||
| 116 | |||
| 117 | BIO_free(bio_mem); | ||
| 118 | if ((bio_mem = BIO_new_mem_buf(cms_key_1, -1)) == NULL) | ||
| 119 | errx(1, "failed to create BIO for key"); | ||
| 120 | if ((pkey = PEM_read_bio_PrivateKey(bio_mem, NULL, NULL, NULL)) == NULL) | ||
| 121 | errx(1, "failed to read key"); | ||
| 122 | |||
| 123 | BIO_free(bio_mem); | ||
| 124 | if ((bio_mem = BIO_new_mem_buf(cms_msg, -1)) == NULL) | ||
| 125 | errx(1, "failed to create BIO for message"); | ||
| 126 | |||
| 127 | if ((ci = CMS_encrypt(certs, bio_mem, EVP_aes_256_cbc(), 0)) == NULL) { | ||
| 128 | fprintf(stderr, "FAIL: CMS_encrypt returned NULL\n"); | ||
| 129 | ERR_print_errors_fp(stderr); | ||
| 130 | goto failure; | ||
| 131 | } | ||
| 132 | |||
| 133 | if (verbose) { | ||
| 134 | if (!CMS_ContentInfo_print_ctx(bio_out, ci, 0, NULL)) | ||
| 135 | errx(1, "failed to print CMS ContentInfo"); | ||
| 136 | if (!PEM_write_bio_CMS(bio_out, ci)) | ||
| 137 | errx(1, "failed to print CMS PEM"); | ||
| 138 | } | ||
| 139 | |||
| 140 | BIO_free(bio_mem); | ||
| 141 | if ((bio_mem = BIO_new(BIO_s_mem())) == NULL) | ||
| 142 | errx(1, "failed to create BIO for message"); | ||
| 143 | |||
| 144 | if (!CMS_decrypt(ci, pkey, cert, NULL, bio_mem, 0)) { | ||
| 145 | fprintf(stderr, "FAIL: CMS_decrypt failed\n"); | ||
| 146 | ERR_print_errors_fp(stderr); | ||
| 147 | goto failure; | ||
| 148 | } | ||
| 149 | |||
| 150 | if ((len = BIO_get_mem_data(bio_mem, &p)) != strlen(cms_msg)) { | ||
| 151 | fprintf(stderr, "FAIL: CMS decrypt returned %li bytes, " | ||
| 152 | "want %zi bytes\n", len, strlen(cms_msg)); | ||
| 153 | fprintf(stderr, "Got CMS data:\n"); | ||
| 154 | hexdump(p, len); | ||
| 155 | fprintf(stderr, "Want CMS data:\n"); | ||
| 156 | hexdump(cms_msg, strlen(cms_msg)); | ||
| 157 | goto failure; | ||
| 158 | } | ||
| 159 | if (memcmp(p, cms_msg, len) != 0) { | ||
| 160 | fprintf(stderr, "FAIL: CMS decrypt message differs"); | ||
| 161 | fprintf(stderr, "Got CMS data:\n"); | ||
| 162 | hexdump(p, len); | ||
| 163 | fprintf(stderr, "Want CMS data:\n"); | ||
| 164 | hexdump(cms_msg, strlen(cms_msg)); | ||
| 165 | goto failure; | ||
| 166 | } | ||
| 167 | |||
| 168 | failed = 0; | ||
| 169 | |||
| 170 | failure: | ||
| 171 | BIO_free(bio_mem); | ||
| 172 | CMS_ContentInfo_free(ci); | ||
| 173 | EVP_PKEY_free(pkey); | ||
| 174 | sk_X509_free(certs); | ||
| 175 | X509_free(cert); | ||
| 176 | |||
| 177 | return failed; | ||
| 178 | } | ||
| 179 | |||
| 180 | int | ||
| 181 | main(int argc, char **argv) | ||
| 182 | { | ||
| 183 | int failed = 0; | ||
| 184 | |||
| 185 | ERR_load_crypto_strings(); | ||
| 186 | |||
| 187 | failed |= test_cms_encrypt_decrypt(); | ||
| 188 | |||
| 189 | return failed; | ||
| 190 | } | ||
