| 1 |
/* |
| 2 |
* Copyright (c) 2010 .SE (The Internet Infrastructure Foundation) |
| 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 |
BotanECDSAPrivateKey.cpp |
| 29 |
|
| 30 |
Botan ECDSA private key class |
| 31 |
*****************************************************************************/ |
| 32 |
|
| 33 |
#include "config.h" |
| 34 |
#ifdef WITH_ECC |
| 35 |
#include "log.h" |
| 36 |
#include "BotanECDSAPrivateKey.h" |
| 37 |
#include "BotanCryptoFactory.h" |
| 38 |
#include "BotanRNG.h" |
| 39 |
#include "BotanUtil.h" |
| 40 |
#include <string.h> |
| 41 |
#include <botan/pkcs8.h> |
| 42 |
#include <botan/ber_dec.h> |
| 43 |
#include <botan/der_enc.h> |
| 44 |
#include <botan/asn1_oid.h> |
| 45 |
#include <botan/oids.h> |
| 46 |
#include <botan/version.h> |
| 47 |
|
| 48 |
// Constructors |
| 49 |
BotanECDSAPrivateKey::BotanECDSAPrivateKey() |
| 50 |
{ |
| 51 |
eckey = NULL; |
| 52 |
} |
| 53 |
|
| 54 |
BotanECDSAPrivateKey::BotanECDSAPrivateKey(const Botan::ECDSA_PrivateKey* inECKEY) |
| 55 |
{ |
| 56 |
eckey = NULL; |
| 57 |
|
| 58 |
setFromBotan(inECKEY); |
| 59 |
} |
| 60 |
|
| 61 |
// Destructor |
| 62 |
BotanECDSAPrivateKey::~BotanECDSAPrivateKey() |
| 63 |
{ |
| 64 |
delete eckey; |
| 65 |
} |
| 66 |
|
| 67 |
// The type |
| 68 |
/*static*/ const char* BotanECDSAPrivateKey::type = "Botan ECDSA Private Key"; |
| 69 |
|
| 70 |
// Get the base point order length |
| 71 |
unsigned long BotanECDSAPrivateKey::getOrderLength() const |
| 72 |
{ |
| 73 |
try |
| 74 |
{ |
| 75 |
Botan::EC_Group group = BotanUtil::byteString2ECGroup(ec); |
| 76 |
return group.get_order().bytes(); |
| 77 |
} |
| 78 |
catch (...) |
| 79 |
{ |
| 80 |
ERROR_MSG("Can't get EC group for order length"); |
| 81 |
|
| 82 |
return 0; |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
// Set from Botan representation |
| 87 |
void BotanECDSAPrivateKey::setFromBotan(const Botan::ECDSA_PrivateKey* inECKEY) |
| 88 |
{ |
| 89 |
ByteString inEC = BotanUtil::ecGroup2ByteString(inECKEY->domain()); |
| 90 |
setEC(inEC); |
| 91 |
ByteString inD = BotanUtil::bigInt2ByteString(inECKEY->private_value()); |
| 92 |
setD(inD); |
| 93 |
} |
| 94 |
|
| 95 |
// Check if the key is of the given type |
| 96 |
bool BotanECDSAPrivateKey::isOfType(const char* inType) |
| 97 |
{ |
| 98 |
return !strcmp(type, inType); |
| 99 |
} |
| 100 |
|
| 101 |
// Setters for the ECDSA private key components |
| 102 |
void BotanECDSAPrivateKey::setD(const ByteString& inD) |
| 103 |
{ |
| 104 |
ECPrivateKey::setD(inD); |
| 105 |
|
| 106 |
if (eckey) |
| 107 |
{ |
| 108 |
delete eckey; |
| 109 |
eckey = NULL; |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
// Setters for the ECDSA public key components |
| 114 |
void BotanECDSAPrivateKey::setEC(const ByteString& inEC) |
| 115 |
{ |
| 116 |
ECPrivateKey::setEC(inEC); |
| 117 |
|
| 118 |
if (eckey) |
| 119 |
{ |
| 120 |
delete eckey; |
| 121 |
eckey = NULL; |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
// Encode into PKCS#8 DER |
| 126 |
ByteString BotanECDSAPrivateKey::PKCS8Encode() |
| 127 |
{ |
| 128 |
ByteString der; |
| 129 |
createBotanKey(); |
| 130 |
if (eckey == NULL) return der; |
| 131 |
// Force EC_DOMPAR_ENC_OID |
| 132 |
const size_t PKCS8_VERSION = 0; |
| 133 |
const std::vector<uint8_t> parameters = eckey->domain().DER_encode(Botan::EC_DOMPAR_ENC_OID); |
| 134 |
const Botan::AlgorithmIdentifier alg_id(eckey->get_oid(), parameters); |
| 135 |
const Botan::secure_vector<uint8_t> ber = |
| 136 |
Botan::DER_Encoder() |
| 137 |
.start_cons(Botan::SEQUENCE) |
| 138 |
.encode(PKCS8_VERSION) |
| 139 |
.encode(alg_id) |
| 140 |
.encode(eckey->private_key_bits(), Botan::OCTET_STRING) |
| 141 |
.end_cons() |
| 142 |
.get_contents(); |
| 143 |
der.resize(ber.size()); |
| 144 |
memcpy(&der[0], &ber[0], ber.size()); |
| 145 |
return der; |
| 146 |
} |
| 147 |
|
| 148 |
// Decode from PKCS#8 BER |
| 149 |
bool BotanECDSAPrivateKey::PKCS8Decode(const ByteString& ber) |
| 150 |
{ |
| 151 |
Botan::DataSource_Memory source(ber.const_byte_str(), ber.size()); |
| 152 |
if (source.end_of_data()) return false; |
| 153 |
Botan::secure_vector<uint8_t> keydata; |
| 154 |
Botan::AlgorithmIdentifier alg_id; |
| 155 |
Botan::ECDSA_PrivateKey* key = NULL; |
| 156 |
try |
| 157 |
{ |
| 158 |
Botan::BER_Decoder(source) |
| 159 |
.start_cons(Botan::SEQUENCE) |
| 160 |
.decode_and_check<size_t>(0, "Unknown PKCS #8 version number") |
| 161 |
.decode(alg_id) |
| 162 |
.decode(keydata, Botan::OCTET_STRING) |
| 163 |
.discard_remaining() |
| 164 |
.end_cons(); |
| 165 |
if (keydata.empty()) |
| 166 |
throw Botan::Decoding_Error("PKCS #8 private key decoding failed"); |
| 167 |
if (Botan::OIDS::lookup(alg_id.oid).compare("ECDSA")) |
| 168 |
{ |
| 169 |
ERROR_MSG("Decoded private key not ECDSA"); |
| 170 |
|
| 171 |
return false; |
| 172 |
} |
| 173 |
key = new Botan::ECDSA_PrivateKey(alg_id, keydata); |
| 174 |
if (key == NULL) return false; |
| 175 |
|
| 176 |
setFromBotan(key); |
| 177 |
|
| 178 |
delete key; |
| 179 |
} |
| 180 |
catch (std::exception& e) |
| 181 |
{ |
| 182 |
ERROR_MSG("Decode failed on %s", e.what()); |
| 183 |
|
| 184 |
return false; |
| 185 |
} |
| 186 |
|
| 187 |
return true; |
| 188 |
} |
| 189 |
|
| 190 |
// Retrieve the Botan representation of the key |
| 191 |
Botan::ECDSA_PrivateKey* BotanECDSAPrivateKey::getBotanKey() |
| 192 |
{ |
| 193 |
if (!eckey) |
| 194 |
{ |
| 195 |
createBotanKey(); |
| 196 |
} |
| 197 |
|
| 198 |
return eckey; |
| 199 |
} |
| 200 |
|
| 201 |
// Create the Botan representation of the key |
| 202 |
void BotanECDSAPrivateKey::createBotanKey() |
| 203 |
{ |
| 204 |
if (ec.size() != 0 && |
| 205 |
d.size() != 0) |
| 206 |
{ |
| 207 |
if (eckey) |
| 208 |
{ |
| 209 |
delete eckey; |
| 210 |
eckey = NULL; |
| 211 |
} |
| 212 |
|
| 213 |
try |
| 214 |
{ |
| 215 |
BotanRNG* rng = (BotanRNG*)BotanCryptoFactory::i()->getRNG(); |
| 216 |
Botan::EC_Group group = BotanUtil::byteString2ECGroup(ec); |
| 217 |
eckey = new Botan::ECDSA_PrivateKey(*rng->getRNG(), |
| 218 |
group, |
| 219 |
BotanUtil::byteString2bigInt(d)); |
| 220 |
} |
| 221 |
catch (...) |
| 222 |
{ |
| 223 |
ERROR_MSG("Could not create the Botan public key"); |
| 224 |
} |
| 225 |
} |
| 226 |
} |
| 227 |
#endif |