diff options
Diffstat (limited to 'src/lib/libcrypto/asn1/x_name.c')
| -rw-r--r-- | src/lib/libcrypto/asn1/x_name.c | 249 |
1 files changed, 135 insertions, 114 deletions
diff --git a/src/lib/libcrypto/asn1/x_name.c b/src/lib/libcrypto/asn1/x_name.c index d10ac5acd1..70459babc4 100644 --- a/src/lib/libcrypto/asn1/x_name.c +++ b/src/lib/libcrypto/asn1/x_name.c | |||
| @@ -5,21 +5,21 @@ | |||
| 5 | * This package is an SSL implementation written | 5 | * This package is an SSL implementation written |
| 6 | * by Eric Young (eay@cryptsoft.com). | 6 | * by Eric Young (eay@cryptsoft.com). |
| 7 | * The implementation was written so as to conform with Netscapes SSL. | 7 | * The implementation was written so as to conform with Netscapes SSL. |
| 8 | * | 8 | * |
| 9 | * This library is free for commercial and non-commercial use as long as | 9 | * This library is free for commercial and non-commercial use as long as |
| 10 | * the following conditions are aheared to. The following conditions | 10 | * the following conditions are aheared to. The following conditions |
| 11 | * apply to all code found in this distribution, be it the RC4, RSA, | 11 | * apply to all code found in this distribution, be it the RC4, RSA, |
| 12 | * lhash, DES, etc., code; not just the SSL code. The SSL documentation | 12 | * lhash, DES, etc., code; not just the SSL code. The SSL documentation |
| 13 | * included with this distribution is covered by the same copyright terms | 13 | * included with this distribution is covered by the same copyright terms |
| 14 | * except that the holder is Tim Hudson (tjh@cryptsoft.com). | 14 | * except that the holder is Tim Hudson (tjh@cryptsoft.com). |
| 15 | * | 15 | * |
| 16 | * Copyright remains Eric Young's, and as such any Copyright notices in | 16 | * Copyright remains Eric Young's, and as such any Copyright notices in |
| 17 | * the code are not to be removed. | 17 | * the code are not to be removed. |
| 18 | * If this package is used in a product, Eric Young should be given attribution | 18 | * If this package is used in a product, Eric Young should be given attribution |
| 19 | * as the author of the parts of the library used. | 19 | * as the author of the parts of the library used. |
| 20 | * This can be in the form of a textual message at program startup or | 20 | * This can be in the form of a textual message at program startup or |
| 21 | * in documentation (online or textual) provided with the package. | 21 | * in documentation (online or textual) provided with the package. |
| 22 | * | 22 | * |
| 23 | * Redistribution and use in source and binary forms, with or without | 23 | * Redistribution and use in source and binary forms, with or without |
| 24 | * modification, are permitted provided that the following conditions | 24 | * modification, are permitted provided that the following conditions |
| 25 | * are met: | 25 | * are met: |
| @@ -34,10 +34,10 @@ | |||
| 34 | * Eric Young (eay@cryptsoft.com)" | 34 | * Eric Young (eay@cryptsoft.com)" |
| 35 | * The word 'cryptographic' can be left out if the rouines from the library | 35 | * The word 'cryptographic' can be left out if the rouines from the library |
| 36 | * being used are not cryptographic related :-). | 36 | * being used are not cryptographic related :-). |
| 37 | * 4. If you include any Windows specific code (or a derivative thereof) from | 37 | * 4. If you include any Windows specific code (or a derivative thereof) from |
| 38 | * the apps directory (application code) you must include an acknowledgement: | 38 | * the apps directory (application code) you must include an acknowledgement: |
| 39 | * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" | 39 | * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" |
| 40 | * | 40 | * |
| 41 | * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND | 41 | * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND |
| 42 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | 42 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 43 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | 43 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| @@ -49,7 +49,7 @@ | |||
| 49 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | 49 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 50 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | 50 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 51 | * SUCH DAMAGE. | 51 | * SUCH DAMAGE. |
| 52 | * | 52 | * |
| 53 | * The licence and distribution terms for any publically available version or | 53 | * The licence and distribution terms for any publically available version or |
| 54 | * derivative of this code cannot be changed. i.e. this code cannot simply be | 54 | * derivative of this code cannot be changed. i.e. this code cannot simply be |
| 55 | * copied and put under another distribution licence | 55 | * copied and put under another distribution licence |
| @@ -66,13 +66,12 @@ | |||
| 66 | typedef STACK_OF(X509_NAME_ENTRY) STACK_OF_X509_NAME_ENTRY; | 66 | typedef STACK_OF(X509_NAME_ENTRY) STACK_OF_X509_NAME_ENTRY; |
| 67 | DECLARE_STACK_OF(STACK_OF_X509_NAME_ENTRY) | 67 | DECLARE_STACK_OF(STACK_OF_X509_NAME_ENTRY) |
| 68 | 68 | ||
| 69 | static int x509_name_ex_d2i(ASN1_VALUE **val, | 69 | static int x509_name_ex_d2i(ASN1_VALUE **val, const unsigned char **in, |
| 70 | const unsigned char **in, long len, | 70 | long len, const ASN1_ITEM *it, int tag, int aclass, char opt, |
| 71 | const ASN1_ITEM *it, | 71 | ASN1_TLC *ctx); |
| 72 | int tag, int aclass, char opt, ASN1_TLC *ctx); | ||
| 73 | 72 | ||
| 74 | static int x509_name_ex_i2d(ASN1_VALUE **val, unsigned char **out, | 73 | static int x509_name_ex_i2d(ASN1_VALUE **val, unsigned char **out, |
| 75 | const ASN1_ITEM *it, int tag, int aclass); | 74 | const ASN1_ITEM *it, int tag, int aclass); |
| 76 | static int x509_name_ex_new(ASN1_VALUE **val, const ASN1_ITEM *it); | 75 | static int x509_name_ex_new(ASN1_VALUE **val, const ASN1_ITEM *it); |
| 77 | static void x509_name_ex_free(ASN1_VALUE **val, const ASN1_ITEM *it); | 76 | static void x509_name_ex_free(ASN1_VALUE **val, const ASN1_ITEM *it); |
| 78 | 77 | ||
| @@ -80,13 +79,10 @@ static int x509_name_encode(X509_NAME *a); | |||
| 80 | static int x509_name_canon(X509_NAME *a); | 79 | static int x509_name_canon(X509_NAME *a); |
| 81 | static int asn1_string_canon(ASN1_STRING *out, ASN1_STRING *in); | 80 | static int asn1_string_canon(ASN1_STRING *out, ASN1_STRING *in); |
| 82 | static int i2d_name_canon(STACK_OF(STACK_OF_X509_NAME_ENTRY) *intname, | 81 | static int i2d_name_canon(STACK_OF(STACK_OF_X509_NAME_ENTRY) *intname, |
| 83 | unsigned char **in); | 82 | unsigned char **in); |
| 84 | 83 | ||
| 85 | 84 | static int x509_name_ex_print(BIO *out, ASN1_VALUE **pval, int indent, | |
| 86 | static int x509_name_ex_print(BIO *out, ASN1_VALUE **pval, | 85 | const char *fname, const ASN1_PCTX *pctx); |
| 87 | int indent, | ||
| 88 | const char *fname, | ||
| 89 | const ASN1_PCTX *pctx); | ||
| 90 | 86 | ||
| 91 | ASN1_SEQUENCE(X509_NAME_ENTRY) = { | 87 | ASN1_SEQUENCE(X509_NAME_ENTRY) = { |
| 92 | ASN1_SIMPLE(X509_NAME_ENTRY, object, ASN1_OBJECT), | 88 | ASN1_SIMPLE(X509_NAME_ENTRY, object, ASN1_OBJECT), |
| @@ -101,11 +97,11 @@ IMPLEMENT_ASN1_DUP_FUNCTION(X509_NAME_ENTRY) | |||
| 101 | */ | 97 | */ |
| 102 | 98 | ||
| 103 | ASN1_ITEM_TEMPLATE(X509_NAME_ENTRIES) = | 99 | ASN1_ITEM_TEMPLATE(X509_NAME_ENTRIES) = |
| 104 | ASN1_EX_TEMPLATE_TYPE(ASN1_TFLG_SET_OF, 0, RDNS, X509_NAME_ENTRY) | 100 | ASN1_EX_TEMPLATE_TYPE(ASN1_TFLG_SET_OF, 0, RDNS, X509_NAME_ENTRY) |
| 105 | ASN1_ITEM_TEMPLATE_END(X509_NAME_ENTRIES) | 101 | ASN1_ITEM_TEMPLATE_END(X509_NAME_ENTRIES) |
| 106 | 102 | ||
| 107 | ASN1_ITEM_TEMPLATE(X509_NAME_INTERNAL) = | 103 | ASN1_ITEM_TEMPLATE(X509_NAME_INTERNAL) = |
| 108 | ASN1_EX_TEMPLATE_TYPE(ASN1_TFLG_SEQUENCE_OF, 0, Name, X509_NAME_ENTRIES) | 104 | ASN1_EX_TEMPLATE_TYPE(ASN1_TFLG_SEQUENCE_OF, 0, Name, X509_NAME_ENTRIES) |
| 109 | ASN1_ITEM_TEMPLATE_END(X509_NAME_INTERNAL) | 105 | ASN1_ITEM_TEMPLATE_END(X509_NAME_INTERNAL) |
| 110 | 106 | ||
| 111 | /* Normally that's where it would end: we'd have two nested STACK structures | 107 | /* Normally that's where it would end: we'd have two nested STACK structures |
| @@ -124,26 +120,30 @@ const ASN1_EXTERN_FUNCS x509_name_ff = { | |||
| 124 | x509_name_ex_print | 120 | x509_name_ex_print |
| 125 | }; | 121 | }; |
| 126 | 122 | ||
| 127 | IMPLEMENT_EXTERN_ASN1(X509_NAME, V_ASN1_SEQUENCE, x509_name_ff) | 123 | IMPLEMENT_EXTERN_ASN1(X509_NAME, V_ASN1_SEQUENCE, x509_name_ff) |
| 128 | 124 | ||
| 129 | IMPLEMENT_ASN1_FUNCTIONS(X509_NAME) | 125 | IMPLEMENT_ASN1_FUNCTIONS(X509_NAME) |
| 130 | IMPLEMENT_ASN1_DUP_FUNCTION(X509_NAME) | 126 | IMPLEMENT_ASN1_DUP_FUNCTION(X509_NAME) |
| 131 | 127 | ||
| 132 | static int x509_name_ex_new(ASN1_VALUE **val, const ASN1_ITEM *it) | 128 | static int |
| 129 | x509_name_ex_new(ASN1_VALUE **val, const ASN1_ITEM *it) | ||
| 133 | { | 130 | { |
| 134 | X509_NAME *ret = NULL; | 131 | X509_NAME *ret = NULL; |
| 132 | |||
| 135 | ret = malloc(sizeof(X509_NAME)); | 133 | ret = malloc(sizeof(X509_NAME)); |
| 136 | if(!ret) goto memerr; | 134 | if (!ret) |
| 137 | if ((ret->entries=sk_X509_NAME_ENTRY_new_null()) == NULL) | 135 | goto memerr; |
| 136 | if ((ret->entries = sk_X509_NAME_ENTRY_new_null()) == NULL) | ||
| 137 | goto memerr; | ||
| 138 | if ((ret->bytes = BUF_MEM_new()) == NULL) | ||
| 138 | goto memerr; | 139 | goto memerr; |
| 139 | if((ret->bytes = BUF_MEM_new()) == NULL) goto memerr; | ||
| 140 | ret->canon_enc = NULL; | 140 | ret->canon_enc = NULL; |
| 141 | ret->canon_enclen = 0; | 141 | ret->canon_enclen = 0; |
| 142 | ret->modified=1; | 142 | ret->modified = 1; |
| 143 | *val = (ASN1_VALUE *)ret; | 143 | *val = (ASN1_VALUE *)ret; |
| 144 | return 1; | 144 | return 1; |
| 145 | 145 | ||
| 146 | memerr: | 146 | memerr: |
| 147 | ASN1err(ASN1_F_X509_NAME_EX_NEW, ERR_R_MALLOC_FAILURE); | 147 | ASN1err(ASN1_F_X509_NAME_EX_NEW, ERR_R_MALLOC_FAILURE); |
| 148 | if (ret) { | 148 | if (ret) { |
| 149 | if (ret->entries) | 149 | if (ret->entries) |
| @@ -153,54 +153,64 @@ static int x509_name_ex_new(ASN1_VALUE **val, const ASN1_ITEM *it) | |||
| 153 | return 0; | 153 | return 0; |
| 154 | } | 154 | } |
| 155 | 155 | ||
| 156 | static void x509_name_ex_free(ASN1_VALUE **pval, const ASN1_ITEM *it) | 156 | static void |
| 157 | x509_name_ex_free(ASN1_VALUE **pval, const ASN1_ITEM *it) | ||
| 157 | { | 158 | { |
| 158 | X509_NAME *a; | 159 | X509_NAME *a; |
| 159 | if(!pval || !*pval) | 160 | |
| 160 | return; | 161 | if (!pval || !*pval) |
| 162 | return; | ||
| 161 | a = (X509_NAME *)*pval; | 163 | a = (X509_NAME *)*pval; |
| 162 | 164 | ||
| 163 | BUF_MEM_free(a->bytes); | 165 | BUF_MEM_free(a->bytes); |
| 164 | sk_X509_NAME_ENTRY_pop_free(a->entries,X509_NAME_ENTRY_free); | 166 | sk_X509_NAME_ENTRY_pop_free(a->entries, X509_NAME_ENTRY_free); |
| 165 | if (a->canon_enc) | 167 | if (a->canon_enc) |
| 166 | free(a->canon_enc); | 168 | free(a->canon_enc); |
| 167 | free(a); | 169 | free(a); |
| 168 | *pval = NULL; | 170 | *pval = NULL; |
| 169 | } | 171 | } |
| 170 | 172 | ||
| 171 | static int x509_name_ex_d2i(ASN1_VALUE **val, | 173 | static int |
| 172 | const unsigned char **in, long len, const ASN1_ITEM *it, | 174 | x509_name_ex_d2i(ASN1_VALUE **val, const unsigned char **in, long len, |
| 173 | int tag, int aclass, char opt, ASN1_TLC *ctx) | 175 | const ASN1_ITEM *it, int tag, int aclass, char opt, ASN1_TLC *ctx) |
| 174 | { | 176 | { |
| 175 | const unsigned char *p = *in, *q; | 177 | const unsigned char *p = *in, *q; |
| 176 | union { STACK_OF(STACK_OF_X509_NAME_ENTRY) *s; | 178 | union { |
| 177 | ASN1_VALUE *a; } intname = {NULL}; | 179 | STACK_OF(STACK_OF_X509_NAME_ENTRY) *s; |
| 178 | union { X509_NAME *x; ASN1_VALUE *a; } nm = {NULL}; | 180 | ASN1_VALUE *a; |
| 181 | } intname = {NULL}; | ||
| 182 | union { | ||
| 183 | X509_NAME *x; | ||
| 184 | ASN1_VALUE *a; | ||
| 185 | } nm = {NULL}; | ||
| 179 | int i, j, ret; | 186 | int i, j, ret; |
| 180 | STACK_OF(X509_NAME_ENTRY) *entries; | 187 | STACK_OF(X509_NAME_ENTRY) *entries; |
| 181 | X509_NAME_ENTRY *entry; | 188 | X509_NAME_ENTRY *entry; |
| 182 | q = p; | 189 | q = p; |
| 183 | 190 | ||
| 184 | /* Get internal representation of Name */ | 191 | /* Get internal representation of Name */ |
| 185 | ret = ASN1_item_ex_d2i(&intname.a, | 192 | ret = ASN1_item_ex_d2i(&intname.a, &p, len, |
| 186 | &p, len, ASN1_ITEM_rptr(X509_NAME_INTERNAL), | 193 | ASN1_ITEM_rptr(X509_NAME_INTERNAL), tag, aclass, opt, ctx); |
| 187 | tag, aclass, opt, ctx); | 194 | |
| 188 | 195 | if (ret <= 0) | |
| 189 | if(ret <= 0) return ret; | 196 | return ret; |
| 190 | 197 | ||
| 191 | if(*val) x509_name_ex_free(val, NULL); | 198 | if (*val) |
| 192 | if(!x509_name_ex_new(&nm.a, NULL)) goto err; | 199 | x509_name_ex_free(val, NULL); |
| 200 | if (!x509_name_ex_new(&nm.a, NULL)) | ||
| 201 | goto err; | ||
| 193 | /* We've decoded it: now cache encoding */ | 202 | /* We've decoded it: now cache encoding */ |
| 194 | if(!BUF_MEM_grow(nm.x->bytes, p - q)) goto err; | 203 | if (!BUF_MEM_grow(nm.x->bytes, p - q)) |
| 204 | goto err; | ||
| 195 | memcpy(nm.x->bytes->data, q, p - q); | 205 | memcpy(nm.x->bytes->data, q, p - q); |
| 196 | 206 | ||
| 197 | /* Convert internal representation to X509_NAME structure */ | 207 | /* Convert internal representation to X509_NAME structure */ |
| 198 | for(i = 0; i < sk_STACK_OF_X509_NAME_ENTRY_num(intname.s); i++) { | 208 | for (i = 0; i < sk_STACK_OF_X509_NAME_ENTRY_num(intname.s); i++) { |
| 199 | entries = sk_STACK_OF_X509_NAME_ENTRY_value(intname.s, i); | 209 | entries = sk_STACK_OF_X509_NAME_ENTRY_value(intname.s, i); |
| 200 | for(j = 0; j < sk_X509_NAME_ENTRY_num(entries); j++) { | 210 | for (j = 0; j < sk_X509_NAME_ENTRY_num(entries); j++) { |
| 201 | entry = sk_X509_NAME_ENTRY_value(entries, j); | 211 | entry = sk_X509_NAME_ENTRY_value(entries, j); |
| 202 | entry->set = i; | 212 | entry->set = i; |
| 203 | if(!sk_X509_NAME_ENTRY_push(nm.x->entries, entry)) | 213 | if (!sk_X509_NAME_ENTRY_push(nm.x->entries, entry)) |
| 204 | goto err; | 214 | goto err; |
| 205 | } | 215 | } |
| 206 | sk_X509_NAME_ENTRY_free(entries); | 216 | sk_X509_NAME_ENTRY_free(entries); |
| @@ -213,90 +223,104 @@ static int x509_name_ex_d2i(ASN1_VALUE **val, | |||
| 213 | *val = nm.a; | 223 | *val = nm.a; |
| 214 | *in = p; | 224 | *in = p; |
| 215 | return ret; | 225 | return ret; |
| 226 | |||
| 216 | err: | 227 | err: |
| 217 | if (nm.x != NULL) | 228 | if (nm.x != NULL) |
| 218 | X509_NAME_free(nm.x); | 229 | X509_NAME_free(nm.x); |
| 219 | ASN1err(ASN1_F_X509_NAME_EX_D2I, ERR_R_NESTED_ASN1_ERROR); | 230 | ASN1err(ASN1_F_X509_NAME_EX_D2I, ERR_R_NESTED_ASN1_ERROR); |
| 220 | return 0; | 231 | return 0; |
| 221 | } | 232 | } |
| 222 | 233 | ||
| 223 | static int x509_name_ex_i2d(ASN1_VALUE **val, unsigned char **out, const ASN1_ITEM *it, int tag, int aclass) | 234 | static int |
| 235 | x509_name_ex_i2d(ASN1_VALUE **val, unsigned char **out, const ASN1_ITEM *it, | ||
| 236 | int tag, int aclass) | ||
| 224 | { | 237 | { |
| 225 | int ret; | 238 | int ret; |
| 226 | X509_NAME *a = (X509_NAME *)*val; | 239 | X509_NAME *a = (X509_NAME *)*val; |
| 227 | if(a->modified) { | 240 | |
| 241 | if (a->modified) { | ||
| 228 | ret = x509_name_encode(a); | 242 | ret = x509_name_encode(a); |
| 229 | if(ret < 0) | 243 | if (ret < 0) |
| 230 | return ret; | 244 | return ret; |
| 231 | ret = x509_name_canon(a); | 245 | ret = x509_name_canon(a); |
| 232 | if(ret < 0) | 246 | if (ret < 0) |
| 233 | return ret; | 247 | return ret; |
| 234 | } | 248 | } |
| 235 | ret = a->bytes->length; | 249 | ret = a->bytes->length; |
| 236 | if(out != NULL) { | 250 | if (out != NULL) { |
| 237 | memcpy(*out,a->bytes->data,ret); | 251 | memcpy(*out, a->bytes->data, ret); |
| 238 | *out+=ret; | 252 | *out += ret; |
| 239 | } | 253 | } |
| 240 | return ret; | 254 | return ret; |
| 241 | } | 255 | } |
| 242 | 256 | ||
| 243 | static void local_sk_X509_NAME_ENTRY_free(STACK_OF(X509_NAME_ENTRY) *ne) | 257 | static void |
| 258 | local_sk_X509_NAME_ENTRY_free(STACK_OF(X509_NAME_ENTRY) *ne) | ||
| 244 | { | 259 | { |
| 245 | sk_X509_NAME_ENTRY_free(ne); | 260 | sk_X509_NAME_ENTRY_free(ne); |
| 246 | } | 261 | } |
| 247 | 262 | ||
| 248 | static void local_sk_X509_NAME_ENTRY_pop_free(STACK_OF(X509_NAME_ENTRY) *ne) | 263 | static void |
| 264 | local_sk_X509_NAME_ENTRY_pop_free(STACK_OF(X509_NAME_ENTRY) *ne) | ||
| 249 | { | 265 | { |
| 250 | sk_X509_NAME_ENTRY_pop_free(ne, X509_NAME_ENTRY_free); | 266 | sk_X509_NAME_ENTRY_pop_free(ne, X509_NAME_ENTRY_free); |
| 251 | } | 267 | } |
| 252 | 268 | ||
| 253 | static int x509_name_encode(X509_NAME *a) | 269 | static int |
| 270 | x509_name_encode(X509_NAME *a) | ||
| 254 | { | 271 | { |
| 255 | union { STACK_OF(STACK_OF_X509_NAME_ENTRY) *s; | 272 | union { |
| 256 | ASN1_VALUE *a; } intname = {NULL}; | 273 | STACK_OF(STACK_OF_X509_NAME_ENTRY) *s; |
| 274 | ASN1_VALUE *a; | ||
| 275 | } intname = {NULL}; | ||
| 257 | int len; | 276 | int len; |
| 258 | unsigned char *p; | 277 | unsigned char *p; |
| 259 | STACK_OF(X509_NAME_ENTRY) *entries = NULL; | 278 | STACK_OF(X509_NAME_ENTRY) *entries = NULL; |
| 260 | X509_NAME_ENTRY *entry; | 279 | X509_NAME_ENTRY *entry; |
| 261 | int i, set = -1; | 280 | int i, set = -1; |
| 281 | |||
| 262 | intname.s = sk_STACK_OF_X509_NAME_ENTRY_new_null(); | 282 | intname.s = sk_STACK_OF_X509_NAME_ENTRY_new_null(); |
| 263 | if(!intname.s) goto memerr; | 283 | if (!intname.s) |
| 264 | for(i = 0; i < sk_X509_NAME_ENTRY_num(a->entries); i++) { | 284 | goto memerr; |
| 285 | for (i = 0; i < sk_X509_NAME_ENTRY_num(a->entries); i++) { | ||
| 265 | entry = sk_X509_NAME_ENTRY_value(a->entries, i); | 286 | entry = sk_X509_NAME_ENTRY_value(a->entries, i); |
| 266 | if(entry->set != set) { | 287 | if (entry->set != set) { |
| 267 | entries = sk_X509_NAME_ENTRY_new_null(); | 288 | entries = sk_X509_NAME_ENTRY_new_null(); |
| 268 | if(!entries) goto memerr; | 289 | if (!entries) |
| 269 | if(!sk_STACK_OF_X509_NAME_ENTRY_push(intname.s, | 290 | goto memerr; |
| 270 | entries)) | 291 | if (!sk_STACK_OF_X509_NAME_ENTRY_push(intname.s, |
| 292 | entries)) | ||
| 271 | goto memerr; | 293 | goto memerr; |
| 272 | set = entry->set; | 294 | set = entry->set; |
| 273 | } | 295 | } |
| 274 | if(!sk_X509_NAME_ENTRY_push(entries, entry)) goto memerr; | 296 | if (!sk_X509_NAME_ENTRY_push(entries, entry)) |
| 297 | goto memerr; | ||
| 275 | } | 298 | } |
| 276 | len = ASN1_item_ex_i2d(&intname.a, NULL, | 299 | len = ASN1_item_ex_i2d(&intname.a, NULL, |
| 277 | ASN1_ITEM_rptr(X509_NAME_INTERNAL), -1, -1); | 300 | ASN1_ITEM_rptr(X509_NAME_INTERNAL), -1, -1); |
| 278 | if (!BUF_MEM_grow(a->bytes,len)) goto memerr; | 301 | if (!BUF_MEM_grow(a->bytes, len)) |
| 279 | p=(unsigned char *)a->bytes->data; | 302 | goto memerr; |
| 280 | ASN1_item_ex_i2d(&intname.a, | 303 | p = (unsigned char *)a->bytes->data; |
| 281 | &p, ASN1_ITEM_rptr(X509_NAME_INTERNAL), -1, -1); | 304 | ASN1_item_ex_i2d(&intname.a, &p, ASN1_ITEM_rptr(X509_NAME_INTERNAL), |
| 305 | -1, -1); | ||
| 282 | sk_STACK_OF_X509_NAME_ENTRY_pop_free(intname.s, | 306 | sk_STACK_OF_X509_NAME_ENTRY_pop_free(intname.s, |
| 283 | local_sk_X509_NAME_ENTRY_free); | 307 | local_sk_X509_NAME_ENTRY_free); |
| 284 | a->modified = 0; | 308 | a->modified = 0; |
| 285 | return len; | 309 | return len; |
| 310 | |||
| 286 | memerr: | 311 | memerr: |
| 287 | sk_STACK_OF_X509_NAME_ENTRY_pop_free(intname.s, | 312 | sk_STACK_OF_X509_NAME_ENTRY_pop_free(intname.s, |
| 288 | local_sk_X509_NAME_ENTRY_free); | 313 | local_sk_X509_NAME_ENTRY_free); |
| 289 | ASN1err(ASN1_F_X509_NAME_ENCODE, ERR_R_MALLOC_FAILURE); | 314 | ASN1err(ASN1_F_X509_NAME_ENCODE, ERR_R_MALLOC_FAILURE); |
| 290 | return -1; | 315 | return -1; |
| 291 | } | 316 | } |
| 292 | 317 | ||
| 293 | static int x509_name_ex_print(BIO *out, ASN1_VALUE **pval, | 318 | static int |
| 294 | int indent, | 319 | x509_name_ex_print(BIO *out, ASN1_VALUE **pval, int indent, const char *fname, |
| 295 | const char *fname, | 320 | const ASN1_PCTX *pctx) |
| 296 | const ASN1_PCTX *pctx) | ||
| 297 | { | 321 | { |
| 298 | if (X509_NAME_print_ex(out, (X509_NAME *)*pval, | 322 | if (X509_NAME_print_ex(out, (X509_NAME *)*pval, indent, |
| 299 | indent, pctx->nm_flags) <= 0) | 323 | pctx->nm_flags) <= 0) |
| 300 | return 0; | 324 | return 0; |
| 301 | return 2; | 325 | return 2; |
| 302 | } | 326 | } |
| @@ -314,7 +338,8 @@ static int x509_name_ex_print(BIO *out, ASN1_VALUE **pval, | |||
| 314 | * dirName can also be checked with a simple memcmp(). | 338 | * dirName can also be checked with a simple memcmp(). |
| 315 | */ | 339 | */ |
| 316 | 340 | ||
| 317 | static int x509_name_canon(X509_NAME *a) | 341 | static int |
| 342 | x509_name_canon(X509_NAME *a) | ||
| 318 | { | 343 | { |
| 319 | unsigned char *p; | 344 | unsigned char *p; |
| 320 | STACK_OF(STACK_OF_X509_NAME_ENTRY) *intname = NULL; | 345 | STACK_OF(STACK_OF_X509_NAME_ENTRY) *intname = NULL; |
| @@ -325,22 +350,22 @@ static int x509_name_canon(X509_NAME *a) | |||
| 325 | if (a->canon_enc) { | 350 | if (a->canon_enc) { |
| 326 | free(a->canon_enc); | 351 | free(a->canon_enc); |
| 327 | a->canon_enc = NULL; | 352 | a->canon_enc = NULL; |
| 328 | } | 353 | } |
| 329 | /* Special case: empty X509_NAME => null encoding */ | 354 | /* Special case: empty X509_NAME => null encoding */ |
| 330 | if (sk_X509_NAME_ENTRY_num(a->entries) == 0) { | 355 | if (sk_X509_NAME_ENTRY_num(a->entries) == 0) { |
| 331 | a->canon_enclen = 0; | 356 | a->canon_enclen = 0; |
| 332 | return 1; | 357 | return 1; |
| 333 | } | 358 | } |
| 334 | intname = sk_STACK_OF_X509_NAME_ENTRY_new_null(); | 359 | intname = sk_STACK_OF_X509_NAME_ENTRY_new_null(); |
| 335 | if(!intname) | 360 | if (!intname) |
| 336 | goto err; | 361 | goto err; |
| 337 | for(i = 0; i < sk_X509_NAME_ENTRY_num(a->entries); i++) { | 362 | for (i = 0; i < sk_X509_NAME_ENTRY_num(a->entries); i++) { |
| 338 | entry = sk_X509_NAME_ENTRY_value(a->entries, i); | 363 | entry = sk_X509_NAME_ENTRY_value(a->entries, i); |
| 339 | if(entry->set != set) { | 364 | if (entry->set != set) { |
| 340 | entries = sk_X509_NAME_ENTRY_new_null(); | 365 | entries = sk_X509_NAME_ENTRY_new_null(); |
| 341 | if(!entries) | 366 | if (!entries) |
| 342 | goto err; | 367 | goto err; |
| 343 | if(!sk_STACK_OF_X509_NAME_ENTRY_push(intname, entries)) | 368 | if (!sk_STACK_OF_X509_NAME_ENTRY_push(intname, entries)) |
| 344 | goto err; | 369 | goto err; |
| 345 | set = entry->set; | 370 | set = entry->set; |
| 346 | } | 371 | } |
| @@ -348,33 +373,27 @@ static int x509_name_canon(X509_NAME *a) | |||
| 348 | tmpentry->object = OBJ_dup(entry->object); | 373 | tmpentry->object = OBJ_dup(entry->object); |
| 349 | if (!asn1_string_canon(tmpentry->value, entry->value)) | 374 | if (!asn1_string_canon(tmpentry->value, entry->value)) |
| 350 | goto err; | 375 | goto err; |
| 351 | if(!sk_X509_NAME_ENTRY_push(entries, tmpentry)) | 376 | if (!sk_X509_NAME_ENTRY_push(entries, tmpentry)) |
| 352 | goto err; | 377 | goto err; |
| 353 | tmpentry = NULL; | 378 | tmpentry = NULL; |
| 354 | } | 379 | } |
| 355 | 380 | ||
| 356 | /* Finally generate encoding */ | 381 | /* Finally generate encoding */ |
| 357 | |||
| 358 | a->canon_enclen = i2d_name_canon(intname, NULL); | 382 | a->canon_enclen = i2d_name_canon(intname, NULL); |
| 359 | |||
| 360 | p = malloc(a->canon_enclen); | 383 | p = malloc(a->canon_enclen); |
| 361 | |||
| 362 | if (!p) | 384 | if (!p) |
| 363 | goto err; | 385 | goto err; |
| 364 | |||
| 365 | a->canon_enc = p; | 386 | a->canon_enc = p; |
| 366 | |||
| 367 | i2d_name_canon(intname, &p); | 387 | i2d_name_canon(intname, &p); |
| 368 | |||
| 369 | ret = 1; | 388 | ret = 1; |
| 370 | 389 | ||
| 371 | err: | 390 | err: |
| 372 | 391 | ||
| 373 | if (tmpentry) | 392 | if (tmpentry) |
| 374 | X509_NAME_ENTRY_free(tmpentry); | 393 | X509_NAME_ENTRY_free(tmpentry); |
| 375 | if (intname) | 394 | if (intname) |
| 376 | sk_STACK_OF_X509_NAME_ENTRY_pop_free(intname, | 395 | sk_STACK_OF_X509_NAME_ENTRY_pop_free(intname, |
| 377 | local_sk_X509_NAME_ENTRY_pop_free); | 396 | local_sk_X509_NAME_ENTRY_pop_free); |
| 378 | return ret; | 397 | return ret; |
| 379 | } | 398 | } |
| 380 | 399 | ||
| @@ -384,9 +403,10 @@ static int x509_name_canon(X509_NAME *a) | |||
| 384 | (B_ASN1_UTF8STRING | B_ASN1_BMPSTRING | B_ASN1_UNIVERSALSTRING \ | 403 | (B_ASN1_UTF8STRING | B_ASN1_BMPSTRING | B_ASN1_UNIVERSALSTRING \ |
| 385 | | B_ASN1_PRINTABLESTRING | B_ASN1_T61STRING | B_ASN1_IA5STRING \ | 404 | | B_ASN1_PRINTABLESTRING | B_ASN1_T61STRING | B_ASN1_IA5STRING \ |
| 386 | | B_ASN1_VISIBLESTRING) | 405 | | B_ASN1_VISIBLESTRING) |
| 387 | |||
| 388 | 406 | ||
| 389 | static int asn1_string_canon(ASN1_STRING *out, ASN1_STRING *in) | 407 | |
| 408 | static int | ||
| 409 | asn1_string_canon(ASN1_STRING *out, ASN1_STRING *in) | ||
| 390 | { | 410 | { |
| 391 | unsigned char *to, *from; | 411 | unsigned char *to, *from; |
| 392 | int len, i; | 412 | int len, i; |
| @@ -415,7 +435,7 @@ static int asn1_string_canon(ASN1_STRING *out, ASN1_STRING *in) | |||
| 415 | */ | 435 | */ |
| 416 | 436 | ||
| 417 | /* Ignore leading spaces */ | 437 | /* Ignore leading spaces */ |
| 418 | while((len > 0) && !(*from & 0x80) && isspace(*from)) { | 438 | while ((len > 0) && !(*from & 0x80) && isspace(*from)) { |
| 419 | from++; | 439 | from++; |
| 420 | len--; | 440 | len--; |
| 421 | } | 441 | } |
| @@ -431,24 +451,24 @@ static int asn1_string_canon(ASN1_STRING *out, ASN1_STRING *in) | |||
| 431 | to = out->data; | 451 | to = out->data; |
| 432 | 452 | ||
| 433 | i = 0; | 453 | i = 0; |
| 434 | while(i < len) { | 454 | while (i < len) { |
| 435 | /* If MSB set just copy across */ | 455 | /* If MSB set just copy across */ |
| 436 | if (*from & 0x80) { | 456 | if (*from & 0x80) { |
| 437 | *to++ = *from++; | 457 | *to++ = *from++; |
| 438 | i++; | 458 | i++; |
| 439 | } | 459 | } |
| 440 | /* Collapse multiple spaces */ | 460 | /* Collapse multiple spaces */ |
| 441 | else if (isspace(*from)) { | 461 | else if (isspace(*from)) { |
| 442 | /* Copy one space across */ | 462 | /* Copy one space across */ |
| 443 | *to++ = ' '; | 463 | *to++ = ' '; |
| 444 | /* Ignore subsequent spaces. Note: don't need to | 464 | /* Ignore subsequent spaces. Note: don't need to |
| 445 | * check len here because we know the last | 465 | * check len here because we know the last |
| 446 | * character is a non-space so we can't overflow. | 466 | * character is a non-space so we can't overflow. |
| 447 | */ | 467 | */ |
| 448 | do { | 468 | do { |
| 449 | from++; | 469 | from++; |
| 450 | i++; | 470 | i++; |
| 451 | } while(!(*from & 0x80) && isspace(*from)); | 471 | } while (!(*from & 0x80) && isspace(*from)); |
| 452 | } else { | 472 | } else { |
| 453 | *to++ = tolower(*from); | 473 | *to++ = tolower(*from); |
| 454 | from++; | 474 | from++; |
| @@ -459,11 +479,10 @@ static int asn1_string_canon(ASN1_STRING *out, ASN1_STRING *in) | |||
| 459 | out->length = to - out->data; | 479 | out->length = to - out->data; |
| 460 | 480 | ||
| 461 | return 1; | 481 | return 1; |
| 462 | |||
| 463 | } | 482 | } |
| 464 | 483 | ||
| 465 | static int i2d_name_canon(STACK_OF(STACK_OF_X509_NAME_ENTRY) *_intname, | 484 | static int |
| 466 | unsigned char **in) | 485 | i2d_name_canon(STACK_OF(STACK_OF_X509_NAME_ENTRY) *_intname, unsigned char **in) |
| 467 | { | 486 | { |
| 468 | int i, len, ltmp; | 487 | int i, len, ltmp; |
| 469 | ASN1_VALUE *v; | 488 | ASN1_VALUE *v; |
| @@ -473,7 +492,7 @@ static int i2d_name_canon(STACK_OF(STACK_OF_X509_NAME_ENTRY) *_intname, | |||
| 473 | for (i = 0; i < sk_ASN1_VALUE_num(intname); i++) { | 492 | for (i = 0; i < sk_ASN1_VALUE_num(intname); i++) { |
| 474 | v = sk_ASN1_VALUE_value(intname, i); | 493 | v = sk_ASN1_VALUE_value(intname, i); |
| 475 | ltmp = ASN1_item_ex_i2d(&v, in, | 494 | ltmp = ASN1_item_ex_i2d(&v, in, |
| 476 | ASN1_ITEM_rptr(X509_NAME_ENTRIES), -1, -1); | 495 | ASN1_ITEM_rptr(X509_NAME_ENTRIES), -1, -1); |
| 477 | if (ltmp < 0) | 496 | if (ltmp < 0) |
| 478 | return ltmp; | 497 | return ltmp; |
| 479 | len += ltmp; | 498 | len += ltmp; |
| @@ -481,21 +500,23 @@ static int i2d_name_canon(STACK_OF(STACK_OF_X509_NAME_ENTRY) *_intname, | |||
| 481 | return len; | 500 | return len; |
| 482 | } | 501 | } |
| 483 | 502 | ||
| 484 | int X509_NAME_set(X509_NAME **xn, X509_NAME *name) | 503 | int |
| 504 | X509_NAME_set(X509_NAME **xn, X509_NAME *name) | ||
| 485 | { | 505 | { |
| 486 | X509_NAME *in; | 506 | X509_NAME *in; |
| 487 | 507 | ||
| 488 | if (!xn || !name) return(0); | 508 | if (!xn || !name) |
| 509 | return (0); | ||
| 489 | 510 | ||
| 490 | if (*xn != name) { | 511 | if (*xn != name) { |
| 491 | in=X509_NAME_dup(name); | 512 | in = X509_NAME_dup(name); |
| 492 | if (in != NULL) { | 513 | if (in != NULL) { |
| 493 | X509_NAME_free(*xn); | 514 | X509_NAME_free(*xn); |
| 494 | *xn=in; | 515 | *xn = in; |
| 495 | } | 516 | } |
| 496 | } | 517 | } |
| 497 | return(*xn != NULL); | 518 | return (*xn != NULL); |
| 498 | } | 519 | } |
| 499 | 520 | ||
| 500 | IMPLEMENT_STACK_OF(X509_NAME_ENTRY) | 521 | IMPLEMENT_STACK_OF(X509_NAME_ENTRY) |
| 501 | IMPLEMENT_ASN1_SET_OF(X509_NAME_ENTRY) | 522 | IMPLEMENT_ASN1_SET_OF(X509_NAME_ENTRY) |
