David Benjamin | dc9194f | 2016-04-24 12:17:46 -0400 | [diff] [blame] | 1 | /* Copyright (c) 2016, Google Inc. |
| 2 | * |
| 3 | * Permission to use, copy, modify, and/or distribute this software for any |
| 4 | * purpose with or without fee is hereby granted, provided that the above |
| 5 | * copyright notice and this permission notice appear in all copies. |
| 6 | * |
| 7 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
| 8 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 9 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY |
| 10 | * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 11 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION |
| 12 | * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN |
| 13 | * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ |
| 14 | |
Kári Tristan Helgason | ce9f693 | 2017-06-02 14:55:16 +0200 | [diff] [blame] | 15 | #include <gtest/gtest.h> |
David Benjamin | dc9194f | 2016-04-24 12:17:46 -0400 | [diff] [blame] | 16 | |
David Benjamin | ea655fa | 2016-08-04 16:45:15 -0400 | [diff] [blame] | 17 | #include <openssl/asn1.h> |
David Benjamin | dc9194f | 2016-04-24 12:17:46 -0400 | [diff] [blame] | 18 | #include <openssl/bytestring.h> |
| 19 | #include <openssl/crypto.h> |
| 20 | #include <openssl/obj.h> |
| 21 | |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 22 | #include "../internal.h" |
| 23 | |
David Benjamin | dc9194f | 2016-04-24 12:17:46 -0400 | [diff] [blame] | 24 | |
Kári Tristan Helgason | ce9f693 | 2017-06-02 14:55:16 +0200 | [diff] [blame] | 25 | TEST(ObjTest, TestBasic) { |
David Benjamin | dc9194f | 2016-04-24 12:17:46 -0400 | [diff] [blame] | 26 | static const int kNID = NID_sha256WithRSAEncryption; |
| 27 | static const char kShortName[] = "RSA-SHA256"; |
| 28 | static const char kLongName[] = "sha256WithRSAEncryption"; |
| 29 | static const char kText[] = "1.2.840.113549.1.1.11"; |
| 30 | static const uint8_t kDER[] = { |
| 31 | 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0b, |
| 32 | }; |
| 33 | |
| 34 | CBS cbs; |
| 35 | CBS_init(&cbs, kDER, sizeof(kDER)); |
Kári Tristan Helgason | ce9f693 | 2017-06-02 14:55:16 +0200 | [diff] [blame] | 36 | ASSERT_EQ(kNID, OBJ_cbs2nid(&cbs)); |
| 37 | ASSERT_EQ(kNID, OBJ_sn2nid(kShortName)); |
| 38 | ASSERT_EQ(kNID, OBJ_ln2nid(kLongName)); |
| 39 | ASSERT_EQ(kNID, OBJ_txt2nid(kShortName)); |
| 40 | ASSERT_EQ(kNID, OBJ_txt2nid(kLongName)); |
| 41 | ASSERT_EQ(kNID, OBJ_txt2nid(kText)); |
David Benjamin | dc9194f | 2016-04-24 12:17:46 -0400 | [diff] [blame] | 42 | |
Kári Tristan Helgason | ce9f693 | 2017-06-02 14:55:16 +0200 | [diff] [blame] | 43 | ASSERT_STREQ(kShortName, OBJ_nid2sn(kNID)); |
| 44 | ASSERT_STREQ(kLongName, OBJ_nid2ln(kNID)); |
David Benjamin | dc9194f | 2016-04-24 12:17:46 -0400 | [diff] [blame] | 45 | |
Kári Tristan Helgason | ce9f693 | 2017-06-02 14:55:16 +0200 | [diff] [blame] | 46 | ASSERT_EQ(NID_undef, OBJ_sn2nid("this is not an OID")); |
| 47 | ASSERT_EQ(NID_undef, OBJ_ln2nid("this is not an OID")); |
| 48 | ASSERT_EQ(NID_undef, OBJ_txt2nid("this is not an OID")); |
David Benjamin | dc9194f | 2016-04-24 12:17:46 -0400 | [diff] [blame] | 49 | |
| 50 | CBS_init(&cbs, NULL, 0); |
Kári Tristan Helgason | ce9f693 | 2017-06-02 14:55:16 +0200 | [diff] [blame] | 51 | ASSERT_EQ(NID_undef, OBJ_cbs2nid(&cbs)); |
David Benjamin | dc9194f | 2016-04-24 12:17:46 -0400 | [diff] [blame] | 52 | |
| 53 | // 1.2.840.113554.4.1.72585.2 (https://davidben.net/oid). |
| 54 | static const uint8_t kUnknownDER[] = { |
| 55 | 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x12, 0x04, 0x01, 0x84, 0xb7, 0x09, 0x02, |
| 56 | }; |
| 57 | CBS_init(&cbs, kUnknownDER, sizeof(kUnknownDER)); |
Kári Tristan Helgason | ce9f693 | 2017-06-02 14:55:16 +0200 | [diff] [blame] | 58 | ASSERT_EQ(NID_undef, OBJ_cbs2nid(&cbs)); |
David Benjamin | dc9194f | 2016-04-24 12:17:46 -0400 | [diff] [blame] | 59 | } |
| 60 | |
Kári Tristan Helgason | ce9f693 | 2017-06-02 14:55:16 +0200 | [diff] [blame] | 61 | TEST(ObjTest, TestSignatureAlgorithms) { |
David Benjamin | dc9194f | 2016-04-24 12:17:46 -0400 | [diff] [blame] | 62 | int digest_nid, pkey_nid; |
Kári Tristan Helgason | ce9f693 | 2017-06-02 14:55:16 +0200 | [diff] [blame] | 63 | ASSERT_TRUE(OBJ_find_sigid_algs(NID_sha256WithRSAEncryption, &digest_nid, |
| 64 | &pkey_nid)); |
| 65 | ASSERT_EQ(digest_nid, NID_sha256); |
| 66 | ASSERT_EQ(pkey_nid, NID_rsaEncryption); |
David Benjamin | dc9194f | 2016-04-24 12:17:46 -0400 | [diff] [blame] | 67 | |
Kári Tristan Helgason | ce9f693 | 2017-06-02 14:55:16 +0200 | [diff] [blame] | 68 | ASSERT_FALSE(OBJ_find_sigid_algs(NID_sha256, &digest_nid, &pkey_nid)); |
David Benjamin | dc9194f | 2016-04-24 12:17:46 -0400 | [diff] [blame] | 69 | |
| 70 | int sign_nid; |
Kári Tristan Helgason | ce9f693 | 2017-06-02 14:55:16 +0200 | [diff] [blame] | 71 | ASSERT_TRUE(OBJ_find_sigid_by_algs(&sign_nid, NID_sha256, NID_rsaEncryption)); |
| 72 | ASSERT_EQ(sign_nid, NID_sha256WithRSAEncryption); |
| 73 | ASSERT_FALSE(OBJ_find_sigid_by_algs(&sign_nid, NID_dsa, NID_rsaEncryption)); |
David Benjamin | dc9194f | 2016-04-24 12:17:46 -0400 | [diff] [blame] | 74 | } |
| 75 | |
David Benjamin | ea655fa | 2016-08-04 16:45:15 -0400 | [diff] [blame] | 76 | static bool ExpectObj2Txt(const uint8_t *der, size_t der_len, |
Adam Langley | fbe3a7b | 2016-08-09 21:25:45 -0700 | [diff] [blame] | 77 | bool always_return_oid, const char *expected) { |
David Benjamin | ea655fa | 2016-08-04 16:45:15 -0400 | [diff] [blame] | 78 | ASN1_OBJECT obj; |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 79 | OPENSSL_memset(&obj, 0, sizeof(obj)); |
David Benjamin | ea655fa | 2016-08-04 16:45:15 -0400 | [diff] [blame] | 80 | obj.data = der; |
| 81 | obj.length = static_cast<int>(der_len); |
| 82 | |
| 83 | int expected_len = static_cast<int>(strlen(expected)); |
| 84 | |
Adam Langley | fbe3a7b | 2016-08-09 21:25:45 -0700 | [diff] [blame] | 85 | int len = OBJ_obj2txt(nullptr, 0, &obj, always_return_oid); |
David Benjamin | ea655fa | 2016-08-04 16:45:15 -0400 | [diff] [blame] | 86 | if (len != expected_len) { |
| 87 | fprintf(stderr, |
| 88 | "OBJ_obj2txt of %s with out_len = 0 returned %d, wanted %d.\n", |
| 89 | expected, len, expected_len); |
| 90 | return false; |
| 91 | } |
| 92 | |
| 93 | char short_buf[1]; |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 94 | OPENSSL_memset(short_buf, 0xff, sizeof(short_buf)); |
Adam Langley | fbe3a7b | 2016-08-09 21:25:45 -0700 | [diff] [blame] | 95 | len = OBJ_obj2txt(short_buf, sizeof(short_buf), &obj, always_return_oid); |
David Benjamin | ea655fa | 2016-08-04 16:45:15 -0400 | [diff] [blame] | 96 | if (len != expected_len) { |
| 97 | fprintf(stderr, |
| 98 | "OBJ_obj2txt of %s with out_len = 1 returned %d, wanted %d.\n", |
| 99 | expected, len, expected_len); |
| 100 | return false; |
| 101 | } |
| 102 | |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 103 | if (OPENSSL_memchr(short_buf, '\0', sizeof(short_buf)) == nullptr) { |
David Benjamin | ea655fa | 2016-08-04 16:45:15 -0400 | [diff] [blame] | 104 | fprintf(stderr, |
| 105 | "OBJ_obj2txt of %s with out_len = 1 did not NUL-terminate the " |
| 106 | "output.\n", |
| 107 | expected); |
| 108 | return false; |
| 109 | } |
| 110 | |
| 111 | char buf[256]; |
Adam Langley | fbe3a7b | 2016-08-09 21:25:45 -0700 | [diff] [blame] | 112 | len = OBJ_obj2txt(buf, sizeof(buf), &obj, always_return_oid); |
David Benjamin | ea655fa | 2016-08-04 16:45:15 -0400 | [diff] [blame] | 113 | if (len != expected_len) { |
| 114 | fprintf(stderr, |
| 115 | "OBJ_obj2txt of %s with out_len = 256 returned %d, wanted %d.\n", |
| 116 | expected, len, expected_len); |
| 117 | return false; |
| 118 | } |
| 119 | |
| 120 | if (strcmp(buf, expected) != 0) { |
| 121 | fprintf(stderr, "OBJ_obj2txt returned \"%s\"; wanted \"%s\".\n", buf, |
| 122 | expected); |
| 123 | return false; |
| 124 | } |
| 125 | |
| 126 | return true; |
| 127 | } |
| 128 | |
Kári Tristan Helgason | ce9f693 | 2017-06-02 14:55:16 +0200 | [diff] [blame] | 129 | TEST(ObjTest, TestObj2Txt) { |
David Benjamin | ea655fa | 2016-08-04 16:45:15 -0400 | [diff] [blame] | 130 | // kSHA256WithRSAEncryption is the DER representation of |
| 131 | // 1.2.840.113549.1.1.11, id-sha256WithRSAEncryption. |
| 132 | static const uint8_t kSHA256WithRSAEncryption[] = { |
| 133 | 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0b, |
| 134 | }; |
| 135 | |
| 136 | // kBasicConstraints is the DER representation of 2.5.29.19, |
| 137 | // id-basicConstraints. |
| 138 | static const uint8_t kBasicConstraints[] = { |
| 139 | 0x55, 0x1d, 0x13, |
| 140 | }; |
| 141 | |
| 142 | // kTestOID is the DER representation of 1.2.840.113554.4.1.72585.0, |
| 143 | // from https://davidben.net/oid. |
| 144 | static const uint8_t kTestOID[] = { |
| 145 | 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x12, 0x04, 0x01, 0x84, 0xb7, 0x09, 0x00, |
| 146 | }; |
| 147 | |
Kári Tristan Helgason | ce9f693 | 2017-06-02 14:55:16 +0200 | [diff] [blame] | 148 | ASSERT_TRUE( |
| 149 | ExpectObj2Txt(kSHA256WithRSAEncryption, sizeof(kSHA256WithRSAEncryption), |
| 150 | true /* don't return name */, "1.2.840.113549.1.1.11")); |
| 151 | ASSERT_TRUE( |
| 152 | ExpectObj2Txt(kSHA256WithRSAEncryption, sizeof(kSHA256WithRSAEncryption), |
| 153 | false /* return name */, "sha256WithRSAEncryption")); |
| 154 | ASSERT_TRUE(ExpectObj2Txt(kBasicConstraints, sizeof(kBasicConstraints), |
| 155 | true /* don't return name */, "2.5.29.19")); |
| 156 | ASSERT_TRUE(ExpectObj2Txt(kBasicConstraints, sizeof(kBasicConstraints), |
| 157 | false /* return name */, |
| 158 | "X509v3 Basic Constraints")); |
| 159 | ASSERT_TRUE(ExpectObj2Txt(kTestOID, sizeof(kTestOID), |
| 160 | true /* don't return name */, |
| 161 | "1.2.840.113554.4.1.72585.0")); |
| 162 | ASSERT_TRUE(ExpectObj2Txt(kTestOID, sizeof(kTestOID), false /* return name */, |
| 163 | "1.2.840.113554.4.1.72585.0")); |
| 164 | // Python depends on the empty OID successfully encoding as the empty |
| 165 | // string. |
| 166 | ASSERT_TRUE(ExpectObj2Txt(nullptr, 0, false /* return name */, "")); |
| 167 | ASSERT_TRUE(ExpectObj2Txt(nullptr, 0, true /* don't return name */, "")); |
David Benjamin | ea655fa | 2016-08-04 16:45:15 -0400 | [diff] [blame] | 168 | |
David Benjamin | 2f8ea54 | 2016-08-04 17:21:36 -0400 | [diff] [blame] | 169 | ASN1_OBJECT obj; |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 170 | OPENSSL_memset(&obj, 0, sizeof(obj)); |
David Benjamin | 2f8ea54 | 2016-08-04 17:21:36 -0400 | [diff] [blame] | 171 | |
David Benjamin | 2f8ea54 | 2016-08-04 17:21:36 -0400 | [diff] [blame] | 172 | // kNonMinimalOID is kBasicConstraints with the final component non-minimally |
| 173 | // encoded. |
| 174 | static const uint8_t kNonMinimalOID[] = { |
| 175 | 0x55, 0x1d, 0x80, 0x13, |
| 176 | }; |
| 177 | obj.data = kNonMinimalOID; |
| 178 | obj.length = sizeof(kNonMinimalOID); |
Kári Tristan Helgason | ce9f693 | 2017-06-02 14:55:16 +0200 | [diff] [blame] | 179 | ASSERT_EQ(-1, OBJ_obj2txt(NULL, 0, &obj, 0)); |
David Benjamin | 2f8ea54 | 2016-08-04 17:21:36 -0400 | [diff] [blame] | 180 | |
| 181 | // kOverflowOID is the DER representation of |
| 182 | // 1.2.840.113554.4.1.72585.18446744073709551616. (The final value is 2^64.) |
| 183 | static const uint8_t kOverflowOID[] = { |
| 184 | 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x12, 0x04, 0x01, 0x84, 0xb7, 0x09, |
| 185 | 0x82, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x00, |
| 186 | }; |
| 187 | obj.data = kOverflowOID; |
| 188 | obj.length = sizeof(kOverflowOID); |
Kári Tristan Helgason | ce9f693 | 2017-06-02 14:55:16 +0200 | [diff] [blame] | 189 | ASSERT_EQ(-1, OBJ_obj2txt(NULL, 0, &obj, 0)); |
David Benjamin | 2f8ea54 | 2016-08-04 17:21:36 -0400 | [diff] [blame] | 190 | |
| 191 | // kInvalidOID is a mis-encoded version of kBasicConstraints with the final |
| 192 | // octet having the high bit set. |
| 193 | static const uint8_t kInvalidOID[] = { |
| 194 | 0x55, 0x1d, 0x93, |
| 195 | }; |
| 196 | obj.data = kInvalidOID; |
| 197 | obj.length = sizeof(kInvalidOID); |
Kári Tristan Helgason | ce9f693 | 2017-06-02 14:55:16 +0200 | [diff] [blame] | 198 | ASSERT_EQ(-1, OBJ_obj2txt(NULL, 0, &obj, 0)); |
David Benjamin | dc9194f | 2016-04-24 12:17:46 -0400 | [diff] [blame] | 199 | } |