obj_test.cc
Go to the documentation of this file.
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 
15 #include <gtest/gtest.h>
16 
17 #include <openssl/asn1.h>
18 #include <openssl/bytestring.h>
19 #include <openssl/crypto.h>
20 #include <openssl/obj.h>
21 
22 #include "../internal.h"
23 
24 
25 TEST(ObjTest, TestBasic) {
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));
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));
42 
43  ASSERT_STREQ(kShortName, OBJ_nid2sn(kNID));
44  ASSERT_STREQ(kLongName, OBJ_nid2ln(kNID));
45 
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"));
49 
50  CBS_init(&cbs, NULL, 0);
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));
59 }
60 
61 TEST(ObjTest, TestSignatureAlgorithms) {
62  int digest_nid, pkey_nid;
64  &pkey_nid));
65  ASSERT_EQ(digest_nid, NID_sha256);
66  ASSERT_EQ(pkey_nid, NID_rsaEncryption);
67 
68  ASSERT_FALSE(OBJ_find_sigid_algs(NID_sha256, &digest_nid, &pkey_nid));
69 
70  int sign_nid;
74 }
75 
76 static bool ExpectObj2Txt(const uint8_t *der, size_t der_len,
77  bool always_return_oid, const char *expected) {
78  bssl::UniquePtr<ASN1_OBJECT> obj(
79  ASN1_OBJECT_create(NID_undef, der, static_cast<int>(der_len),
80  /*sn=*/nullptr, /*ln=*/nullptr));
81  if (!obj) {
82  return false;
83  }
84 
85  int expected_len = static_cast<int>(strlen(expected));
86 
87  int len = OBJ_obj2txt(nullptr, 0, obj.get(), always_return_oid);
88  if (len != expected_len) {
89  fprintf(stderr,
90  "OBJ_obj2txt of %s with out_len = 0 returned %d, wanted %d.\n",
91  expected, len, expected_len);
92  return false;
93  }
94 
95  char short_buf[1];
96  OPENSSL_memset(short_buf, 0xff, sizeof(short_buf));
97  len = OBJ_obj2txt(short_buf, sizeof(short_buf), obj.get(), always_return_oid);
98  if (len != expected_len) {
99  fprintf(stderr,
100  "OBJ_obj2txt of %s with out_len = 1 returned %d, wanted %d.\n",
101  expected, len, expected_len);
102  return false;
103  }
104 
105  if (OPENSSL_memchr(short_buf, '\0', sizeof(short_buf)) == nullptr) {
106  fprintf(stderr,
107  "OBJ_obj2txt of %s with out_len = 1 did not NUL-terminate the "
108  "output.\n",
109  expected);
110  return false;
111  }
112 
113  char buf[256];
114  len = OBJ_obj2txt(buf, sizeof(buf), obj.get(), always_return_oid);
115  if (len != expected_len) {
116  fprintf(stderr,
117  "OBJ_obj2txt of %s with out_len = 256 returned %d, wanted %d.\n",
118  expected, len, expected_len);
119  return false;
120  }
121 
122  if (strcmp(buf, expected) != 0) {
123  fprintf(stderr, "OBJ_obj2txt returned \"%s\"; wanted \"%s\".\n", buf,
124  expected);
125  return false;
126  }
127 
128  return true;
129 }
130 
131 TEST(ObjTest, TestObj2Txt) {
132  // kSHA256WithRSAEncryption is the DER representation of
133  // 1.2.840.113549.1.1.11, id-sha256WithRSAEncryption.
134  static const uint8_t kSHA256WithRSAEncryption[] = {
135  0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0b,
136  };
137 
138  // kBasicConstraints is the DER representation of 2.5.29.19,
139  // id-basicConstraints.
140  static const uint8_t kBasicConstraints[] = {
141  0x55, 0x1d, 0x13,
142  };
143 
144  // kTestOID is the DER representation of 1.2.840.113554.4.1.72585.0,
145  // from https://davidben.net/oid.
146  static const uint8_t kTestOID[] = {
147  0x2a, 0x86, 0x48, 0x86, 0xf7, 0x12, 0x04, 0x01, 0x84, 0xb7, 0x09, 0x00,
148  };
149 
150  ASSERT_TRUE(
151  ExpectObj2Txt(kSHA256WithRSAEncryption, sizeof(kSHA256WithRSAEncryption),
152  true /* don't return name */, "1.2.840.113549.1.1.11"));
153  ASSERT_TRUE(
154  ExpectObj2Txt(kSHA256WithRSAEncryption, sizeof(kSHA256WithRSAEncryption),
155  false /* return name */, "sha256WithRSAEncryption"));
156  ASSERT_TRUE(ExpectObj2Txt(kBasicConstraints, sizeof(kBasicConstraints),
157  true /* don't return name */, "2.5.29.19"));
158  ASSERT_TRUE(ExpectObj2Txt(kBasicConstraints, sizeof(kBasicConstraints),
159  false /* return name */,
160  "X509v3 Basic Constraints"));
161  ASSERT_TRUE(ExpectObj2Txt(kTestOID, sizeof(kTestOID),
162  true /* don't return name */,
163  "1.2.840.113554.4.1.72585.0"));
164  ASSERT_TRUE(ExpectObj2Txt(kTestOID, sizeof(kTestOID), false /* return name */,
165  "1.2.840.113554.4.1.72585.0"));
166  // Python depends on the empty OID successfully encoding as the empty
167  // string.
168  ASSERT_TRUE(ExpectObj2Txt(nullptr, 0, false /* return name */, ""));
169  ASSERT_TRUE(ExpectObj2Txt(nullptr, 0, true /* don't return name */, ""));
170 
171  // kNonMinimalOID is kBasicConstraints with the final component non-minimally
172  // encoded.
173  static const uint8_t kNonMinimalOID[] = {0x55, 0x1d, 0x80, 0x13};
174  bssl::UniquePtr<ASN1_OBJECT> obj(
175  ASN1_OBJECT_create(NID_undef, kNonMinimalOID, sizeof(kNonMinimalOID),
176  /*sn=*/nullptr, /*ln=*/nullptr));
177  ASSERT_TRUE(obj);
178  ASSERT_EQ(-1, OBJ_obj2txt(NULL, 0, obj.get(), 0));
179 
180  // kOverflowOID is the DER representation of
181  // 1.2.840.113554.4.1.72585.18446744073709551616. (The final value is 2^64.)
182  static const uint8_t kOverflowOID[] = {
183  0x2a, 0x86, 0x48, 0x86, 0xf7, 0x12, 0x04, 0x01, 0x84, 0xb7, 0x09,
184  0x82, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x00,
185  };
186  obj.reset(ASN1_OBJECT_create(NID_undef, kOverflowOID, sizeof(kOverflowOID),
187  /*sn=*/nullptr, /*ln=*/nullptr));
188  ASSERT_TRUE(obj);
189  ASSERT_EQ(-1, OBJ_obj2txt(NULL, 0, obj.get(), 0));
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[] = {0x55, 0x1d, 0x93};
194  obj.reset(ASN1_OBJECT_create(NID_undef, kInvalidOID, sizeof(kInvalidOID),
195  /*sn=*/nullptr, /*ln=*/nullptr));
196  ASSERT_TRUE(obj);
197  ASSERT_EQ(-1, OBJ_obj2txt(NULL, 0, obj.get(), 0));
198 }
obj
OPENSSL_EXPORT const ASN1_OBJECT * obj
Definition: x509.h:1671
cbs_st
Definition: bytestring.h:39
ASSERT_STREQ
#define ASSERT_STREQ(s1, s2)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:2104
TestBasic
Definition: TestBasic.java:6
OBJ_ln2nid
#define OBJ_ln2nid
Definition: boringssl_prefix_symbols.h:1852
NID_rsaEncryption
#define NID_rsaEncryption
Definition: nid.h:114
buf
voidpf void * buf
Definition: bloaty/third_party/zlib/contrib/minizip/ioapi.h:136
OPENSSL_memchr
static void * OPENSSL_memchr(const void *s, int c, size_t n)
Definition: third_party/boringssl-with-bazel/src/crypto/internal.h:801
cbs
const CBS * cbs
Definition: third_party/boringssl-with-bazel/src/crypto/trust_token/internal.h:107
ExpectObj2Txt
static bool ExpectObj2Txt(const uint8_t *der, size_t der_len, bool always_return_oid, const char *expected)
Definition: obj_test.cc:76
CBS_init
#define CBS_init
Definition: boringssl_prefix_symbols.h:1085
uint8_t
unsigned char uint8_t
Definition: stdint-msvc2008.h:78
OPENSSL_memset
static void * OPENSSL_memset(void *dst, int c, size_t n)
Definition: third_party/boringssl-with-bazel/src/crypto/internal.h:835
NID_sha256WithRSAEncryption
#define NID_sha256WithRSAEncryption
Definition: nid.h:2973
python_utils.port_server.stderr
stderr
Definition: port_server.py:51
bytestring.h
NID_sha256
#define NID_sha256
Definition: nid.h:2993
OBJ_cbs2nid
#define OBJ_cbs2nid
Definition: boringssl_prefix_symbols.h:1843
crypto.h
NID_undef
#define NID_undef
Definition: nid.h:85
OBJ_txt2nid
#define OBJ_txt2nid
Definition: boringssl_prefix_symbols.h:1860
OBJ_find_sigid_by_algs
#define OBJ_find_sigid_by_algs
Definition: boringssl_prefix_symbols.h:1849
OBJ_nid2sn
#define OBJ_nid2sn
Definition: boringssl_prefix_symbols.h:1856
OBJ_sn2nid
#define OBJ_sn2nid
Definition: boringssl_prefix_symbols.h:1859
ASSERT_TRUE
#define ASSERT_TRUE(condition)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:1973
ASSERT_FALSE
#define ASSERT_FALSE(condition)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:1976
NID_dsa
#define NID_dsa
Definition: nid.h:612
OBJ_obj2txt
#define OBJ_obj2txt
Definition: boringssl_prefix_symbols.h:1858
obj.h
TEST
TEST(ObjTest, TestBasic)
Definition: obj_test.cc:25
len
int len
Definition: abseil-cpp/absl/base/internal/low_level_alloc_test.cc:46
OBJ_find_sigid_algs
#define OBJ_find_sigid_algs
Definition: boringssl_prefix_symbols.h:1848
OBJ_nid2ln
#define OBJ_nid2ln
Definition: boringssl_prefix_symbols.h:1854
ASN1_OBJECT_create
#define ASN1_OBJECT_create
Definition: boringssl_prefix_symbols.h:654
asn1.h
ASSERT_EQ
#define ASSERT_EQ(val1, val2)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:2056


grpc
Author(s):
autogenerated on Thu Mar 13 2025 03:00:43