summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authortedu <>2015-03-19 14:00:22 +0000
committertedu <>2015-03-19 14:00:22 +0000
commit325847dec91a0775a2c9806147ab783c0737cc84 (patch)
treee1849327a9d18d917e7698c775d31f7ae7c4e34f
parent5018b21486fe3d91084c0d32a86d1240d832e25e (diff)
downloadopenbsd-325847dec91a0775a2c9806147ab783c0737cc84.tar.gz
openbsd-325847dec91a0775a2c9806147ab783c0737cc84.tar.bz2
openbsd-325847dec91a0775a2c9806147ab783c0737cc84.zip
Fix several crash causing defects from OpenSSL.
These include: CVE-2015-0209 - Use After Free following d2i_ECPrivatekey error CVE-2015-0286 - Segmentation fault in ASN1_TYPE_cmp CVE-2015-0287 - ASN.1 structure reuse memory corruption CVE-2015-0289 - PKCS7 NULL pointer dereferences Several other issues did not apply or were already fixed. Refer to https://www.openssl.org/news/secadv_20150319.txt joint work with beck, doug, guenther, jsing, miod
-rw-r--r--src/lib/libcrypto/asn1/a_int.c6
-rw-r--r--src/lib/libcrypto/asn1/a_set.c4
-rw-r--r--src/lib/libcrypto/asn1/a_type.c6
-rw-r--r--src/lib/libcrypto/asn1/d2i_pr.c4
-rw-r--r--src/lib/libcrypto/asn1/d2i_pu.c4
-rw-r--r--src/lib/libcrypto/asn1/n_pkey.c10
-rw-r--r--src/lib/libcrypto/asn1/tasn_dec.c27
-rw-r--r--src/lib/libcrypto/asn1/x_x509.c16
-rw-r--r--src/lib/libcrypto/ec/ec_asn1.c41
-rw-r--r--src/lib/libcrypto/pkcs7/pk7_doit.c98
-rw-r--r--src/lib/libcrypto/pkcs7/pk7_lib.c4
-rw-r--r--src/lib/libssl/d1_lib.c5
-rw-r--r--src/lib/libssl/src/crypto/asn1/a_int.c6
-rw-r--r--src/lib/libssl/src/crypto/asn1/a_set.c4
-rw-r--r--src/lib/libssl/src/crypto/asn1/a_type.c6
-rw-r--r--src/lib/libssl/src/crypto/asn1/d2i_pr.c4
-rw-r--r--src/lib/libssl/src/crypto/asn1/d2i_pu.c4
-rw-r--r--src/lib/libssl/src/crypto/asn1/n_pkey.c10
-rw-r--r--src/lib/libssl/src/crypto/asn1/tasn_dec.c27
-rw-r--r--src/lib/libssl/src/crypto/asn1/x_x509.c16
-rw-r--r--src/lib/libssl/src/crypto/ec/ec_asn1.c41
-rw-r--r--src/lib/libssl/src/crypto/pkcs7/pk7_doit.c98
-rw-r--r--src/lib/libssl/src/crypto/pkcs7/pk7_lib.c4
-rw-r--r--src/lib/libssl/src/ssl/d1_lib.c5
24 files changed, 324 insertions, 126 deletions
diff --git a/src/lib/libcrypto/asn1/a_int.c b/src/lib/libcrypto/asn1/a_int.c
index fe6ce5ee9f..af5d64d056 100644
--- a/src/lib/libcrypto/asn1/a_int.c
+++ b/src/lib/libcrypto/asn1/a_int.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: a_int.c,v 1.25 2015/02/10 08:33:10 jsing Exp $ */ 1/* $OpenBSD: a_int.c,v 1.26 2015/03/19 14:00:22 tedu Exp $ */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved. 3 * All rights reserved.
4 * 4 *
@@ -268,7 +268,7 @@ c2i_ASN1_INTEGER(ASN1_INTEGER **a, const unsigned char **pp, long len)
268 268
269err: 269err:
270 ASN1err(ASN1_F_C2I_ASN1_INTEGER, i); 270 ASN1err(ASN1_F_C2I_ASN1_INTEGER, i);
271 if ((ret != NULL) && ((a == NULL) || (*a != ret))) 271 if (a == NULL || *a != ret)
272 M_ASN1_INTEGER_free(ret); 272 M_ASN1_INTEGER_free(ret);
273 return (NULL); 273 return (NULL);
274} 274}
@@ -335,7 +335,7 @@ d2i_ASN1_UINTEGER(ASN1_INTEGER **a, const unsigned char **pp, long length)
335 335
336err: 336err:
337 ASN1err(ASN1_F_D2I_ASN1_UINTEGER, i); 337 ASN1err(ASN1_F_D2I_ASN1_UINTEGER, i);
338 if ((ret != NULL) && ((a == NULL) || (*a != ret))) 338 if (a == NULL || *a != ret)
339 M_ASN1_INTEGER_free(ret); 339 M_ASN1_INTEGER_free(ret);
340 return (NULL); 340 return (NULL);
341} 341}
diff --git a/src/lib/libcrypto/asn1/a_set.c b/src/lib/libcrypto/asn1/a_set.c
index ba4f28be34..63d55c3714 100644
--- a/src/lib/libcrypto/asn1/a_set.c
+++ b/src/lib/libcrypto/asn1/a_set.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: a_set.c,v 1.16 2014/07/11 08:44:47 jsing Exp $ */ 1/* $OpenBSD: a_set.c,v 1.17 2015/03/19 14:00:22 tedu Exp $ */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved. 3 * All rights reserved.
4 * 4 *
@@ -225,7 +225,7 @@ d2i_ASN1_SET(STACK_OF(OPENSSL_BLOCK) **a, const unsigned char **pp, long length,
225 return ret; 225 return ret;
226 226
227err: 227err:
228 if (ret != NULL && (a == NULL || *a != ret)) { 228 if (a == NULL || *a != ret) {
229 if (free_func != NULL) 229 if (free_func != NULL)
230 sk_OPENSSL_BLOCK_pop_free(ret, free_func); 230 sk_OPENSSL_BLOCK_pop_free(ret, free_func);
231 else 231 else
diff --git a/src/lib/libcrypto/asn1/a_type.c b/src/lib/libcrypto/asn1/a_type.c
index 7c732cfec5..38b3c65beb 100644
--- a/src/lib/libcrypto/asn1/a_type.c
+++ b/src/lib/libcrypto/asn1/a_type.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: a_type.c,v 1.15 2015/02/10 08:33:10 jsing Exp $ */ 1/* $OpenBSD: a_type.c,v 1.16 2015/03/19 14:00:22 tedu Exp $ */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved. 3 * All rights reserved.
4 * 4 *
@@ -119,7 +119,9 @@ ASN1_TYPE_cmp(ASN1_TYPE *a, ASN1_TYPE *b)
119 case V_ASN1_OBJECT: 119 case V_ASN1_OBJECT:
120 result = OBJ_cmp(a->value.object, b->value.object); 120 result = OBJ_cmp(a->value.object, b->value.object);
121 break; 121 break;
122 122 case V_ASN1_BOOLEAN:
123 result = a->value.boolean - b->value.boolean;
124 break;
123 case V_ASN1_NULL: 125 case V_ASN1_NULL:
124 result = 0; /* They do not have content. */ 126 result = 0; /* They do not have content. */
125 break; 127 break;
diff --git a/src/lib/libcrypto/asn1/d2i_pr.c b/src/lib/libcrypto/asn1/d2i_pr.c
index 14f08e1380..68d02177c4 100644
--- a/src/lib/libcrypto/asn1/d2i_pr.c
+++ b/src/lib/libcrypto/asn1/d2i_pr.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: d2i_pr.c,v 1.13 2015/02/11 03:19:37 doug Exp $ */ 1/* $OpenBSD: d2i_pr.c,v 1.14 2015/03/19 14:00:22 tedu Exp $ */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved. 3 * All rights reserved.
4 * 4 *
@@ -118,7 +118,7 @@ d2i_PrivateKey(int type, EVP_PKEY **a, const unsigned char **pp, long length)
118 return (ret); 118 return (ret);
119 119
120err: 120err:
121 if ((ret != NULL) && ((a == NULL) || (*a != ret))) 121 if (a == NULL || *a != ret)
122 EVP_PKEY_free(ret); 122 EVP_PKEY_free(ret);
123 return (NULL); 123 return (NULL);
124} 124}
diff --git a/src/lib/libcrypto/asn1/d2i_pu.c b/src/lib/libcrypto/asn1/d2i_pu.c
index df6fea4af5..e917356254 100644
--- a/src/lib/libcrypto/asn1/d2i_pu.c
+++ b/src/lib/libcrypto/asn1/d2i_pu.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: d2i_pu.c,v 1.12 2014/07/11 08:44:47 jsing Exp $ */ 1/* $OpenBSD: d2i_pu.c,v 1.13 2015/03/19 14:00:22 tedu Exp $ */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved. 3 * All rights reserved.
4 * 4 *
@@ -130,7 +130,7 @@ d2i_PublicKey(int type, EVP_PKEY **a, const unsigned char **pp, long length)
130 return (ret); 130 return (ret);
131 131
132err: 132err:
133 if ((ret != NULL) && ((a == NULL) || (*a != ret))) 133 if (a == NULL || *a != ret)
134 EVP_PKEY_free(ret); 134 EVP_PKEY_free(ret);
135 return (NULL); 135 return (NULL);
136} 136}
diff --git a/src/lib/libcrypto/asn1/n_pkey.c b/src/lib/libcrypto/asn1/n_pkey.c
index bb369fde6e..d3a7431356 100644
--- a/src/lib/libcrypto/asn1/n_pkey.c
+++ b/src/lib/libcrypto/asn1/n_pkey.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: n_pkey.c,v 1.25 2015/02/11 04:00:39 jsing Exp $ */ 1/* $OpenBSD: n_pkey.c,v 1.26 2015/03/19 14:00:22 tedu Exp $ */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved. 3 * All rights reserved.
4 * 4 *
@@ -340,11 +340,11 @@ d2i_RSA_NET(RSA **a, const unsigned char **pp, long length,
340 return NULL; 340 return NULL;
341 } 341 }
342 342
343 if ((enckey->os->length != 11) || (strncmp("private-key", 343 /* XXX 11 == strlen("private-key") */
344 (char *)enckey->os->data, 11) != 0)) { 344 if (enckey->os->length != 11 ||
345 memcmp("private-key", enckey->os->data, 11) != 0) {
345 ASN1err(ASN1_F_D2I_RSA_NET, ASN1_R_PRIVATE_KEY_HEADER_MISSING); 346 ASN1err(ASN1_F_D2I_RSA_NET, ASN1_R_PRIVATE_KEY_HEADER_MISSING);
346 NETSCAPE_ENCRYPTED_PKEY_free(enckey); 347 goto err;
347 return NULL;
348 } 348 }
349 if (OBJ_obj2nid(enckey->enckey->algor->algorithm) != NID_rc4) { 349 if (OBJ_obj2nid(enckey->enckey->algor->algorithm) != NID_rc4) {
350 ASN1err(ASN1_F_D2I_RSA_NET, 350 ASN1err(ASN1_F_D2I_RSA_NET,
diff --git a/src/lib/libcrypto/asn1/tasn_dec.c b/src/lib/libcrypto/asn1/tasn_dec.c
index 791a10a9c0..7d61a6a233 100644
--- a/src/lib/libcrypto/asn1/tasn_dec.c
+++ b/src/lib/libcrypto/asn1/tasn_dec.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: tasn_dec.c,v 1.25 2015/02/14 15:23:57 miod Exp $ */ 1/* $OpenBSD: tasn_dec.c,v 1.26 2015/03/19 14:00:22 tedu Exp $ */
2/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL 2/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3 * project 2000. 3 * project 2000.
4 */ 4 */
@@ -238,8 +238,16 @@ ASN1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, long len,
238 if (asn1_cb && !asn1_cb(ASN1_OP_D2I_PRE, pval, it, NULL)) 238 if (asn1_cb && !asn1_cb(ASN1_OP_D2I_PRE, pval, it, NULL))
239 goto auxerr; 239 goto auxerr;
240 240
241 /* Allocate structure */ 241 if (*pval) {
242 if (!*pval && !ASN1_item_ex_new(pval, it)) { 242 /* Free up and zero CHOICE value if initialised */
243 i = asn1_get_choice_selector(pval, it);
244 if ((i >= 0) && (i < it->tcount)) {
245 tt = it->templates + i;
246 pchptr = asn1_get_field_ptr(pval, tt);
247 ASN1_template_free(pchptr, tt);
248 asn1_set_choice_selector(pval, -1, it);
249 }
250 } else if (!ASN1_item_ex_new(pval, it)) {
243 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, 251 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
244 ERR_R_NESTED_ASN1_ERROR); 252 ERR_R_NESTED_ASN1_ERROR);
245 goto err; 253 goto err;
@@ -325,6 +333,19 @@ ASN1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, long len,
325 if (asn1_cb && !asn1_cb(ASN1_OP_D2I_PRE, pval, it, NULL)) 333 if (asn1_cb && !asn1_cb(ASN1_OP_D2I_PRE, pval, it, NULL))
326 goto auxerr; 334 goto auxerr;
327 335
336 /* Free up and zero any ADB found */
337 for (i = 0, tt = it->templates; i < it->tcount; i++, tt++) {
338 if (tt->flags & ASN1_TFLG_ADB_MASK) {
339 const ASN1_TEMPLATE *seqtt;
340 ASN1_VALUE **pseqval;
341 seqtt = asn1_do_adb(pval, tt, 1);
342 if (!seqtt)
343 goto err;
344 pseqval = asn1_get_field_ptr(pval, seqtt);
345 ASN1_template_free(pseqval, seqtt);
346 }
347 }
348
328 /* Get each field entry */ 349 /* Get each field entry */
329 for (i = 0, tt = it->templates; i < it->tcount; i++, tt++) { 350 for (i = 0, tt = it->templates; i < it->tcount; i++, tt++) {
330 const ASN1_TEMPLATE *seqtt; 351 const ASN1_TEMPLATE *seqtt;
diff --git a/src/lib/libcrypto/asn1/x_x509.c b/src/lib/libcrypto/asn1/x_x509.c
index 70d38221b6..168c2c0fcd 100644
--- a/src/lib/libcrypto/asn1/x_x509.c
+++ b/src/lib/libcrypto/asn1/x_x509.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: x_x509.c,v 1.23 2015/02/11 04:00:39 jsing Exp $ */ 1/* $OpenBSD: x_x509.c,v 1.24 2015/03/19 14:00:22 tedu Exp $ */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved. 3 * All rights reserved.
4 * 4 *
@@ -313,16 +313,20 @@ d2i_X509_AUX(X509 **a, const unsigned char **pp, long length)
313 313
314 /* Save start position */ 314 /* Save start position */
315 q = *pp; 315 q = *pp;
316 ret = d2i_X509(a, pp, length); 316 ret = d2i_X509(NULL, pp, length);
317 /* If certificate unreadable then forget it */ 317 /* If certificate unreadable then forget it */
318 if (!ret) 318 if (!ret)
319 return NULL; 319 return NULL;
320 /* update length */ 320 /* update length */
321 length -= *pp - q; 321 length -= *pp - q;
322 if (!length) 322 if (length > 0) {
323 return ret; 323 if (!d2i_X509_CERT_AUX(&ret->aux, pp, length))
324 if (!d2i_X509_CERT_AUX(&ret->aux, pp, length)) 324 goto err;
325 goto err; 325 }
326 if (a != NULL) {
327 X509_free(*a);
328 *a = ret;
329 }
326 return ret; 330 return ret;
327 331
328err: 332err:
diff --git a/src/lib/libcrypto/ec/ec_asn1.c b/src/lib/libcrypto/ec/ec_asn1.c
index c0ef6f40e4..f01008ec43 100644
--- a/src/lib/libcrypto/ec/ec_asn1.c
+++ b/src/lib/libcrypto/ec/ec_asn1.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: ec_asn1.c,v 1.12 2015/02/10 05:43:09 jsing Exp $ */ 1/* $OpenBSD: ec_asn1.c,v 1.13 2015/03/19 14:00:22 tedu Exp $ */
2/* 2/*
3 * Written by Nils Larsch for the OpenSSL project. 3 * Written by Nils Larsch for the OpenSSL project.
4 */ 4 */
@@ -999,19 +999,19 @@ d2i_ECPKParameters(EC_GROUP ** a, const unsigned char **in, long len)
999 999
1000 if ((params = d2i_ECPKPARAMETERS(NULL, in, len)) == NULL) { 1000 if ((params = d2i_ECPKPARAMETERS(NULL, in, len)) == NULL) {
1001 ECerr(EC_F_D2I_ECPKPARAMETERS, EC_R_D2I_ECPKPARAMETERS_FAILURE); 1001 ECerr(EC_F_D2I_ECPKPARAMETERS, EC_R_D2I_ECPKPARAMETERS_FAILURE);
1002 ECPKPARAMETERS_free(params); 1002 goto err;
1003 return NULL;
1004 } 1003 }
1005 if ((group = ec_asn1_pkparameters2group(params)) == NULL) { 1004 if ((group = ec_asn1_pkparameters2group(params)) == NULL) {
1006 ECerr(EC_F_D2I_ECPKPARAMETERS, EC_R_PKPARAMETERS2GROUP_FAILURE); 1005 ECerr(EC_F_D2I_ECPKPARAMETERS, EC_R_PKPARAMETERS2GROUP_FAILURE);
1007 ECPKPARAMETERS_free(params); 1006 goto err;
1008 return NULL;
1009 } 1007 }
1010 if (a && *a) 1008
1009 if (a != NULL) {
1011 EC_GROUP_clear_free(*a); 1010 EC_GROUP_clear_free(*a);
1012 if (a)
1013 *a = group; 1011 *a = group;
1012 }
1014 1013
1014err:
1015 ECPKPARAMETERS_free(params); 1015 ECPKPARAMETERS_free(params);
1016 return (group); 1016 return (group);
1017} 1017}
@@ -1039,7 +1039,6 @@ i2d_ECPKParameters(const EC_GROUP * a, unsigned char **out)
1039EC_KEY * 1039EC_KEY *
1040d2i_ECPrivateKey(EC_KEY ** a, const unsigned char **in, long len) 1040d2i_ECPrivateKey(EC_KEY ** a, const unsigned char **in, long len)
1041{ 1041{
1042 int ok = 0;
1043 EC_KEY *ret = NULL; 1042 EC_KEY *ret = NULL;
1044 EC_PRIVATEKEY *priv_key = NULL; 1043 EC_PRIVATEKEY *priv_key = NULL;
1045 1044
@@ -1054,12 +1053,9 @@ d2i_ECPrivateKey(EC_KEY ** a, const unsigned char **in, long len)
1054 } 1053 }
1055 if (a == NULL || *a == NULL) { 1054 if (a == NULL || *a == NULL) {
1056 if ((ret = EC_KEY_new()) == NULL) { 1055 if ((ret = EC_KEY_new()) == NULL) {
1057 ECerr(EC_F_D2I_ECPRIVATEKEY, 1056 ECerr(EC_F_D2I_ECPRIVATEKEY, ERR_R_MALLOC_FAILURE);
1058 ERR_R_MALLOC_FAILURE);
1059 goto err; 1057 goto err;
1060 } 1058 }
1061 if (a)
1062 *a = ret;
1063 } else 1059 } else
1064 ret = *a; 1060 ret = *a;
1065 1061
@@ -1109,17 +1105,19 @@ d2i_ECPrivateKey(EC_KEY ** a, const unsigned char **in, long len)
1109 goto err; 1105 goto err;
1110 } 1106 }
1111 } 1107 }
1112 ok = 1; 1108
1109 EC_PRIVATEKEY_free(priv_key);
1110 if (a != NULL)
1111 *a = ret;
1112 return (ret);
1113
1113err: 1114err:
1114 if (!ok) { 1115 if (a == NULL || *a != ret)
1115 if (ret) 1116 EC_KEY_free(ret);
1116 EC_KEY_free(ret);
1117 ret = NULL;
1118 }
1119 if (priv_key) 1117 if (priv_key)
1120 EC_PRIVATEKEY_free(priv_key); 1118 EC_PRIVATEKEY_free(priv_key);
1121 1119
1122 return (ret); 1120 return (NULL);
1123} 1121}
1124 1122
1125int 1123int
@@ -1232,8 +1230,6 @@ d2i_ECParameters(EC_KEY ** a, const unsigned char **in, long len)
1232 ECerr(EC_F_D2I_ECPARAMETERS, ERR_R_MALLOC_FAILURE); 1230 ECerr(EC_F_D2I_ECPARAMETERS, ERR_R_MALLOC_FAILURE);
1233 return NULL; 1231 return NULL;
1234 } 1232 }
1235 if (a)
1236 *a = ret;
1237 } else 1233 } else
1238 ret = *a; 1234 ret = *a;
1239 1235
@@ -1241,6 +1237,9 @@ d2i_ECParameters(EC_KEY ** a, const unsigned char **in, long len)
1241 ECerr(EC_F_D2I_ECPARAMETERS, ERR_R_EC_LIB); 1237 ECerr(EC_F_D2I_ECPARAMETERS, ERR_R_EC_LIB);
1242 return NULL; 1238 return NULL;
1243 } 1239 }
1240
1241 if (a != NULL)
1242 *a = ret;
1244 return ret; 1243 return ret;
1245} 1244}
1246 1245
diff --git a/src/lib/libcrypto/pkcs7/pk7_doit.c b/src/lib/libcrypto/pkcs7/pk7_doit.c
index 252fab04d7..d0cf84df80 100644
--- a/src/lib/libcrypto/pkcs7/pk7_doit.c
+++ b/src/lib/libcrypto/pkcs7/pk7_doit.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: pk7_doit.c,v 1.31 2015/02/07 13:19:15 doug Exp $ */ 1/* $OpenBSD: pk7_doit.c,v 1.32 2015/03/19 14:00:22 tedu Exp $ */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved. 3 * All rights reserved.
4 * 4 *
@@ -261,6 +261,28 @@ PKCS7_dataInit(PKCS7 *p7, BIO *bio)
261 PKCS7_RECIP_INFO *ri = NULL; 261 PKCS7_RECIP_INFO *ri = NULL;
262 ASN1_OCTET_STRING *os = NULL; 262 ASN1_OCTET_STRING *os = NULL;
263 263
264 if (p7 == NULL) {
265 PKCS7err(PKCS7_F_PKCS7_DATAINIT, PKCS7_R_INVALID_NULL_POINTER);
266 return NULL;
267 }
268
269 /*
270 * The content field in the PKCS7 ContentInfo is optional,
271 * but that really only applies to inner content (precisely,
272 * detached signatures).
273 *
274 * When reading content, missing outer content is therefore
275 * treated as an error.
276 *
277 * When creating content, PKCS7_content_new() must be called
278 * before calling this method, so a NULL p7->d is always
279 * an error.
280 */
281 if (p7->d.ptr == NULL) {
282 PKCS7err(PKCS7_F_PKCS7_DATAINIT, PKCS7_R_NO_CONTENT);
283 return NULL;
284 }
285
264 i = OBJ_obj2nid(p7->type); 286 i = OBJ_obj2nid(p7->type);
265 p7->state = PKCS7_S_HEADER; 287 p7->state = PKCS7_S_HEADER;
266 288
@@ -417,6 +439,17 @@ PKCS7_dataDecode(PKCS7 *p7, EVP_PKEY *pkey, BIO *in_bio, X509 *pcert)
417 unsigned char *ek = NULL, *tkey = NULL; 439 unsigned char *ek = NULL, *tkey = NULL;
418 int eklen = 0, tkeylen = 0; 440 int eklen = 0, tkeylen = 0;
419 441
442 if (p7 == NULL) {
443 PKCS7err(PKCS7_F_PKCS7_DATADECODE,
444 PKCS7_R_INVALID_NULL_POINTER);
445 return NULL;
446 }
447
448 if (p7->d.ptr == NULL) {
449 PKCS7err(PKCS7_F_PKCS7_DATADECODE, PKCS7_R_NO_CONTENT);
450 return NULL;
451 }
452
420 i = OBJ_obj2nid(p7->type); 453 i = OBJ_obj2nid(p7->type);
421 p7->state = PKCS7_S_HEADER; 454 p7->state = PKCS7_S_HEADER;
422 455
@@ -691,6 +724,17 @@ PKCS7_dataFinal(PKCS7 *p7, BIO *bio)
691 STACK_OF(PKCS7_SIGNER_INFO) *si_sk = NULL; 724 STACK_OF(PKCS7_SIGNER_INFO) *si_sk = NULL;
692 ASN1_OCTET_STRING *os = NULL; 725 ASN1_OCTET_STRING *os = NULL;
693 726
727 if (p7 == NULL) {
728 PKCS7err(PKCS7_F_PKCS7_DATAFINAL,
729 PKCS7_R_INVALID_NULL_POINTER);
730 return 0;
731 }
732
733 if (p7->d.ptr == NULL) {
734 PKCS7err(PKCS7_F_PKCS7_DATAFINAL, PKCS7_R_NO_CONTENT);
735 return 0;
736 }
737
694 EVP_MD_CTX_init(&ctx_tmp); 738 EVP_MD_CTX_init(&ctx_tmp);
695 i = OBJ_obj2nid(p7->type); 739 i = OBJ_obj2nid(p7->type);
696 p7->state = PKCS7_S_HEADER; 740 p7->state = PKCS7_S_HEADER;
@@ -736,6 +780,7 @@ PKCS7_dataFinal(PKCS7 *p7, BIO *bio)
736 /* If detached data then the content is excluded */ 780 /* If detached data then the content is excluded */
737 if (PKCS7_type_is_data(p7->d.sign->contents) && p7->detached) { 781 if (PKCS7_type_is_data(p7->d.sign->contents) && p7->detached) {
738 M_ASN1_OCTET_STRING_free(os); 782 M_ASN1_OCTET_STRING_free(os);
783 os = NULL;
739 p7->d.sign->contents->d.data = NULL; 784 p7->d.sign->contents->d.data = NULL;
740 } 785 }
741 break; 786 break;
@@ -750,6 +795,7 @@ PKCS7_dataFinal(PKCS7 *p7, BIO *bio)
750 if (PKCS7_type_is_data(p7->d.digest->contents) && 795 if (PKCS7_type_is_data(p7->d.digest->contents) &&
751 p7->detached) { 796 p7->detached) {
752 M_ASN1_OCTET_STRING_free(os); 797 M_ASN1_OCTET_STRING_free(os);
798 os = NULL;
753 p7->d.digest->contents->d.data = NULL; 799 p7->d.digest->contents->d.data = NULL;
754 } 800 }
755 break; 801 break;
@@ -815,22 +861,32 @@ PKCS7_dataFinal(PKCS7 *p7, BIO *bio)
815 M_ASN1_OCTET_STRING_set(p7->d.digest->digest, md_data, md_len); 861 M_ASN1_OCTET_STRING_set(p7->d.digest->digest, md_data, md_len);
816 } 862 }
817 863
818 if (!PKCS7_is_detached(p7) && !(os->flags & ASN1_STRING_FLAG_NDEF)) { 864 if (!PKCS7_is_detached(p7)) {
819 char *cont; 865 /*
820 long contlen; 866 * NOTE: only reach os == NULL here because detached
821 btmp = BIO_find_type(bio, BIO_TYPE_MEM); 867 * digested data support is broken?
822 if (btmp == NULL) { 868 */
823 PKCS7err(PKCS7_F_PKCS7_DATAFINAL, 869 if (os == NULL)
824 PKCS7_R_UNABLE_TO_FIND_MEM_BIO);
825 goto err; 870 goto err;
871 if (!(os->flags & ASN1_STRING_FLAG_NDEF)) {
872 char *cont;
873 long contlen;
874
875 btmp = BIO_find_type(bio, BIO_TYPE_MEM);
876 if (btmp == NULL) {
877 PKCS7err(PKCS7_F_PKCS7_DATAFINAL,
878 PKCS7_R_UNABLE_TO_FIND_MEM_BIO);
879 goto err;
880 }
881 contlen = BIO_get_mem_data(btmp, &cont);
882 /*
883 * Mark the BIO read only then we can use its copy
884 * of the data instead of making an extra copy.
885 */
886 BIO_set_flags(btmp, BIO_FLAGS_MEM_RDONLY);
887 BIO_set_mem_eof_return(btmp, 0);
888 ASN1_STRING_set0(os, (unsigned char *)cont, contlen);
826 } 889 }
827 contlen = BIO_get_mem_data(btmp, &cont);
828 /* Mark the BIO read only then we can use its copy of the data
829 * instead of making an extra copy.
830 */
831 BIO_set_flags(btmp, BIO_FLAGS_MEM_RDONLY);
832 BIO_set_mem_eof_return(btmp, 0);
833 ASN1_STRING_set0(os, (unsigned char *)cont, contlen);
834 } 890 }
835 ret = 1; 891 ret = 1;
836err: 892err:
@@ -905,6 +961,17 @@ PKCS7_dataVerify(X509_STORE *cert_store, X509_STORE_CTX *ctx, BIO *bio,
905 STACK_OF(X509) *cert; 961 STACK_OF(X509) *cert;
906 X509 *x509; 962 X509 *x509;
907 963
964 if (p7 == NULL) {
965 PKCS7err(PKCS7_F_PKCS7_DATAVERIFY,
966 PKCS7_R_INVALID_NULL_POINTER);
967 return 0;
968 }
969
970 if (p7->d.ptr == NULL) {
971 PKCS7err(PKCS7_F_PKCS7_DATAVERIFY, PKCS7_R_NO_CONTENT);
972 return 0;
973 }
974
908 if (PKCS7_type_is_signed(p7)) { 975 if (PKCS7_type_is_signed(p7)) {
909 cert = p7->d.sign->cert; 976 cert = p7->d.sign->cert;
910 } else if (PKCS7_type_is_signedAndEnveloped(p7)) { 977 } else if (PKCS7_type_is_signedAndEnveloped(p7)) {
@@ -941,6 +1008,7 @@ PKCS7_dataVerify(X509_STORE *cert_store, X509_STORE_CTX *ctx, BIO *bio,
941 1008
942 return PKCS7_signatureVerify(bio, p7, si, x509); 1009 return PKCS7_signatureVerify(bio, p7, si, x509);
943err: 1010err:
1011
944 return ret; 1012 return ret;
945} 1013}
946 1014
diff --git a/src/lib/libcrypto/pkcs7/pk7_lib.c b/src/lib/libcrypto/pkcs7/pk7_lib.c
index 27370800c9..3eec92e29b 100644
--- a/src/lib/libcrypto/pkcs7/pk7_lib.c
+++ b/src/lib/libcrypto/pkcs7/pk7_lib.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: pk7_lib.c,v 1.14 2014/07/12 16:03:37 miod Exp $ */ 1/* $OpenBSD: pk7_lib.c,v 1.15 2015/03/19 14:00:22 tedu Exp $ */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved. 3 * All rights reserved.
4 * 4 *
@@ -460,6 +460,8 @@ PKCS7_set_digest(PKCS7 *p7, const EVP_MD *md)
460STACK_OF(PKCS7_SIGNER_INFO) * 460STACK_OF(PKCS7_SIGNER_INFO) *
461PKCS7_get_signer_info(PKCS7 *p7) 461PKCS7_get_signer_info(PKCS7 *p7)
462{ 462{
463 if (p7 == NULL || p7->d.ptr == NULL)
464 return (NULL);
463 if (PKCS7_type_is_signed(p7)) { 465 if (PKCS7_type_is_signed(p7)) {
464 return (p7->d.sign->signer_info); 466 return (p7->d.sign->signer_info);
465 } else if (PKCS7_type_is_signedAndEnveloped(p7)) { 467 } else if (PKCS7_type_is_signedAndEnveloped(p7)) {
diff --git a/src/lib/libssl/d1_lib.c b/src/lib/libssl/d1_lib.c
index 9d9a90d76a..dd789ccc70 100644
--- a/src/lib/libssl/d1_lib.c
+++ b/src/lib/libssl/d1_lib.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: d1_lib.c,v 1.27 2015/02/09 10:53:28 jsing Exp $ */ 1/* $OpenBSD: d1_lib.c,v 1.28 2015/03/19 14:00:22 tedu Exp $ */
2/* 2/*
3 * DTLS implementation written by Nagendra Modadugu 3 * DTLS implementation written by Nagendra Modadugu
4 * (nagendra@cs.stanford.edu) for the OpenSSL project 2005. 4 * (nagendra@cs.stanford.edu) for the OpenSSL project 2005.
@@ -443,6 +443,9 @@ dtls1_listen(SSL *s, struct sockaddr *client)
443{ 443{
444 int ret; 444 int ret;
445 445
446 /* Ensure there is no state left over from a previous invocation */
447 SSL_clear(s);
448
446 SSL_set_options(s, SSL_OP_COOKIE_EXCHANGE); 449 SSL_set_options(s, SSL_OP_COOKIE_EXCHANGE);
447 s->d1->listen = 1; 450 s->d1->listen = 1;
448 451
diff --git a/src/lib/libssl/src/crypto/asn1/a_int.c b/src/lib/libssl/src/crypto/asn1/a_int.c
index fe6ce5ee9f..af5d64d056 100644
--- a/src/lib/libssl/src/crypto/asn1/a_int.c
+++ b/src/lib/libssl/src/crypto/asn1/a_int.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: a_int.c,v 1.25 2015/02/10 08:33:10 jsing Exp $ */ 1/* $OpenBSD: a_int.c,v 1.26 2015/03/19 14:00:22 tedu Exp $ */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved. 3 * All rights reserved.
4 * 4 *
@@ -268,7 +268,7 @@ c2i_ASN1_INTEGER(ASN1_INTEGER **a, const unsigned char **pp, long len)
268 268
269err: 269err:
270 ASN1err(ASN1_F_C2I_ASN1_INTEGER, i); 270 ASN1err(ASN1_F_C2I_ASN1_INTEGER, i);
271 if ((ret != NULL) && ((a == NULL) || (*a != ret))) 271 if (a == NULL || *a != ret)
272 M_ASN1_INTEGER_free(ret); 272 M_ASN1_INTEGER_free(ret);
273 return (NULL); 273 return (NULL);
274} 274}
@@ -335,7 +335,7 @@ d2i_ASN1_UINTEGER(ASN1_INTEGER **a, const unsigned char **pp, long length)
335 335
336err: 336err:
337 ASN1err(ASN1_F_D2I_ASN1_UINTEGER, i); 337 ASN1err(ASN1_F_D2I_ASN1_UINTEGER, i);
338 if ((ret != NULL) && ((a == NULL) || (*a != ret))) 338 if (a == NULL || *a != ret)
339 M_ASN1_INTEGER_free(ret); 339 M_ASN1_INTEGER_free(ret);
340 return (NULL); 340 return (NULL);
341} 341}
diff --git a/src/lib/libssl/src/crypto/asn1/a_set.c b/src/lib/libssl/src/crypto/asn1/a_set.c
index ba4f28be34..63d55c3714 100644
--- a/src/lib/libssl/src/crypto/asn1/a_set.c
+++ b/src/lib/libssl/src/crypto/asn1/a_set.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: a_set.c,v 1.16 2014/07/11 08:44:47 jsing Exp $ */ 1/* $OpenBSD: a_set.c,v 1.17 2015/03/19 14:00:22 tedu Exp $ */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved. 3 * All rights reserved.
4 * 4 *
@@ -225,7 +225,7 @@ d2i_ASN1_SET(STACK_OF(OPENSSL_BLOCK) **a, const unsigned char **pp, long length,
225 return ret; 225 return ret;
226 226
227err: 227err:
228 if (ret != NULL && (a == NULL || *a != ret)) { 228 if (a == NULL || *a != ret) {
229 if (free_func != NULL) 229 if (free_func != NULL)
230 sk_OPENSSL_BLOCK_pop_free(ret, free_func); 230 sk_OPENSSL_BLOCK_pop_free(ret, free_func);
231 else 231 else
diff --git a/src/lib/libssl/src/crypto/asn1/a_type.c b/src/lib/libssl/src/crypto/asn1/a_type.c
index 7c732cfec5..38b3c65beb 100644
--- a/src/lib/libssl/src/crypto/asn1/a_type.c
+++ b/src/lib/libssl/src/crypto/asn1/a_type.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: a_type.c,v 1.15 2015/02/10 08:33:10 jsing Exp $ */ 1/* $OpenBSD: a_type.c,v 1.16 2015/03/19 14:00:22 tedu Exp $ */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved. 3 * All rights reserved.
4 * 4 *
@@ -119,7 +119,9 @@ ASN1_TYPE_cmp(ASN1_TYPE *a, ASN1_TYPE *b)
119 case V_ASN1_OBJECT: 119 case V_ASN1_OBJECT:
120 result = OBJ_cmp(a->value.object, b->value.object); 120 result = OBJ_cmp(a->value.object, b->value.object);
121 break; 121 break;
122 122 case V_ASN1_BOOLEAN:
123 result = a->value.boolean - b->value.boolean;
124 break;
123 case V_ASN1_NULL: 125 case V_ASN1_NULL:
124 result = 0; /* They do not have content. */ 126 result = 0; /* They do not have content. */
125 break; 127 break;
diff --git a/src/lib/libssl/src/crypto/asn1/d2i_pr.c b/src/lib/libssl/src/crypto/asn1/d2i_pr.c
index 14f08e1380..68d02177c4 100644
--- a/src/lib/libssl/src/crypto/asn1/d2i_pr.c
+++ b/src/lib/libssl/src/crypto/asn1/d2i_pr.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: d2i_pr.c,v 1.13 2015/02/11 03:19:37 doug Exp $ */ 1/* $OpenBSD: d2i_pr.c,v 1.14 2015/03/19 14:00:22 tedu Exp $ */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved. 3 * All rights reserved.
4 * 4 *
@@ -118,7 +118,7 @@ d2i_PrivateKey(int type, EVP_PKEY **a, const unsigned char **pp, long length)
118 return (ret); 118 return (ret);
119 119
120err: 120err:
121 if ((ret != NULL) && ((a == NULL) || (*a != ret))) 121 if (a == NULL || *a != ret)
122 EVP_PKEY_free(ret); 122 EVP_PKEY_free(ret);
123 return (NULL); 123 return (NULL);
124} 124}
diff --git a/src/lib/libssl/src/crypto/asn1/d2i_pu.c b/src/lib/libssl/src/crypto/asn1/d2i_pu.c
index df6fea4af5..e917356254 100644
--- a/src/lib/libssl/src/crypto/asn1/d2i_pu.c
+++ b/src/lib/libssl/src/crypto/asn1/d2i_pu.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: d2i_pu.c,v 1.12 2014/07/11 08:44:47 jsing Exp $ */ 1/* $OpenBSD: d2i_pu.c,v 1.13 2015/03/19 14:00:22 tedu Exp $ */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved. 3 * All rights reserved.
4 * 4 *
@@ -130,7 +130,7 @@ d2i_PublicKey(int type, EVP_PKEY **a, const unsigned char **pp, long length)
130 return (ret); 130 return (ret);
131 131
132err: 132err:
133 if ((ret != NULL) && ((a == NULL) || (*a != ret))) 133 if (a == NULL || *a != ret)
134 EVP_PKEY_free(ret); 134 EVP_PKEY_free(ret);
135 return (NULL); 135 return (NULL);
136} 136}
diff --git a/src/lib/libssl/src/crypto/asn1/n_pkey.c b/src/lib/libssl/src/crypto/asn1/n_pkey.c
index bb369fde6e..d3a7431356 100644
--- a/src/lib/libssl/src/crypto/asn1/n_pkey.c
+++ b/src/lib/libssl/src/crypto/asn1/n_pkey.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: n_pkey.c,v 1.25 2015/02/11 04:00:39 jsing Exp $ */ 1/* $OpenBSD: n_pkey.c,v 1.26 2015/03/19 14:00:22 tedu Exp $ */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved. 3 * All rights reserved.
4 * 4 *
@@ -340,11 +340,11 @@ d2i_RSA_NET(RSA **a, const unsigned char **pp, long length,
340 return NULL; 340 return NULL;
341 } 341 }
342 342
343 if ((enckey->os->length != 11) || (strncmp("private-key", 343 /* XXX 11 == strlen("private-key") */
344 (char *)enckey->os->data, 11) != 0)) { 344 if (enckey->os->length != 11 ||
345 memcmp("private-key", enckey->os->data, 11) != 0) {
345 ASN1err(ASN1_F_D2I_RSA_NET, ASN1_R_PRIVATE_KEY_HEADER_MISSING); 346 ASN1err(ASN1_F_D2I_RSA_NET, ASN1_R_PRIVATE_KEY_HEADER_MISSING);
346 NETSCAPE_ENCRYPTED_PKEY_free(enckey); 347 goto err;
347 return NULL;
348 } 348 }
349 if (OBJ_obj2nid(enckey->enckey->algor->algorithm) != NID_rc4) { 349 if (OBJ_obj2nid(enckey->enckey->algor->algorithm) != NID_rc4) {
350 ASN1err(ASN1_F_D2I_RSA_NET, 350 ASN1err(ASN1_F_D2I_RSA_NET,
diff --git a/src/lib/libssl/src/crypto/asn1/tasn_dec.c b/src/lib/libssl/src/crypto/asn1/tasn_dec.c
index 791a10a9c0..7d61a6a233 100644
--- a/src/lib/libssl/src/crypto/asn1/tasn_dec.c
+++ b/src/lib/libssl/src/crypto/asn1/tasn_dec.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: tasn_dec.c,v 1.25 2015/02/14 15:23:57 miod Exp $ */ 1/* $OpenBSD: tasn_dec.c,v 1.26 2015/03/19 14:00:22 tedu Exp $ */
2/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL 2/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3 * project 2000. 3 * project 2000.
4 */ 4 */
@@ -238,8 +238,16 @@ ASN1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, long len,
238 if (asn1_cb && !asn1_cb(ASN1_OP_D2I_PRE, pval, it, NULL)) 238 if (asn1_cb && !asn1_cb(ASN1_OP_D2I_PRE, pval, it, NULL))
239 goto auxerr; 239 goto auxerr;
240 240
241 /* Allocate structure */ 241 if (*pval) {
242 if (!*pval && !ASN1_item_ex_new(pval, it)) { 242 /* Free up and zero CHOICE value if initialised */
243 i = asn1_get_choice_selector(pval, it);
244 if ((i >= 0) && (i < it->tcount)) {
245 tt = it->templates + i;
246 pchptr = asn1_get_field_ptr(pval, tt);
247 ASN1_template_free(pchptr, tt);
248 asn1_set_choice_selector(pval, -1, it);
249 }
250 } else if (!ASN1_item_ex_new(pval, it)) {
243 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, 251 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
244 ERR_R_NESTED_ASN1_ERROR); 252 ERR_R_NESTED_ASN1_ERROR);
245 goto err; 253 goto err;
@@ -325,6 +333,19 @@ ASN1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, long len,
325 if (asn1_cb && !asn1_cb(ASN1_OP_D2I_PRE, pval, it, NULL)) 333 if (asn1_cb && !asn1_cb(ASN1_OP_D2I_PRE, pval, it, NULL))
326 goto auxerr; 334 goto auxerr;
327 335
336 /* Free up and zero any ADB found */
337 for (i = 0, tt = it->templates; i < it->tcount; i++, tt++) {
338 if (tt->flags & ASN1_TFLG_ADB_MASK) {
339 const ASN1_TEMPLATE *seqtt;
340 ASN1_VALUE **pseqval;
341 seqtt = asn1_do_adb(pval, tt, 1);
342 if (!seqtt)
343 goto err;
344 pseqval = asn1_get_field_ptr(pval, seqtt);
345 ASN1_template_free(pseqval, seqtt);
346 }
347 }
348
328 /* Get each field entry */ 349 /* Get each field entry */
329 for (i = 0, tt = it->templates; i < it->tcount; i++, tt++) { 350 for (i = 0, tt = it->templates; i < it->tcount; i++, tt++) {
330 const ASN1_TEMPLATE *seqtt; 351 const ASN1_TEMPLATE *seqtt;
diff --git a/src/lib/libssl/src/crypto/asn1/x_x509.c b/src/lib/libssl/src/crypto/asn1/x_x509.c
index 70d38221b6..168c2c0fcd 100644
--- a/src/lib/libssl/src/crypto/asn1/x_x509.c
+++ b/src/lib/libssl/src/crypto/asn1/x_x509.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: x_x509.c,v 1.23 2015/02/11 04:00:39 jsing Exp $ */ 1/* $OpenBSD: x_x509.c,v 1.24 2015/03/19 14:00:22 tedu Exp $ */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved. 3 * All rights reserved.
4 * 4 *
@@ -313,16 +313,20 @@ d2i_X509_AUX(X509 **a, const unsigned char **pp, long length)
313 313
314 /* Save start position */ 314 /* Save start position */
315 q = *pp; 315 q = *pp;
316 ret = d2i_X509(a, pp, length); 316 ret = d2i_X509(NULL, pp, length);
317 /* If certificate unreadable then forget it */ 317 /* If certificate unreadable then forget it */
318 if (!ret) 318 if (!ret)
319 return NULL; 319 return NULL;
320 /* update length */ 320 /* update length */
321 length -= *pp - q; 321 length -= *pp - q;
322 if (!length) 322 if (length > 0) {
323 return ret; 323 if (!d2i_X509_CERT_AUX(&ret->aux, pp, length))
324 if (!d2i_X509_CERT_AUX(&ret->aux, pp, length)) 324 goto err;
325 goto err; 325 }
326 if (a != NULL) {
327 X509_free(*a);
328 *a = ret;
329 }
326 return ret; 330 return ret;
327 331
328err: 332err:
diff --git a/src/lib/libssl/src/crypto/ec/ec_asn1.c b/src/lib/libssl/src/crypto/ec/ec_asn1.c
index c0ef6f40e4..f01008ec43 100644
--- a/src/lib/libssl/src/crypto/ec/ec_asn1.c
+++ b/src/lib/libssl/src/crypto/ec/ec_asn1.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: ec_asn1.c,v 1.12 2015/02/10 05:43:09 jsing Exp $ */ 1/* $OpenBSD: ec_asn1.c,v 1.13 2015/03/19 14:00:22 tedu Exp $ */
2/* 2/*
3 * Written by Nils Larsch for the OpenSSL project. 3 * Written by Nils Larsch for the OpenSSL project.
4 */ 4 */
@@ -999,19 +999,19 @@ d2i_ECPKParameters(EC_GROUP ** a, const unsigned char **in, long len)
999 999
1000 if ((params = d2i_ECPKPARAMETERS(NULL, in, len)) == NULL) { 1000 if ((params = d2i_ECPKPARAMETERS(NULL, in, len)) == NULL) {
1001 ECerr(EC_F_D2I_ECPKPARAMETERS, EC_R_D2I_ECPKPARAMETERS_FAILURE); 1001 ECerr(EC_F_D2I_ECPKPARAMETERS, EC_R_D2I_ECPKPARAMETERS_FAILURE);
1002 ECPKPARAMETERS_free(params); 1002 goto err;
1003 return NULL;
1004 } 1003 }
1005 if ((group = ec_asn1_pkparameters2group(params)) == NULL) { 1004 if ((group = ec_asn1_pkparameters2group(params)) == NULL) {
1006 ECerr(EC_F_D2I_ECPKPARAMETERS, EC_R_PKPARAMETERS2GROUP_FAILURE); 1005 ECerr(EC_F_D2I_ECPKPARAMETERS, EC_R_PKPARAMETERS2GROUP_FAILURE);
1007 ECPKPARAMETERS_free(params); 1006 goto err;
1008 return NULL;
1009 } 1007 }
1010 if (a && *a) 1008
1009 if (a != NULL) {
1011 EC_GROUP_clear_free(*a); 1010 EC_GROUP_clear_free(*a);
1012 if (a)
1013 *a = group; 1011 *a = group;
1012 }
1014 1013
1014err:
1015 ECPKPARAMETERS_free(params); 1015 ECPKPARAMETERS_free(params);
1016 return (group); 1016 return (group);
1017} 1017}
@@ -1039,7 +1039,6 @@ i2d_ECPKParameters(const EC_GROUP * a, unsigned char **out)
1039EC_KEY * 1039EC_KEY *
1040d2i_ECPrivateKey(EC_KEY ** a, const unsigned char **in, long len) 1040d2i_ECPrivateKey(EC_KEY ** a, const unsigned char **in, long len)
1041{ 1041{
1042 int ok = 0;
1043 EC_KEY *ret = NULL; 1042 EC_KEY *ret = NULL;
1044 EC_PRIVATEKEY *priv_key = NULL; 1043 EC_PRIVATEKEY *priv_key = NULL;
1045 1044
@@ -1054,12 +1053,9 @@ d2i_ECPrivateKey(EC_KEY ** a, const unsigned char **in, long len)
1054 } 1053 }
1055 if (a == NULL || *a == NULL) { 1054 if (a == NULL || *a == NULL) {
1056 if ((ret = EC_KEY_new()) == NULL) { 1055 if ((ret = EC_KEY_new()) == NULL) {
1057 ECerr(EC_F_D2I_ECPRIVATEKEY, 1056 ECerr(EC_F_D2I_ECPRIVATEKEY, ERR_R_MALLOC_FAILURE);
1058 ERR_R_MALLOC_FAILURE);
1059 goto err; 1057 goto err;
1060 } 1058 }
1061 if (a)
1062 *a = ret;
1063 } else 1059 } else
1064 ret = *a; 1060 ret = *a;
1065 1061
@@ -1109,17 +1105,19 @@ d2i_ECPrivateKey(EC_KEY ** a, const unsigned char **in, long len)
1109 goto err; 1105 goto err;
1110 } 1106 }
1111 } 1107 }
1112 ok = 1; 1108
1109 EC_PRIVATEKEY_free(priv_key);
1110 if (a != NULL)
1111 *a = ret;
1112 return (ret);
1113
1113err: 1114err:
1114 if (!ok) { 1115 if (a == NULL || *a != ret)
1115 if (ret) 1116 EC_KEY_free(ret);
1116 EC_KEY_free(ret);
1117 ret = NULL;
1118 }
1119 if (priv_key) 1117 if (priv_key)
1120 EC_PRIVATEKEY_free(priv_key); 1118 EC_PRIVATEKEY_free(priv_key);
1121 1119
1122 return (ret); 1120 return (NULL);
1123} 1121}
1124 1122
1125int 1123int
@@ -1232,8 +1230,6 @@ d2i_ECParameters(EC_KEY ** a, const unsigned char **in, long len)
1232 ECerr(EC_F_D2I_ECPARAMETERS, ERR_R_MALLOC_FAILURE); 1230 ECerr(EC_F_D2I_ECPARAMETERS, ERR_R_MALLOC_FAILURE);
1233 return NULL; 1231 return NULL;
1234 } 1232 }
1235 if (a)
1236 *a = ret;
1237 } else 1233 } else
1238 ret = *a; 1234 ret = *a;
1239 1235
@@ -1241,6 +1237,9 @@ d2i_ECParameters(EC_KEY ** a, const unsigned char **in, long len)
1241 ECerr(EC_F_D2I_ECPARAMETERS, ERR_R_EC_LIB); 1237 ECerr(EC_F_D2I_ECPARAMETERS, ERR_R_EC_LIB);
1242 return NULL; 1238 return NULL;
1243 } 1239 }
1240
1241 if (a != NULL)
1242 *a = ret;
1244 return ret; 1243 return ret;
1245} 1244}
1246 1245
diff --git a/src/lib/libssl/src/crypto/pkcs7/pk7_doit.c b/src/lib/libssl/src/crypto/pkcs7/pk7_doit.c
index 252fab04d7..d0cf84df80 100644
--- a/src/lib/libssl/src/crypto/pkcs7/pk7_doit.c
+++ b/src/lib/libssl/src/crypto/pkcs7/pk7_doit.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: pk7_doit.c,v 1.31 2015/02/07 13:19:15 doug Exp $ */ 1/* $OpenBSD: pk7_doit.c,v 1.32 2015/03/19 14:00:22 tedu Exp $ */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved. 3 * All rights reserved.
4 * 4 *
@@ -261,6 +261,28 @@ PKCS7_dataInit(PKCS7 *p7, BIO *bio)
261 PKCS7_RECIP_INFO *ri = NULL; 261 PKCS7_RECIP_INFO *ri = NULL;
262 ASN1_OCTET_STRING *os = NULL; 262 ASN1_OCTET_STRING *os = NULL;
263 263
264 if (p7 == NULL) {
265 PKCS7err(PKCS7_F_PKCS7_DATAINIT, PKCS7_R_INVALID_NULL_POINTER);
266 return NULL;
267 }
268
269 /*
270 * The content field in the PKCS7 ContentInfo is optional,
271 * but that really only applies to inner content (precisely,
272 * detached signatures).
273 *
274 * When reading content, missing outer content is therefore
275 * treated as an error.
276 *
277 * When creating content, PKCS7_content_new() must be called
278 * before calling this method, so a NULL p7->d is always
279 * an error.
280 */
281 if (p7->d.ptr == NULL) {
282 PKCS7err(PKCS7_F_PKCS7_DATAINIT, PKCS7_R_NO_CONTENT);
283 return NULL;
284 }
285
264 i = OBJ_obj2nid(p7->type); 286 i = OBJ_obj2nid(p7->type);
265 p7->state = PKCS7_S_HEADER; 287 p7->state = PKCS7_S_HEADER;
266 288
@@ -417,6 +439,17 @@ PKCS7_dataDecode(PKCS7 *p7, EVP_PKEY *pkey, BIO *in_bio, X509 *pcert)
417 unsigned char *ek = NULL, *tkey = NULL; 439 unsigned char *ek = NULL, *tkey = NULL;
418 int eklen = 0, tkeylen = 0; 440 int eklen = 0, tkeylen = 0;
419 441
442 if (p7 == NULL) {
443 PKCS7err(PKCS7_F_PKCS7_DATADECODE,
444 PKCS7_R_INVALID_NULL_POINTER);
445 return NULL;
446 }
447
448 if (p7->d.ptr == NULL) {
449 PKCS7err(PKCS7_F_PKCS7_DATADECODE, PKCS7_R_NO_CONTENT);
450 return NULL;
451 }
452
420 i = OBJ_obj2nid(p7->type); 453 i = OBJ_obj2nid(p7->type);
421 p7->state = PKCS7_S_HEADER; 454 p7->state = PKCS7_S_HEADER;
422 455
@@ -691,6 +724,17 @@ PKCS7_dataFinal(PKCS7 *p7, BIO *bio)
691 STACK_OF(PKCS7_SIGNER_INFO) *si_sk = NULL; 724 STACK_OF(PKCS7_SIGNER_INFO) *si_sk = NULL;
692 ASN1_OCTET_STRING *os = NULL; 725 ASN1_OCTET_STRING *os = NULL;
693 726
727 if (p7 == NULL) {
728 PKCS7err(PKCS7_F_PKCS7_DATAFINAL,
729 PKCS7_R_INVALID_NULL_POINTER);
730 return 0;
731 }
732
733 if (p7->d.ptr == NULL) {
734 PKCS7err(PKCS7_F_PKCS7_DATAFINAL, PKCS7_R_NO_CONTENT);
735 return 0;
736 }
737
694 EVP_MD_CTX_init(&ctx_tmp); 738 EVP_MD_CTX_init(&ctx_tmp);
695 i = OBJ_obj2nid(p7->type); 739 i = OBJ_obj2nid(p7->type);
696 p7->state = PKCS7_S_HEADER; 740 p7->state = PKCS7_S_HEADER;
@@ -736,6 +780,7 @@ PKCS7_dataFinal(PKCS7 *p7, BIO *bio)
736 /* If detached data then the content is excluded */ 780 /* If detached data then the content is excluded */
737 if (PKCS7_type_is_data(p7->d.sign->contents) && p7->detached) { 781 if (PKCS7_type_is_data(p7->d.sign->contents) && p7->detached) {
738 M_ASN1_OCTET_STRING_free(os); 782 M_ASN1_OCTET_STRING_free(os);
783 os = NULL;
739 p7->d.sign->contents->d.data = NULL; 784 p7->d.sign->contents->d.data = NULL;
740 } 785 }
741 break; 786 break;
@@ -750,6 +795,7 @@ PKCS7_dataFinal(PKCS7 *p7, BIO *bio)
750 if (PKCS7_type_is_data(p7->d.digest->contents) && 795 if (PKCS7_type_is_data(p7->d.digest->contents) &&
751 p7->detached) { 796 p7->detached) {
752 M_ASN1_OCTET_STRING_free(os); 797 M_ASN1_OCTET_STRING_free(os);
798 os = NULL;
753 p7->d.digest->contents->d.data = NULL; 799 p7->d.digest->contents->d.data = NULL;
754 } 800 }
755 break; 801 break;
@@ -815,22 +861,32 @@ PKCS7_dataFinal(PKCS7 *p7, BIO *bio)
815 M_ASN1_OCTET_STRING_set(p7->d.digest->digest, md_data, md_len); 861 M_ASN1_OCTET_STRING_set(p7->d.digest->digest, md_data, md_len);
816 } 862 }
817 863
818 if (!PKCS7_is_detached(p7) && !(os->flags & ASN1_STRING_FLAG_NDEF)) { 864 if (!PKCS7_is_detached(p7)) {
819 char *cont; 865 /*
820 long contlen; 866 * NOTE: only reach os == NULL here because detached
821 btmp = BIO_find_type(bio, BIO_TYPE_MEM); 867 * digested data support is broken?
822 if (btmp == NULL) { 868 */
823 PKCS7err(PKCS7_F_PKCS7_DATAFINAL, 869 if (os == NULL)
824 PKCS7_R_UNABLE_TO_FIND_MEM_BIO);
825 goto err; 870 goto err;
871 if (!(os->flags & ASN1_STRING_FLAG_NDEF)) {
872 char *cont;
873 long contlen;
874
875 btmp = BIO_find_type(bio, BIO_TYPE_MEM);
876 if (btmp == NULL) {
877 PKCS7err(PKCS7_F_PKCS7_DATAFINAL,
878 PKCS7_R_UNABLE_TO_FIND_MEM_BIO);
879 goto err;
880 }
881 contlen = BIO_get_mem_data(btmp, &cont);
882 /*
883 * Mark the BIO read only then we can use its copy
884 * of the data instead of making an extra copy.
885 */
886 BIO_set_flags(btmp, BIO_FLAGS_MEM_RDONLY);
887 BIO_set_mem_eof_return(btmp, 0);
888 ASN1_STRING_set0(os, (unsigned char *)cont, contlen);
826 } 889 }
827 contlen = BIO_get_mem_data(btmp, &cont);
828 /* Mark the BIO read only then we can use its copy of the data
829 * instead of making an extra copy.
830 */
831 BIO_set_flags(btmp, BIO_FLAGS_MEM_RDONLY);
832 BIO_set_mem_eof_return(btmp, 0);
833 ASN1_STRING_set0(os, (unsigned char *)cont, contlen);
834 } 890 }
835 ret = 1; 891 ret = 1;
836err: 892err:
@@ -905,6 +961,17 @@ PKCS7_dataVerify(X509_STORE *cert_store, X509_STORE_CTX *ctx, BIO *bio,
905 STACK_OF(X509) *cert; 961 STACK_OF(X509) *cert;
906 X509 *x509; 962 X509 *x509;
907 963
964 if (p7 == NULL) {
965 PKCS7err(PKCS7_F_PKCS7_DATAVERIFY,
966 PKCS7_R_INVALID_NULL_POINTER);
967 return 0;
968 }
969
970 if (p7->d.ptr == NULL) {
971 PKCS7err(PKCS7_F_PKCS7_DATAVERIFY, PKCS7_R_NO_CONTENT);
972 return 0;
973 }
974
908 if (PKCS7_type_is_signed(p7)) { 975 if (PKCS7_type_is_signed(p7)) {
909 cert = p7->d.sign->cert; 976 cert = p7->d.sign->cert;
910 } else if (PKCS7_type_is_signedAndEnveloped(p7)) { 977 } else if (PKCS7_type_is_signedAndEnveloped(p7)) {
@@ -941,6 +1008,7 @@ PKCS7_dataVerify(X509_STORE *cert_store, X509_STORE_CTX *ctx, BIO *bio,
941 1008
942 return PKCS7_signatureVerify(bio, p7, si, x509); 1009 return PKCS7_signatureVerify(bio, p7, si, x509);
943err: 1010err:
1011
944 return ret; 1012 return ret;
945} 1013}
946 1014
diff --git a/src/lib/libssl/src/crypto/pkcs7/pk7_lib.c b/src/lib/libssl/src/crypto/pkcs7/pk7_lib.c
index 27370800c9..3eec92e29b 100644
--- a/src/lib/libssl/src/crypto/pkcs7/pk7_lib.c
+++ b/src/lib/libssl/src/crypto/pkcs7/pk7_lib.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: pk7_lib.c,v 1.14 2014/07/12 16:03:37 miod Exp $ */ 1/* $OpenBSD: pk7_lib.c,v 1.15 2015/03/19 14:00:22 tedu Exp $ */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved. 3 * All rights reserved.
4 * 4 *
@@ -460,6 +460,8 @@ PKCS7_set_digest(PKCS7 *p7, const EVP_MD *md)
460STACK_OF(PKCS7_SIGNER_INFO) * 460STACK_OF(PKCS7_SIGNER_INFO) *
461PKCS7_get_signer_info(PKCS7 *p7) 461PKCS7_get_signer_info(PKCS7 *p7)
462{ 462{
463 if (p7 == NULL || p7->d.ptr == NULL)
464 return (NULL);
463 if (PKCS7_type_is_signed(p7)) { 465 if (PKCS7_type_is_signed(p7)) {
464 return (p7->d.sign->signer_info); 466 return (p7->d.sign->signer_info);
465 } else if (PKCS7_type_is_signedAndEnveloped(p7)) { 467 } else if (PKCS7_type_is_signedAndEnveloped(p7)) {
diff --git a/src/lib/libssl/src/ssl/d1_lib.c b/src/lib/libssl/src/ssl/d1_lib.c
index 9d9a90d76a..dd789ccc70 100644
--- a/src/lib/libssl/src/ssl/d1_lib.c
+++ b/src/lib/libssl/src/ssl/d1_lib.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: d1_lib.c,v 1.27 2015/02/09 10:53:28 jsing Exp $ */ 1/* $OpenBSD: d1_lib.c,v 1.28 2015/03/19 14:00:22 tedu Exp $ */
2/* 2/*
3 * DTLS implementation written by Nagendra Modadugu 3 * DTLS implementation written by Nagendra Modadugu
4 * (nagendra@cs.stanford.edu) for the OpenSSL project 2005. 4 * (nagendra@cs.stanford.edu) for the OpenSSL project 2005.
@@ -443,6 +443,9 @@ dtls1_listen(SSL *s, struct sockaddr *client)
443{ 443{
444 int ret; 444 int ret;
445 445
446 /* Ensure there is no state left over from a previous invocation */
447 SSL_clear(s);
448
446 SSL_set_options(s, SSL_OP_COOKIE_EXCHANGE); 449 SSL_set_options(s, SSL_OP_COOKIE_EXCHANGE);
447 s->d1->listen = 1; 450 s->d1->listen = 1;
448 451