summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/dh/dh_check.c
diff options
context:
space:
mode:
authortb <>2022-01-10 12:00:52 +0000
committertb <>2022-01-10 12:00:52 +0000
commit66fc1114c3900ed41771dba70d33a853a0a925f4 (patch)
treea4d80645cff5817beab5299ea58a2124c2e04ea1 /src/lib/libcrypto/dh/dh_check.c
parenta447c077ad67d6e81ed1a4fbe9003875add773c2 (diff)
downloadopenbsd-66fc1114c3900ed41771dba70d33a853a0a925f4.tar.gz
openbsd-66fc1114c3900ed41771dba70d33a853a0a925f4.tar.bz2
openbsd-66fc1114c3900ed41771dba70d33a853a0a925f4.zip
Provide DH_check*_ex and many error codes
DH_check{,_pub_key}_ex() wrap their non-ex versions to translate the flags argument of the original functions into OpenSSL errors. For this almost a dozen new error codes need to be added. DH_params_check{,_ex}() is a new version of DH_check that only performs a cheap subset of the checks. They are needed to implement EVP_PKEY_{public,param}_check() (observe the consistent naming) although the actual implementation of EVP_PKEY_param_check() chose to use DH_check_ex(). As far as I can tell, the only raison d'ĂȘtre of the _ex functions and error codes is to spew them to stderr in a couple of openssl(1) commands. This couldn't have been solved differently... These functions will not be exposed publicly. ok inoguchi jsing
Diffstat (limited to 'src/lib/libcrypto/dh/dh_check.c')
-rw-r--r--src/lib/libcrypto/dh/dh_check.c69
1 files changed, 68 insertions, 1 deletions
diff --git a/src/lib/libcrypto/dh/dh_check.c b/src/lib/libcrypto/dh/dh_check.c
index 7203936611..1d20952e26 100644
--- a/src/lib/libcrypto/dh/dh_check.c
+++ b/src/lib/libcrypto/dh/dh_check.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: dh_check.c,v 1.23 2022/01/07 09:27:13 tb Exp $ */ 1/* $OpenBSD: dh_check.c,v 1.24 2022/01/10 12:00:52 tb 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 *
@@ -60,12 +60,34 @@
60 60
61#include <openssl/bn.h> 61#include <openssl/bn.h>
62#include <openssl/dh.h> 62#include <openssl/dh.h>
63#include <openssl/err.h>
63 64
64#include "bn_lcl.h" 65#include "bn_lcl.h"
65#include "dh_local.h" 66#include "dh_local.h"
66 67
67#define DH_NUMBER_ITERATIONS_FOR_PRIME 64 68#define DH_NUMBER_ITERATIONS_FOR_PRIME 64
68 69
70/*
71 * Check that p is odd and 1 < g < p - 1. The _ex version removes the need of
72 * inspecting flags and pushes errors on the stack instead.
73 */
74
75int
76DH_check_params_ex(const DH *dh)
77{
78 int flags = 0;
79
80 if (!DH_check_params(dh, &flags))
81 return 0;
82
83 if ((flags & DH_CHECK_P_NOT_PRIME) != 0)
84 DHerror(DH_R_CHECK_P_NOT_PRIME);
85 if ((flags & DH_NOT_SUITABLE_GENERATOR) != 0)
86 DHerror(DH_R_NOT_SUITABLE_GENERATOR);
87
88 return flags == 0;
89}
90
69int 91int
70DH_check_params(const DH *dh, int *flags) 92DH_check_params(const DH *dh, int *flags)
71{ 93{
@@ -102,9 +124,36 @@ DH_check_params(const DH *dh, int *flags)
102 124
103/* 125/*
104 * Check that p is a safe prime and that g is a suitable generator. 126 * Check that p is a safe prime and that g is a suitable generator.
127 * The _ex version puts errors on the stack instead of returning flags.
105 */ 128 */
106 129
107int 130int
131DH_check_ex(const DH *dh)
132{
133 int flags = 0;
134
135 if (!DH_check(dh, &flags))
136 return 0;
137
138 if ((flags & DH_NOT_SUITABLE_GENERATOR) != 0)
139 DHerror(DH_R_NOT_SUITABLE_GENERATOR);
140 if ((flags & DH_CHECK_Q_NOT_PRIME) != 0)
141 DHerror(DH_R_CHECK_Q_NOT_PRIME);
142 if ((flags & DH_CHECK_INVALID_Q_VALUE) != 0)
143 DHerror(DH_R_CHECK_INVALID_Q_VALUE);
144 if ((flags & DH_CHECK_INVALID_J_VALUE) != 0)
145 DHerror(DH_R_CHECK_INVALID_J_VALUE);
146 if ((flags & DH_UNABLE_TO_CHECK_GENERATOR) != 0)
147 DHerror(DH_R_UNABLE_TO_CHECK_GENERATOR);
148 if ((flags & DH_CHECK_P_NOT_PRIME) != 0)
149 DHerror(DH_R_CHECK_P_NOT_PRIME);
150 if ((flags & DH_CHECK_P_NOT_SAFE_PRIME) != 0)
151 DHerror(DH_R_CHECK_P_NOT_SAFE_PRIME);
152
153 return flags == 0;
154}
155
156int
108DH_check(const DH *dh, int *flags) 157DH_check(const DH *dh, int *flags)
109{ 158{
110 BN_CTX *ctx = NULL; 159 BN_CTX *ctx = NULL;
@@ -180,6 +229,24 @@ DH_check(const DH *dh, int *flags)
180} 229}
181 230
182int 231int
232DH_check_pub_key_ex(const DH *dh, const BIGNUM *pub_key)
233{
234 int flags = 0;
235
236 if (!DH_check_pub_key(dh, pub_key, &flags))
237 return 0;
238
239 if ((flags & DH_CHECK_PUBKEY_TOO_SMALL) != 0)
240 DHerror(DH_R_CHECK_PUBKEY_TOO_SMALL);
241 if ((flags & DH_CHECK_PUBKEY_TOO_LARGE) != 0)
242 DHerror(DH_R_CHECK_PUBKEY_TOO_LARGE);
243 if ((flags & DH_CHECK_PUBKEY_INVALID) != 0)
244 DHerror(DH_R_CHECK_PUBKEY_INVALID);
245
246 return flags == 0;
247}
248
249int
183DH_check_pub_key(const DH *dh, const BIGNUM *pub_key, int *flags) 250DH_check_pub_key(const DH *dh, const BIGNUM *pub_key, int *flags)
184{ 251{
185 BN_CTX *ctx = NULL; 252 BN_CTX *ctx = NULL;