diff options
author | doug <> | 2015-07-19 01:44:16 +0000 |
---|---|---|
committer | doug <> | 2015-07-19 01:44:16 +0000 |
commit | 8f0f19f3e1d975fa31a75cf440fbacfd73167ecb (patch) | |
tree | d8a2fcc54029819a48f619aefa49b165804ee330 /src | |
parent | 6eefebebc0f30c7c04beee4f7b4703963c7648ba (diff) | |
download | openbsd-8f0f19f3e1d975fa31a75cf440fbacfd73167ecb.tar.gz openbsd-8f0f19f3e1d975fa31a75cf440fbacfd73167ecb.tar.bz2 openbsd-8f0f19f3e1d975fa31a75cf440fbacfd73167ecb.zip |
Simplify X509_STORE_CTX_init and make it safe with stack variables.
The current version is not safe with stack variables because it may
return prematurely with a partially constructed object on error.
ok miod@ a while back
Diffstat (limited to 'src')
-rw-r--r-- | src/lib/libcrypto/x509/x509_vfy.c | 113 | ||||
-rw-r--r-- | src/lib/libssl/src/crypto/x509/x509_vfy.c | 113 |
2 files changed, 110 insertions, 116 deletions
diff --git a/src/lib/libcrypto/x509/x509_vfy.c b/src/lib/libcrypto/x509/x509_vfy.c index a20c755d7f..bc5905784d 100644 --- a/src/lib/libcrypto/x509/x509_vfy.c +++ b/src/lib/libcrypto/x509/x509_vfy.c | |||
@@ -1,4 +1,4 @@ | |||
1 | /* $OpenBSD: x509_vfy.c,v 1.42 2015/06/11 15:58:53 jsing Exp $ */ | 1 | /* $OpenBSD: x509_vfy.c,v 1.43 2015/07/19 01:44:16 doug 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 | * |
@@ -2001,78 +2001,48 @@ int | |||
2001 | X509_STORE_CTX_init(X509_STORE_CTX *ctx, X509_STORE *store, X509 *x509, | 2001 | X509_STORE_CTX_init(X509_STORE_CTX *ctx, X509_STORE *store, X509 *x509, |
2002 | STACK_OF(X509) *chain) | 2002 | STACK_OF(X509) *chain) |
2003 | { | 2003 | { |
2004 | int ret = 1; | 2004 | int param_ret = 1; |
2005 | 2005 | ||
2006 | /* | ||
2007 | * Make sure everything is initialized properly even in case of an | ||
2008 | * early return due to an error. | ||
2009 | * | ||
2010 | * While this 'ctx' can be reused, X509_STORE_CTX_cleanup() will have | ||
2011 | * freed everything and memset ex_data anyway. This also allows us | ||
2012 | * to safely use X509_STORE_CTX variables from the stack which will | ||
2013 | * have uninitialized data. | ||
2014 | */ | ||
2015 | memset(ctx, 0, sizeof(*ctx)); | ||
2016 | |||
2017 | /* | ||
2018 | * Set values other than 0. Keep this in the same order as | ||
2019 | * X509_STORE_CTX except for values that may fail. All fields that | ||
2020 | * may fail should go last to make sure 'ctx' is as consistent as | ||
2021 | * possible even on early exits. | ||
2022 | */ | ||
2006 | ctx->ctx = store; | 2023 | ctx->ctx = store; |
2007 | ctx->current_method = 0; | ||
2008 | ctx->cert = x509; | 2024 | ctx->cert = x509; |
2009 | ctx->untrusted = chain; | 2025 | ctx->untrusted = chain; |
2010 | ctx->crls = NULL; | ||
2011 | ctx->last_untrusted = 0; | ||
2012 | ctx->other_ctx = NULL; | ||
2013 | ctx->valid = 0; | ||
2014 | ctx->chain = NULL; | ||
2015 | ctx->error = 0; | ||
2016 | ctx->explicit_policy = 0; | ||
2017 | ctx->error_depth = 0; | ||
2018 | ctx->current_cert = NULL; | ||
2019 | ctx->current_issuer = NULL; | ||
2020 | ctx->current_crl = NULL; | ||
2021 | ctx->current_crl_score = 0; | ||
2022 | ctx->current_reasons = 0; | ||
2023 | ctx->tree = NULL; | ||
2024 | ctx->parent = NULL; | ||
2025 | |||
2026 | ctx->param = X509_VERIFY_PARAM_new(); | ||
2027 | |||
2028 | if (!ctx->param) { | ||
2029 | X509err(X509_F_X509_STORE_CTX_INIT, ERR_R_MALLOC_FAILURE); | ||
2030 | return 0; | ||
2031 | } | ||
2032 | |||
2033 | /* Inherit callbacks and flags from X509_STORE if not set | ||
2034 | * use defaults. | ||
2035 | */ | ||
2036 | 2026 | ||
2037 | if (store) | 2027 | if (store && store->verify) |
2038 | ret = X509_VERIFY_PARAM_inherit(ctx->param, store->param); | 2028 | ctx->verify = store->verify; |
2039 | else | 2029 | else |
2040 | ctx->param->inh_flags |= X509_VP_FLAG_DEFAULT|X509_VP_FLAG_ONCE; | 2030 | ctx->verify = internal_verify; |
2041 | 2031 | ||
2042 | if (store) { | 2032 | if (store && store->verify_cb) |
2043 | ctx->verify_cb = store->verify_cb; | 2033 | ctx->verify_cb = store->verify_cb; |
2044 | ctx->cleanup = store->cleanup; | ||
2045 | } else | ||
2046 | ctx->cleanup = 0; | ||
2047 | |||
2048 | if (ret) | ||
2049 | ret = X509_VERIFY_PARAM_inherit(ctx->param, | ||
2050 | X509_VERIFY_PARAM_lookup("default")); | ||
2051 | |||
2052 | if (ret == 0) { | ||
2053 | X509err(X509_F_X509_STORE_CTX_INIT, ERR_R_MALLOC_FAILURE); | ||
2054 | return 0; | ||
2055 | } | ||
2056 | |||
2057 | if (store && store->check_issued) | ||
2058 | ctx->check_issued = store->check_issued; | ||
2059 | else | 2034 | else |
2060 | ctx->check_issued = check_issued; | 2035 | ctx->verify_cb = null_callback; |
2061 | 2036 | ||
2062 | if (store && store->get_issuer) | 2037 | if (store && store->get_issuer) |
2063 | ctx->get_issuer = store->get_issuer; | 2038 | ctx->get_issuer = store->get_issuer; |
2064 | else | 2039 | else |
2065 | ctx->get_issuer = X509_STORE_CTX_get1_issuer; | 2040 | ctx->get_issuer = X509_STORE_CTX_get1_issuer; |
2066 | 2041 | ||
2067 | if (store && store->verify_cb) | 2042 | if (store && store->check_issued) |
2068 | ctx->verify_cb = store->verify_cb; | 2043 | ctx->check_issued = store->check_issued; |
2069 | else | ||
2070 | ctx->verify_cb = null_callback; | ||
2071 | |||
2072 | if (store && store->verify) | ||
2073 | ctx->verify = store->verify; | ||
2074 | else | 2044 | else |
2075 | ctx->verify = internal_verify; | 2045 | ctx->check_issued = check_issued; |
2076 | 2046 | ||
2077 | if (store && store->check_revocation) | 2047 | if (store && store->check_revocation) |
2078 | ctx->check_revocation = store->check_revocation; | 2048 | ctx->check_revocation = store->check_revocation; |
@@ -2094,6 +2064,8 @@ X509_STORE_CTX_init(X509_STORE_CTX *ctx, X509_STORE *store, X509 *x509, | |||
2094 | else | 2064 | else |
2095 | ctx->cert_crl = cert_crl; | 2065 | ctx->cert_crl = cert_crl; |
2096 | 2066 | ||
2067 | ctx->check_policy = check_policy; | ||
2068 | |||
2097 | if (store && store->lookup_certs) | 2069 | if (store && store->lookup_certs) |
2098 | ctx->lookup_certs = store->lookup_certs; | 2070 | ctx->lookup_certs = store->lookup_certs; |
2099 | else | 2071 | else |
@@ -2104,8 +2076,33 @@ X509_STORE_CTX_init(X509_STORE_CTX *ctx, X509_STORE *store, X509 *x509, | |||
2104 | else | 2076 | else |
2105 | ctx->lookup_crls = X509_STORE_get1_crls; | 2077 | ctx->lookup_crls = X509_STORE_get1_crls; |
2106 | 2078 | ||
2107 | ctx->check_policy = check_policy; | 2079 | if (store && store->cleanup) |
2080 | ctx->cleanup = store->cleanup; | ||
2081 | else | ||
2082 | ctx->cleanup = NULL; | ||
2108 | 2083 | ||
2084 | ctx->param = X509_VERIFY_PARAM_new(); | ||
2085 | if (!ctx->param) { | ||
2086 | X509err(X509_F_X509_STORE_CTX_INIT, ERR_R_MALLOC_FAILURE); | ||
2087 | return 0; | ||
2088 | } | ||
2089 | |||
2090 | /* Inherit callbacks and flags from X509_STORE if not set | ||
2091 | * use defaults. | ||
2092 | */ | ||
2093 | if (store) | ||
2094 | param_ret = X509_VERIFY_PARAM_inherit(ctx->param, store->param); | ||
2095 | else | ||
2096 | ctx->param->inh_flags |= X509_VP_FLAG_DEFAULT|X509_VP_FLAG_ONCE; | ||
2097 | |||
2098 | if (param_ret) | ||
2099 | param_ret = X509_VERIFY_PARAM_inherit(ctx->param, | ||
2100 | X509_VERIFY_PARAM_lookup("default")); | ||
2101 | |||
2102 | if (param_ret == 0) { | ||
2103 | X509err(X509_F_X509_STORE_CTX_INIT, ERR_R_MALLOC_FAILURE); | ||
2104 | return 0; | ||
2105 | } | ||
2109 | 2106 | ||
2110 | if (CRYPTO_new_ex_data(CRYPTO_EX_INDEX_X509_STORE_CTX, ctx, | 2107 | if (CRYPTO_new_ex_data(CRYPTO_EX_INDEX_X509_STORE_CTX, ctx, |
2111 | &(ctx->ex_data)) == 0) { | 2108 | &(ctx->ex_data)) == 0) { |
diff --git a/src/lib/libssl/src/crypto/x509/x509_vfy.c b/src/lib/libssl/src/crypto/x509/x509_vfy.c index a20c755d7f..bc5905784d 100644 --- a/src/lib/libssl/src/crypto/x509/x509_vfy.c +++ b/src/lib/libssl/src/crypto/x509/x509_vfy.c | |||
@@ -1,4 +1,4 @@ | |||
1 | /* $OpenBSD: x509_vfy.c,v 1.42 2015/06/11 15:58:53 jsing Exp $ */ | 1 | /* $OpenBSD: x509_vfy.c,v 1.43 2015/07/19 01:44:16 doug 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 | * |
@@ -2001,78 +2001,48 @@ int | |||
2001 | X509_STORE_CTX_init(X509_STORE_CTX *ctx, X509_STORE *store, X509 *x509, | 2001 | X509_STORE_CTX_init(X509_STORE_CTX *ctx, X509_STORE *store, X509 *x509, |
2002 | STACK_OF(X509) *chain) | 2002 | STACK_OF(X509) *chain) |
2003 | { | 2003 | { |
2004 | int ret = 1; | 2004 | int param_ret = 1; |
2005 | 2005 | ||
2006 | /* | ||
2007 | * Make sure everything is initialized properly even in case of an | ||
2008 | * early return due to an error. | ||
2009 | * | ||
2010 | * While this 'ctx' can be reused, X509_STORE_CTX_cleanup() will have | ||
2011 | * freed everything and memset ex_data anyway. This also allows us | ||
2012 | * to safely use X509_STORE_CTX variables from the stack which will | ||
2013 | * have uninitialized data. | ||
2014 | */ | ||
2015 | memset(ctx, 0, sizeof(*ctx)); | ||
2016 | |||
2017 | /* | ||
2018 | * Set values other than 0. Keep this in the same order as | ||
2019 | * X509_STORE_CTX except for values that may fail. All fields that | ||
2020 | * may fail should go last to make sure 'ctx' is as consistent as | ||
2021 | * possible even on early exits. | ||
2022 | */ | ||
2006 | ctx->ctx = store; | 2023 | ctx->ctx = store; |
2007 | ctx->current_method = 0; | ||
2008 | ctx->cert = x509; | 2024 | ctx->cert = x509; |
2009 | ctx->untrusted = chain; | 2025 | ctx->untrusted = chain; |
2010 | ctx->crls = NULL; | ||
2011 | ctx->last_untrusted = 0; | ||
2012 | ctx->other_ctx = NULL; | ||
2013 | ctx->valid = 0; | ||
2014 | ctx->chain = NULL; | ||
2015 | ctx->error = 0; | ||
2016 | ctx->explicit_policy = 0; | ||
2017 | ctx->error_depth = 0; | ||
2018 | ctx->current_cert = NULL; | ||
2019 | ctx->current_issuer = NULL; | ||
2020 | ctx->current_crl = NULL; | ||
2021 | ctx->current_crl_score = 0; | ||
2022 | ctx->current_reasons = 0; | ||
2023 | ctx->tree = NULL; | ||
2024 | ctx->parent = NULL; | ||
2025 | |||
2026 | ctx->param = X509_VERIFY_PARAM_new(); | ||
2027 | |||
2028 | if (!ctx->param) { | ||
2029 | X509err(X509_F_X509_STORE_CTX_INIT, ERR_R_MALLOC_FAILURE); | ||
2030 | return 0; | ||
2031 | } | ||
2032 | |||
2033 | /* Inherit callbacks and flags from X509_STORE if not set | ||
2034 | * use defaults. | ||
2035 | */ | ||
2036 | 2026 | ||
2037 | if (store) | 2027 | if (store && store->verify) |
2038 | ret = X509_VERIFY_PARAM_inherit(ctx->param, store->param); | 2028 | ctx->verify = store->verify; |
2039 | else | 2029 | else |
2040 | ctx->param->inh_flags |= X509_VP_FLAG_DEFAULT|X509_VP_FLAG_ONCE; | 2030 | ctx->verify = internal_verify; |
2041 | 2031 | ||
2042 | if (store) { | 2032 | if (store && store->verify_cb) |
2043 | ctx->verify_cb = store->verify_cb; | 2033 | ctx->verify_cb = store->verify_cb; |
2044 | ctx->cleanup = store->cleanup; | ||
2045 | } else | ||
2046 | ctx->cleanup = 0; | ||
2047 | |||
2048 | if (ret) | ||
2049 | ret = X509_VERIFY_PARAM_inherit(ctx->param, | ||
2050 | X509_VERIFY_PARAM_lookup("default")); | ||
2051 | |||
2052 | if (ret == 0) { | ||
2053 | X509err(X509_F_X509_STORE_CTX_INIT, ERR_R_MALLOC_FAILURE); | ||
2054 | return 0; | ||
2055 | } | ||
2056 | |||
2057 | if (store && store->check_issued) | ||
2058 | ctx->check_issued = store->check_issued; | ||
2059 | else | 2034 | else |
2060 | ctx->check_issued = check_issued; | 2035 | ctx->verify_cb = null_callback; |
2061 | 2036 | ||
2062 | if (store && store->get_issuer) | 2037 | if (store && store->get_issuer) |
2063 | ctx->get_issuer = store->get_issuer; | 2038 | ctx->get_issuer = store->get_issuer; |
2064 | else | 2039 | else |
2065 | ctx->get_issuer = X509_STORE_CTX_get1_issuer; | 2040 | ctx->get_issuer = X509_STORE_CTX_get1_issuer; |
2066 | 2041 | ||
2067 | if (store && store->verify_cb) | 2042 | if (store && store->check_issued) |
2068 | ctx->verify_cb = store->verify_cb; | 2043 | ctx->check_issued = store->check_issued; |
2069 | else | ||
2070 | ctx->verify_cb = null_callback; | ||
2071 | |||
2072 | if (store && store->verify) | ||
2073 | ctx->verify = store->verify; | ||
2074 | else | 2044 | else |
2075 | ctx->verify = internal_verify; | 2045 | ctx->check_issued = check_issued; |
2076 | 2046 | ||
2077 | if (store && store->check_revocation) | 2047 | if (store && store->check_revocation) |
2078 | ctx->check_revocation = store->check_revocation; | 2048 | ctx->check_revocation = store->check_revocation; |
@@ -2094,6 +2064,8 @@ X509_STORE_CTX_init(X509_STORE_CTX *ctx, X509_STORE *store, X509 *x509, | |||
2094 | else | 2064 | else |
2095 | ctx->cert_crl = cert_crl; | 2065 | ctx->cert_crl = cert_crl; |
2096 | 2066 | ||
2067 | ctx->check_policy = check_policy; | ||
2068 | |||
2097 | if (store && store->lookup_certs) | 2069 | if (store && store->lookup_certs) |
2098 | ctx->lookup_certs = store->lookup_certs; | 2070 | ctx->lookup_certs = store->lookup_certs; |
2099 | else | 2071 | else |
@@ -2104,8 +2076,33 @@ X509_STORE_CTX_init(X509_STORE_CTX *ctx, X509_STORE *store, X509 *x509, | |||
2104 | else | 2076 | else |
2105 | ctx->lookup_crls = X509_STORE_get1_crls; | 2077 | ctx->lookup_crls = X509_STORE_get1_crls; |
2106 | 2078 | ||
2107 | ctx->check_policy = check_policy; | 2079 | if (store && store->cleanup) |
2080 | ctx->cleanup = store->cleanup; | ||
2081 | else | ||
2082 | ctx->cleanup = NULL; | ||
2108 | 2083 | ||
2084 | ctx->param = X509_VERIFY_PARAM_new(); | ||
2085 | if (!ctx->param) { | ||
2086 | X509err(X509_F_X509_STORE_CTX_INIT, ERR_R_MALLOC_FAILURE); | ||
2087 | return 0; | ||
2088 | } | ||
2089 | |||
2090 | /* Inherit callbacks and flags from X509_STORE if not set | ||
2091 | * use defaults. | ||
2092 | */ | ||
2093 | if (store) | ||
2094 | param_ret = X509_VERIFY_PARAM_inherit(ctx->param, store->param); | ||
2095 | else | ||
2096 | ctx->param->inh_flags |= X509_VP_FLAG_DEFAULT|X509_VP_FLAG_ONCE; | ||
2097 | |||
2098 | if (param_ret) | ||
2099 | param_ret = X509_VERIFY_PARAM_inherit(ctx->param, | ||
2100 | X509_VERIFY_PARAM_lookup("default")); | ||
2101 | |||
2102 | if (param_ret == 0) { | ||
2103 | X509err(X509_F_X509_STORE_CTX_INIT, ERR_R_MALLOC_FAILURE); | ||
2104 | return 0; | ||
2105 | } | ||
2109 | 2106 | ||
2110 | if (CRYPTO_new_ex_data(CRYPTO_EX_INDEX_X509_STORE_CTX, ctx, | 2107 | if (CRYPTO_new_ex_data(CRYPTO_EX_INDEX_X509_STORE_CTX, ctx, |
2111 | &(ctx->ex_data)) == 0) { | 2108 | &(ctx->ex_data)) == 0) { |