| 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 |
OSSLDSAPublicKey.cpp |
| 29 |
|
| 30 |
OpenSSL DSA public key class |
| 31 |
*****************************************************************************/ |
| 32 |
|
| 33 |
#include "config.h" |
| 34 |
#include "log.h" |
| 35 |
#include "OSSLDSAPublicKey.h" |
| 36 |
#include "OSSLComp.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 |
OSSLDSAPublicKey::OSSLDSAPublicKey() |
| 46 |
{ |
| 47 |
dsa = NULL; |
| 48 |
} |
| 49 |
|
| 50 |
OSSLDSAPublicKey::OSSLDSAPublicKey(const DSA* inDSA) |
| 51 |
{ |
| 52 |
dsa = NULL; |
| 53 |
|
| 54 |
setFromOSSL(inDSA); |
| 55 |
} |
| 56 |
|
| 57 |
// Destructor |
| 58 |
OSSLDSAPublicKey::~OSSLDSAPublicKey() |
| 59 |
{ |
| 60 |
DSA_free(dsa); |
| 61 |
} |
| 62 |
|
| 63 |
// The type |
| 64 |
/*static*/ const char* OSSLDSAPublicKey::type = "OpenSSL DSA Public Key"; |
| 65 |
|
| 66 |
// Set from OpenSSL representation |
| 67 |
void OSSLDSAPublicKey::setFromOSSL(const DSA* inDSA) |
| 68 |
{ |
| 69 |
const BIGNUM* bn_p = NULL; |
| 70 |
const BIGNUM* bn_q = NULL; |
| 71 |
const BIGNUM* bn_g = NULL; |
| 72 |
const BIGNUM* bn_pub_key = NULL; |
| 73 |
|
| 74 |
DSA_get0_pqg(inDSA, &bn_p, &bn_q, &bn_g); |
| 75 |
DSA_get0_key(inDSA, &bn_pub_key, NULL); |
| 76 |
|
| 77 |
if (bn_p) |
| 78 |
{ |
| 79 |
ByteString inP = OSSL::bn2ByteString(bn_p); |
| 80 |
setP(inP); |
| 81 |
} |
| 82 |
if (bn_q) |
| 83 |
{ |
| 84 |
ByteString inQ = OSSL::bn2ByteString(bn_q); |
| 85 |
setQ(inQ); |
| 86 |
} |
| 87 |
if (bn_g) |
| 88 |
{ |
| 89 |
ByteString inG = OSSL::bn2ByteString(bn_g); |
| 90 |
setG(inG); |
| 91 |
} |
| 92 |
if (bn_pub_key) |
| 93 |
{ |
| 94 |
ByteString inY = OSSL::bn2ByteString(bn_pub_key); |
| 95 |
setY(inY); |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
// Check if the key is of the given type |
| 100 |
bool OSSLDSAPublicKey::isOfType(const char* inType) |
| 101 |
{ |
| 102 |
return !strcmp(type, inType); |
| 103 |
} |
| 104 |
|
| 105 |
// Setters for the DSA public key components |
| 106 |
void OSSLDSAPublicKey::setP(const ByteString& inP) |
| 107 |
{ |
| 108 |
DSAPublicKey::setP(inP); |
| 109 |
|
| 110 |
if (dsa) |
| 111 |
{ |
| 112 |
DSA_free(dsa); |
| 113 |
dsa = NULL; |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
void OSSLDSAPublicKey::setQ(const ByteString& inQ) |
| 118 |
{ |
| 119 |
DSAPublicKey::setQ(inQ); |
| 120 |
|
| 121 |
if (dsa) |
| 122 |
{ |
| 123 |
DSA_free(dsa); |
| 124 |
dsa = NULL; |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
void OSSLDSAPublicKey::setG(const ByteString& inG) |
| 129 |
{ |
| 130 |
DSAPublicKey::setG(inG); |
| 131 |
|
| 132 |
if (dsa) |
| 133 |
{ |
| 134 |
DSA_free(dsa); |
| 135 |
dsa = NULL; |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
void OSSLDSAPublicKey::setY(const ByteString& inY) |
| 140 |
{ |
| 141 |
DSAPublicKey::setY(inY); |
| 142 |
|
| 143 |
if (dsa) |
| 144 |
{ |
| 145 |
DSA_free(dsa); |
| 146 |
dsa = NULL; |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
// Retrieve the OpenSSL representation of the key |
| 151 |
DSA* OSSLDSAPublicKey::getOSSLKey() |
| 152 |
{ |
| 153 |
if (dsa == NULL) createOSSLKey(); |
| 154 |
|
| 155 |
return dsa; |
| 156 |
} |
| 157 |
|
| 158 |
// Create the OpenSSL representation of the key |
| 159 |
void OSSLDSAPublicKey::createOSSLKey() |
| 160 |
{ |
| 161 |
if (dsa != NULL) return; |
| 162 |
|
| 163 |
dsa = DSA_new(); |
| 164 |
if (dsa == NULL) |
| 165 |
{ |
| 166 |
ERROR_MSG("Could not create DSA object"); |
| 167 |
return; |
| 168 |
} |
| 169 |
|
| 170 |
// Use the OpenSSL implementation and not any engine |
| 171 |
#if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER) |
| 172 |
|
| 173 |
#ifdef WITH_FIPS |
| 174 |
if (FIPS_mode()) |
| 175 |
DSA_set_method(dsa, FIPS_dsa_openssl()); |
| 176 |
else |
| 177 |
DSA_set_method(dsa, DSA_OpenSSL()); |
| 178 |
#else |
| 179 |
DSA_set_method(dsa, DSA_OpenSSL()); |
| 180 |
#endif |
| 181 |
|
| 182 |
#else |
| 183 |
DSA_set_method(dsa, DSA_OpenSSL()); |
| 184 |
#endif |
| 185 |
|
| 186 |
BIGNUM* bn_p = OSSL::byteString2bn(p); |
| 187 |
BIGNUM* bn_q = OSSL::byteString2bn(q); |
| 188 |
BIGNUM* bn_g = OSSL::byteString2bn(g); |
| 189 |
BIGNUM* bn_pub_key = OSSL::byteString2bn(y); |
| 190 |
|
| 191 |
DSA_set0_pqg(dsa, bn_p, bn_q, bn_g); |
| 192 |
DSA_set0_key(dsa, bn_pub_key, NULL); |
| 193 |
} |