Skip to content

Commit a83bcb5

Browse files
Revert "Trim some unused XN_FLAG_* values" (aws#1582)
1 parent b434043 commit a83bcb5

File tree

3 files changed

+96
-45
lines changed

3 files changed

+96
-45
lines changed

crypto/x509/name_print.c

+39-21
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@
5656

5757
#include <openssl/x509.h>
5858

59-
#include <assert.h>
6059
#include <inttypes.h>
6160
#include <string.h>
6261

@@ -85,7 +84,8 @@ static int do_indent(BIO *out, int indent) {
8584

8685
static int do_name_ex(BIO *out, const X509_NAME *n, int indent,
8786
unsigned long flags) {
88-
int prev = -1, orflags;
87+
int i, prev = -1, orflags, cnt;
88+
int fn_opt, fn_nid;
8989
char objtmp[80];
9090
const char *objbuf;
9191
int outlen, len;
@@ -142,8 +142,10 @@ static int do_name_ex(BIO *out, const X509_NAME *n, int indent,
142142
sep_eq_len = 1;
143143
}
144144

145-
int cnt = X509_NAME_entry_count(n);
146-
for (int i = 0; i < cnt; i++) {
145+
fn_opt = flags & XN_FLAG_FN_MASK;
146+
147+
cnt = X509_NAME_entry_count(n);
148+
for (i = 0; i < cnt; i++) {
147149
const X509_NAME_ENTRY *ent;
148150
if (flags & XN_FLAG_DN_REV) {
149151
ent = X509_NAME_get_entry(n, cnt - i - 1);
@@ -170,24 +172,40 @@ static int do_name_ex(BIO *out, const X509_NAME *n, int indent,
170172
prev = X509_NAME_ENTRY_set(ent);
171173
const ASN1_OBJECT *fn = X509_NAME_ENTRY_get_object(ent);
172174
const ASN1_STRING *val = X509_NAME_ENTRY_get_data(ent);
173-
assert((flags & XN_FLAG_FN_MASK) == XN_FLAG_FN_SN);
174-
int fn_nid = OBJ_obj2nid(fn);
175-
if (fn_nid == NID_undef) {
176-
OBJ_obj2txt(objtmp, sizeof(objtmp), fn, 1);
177-
objbuf = objtmp;
178-
} else {
179-
objbuf = OBJ_nid2sn(fn_nid);
180-
}
181-
if (objbuf == NULL) {
182-
return -1;
183-
}
184-
185-
int objlen = strlen(objbuf);
186-
if (!maybe_write(out, objbuf, objlen) ||
187-
!maybe_write(out, sep_eq, sep_eq_len)) {
188-
return -1;
175+
fn_nid = OBJ_obj2nid(fn);
176+
if (fn_opt != XN_FLAG_FN_NONE) {
177+
int objlen, fld_len;
178+
if ((fn_opt == XN_FLAG_FN_OID) || (fn_nid == NID_undef)) {
179+
OBJ_obj2txt(objtmp, sizeof objtmp, fn, 1);
180+
fld_len = 0; // XXX: what should this be?
181+
objbuf = objtmp;
182+
} else {
183+
if (fn_opt == XN_FLAG_FN_SN) {
184+
fld_len = FN_WIDTH_SN;
185+
objbuf = OBJ_nid2sn(fn_nid);
186+
} else if (fn_opt == XN_FLAG_FN_LN) {
187+
fld_len = FN_WIDTH_LN;
188+
objbuf = OBJ_nid2ln(fn_nid);
189+
} else {
190+
fld_len = 0; // XXX: what should this be?
191+
objbuf = "";
192+
}
193+
}
194+
objlen = strlen(objbuf);
195+
if (!maybe_write(out, objbuf, objlen)) {
196+
return -1;
197+
}
198+
if ((objlen < fld_len) && (flags & XN_FLAG_FN_ALIGN)) {
199+
if (!do_indent(out, fld_len - objlen)) {
200+
return -1;
201+
}
202+
outlen += fld_len - objlen;
203+
}
204+
if (!maybe_write(out, sep_eq, sep_eq_len)) {
205+
return -1;
206+
}
207+
outlen += objlen + sep_eq_len;
189208
}
190-
outlen += objlen + sep_eq_len;
191209
// If the field name is unknown then fix up the DER dump flag. We
192210
// might want to limit this further so it will DER dump on anything
193211
// other than a few 'standard' fields.

crypto/x509/x509_test.cc

+39-24
Original file line numberDiff line numberDiff line change
@@ -5262,35 +5262,50 @@ TEST(X509Test, NamePrint) {
52625262
"CN = \"Common "
52635263
"Name/CN=A/CN=B,CN=A,CN=B+CN=A+CN=B;CN=A;CN=B\\0ACN=A\\0A\", "
52645264
"CN = \" spaces \""},
5265-
// Callers can also customize the output, with both |XN_FLAG_*| and
5266-
// |ASN1_STRFLGS_*|. |XN_FLAG_SEP_SPLUS_SPC| uses semicolon separators.
5265+
// |XN_FLAG_MULTILINE| is an OpenSSL-specific multi-line format that tries
5266+
// to vertically align the equal sizes. The vertical alignment doesn't
5267+
// quite handle multi-valued RDNs right and uses a non-RFC-2253 escaping.
52675268
{/*indent=*/0,
5268-
/*flags=*/XN_FLAG_SEP_SPLUS_SPC | ASN1_STRFLGS_RFC2253 |
5269+
/*flags=*/XN_FLAG_MULTILINE,
5270+
"countryName = US\n"
5271+
"stateOrProvinceName = Some State + "
5272+
"stateOrProvinceName = Some Other State \\U2603 + "
5273+
"stateOrProvinceName = Another State \\U2603 + "
5274+
"1.2.840.113554.4.1.72585.2 = \\U2603\n"
5275+
"1.2.840.113554.4.1.72585.3 = 0\\06\\02\\01\\01\\02\\01\\02\n"
5276+
"organizationName = Org Name\n"
5277+
"commonName = Common "
5278+
"Name/CN=A/CN=B,CN=A,CN=B+CN=A+CN=B;CN=A;CN=B\\0ACN=A\\0A\n"
5279+
"commonName = spaces "},
5280+
// The multiline format indents every line.
5281+
{/*indent=*/2,
5282+
/*flags=*/XN_FLAG_MULTILINE,
5283+
" countryName = US\n"
5284+
" stateOrProvinceName = Some State + "
5285+
"stateOrProvinceName = Some Other State \\U2603 + "
5286+
"stateOrProvinceName = Another State \\U2603 + "
5287+
"1.2.840.113554.4.1.72585.2 = \\U2603\n"
5288+
" 1.2.840.113554.4.1.72585.3 = 0\\06\\02\\01\\01\\02\\01\\02\n"
5289+
" organizationName = Org Name\n"
5290+
" commonName = Common "
5291+
"Name/CN=A/CN=B,CN=A,CN=B+CN=A+CN=B;CN=A;CN=B\\0ACN=A\\0A\n"
5292+
" commonName = spaces "},
5293+
// Callers can also customize the output, wuith both |XN_FLAG_*| and
5294+
// |ASN1_STRFLGS_*|. |XN_FLAG_SEP_SPLUS_SPC| uses semicolon separators and
5295+
// |XN_FLAG_FN_OID| forces OIDs.
5296+
{/*indent=*/0,
5297+
/*flags=*/XN_FLAG_SEP_SPLUS_SPC | XN_FLAG_FN_OID | ASN1_STRFLGS_RFC2253 |
52695298
ASN1_STRFLGS_ESC_QUOTE,
5270-
"C=US; "
5271-
"ST=Some State + "
5272-
"ST=Some Other State \\E2\\98\\83 + "
5273-
"ST=Another State \\E2\\98\\83 + "
5299+
"2.5.4.6=US; "
5300+
"2.5.4.8=Some State + "
5301+
"2.5.4.8=Some Other State \\E2\\98\\83 + "
5302+
"2.5.4.8=Another State \\E2\\98\\83 + "
52745303
"1.2.840.113554.4.1.72585.2=\\E2\\98\\83; "
52755304
"1.2.840.113554.4.1.72585.3=#3006020101020102; "
5276-
"O=Org Name; "
5277-
"CN=\"Common "
5305+
"2.5.4.10=Org Name; "
5306+
"2.5.4.3=\"Common "
52785307
"Name/CN=A/CN=B,CN=A,CN=B+CN=A+CN=B;CN=A;CN=B\\0ACN=A\\0A\"; "
5279-
"CN=\" spaces \""},
5280-
// Node uses these parameters.
5281-
{/*indent=*/0,
5282-
/*flags=*/ASN1_STRFLGS_ESC_2253 | ASN1_STRFLGS_ESC_CTRL |
5283-
ASN1_STRFLGS_UTF8_CONVERT | XN_FLAG_SEP_MULTILINE | XN_FLAG_FN_SN,
5284-
"C=US\n"
5285-
"ST=Some State + "
5286-
"ST=Some Other State \xE2\x98\x83 + "
5287-
"ST=Another State \xE2\x98\x83 + "
5288-
"1.2.840.113554.4.1.72585.2=\xE2\x98\x83\n"
5289-
"1.2.840.113554.4.1.72585.3=0\\06\\02\\01\\01\\02\\01\\02\n"
5290-
"O=Org Name\n"
5291-
"CN=Common "
5292-
"Name/CN=A/CN=B\\,CN=A\\,CN=B\\+CN=A\\+CN=B\\;CN=A\\;CN=B\\0ACN=A\\0A\n"
5293-
"CN=\\ spaces\\ "},
5308+
"2.5.4.3=\" spaces \""},
52945309
// |XN_FLAG_COMPAT| matches |X509_NAME_print|, rather than
52955310
// |X509_NAME_print_ex|.
52965311
//

include/openssl/x509.h

+18
Original file line numberDiff line numberDiff line change
@@ -2281,13 +2281,26 @@ OPENSSL_EXPORT int X509_REQ_print_fp(FILE *fp, X509_REQ *req);
22812281
// XN_FLAG_FN_SN uses the attribute type's short name, when available.
22822282
#define XN_FLAG_FN_SN 0
22832283

2284+
// XN_FLAG_FN_LN uses the attribute type's long name, when available.
2285+
#define XN_FLAG_FN_LN (1 << 21)
2286+
2287+
// XN_FLAG_FN_OID always prints attribute types as OIDs.
2288+
#define XN_FLAG_FN_OID (2 << 21)
2289+
2290+
// XN_FLAG_FN_NONE skips printing field names.
2291+
#define XN_FLAG_FN_NONE (3 << 21)
2292+
22842293
// XN_FLAG_SPC_EQ wraps the "=" operator with spaces when printing attributes.
22852294
#define XN_FLAG_SPC_EQ (1 << 23)
22862295

22872296
// XN_FLAG_DUMP_UNKNOWN_FIELDS causes unknown attribute types to be printed in
22882297
// hex, as in RFC 2253.
22892298
#define XN_FLAG_DUMP_UNKNOWN_FIELDS (1 << 24)
22902299

2300+
// XN_FLAG_FN_ALIGN aligns attribute names to 10 characters if using short
2301+
// names, and 25 characters if using long names.
2302+
#define XN_FLAG_FN_ALIGN (1 << 25)
2303+
22912304
// XN_FLAG_RFC2253 prints like RFC 2253.
22922305
#define XN_FLAG_RFC2253 \
22932306
(ASN1_STRFLGS_RFC2253 | XN_FLAG_SEP_COMMA_PLUS | XN_FLAG_DN_REV | \
@@ -2298,6 +2311,11 @@ OPENSSL_EXPORT int X509_REQ_print_fp(FILE *fp, X509_REQ *req);
22982311
(ASN1_STRFLGS_RFC2253 | ASN1_STRFLGS_ESC_QUOTE | XN_FLAG_SEP_CPLUS_SPC | \
22992312
XN_FLAG_SPC_EQ | XN_FLAG_FN_SN)
23002313

2314+
// XN_FLAG_MULTILINE prints a multi-line representation of the name.
2315+
#define XN_FLAG_MULTILINE \
2316+
(ASN1_STRFLGS_ESC_CTRL | ASN1_STRFLGS_ESC_MSB | XN_FLAG_SEP_MULTILINE | \
2317+
XN_FLAG_SPC_EQ | XN_FLAG_FN_LN | XN_FLAG_FN_ALIGN)
2318+
23012319
// X509_NAME_print_ex writes a human-readable representation of |nm| to |out|.
23022320
// Each line of output is indented by |indent| spaces. It returns the number of
23032321
// bytes written on success, and -1 on error. If |out| is NULL, it returns the

0 commit comments

Comments
 (0)