| 1 |
/* |
| 2 |
* Copyright (c) 2010 SURFnet bv |
| 3 |
* All rights reserved. |
| 4 |
* |
| 5 |
* Redistribution and use in source and binary forms, with or without |
| 6 |
* modification, are permitted provided that the following conditions |
| 7 |
* are met: |
| 8 |
* 1. Redistributions of source code must retain the above copyright |
| 9 |
* notice, this list of conditions and the following disclaimer. |
| 10 |
* 2. Redistributions in binary form must reproduce the above copyright |
| 11 |
* notice, this list of conditions and the following disclaimer in the |
| 12 |
* documentation and/or other materials provided with the distribution. |
| 13 |
* |
| 14 |
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 15 |
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 16 |
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 17 |
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY |
| 18 |
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 19 |
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE |
| 20 |
* GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 21 |
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER |
| 22 |
* IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
| 23 |
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN |
| 24 |
* IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 25 |
*/ |
| 26 |
|
| 27 |
/***************************************************************************** |
| 28 |
OSSLDHPublicKey.cpp |
| 29 |
|
| 30 |
OpenSSL Diffie-Hellman public key class |
| 31 |
*****************************************************************************/ |
| 32 |
|
| 33 |
#include "config.h" |
| 34 |
#include "log.h" |
| 35 |
#include "OSSLComp.h" |
| 36 |
#include "OSSLDHPublicKey.h" |
| 37 |
#include "OSSLUtil.h" |
| 38 |
#include <openssl/bn.h> |
| 39 |
#ifdef WITH_FIPS |
| 40 |
#include <openssl/fips.h> |
| 41 |
#endif |
| 42 |
#include <string.h> |
| 43 |
|
| 44 |
// Constructors |
| 45 |
OSSLDHPublicKey::OSSLDHPublicKey() |
| 46 |
{ |
| 47 |
dh = NULL; |
| 48 |
} |
| 49 |
|
| 50 |
OSSLDHPublicKey::OSSLDHPublicKey(const DH* inDH) |
| 51 |
{ |
| 52 |
dh = NULL; |
| 53 |
|
| 54 |
setFromOSSL(inDH); |
| 55 |
} |
| 56 |
|
| 57 |
// Destructor |
| 58 |
OSSLDHPublicKey::~OSSLDHPublicKey() |
| 59 |
{ |
| 60 |
DH_free(dh); |
| 61 |
} |
| 62 |
|
| 63 |
// The type |
| 64 |
/*static*/ const char* OSSLDHPublicKey::type = "OpenSSL DH Public Key"; |
| 65 |
|
| 66 |
// Set from OpenSSL representation |
| 67 |
void OSSLDHPublicKey::setFromOSSL(const DH* inDH) |
| 68 |
{ |
| 69 |
const BIGNUM* bn_p = NULL; |
| 70 |
const BIGNUM* bn_g = NULL; |
| 71 |
const BIGNUM* bn_pub_key = NULL; |
| 72 |
|
| 73 |
DH_get0_pqg(inDH, &bn_p, NULL, &bn_g); |
| 74 |
DH_get0_key(inDH, &bn_pub_key, NULL); |
| 75 |
|
| 76 |
if (bn_p) |
| 77 |
{ |
| 78 |
ByteString inP = OSSL::bn2ByteString(bn_p); |
| 79 |
setP(inP); |
| 80 |
} |
| 81 |
if (bn_g) |
| 82 |
{ |
| 83 |
ByteString inG = OSSL::bn2ByteString(bn_g); |
| 84 |
setG(inG); |
| 85 |
} |
| 86 |
if (bn_pub_key) |
| 87 |
{ |
| 88 |
ByteString inY = OSSL::bn2ByteString(bn_pub_key); |
| 89 |
setY(inY); |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
// Check if the key is of the given type |
| 94 |
bool OSSLDHPublicKey::isOfType(const char* inType) |
| 95 |
{ |
| 96 |
return !strcmp(type, inType); |
| 97 |
} |
| 98 |
|
| 99 |
// Setters for the DH public key components |
| 100 |
void OSSLDHPublicKey::setP(const ByteString& inP) |
| 101 |
{ |
| 102 |
DHPublicKey::setP(inP); |
| 103 |
|
| 104 |
if (dh) |
| 105 |
{ |
| 106 |
DH_free(dh); |
| 107 |
dh = NULL; |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
void OSSLDHPublicKey::setG(const ByteString& inG) |
| 112 |
{ |
| 113 |
DHPublicKey::setG(inG); |
| 114 |
|
| 115 |
if (dh) |
| 116 |
{ |
| 117 |
DH_free(dh); |
| 118 |
dh = NULL; |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
void OSSLDHPublicKey::setY(const ByteString& inY) |
| 123 |
{ |
| 124 |
DHPublicKey::setY(inY); |
| 125 |
|
| 126 |
if (dh) |
| 127 |
{ |
| 128 |
DH_free(dh); |
| 129 |
dh = NULL; |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
// Retrieve the OpenSSL representation of the key |
| 134 |
DH* OSSLDHPublicKey::getOSSLKey() |
| 135 |
{ |
| 136 |
if (dh == NULL) createOSSLKey(); |
| 137 |
|
| 138 |
return dh; |
| 139 |
} |
| 140 |
|
| 141 |
// Create the OpenSSL representation of the key |
| 142 |
void OSSLDHPublicKey::createOSSLKey() |
| 143 |
{ |
| 144 |
if (dh != NULL) return; |
| 145 |
|
| 146 |
dh = DH_new(); |
| 147 |
if (dh == NULL) |
| 148 |
{ |
| 149 |
ERROR_MSG("Could not create DH object"); |
| 150 |
return; |
| 151 |
} |
| 152 |
|
| 153 |
// Use the OpenSSL implementation and not any engine |
| 154 |
#if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER) |
| 155 |
|
| 156 |
#ifdef WITH_FIPS |
| 157 |
if (FIPS_mode()) |
| 158 |
DH_set_method(dh, FIPS_dh_openssl()); |
| 159 |
else |
| 160 |
DH_set_method(dh, DH_OpenSSL()); |
| 161 |
#else |
| 162 |
DH_set_method(dh, DH_OpenSSL()); |
| 163 |
#endif |
| 164 |
|
| 165 |
#else |
| 166 |
DH_set_method(dh, DH_OpenSSL()); |
| 167 |
#endif |
| 168 |
|
| 169 |
BIGNUM* bn_p = OSSL::byteString2bn(p); |
| 170 |
BIGNUM* bn_g = OSSL::byteString2bn(g); |
| 171 |
BIGNUM* bn_pub_key = OSSL::byteString2bn(y); |
| 172 |
|
| 173 |
DH_set0_pqg(dh, bn_p, NULL, bn_g); |
| 174 |
DH_set0_key(dh, bn_pub_key, NULL); |
| 175 |
} |