yuezonghe | 824eb0c | 2024-06-27 02:32:26 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 1999-2022 The OpenSSL Project Authors. All Rights Reserved. |
| 3 | * |
| 4 | * Licensed under the OpenSSL license (the "License"). You may not use |
| 5 | * this file except in compliance with the License. You can obtain a copy |
| 6 | * in the file LICENSE in the source distribution or at |
| 7 | * https://www.openssl.org/source/license.html |
| 8 | */ |
| 9 | |
| 10 | /* Internal tests for the asn1 module */ |
| 11 | |
| 12 | #include <stdio.h> |
| 13 | #include <string.h> |
| 14 | |
| 15 | #include <openssl/asn1.h> |
| 16 | #include <openssl/evp.h> |
| 17 | #include <openssl/objects.h> |
| 18 | #include "testutil.h" |
| 19 | #include "internal/nelem.h" |
| 20 | |
| 21 | /********************************************************************** |
| 22 | * |
| 23 | * Test of a_strnid's tbl_standard |
| 24 | * |
| 25 | ***/ |
| 26 | |
| 27 | #include "../crypto/asn1/tbl_standard.h" |
| 28 | |
| 29 | static int test_tbl_standard(void) |
| 30 | { |
| 31 | const ASN1_STRING_TABLE *tmp; |
| 32 | int last_nid = -1; |
| 33 | size_t i; |
| 34 | |
| 35 | for (tmp = tbl_standard, i = 0; i < OSSL_NELEM(tbl_standard); i++, tmp++) { |
| 36 | if (tmp->nid < last_nid) { |
| 37 | last_nid = 0; |
| 38 | break; |
| 39 | } |
| 40 | last_nid = tmp->nid; |
| 41 | } |
| 42 | |
| 43 | if (TEST_int_ne(last_nid, 0)) { |
| 44 | TEST_info("asn1 tbl_standard: Table order OK"); |
| 45 | return 1; |
| 46 | } |
| 47 | |
| 48 | TEST_info("asn1 tbl_standard: out of order"); |
| 49 | for (tmp = tbl_standard, i = 0; i < OSSL_NELEM(tbl_standard); i++, tmp++) |
| 50 | TEST_note("asn1 tbl_standard: Index %zu, NID %d, Name=%s", |
| 51 | i, tmp->nid, OBJ_nid2ln(tmp->nid)); |
| 52 | |
| 53 | return 0; |
| 54 | } |
| 55 | |
| 56 | /********************************************************************** |
| 57 | * |
| 58 | * Test of ameth_lib's standard_methods |
| 59 | * |
| 60 | ***/ |
| 61 | |
| 62 | #include "crypto/asn1.h" |
| 63 | #include "../crypto/asn1/standard_methods.h" |
| 64 | |
| 65 | static int test_standard_methods(void) |
| 66 | { |
| 67 | const EVP_PKEY_ASN1_METHOD **tmp; |
| 68 | int last_pkey_id = -1; |
| 69 | size_t i; |
| 70 | int ok = 1; |
| 71 | |
| 72 | for (tmp = standard_methods, i = 0; i < OSSL_NELEM(standard_methods); |
| 73 | i++, tmp++) { |
| 74 | if ((*tmp)->pkey_id < last_pkey_id) { |
| 75 | last_pkey_id = 0; |
| 76 | break; |
| 77 | } |
| 78 | last_pkey_id = (*tmp)->pkey_id; |
| 79 | |
| 80 | /* |
| 81 | * One of the following must be true: |
| 82 | * |
| 83 | * pem_str == NULL AND ASN1_PKEY_ALIAS is set |
| 84 | * pem_str != NULL AND ASN1_PKEY_ALIAS is clear |
| 85 | * |
| 86 | * Anything else is an error and may lead to a corrupt ASN1 method table |
| 87 | */ |
| 88 | if (!TEST_true(((*tmp)->pem_str == NULL && ((*tmp)->pkey_flags & ASN1_PKEY_ALIAS) != 0) |
| 89 | || ((*tmp)->pem_str != NULL && ((*tmp)->pkey_flags & ASN1_PKEY_ALIAS) == 0))) { |
| 90 | TEST_note("asn1 standard methods: Index %zu, pkey ID %d, Name=%s", |
| 91 | i, (*tmp)->pkey_id, OBJ_nid2sn((*tmp)->pkey_id)); |
| 92 | ok = 0; |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | if (TEST_int_ne(last_pkey_id, 0)) { |
| 97 | TEST_info("asn1 standard methods: Table order OK"); |
| 98 | return ok; |
| 99 | } |
| 100 | |
| 101 | TEST_note("asn1 standard methods: out of order"); |
| 102 | for (tmp = standard_methods, i = 0; i < OSSL_NELEM(standard_methods); |
| 103 | i++, tmp++) |
| 104 | TEST_note("asn1 standard methods: Index %zu, pkey ID %d, Name=%s", |
| 105 | i, (*tmp)->pkey_id, OBJ_nid2sn((*tmp)->pkey_id)); |
| 106 | |
| 107 | return 0; |
| 108 | } |
| 109 | |
| 110 | /********************************************************************** |
| 111 | * |
| 112 | * Regression test for issue where OBJ_nid2obj does not raise |
| 113 | * an error when a NID is not registered. |
| 114 | * |
| 115 | ***/ |
| 116 | static int test_nid2obj_nonexist(void) |
| 117 | { |
| 118 | ASN1_OBJECT *obj; |
| 119 | unsigned long err; |
| 120 | |
| 121 | obj = OBJ_nid2obj(INT_MAX); |
| 122 | if (!TEST_true(obj == NULL)) |
| 123 | return 0; |
| 124 | |
| 125 | err = ERR_get_error(); |
| 126 | |
| 127 | if (!TEST_int_eq(ERR_GET_FUNC(err), OBJ_F_OBJ_NID2OBJ)) |
| 128 | return 0; |
| 129 | |
| 130 | if (!TEST_int_eq(ERR_GET_REASON(err), OBJ_R_UNKNOWN_NID)) |
| 131 | return 0; |
| 132 | |
| 133 | return 1; |
| 134 | } |
| 135 | |
| 136 | int setup_tests(void) |
| 137 | { |
| 138 | ADD_TEST(test_tbl_standard); |
| 139 | ADD_TEST(test_standard_methods); |
| 140 | ADD_TEST(test_nid2obj_nonexist); |
| 141 | return 1; |
| 142 | } |