1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338
|
/*
* libssl_compat.c -- OpenSSL v1.1 compatibility functions
*
* ---------------------------------------------------------------------
* Written by Juergen Perlinger <perlinger@ntp.org> for the NTP project
*
* Based on an idea by Kurt Roeckx <kurt@roeckx.be>
*
* ---------------------------------------------------------------------
* This is a clean room implementation of shim functions that have
* counterparts in the OpenSSL v1.1 API but not in earlier versions. So
* while OpenSSL broke binary compatibility with v1.1, this shim module
* should provide the necessary source code compatibility with older
* versions of OpenSSL.
* ---------------------------------------------------------------------
*/
#include "config.h"
#include "ntp_types.h"
/* ----------------------------------------------------------------- */
#ifdef OPENSSL
# include <string.h>
# include <openssl/bn.h>
# include <openssl/evp.h>
#endif
/* ----------------------------------------------------------------- */
/* ----------------------------------------------------------------- */
#if defined(OPENSSL) && OPENSSL_VERSION_NUMBER < 0x10100000L
/* ----------------------------------------------------------------- */
#include "libssl_compat.h"
#include "ntp_assert.h"
/* --------------------------------------------------------------------
* replace a BIGNUM owned by the caller with another one if it's not
* NULL, taking over the ownership of the new value. This clears & frees
* the old value -- the clear might be overkill, but it's better to err
* on the side of paranoia here.
*/
static void
replace_bn_nn(
BIGNUM ** ps,
BIGNUM * n
)
{
if (n) {
REQUIRE(*ps != n);
BN_clear_free(*ps);
*ps = n;
}
}
/* --------------------------------------------------------------------
* allocation and deallocation of prime number callbacks
*/
BN_GENCB*
sslshimBN_GENCB_new(void)
{
return calloc(1,sizeof(BN_GENCB));
}
void
sslshimBN_GENCB_free(
BN_GENCB *cb
)
{
free(cb);
}
/* --------------------------------------------------------------------
* allocation and deallocation of message digests
*/
EVP_MD_CTX*
sslshim_EVP_MD_CTX_new(void)
{
return calloc(1, sizeof(EVP_MD_CTX));
}
void
sslshim_EVP_MD_CTX_free(
EVP_MD_CTX * pctx
)
{
free(pctx);
}
/* --------------------------------------------------------------------
* get EVP keys and key type
*/
int
sslshim_EVP_PKEY_id(
const EVP_PKEY *pkey
)
{
return (pkey) ? pkey->type : EVP_PKEY_NONE;
}
int
sslshim_EVP_PKEY_base_id(
const EVP_PKEY *pkey
)
{
return (pkey) ? EVP_PKEY_type(pkey->type) : EVP_PKEY_NONE;
}
RSA*
sslshim_EVP_PKEY_get0_RSA(
EVP_PKEY * pkey
)
{
return (pkey) ? pkey->pkey.rsa : NULL;
}
DSA*
sslshim_EVP_PKEY_get0_DSA(
EVP_PKEY * pkey
)
{
return (pkey) ? pkey->pkey.dsa : NULL;
}
/* --------------------------------------------------------------------
* set/get RSA params
*/
void
sslshim_RSA_get0_key(
const RSA * prsa,
const BIGNUM ** pn,
const BIGNUM ** pe,
const BIGNUM ** pd
)
{
REQUIRE(prsa != NULL);
if (pn)
*pn = prsa->n;
if (pe)
*pe = prsa->e;
if (pd)
*pd = prsa->d;
}
int
sslshim_RSA_set0_key(
RSA * prsa,
BIGNUM * n,
BIGNUM * e,
BIGNUM * d
)
{
REQUIRE(prsa != NULL);
if (!((prsa->n || n) && (prsa->e || e)))
return 0;
replace_bn_nn(&prsa->n, n);
replace_bn_nn(&prsa->e, e);
replace_bn_nn(&prsa->d, d);
return 1;
}
void
sslshim_RSA_get0_factors(
const RSA * prsa,
const BIGNUM ** pp,
const BIGNUM ** pq
)
{
REQUIRE(prsa != NULL);
if (pp)
*pp = prsa->p;
if (pq)
*pq = prsa->q;
}
int
sslshim_RSA_set0_factors(
RSA * prsa,
BIGNUM * p,
BIGNUM * q
)
{
REQUIRE(prsa != NULL);
if (!((prsa->p || p) && (prsa->q || q)))
return 0;
replace_bn_nn(&prsa->p, p);
replace_bn_nn(&prsa->q, q);
return 1;
}
int
sslshim_RSA_set0_crt_params(
RSA * prsa,
BIGNUM * dmp1,
BIGNUM * dmq1,
BIGNUM * iqmp
)
{
REQUIRE(prsa != NULL);
if (!((prsa->dmp1 || dmp1) &&
(prsa->dmq1 || dmq1) &&
(prsa->iqmp || iqmp) ))
return 0;
replace_bn_nn(&prsa->dmp1, dmp1);
replace_bn_nn(&prsa->dmq1, dmq1);
replace_bn_nn(&prsa->iqmp, iqmp);
return 1;
}
/* --------------------------------------------------------------------
* set/get DSA signature parameters
*/
void
sslshim_DSA_SIG_get0(
const DSA_SIG * psig,
const BIGNUM ** pr,
const BIGNUM ** ps
)
{
REQUIRE(psig != NULL);
if (pr != NULL)
*pr = psig->r;
if (ps != NULL)
*ps = psig->s;
}
int
sslshim_DSA_SIG_set0(
DSA_SIG * psig,
BIGNUM * r,
BIGNUM * s
)
{
REQUIRE(psig != NULL);
if (!(r && s))
return 0;
replace_bn_nn(&psig->r, r);
replace_bn_nn(&psig->s, s);
return 1;
}
/* --------------------------------------------------------------------
* get/set DSA parameters
*/
void
sslshim_DSA_get0_pqg(
const DSA * pdsa,
const BIGNUM ** pp,
const BIGNUM ** pq,
const BIGNUM ** pg
)
{
REQUIRE(pdsa != NULL);
if (pp != NULL)
*pp = pdsa->p;
if (pq != NULL)
*pq = pdsa->q;
if (pg != NULL)
*pg = pdsa->g;
}
int
sslshim_DSA_set0_pqg(
DSA * pdsa,
BIGNUM * p,
BIGNUM * q,
BIGNUM * g
)
{
if (!((pdsa->p || p) && (pdsa->q || q) && (pdsa->g || g)))
return 0;
replace_bn_nn(&pdsa->p, p);
replace_bn_nn(&pdsa->q, q);
replace_bn_nn(&pdsa->g, g);
return 1;
}
void
sslshim_DSA_get0_key(
const DSA * pdsa,
const BIGNUM ** ppub_key,
const BIGNUM ** ppriv_key
)
{
REQUIRE(pdsa != NULL);
if (ppub_key != NULL)
*ppub_key = pdsa->pub_key;
if (ppriv_key != NULL)
*ppriv_key = pdsa->priv_key;
}
int
sslshim_DSA_set0_key(
DSA * pdsa,
BIGNUM * pub_key,
BIGNUM * priv_key
)
{
REQUIRE(pdsa != NULL);
if (!(pdsa->pub_key || pub_key))
return 0;
replace_bn_nn(&pdsa->pub_key, pub_key);
replace_bn_nn(&pdsa->priv_key, priv_key);
return 1;
}
int
sslshim_X509_get_signature_nid(
const X509 *x
)
{
return OBJ_obj2nid(x->sig_alg->algorithm);
}
/* ----------------------------------------------------------------- */
#else /* OPENSSL && OPENSSL_VERSION_NUMBER >= v1.1.0 */
/* ----------------------------------------------------------------- */
NONEMPTY_TRANSLATION_UNIT
/* ----------------------------------------------------------------- */
#endif
/* ----------------------------------------------------------------- */
|