OLD | NEW |
1 /* alg1485.c - implementation of RFCs 1485, 1779 and 2253. | 1 /* alg1485.c - implementation of RFCs 1485, 1779 and 2253. |
2 * | 2 * |
3 * This Source Code Form is subject to the terms of the Mozilla Public | 3 * This Source Code Form is subject to the terms of the Mozilla Public |
4 * License, v. 2.0. If a copy of the MPL was not distributed with this | 4 * License, v. 2.0. If a copy of the MPL was not distributed with this |
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
6 | 6 |
7 #include "prprf.h" | 7 #include "prprf.h" |
8 #include "cert.h" | 8 #include "cert.h" |
9 #include "certi.h" | 9 #include "certi.h" |
10 #include "xconst.h" | 10 #include "xconst.h" |
11 #include "genname.h" | 11 #include "genname.h" |
12 #include "secitem.h" | 12 #include "secitem.h" |
13 #include "secerr.h" | 13 #include "secerr.h" |
14 | 14 |
15 typedef struct NameToKindStr { | 15 typedef struct NameToKindStr { |
16 const char * name; | 16 const char* name; |
17 unsigned int maxLen; /* max bytes in UTF8 encoded string value */ | 17 unsigned int maxLen; /* max bytes in UTF8 encoded string value */ |
18 SECOidTag kind; | 18 SECOidTag kind; |
19 int»» valueType; | 19 int valueType; |
20 } NameToKind; | 20 } NameToKind; |
21 | 21 |
22 /* local type for directory string--could be printable_string or utf8 */ | 22 /* local type for directory string--could be printable_string or utf8 */ |
23 #define SEC_ASN1_DS SEC_ASN1_HIGH_TAG_NUMBER | 23 #define SEC_ASN1_DS SEC_ASN1_HIGH_TAG_NUMBER |
24 | 24 |
| 25 /* clang-format off */ |
| 26 |
25 /* Add new entries to this table, and maybe to function ParseRFC1485AVA */ | 27 /* Add new entries to this table, and maybe to function ParseRFC1485AVA */ |
26 static const NameToKind name2kinds[] = { | 28 static const NameToKind name2kinds[] = { |
27 /* IANA registered type names | 29 /* IANA registered type names |
28 * (See: http://www.iana.org/assignments/ldap-parameters) | 30 * (See: http://www.iana.org/assignments/ldap-parameters) |
29 */ | 31 */ |
30 /* RFC 3280, 4630 MUST SUPPORT */ | 32 /* RFC 3280, 4630 MUST SUPPORT */ |
31 { "CN", 640, SEC_OID_AVA_COMMON_NAME, SEC_ASN1_DS}, | 33 { "CN", 640, SEC_OID_AVA_COMMON_NAME, SEC_ASN1_DS}, |
32 { "ST", 128, SEC_OID_AVA_STATE_OR_PROVINCE, | 34 { "ST", 128, SEC_OID_AVA_STATE_OR_PROVINCE, |
33 » » » » » » » SEC_ASN1_DS}, | 35 SEC_ASN1_DS}, |
34 { "O", 128, SEC_OID_AVA_ORGANIZATION_NAME, | 36 { "O", 128, SEC_OID_AVA_ORGANIZATION_NAME, |
35 » » » » » » » SEC_ASN1_DS}, | 37 SEC_ASN1_DS}, |
36 { "OU", 128, SEC_OID_AVA_ORGANIZATIONAL_UNIT_NAME, | 38 { "OU", 128, SEC_OID_AVA_ORGANIZATIONAL_UNIT_NAME, |
37 SEC_ASN1_DS}, | 39 SEC_ASN1_DS}, |
38 { "dnQualifier", 32767, SEC_OID_AVA_DN_QUALIFIER, SEC_ASN1_PRINTABLE_STRING}
, | 40 { "dnQualifier", 32767, SEC_OID_AVA_DN_QUALIFIER, SEC_ASN1_PRINTABLE_STRING}
, |
39 { "C", 2, SEC_OID_AVA_COUNTRY_NAME, SEC_ASN1_PRINTABLE_STRING}
, | 41 { "C", 2, SEC_OID_AVA_COUNTRY_NAME, SEC_ASN1_PRINTABLE_STRING}
, |
40 { "serialNumber", 64, SEC_OID_AVA_SERIAL_NUMBER,SEC_ASN1_PRINTABLE_STRING}
, | 42 { "serialNumber", 64, SEC_OID_AVA_SERIAL_NUMBER,SEC_ASN1_PRINTABLE_STRING}
, |
41 | 43 |
42 /* RFC 3280, 4630 SHOULD SUPPORT */ | 44 /* RFC 3280, 4630 SHOULD SUPPORT */ |
43 { "L", 128, SEC_OID_AVA_LOCALITY, SEC_ASN1_DS}, | 45 { "L", 128, SEC_OID_AVA_LOCALITY, SEC_ASN1_DS}, |
44 { "title", 64, SEC_OID_AVA_TITLE, SEC_ASN1_DS}, | 46 { "title", 64, SEC_OID_AVA_TITLE, SEC_ASN1_DS}, |
45 { "SN", 64, SEC_OID_AVA_SURNAME, SEC_ASN1_DS}, | 47 { "SN", 64, SEC_OID_AVA_SURNAME, SEC_ASN1_DS}, |
46 { "givenName", 64, SEC_OID_AVA_GIVEN_NAME, SEC_ASN1_DS}, | 48 { "givenName", 64, SEC_OID_AVA_GIVEN_NAME, SEC_ASN1_DS}, |
47 { "initials", 64, SEC_OID_AVA_INITIALS, SEC_ASN1_DS}, | 49 { "initials", 64, SEC_OID_AVA_INITIALS, SEC_ASN1_DS}, |
48 { "generationQualifier", | 50 { "generationQualifier", |
49 64, SEC_OID_AVA_GENERATION_QUALIFIER, | 51 64, SEC_OID_AVA_GENERATION_QUALIFIER, |
50 SEC_ASN1_DS}, | 52 SEC_ASN1_DS}, |
51 /* RFC 3280, 4630 MAY SUPPORT */ | 53 /* RFC 3280, 4630 MAY SUPPORT */ |
52 { "DC", 128, SEC_OID_AVA_DC, SEC_ASN1_IA5_STRING}, | 54 { "DC", 128, SEC_OID_AVA_DC, SEC_ASN1_IA5_STRING}, |
53 { "MAIL", 256, SEC_OID_RFC1274_MAIL, SEC_ASN1_IA5_STRING}, | 55 { "MAIL", 256, SEC_OID_RFC1274_MAIL, SEC_ASN1_IA5_STRING}, |
54 { "UID", 256, SEC_OID_RFC1274_UID, SEC_ASN1_DS}, | 56 { "UID", 256, SEC_OID_RFC1274_UID, SEC_ASN1_DS}, |
55 | 57 |
56 /* ------------------ "strict" boundary --------------------------------- | 58 /* ------------------ "strict" boundary --------------------------------- |
57 * In strict mode, cert_NameToAscii does not encode any of the attributes | 59 * In strict mode, cert_NameToAscii does not encode any of the attributes |
58 * below this line. The first SECOidTag below this line must be used to | 60 * below this line. The first SECOidTag below this line must be used to |
59 * conditionally define the "endKind" in function AppendAVA() below. | 61 * conditionally define the "endKind" in function AppendAVA() below. |
60 * Most new attribute names should be added below this line. | 62 * Most new attribute names should be added below this line. |
61 * Maybe this line should be up higher? Say, after the 3280 MUSTs and | 63 * Maybe this line should be up higher? Say, after the 3280 MUSTs and |
62 * before the 3280 SHOULDs? | 64 * before the 3280 SHOULDs? |
63 */ | 65 */ |
64 | 66 |
65 /* values from draft-ietf-ldapbis-user-schema-05 (not in RFC 3280) */ | 67 /* values from draft-ietf-ldapbis-user-schema-05 (not in RFC 3280) */ |
66 { "postalAddress", 128, SEC_OID_AVA_POSTAL_ADDRESS, SEC_ASN1_DS}, | 68 { "postalAddress", 128, SEC_OID_AVA_POSTAL_ADDRESS, SEC_ASN1_DS}, |
67 { "postalCode", 40, SEC_OID_AVA_POSTAL_CODE, SEC_ASN1_DS}, | 69 { "postalCode", 40, SEC_OID_AVA_POSTAL_CODE, SEC_ASN1_DS}, |
68 { "postOfficeBox", 40, SEC_OID_AVA_POST_OFFICE_BOX,SEC_ASN1_DS}, | 70 { "postOfficeBox", 40, SEC_OID_AVA_POST_OFFICE_BOX,SEC_ASN1_DS}, |
69 { "houseIdentifier",64, SEC_OID_AVA_HOUSE_IDENTIFIER,SEC_ASN1_DS}, | 71 { "houseIdentifier",64, SEC_OID_AVA_HOUSE_IDENTIFIER,SEC_ASN1_DS}, |
70 /* end of IANA registered type names */ | 72 /* end of IANA registered type names */ |
71 | 73 |
72 /* legacy keywords */ | 74 /* legacy keywords */ |
73 { "E", 128, SEC_OID_PKCS9_EMAIL_ADDRESS,SEC_ASN1_IA5_STRING}, | 75 { "E", 128, SEC_OID_PKCS9_EMAIL_ADDRESS,SEC_ASN1_IA5_STRING}, |
74 { "STREET", 128, SEC_OID_AVA_STREET_ADDRESS, SEC_ASN1_DS}, | 76 { "STREET", 128, SEC_OID_AVA_STREET_ADDRESS, SEC_ASN1_DS}, |
75 { "pseudonym", 64, SEC_OID_AVA_PSEUDONYM, SEC_ASN1_DS}, | 77 { "pseudonym", 64, SEC_OID_AVA_PSEUDONYM, SEC_ASN1_DS}, |
76 | 78 |
77 /* values defined by the CAB Forum for EV */ | 79 /* values defined by the CAB Forum for EV */ |
78 { "incorporationLocality", 128, SEC_OID_EV_INCORPORATION_LOCALITY, | 80 { "incorporationLocality", 128, SEC_OID_EV_INCORPORATION_LOCALITY, |
79 SEC_ASN1_DS}, | 81 SEC_ASN1_DS}, |
80 { "incorporationState", 128, SEC_OID_EV_INCORPORATION_STATE, | 82 { "incorporationState", 128, SEC_OID_EV_INCORPORATION_STATE, |
81 SEC_ASN1_DS}, | 83 SEC_ASN1_DS}, |
82 { "incorporationCountry", 2, SEC_OID_EV_INCORPORATION_COUNTRY, | 84 { "incorporationCountry", 2, SEC_OID_EV_INCORPORATION_COUNTRY, |
83 SEC_ASN1_PRINTABLE_STRING}, | 85 SEC_ASN1_PRINTABLE_STRING}, |
84 { "businessCategory", 64, SEC_OID_BUSINESS_CATEGORY, SEC_ASN1_DS}, | 86 { "businessCategory", 64, SEC_OID_BUSINESS_CATEGORY, SEC_ASN1_DS}, |
85 | 87 |
86 /* values defined in X.520 */ | 88 /* values defined in X.520 */ |
87 { "name", 64, SEC_OID_AVA_NAME, SEC_ASN1_DS}, | 89 { "name", 64, SEC_OID_AVA_NAME, SEC_ASN1_DS}, |
88 | 90 |
89 { 0, 256, SEC_OID_UNKNOWN, 0}, | 91 { 0, 256, SEC_OID_UNKNOWN, 0}, |
90 }; | 92 }; |
91 | 93 |
92 /* Table facilitates conversion of ASCII hex to binary. */ | 94 /* Table facilitates conversion of ASCII hex to binary. */ |
93 static const PRInt16 x2b[256] = { | 95 static const PRInt16 x2b[256] = { |
94 /* #0x */ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, | 96 /* #0x */ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
95 /* #1x */ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, | 97 /* #1x */ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
96 /* #2x */ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, | 98 /* #2x */ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
97 /* #3x */ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1, | 99 /* #3x */ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1, |
98 /* #4x */ -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, | 100 /* #4x */ -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
99 /* #5x */ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, | 101 /* #5x */ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
100 /* #6x */ -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, | 102 /* #6x */ -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
101 /* #7x */ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, | 103 /* #7x */ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
102 /* #8x */ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, | 104 /* #8x */ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
103 /* #9x */ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, | 105 /* #9x */ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
104 /* #ax */ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, | 106 /* #ax */ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
105 /* #bx */ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, | 107 /* #bx */ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
106 /* #cx */ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, | 108 /* #cx */ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
107 /* #dx */ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, | 109 /* #dx */ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
108 /* #ex */ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, | 110 /* #ex */ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
109 /* #fx */ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 | 111 /* #fx */ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 |
110 }; | 112 }; |
111 | 113 |
112 #define IS_HEX(c) (x2b[(PRUint8)(c)] >= 0) | 114 #define IS_HEX(c) (x2b[(PRUint8)(c)] >= 0) |
113 | 115 |
114 #define C_DOUBLE_QUOTE '\042' | 116 #define C_DOUBLE_QUOTE '\042' |
115 | 117 |
116 #define C_BACKSLASH '\134' | 118 #define C_BACKSLASH '\134' |
117 | 119 |
118 #define C_EQUAL '=' | 120 #define C_EQUAL '=' |
119 | 121 |
120 #define OPTIONAL_SPACE(c) \ | 122 #define OPTIONAL_SPACE(c) \ |
121 (((c) == ' ') || ((c) == '\r') || ((c) == '\n')) | 123 (((c) == ' ') || ((c) == '\r') || ((c) == '\n')) |
122 | 124 |
123 #define SPECIAL_CHAR(c)»» » » » » \ | 125 #define SPECIAL_CHAR(c) \ |
124 (((c) == ',') || ((c) == '=') || ((c) == C_DOUBLE_QUOTE) ||»\ | 126 (((c) == ',') || ((c) == '=') || ((c) == C_DOUBLE_QUOTE) || \ |
125 ((c) == '\r') || ((c) == '\n') || ((c) == '+') ||» » \ | 127 ((c) == '\r') || ((c) == '\n') || ((c) == '+') || \ |
126 ((c) == '<') || ((c) == '>') || ((c) == '#') ||» » \ | 128 ((c) == '<') || ((c) == '>') || ((c) == '#') || \ |
127 ((c) == ';') || ((c) == C_BACKSLASH)) | 129 ((c) == ';') || ((c) == C_BACKSLASH)) |
128 | 130 |
129 | 131 |
130 #define IS_PRINTABLE(c)»» » » » » \ | 132 #define IS_PRINTABLE(c) \ |
131 ((((c) >= 'a') && ((c) <= 'z')) ||» » » » \ | 133 ((((c) >= 'a') && ((c) <= 'z')) || \ |
132 (((c) >= 'A') && ((c) <= 'Z')) ||» » » » \ | 134 (((c) >= 'A') && ((c) <= 'Z')) || \ |
133 (((c) >= '0') && ((c) <= '9')) ||» » » » \ | 135 (((c) >= '0') && ((c) <= '9')) || \ |
134 ((c) == ' ') ||» » » » » » \ | 136 ((c) == ' ') || \ |
135 ((c) == '\'') ||» » » » » » \ | 137 ((c) == '\'') || \ |
136 ((c) == '\050') ||»» » » /* ( */»» \ | 138 ((c) == '\050') || /* ( */ \ |
137 ((c) == '\051') ||»» » » /* ) */»» \ | 139 ((c) == '\051') || /* ) */ \ |
138 (((c) >= '+') && ((c) <= '/')) ||» » /* + , - . / */»\ | 140 (((c) >= '+') && ((c) <= '/')) || /* + , - . / */ \ |
139 ((c) == ':') ||» » » » » » \ | 141 ((c) == ':') || \ |
140 ((c) == '=') ||» » » » » » \ | 142 ((c) == '=') || \ |
141 ((c) == '?')) | 143 ((c) == '?')) |
142 | 144 |
| 145 /* clang-format on */ |
| 146 |
143 /* RFC 2253 says we must escape ",+\"\\<>;=" EXCEPT inside a quoted string. | 147 /* RFC 2253 says we must escape ",+\"\\<>;=" EXCEPT inside a quoted string. |
144 * Inside a quoted string, we only need to escape " and \ | 148 * Inside a quoted string, we only need to escape " and \ |
145 * We choose to quote strings containing any of those special characters, | 149 * We choose to quote strings containing any of those special characters, |
146 * so we only need to escape " and \ | 150 * so we only need to escape " and \ |
147 */ | 151 */ |
148 #define NEEDS_ESCAPE(c) \ | 152 #define NEEDS_ESCAPE(c) (c == C_DOUBLE_QUOTE || c == C_BACKSLASH) |
149 (c == C_DOUBLE_QUOTE || c == C_BACKSLASH) | 153 |
150 | 154 #define NEEDS_HEX_ESCAPE(c) ((PRUint8)c < 0x20 || c == 0x7f) |
151 #define NEEDS_HEX_ESCAPE(c) \ | |
152 ((PRUint8)c < 0x20 || c == 0x7f) | |
153 | 155 |
154 int | 156 int |
155 cert_AVAOidTagToMaxLen(SECOidTag tag) | 157 cert_AVAOidTagToMaxLen(SECOidTag tag) |
156 { | 158 { |
157 const NameToKind *n2k = name2kinds; | 159 const NameToKind* n2k = name2kinds; |
158 | 160 |
159 while (n2k->kind != tag && n2k->kind != SEC_OID_UNKNOWN) { | 161 while (n2k->kind != tag && n2k->kind != SEC_OID_UNKNOWN) { |
160 » ++n2k; | 162 ++n2k; |
161 } | 163 } |
162 return (n2k->kind != SEC_OID_UNKNOWN) ? n2k->maxLen : -1; | 164 return (n2k->kind != SEC_OID_UNKNOWN) ? n2k->maxLen : -1; |
163 } | 165 } |
164 | 166 |
165 static PRBool | 167 static PRBool |
166 IsPrintable(unsigned char *data, unsigned len) | 168 IsPrintable(unsigned char* data, unsigned len) |
167 { | 169 { |
168 unsigned char ch, *end; | 170 unsigned char ch, *end; |
169 | 171 |
170 end = data + len; | 172 end = data + len; |
171 while (data < end) { | 173 while (data < end) { |
172 » ch = *data++; | 174 ch = *data++; |
173 » if (!IS_PRINTABLE(ch)) { | 175 if (!IS_PRINTABLE(ch)) { |
174 » return PR_FALSE; | 176 return PR_FALSE; |
175 » } | 177 } |
176 } | 178 } |
177 return PR_TRUE; | 179 return PR_TRUE; |
178 } | 180 } |
179 | 181 |
180 static void | 182 static void |
181 skipSpace(const char **pbp, const char *endptr) | 183 skipSpace(const char** pbp, const char* endptr) |
182 { | 184 { |
183 const char *bp = *pbp; | 185 const char* bp = *pbp; |
184 while (bp < endptr && OPTIONAL_SPACE(*bp)) { | 186 while (bp < endptr && OPTIONAL_SPACE(*bp)) { |
185 » bp++; | 187 bp++; |
186 } | 188 } |
187 *pbp = bp; | 189 *pbp = bp; |
188 } | 190 } |
189 | 191 |
190 static SECStatus | 192 static SECStatus |
191 scanTag(const char **pbp, const char *endptr, char *tagBuf, int tagBufSize) | 193 scanTag(const char** pbp, const char* endptr, char* tagBuf, int tagBufSize) |
192 { | 194 { |
193 const char *bp; | 195 const char* bp; |
194 char *tagBufp; | 196 char* tagBufp; |
195 int taglen; | 197 int taglen; |
196 | 198 |
197 PORT_Assert(tagBufSize > 0); | 199 PORT_Assert(tagBufSize > 0); |
198 | 200 |
199 /* skip optional leading space */ | 201 /* skip optional leading space */ |
200 skipSpace(pbp, endptr); | 202 skipSpace(pbp, endptr); |
201 if (*pbp == endptr) { | 203 if (*pbp == endptr) { |
202 » /* nothing left */ | 204 /* nothing left */ |
203 » return SECFailure; | 205 return SECFailure; |
204 } | 206 } |
205 | 207 |
206 /* fill tagBuf */ | 208 /* fill tagBuf */ |
207 taglen = 0; | 209 taglen = 0; |
208 bp = *pbp; | 210 bp = *pbp; |
209 tagBufp = tagBuf; | 211 tagBufp = tagBuf; |
210 while (bp < endptr && !OPTIONAL_SPACE(*bp) && (*bp != C_EQUAL)) { | 212 while (bp < endptr && !OPTIONAL_SPACE(*bp) && (*bp != C_EQUAL)) { |
211 » if (++taglen >= tagBufSize) { | 213 if (++taglen >= tagBufSize) { |
212 » *pbp = bp; | 214 *pbp = bp; |
213 » return SECFailure; | 215 return SECFailure; |
214 » } | 216 } |
215 » *tagBufp++ = *bp++; | 217 *tagBufp++ = *bp++; |
216 } | 218 } |
217 /* null-terminate tagBuf -- guaranteed at least one space left */ | 219 /* null-terminate tagBuf -- guaranteed at least one space left */ |
218 *tagBufp++ = 0; | 220 *tagBufp++ = 0; |
219 *pbp = bp; | 221 *pbp = bp; |
220 | 222 |
221 /* skip trailing spaces till we hit something - should be an equal sign */ | 223 /* skip trailing spaces till we hit something - should be an equal sign */ |
222 skipSpace(pbp, endptr); | 224 skipSpace(pbp, endptr); |
223 if (*pbp == endptr) { | 225 if (*pbp == endptr) { |
224 » /* nothing left */ | 226 /* nothing left */ |
225 » return SECFailure; | 227 return SECFailure; |
226 } | 228 } |
227 if (**pbp != C_EQUAL) { | 229 if (**pbp != C_EQUAL) { |
228 » /* should be an equal sign */ | 230 /* should be an equal sign */ |
229 » return SECFailure; | 231 return SECFailure; |
230 } | 232 } |
231 /* skip over the equal sign */ | 233 /* skip over the equal sign */ |
232 (*pbp)++; | 234 (*pbp)++; |
233 | 235 |
234 return SECSuccess; | 236 return SECSuccess; |
235 } | 237 } |
236 | 238 |
237 /* Returns the number of bytes in the value. 0 means failure. */ | 239 /* Returns the number of bytes in the value. 0 means failure. */ |
238 static int | 240 static int |
239 scanVal(const char **pbp, const char *endptr, char *valBuf, int valBufSize) | 241 scanVal(const char** pbp, const char* endptr, char* valBuf, int valBufSize) |
240 { | 242 { |
241 const char *bp; | 243 const char* bp; |
242 char *valBufp; | 244 char* valBufp; |
243 int vallen = 0; | 245 int vallen = 0; |
244 PRBool isQuoted; | 246 PRBool isQuoted; |
245 | 247 |
246 PORT_Assert(valBufSize > 0); | 248 PORT_Assert(valBufSize > 0); |
247 | 249 |
248 /* skip optional leading space */ | 250 /* skip optional leading space */ |
249 skipSpace(pbp, endptr); | 251 skipSpace(pbp, endptr); |
250 if(*pbp == endptr) { | 252 if (*pbp == endptr) { |
251 » /* nothing left */ | 253 /* nothing left */ |
252 » return 0; | 254 return 0; |
253 } | 255 } |
254 | 256 |
255 bp = *pbp; | 257 bp = *pbp; |
256 | 258 |
257 /* quoted? */ | 259 /* quoted? */ |
258 if (*bp == C_DOUBLE_QUOTE) { | 260 if (*bp == C_DOUBLE_QUOTE) { |
259 » isQuoted = PR_TRUE; | 261 isQuoted = PR_TRUE; |
260 » /* skip over it */ | 262 /* skip over it */ |
261 » bp++; | 263 bp++; |
262 } else { | 264 } else { |
263 » isQuoted = PR_FALSE; | 265 isQuoted = PR_FALSE; |
264 } | 266 } |
265 | 267 |
266 valBufp = valBuf; | 268 valBufp = valBuf; |
267 while (bp < endptr) { | 269 while (bp < endptr) { |
268 » char c = *bp; | 270 char c = *bp; |
269 » if (c == C_BACKSLASH) { | 271 if (c == C_BACKSLASH) { |
270 » /* escape character */ | 272 /* escape character */ |
271 » bp++; | 273 bp++; |
272 » if (bp >= endptr) { | 274 if (bp >= endptr) { |
273 » » /* escape charater must appear with paired char */ | 275 /* escape charater must appear with paired char */ |
274 » » *pbp = bp; | 276 *pbp = bp; |
275 » » return 0; | 277 return 0; |
276 » } | 278 } |
277 » c = *bp; | 279 c = *bp; |
278 » if (IS_HEX(c) && (endptr - bp) >= 2 && IS_HEX(bp[1])) { | 280 if (IS_HEX(c) && (endptr - bp) >= 2 && IS_HEX(bp[1])) { |
279 » » bp++; | 281 bp++; |
280 » » c = (char)((x2b[(PRUint8)c] << 4) | x2b[(PRUint8)*bp]); | 282 c = (char)((x2b[(PRUint8)c] << 4) | x2b[(PRUint8)*bp]); |
281 » } | 283 } |
282 » } else if (c == '#' && bp == *pbp) { | 284 } else if (c == '#' && bp == *pbp) { |
283 » /* ignore leading #, quotation not required for it. */ | 285 /* ignore leading #, quotation not required for it. */ |
284 » } else if (!isQuoted && SPECIAL_CHAR(c)) { | 286 } else if (!isQuoted && SPECIAL_CHAR(c)) { |
285 » /* unescaped special and not within quoted value */ | 287 /* unescaped special and not within quoted value */ |
286 » break; | 288 break; |
287 » } else if (c == C_DOUBLE_QUOTE) { | 289 } else if (c == C_DOUBLE_QUOTE) { |
288 » /* reached unescaped double quote */ | 290 /* reached unescaped double quote */ |
289 » break; | 291 break; |
290 » } | 292 } |
291 » /* append character */ | 293 /* append character */ |
292 vallen++; | 294 vallen++; |
293 » if (vallen >= valBufSize) { | 295 if (vallen >= valBufSize) { |
294 » *pbp = bp; | 296 *pbp = bp; |
295 » return 0; | 297 return 0; |
296 » } | 298 } |
297 » *valBufp++ = c; | 299 *valBufp++ = c; |
298 » bp++; | 300 bp++; |
299 } | 301 } |
300 | 302 |
301 /* strip trailing spaces from unquoted values */ | 303 /* strip trailing spaces from unquoted values */ |
302 if (!isQuoted) { | 304 if (!isQuoted) { |
303 » while (valBufp > valBuf) { | 305 while (valBufp > valBuf) { |
304 » char c = valBufp[-1]; | 306 char c = valBufp[-1]; |
305 » if (! OPTIONAL_SPACE(c)) | 307 if (!OPTIONAL_SPACE(c)) |
306 » break; | 308 break; |
307 » --valBufp; | 309 --valBufp; |
308 » } | 310 } |
309 » vallen = valBufp - valBuf; | 311 vallen = valBufp - valBuf; |
310 } | 312 } |
311 | 313 |
312 if (isQuoted) { | 314 if (isQuoted) { |
313 » /* insist that we stopped on a double quote */ | 315 /* insist that we stopped on a double quote */ |
314 » if (*bp != C_DOUBLE_QUOTE) { | 316 if (*bp != C_DOUBLE_QUOTE) { |
315 » *pbp = bp; | 317 *pbp = bp; |
316 » return 0; | 318 return 0; |
317 » } | 319 } |
318 » /* skip over the quote and skip optional space */ | 320 /* skip over the quote and skip optional space */ |
319 » bp++; | 321 bp++; |
320 » skipSpace(&bp, endptr); | 322 skipSpace(&bp, endptr); |
321 } | 323 } |
322 | 324 |
323 *pbp = bp; | 325 *pbp = bp; |
324 | 326 |
325 /* null-terminate valBuf -- guaranteed at least one space left */ | 327 /* null-terminate valBuf -- guaranteed at least one space left */ |
326 *valBufp = 0; | 328 *valBufp = 0; |
327 | 329 |
328 return vallen; | 330 return vallen; |
329 } | 331 } |
330 | 332 |
331 /* Caller must set error code upon failure */ | 333 /* Caller must set error code upon failure */ |
332 static SECStatus | 334 static SECStatus |
333 hexToBin(PLArenaPool *pool, SECItem * destItem, const char * src, int len) | 335 hexToBin(PLArenaPool* pool, SECItem* destItem, const char* src, int len) |
334 { | 336 { |
335 PRUint8 * dest; | 337 PRUint8* dest; |
336 | 338 |
337 destItem->data = NULL; | 339 destItem->data = NULL; |
338 if (len <= 0 || (len & 1)) { | 340 if (len <= 0 || (len & 1)) { |
339 » goto loser; | 341 goto loser; |
340 } | 342 } |
341 len >>= 1; | 343 len >>= 1; |
342 if (!SECITEM_AllocItem(pool, destItem, len)) | 344 if (!SECITEM_AllocItem(pool, destItem, len)) |
343 » goto loser; | 345 goto loser; |
344 dest = destItem->data; | 346 dest = destItem->data; |
345 for (; len > 0; len--, src += 2) { | 347 for (; len > 0; len--, src += 2) { |
346 » PRInt16 bin = (x2b[(PRUint8)src[0]] << 4) | x2b[(PRUint8)src[1]]; | 348 PRInt16 bin = (x2b[(PRUint8)src[0]] << 4) | x2b[(PRUint8)src[1]]; |
347 » if (bin < 0) | 349 if (bin < 0) |
348 » goto loser; | 350 goto loser; |
349 » *dest++ = (PRUint8)bin; | 351 *dest++ = (PRUint8)bin; |
350 } | 352 } |
351 return SECSuccess; | 353 return SECSuccess; |
352 loser: | 354 loser: |
353 if (!pool) | 355 if (!pool) |
354 » SECITEM_FreeItem(destItem, PR_FALSE); | 356 SECITEM_FreeItem(destItem, PR_FALSE); |
355 return SECFailure; | 357 return SECFailure; |
356 } | 358 } |
357 | 359 |
358 /* Parses one AVA, starting at *pbp. Stops at endptr. | 360 /* Parses one AVA, starting at *pbp. Stops at endptr. |
359 * Advances *pbp past parsed AVA and trailing separator (if present). | 361 * Advances *pbp past parsed AVA and trailing separator (if present). |
360 * On any error, returns NULL and *pbp is undefined. | 362 * On any error, returns NULL and *pbp is undefined. |
361 * On success, returns CERTAVA allocated from arena, and (*pbp)[-1] was | 363 * On success, returns CERTAVA allocated from arena, and (*pbp)[-1] was |
362 * the last character parsed. *pbp is either equal to endptr or | 364 * the last character parsed. *pbp is either equal to endptr or |
363 * points to first character after separator. | 365 * points to first character after separator. |
364 */ | 366 */ |
365 static CERTAVA * | 367 static CERTAVA* |
366 ParseRFC1485AVA(PLArenaPool *arena, const char **pbp, const char *endptr) | 368 ParseRFC1485AVA(PLArenaPool* arena, const char** pbp, const char* endptr) |
367 { | 369 { |
368 CERTAVA *a; | 370 CERTAVA* a; |
369 const NameToKind *n2k; | 371 const NameToKind* n2k; |
370 const char *bp; | 372 const char* bp; |
371 int vt = -1; | 373 int vt = -1; |
372 int valLen; | 374 int valLen; |
373 SECOidTag kind = SEC_OID_UNKNOWN; | 375 SECOidTag kind = SEC_OID_UNKNOWN; |
374 SECStatus rv = SECFailure; | 376 SECStatus rv = SECFailure; |
375 SECItem derOid = { 0, NULL, 0 }; | 377 SECItem derOid = { 0, NULL, 0 }; |
376 SECItem derVal = { 0, NULL, 0}; | 378 SECItem derVal = { 0, NULL, 0 }; |
377 char sep = 0; | 379 char sep = 0; |
378 | 380 |
379 char tagBuf[32]; | 381 char tagBuf[32]; |
380 char valBuf[1024]; | 382 char valBuf[1024]; |
381 | 383 |
382 PORT_Assert(arena); | 384 PORT_Assert(arena); |
383 if (SECSuccess != scanTag(pbp, endptr, tagBuf, sizeof tagBuf) || | 385 if (SECSuccess != scanTag(pbp, endptr, tagBuf, sizeof tagBuf) || |
384 » !(valLen = scanVal(pbp, endptr, valBuf, sizeof valBuf))) { | 386 !(valLen = scanVal(pbp, endptr, valBuf, sizeof valBuf))) { |
385 » goto loser; | 387 goto loser; |
386 } | 388 } |
387 | 389 |
388 bp = *pbp; | 390 bp = *pbp; |
389 if (bp < endptr) { | 391 if (bp < endptr) { |
390 » sep = *bp++; /* skip over separator */ | 392 sep = *bp++; /* skip over separator */ |
391 } | 393 } |
392 *pbp = bp; | 394 *pbp = bp; |
393 /* if we haven't finished, insist that we've stopped on a separator */ | 395 /* if we haven't finished, insist that we've stopped on a separator */ |
394 if (sep && sep != ',' && sep != ';' && sep != '+') { | 396 if (sep && sep != ',' && sep != ';' && sep != '+') { |
395 » goto loser; | 397 goto loser; |
396 } | 398 } |
397 | 399 |
398 /* is this a dotted decimal OID attribute type ? */ | 400 /* is this a dotted decimal OID attribute type ? */ |
399 if (!PL_strncasecmp("oid.", tagBuf, 4)) { | 401 if (!PL_strncasecmp("oid.", tagBuf, 4)) { |
400 rv = SEC_StringToOID(arena, &derOid, tagBuf, strlen(tagBuf)); | 402 rv = SEC_StringToOID(arena, &derOid, tagBuf, strlen(tagBuf)); |
401 } else { | 403 } else { |
402 » for (n2k = name2kinds; n2k->name; n2k++) { | 404 for (n2k = name2kinds; n2k->name; n2k++) { |
403 » SECOidData *oidrec; | 405 SECOidData* oidrec; |
404 » if (PORT_Strcasecmp(n2k->name, tagBuf) == 0) { | 406 if (PORT_Strcasecmp(n2k->name, tagBuf) == 0) { |
405 » » kind = n2k->kind; | 407 kind = n2k->kind; |
406 » » vt = n2k->valueType; | 408 vt = n2k->valueType; |
407 » » oidrec = SECOID_FindOIDByTag(kind); | 409 oidrec = SECOID_FindOIDByTag(kind); |
408 » » if (oidrec == NULL) | 410 if (oidrec == NULL) |
409 » » goto loser; | 411 goto loser; |
410 » » derOid = oidrec->oid; | 412 derOid = oidrec->oid; |
411 » » break; | 413 break; |
412 » } | 414 } |
413 » } | 415 } |
414 } | 416 } |
415 if (kind == SEC_OID_UNKNOWN && rv != SECSuccess) | 417 if (kind == SEC_OID_UNKNOWN && rv != SECSuccess) |
416 » goto loser; | 418 goto loser; |
417 | 419 |
418 /* Is this a hex encoding of a DER attribute value ? */ | 420 /* Is this a hex encoding of a DER attribute value ? */ |
419 if ('#' == valBuf[0]) { | 421 if ('#' == valBuf[0]) { |
420 » /* convert attribute value from hex to binary */ | 422 /* convert attribute value from hex to binary */ |
421 » rv = hexToBin(arena, &derVal, valBuf + 1, valLen - 1); | 423 rv = hexToBin(arena, &derVal, valBuf + 1, valLen - 1); |
422 » if (rv) | 424 if (rv) |
423 » goto loser; | 425 goto loser; |
424 » a = CERT_CreateAVAFromRaw(arena, &derOid, &derVal); | 426 a = CERT_CreateAVAFromRaw(arena, &derOid, &derVal); |
425 } else { | 427 } else { |
426 » if (kind == SEC_OID_UNKNOWN) | 428 if (kind == SEC_OID_UNKNOWN) |
427 » goto loser; | 429 goto loser; |
428 » if (kind == SEC_OID_AVA_COUNTRY_NAME && valLen != 2) | 430 if (kind == SEC_OID_AVA_COUNTRY_NAME && valLen != 2) |
429 » goto loser; | 431 goto loser; |
430 » if (vt == SEC_ASN1_PRINTABLE_STRING && | 432 if (vt == SEC_ASN1_PRINTABLE_STRING && |
431 » !IsPrintable((unsigned char*) valBuf, valLen)) | 433 !IsPrintable((unsigned char*)valBuf, valLen)) |
432 » goto loser; | 434 goto loser; |
433 » if (vt == SEC_ASN1_DS) { | 435 if (vt == SEC_ASN1_DS) { |
434 » /* RFC 4630: choose PrintableString or UTF8String */ | 436 /* RFC 4630: choose PrintableString or UTF8String */ |
435 » if (IsPrintable((unsigned char*) valBuf, valLen)) | 437 if (IsPrintable((unsigned char*)valBuf, valLen)) |
436 » » vt = SEC_ASN1_PRINTABLE_STRING; | 438 vt = SEC_ASN1_PRINTABLE_STRING; |
437 » else | 439 else |
438 » » vt = SEC_ASN1_UTF8_STRING; | 440 vt = SEC_ASN1_UTF8_STRING; |
439 » } | 441 } |
440 | 442 |
441 » derVal.data = (unsigned char*) valBuf; | 443 derVal.data = (unsigned char*)valBuf; |
442 » derVal.len = valLen; | 444 derVal.len = valLen; |
443 » a = CERT_CreateAVAFromSECItem(arena, kind, vt, &derVal); | 445 a = CERT_CreateAVAFromSECItem(arena, kind, vt, &derVal); |
444 } | 446 } |
445 return a; | 447 return a; |
446 | 448 |
447 loser: | 449 loser: |
448 /* matched no kind -- invalid tag */ | 450 /* matched no kind -- invalid tag */ |
449 PORT_SetError(SEC_ERROR_INVALID_AVA); | 451 PORT_SetError(SEC_ERROR_INVALID_AVA); |
450 return 0; | 452 return 0; |
451 } | 453 } |
452 | 454 |
453 static CERTName * | 455 static CERTName* |
454 ParseRFC1485Name(const char *buf, int len) | 456 ParseRFC1485Name(const char* buf, int len) |
455 { | 457 { |
456 SECStatus rv; | 458 SECStatus rv; |
457 CERTName *name; | 459 CERTName* name; |
458 const char *bp, *e; | 460 const char *bp, *e; |
459 CERTAVA *ava; | 461 CERTAVA* ava; |
460 CERTRDN *rdn = NULL; | 462 CERTRDN* rdn = NULL; |
461 | 463 |
462 name = CERT_CreateName(NULL); | 464 name = CERT_CreateName(NULL); |
463 if (name == NULL) { | 465 if (name == NULL) { |
464 » return NULL; | 466 return NULL; |
465 } | 467 } |
466 | 468 |
467 e = buf + len; | 469 e = buf + len; |
468 bp = buf; | 470 bp = buf; |
469 while (bp < e) { | 471 while (bp < e) { |
470 » ava = ParseRFC1485AVA(name->arena, &bp, e); | 472 ava = ParseRFC1485AVA(name->arena, &bp, e); |
471 » if (ava == 0) | 473 if (ava == 0) |
472 » goto loser; | 474 goto loser; |
473 » if (!rdn) { | 475 if (!rdn) { |
474 » rdn = CERT_CreateRDN(name->arena, ava, (CERTAVA *)0); | 476 rdn = CERT_CreateRDN(name->arena, ava, (CERTAVA*)0); |
475 » if (rdn == 0) | 477 if (rdn == 0) |
476 » » goto loser; | 478 goto loser; |
477 » rv = CERT_AddRDN(name, rdn); | 479 rv = CERT_AddRDN(name, rdn); |
478 » } else { | 480 } else { |
479 » rv = CERT_AddAVA(name->arena, rdn, ava); | 481 rv = CERT_AddAVA(name->arena, rdn, ava); |
480 » } | 482 } |
481 » if (rv) | 483 if (rv) |
482 » goto loser; | 484 goto loser; |
483 » if (bp[-1] != '+') | 485 if (bp[-1] != '+') |
484 » rdn = NULL; /* done with this RDN */ | 486 rdn = NULL; /* done with this RDN */ |
485 » skipSpace(&bp, e); | 487 skipSpace(&bp, e); |
486 } | 488 } |
487 | 489 |
488 if (name->rdns[0] == 0) { | 490 if (name->rdns[0] == 0) { |
489 » /* empty name -- illegal */ | 491 /* empty name -- illegal */ |
490 » goto loser; | 492 goto loser; |
491 } | 493 } |
492 | 494 |
493 /* Reverse order of RDNS to comply with RFC */ | 495 /* Reverse order of RDNS to comply with RFC */ |
494 { | 496 { |
495 » CERTRDN **firstRdn; | 497 CERTRDN** firstRdn; |
496 » CERTRDN **lastRdn; | 498 CERTRDN** lastRdn; |
497 » CERTRDN *tmp; | 499 CERTRDN* tmp; |
498 » | 500 |
499 » /* get first one */ | 501 /* get first one */ |
500 » firstRdn = name->rdns; | 502 firstRdn = name->rdns; |
501 » | 503 |
502 » /* find last one */ | 504 /* find last one */ |
503 » lastRdn = name->rdns; | 505 lastRdn = name->rdns; |
504 » while (*lastRdn) lastRdn++; | 506 while (*lastRdn) |
505 » lastRdn--; | 507 lastRdn++; |
506 » | 508 lastRdn--; |
507 » /* reverse list */ | 509 |
508 » for ( ; firstRdn < lastRdn; firstRdn++, lastRdn--) { | 510 /* reverse list */ |
509 » tmp = *firstRdn; | 511 for (; firstRdn < lastRdn; firstRdn++, lastRdn--) { |
510 » *firstRdn = *lastRdn; | 512 tmp = *firstRdn; |
511 » *lastRdn = tmp; | 513 *firstRdn = *lastRdn; |
512 » } | 514 *lastRdn = tmp; |
| 515 } |
513 } | 516 } |
514 | 517 |
515 /* return result */ | 518 /* return result */ |
516 return name; | 519 return name; |
517 | 520 |
518 loser: | 521 loser: |
519 CERT_DestroyName(name); | 522 CERT_DestroyName(name); |
520 return NULL; | 523 return NULL; |
521 } | 524 } |
522 | 525 |
523 CERTName * | 526 CERTName* |
524 CERT_AsciiToName(const char *string) | 527 CERT_AsciiToName(const char* string) |
525 { | 528 { |
526 CERTName *name; | 529 CERTName* name; |
527 name = ParseRFC1485Name(string, PORT_Strlen(string)); | 530 name = ParseRFC1485Name(string, PORT_Strlen(string)); |
528 return name; | 531 return name; |
529 } | 532 } |
530 | 533 |
531 /************************************************************************/ | 534 /************************************************************************/ |
532 | 535 |
533 typedef struct stringBufStr { | 536 typedef struct stringBufStr { |
534 char *buffer; | 537 char* buffer; |
535 unsigned offset; | 538 unsigned offset; |
536 unsigned size; | 539 unsigned size; |
537 } stringBuf; | 540 } stringBuf; |
538 | 541 |
539 #define DEFAULT_BUFFER_SIZE 200 | 542 #define DEFAULT_BUFFER_SIZE 200 |
540 | 543 |
541 static SECStatus | 544 static SECStatus |
542 AppendStr(stringBuf *bufp, char *str) | 545 AppendStr(stringBuf* bufp, char* str) |
543 { | 546 { |
544 char *buf; | 547 char* buf; |
545 unsigned bufLen, bufSize, len; | 548 unsigned bufLen, bufSize, len; |
546 int size = 0; | 549 int size = 0; |
547 | 550 |
548 /* Figure out how much to grow buf by (add in the '\0') */ | 551 /* Figure out how much to grow buf by (add in the '\0') */ |
549 buf = bufp->buffer; | 552 buf = bufp->buffer; |
550 bufLen = bufp->offset; | 553 bufLen = bufp->offset; |
551 len = PORT_Strlen(str); | 554 len = PORT_Strlen(str); |
552 bufSize = bufLen + len; | 555 bufSize = bufLen + len; |
553 if (!buf) { | 556 if (!buf) { |
554 » bufSize++; | 557 bufSize++; |
555 » size = PR_MAX(DEFAULT_BUFFER_SIZE,bufSize*2); | 558 size = PR_MAX(DEFAULT_BUFFER_SIZE, bufSize * 2); |
556 » buf = (char *) PORT_Alloc(size); | 559 buf = (char*)PORT_Alloc(size); |
557 » bufp->size = size; | 560 bufp->size = size; |
558 } else if (bufp->size < bufSize) { | 561 } else if (bufp->size < bufSize) { |
559 » size = bufSize*2; | 562 size = bufSize * 2; |
560 » buf =(char *) PORT_Realloc(buf,size); | 563 buf = (char*)PORT_Realloc(buf, size); |
561 » bufp->size = size; | 564 bufp->size = size; |
562 } | 565 } |
563 if (!buf) { | 566 if (!buf) { |
564 » PORT_SetError(SEC_ERROR_NO_MEMORY); | 567 PORT_SetError(SEC_ERROR_NO_MEMORY); |
565 » return SECFailure; | 568 return SECFailure; |
566 } | 569 } |
567 bufp->buffer = buf; | 570 bufp->buffer = buf; |
568 bufp->offset = bufSize; | 571 bufp->offset = bufSize; |
569 | 572 |
570 /* Concatenate str onto buf */ | 573 /* Concatenate str onto buf */ |
571 buf = buf + bufLen; | 574 buf = buf + bufLen; |
572 if (bufLen) buf--;» » » /* stomp on old '\0' */ | 575 if (bufLen) |
573 PORT_Memcpy(buf, str, len+1);» » /* put in new null */ | 576 buf--; /* stomp on old '\0' */ |
| 577 PORT_Memcpy(buf, str, len + 1); /* put in new null */ |
574 return SECSuccess; | 578 return SECSuccess; |
575 } | 579 } |
576 | 580 |
577 typedef enum { | 581 typedef enum { |
578 minimalEscape = 0,» » /* only hex escapes, and " and \ */ | 582 minimalEscape = 0, /* only hex escapes, and " and \ */ |
579 minimalEscapeAndQuote,» /* as above, plus quoting */ | 583 minimalEscapeAndQuote, /* as above, plus quoting */ |
580 fullEscape /* no quoting, full escaping */ | 584 fullEscape /* no quoting, full escaping */ |
581 } EQMode; | 585 } EQMode; |
582 | 586 |
583 /* Some characters must be escaped as a hex string, e.g. c -> \nn . | 587 /* Some characters must be escaped as a hex string, e.g. c -> \nn . |
584 * Others must be escaped by preceding with a '\', e.g. c -> \c , but | 588 * Others must be escaped by preceding with a '\', e.g. c -> \c , but |
585 * there are certain "special characters" that may be handled by either | 589 * there are certain "special characters" that may be handled by either |
586 * escaping them, or by enclosing the entire attribute value in quotes. | 590 * escaping them, or by enclosing the entire attribute value in quotes. |
587 * A NULL value for pEQMode implies selecting minimalEscape mode. | 591 * A NULL value for pEQMode implies selecting minimalEscape mode. |
588 * Some callers will do quoting when needed, others will not. | 592 * Some callers will do quoting when needed, others will not. |
589 * If a caller selects minimalEscapeAndQuote, and the string does not | 593 * If a caller selects minimalEscapeAndQuote, and the string does not |
590 * need quoting, then this function changes it to minimalEscape. | 594 * need quoting, then this function changes it to minimalEscape. |
591 */ | 595 */ |
592 static int | 596 static int |
593 cert_RFC1485_GetRequiredLen(const char *src, int srclen, EQMode *pEQMode) | 597 cert_RFC1485_GetRequiredLen(const char* src, int srclen, EQMode* pEQMode) |
594 { | 598 { |
595 int i, reqLen=0; | 599 int i, reqLen = 0; |
596 EQMode mode = pEQMode ? *pEQMode : minimalEscape; | 600 EQMode mode = pEQMode ? *pEQMode : minimalEscape; |
597 PRBool needsQuoting = PR_FALSE; | 601 PRBool needsQuoting = PR_FALSE; |
598 char lastC = 0; | 602 char lastC = 0; |
599 | 603 |
600 /* need to make an initial pass to determine if quoting is needed */ | 604 /* need to make an initial pass to determine if quoting is needed */ |
601 for (i = 0; i < srclen; i++) { | 605 for (i = 0; i < srclen; i++) { |
602 » char c = src[i]; | 606 char c = src[i]; |
603 » reqLen++; | 607 reqLen++; |
604 » if (NEEDS_HEX_ESCAPE(c)) { /* c -> \xx */ | 608 if (NEEDS_HEX_ESCAPE(c)) { /* c -> \xx */ |
605 » reqLen += 2; | 609 reqLen += 2; |
606 » } else if (NEEDS_ESCAPE(c)) { /* c -> \c */ | 610 } else if (NEEDS_ESCAPE(c)) { /* c -> \c */ |
607 » reqLen++; | 611 reqLen++; |
608 » } else if (SPECIAL_CHAR(c)) { | 612 } else if (SPECIAL_CHAR(c)) { |
609 » if (mode == minimalEscapeAndQuote) /* quoting is allowed */ | 613 if (mode == minimalEscapeAndQuote) /* quoting is allowed */ |
610 » » needsQuoting = PR_TRUE; /* entirety will need quoting */ | 614 needsQuoting = PR_TRUE; /* entirety will need quoting */ |
611 » else if (mode == fullEscape) | 615 else if (mode == fullEscape) |
612 » » reqLen++; /* MAY escape this character */ | 616 reqLen++; /* MAY escape this character */ |
613 » } else if (OPTIONAL_SPACE(c) && OPTIONAL_SPACE(lastC)) { | 617 } else if (OPTIONAL_SPACE(c) && OPTIONAL_SPACE(lastC)) { |
614 » if (mode == minimalEscapeAndQuote) /* quoting is allowed */ | 618 if (mode == minimalEscapeAndQuote) /* quoting is allowed */ |
615 » » needsQuoting = PR_TRUE; /* entirety will need quoting */ | 619 needsQuoting = PR_TRUE; /* entirety will need quoting */ |
616 » } | 620 } |
617 » lastC = c; | 621 lastC = c; |
618 } | 622 } |
619 /* if it begins or ends in optional space it needs quoting */ | 623 /* if it begins or ends in optional space it needs quoting */ |
620 if (!needsQuoting && srclen > 0 && mode == minimalEscapeAndQuote && | 624 if (!needsQuoting && srclen > 0 && mode == minimalEscapeAndQuote && |
621 » (OPTIONAL_SPACE(src[srclen-1]) || OPTIONAL_SPACE(src[0]))) { | 625 (OPTIONAL_SPACE(src[srclen - 1]) || OPTIONAL_SPACE(src[0]))) { |
622 » needsQuoting = PR_TRUE; | 626 needsQuoting = PR_TRUE; |
623 } | 627 } |
624 | 628 |
625 if (needsQuoting) | 629 if (needsQuoting) |
626 » reqLen += 2; | 630 reqLen += 2; |
627 if (pEQMode && mode == minimalEscapeAndQuote && !needsQuoting) | 631 if (pEQMode && mode == minimalEscapeAndQuote && !needsQuoting) |
628 » *pEQMode = minimalEscape; | 632 *pEQMode = minimalEscape; |
629 return reqLen; | 633 return reqLen; |
630 } | 634 } |
631 | 635 |
632 static const char hexChars[16] = { "0123456789abcdef" }; | 636 static const char hexChars[16] = { "0123456789abcdef" }; |
633 | 637 |
634 static SECStatus | 638 static SECStatus |
635 escapeAndQuote(char *dst, int dstlen, char *src, int srclen, EQMode *pEQMode) | 639 escapeAndQuote(char* dst, int dstlen, char* src, int srclen, EQMode* pEQMode) |
636 { | 640 { |
637 int i, reqLen=0; | 641 int i, reqLen = 0; |
638 EQMode mode = pEQMode ? *pEQMode : minimalEscape; | 642 EQMode mode = pEQMode ? *pEQMode : minimalEscape; |
639 | 643 |
640 /* space for terminal null */ | 644 /* space for terminal null */ |
641 reqLen = cert_RFC1485_GetRequiredLen(src, srclen, &mode) + 1; | 645 reqLen = cert_RFC1485_GetRequiredLen(src, srclen, &mode) + 1; |
642 if (reqLen > dstlen) { | 646 if (reqLen > dstlen) { |
643 » PORT_SetError(SEC_ERROR_OUTPUT_LEN); | 647 PORT_SetError(SEC_ERROR_OUTPUT_LEN); |
644 » return SECFailure; | 648 return SECFailure; |
645 } | 649 } |
646 | 650 |
647 if (mode == minimalEscapeAndQuote) | 651 if (mode == minimalEscapeAndQuote) |
648 *dst++ = C_DOUBLE_QUOTE; | 652 *dst++ = C_DOUBLE_QUOTE; |
649 for (i = 0; i < srclen; i++) { | 653 for (i = 0; i < srclen; i++) { |
650 » char c = src[i]; | 654 char c = src[i]; |
651 » if (NEEDS_HEX_ESCAPE(c)) { | 655 if (NEEDS_HEX_ESCAPE(c)) { |
652 » *dst++ = C_BACKSLASH; | 656 *dst++ = C_BACKSLASH; |
653 » *dst++ = hexChars[ (c >> 4) & 0x0f ]; | 657 *dst++ = hexChars[(c >> 4) & 0x0f]; |
654 » *dst++ = hexChars[ c & 0x0f ]; | 658 *dst++ = hexChars[c & 0x0f]; |
655 » } else { | 659 } else { |
656 » if (NEEDS_ESCAPE(c) || (SPECIAL_CHAR(c) && mode == fullEscape)) { | 660 if (NEEDS_ESCAPE(c) || (SPECIAL_CHAR(c) && mode == fullEscape)) { |
657 » » *dst++ = C_BACKSLASH; | 661 *dst++ = C_BACKSLASH; |
658 » } | 662 } |
659 » *dst++ = c; | 663 *dst++ = c; |
660 » } | 664 } |
661 } | 665 } |
662 if (mode == minimalEscapeAndQuote) | 666 if (mode == minimalEscapeAndQuote) |
663 » *dst++ = C_DOUBLE_QUOTE; | 667 *dst++ = C_DOUBLE_QUOTE; |
664 *dst++ = 0; | 668 *dst++ = 0; |
665 if (pEQMode) | 669 if (pEQMode) |
666 » *pEQMode = mode; | 670 *pEQMode = mode; |
667 return SECSuccess; | 671 return SECSuccess; |
668 } | 672 } |
669 | 673 |
670 SECStatus | 674 SECStatus |
671 CERT_RFC1485_EscapeAndQuote(char *dst, int dstlen, char *src, int srclen) | 675 CERT_RFC1485_EscapeAndQuote(char* dst, int dstlen, char* src, int srclen) |
672 { | 676 { |
673 EQMode mode = minimalEscapeAndQuote; | 677 EQMode mode = minimalEscapeAndQuote; |
674 return escapeAndQuote(dst, dstlen, src, srclen, &mode); | 678 return escapeAndQuote(dst, dstlen, src, srclen, &mode); |
675 } | 679 } |
676 | 680 |
677 | |
678 /* convert an OID to dotted-decimal representation */ | 681 /* convert an OID to dotted-decimal representation */ |
679 /* Returns a string that must be freed with PR_smprintf_free(), */ | 682 /* Returns a string that must be freed with PR_smprintf_free(), */ |
680 char * | 683 char* |
681 CERT_GetOidString(const SECItem *oid) | 684 CERT_GetOidString(const SECItem* oid) |
682 { | 685 { |
683 PRUint8 *stop; /* points to first byte after OID string */ | 686 PRUint8* stop; /* points to first byte after OID string */ |
684 PRUint8 *first; /* byte of an OID component integer */ | 687 PRUint8* first; /* byte of an OID component integer */ |
685 PRUint8 *last; /* byte of an OID component integer */ | 688 PRUint8* last; /* byte of an OID component integer */ |
686 char *rvString = NULL; | 689 char* rvString = NULL; |
687 char *prefix = NULL; | 690 char* prefix = NULL; |
688 | 691 |
689 #define MAX_OID_LEN 1024 /* bytes */ | 692 #define MAX_OID_LEN 1024 /* bytes */ |
690 | 693 |
691 if (oid->len > MAX_OID_LEN) { | 694 if (oid->len > MAX_OID_LEN) { |
692 » PORT_SetError(SEC_ERROR_INPUT_LEN); | 695 PORT_SetError(SEC_ERROR_INPUT_LEN); |
693 » return NULL; | 696 return NULL; |
694 } | 697 } |
695 | 698 |
696 /* first will point to the next sequence of bytes to decode */ | 699 /* first will point to the next sequence of bytes to decode */ |
697 first = (PRUint8 *)oid->data; | 700 first = (PRUint8*)oid->data; |
698 /* stop points to one past the legitimate data */ | 701 /* stop points to one past the legitimate data */ |
699 stop = &first[ oid->len ]; | 702 stop = &first[oid->len]; |
700 | 703 |
701 /* | 704 /* |
702 * Check for our pseudo-encoded single-digit OIDs | 705 * Check for our pseudo-encoded single-digit OIDs |
703 */ | 706 */ |
704 if ((*first == 0x80) && (2 == oid->len)) { | 707 if ((*first == 0x80) && (2 == oid->len)) { |
705 » /* Funky encoding. The second byte is the number */ | 708 /* Funky encoding. The second byte is the number */ |
706 » rvString = PR_smprintf("%lu", (PRUint32)first[1]); | 709 rvString = PR_smprintf("%lu", (PRUint32)first[1]); |
707 » if (!rvString) { | 710 if (!rvString) { |
708 » PORT_SetError(SEC_ERROR_NO_MEMORY); | 711 PORT_SetError(SEC_ERROR_NO_MEMORY); |
709 » } | 712 } |
710 » return rvString; | 713 return rvString; |
711 } | 714 } |
712 | 715 |
713 for (; first < stop; first = last + 1) { | 716 for (; first < stop; first = last + 1) { |
714 » unsigned int bytesBeforeLast; | 717 unsigned int bytesBeforeLast; |
715 | 718 |
716 » for (last = first; last < stop; last++) { | 719 for (last = first; last < stop; last++) { |
717 » if (0 == (*last & 0x80)) { | 720 if (0 == (*last & 0x80)) { |
718 » » break; | 721 break; |
719 » } | 722 } |
720 » } | 723 } |
721 » bytesBeforeLast = (unsigned int)(last - first); | 724 bytesBeforeLast = (unsigned int)(last - first); |
722 » if (bytesBeforeLast <= 3U) { /* 0-28 bit number */ | 725 if (bytesBeforeLast <= 3U) { /* 0-28 bit number */ |
723 » PRUint32 n = 0; | 726 PRUint32 n = 0; |
724 » PRUint32 c; | 727 PRUint32 c; |
725 | 728 |
726 #define CGET(i, m) \ | 729 #define CGET(i, m) \ |
727 » » c = last[-i] & m; \ | 730 c = last[-i] & m; \ |
728 » » n |= c << (7 * i) | 731 n |= c << (7 * i) |
729 | 732 |
730 #define CASE(i, m) \ | 733 #define CASE(i, m) \ |
731 » case i: \ | 734 case i: \ |
732 » » CGET(i, m); \ | 735 CGET(i, m); \ |
733 » » if (!n) goto unsupported \ | 736 if (!n) \ |
734 » » /* fall-through */ | 737 goto unsupported /* fall-through */ |
735 | 738 |
736 » switch (bytesBeforeLast) { | 739 switch (bytesBeforeLast) { |
737 » CASE(3, 0x7f); | 740 CASE(3, 0x7f); |
738 » CASE(2, 0x7f); | 741 CASE(2, 0x7f); |
739 » CASE(1, 0x7f); | 742 CASE(1, 0x7f); |
740 » case 0: n |= last[0] & 0x7f; | 743 case 0: |
741 » » break; | 744 n |= |
742 » } | 745 last[0] & 0x7f; |
743 » if (last[0] & 0x80) | 746 break; |
744 » » goto unsupported; | 747 } |
745 | 748 if (last[0] & 0x80) |
746 » if (!rvString) { | 749 goto unsupported; |
747 » » /* This is the first number.. decompose it */ | 750 |
748 » » PRUint32 one = PR_MIN(n/40, 2); /* never > 2 */ | 751 if (!rvString) { |
749 » » PRUint32 two = n - (one * 40); | 752 /* This is the first number.. decompose it */ |
750 | 753 PRUint32 one = PR_MIN(n / 40, 2); /* never > 2 */ |
751 » » rvString = PR_smprintf("OID.%lu.%lu", one, two); | 754 PRUint32 two = n - (one * 40); |
752 » } else { | 755 |
753 » » prefix = rvString; | 756 rvString = PR_smprintf("OID.%lu.%lu", one, two); |
754 » » rvString = PR_smprintf("%s.%lu", prefix, n); | 757 } else { |
755 » } | 758 prefix = rvString; |
756 » } else if (bytesBeforeLast <= 9U) { /* 29-64 bit number */ | 759 rvString = PR_smprintf("%s.%lu", prefix, n); |
757 » PRUint64 n = 0; | 760 } |
758 » PRUint64 c; | 761 } else if (bytesBeforeLast <= 9U) { /* 29-64 bit number */ |
759 | 762 PRUint64 n = 0; |
760 » switch (bytesBeforeLast) { | 763 PRUint64 c; |
761 » CASE(9, 0x01); | 764 |
762 » CASE(8, 0x7f); | 765 switch (bytesBeforeLast) { |
763 » CASE(7, 0x7f); | 766 CASE(9, 0x01); |
764 » CASE(6, 0x7f); | 767 CASE(8, 0x7f); |
765 » CASE(5, 0x7f); | 768 CASE(7, 0x7f); |
766 » CASE(4, 0x7f); | 769 CASE(6, 0x7f); |
767 » CGET(3, 0x7f); | 770 CASE(5, 0x7f); |
768 » CGET(2, 0x7f); | 771 CASE(4, 0x7f); |
769 » CGET(1, 0x7f); | 772 CGET(3, 0x7f); |
770 » CGET(0, 0x7f); | 773 CGET(2, 0x7f); |
771 » » break; | 774 CGET(1, 0x7f); |
772 » } | 775 CGET(0, 0x7f); |
773 » if (last[0] & 0x80) | 776 break; |
774 » » goto unsupported; | 777 } |
775 | 778 if (last[0] & 0x80) |
776 » if (!rvString) { | 779 goto unsupported; |
777 » » /* This is the first number.. decompose it */ | 780 |
778 » » PRUint64 one = PR_MIN(n/40, 2); /* never > 2 */ | 781 if (!rvString) { |
779 » » PRUint64 two = n - (one * 40); | 782 /* This is the first number.. decompose it */ |
780 | 783 PRUint64 one = PR_MIN(n / 40, 2); /* never > 2 */ |
781 » » rvString = PR_smprintf("OID.%llu.%llu", one, two); | 784 PRUint64 two = n - (one * 40); |
782 » } else { | 785 |
783 » » prefix = rvString; | 786 rvString = PR_smprintf("OID.%llu.%llu", one, two); |
784 » » rvString = PR_smprintf("%s.%llu", prefix, n); | 787 } else { |
785 » } | 788 prefix = rvString; |
786 » } else { | 789 rvString = PR_smprintf("%s.%llu", prefix, n); |
787 » /* More than a 64-bit number, or not minimal encoding. */ | 790 } |
788 unsupported: | 791 } else { |
789 » if (!rvString) | 792 /* More than a 64-bit number, or not minimal encoding. */ |
790 » » rvString = PR_smprintf("OID.UNSUPPORTED"); | 793 unsupported: |
791 » else { | 794 if (!rvString) |
792 » » prefix = rvString; | 795 rvString = PR_smprintf("OID.UNSUPPORTED"); |
793 » » rvString = PR_smprintf("%s.UNSUPPORTED", prefix); | 796 else { |
794 » } | 797 prefix = rvString; |
795 » } | 798 rvString = PR_smprintf("%s.UNSUPPORTED", prefix); |
796 | 799 } |
797 » if (prefix) { | 800 } |
798 » PR_smprintf_free(prefix); | 801 |
799 » prefix = NULL; | 802 if (prefix) { |
800 » } | 803 PR_smprintf_free(prefix); |
801 » if (!rvString) { | 804 prefix = NULL; |
802 » PORT_SetError(SEC_ERROR_NO_MEMORY); | 805 } |
803 » break; | 806 if (!rvString) { |
804 » } | 807 PORT_SetError(SEC_ERROR_NO_MEMORY); |
| 808 break; |
| 809 } |
805 } | 810 } |
806 return rvString; | 811 return rvString; |
807 } | 812 } |
808 | 813 |
809 /* convert DER-encoded hex to a string */ | 814 /* convert DER-encoded hex to a string */ |
810 static SECItem * | 815 static SECItem* |
811 get_hex_string(SECItem *data) | 816 get_hex_string(SECItem* data) |
812 { | 817 { |
813 SECItem *rv; | 818 SECItem* rv; |
814 unsigned int i, j; | 819 unsigned int i, j; |
815 static const char hex[] = { "0123456789ABCDEF" }; | 820 static const char hex[] = { "0123456789ABCDEF" }; |
816 | 821 |
817 /* '#' + 2 chars per octet + terminator */ | 822 /* '#' + 2 chars per octet + terminator */ |
818 rv = SECITEM_AllocItem(NULL, NULL, data->len*2 + 2); | 823 rv = SECITEM_AllocItem(NULL, NULL, data->len * 2 + 2); |
819 if (!rv) { | 824 if (!rv) { |
820 » return NULL; | 825 return NULL; |
821 } | 826 } |
822 rv->data[0] = '#'; | 827 rv->data[0] = '#'; |
823 rv->len = 1 + 2 * data->len; | 828 rv->len = 1 + 2 * data->len; |
824 for (i=0; i<data->len; i++) { | 829 for (i = 0; i < data->len; i++) { |
825 » j = data->data[i]; | 830 j = data->data[i]; |
826 » rv->data[2*i+1] = hex[j >> 4]; | 831 rv->data[2 * i + 1] = hex[j >> 4]; |
827 » rv->data[2*i+2] = hex[j & 15]; | 832 rv->data[2 * i + 2] = hex[j & 15]; |
828 } | 833 } |
829 rv->data[rv->len] = 0; | 834 rv->data[rv->len] = 0; |
830 return rv; | 835 return rv; |
831 } | 836 } |
832 | 837 |
833 /* For compliance with RFC 2253, RFC 3280 and RFC 4630, we choose to | 838 /* For compliance with RFC 2253, RFC 3280 and RFC 4630, we choose to |
834 * use the NAME=STRING form, rather than the OID.N.N=#hexXXXX form, | 839 * use the NAME=STRING form, rather than the OID.N.N=#hexXXXX form, |
835 * when both of these conditions are met: | 840 * when both of these conditions are met: |
836 * 1) The attribute name OID (kind) has a known name string that is | 841 * 1) The attribute name OID (kind) has a known name string that is |
837 * defined in one of those RFCs, or in RFCs that they cite, AND | 842 * defined in one of those RFCs, or in RFCs that they cite, AND |
838 * 2) The attribute's value encoding is RFC compliant for the kind | 843 * 2) The attribute's value encoding is RFC compliant for the kind |
839 * (e.g., the value's encoding tag is correct for the kind, and | 844 * (e.g., the value's encoding tag is correct for the kind, and |
840 * the value's length is in the range allowed for the kind, and | 845 * the value's length is in the range allowed for the kind, and |
841 * the value's contents are appropriate for the encoding tag). | 846 * the value's contents are appropriate for the encoding tag). |
842 * Otherwise, we use the OID.N.N=#hexXXXX form. | 847 * Otherwise, we use the OID.N.N=#hexXXXX form. |
843 * | 848 * |
844 * If the caller prefers maximum human readability to RFC compliance, | 849 * If the caller prefers maximum human readability to RFC compliance, |
845 * then | 850 * then |
846 * - We print the kind in NAME= string form if we know the name | 851 * - We print the kind in NAME= string form if we know the name |
847 * string for the attribute type OID, regardless of whether the | 852 * string for the attribute type OID, regardless of whether the |
848 * value is correctly encoded or not. else we use the OID.N.N= form. | 853 * value is correctly encoded or not. else we use the OID.N.N= form. |
849 * - We use the non-hex STRING form for the attribute value if the | 854 * - We use the non-hex STRING form for the attribute value if the |
850 * value can be represented in such a form. Otherwise, we use | 855 * value can be represented in such a form. Otherwise, we use |
851 * the hex string form. | 856 * the hex string form. |
852 * This implies that, for maximum human readability, in addition to | 857 * This implies that, for maximum human readability, in addition to |
853 * the two forms allowed by the RFC, we allow two other forms of output: | 858 * the two forms allowed by the RFC, we allow two other forms of output: |
854 * - the OID.N.N=STRING form, and | 859 * - the OID.N.N=STRING form, and |
855 * - the NAME=#hexXXXX form | 860 * - the NAME=#hexXXXX form |
856 * When the caller prefers maximum human readability, we do not allow | 861 * When the caller prefers maximum human readability, we do not allow |
857 * the value of any attribute to exceed the length allowed by the RFC. | 862 * the value of any attribute to exceed the length allowed by the RFC. |
858 * If the attribute value exceeds the allowed length, we truncate it to | 863 * If the attribute value exceeds the allowed length, we truncate it to |
859 * the allowed length and append "...". | 864 * the allowed length and append "...". |
860 * Also in this case, we arbitrarily impose a limit on the length of the | 865 * Also in this case, we arbitrarily impose a limit on the length of the |
861 * entire AVA encoding, regardless of the form, of 384 bytes per AVA. | 866 * entire AVA encoding, regardless of the form, of 384 bytes per AVA. |
862 * This limit includes the trailing NULL character. If the encoded | 867 * This limit includes the trailing NULL character. If the encoded |
863 * AVA length exceeds that limit, this function reports failure to encode | 868 * AVA length exceeds that limit, this function reports failure to encode |
864 * the AVA. | 869 * the AVA. |
865 * | 870 * |
866 * An ASCII representation of an AVA is said to be "invertible" if | 871 * An ASCII representation of an AVA is said to be "invertible" if |
867 * conversion back to DER reproduces the original DER encoding exactly. | 872 * conversion back to DER reproduces the original DER encoding exactly. |
868 * The RFC 2253 rules do not ensure that all ASCII AVAs derived according | 873 * The RFC 2253 rules do not ensure that all ASCII AVAs derived according |
869 * to its rules are invertible. That is because the RFCs allow some | 874 * to its rules are invertible. That is because the RFCs allow some |
870 * attribute values to be encoded in any of a number of encodings, | 875 * attribute values to be encoded in any of a number of encodings, |
871 * and the encoding type information is lost in the non-hex STRING form. | 876 * and the encoding type information is lost in the non-hex STRING form. |
872 * This is particularly true of attributes of type DirectoryString. | 877 * This is particularly true of attributes of type DirectoryString. |
873 * The encoding type information is always preserved in the hex string | 878 * The encoding type information is always preserved in the hex string |
874 * form, because the hex includes the entire DER encoding of the value. | 879 * form, because the hex includes the entire DER encoding of the value. |
875 * | 880 * |
876 * So, when the caller perfers maximum invertibility, we apply the | 881 * So, when the caller perfers maximum invertibility, we apply the |
877 * RFC compliance rules stated above, and add a third required | 882 * RFC compliance rules stated above, and add a third required |
878 * condition on the use of the NAME=STRING form. | 883 * condition on the use of the NAME=STRING form. |
879 * 3) The attribute's kind is not is allowed to be encoded in any of | 884 * 3) The attribute's kind is not is allowed to be encoded in any of |
880 * several different encodings, such as DirectoryStrings. | 885 * several different encodings, such as DirectoryStrings. |
881 * | 886 * |
882 * The chief difference between CERT_N2A_STRICT and CERT_N2A_INVERTIBLE | 887 * The chief difference between CERT_N2A_STRICT and CERT_N2A_INVERTIBLE |
883 * is that the latter forces DirectoryStrings to be hex encoded. | 888 * is that the latter forces DirectoryStrings to be hex encoded. |
884 * | 889 * |
885 * As a simplification, we assume the value is correctly encoded for | 890 * As a simplification, we assume the value is correctly encoded for |
886 * its encoding type. That is, we do not test that all the characters | 891 * its encoding type. That is, we do not test that all the characters |
887 * in a string encoded type are allowed by that type. We assume it. | 892 * in a string encoded type are allowed by that type. We assume it. |
888 */ | 893 */ |
889 static SECStatus | 894 static SECStatus |
890 AppendAVA(stringBuf *bufp, CERTAVA *ava, CertStrictnessLevel strict) | 895 AppendAVA(stringBuf* bufp, CERTAVA* ava, CertStrictnessLevel strict) |
891 { | 896 { |
892 #define TMPBUF_LEN 2048 | 897 #define TMPBUF_LEN 2048 |
893 const NameToKind *pn2k = name2kinds; | 898 const NameToKind* pn2k = name2kinds; |
894 SECItem *avaValue = NULL; | 899 SECItem* avaValue = NULL; |
895 char *unknownTag = NULL; | 900 char* unknownTag = NULL; |
896 char *encodedAVA = NULL; | 901 char* encodedAVA = NULL; |
897 PRBool useHex = PR_FALSE; /* use =#hexXXXX form */ | 902 PRBool useHex = PR_FALSE; /* use =#hexXXXX form */ |
898 PRBool truncateName = PR_FALSE; | 903 PRBool truncateName = PR_FALSE; |
899 PRBool truncateValue = PR_FALSE; | 904 PRBool truncateValue = PR_FALSE; |
900 SECOidTag endKind; | 905 SECOidTag endKind; |
901 SECStatus rv; | 906 SECStatus rv; |
902 unsigned int len; | 907 unsigned int len; |
903 unsigned int nameLen, valueLen; | 908 unsigned int nameLen, valueLen; |
904 unsigned int maxName, maxValue; | 909 unsigned int maxName, maxValue; |
905 EQMode mode = minimalEscapeAndQuote; | 910 EQMode mode = minimalEscapeAndQuote; |
906 NameToKind n2k = { NULL, 32767, SEC_OID_UNKNOWN, SEC_ASN1_DS }; | 911 NameToKind n2k = { NULL, 32767, SEC_OID_UNKNOWN, SEC_ASN1_DS }; |
907 char tmpBuf[TMPBUF_LEN]; | 912 char tmpBuf[TMPBUF_LEN]; |
908 | 913 |
909 #define tagName n2k.name /* non-NULL means use NAME= form */ | 914 #define tagName n2k.name /* non-NULL means use NAME= form */ |
910 #define maxBytes n2k.maxLen | 915 #define maxBytes n2k.maxLen |
911 #define tag n2k.kind | 916 #define tag n2k.kind |
912 #define vt n2k.valueType | 917 #define vt n2k.valueType |
913 | 918 |
914 /* READABLE mode recognizes more names from the name2kinds table | 919 /* READABLE mode recognizes more names from the name2kinds table |
915 * than do STRICT or INVERTIBLE modes. This assignment chooses the | 920 * than do STRICT or INVERTIBLE modes. This assignment chooses the |
916 * point in the table where the attribute type name scanning stops. | 921 * point in the table where the attribute type name scanning stops. |
917 */ | 922 */ |
918 endKind = (strict == CERT_N2A_READABLE) ? SEC_OID_UNKNOWN | 923 endKind = (strict == CERT_N2A_READABLE) ? SEC_OID_UNKNOWN |
919 : SEC_OID_AVA_POSTAL_ADDRESS; | 924 : SEC_OID_AVA_POSTAL_ADDRESS; |
920 tag = CERT_GetAVATag(ava); | 925 tag = CERT_GetAVATag(ava); |
921 while (pn2k->kind != tag && pn2k->kind != endKind) { | 926 while (pn2k->kind != tag && pn2k->kind != endKind) { |
922 ++pn2k; | 927 ++pn2k; |
923 } | 928 } |
924 | 929 |
925 if (pn2k->kind != endKind ) { | 930 if (pn2k->kind != endKind) { |
926 n2k = *pn2k; | 931 n2k = *pn2k; |
927 } else if (strict != CERT_N2A_READABLE) { | 932 } else if (strict != CERT_N2A_READABLE) { |
928 useHex = PR_TRUE; | 933 useHex = PR_TRUE; |
929 } | 934 } |
930 /* For invertable form, force Directory Strings to use hex form. */ | 935 /* For invertable form, force Directory Strings to use hex form. */ |
931 if (strict == CERT_N2A_INVERTIBLE && vt == SEC_ASN1_DS) { | 936 if (strict == CERT_N2A_INVERTIBLE && vt == SEC_ASN1_DS) { |
932 » tagName = NULL; /* must use OID.N form */ | 937 tagName = NULL; /* must use OID.N form */ |
933 » useHex = PR_TRUE; /* must use hex string */ | 938 useHex = PR_TRUE; /* must use hex string */ |
934 } | 939 } |
935 if (!useHex) { | 940 if (!useHex) { |
936 » avaValue = CERT_DecodeAVAValue(&ava->value); | 941 avaValue = CERT_DecodeAVAValue(&ava->value); |
937 » if (!avaValue) { | 942 if (!avaValue) { |
938 » useHex = PR_TRUE; | 943 useHex = PR_TRUE; |
939 » if (strict != CERT_N2A_READABLE) { | 944 if (strict != CERT_N2A_READABLE) { |
940 » » tagName = NULL; /* must use OID.N form */ | 945 tagName = NULL; /* must use OID.N form */ |
941 » } | 946 } |
942 » } | 947 } |
943 } | 948 } |
944 if (!tagName) { | 949 if (!tagName) { |
945 » /* handle unknown attribute types per RFC 2253 */ | 950 /* handle unknown attribute types per RFC 2253 */ |
946 » tagName = unknownTag = CERT_GetOidString(&ava->type); | 951 tagName = unknownTag = CERT_GetOidString(&ava->type); |
947 » if (!tagName) { | 952 if (!tagName) { |
948 » if (avaValue) | 953 if (avaValue) |
949 » » SECITEM_FreeItem(avaValue, PR_TRUE); | 954 SECITEM_FreeItem(avaValue, PR_TRUE); |
950 » return SECFailure; | 955 return SECFailure; |
951 » } | 956 } |
952 } | 957 } |
953 if (useHex) { | 958 if (useHex) { |
954 » avaValue = get_hex_string(&ava->value); | 959 avaValue = get_hex_string(&ava->value); |
955 » if (!avaValue) { | 960 if (!avaValue) { |
956 » if (unknownTag) | 961 if (unknownTag) |
957 » » PR_smprintf_free(unknownTag); | 962 PR_smprintf_free(unknownTag); |
958 » return SECFailure; | 963 return SECFailure; |
959 » } | 964 } |
960 } | 965 } |
961 | 966 |
962 nameLen = strlen(tagName); | 967 nameLen = strlen(tagName); |
963 valueLen = (useHex ? avaValue->len : | 968 valueLen = |
964 » » cert_RFC1485_GetRequiredLen((char *)avaValue->data, avaValue->le
n, | 969 (useHex ? avaValue->len : cert_RFC1485_GetRequiredLen( |
965 » » » » » &mode)); | 970 (char*)avaValue->data, avaValue->len, &mod
e)); |
966 len = nameLen + valueLen + 2; /* Add 2 for '=' and trailing NUL */ | 971 len = nameLen + valueLen + 2; /* Add 2 for '=' and trailing NUL */ |
967 | 972 |
968 maxName = nameLen; | 973 maxName = nameLen; |
969 maxValue = valueLen; | 974 maxValue = valueLen; |
970 if (len <= sizeof(tmpBuf)) { | 975 if (len <= sizeof(tmpBuf)) { |
971 » encodedAVA = tmpBuf; | 976 encodedAVA = tmpBuf; |
972 } else if (strict != CERT_N2A_READABLE) { | 977 } else if (strict != CERT_N2A_READABLE) { |
973 » encodedAVA = PORT_Alloc(len); | 978 encodedAVA = PORT_Alloc(len); |
974 » if (!encodedAVA) { | 979 if (!encodedAVA) { |
975 » SECITEM_FreeItem(avaValue, PR_TRUE); | 980 SECITEM_FreeItem(avaValue, PR_TRUE); |
976 » if (unknownTag) | 981 if (unknownTag) |
977 » » PR_smprintf_free(unknownTag); | 982 PR_smprintf_free(unknownTag); |
978 » return SECFailure; | 983 return SECFailure; |
979 » } | 984 } |
980 } else { | 985 } else { |
981 » /* Must make output fit in tmpbuf */ | 986 /* Must make output fit in tmpbuf */ |
982 » unsigned int fair = (sizeof tmpBuf)/2 - 1; /* for = and \0 */ | 987 unsigned int fair = (sizeof tmpBuf) / 2 - 1; /* for = and \0 */ |
983 | 988 |
984 » if (nameLen < fair) { | 989 if (nameLen < fair) { |
985 » /* just truncate the value */ | 990 /* just truncate the value */ |
986 » maxValue = (sizeof tmpBuf) - (nameLen + 6); /* for "=...\0", | 991 maxValue = (sizeof tmpBuf) - (nameLen + 6); /* for "=...\0", |
987 and possibly '"' */ | 992 and possibly '"' */ |
988 » } else if (valueLen < fair) { | 993 } else if (valueLen < fair) { |
989 » /* just truncate the name */ | 994 /* just truncate the name */ |
990 » maxName = (sizeof tmpBuf) - (valueLen + 5); /* for "=...\0" */ | 995 maxName = (sizeof tmpBuf) - (valueLen + 5); /* for "=...\0" */ |
991 » } else { | 996 } else { |
992 » /* truncate both */ | 997 /* truncate both */ |
993 » maxName = maxValue = fair - 3; /* for "..." */ | 998 maxName = maxValue = fair - 3; /* for "..." */ |
994 » } | 999 } |
995 » if (nameLen > maxName) { | 1000 if (nameLen > maxName) { |
996 » PORT_Assert(unknownTag && unknownTag == tagName); | 1001 PORT_Assert(unknownTag && unknownTag == tagName); |
997 » truncateName = PR_TRUE; | 1002 truncateName = PR_TRUE; |
998 » nameLen = maxName; | 1003 nameLen = maxName; |
999 » } | 1004 } |
1000 » encodedAVA = tmpBuf; | 1005 encodedAVA = tmpBuf; |
1001 } | 1006 } |
1002 | 1007 |
1003 memcpy(encodedAVA, tagName, nameLen); | 1008 memcpy(encodedAVA, tagName, nameLen); |
1004 if (truncateName) { | 1009 if (truncateName) { |
1005 » /* If tag name is too long, we know it is an OID form that was | 1010 /* If tag name is too long, we know it is an OID form that was |
1006 » * allocated from the heap, so we can modify it in place | 1011 * allocated from the heap, so we can modify it in place |
1007 » */ | 1012 */ |
1008 » encodedAVA[nameLen-1] = '.'; | 1013 encodedAVA[nameLen - 1] = '.'; |
1009 » encodedAVA[nameLen-2] = '.'; | 1014 encodedAVA[nameLen - 2] = '.'; |
1010 » encodedAVA[nameLen-3] = '.'; | 1015 encodedAVA[nameLen - 3] = '.'; |
1011 } | 1016 } |
1012 encodedAVA[nameLen++] = '='; | 1017 encodedAVA[nameLen++] = '='; |
1013 if (unknownTag) | 1018 if (unknownTag) |
1014 » PR_smprintf_free(unknownTag); | 1019 PR_smprintf_free(unknownTag); |
1015 | 1020 |
1016 if (strict == CERT_N2A_READABLE && maxValue > maxBytes) | 1021 if (strict == CERT_N2A_READABLE && maxValue > maxBytes) |
1017 » maxValue = maxBytes; | 1022 maxValue = maxBytes; |
1018 if (valueLen > maxValue) { | 1023 if (valueLen > maxValue) { |
1019 » valueLen = maxValue; | 1024 valueLen = maxValue; |
1020 » truncateValue = PR_TRUE; | 1025 truncateValue = PR_TRUE; |
1021 } | 1026 } |
1022 /* escape and quote as necessary - don't quote hex strings */ | 1027 /* escape and quote as necessary - don't quote hex strings */ |
1023 if (useHex) { | 1028 if (useHex) { |
1024 » char * end = encodedAVA + nameLen + valueLen; | 1029 char* end = encodedAVA + nameLen + valueLen; |
1025 » memcpy(encodedAVA + nameLen, (char *)avaValue->data, valueLen); | 1030 memcpy(encodedAVA + nameLen, (char*)avaValue->data, valueLen); |
1026 » end[0] = '\0'; | 1031 end[0] = '\0'; |
1027 » if (truncateValue) { | 1032 if (truncateValue) { |
1028 » end[-1] = '.'; | 1033 end[-1] = '.'; |
1029 » end[-2] = '.'; | 1034 end[-2] = '.'; |
1030 » end[-3] = '.'; | 1035 end[-3] = '.'; |
1031 » } | 1036 } |
1032 » rv = SECSuccess; | 1037 rv = SECSuccess; |
1033 } else if (!truncateValue) { | 1038 } else if (!truncateValue) { |
1034 » rv = escapeAndQuote(encodedAVA + nameLen, len - nameLen, | 1039 rv = escapeAndQuote(encodedAVA + nameLen, len - nameLen, |
1035 » » » (char *)avaValue->data, avaValue->len, &mode); | 1040 (char*)avaValue->data, avaValue->len, &mode); |
1036 } else { | 1041 } else { |
1037 » /* must truncate the escaped and quoted value */ | 1042 /* must truncate the escaped and quoted value */ |
1038 » char bigTmpBuf[TMPBUF_LEN * 3 + 3]; | 1043 char bigTmpBuf[TMPBUF_LEN * 3 + 3]; |
1039 » PORT_Assert(valueLen < sizeof tmpBuf); | 1044 PORT_Assert(valueLen < sizeof tmpBuf); |
1040 » rv = escapeAndQuote(bigTmpBuf, sizeof bigTmpBuf, | 1045 rv = escapeAndQuote(bigTmpBuf, sizeof bigTmpBuf, (char*)avaValue->data, |
1041 » » » (char *)avaValue->data, | 1046 PR_MIN(avaValue->len, valueLen), &mode); |
1042 » » » PR_MIN(avaValue->len, valueLen), &mode); | |
1043 | 1047 |
1044 » bigTmpBuf[valueLen--] = '\0'; /* hard stop here */ | 1048 bigTmpBuf[valueLen--] = '\0'; /* hard stop here */ |
1045 » /* See if we're in the middle of a multi-byte UTF8 character */ | 1049 /* See if we're in the middle of a multi-byte UTF8 character */ |
1046 » while (((bigTmpBuf[valueLen] & 0xc0) == 0x80) && valueLen > 0) { | 1050 while (((bigTmpBuf[valueLen] & 0xc0) == 0x80) && valueLen > 0) { |
1047 » bigTmpBuf[valueLen--] = '\0'; | 1051 bigTmpBuf[valueLen--] = '\0'; |
1048 » } | 1052 } |
1049 » /* add ellipsis to signify truncation. */ | 1053 /* add ellipsis to signify truncation. */ |
1050 » bigTmpBuf[++valueLen] = '.'; | 1054 bigTmpBuf[++valueLen] = '.'; |
1051 » bigTmpBuf[++valueLen] = '.'; | 1055 bigTmpBuf[++valueLen] = '.'; |
1052 » bigTmpBuf[++valueLen] = '.'; | 1056 bigTmpBuf[++valueLen] = '.'; |
1053 » if (bigTmpBuf[0] == '"') | 1057 if (bigTmpBuf[0] == '"') |
1054 » bigTmpBuf[++valueLen] = '"'; | 1058 bigTmpBuf[++valueLen] = '"'; |
1055 » bigTmpBuf[++valueLen] = '\0'; | 1059 bigTmpBuf[++valueLen] = '\0'; |
1056 » PORT_Assert(nameLen + valueLen <= (sizeof tmpBuf) - 1); | 1060 PORT_Assert(nameLen + valueLen <= (sizeof tmpBuf) - 1); |
1057 » memcpy(encodedAVA + nameLen, bigTmpBuf, valueLen+1); | 1061 memcpy(encodedAVA + nameLen, bigTmpBuf, valueLen + 1); |
1058 } | 1062 } |
1059 | 1063 |
1060 SECITEM_FreeItem(avaValue, PR_TRUE); | 1064 SECITEM_FreeItem(avaValue, PR_TRUE); |
1061 if (rv == SECSuccess) | 1065 if (rv == SECSuccess) |
1062 » rv = AppendStr(bufp, encodedAVA); | 1066 rv = AppendStr(bufp, encodedAVA); |
1063 if (encodedAVA != tmpBuf) | 1067 if (encodedAVA != tmpBuf) |
1064 » PORT_Free(encodedAVA); | 1068 PORT_Free(encodedAVA); |
1065 return rv; | 1069 return rv; |
1066 } | 1070 } |
1067 | 1071 |
1068 #undef tagName | 1072 #undef tagName |
1069 #undef maxBytes | 1073 #undef maxBytes |
1070 #undef tag | 1074 #undef tag |
1071 #undef vt | 1075 #undef vt |
1072 | 1076 |
1073 char * | 1077 char* |
1074 CERT_NameToAsciiInvertible(CERTName *name, CertStrictnessLevel strict) | 1078 CERT_NameToAsciiInvertible(CERTName* name, CertStrictnessLevel strict) |
1075 { | 1079 { |
1076 CERTRDN** rdns; | 1080 CERTRDN** rdns; |
1077 CERTRDN** lastRdn; | 1081 CERTRDN** lastRdn; |
1078 CERTRDN** rdn; | 1082 CERTRDN** rdn; |
1079 PRBool first = PR_TRUE; | 1083 PRBool first = PR_TRUE; |
1080 stringBuf strBuf = { NULL, 0, 0 }; | 1084 stringBuf strBuf = { NULL, 0, 0 }; |
1081 | 1085 |
1082 rdns = name->rdns; | 1086 rdns = name->rdns; |
1083 if (rdns == NULL) { | 1087 if (rdns == NULL) { |
1084 » return NULL; | 1088 return NULL; |
1085 } | 1089 } |
1086 | 1090 |
1087 /* find last RDN */ | 1091 /* find last RDN */ |
1088 lastRdn = rdns; | 1092 lastRdn = rdns; |
1089 while (*lastRdn) lastRdn++; | 1093 while (*lastRdn) |
| 1094 lastRdn++; |
1090 lastRdn--; | 1095 lastRdn--; |
1091 | 1096 |
1092 /* | 1097 /* |
1093 * Loop over name contents in _reverse_ RDN order appending to string | 1098 * Loop over name contents in _reverse_ RDN order appending to string |
| 1099 */ |
| 1100 for (rdn = lastRdn; rdn >= rdns; rdn--) { |
| 1101 CERTAVA** avas = (*rdn)->avas; |
| 1102 CERTAVA* ava; |
| 1103 PRBool newRDN = PR_TRUE; |
| 1104 |
| 1105 /* |
| 1106 * XXX Do we need to traverse the AVAs in reverse order, too? |
1094 */ | 1107 */ |
1095 for (rdn = lastRdn; rdn >= rdns; rdn--) { | 1108 while (avas && (ava = *avas++) != NULL) { |
1096 » CERTAVA** avas = (*rdn)->avas; | 1109 SECStatus rv; |
1097 » CERTAVA* ava; | 1110 /* Put in comma or plus separator */ |
1098 » PRBool newRDN = PR_TRUE; | 1111 if (!first) { |
| 1112 /* Use of spaces is deprecated in RFC 2253. */ |
| 1113 rv = AppendStr(&strBuf, newRDN ? "," : "+"); |
| 1114 if (rv) |
| 1115 goto loser; |
| 1116 } else { |
| 1117 first = PR_FALSE; |
| 1118 } |
1099 | 1119 |
1100 » /* | 1120 /* Add in tag type plus value into strBuf */ |
1101 » * XXX Do we need to traverse the AVAs in reverse order, too? | 1121 rv = AppendAVA(&strBuf, ava, strict); |
1102 » */ | 1122 if (rv) |
1103 » while (avas && (ava = *avas++) != NULL) { | 1123 goto loser; |
1104 » SECStatus rv; | 1124 newRDN = PR_FALSE; |
1105 » /* Put in comma or plus separator */ | 1125 } |
1106 » if (!first) { | |
1107 » » /* Use of spaces is deprecated in RFC 2253. */ | |
1108 » » rv = AppendStr(&strBuf, newRDN ? "," : "+"); | |
1109 » » if (rv) goto loser; | |
1110 » } else { | |
1111 » » first = PR_FALSE; | |
1112 » } | |
1113 » | |
1114 » /* Add in tag type plus value into strBuf */ | |
1115 » rv = AppendAVA(&strBuf, ava, strict); | |
1116 » if (rv) goto loser; | |
1117 » newRDN = PR_FALSE; | |
1118 » } | |
1119 } | 1126 } |
1120 return strBuf.buffer; | 1127 return strBuf.buffer; |
1121 loser: | 1128 loser: |
1122 if (strBuf.buffer) { | 1129 if (strBuf.buffer) { |
1123 » PORT_Free(strBuf.buffer); | 1130 PORT_Free(strBuf.buffer); |
1124 } | 1131 } |
1125 return NULL; | 1132 return NULL; |
1126 } | 1133 } |
1127 | 1134 |
1128 char * | 1135 char* |
1129 CERT_NameToAscii(CERTName *name) | 1136 CERT_NameToAscii(CERTName* name) |
1130 { | 1137 { |
1131 return CERT_NameToAsciiInvertible(name, CERT_N2A_READABLE); | 1138 return CERT_NameToAsciiInvertible(name, CERT_N2A_READABLE); |
1132 } | 1139 } |
1133 | 1140 |
1134 /* | 1141 /* |
1135 * Return the string representation of a DER encoded distinguished name | 1142 * Return the string representation of a DER encoded distinguished name |
1136 * "dername" - The DER encoded name to convert | 1143 * "dername" - The DER encoded name to convert |
1137 */ | 1144 */ |
1138 char * | 1145 char* |
1139 CERT_DerNameToAscii(SECItem *dername) | 1146 CERT_DerNameToAscii(SECItem* dername) |
1140 { | 1147 { |
1141 int rv; | 1148 int rv; |
1142 PLArenaPool *arena = NULL; | 1149 PLArenaPool* arena = NULL; |
1143 CERTName name; | 1150 CERTName name; |
1144 char *retstr = NULL; | 1151 char* retstr = NULL; |
1145 | 1152 |
1146 arena = PORT_NewArena(DER_DEFAULT_CHUNKSIZE); | 1153 arena = PORT_NewArena(DER_DEFAULT_CHUNKSIZE); |
1147 | 1154 |
1148 if ( arena == NULL) { | 1155 if (arena == NULL) { |
1149 » goto loser; | 1156 goto loser; |
1150 } | 1157 } |
1151 | 1158 |
1152 rv = SEC_QuickDERDecodeItem(arena, &name, CERT_NameTemplate, dername); | 1159 rv = SEC_QuickDERDecodeItem(arena, &name, CERT_NameTemplate, dername); |
1153 | 1160 |
1154 if ( rv != SECSuccess ) { | 1161 if (rv != SECSuccess) { |
1155 » goto loser; | 1162 goto loser; |
1156 } | 1163 } |
1157 | 1164 |
1158 retstr = CERT_NameToAscii(&name); | 1165 retstr = CERT_NameToAscii(&name); |
1159 | 1166 |
1160 loser: | 1167 loser: |
1161 if ( arena != NULL ) { | 1168 if (arena != NULL) { |
1162 » PORT_FreeArena(arena, PR_FALSE); | 1169 PORT_FreeArena(arena, PR_FALSE); |
1163 } | 1170 } |
1164 | 1171 |
1165 return(retstr); | 1172 return (retstr); |
1166 } | 1173 } |
1167 | 1174 |
1168 static char * | 1175 static char* |
1169 avaToString(PLArenaPool *arena, CERTAVA *ava) | 1176 avaToString(PLArenaPool* arena, CERTAVA* ava) |
1170 { | 1177 { |
1171 char * buf = NULL; | 1178 char* buf = NULL; |
1172 SECItem* avaValue; | 1179 SECItem* avaValue; |
1173 int valueLen; | 1180 int valueLen; |
1174 | 1181 |
1175 avaValue = CERT_DecodeAVAValue(&ava->value); | 1182 avaValue = CERT_DecodeAVAValue(&ava->value); |
1176 if(!avaValue) { | 1183 if (!avaValue) { |
1177 » return buf; | 1184 return buf; |
1178 } | 1185 } |
1179 valueLen = cert_RFC1485_GetRequiredLen((char *)avaValue->data, | 1186 valueLen = |
1180 avaValue->len, NULL) + 1; | 1187 cert_RFC1485_GetRequiredLen((char*)avaValue->data, avaValue->len, NULL)
+ 1; |
1181 if (arena) { | 1188 if (arena) { |
1182 » buf = (char *)PORT_ArenaZAlloc(arena, valueLen); | 1189 buf = (char*)PORT_ArenaZAlloc(arena, valueLen); |
1183 } else { | 1190 } else { |
1184 » buf = (char *)PORT_ZAlloc(valueLen); | 1191 buf = (char*)PORT_ZAlloc(valueLen); |
1185 } | 1192 } |
1186 if (buf) { | 1193 if (buf) { |
1187 » SECStatus rv = escapeAndQuote(buf, valueLen, (char *)avaValue->data, | 1194 SECStatus rv = |
1188 » avaValue->len, NULL); | 1195 escapeAndQuote(buf, valueLen, (char*)avaValue->data, avaValue->len,
NULL); |
1189 » if (rv != SECSuccess) { | 1196 if (rv != SECSuccess) { |
1190 » if (!arena) | 1197 if (!arena) |
1191 » » PORT_Free(buf); | 1198 PORT_Free(buf); |
1192 » buf = NULL; | 1199 buf = NULL; |
1193 » } | 1200 } |
1194 } | 1201 } |
1195 SECITEM_FreeItem(avaValue, PR_TRUE); | 1202 SECITEM_FreeItem(avaValue, PR_TRUE); |
1196 return buf; | 1203 return buf; |
1197 } | 1204 } |
1198 | 1205 |
1199 /* RDNs are sorted from most general to most specific. | 1206 /* RDNs are sorted from most general to most specific. |
1200 * This code returns the FIRST one found, the most general one found. | 1207 * This code returns the FIRST one found, the most general one found. |
1201 */ | 1208 */ |
1202 static char * | 1209 static char* |
1203 CERT_GetNameElement(PLArenaPool *arena, const CERTName *name, int wantedTag) | 1210 CERT_GetNameElement(PLArenaPool* arena, const CERTName* name, int wantedTag) |
1204 { | 1211 { |
1205 CERTRDN** rdns = name->rdns; | 1212 CERTRDN** rdns = name->rdns; |
1206 CERTRDN* rdn; | 1213 CERTRDN* rdn; |
1207 CERTAVA* ava = NULL; | 1214 CERTAVA* ava = NULL; |
1208 | 1215 |
1209 while (rdns && (rdn = *rdns++) != 0) { | 1216 while (rdns && (rdn = *rdns++) != 0) { |
1210 » CERTAVA** avas = rdn->avas; | 1217 CERTAVA** avas = rdn->avas; |
1211 » while (avas && (ava = *avas++) != 0) { | 1218 while (avas && (ava = *avas++) != 0) { |
1212 » int tag = CERT_GetAVATag(ava); | 1219 int tag = CERT_GetAVATag(ava); |
1213 » if ( tag == wantedTag ) { | 1220 if (tag == wantedTag) { |
1214 » » avas = NULL; | 1221 avas = NULL; |
1215 » » rdns = NULL; /* break out of all loops */ | 1222 rdns = NULL; /* break out of all loops */ |
1216 » } | 1223 } |
1217 » } | 1224 } |
1218 } | 1225 } |
1219 return ava ? avaToString(arena, ava) : NULL; | 1226 return ava ? avaToString(arena, ava) : NULL; |
1220 } | 1227 } |
1221 | 1228 |
1222 /* RDNs are sorted from most general to most specific. | 1229 /* RDNs are sorted from most general to most specific. |
1223 * This code returns the LAST one found, the most specific one found. | 1230 * This code returns the LAST one found, the most specific one found. |
1224 * This is particularly appropriate for Common Name. See RFC 2818. | 1231 * This is particularly appropriate for Common Name. See RFC 2818. |
1225 */ | 1232 */ |
1226 static char * | 1233 static char* |
1227 CERT_GetLastNameElement(PLArenaPool *arena, const CERTName *name, int wantedTag) | 1234 CERT_GetLastNameElement(PLArenaPool* arena, const CERTName* name, int wantedTag) |
1228 { | 1235 { |
1229 CERTRDN** rdns = name->rdns; | 1236 CERTRDN** rdns = name->rdns; |
1230 CERTRDN* rdn; | 1237 CERTRDN* rdn; |
1231 CERTAVA* lastAva = NULL; | 1238 CERTAVA* lastAva = NULL; |
1232 | 1239 |
1233 while (rdns && (rdn = *rdns++) != 0) { | 1240 while (rdns && (rdn = *rdns++) != 0) { |
1234 » CERTAVA** avas = rdn->avas; | 1241 CERTAVA** avas = rdn->avas; |
1235 » CERTAVA* ava; | 1242 CERTAVA* ava; |
1236 » while (avas && (ava = *avas++) != 0) { | 1243 while (avas && (ava = *avas++) != 0) { |
1237 » int tag = CERT_GetAVATag(ava); | 1244 int tag = CERT_GetAVATag(ava); |
1238 » if ( tag == wantedTag ) { | 1245 if (tag == wantedTag) { |
1239 » » lastAva = ava; | 1246 lastAva = ava; |
1240 » } | 1247 } |
1241 » } | 1248 } |
1242 } | 1249 } |
1243 return lastAva ? avaToString(arena, lastAva) : NULL; | 1250 return lastAva ? avaToString(arena, lastAva) : NULL; |
1244 } | 1251 } |
1245 | 1252 |
1246 char * | 1253 char* |
1247 CERT_GetCertificateEmailAddress(CERTCertificate *cert) | 1254 CERT_GetCertificateEmailAddress(CERTCertificate* cert) |
1248 { | 1255 { |
1249 char *rawEmailAddr = NULL; | 1256 char* rawEmailAddr = NULL; |
1250 SECItem subAltName; | 1257 SECItem subAltName; |
1251 SECStatus rv; | 1258 SECStatus rv; |
1252 CERTGeneralName *nameList = NULL; | 1259 CERTGeneralName* nameList = NULL; |
1253 CERTGeneralName *current; | 1260 CERTGeneralName* current; |
1254 PLArenaPool *arena = NULL; | 1261 PLArenaPool* arena = NULL; |
1255 int i; | 1262 int i; |
1256 | 1263 |
1257 subAltName.data = NULL; | 1264 subAltName.data = NULL; |
1258 | 1265 |
1259 rawEmailAddr = CERT_GetNameElement(cert->arena, &(cert->subject), | 1266 rawEmailAddr = CERT_GetNameElement(cert->arena, &(cert->subject), |
1260 » » » » » » SEC_OID_PKCS9_EMAIL_ADDRESS); | 1267 SEC_OID_PKCS9_EMAIL_ADDRESS); |
1261 if ( rawEmailAddr == NULL ) { | 1268 if (rawEmailAddr == NULL) { |
1262 » rawEmailAddr = CERT_GetNameElement(cert->arena, &(cert->subject), | 1269 rawEmailAddr = |
1263 » » » » » » » SEC_OID_RFC1274_MAIL); | 1270 CERT_GetNameElement(cert->arena, &(cert->subject), SEC_OID_RFC1274_M
AIL); |
1264 } | 1271 } |
1265 if ( rawEmailAddr == NULL) { | 1272 if (rawEmailAddr == NULL) { |
1266 | 1273 |
1267 » rv = CERT_FindCertExtension(cert, SEC_OID_X509_SUBJECT_ALT_NAME, | 1274 rv = |
1268 » » » » » » » » &subAltName); | 1275 CERT_FindCertExtension(cert, SEC_OID_X509_SUBJECT_ALT_NAME, &subAltN
ame); |
1269 » if (rv != SECSuccess) { | 1276 if (rv != SECSuccess) { |
1270 » goto finish; | 1277 goto finish; |
1271 » } | 1278 } |
1272 » arena = PORT_NewArena(DER_DEFAULT_CHUNKSIZE); | 1279 arena = PORT_NewArena(DER_DEFAULT_CHUNKSIZE); |
1273 » if (!arena) { | 1280 if (!arena) { |
1274 » goto finish; | 1281 goto finish; |
1275 » } | 1282 } |
1276 » nameList = current = CERT_DecodeAltNameExtension(arena, &subAltName); | 1283 nameList = current = CERT_DecodeAltNameExtension(arena, &subAltName); |
1277 » if (!nameList ) { | 1284 if (!nameList) { |
1278 » goto finish; | 1285 goto finish; |
1279 » } | 1286 } |
1280 » if (nameList != NULL) { | 1287 if (nameList != NULL) { |
1281 » do { | 1288 do { |
1282 » » if (current->type == certDirectoryName) { | 1289 if (current->type == certDirectoryName) { |
1283 » » rawEmailAddr = CERT_GetNameElement(cert->arena, | 1290 rawEmailAddr = |
1284 » » » &(current->name.directoryName), | 1291 CERT_GetNameElement(cert->arena, &(current->name.directo
ryName), |
1285 » » » » » SEC_OID_PKCS9_EMAIL_ADDRESS); | 1292 SEC_OID_PKCS9_EMAIL_ADDRESS); |
1286 » » if ( rawEmailAddr == NULL ) { | 1293 if (rawEmailAddr == |
1287 » » » rawEmailAddr = CERT_GetNameElement(cert->arena, | 1294 NULL) { |
1288 » » » &(current->name.directoryName), SEC_OID_RFC1274_MAIL); | 1295 rawEmailAddr = |
1289 » » } | 1296 CERT_GetNameElement(cert->arena, &(current->name.dir
ectoryName), |
1290 » » } else if (current->type == certRFC822Name) { | 1297 SEC_OID_RFC1274_MAIL); |
1291 » » rawEmailAddr = (char*)PORT_ArenaZAlloc(cert->arena, | 1298 } |
1292 » » » » » » current->name.other.len + 1); | 1299 } else if (current->type == certRFC822Name) { |
1293 » » if (!rawEmailAddr) { | 1300 rawEmailAddr = |
1294 » » » goto finish; | 1301 (char*)PORT_ArenaZAlloc(cert->arena, current->name.other
.len + |
1295 » » } | 1302 1); |
1296 » » PORT_Memcpy(rawEmailAddr, current->name.other.data, | 1303 if (!rawEmailAddr) { |
1297 » » » » current->name.other.len); | 1304 goto finish; |
1298 » » rawEmailAddr[current->name.other.len] = '\0'; | 1305 } |
1299 » » } | 1306 PORT_Memcpy(rawEmailAddr, current->name.other.data, |
1300 » » if (rawEmailAddr) { | 1307 current->name.other.len); |
1301 » » break; | 1308 rawEmailAddr[current->name.other.len] = |
1302 » » } | 1309 '\0'; |
1303 » » current = CERT_GetNextGeneralName(current); | 1310 } |
1304 » } while (current != nameList); | 1311 if (rawEmailAddr) { |
1305 » } | 1312 break; |
| 1313 } |
| 1314 current = CERT_GetNextGeneralName(current); |
| 1315 } while (current != nameList); |
| 1316 } |
1306 } | 1317 } |
1307 if (rawEmailAddr) { | 1318 if (rawEmailAddr) { |
1308 » for (i = 0; i <= (int) PORT_Strlen(rawEmailAddr); i++) { | 1319 for (i = 0; i <= (int)PORT_Strlen(rawEmailAddr); i++) { |
1309 » rawEmailAddr[i] = tolower(rawEmailAddr[i]); | 1320 rawEmailAddr[i] = tolower(rawEmailAddr[i]); |
1310 » } | 1321 } |
1311 } | 1322 } |
1312 | 1323 |
1313 finish: | 1324 finish: |
1314 | 1325 |
1315 /* Don't free nameList, it's part of the arena. */ | 1326 /* Don't free nameList, it's part of the arena. */ |
1316 | 1327 |
1317 if (arena) { | 1328 if (arena) { |
1318 » PORT_FreeArena(arena, PR_FALSE); | 1329 PORT_FreeArena(arena, PR_FALSE); |
1319 } | 1330 } |
1320 | 1331 |
1321 if ( subAltName.data ) { | 1332 if (subAltName.data) { |
1322 » SECITEM_FreeItem(&subAltName, PR_FALSE); | 1333 SECITEM_FreeItem(&subAltName, PR_FALSE); |
1323 } | 1334 } |
1324 | 1335 |
1325 return(rawEmailAddr); | 1336 return (rawEmailAddr); |
1326 } | 1337 } |
1327 | 1338 |
1328 static char * | 1339 static char* |
1329 appendStringToBuf(char *dest, char *src, PRUint32 *pRemaining) | 1340 appendStringToBuf(char* dest, char* src, PRUint32* pRemaining) |
1330 { | 1341 { |
1331 PRUint32 len; | 1342 PRUint32 len; |
1332 if (dest && src && src[0] && *pRemaining > (len = PL_strlen(src))) { | 1343 if (dest && src && src[0] && *pRemaining > (len = PL_strlen(src))) { |
1333 » PRUint32 i; | 1344 PRUint32 i; |
1334 » for (i = 0; i < len; ++i) | 1345 for (i = 0; i < len; ++i) |
1335 » dest[i] = tolower(src[i]); | 1346 dest[i] = tolower(src[i]); |
1336 » dest[len] = 0; | 1347 dest[len] = 0; |
1337 » dest += len + 1; | 1348 dest += len + 1; |
1338 » *pRemaining -= len + 1; | 1349 *pRemaining -= len + 1; |
1339 } | 1350 } |
1340 return dest; | 1351 return dest; |
1341 } | 1352 } |
1342 | 1353 |
1343 #undef NEEDS_HEX_ESCAPE | 1354 #undef NEEDS_HEX_ESCAPE |
1344 #define NEEDS_HEX_ESCAPE(c) (c < 0x20) | 1355 #define NEEDS_HEX_ESCAPE(c) (c < 0x20) |
1345 | 1356 |
1346 static char * | 1357 static char* |
1347 appendItemToBuf(char *dest, SECItem *src, PRUint32 *pRemaining) | 1358 appendItemToBuf(char* dest, SECItem* src, PRUint32* pRemaining) |
1348 { | 1359 { |
1349 if (dest && src && src->data && src->len && src->data[0]) { | 1360 if (dest && src && src->data && src->len && src->data[0]) { |
1350 » PRUint32 len = src->len; | 1361 PRUint32 len = src->len; |
1351 » PRUint32 i; | 1362 PRUint32 i; |
1352 » PRUint32 reqLen = len + 1; | 1363 PRUint32 reqLen = len + 1; |
1353 » /* are there any embedded control characters ? */ | 1364 /* are there any embedded control characters ? */ |
1354 » for (i = 0; i < len; i++) { | 1365 for (i = 0; i < len; i++) { |
1355 » if (NEEDS_HEX_ESCAPE(src->data[i])) | 1366 if (NEEDS_HEX_ESCAPE(src->data[i])) |
1356 » » reqLen += 2; | 1367 reqLen += 2; |
1357 » } | 1368 } |
1358 » if (*pRemaining > reqLen) { | 1369 if (*pRemaining > reqLen) { |
1359 » for (i = 0; i < len; ++i) { | 1370 for (i = 0; i < len; ++i) { |
1360 » » PRUint8 c = src->data[i]; | 1371 PRUint8 c = src->data[i]; |
1361 » » if (NEEDS_HEX_ESCAPE(c)) { | 1372 if (NEEDS_HEX_ESCAPE(c)) { |
1362 » » *dest++ = C_BACKSLASH; | 1373 *dest++ = |
1363 » » *dest++ = hexChars[ (c >> 4) & 0x0f ]; | 1374 C_BACKSLASH; |
1364 » » *dest++ = hexChars[ c & 0x0f ]; | 1375 *dest++ = |
1365 » » } else { | 1376 hexChars[(c >> 4) & 0x0f]; |
1366 » » *dest++ = tolower(c); | 1377 *dest++ = |
1367 » » } | 1378 hexChars[c & 0x0f]; |
1368 » } | 1379 } else { |
1369 » *dest++ = '\0'; | 1380 *dest++ = |
1370 » *pRemaining -= reqLen; | 1381 tolower(c); |
1371 » } | 1382 } |
| 1383 } |
| 1384 *dest++ = '\0'; |
| 1385 *pRemaining -= reqLen; |
| 1386 } |
1372 } | 1387 } |
1373 return dest; | 1388 return dest; |
1374 } | 1389 } |
1375 | 1390 |
1376 /* Returns a pointer to an environment-like string, a series of | 1391 /* Returns a pointer to an environment-like string, a series of |
1377 ** null-terminated strings, terminated by a zero-length string. | 1392 ** null-terminated strings, terminated by a zero-length string. |
1378 ** This function is intended to be internal to NSS. | 1393 ** This function is intended to be internal to NSS. |
1379 */ | 1394 */ |
1380 char * | 1395 char* |
1381 cert_GetCertificateEmailAddresses(CERTCertificate *cert) | 1396 cert_GetCertificateEmailAddresses(CERTCertificate* cert) |
1382 { | 1397 { |
1383 char * rawEmailAddr = NULL; | 1398 char* rawEmailAddr = NULL; |
1384 char * addrBuf = NULL; | 1399 char* addrBuf = NULL; |
1385 char * pBuf = NULL; | 1400 char* pBuf = NULL; |
1386 PLArenaPool * tmpArena = PORT_NewArena(DER_DEFAULT_CHUNKSIZE); | 1401 PLArenaPool* tmpArena = PORT_NewArena(DER_DEFAULT_CHUNKSIZE); |
1387 PRUint32 maxLen = 0; | 1402 PRUint32 maxLen = 0; |
1388 PRInt32 finalLen = 0; | 1403 PRInt32 finalLen = 0; |
1389 SECStatus rv; | 1404 SECStatus rv; |
1390 SECItem subAltName; | 1405 SECItem subAltName; |
1391 | 1406 |
1392 if (!tmpArena) | 1407 if (!tmpArena) |
1393 » return addrBuf; | 1408 return addrBuf; |
1394 | 1409 |
1395 subAltName.data = NULL; | 1410 subAltName.data = NULL; |
1396 maxLen = cert->derCert.len; | 1411 maxLen = cert->derCert.len; |
1397 PORT_Assert(maxLen); | 1412 PORT_Assert(maxLen); |
1398 if (!maxLen) | 1413 if (!maxLen) |
1399 » maxLen = 2000; /* a guess, should never happen */ | 1414 maxLen = 2000; /* a guess, should never happen */ |
1400 | 1415 |
1401 pBuf = addrBuf = (char *)PORT_ArenaZAlloc(tmpArena, maxLen + 1); | 1416 pBuf = addrBuf = (char*)PORT_ArenaZAlloc(tmpArena, maxLen + 1); |
1402 if (!addrBuf) | 1417 if (!addrBuf) |
1403 » goto loser; | 1418 goto loser; |
1404 | 1419 |
1405 rawEmailAddr = CERT_GetNameElement(tmpArena, &cert->subject, | 1420 rawEmailAddr = |
1406 » » » » SEC_OID_PKCS9_EMAIL_ADDRESS); | 1421 CERT_GetNameElement(tmpArena, &cert->subject, SEC_OID_PKCS9_EMAIL_ADDRES
S); |
1407 pBuf = appendStringToBuf(pBuf, rawEmailAddr, &maxLen); | 1422 pBuf = appendStringToBuf(pBuf, rawEmailAddr, &maxLen); |
1408 | 1423 |
1409 rawEmailAddr = CERT_GetNameElement(tmpArena, &cert->subject, | 1424 rawEmailAddr = |
1410 » » » » SEC_OID_RFC1274_MAIL); | 1425 CERT_GetNameElement(tmpArena, &cert->subject, SEC_OID_RFC1274_MAIL); |
1411 pBuf = appendStringToBuf(pBuf, rawEmailAddr, &maxLen); | 1426 pBuf = appendStringToBuf(pBuf, rawEmailAddr, &maxLen); |
1412 | 1427 |
1413 rv = CERT_FindCertExtension(cert, SEC_OID_X509_SUBJECT_ALT_NAME, | 1428 rv = CERT_FindCertExtension(cert, SEC_OID_X509_SUBJECT_ALT_NAME, &subAltName
); |
1414 » » » » &subAltName); | |
1415 if (rv == SECSuccess && subAltName.data) { | 1429 if (rv == SECSuccess && subAltName.data) { |
1416 » CERTGeneralName *nameList = NULL; | 1430 CERTGeneralName* nameList = NULL; |
1417 | 1431 |
1418 » if (!!(nameList = CERT_DecodeAltNameExtension(tmpArena, &subAltName))) { | 1432 if (!!(nameList = CERT_DecodeAltNameExtension(tmpArena, &subAltName))) { |
1419 » CERTGeneralName *current = nameList; | 1433 CERTGeneralName* current = nameList; |
1420 » do { | 1434 do { |
1421 » » if (current->type == certDirectoryName) { | 1435 if (current->type == certDirectoryName) { |
1422 » » rawEmailAddr = CERT_GetNameElement(tmpArena, | 1436 rawEmailAddr = |
1423 » » » ¤t->name.directoryName, | 1437 CERT_GetNameElement(tmpArena, ¤t->name.directoryNa
me, |
1424 » » » » » SEC_OID_PKCS9_EMAIL_ADDRESS); | 1438 SEC_OID_PKCS9_EMAIL_ADDRESS); |
1425 » » pBuf = appendStringToBuf(pBuf, rawEmailAddr, &maxLen); | 1439 pBuf = |
| 1440 appendStringToBuf(pBuf, rawEmailAddr, &maxLen); |
1426 | 1441 |
1427 » » rawEmailAddr = CERT_GetNameElement(tmpArena, | 1442 rawEmailAddr = |
1428 » » » » » ¤t->name.directoryName, | 1443 CERT_GetNameElement( |
1429 » » » » » SEC_OID_RFC1274_MAIL); | 1444 tmpArena, ¤t->name.directoryName, SEC_OID_RFC1
274_MAIL); |
1430 » » pBuf = appendStringToBuf(pBuf, rawEmailAddr, &maxLen); | 1445 pBuf = |
1431 » » } else if (current->type == certRFC822Name) { | 1446 appendStringToBuf(pBuf, rawEmailAddr, &maxLen); |
1432 » » pBuf = appendItemToBuf(pBuf, ¤t->name.other, &maxLen); | 1447 } else if (current->type == certRFC822Name) { |
1433 » » } | 1448 pBuf = |
1434 » » current = CERT_GetNextGeneralName(current); | 1449 appendItemToBuf(pBuf, ¤t->name.other, &maxLen); |
1435 » } while (current != nameList); | 1450 } |
1436 » } | 1451 current = CERT_GetNextGeneralName(current); |
1437 » SECITEM_FreeItem(&subAltName, PR_FALSE); | 1452 } while (current != nameList); |
1438 » /* Don't free nameList, it's part of the tmpArena. */ | 1453 } |
| 1454 SECITEM_FreeItem(&subAltName, PR_FALSE); |
| 1455 /* Don't free nameList, it's part of the tmpArena. */ |
1439 } | 1456 } |
1440 /* now copy superstring to cert's arena */ | 1457 /* now copy superstring to cert's arena */ |
1441 finalLen = (pBuf - addrBuf) + 1; | 1458 finalLen = (pBuf - addrBuf) + 1; |
1442 pBuf = NULL; | 1459 pBuf = NULL; |
1443 if (finalLen > 1) { | 1460 if (finalLen > 1) { |
1444 » pBuf = PORT_ArenaAlloc(cert->arena, finalLen); | 1461 pBuf = PORT_ArenaAlloc(cert->arena, finalLen); |
1445 » if (pBuf) { | 1462 if (pBuf) { |
1446 » PORT_Memcpy(pBuf, addrBuf, finalLen); | 1463 PORT_Memcpy(pBuf, addrBuf, finalLen); |
1447 » } | 1464 } |
1448 } | 1465 } |
1449 loser: | 1466 loser: |
1450 if (tmpArena) | 1467 if (tmpArena) |
1451 » PORT_FreeArena(tmpArena, PR_FALSE); | 1468 PORT_FreeArena(tmpArena, PR_FALSE); |
1452 | 1469 |
1453 return pBuf; | 1470 return pBuf; |
1454 } | 1471 } |
1455 | 1472 |
1456 /* returns pointer to storage in cert's arena. Storage remains valid | 1473 /* returns pointer to storage in cert's arena. Storage remains valid |
1457 ** as long as cert's reference count doesn't go to zero. | 1474 ** as long as cert's reference count doesn't go to zero. |
1458 ** Caller should strdup or otherwise copy. | 1475 ** Caller should strdup or otherwise copy. |
1459 */ | 1476 */ |
1460 const char *» /* const so caller won't muck with it. */ | 1477 const char* /* const so caller won't muck with it. */ |
1461 CERT_GetFirstEmailAddress(CERTCertificate * cert) | 1478 CERT_GetFirstEmailAddress(CERTCertificate* cert) |
1462 { | 1479 { |
1463 if (cert && cert->emailAddr && cert->emailAddr[0]) | 1480 if (cert && cert->emailAddr && cert->emailAddr[0]) |
1464 » return (const char *)cert->emailAddr; | 1481 return (const char*)cert->emailAddr; |
1465 return NULL; | 1482 return NULL; |
1466 } | 1483 } |
1467 | 1484 |
1468 /* returns pointer to storage in cert's arena. Storage remains valid | 1485 /* returns pointer to storage in cert's arena. Storage remains valid |
1469 ** as long as cert's reference count doesn't go to zero. | 1486 ** as long as cert's reference count doesn't go to zero. |
1470 ** Caller should strdup or otherwise copy. | 1487 ** Caller should strdup or otherwise copy. |
1471 */ | 1488 */ |
1472 const char *» /* const so caller won't muck with it. */ | 1489 const char* /* const so caller won't muck with it. */ |
1473 CERT_GetNextEmailAddress(CERTCertificate * cert, const char * prev) | 1490 CERT_GetNextEmailAddress(CERTCertificate* cert, const char* prev) |
1474 { | 1491 { |
1475 if (cert && prev && prev[0]) { | 1492 if (cert && prev && prev[0]) { |
1476 » PRUint32 len = PL_strlen(prev); | 1493 PRUint32 len = PL_strlen(prev); |
1477 » prev += len + 1; | 1494 prev += len + 1; |
1478 » if (prev && prev[0]) | 1495 if (prev && prev[0]) |
1479 » return prev; | 1496 return prev; |
1480 } | 1497 } |
1481 return NULL; | 1498 return NULL; |
1482 } | 1499 } |
1483 | 1500 |
1484 /* This is seriously bogus, now that certs store their email addresses in | 1501 /* This is seriously bogus, now that certs store their email addresses in |
1485 ** subject Alternative Name extensions. | 1502 ** subject Alternative Name extensions. |
1486 ** Returns a string allocated by PORT_StrDup, which the caller must free. | 1503 ** Returns a string allocated by PORT_StrDup, which the caller must free. |
1487 */ | 1504 */ |
1488 char * | 1505 char* |
1489 CERT_GetCertEmailAddress(const CERTName *name) | 1506 CERT_GetCertEmailAddress(const CERTName* name) |
1490 { | 1507 { |
1491 char *rawEmailAddr; | 1508 char* rawEmailAddr; |
1492 char *emailAddr; | 1509 char* emailAddr; |
1493 | 1510 |
1494 | |
1495 rawEmailAddr = CERT_GetNameElement(NULL, name, SEC_OID_PKCS9_EMAIL_ADDRESS); | 1511 rawEmailAddr = CERT_GetNameElement(NULL, name, SEC_OID_PKCS9_EMAIL_ADDRESS); |
1496 if ( rawEmailAddr == NULL ) { | 1512 if (rawEmailAddr == NULL) { |
1497 » rawEmailAddr = CERT_GetNameElement(NULL, name, SEC_OID_RFC1274_MAIL); | 1513 rawEmailAddr = CERT_GetNameElement(NULL, name, SEC_OID_RFC1274_MAIL); |
1498 } | 1514 } |
1499 emailAddr = CERT_FixupEmailAddr(rawEmailAddr); | 1515 emailAddr = CERT_FixupEmailAddr(rawEmailAddr); |
1500 if ( rawEmailAddr ) { | 1516 if (rawEmailAddr) { |
1501 » PORT_Free(rawEmailAddr); | 1517 PORT_Free(rawEmailAddr); |
1502 } | 1518 } |
1503 return(emailAddr); | 1519 return (emailAddr); |
1504 } | 1520 } |
1505 | 1521 |
1506 /* The return value must be freed with PORT_Free. */ | 1522 /* The return value must be freed with PORT_Free. */ |
1507 char * | 1523 char* |
1508 CERT_GetCommonName(const CERTName *name) | 1524 CERT_GetCommonName(const CERTName* name) |
1509 { | 1525 { |
1510 return(CERT_GetLastNameElement(NULL, name, SEC_OID_AVA_COMMON_NAME)); | 1526 return (CERT_GetLastNameElement(NULL, name, SEC_OID_AVA_COMMON_NAME)); |
1511 } | 1527 } |
1512 | 1528 |
1513 char * | 1529 char* |
1514 CERT_GetCountryName(const CERTName *name) | 1530 CERT_GetCountryName(const CERTName* name) |
1515 { | 1531 { |
1516 return(CERT_GetNameElement(NULL, name, SEC_OID_AVA_COUNTRY_NAME)); | 1532 return (CERT_GetNameElement(NULL, name, SEC_OID_AVA_COUNTRY_NAME)); |
1517 } | 1533 } |
1518 | 1534 |
1519 char * | 1535 char* |
1520 CERT_GetLocalityName(const CERTName *name) | 1536 CERT_GetLocalityName(const CERTName* name) |
1521 { | 1537 { |
1522 return(CERT_GetNameElement(NULL, name, SEC_OID_AVA_LOCALITY)); | 1538 return (CERT_GetNameElement(NULL, name, SEC_OID_AVA_LOCALITY)); |
1523 } | 1539 } |
1524 | 1540 |
1525 char * | 1541 char* |
1526 CERT_GetStateName(const CERTName *name) | 1542 CERT_GetStateName(const CERTName* name) |
1527 { | 1543 { |
1528 return(CERT_GetNameElement(NULL, name, SEC_OID_AVA_STATE_OR_PROVINCE)); | 1544 return (CERT_GetNameElement(NULL, name, SEC_OID_AVA_STATE_OR_PROVINCE)); |
1529 } | 1545 } |
1530 | 1546 |
1531 char * | 1547 char* |
1532 CERT_GetOrgName(const CERTName *name) | 1548 CERT_GetOrgName(const CERTName* name) |
1533 { | 1549 { |
1534 return(CERT_GetNameElement(NULL, name, SEC_OID_AVA_ORGANIZATION_NAME)); | 1550 return (CERT_GetNameElement(NULL, name, SEC_OID_AVA_ORGANIZATION_NAME)); |
1535 } | 1551 } |
1536 | 1552 |
1537 char * | 1553 char* |
1538 CERT_GetDomainComponentName(const CERTName *name) | 1554 CERT_GetDomainComponentName(const CERTName* name) |
1539 { | 1555 { |
1540 return(CERT_GetNameElement(NULL, name, SEC_OID_AVA_DC)); | 1556 return (CERT_GetNameElement(NULL, name, SEC_OID_AVA_DC)); |
1541 } | 1557 } |
1542 | 1558 |
1543 char * | 1559 char* |
1544 CERT_GetOrgUnitName(const CERTName *name) | 1560 CERT_GetOrgUnitName(const CERTName* name) |
1545 { | 1561 { |
1546 return(CERT_GetNameElement(NULL, name, SEC_OID_AVA_ORGANIZATIONAL_UNIT_NAME)
); | 1562 return ( |
| 1563 CERT_GetNameElement(NULL, name, SEC_OID_AVA_ORGANIZATIONAL_UNIT_NAME)); |
1547 } | 1564 } |
1548 | 1565 |
1549 char * | 1566 char* |
1550 CERT_GetDnQualifier(const CERTName *name) | 1567 CERT_GetDnQualifier(const CERTName* name) |
1551 { | 1568 { |
1552 return(CERT_GetNameElement(NULL, name, SEC_OID_AVA_DN_QUALIFIER)); | 1569 return (CERT_GetNameElement(NULL, name, SEC_OID_AVA_DN_QUALIFIER)); |
1553 } | 1570 } |
1554 | 1571 |
1555 char * | 1572 char* |
1556 CERT_GetCertUid(const CERTName *name) | 1573 CERT_GetCertUid(const CERTName* name) |
1557 { | 1574 { |
1558 return(CERT_GetNameElement(NULL, name, SEC_OID_RFC1274_UID)); | 1575 return (CERT_GetNameElement(NULL, name, SEC_OID_RFC1274_UID)); |
1559 } | 1576 } |
1560 | |
OLD | NEW |