| 
									
										
										
										
											2016-05-03 02:46:51 +08:00
										 |  |  | /*
 | 
					
						
							| 
									
										
										
										
											2021-01-28 20:54:57 +08:00
										 |  |  |  * Copyright 2016-2021 The OpenSSL Project Authors. All Rights Reserved. | 
					
						
							| 
									
										
										
										
											2016-05-03 02:46:51 +08:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2018-12-06 20:05:25 +08:00
										 |  |  |  * Licensed under the Apache License 2.0 (the "License"); | 
					
						
							| 
									
										
										
										
											2016-05-03 02:46:51 +08:00
										 |  |  |  * you may not use this file except in compliance with the License. | 
					
						
							|  |  |  |  * You may obtain a copy of the License at | 
					
						
							|  |  |  |  * https://www.openssl.org/source/license.html
 | 
					
						
							|  |  |  |  * or in the file LICENSE in the source distribution. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <stdio.h>
 | 
					
						
							|  |  |  | #include <string.h>
 | 
					
						
							|  |  |  | #include <errno.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <openssl/x509.h>
 | 
					
						
							|  |  |  | #include <openssl/pem.h>
 | 
					
						
							|  |  |  | #include <openssl/conf.h>
 | 
					
						
							|  |  |  | #include <openssl/err.h>
 | 
					
						
							| 
									
										
										
										
											2017-08-22 20:35:43 +08:00
										 |  |  | #include "internal/nelem.h"
 | 
					
						
							| 
									
										
										
										
											2017-04-12 04:16:33 +08:00
										 |  |  | #include "testutil.h"
 | 
					
						
							| 
									
										
										
										
											2016-05-03 02:46:51 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-12 21:27:33 +08:00
										 |  |  | static int test_certs(int num) | 
					
						
							| 
									
										
										
										
											2016-05-03 02:46:51 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2017-06-20 05:58:51 +08:00
										 |  |  |     int c; | 
					
						
							| 
									
										
										
										
											2016-05-03 02:46:51 +08:00
										 |  |  |     char *name = 0; | 
					
						
							|  |  |  |     char *header = 0; | 
					
						
							|  |  |  |     unsigned char *data = 0; | 
					
						
							|  |  |  |     long len; | 
					
						
							|  |  |  |     typedef X509 *(*d2i_X509_t)(X509 **, const unsigned char **, long); | 
					
						
							| 
									
										
										
										
											2019-01-16 04:51:25 +08:00
										 |  |  |     typedef int (*i2d_X509_t)(const X509 *, unsigned char **); | 
					
						
							| 
									
										
										
										
											2016-05-03 02:46:51 +08:00
										 |  |  |     int err = 0; | 
					
						
							| 
									
										
										
										
											2017-07-18 09:48:27 +08:00
										 |  |  |     BIO *fp = BIO_new_file(test_get_argument(num), "r"); | 
					
						
							| 
									
										
										
										
											2017-04-12 21:27:33 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (!TEST_ptr(fp)) | 
					
						
							|  |  |  |         return 0; | 
					
						
							| 
									
										
										
										
											2016-05-03 02:46:51 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-20 05:58:51 +08:00
										 |  |  |     for (c = 0; !err && PEM_read_bio(fp, &name, &header, &data, &len); ++c) { | 
					
						
							|  |  |  |         const int trusted = (strcmp(name, PEM_STRING_X509_TRUSTED) == 0); | 
					
						
							| 
									
										
										
										
											2016-05-03 02:46:51 +08:00
										 |  |  |         d2i_X509_t d2i = trusted ? d2i_X509_AUX : d2i_X509; | 
					
						
							|  |  |  |         i2d_X509_t i2d = trusted ? i2d_X509_AUX : i2d_X509; | 
					
						
							|  |  |  |         X509 *cert = NULL; | 
					
						
							| 
									
										
										
										
											2020-12-30 16:49:20 +08:00
										 |  |  |         X509 *reuse = NULL; | 
					
						
							| 
									
										
										
										
											2017-06-19 09:21:22 +08:00
										 |  |  |         const unsigned char *p = data; | 
					
						
							| 
									
										
										
										
											2016-05-03 02:46:51 +08:00
										 |  |  |         unsigned char *buf = NULL; | 
					
						
							|  |  |  |         unsigned char *bufp; | 
					
						
							|  |  |  |         long enclen; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-19 09:21:22 +08:00
										 |  |  |         if (!trusted | 
					
						
							| 
									
										
										
										
											2016-05-03 02:46:51 +08:00
										 |  |  |             && strcmp(name, PEM_STRING_X509) != 0 | 
					
						
							| 
									
										
										
										
											2017-06-19 09:21:22 +08:00
										 |  |  |             && strcmp(name, PEM_STRING_X509_OLD) != 0) { | 
					
						
							|  |  |  |             TEST_error("unexpected PEM object: %s", name); | 
					
						
							| 
									
										
										
										
											2016-05-03 02:46:51 +08:00
										 |  |  |             err = 1; | 
					
						
							| 
									
										
										
										
											2017-06-19 09:21:22 +08:00
										 |  |  |             goto next; | 
					
						
							| 
									
										
										
										
											2016-05-03 02:46:51 +08:00
										 |  |  |         } | 
					
						
							|  |  |  |         cert = d2i(NULL, &p, len); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (cert == NULL || (p - data) != len) { | 
					
						
							| 
									
										
										
										
											2017-06-19 09:21:22 +08:00
										 |  |  |             TEST_error("error parsing input %s", name); | 
					
						
							| 
									
										
										
										
											2016-05-03 02:46:51 +08:00
										 |  |  |             err = 1; | 
					
						
							|  |  |  |             goto next; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         /* Test traditional 2-pass encoding into caller allocated buffer */ | 
					
						
							|  |  |  |         enclen = i2d(cert, NULL); | 
					
						
							|  |  |  |         if (len != enclen) { | 
					
						
							| 
									
										
										
										
											2017-06-19 09:21:22 +08:00
										 |  |  |             TEST_error("encoded length %ld of %s != input length %ld", | 
					
						
							|  |  |  |                        enclen, name, len); | 
					
						
							| 
									
										
										
										
											2016-05-03 02:46:51 +08:00
										 |  |  |             err = 1; | 
					
						
							|  |  |  |             goto next; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         if ((buf = bufp = OPENSSL_malloc(len)) == NULL) { | 
					
						
							| 
									
										
										
										
											2017-06-19 09:21:22 +08:00
										 |  |  |             TEST_perror("malloc"); | 
					
						
							| 
									
										
										
										
											2016-05-03 02:46:51 +08:00
										 |  |  |             err = 1; | 
					
						
							|  |  |  |             goto next; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         enclen = i2d(cert, &bufp); | 
					
						
							|  |  |  |         if (len != enclen) { | 
					
						
							| 
									
										
										
										
											2017-06-19 09:21:22 +08:00
										 |  |  |             TEST_error("encoded length %ld of %s != input length %ld", | 
					
						
							|  |  |  |                        enclen, name, len); | 
					
						
							| 
									
										
										
										
											2016-05-03 02:46:51 +08:00
										 |  |  |             err = 1; | 
					
						
							|  |  |  |             goto next; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         enclen = (long) (bufp - buf); | 
					
						
							|  |  |  |         if (enclen != len) { | 
					
						
							| 
									
										
										
										
											2017-06-19 09:21:22 +08:00
										 |  |  |             TEST_error("unexpected buffer position after encoding %s", name); | 
					
						
							| 
									
										
										
										
											2016-05-03 02:46:51 +08:00
										 |  |  |             err = 1; | 
					
						
							|  |  |  |             goto next; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         if (memcmp(buf, data, len) != 0) { | 
					
						
							| 
									
										
										
										
											2017-06-19 09:21:22 +08:00
										 |  |  |             TEST_error("encoded content of %s does not match input", name); | 
					
						
							| 
									
										
										
										
											2016-05-03 02:46:51 +08:00
										 |  |  |             err = 1; | 
					
						
							|  |  |  |             goto next; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2019-01-30 02:51:59 +08:00
										 |  |  |         p = buf; | 
					
						
							| 
									
										
										
										
											2020-12-30 16:49:20 +08:00
										 |  |  |         reuse = d2i(NULL, &p, enclen); | 
					
						
							|  |  |  |         if (reuse == NULL) { | 
					
						
							|  |  |  |             TEST_error("second d2i call failed for %s", name); | 
					
						
							|  |  |  |             err = 1; | 
					
						
							|  |  |  |             goto next; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         err = X509_cmp(reuse, cert); | 
					
						
							|  |  |  |         if (err != 0) { | 
					
						
							|  |  |  |             TEST_error("X509_cmp for %s resulted in %d", name, err); | 
					
						
							| 
									
										
										
										
											2019-01-30 02:51:59 +08:00
										 |  |  |             err = 1; | 
					
						
							|  |  |  |             goto next; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2016-05-03 02:46:51 +08:00
										 |  |  |         OPENSSL_free(buf); | 
					
						
							|  |  |  |         buf = NULL; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         /* Test 1-pass encoding into library allocated buffer */ | 
					
						
							|  |  |  |         enclen = i2d(cert, &buf); | 
					
						
							|  |  |  |         if (len != enclen) { | 
					
						
							| 
									
										
										
										
											2017-06-19 09:21:22 +08:00
										 |  |  |             TEST_error("encoded length %ld of %s != input length %ld", | 
					
						
							|  |  |  |                        enclen, name, len); | 
					
						
							| 
									
										
										
										
											2016-05-03 02:46:51 +08:00
										 |  |  |             err = 1; | 
					
						
							|  |  |  |             goto next; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         if (memcmp(buf, data, len) != 0) { | 
					
						
							| 
									
										
										
										
											2017-06-19 09:21:22 +08:00
										 |  |  |             TEST_error("encoded content of %s does not match input", name); | 
					
						
							| 
									
										
										
										
											2016-05-03 02:46:51 +08:00
										 |  |  |             err = 1; | 
					
						
							|  |  |  |             goto next; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (trusted) { | 
					
						
							|  |  |  |             /* Encode just the cert and compare with initial encoding */ | 
					
						
							|  |  |  |             OPENSSL_free(buf); | 
					
						
							|  |  |  |             buf = NULL; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             /* Test 1-pass encoding into library allocated buffer */ | 
					
						
							|  |  |  |             enclen = i2d(cert, &buf); | 
					
						
							|  |  |  |             if (enclen > len) { | 
					
						
							| 
									
										
										
										
											2017-06-19 09:21:22 +08:00
										 |  |  |                 TEST_error("encoded length %ld of %s > input length %ld", | 
					
						
							|  |  |  |                            enclen, name, len); | 
					
						
							| 
									
										
										
										
											2016-05-03 02:46:51 +08:00
										 |  |  |                 err = 1; | 
					
						
							|  |  |  |                 goto next; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             if (memcmp(buf, data, enclen) != 0) { | 
					
						
							| 
									
										
										
										
											2017-06-19 09:21:22 +08:00
										 |  |  |                 TEST_error("encoded cert content does not match input"); | 
					
						
							| 
									
										
										
										
											2016-05-03 02:46:51 +08:00
										 |  |  |                 err = 1; | 
					
						
							|  |  |  |                 goto next; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-19 09:21:22 +08:00
										 |  |  |         /*
 | 
					
						
							|  |  |  |          * If any of these were null, PEM_read() would have failed. | 
					
						
							|  |  |  |          */ | 
					
						
							| 
									
										
										
										
											2016-05-03 02:46:51 +08:00
										 |  |  |     next: | 
					
						
							|  |  |  |         X509_free(cert); | 
					
						
							| 
									
										
										
										
											2020-12-30 16:49:20 +08:00
										 |  |  |         X509_free(reuse); | 
					
						
							| 
									
										
										
										
											2016-05-03 02:46:51 +08:00
										 |  |  |         OPENSSL_free(buf); | 
					
						
							| 
									
										
										
										
											2017-06-19 09:21:22 +08:00
										 |  |  |         OPENSSL_free(name); | 
					
						
							|  |  |  |         OPENSSL_free(header); | 
					
						
							|  |  |  |         OPENSSL_free(data); | 
					
						
							| 
									
										
										
										
											2016-05-03 02:46:51 +08:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2017-04-12 21:27:33 +08:00
										 |  |  |     BIO_free(fp); | 
					
						
							| 
									
										
										
										
											2016-05-03 02:46:51 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (ERR_GET_REASON(ERR_peek_last_error()) == PEM_R_NO_START_LINE) { | 
					
						
							|  |  |  |         /* Reached end of PEM file */ | 
					
						
							| 
									
										
										
										
											2017-06-20 05:58:51 +08:00
										 |  |  |         if (c > 0) { | 
					
						
							| 
									
										
										
										
											2016-05-03 02:46:51 +08:00
										 |  |  |             ERR_clear_error(); | 
					
						
							|  |  |  |             return 1; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /* Some other PEM read error */ | 
					
						
							|  |  |  |     return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-16 10:36:01 +08:00
										 |  |  | OPT_TEST_DECLARE_USAGE("certfile...\n") | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-18 09:48:27 +08:00
										 |  |  | int setup_tests(void) | 
					
						
							| 
									
										
										
										
											2016-05-03 02:46:51 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-01-30 23:30:17 +08:00
										 |  |  |     size_t n; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (!test_skip_common_options()) { | 
					
						
							|  |  |  |         TEST_error("Error parsing test options\n"); | 
					
						
							|  |  |  |         return 0; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     n = test_get_argument_count(); | 
					
						
							| 
									
										
										
										
											2018-08-16 10:36:01 +08:00
										 |  |  |     if (n == 0) | 
					
						
							| 
									
										
										
										
											2017-04-12 21:27:33 +08:00
										 |  |  |         return 0; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-11 03:15:56 +08:00
										 |  |  |     ADD_ALL_TESTS(test_certs, (int)n); | 
					
						
							| 
									
										
										
										
											2017-07-18 09:48:27 +08:00
										 |  |  |     return 1; | 
					
						
							| 
									
										
										
										
											2016-05-03 02:46:51 +08:00
										 |  |  | } |