diff options
author | djm <> | 2006-05-04 14:19:08 +0000 |
---|---|---|
committer | djm <> | 2006-05-04 14:19:08 +0000 |
commit | f9d2303788ae22bedb13da8c57c49011b74de60a (patch) | |
tree | 153863b6979c54fe2c2bc1e1634af9cea8f6a655 | |
parent | 4fe079716cece1d7c5964785c0961d561af6eb70 (diff) | |
download | openbsd-f9d2303788ae22bedb13da8c57c49011b74de60a.tar.gz openbsd-f9d2303788ae22bedb13da8c57c49011b74de60a.tar.bz2 openbsd-f9d2303788ae22bedb13da8c57c49011b74de60a.zip |
backport checks for degenerate Diffie-Hellman public exponents from
OpenSSL-0.9.8a, where they were added without a corresponding patch to
0.9.7 or an advisory! ok theo@ markus@
-rw-r--r-- | src/lib/libcrypto/dh/dh.h | 6 | ||||
-rw-r--r-- | src/lib/libcrypto/dh/dh_check.c | 22 | ||||
-rw-r--r-- | src/lib/libcrypto/dh/dh_err.c | 1 | ||||
-rw-r--r-- | src/lib/libcrypto/dh/dh_key.c | 7 | ||||
-rw-r--r-- | src/lib/libssl/crypto/shlib_version | 2 | ||||
-rw-r--r-- | src/lib/libssl/src/crypto/dh/dh.h | 6 | ||||
-rw-r--r-- | src/lib/libssl/src/crypto/dh/dh_check.c | 22 | ||||
-rw-r--r-- | src/lib/libssl/src/crypto/dh/dh_err.c | 1 | ||||
-rw-r--r-- | src/lib/libssl/src/crypto/dh/dh_key.c | 7 |
9 files changed, 73 insertions, 1 deletions
diff --git a/src/lib/libcrypto/dh/dh.h b/src/lib/libcrypto/dh/dh.h index d51dc130f4..0aff7fe21f 100644 --- a/src/lib/libcrypto/dh/dh.h +++ b/src/lib/libcrypto/dh/dh.h | |||
@@ -130,6 +130,10 @@ struct dh_st | |||
130 | #define DH_UNABLE_TO_CHECK_GENERATOR 0x04 | 130 | #define DH_UNABLE_TO_CHECK_GENERATOR 0x04 |
131 | #define DH_NOT_SUITABLE_GENERATOR 0x08 | 131 | #define DH_NOT_SUITABLE_GENERATOR 0x08 |
132 | 132 | ||
133 | /* DH_check_pub_key error codes */ | ||
134 | #define DH_CHECK_PUBKEY_TOO_SMALL 0x01 | ||
135 | #define DH_CHECK_PUBKEY_TOO_LARGE 0x02 | ||
136 | |||
133 | /* primes p where (p-1)/2 is prime too are called "safe"; we define | 137 | /* primes p where (p-1)/2 is prime too are called "safe"; we define |
134 | this for backward compatibility: */ | 138 | this for backward compatibility: */ |
135 | #define DH_CHECK_P_NOT_STRONG_PRIME DH_CHECK_P_NOT_SAFE_PRIME | 139 | #define DH_CHECK_P_NOT_STRONG_PRIME DH_CHECK_P_NOT_SAFE_PRIME |
@@ -168,6 +172,7 @@ void *DH_get_ex_data(DH *d, int idx); | |||
168 | DH * DH_generate_parameters(int prime_len,int generator, | 172 | DH * DH_generate_parameters(int prime_len,int generator, |
169 | void (*callback)(int,int,void *),void *cb_arg); | 173 | void (*callback)(int,int,void *),void *cb_arg); |
170 | int DH_check(const DH *dh,int *codes); | 174 | int DH_check(const DH *dh,int *codes); |
175 | int DH_check_pub_key(const DH *dh,const BIGNUM *pub_key, int *codes); | ||
171 | int DH_generate_key(DH *dh); | 176 | int DH_generate_key(DH *dh); |
172 | int DH_compute_key(unsigned char *key,const BIGNUM *pub_key,DH *dh); | 177 | int DH_compute_key(unsigned char *key,const BIGNUM *pub_key,DH *dh); |
173 | DH * d2i_DHparams(DH **a,const unsigned char **pp, long length); | 178 | DH * d2i_DHparams(DH **a,const unsigned char **pp, long length); |
@@ -200,6 +205,7 @@ void ERR_load_DH_strings(void); | |||
200 | /* Reason codes. */ | 205 | /* Reason codes. */ |
201 | #define DH_R_BAD_GENERATOR 101 | 206 | #define DH_R_BAD_GENERATOR 101 |
202 | #define DH_R_NO_PRIVATE_VALUE 100 | 207 | #define DH_R_NO_PRIVATE_VALUE 100 |
208 | #define DH_R_INVALID_PUBKEY 102 | ||
203 | 209 | ||
204 | #ifdef __cplusplus | 210 | #ifdef __cplusplus |
205 | } | 211 | } |
diff --git a/src/lib/libcrypto/dh/dh_check.c b/src/lib/libcrypto/dh/dh_check.c index a7e9920efb..17debff62d 100644 --- a/src/lib/libcrypto/dh/dh_check.c +++ b/src/lib/libcrypto/dh/dh_check.c | |||
@@ -121,4 +121,26 @@ err: | |||
121 | return(ok); | 121 | return(ok); |
122 | } | 122 | } |
123 | 123 | ||
124 | int DH_check_pub_key(const DH *dh, const BIGNUM *pub_key, int *ret) | ||
125 | { | ||
126 | int ok=0; | ||
127 | BIGNUM *q=NULL; | ||
128 | |||
129 | *ret=0; | ||
130 | q=BN_new(); | ||
131 | if (q == NULL) goto err; | ||
132 | BN_set_word(q,1); | ||
133 | if (BN_cmp(pub_key,q) <= 0) | ||
134 | *ret|=DH_CHECK_PUBKEY_TOO_SMALL; | ||
135 | BN_copy(q,dh->p); | ||
136 | BN_sub_word(q,1); | ||
137 | if (BN_cmp(pub_key,q) >= 0) | ||
138 | *ret|=DH_CHECK_PUBKEY_TOO_LARGE; | ||
139 | |||
140 | ok = 1; | ||
141 | err: | ||
142 | if (q != NULL) BN_free(q); | ||
143 | return(ok); | ||
144 | } | ||
145 | |||
124 | #endif | 146 | #endif |
diff --git a/src/lib/libcrypto/dh/dh_err.c b/src/lib/libcrypto/dh/dh_err.c index c2715044c9..914b8a9c53 100644 --- a/src/lib/libcrypto/dh/dh_err.c +++ b/src/lib/libcrypto/dh/dh_err.c | |||
@@ -79,6 +79,7 @@ static ERR_STRING_DATA DH_str_reasons[]= | |||
79 | { | 79 | { |
80 | {DH_R_BAD_GENERATOR ,"bad generator"}, | 80 | {DH_R_BAD_GENERATOR ,"bad generator"}, |
81 | {DH_R_NO_PRIVATE_VALUE ,"no private value"}, | 81 | {DH_R_NO_PRIVATE_VALUE ,"no private value"}, |
82 | {DH_R_INVALID_PUBKEY ,"invalid public key"}, | ||
82 | {0,NULL} | 83 | {0,NULL} |
83 | }; | 84 | }; |
84 | 85 | ||
diff --git a/src/lib/libcrypto/dh/dh_key.c b/src/lib/libcrypto/dh/dh_key.c index ff125c2296..648766a6ec 100644 --- a/src/lib/libcrypto/dh/dh_key.c +++ b/src/lib/libcrypto/dh/dh_key.c | |||
@@ -163,6 +163,7 @@ static int compute_key(unsigned char *key, const BIGNUM *pub_key, DH *dh) | |||
163 | BN_MONT_CTX *mont; | 163 | BN_MONT_CTX *mont; |
164 | BIGNUM *tmp; | 164 | BIGNUM *tmp; |
165 | int ret= -1; | 165 | int ret= -1; |
166 | int check_result; | ||
166 | 167 | ||
167 | ctx = BN_CTX_new(); | 168 | ctx = BN_CTX_new(); |
168 | if (ctx == NULL) goto err; | 169 | if (ctx == NULL) goto err; |
@@ -182,6 +183,12 @@ static int compute_key(unsigned char *key, const BIGNUM *pub_key, DH *dh) | |||
182 | } | 183 | } |
183 | 184 | ||
184 | mont=(BN_MONT_CTX *)dh->method_mont_p; | 185 | mont=(BN_MONT_CTX *)dh->method_mont_p; |
186 | |||
187 | if (!DH_check_pub_key(dh, pub_key, &check_result) || check_result) | ||
188 | { | ||
189 | DHerr(DH_F_DH_COMPUTE_KEY,DH_R_INVALID_PUBKEY); | ||
190 | goto err; | ||
191 | } | ||
185 | if (!dh->meth->bn_mod_exp(dh, tmp, pub_key, dh->priv_key,dh->p,ctx,mont)) | 192 | if (!dh->meth->bn_mod_exp(dh, tmp, pub_key, dh->priv_key,dh->p,ctx,mont)) |
186 | { | 193 | { |
187 | DHerr(DH_F_DH_COMPUTE_KEY,ERR_R_BN_LIB); | 194 | DHerr(DH_F_DH_COMPUTE_KEY,ERR_R_BN_LIB); |
diff --git a/src/lib/libssl/crypto/shlib_version b/src/lib/libssl/crypto/shlib_version index 56246d02b2..eb2c603aec 100644 --- a/src/lib/libssl/crypto/shlib_version +++ b/src/lib/libssl/crypto/shlib_version | |||
@@ -1,2 +1,2 @@ | |||
1 | major=12 | 1 | major=12 |
2 | minor=0 | 2 | minor=1 |
diff --git a/src/lib/libssl/src/crypto/dh/dh.h b/src/lib/libssl/src/crypto/dh/dh.h index d51dc130f4..0aff7fe21f 100644 --- a/src/lib/libssl/src/crypto/dh/dh.h +++ b/src/lib/libssl/src/crypto/dh/dh.h | |||
@@ -130,6 +130,10 @@ struct dh_st | |||
130 | #define DH_UNABLE_TO_CHECK_GENERATOR 0x04 | 130 | #define DH_UNABLE_TO_CHECK_GENERATOR 0x04 |
131 | #define DH_NOT_SUITABLE_GENERATOR 0x08 | 131 | #define DH_NOT_SUITABLE_GENERATOR 0x08 |
132 | 132 | ||
133 | /* DH_check_pub_key error codes */ | ||
134 | #define DH_CHECK_PUBKEY_TOO_SMALL 0x01 | ||
135 | #define DH_CHECK_PUBKEY_TOO_LARGE 0x02 | ||
136 | |||
133 | /* primes p where (p-1)/2 is prime too are called "safe"; we define | 137 | /* primes p where (p-1)/2 is prime too are called "safe"; we define |
134 | this for backward compatibility: */ | 138 | this for backward compatibility: */ |
135 | #define DH_CHECK_P_NOT_STRONG_PRIME DH_CHECK_P_NOT_SAFE_PRIME | 139 | #define DH_CHECK_P_NOT_STRONG_PRIME DH_CHECK_P_NOT_SAFE_PRIME |
@@ -168,6 +172,7 @@ void *DH_get_ex_data(DH *d, int idx); | |||
168 | DH * DH_generate_parameters(int prime_len,int generator, | 172 | DH * DH_generate_parameters(int prime_len,int generator, |
169 | void (*callback)(int,int,void *),void *cb_arg); | 173 | void (*callback)(int,int,void *),void *cb_arg); |
170 | int DH_check(const DH *dh,int *codes); | 174 | int DH_check(const DH *dh,int *codes); |
175 | int DH_check_pub_key(const DH *dh,const BIGNUM *pub_key, int *codes); | ||
171 | int DH_generate_key(DH *dh); | 176 | int DH_generate_key(DH *dh); |
172 | int DH_compute_key(unsigned char *key,const BIGNUM *pub_key,DH *dh); | 177 | int DH_compute_key(unsigned char *key,const BIGNUM *pub_key,DH *dh); |
173 | DH * d2i_DHparams(DH **a,const unsigned char **pp, long length); | 178 | DH * d2i_DHparams(DH **a,const unsigned char **pp, long length); |
@@ -200,6 +205,7 @@ void ERR_load_DH_strings(void); | |||
200 | /* Reason codes. */ | 205 | /* Reason codes. */ |
201 | #define DH_R_BAD_GENERATOR 101 | 206 | #define DH_R_BAD_GENERATOR 101 |
202 | #define DH_R_NO_PRIVATE_VALUE 100 | 207 | #define DH_R_NO_PRIVATE_VALUE 100 |
208 | #define DH_R_INVALID_PUBKEY 102 | ||
203 | 209 | ||
204 | #ifdef __cplusplus | 210 | #ifdef __cplusplus |
205 | } | 211 | } |
diff --git a/src/lib/libssl/src/crypto/dh/dh_check.c b/src/lib/libssl/src/crypto/dh/dh_check.c index a7e9920efb..17debff62d 100644 --- a/src/lib/libssl/src/crypto/dh/dh_check.c +++ b/src/lib/libssl/src/crypto/dh/dh_check.c | |||
@@ -121,4 +121,26 @@ err: | |||
121 | return(ok); | 121 | return(ok); |
122 | } | 122 | } |
123 | 123 | ||
124 | int DH_check_pub_key(const DH *dh, const BIGNUM *pub_key, int *ret) | ||
125 | { | ||
126 | int ok=0; | ||
127 | BIGNUM *q=NULL; | ||
128 | |||
129 | *ret=0; | ||
130 | q=BN_new(); | ||
131 | if (q == NULL) goto err; | ||
132 | BN_set_word(q,1); | ||
133 | if (BN_cmp(pub_key,q) <= 0) | ||
134 | *ret|=DH_CHECK_PUBKEY_TOO_SMALL; | ||
135 | BN_copy(q,dh->p); | ||
136 | BN_sub_word(q,1); | ||
137 | if (BN_cmp(pub_key,q) >= 0) | ||
138 | *ret|=DH_CHECK_PUBKEY_TOO_LARGE; | ||
139 | |||
140 | ok = 1; | ||
141 | err: | ||
142 | if (q != NULL) BN_free(q); | ||
143 | return(ok); | ||
144 | } | ||
145 | |||
124 | #endif | 146 | #endif |
diff --git a/src/lib/libssl/src/crypto/dh/dh_err.c b/src/lib/libssl/src/crypto/dh/dh_err.c index c2715044c9..914b8a9c53 100644 --- a/src/lib/libssl/src/crypto/dh/dh_err.c +++ b/src/lib/libssl/src/crypto/dh/dh_err.c | |||
@@ -79,6 +79,7 @@ static ERR_STRING_DATA DH_str_reasons[]= | |||
79 | { | 79 | { |
80 | {DH_R_BAD_GENERATOR ,"bad generator"}, | 80 | {DH_R_BAD_GENERATOR ,"bad generator"}, |
81 | {DH_R_NO_PRIVATE_VALUE ,"no private value"}, | 81 | {DH_R_NO_PRIVATE_VALUE ,"no private value"}, |
82 | {DH_R_INVALID_PUBKEY ,"invalid public key"}, | ||
82 | {0,NULL} | 83 | {0,NULL} |
83 | }; | 84 | }; |
84 | 85 | ||
diff --git a/src/lib/libssl/src/crypto/dh/dh_key.c b/src/lib/libssl/src/crypto/dh/dh_key.c index ff125c2296..648766a6ec 100644 --- a/src/lib/libssl/src/crypto/dh/dh_key.c +++ b/src/lib/libssl/src/crypto/dh/dh_key.c | |||
@@ -163,6 +163,7 @@ static int compute_key(unsigned char *key, const BIGNUM *pub_key, DH *dh) | |||
163 | BN_MONT_CTX *mont; | 163 | BN_MONT_CTX *mont; |
164 | BIGNUM *tmp; | 164 | BIGNUM *tmp; |
165 | int ret= -1; | 165 | int ret= -1; |
166 | int check_result; | ||
166 | 167 | ||
167 | ctx = BN_CTX_new(); | 168 | ctx = BN_CTX_new(); |
168 | if (ctx == NULL) goto err; | 169 | if (ctx == NULL) goto err; |
@@ -182,6 +183,12 @@ static int compute_key(unsigned char *key, const BIGNUM *pub_key, DH *dh) | |||
182 | } | 183 | } |
183 | 184 | ||
184 | mont=(BN_MONT_CTX *)dh->method_mont_p; | 185 | mont=(BN_MONT_CTX *)dh->method_mont_p; |
186 | |||
187 | if (!DH_check_pub_key(dh, pub_key, &check_result) || check_result) | ||
188 | { | ||
189 | DHerr(DH_F_DH_COMPUTE_KEY,DH_R_INVALID_PUBKEY); | ||
190 | goto err; | ||
191 | } | ||
185 | if (!dh->meth->bn_mod_exp(dh, tmp, pub_key, dh->priv_key,dh->p,ctx,mont)) | 192 | if (!dh->meth->bn_mod_exp(dh, tmp, pub_key, dh->priv_key,dh->p,ctx,mont)) |
186 | { | 193 | { |
187 | DHerr(DH_F_DH_COMPUTE_KEY,ERR_R_BN_LIB); | 194 | DHerr(DH_F_DH_COMPUTE_KEY,ERR_R_BN_LIB); |