| 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 |
OSSLUtil.h |
| 29 |
|
| 30 |
OpenSSL convenience functions |
| 31 |
*****************************************************************************/ |
| 32 |
|
| 33 |
#include "config.h" |
| 34 |
#include "log.h" |
| 35 |
#include "DerUtil.h" |
| 36 |
#include "OSSLUtil.h" |
| 37 |
#include <openssl/asn1.h> |
| 38 |
#include <openssl/evp.h> |
| 39 |
#include <openssl/err.h> |
| 40 |
|
| 41 |
// Convert an OpenSSL BIGNUM to a ByteString |
| 42 |
ByteString OSSL::bn2ByteString(const BIGNUM* bn) |
| 43 |
{ |
| 44 |
ByteString rv; |
| 45 |
|
| 46 |
if (bn != NULL) |
| 47 |
{ |
| 48 |
rv.resize(BN_num_bytes(bn)); |
| 49 |
BN_bn2bin(bn, &rv[0]); |
| 50 |
} |
| 51 |
|
| 52 |
return rv; |
| 53 |
} |
| 54 |
|
| 55 |
// Convert a ByteString to an OpenSSL BIGNUM |
| 56 |
BIGNUM* OSSL::byteString2bn(const ByteString& byteString) |
| 57 |
{ |
| 58 |
if (byteString.size() == 0) return NULL; |
| 59 |
|
| 60 |
return BN_bin2bn(byteString.const_byte_str(), byteString.size(), NULL); |
| 61 |
} |
| 62 |
|
| 63 |
#ifdef WITH_ECC |
| 64 |
// Convert an OpenSSL EC GROUP to a ByteString |
| 65 |
ByteString OSSL::grp2ByteString(const EC_GROUP* grp) |
| 66 |
{ |
| 67 |
ByteString rv; |
| 68 |
|
| 69 |
if (grp != NULL) |
| 70 |
{ |
| 71 |
rv.resize(i2d_ECPKParameters(grp, NULL)); |
| 72 |
unsigned char *p = &rv[0]; |
| 73 |
i2d_ECPKParameters(grp, &p); |
| 74 |
} |
| 75 |
|
| 76 |
return rv; |
| 77 |
} |
| 78 |
|
| 79 |
// Convert a ByteString to an OpenSSL EC GROUP |
| 80 |
EC_GROUP* OSSL::byteString2grp(const ByteString& byteString) |
| 81 |
{ |
| 82 |
const unsigned char *p = byteString.const_byte_str(); |
| 83 |
return d2i_ECPKParameters(NULL, &p, byteString.size()); |
| 84 |
} |
| 85 |
|
| 86 |
// POINT_CONVERSION_UNCOMPRESSED 0x04 |
| 87 |
|
| 88 |
// Convert an OpenSSL EC POINT in the given EC GROUP to a ByteString |
| 89 |
ByteString OSSL::pt2ByteString(const EC_POINT* pt, const EC_GROUP* grp) |
| 90 |
{ |
| 91 |
ByteString raw; |
| 92 |
|
| 93 |
if (pt == NULL || grp == NULL) |
| 94 |
return raw; |
| 95 |
|
| 96 |
size_t len = EC_POINT_point2oct(grp, pt, POINT_CONVERSION_UNCOMPRESSED, NULL, 0, NULL); |
| 97 |
raw.resize(len); |
| 98 |
EC_POINT_point2oct(grp, pt, POINT_CONVERSION_UNCOMPRESSED, &raw[0], len, NULL); |
| 99 |
|
| 100 |
return DERUTIL::raw2Octet(raw); |
| 101 |
} |
| 102 |
|
| 103 |
// Convert a ByteString to an OpenSSL EC POINT in the given EC GROUP |
| 104 |
EC_POINT* OSSL::byteString2pt(const ByteString& byteString, const EC_GROUP* grp) |
| 105 |
{ |
| 106 |
ByteString raw = DERUTIL::octet2Raw(byteString); |
| 107 |
size_t len = raw.size(); |
| 108 |
if (len == 0) return NULL; |
| 109 |
|
| 110 |
EC_POINT* pt = EC_POINT_new(grp); |
| 111 |
if (!EC_POINT_oct2point(grp, pt, &raw[0], len, NULL)) |
| 112 |
{ |
| 113 |
ERROR_MSG("EC_POINT_oct2point failed: %s", ERR_error_string(ERR_get_error(), NULL)); |
| 114 |
EC_POINT_free(pt); |
| 115 |
return NULL; |
| 116 |
} |
| 117 |
return pt; |
| 118 |
} |
| 119 |
#endif |
| 120 |
|
| 121 |
#ifdef WITH_EDDSA |
| 122 |
// Convert an OpenSSL NID to a ByteString |
| 123 |
ByteString OSSL::oid2ByteString(int nid) |
| 124 |
{ |
| 125 |
ByteString rv; |
| 126 |
std::string name; |
| 127 |
|
| 128 |
switch (nid) |
| 129 |
{ |
| 130 |
case EVP_PKEY_ED25519: |
| 131 |
name = "edwards25519"; |
| 132 |
break; |
| 133 |
|
| 134 |
case EVP_PKEY_X25519: |
| 135 |
name = "curve25519"; |
| 136 |
break; |
| 137 |
|
| 138 |
case EVP_PKEY_ED448: |
| 139 |
name = "edwards448"; |
| 140 |
break; |
| 141 |
|
| 142 |
case EVP_PKEY_X448: |
| 143 |
name = "curve448"; |
| 144 |
break; |
| 145 |
|
| 146 |
default: |
| 147 |
return rv; |
| 148 |
} |
| 149 |
|
| 150 |
ASN1_PRINTABLESTRING *str = ASN1_PRINTABLESTRING_new(); |
| 151 |
ASN1_STRING_set(str, name.c_str(), name.length()); |
| 152 |
rv.resize(i2d_ASN1_PRINTABLESTRING(str, NULL)); |
| 153 |
unsigned char *p = &rv[0]; |
| 154 |
i2d_ASN1_PRINTABLESTRING(str, &p); |
| 155 |
ASN1_PRINTABLESTRING_free(str); |
| 156 |
|
| 157 |
return rv; |
| 158 |
} |
| 159 |
|
| 160 |
// Convert a ByteString to an OpenSSL EVP_PKEY id |
| 161 |
int OSSL::byteString2oid(const ByteString& byteString) |
| 162 |
{ |
| 163 |
ASN1_OBJECT *oid = NULL; |
| 164 |
ASN1_PRINTABLESTRING *curve_name = NULL; |
| 165 |
const unsigned char *p = byteString.const_byte_str(); |
| 166 |
const unsigned char *pp = p; |
| 167 |
long length; |
| 168 |
int tag, pclass; |
| 169 |
|
| 170 |
ASN1_get_object(&pp, &length, &tag, &pclass, byteString.size()); |
| 171 |
if (pclass == V_ASN1_UNIVERSAL && tag == V_ASN1_OBJECT) |
| 172 |
{ |
| 173 |
/* The initial release of SoftHSM was expecting just OID value */ |
| 174 |
oid = d2i_ASN1_OBJECT(NULL, &p, byteString.size()); |
| 175 |
|
| 176 |
if (oid == NULL) |
| 177 |
{ |
| 178 |
return NID_undef; |
| 179 |
} |
| 180 |
|
| 181 |
return OBJ_obj2nid(oid); |
| 182 |
} |
| 183 |
else if (pclass == V_ASN1_UNIVERSAL && tag == V_ASN1_PRINTABLESTRING) |
| 184 |
{ |
| 185 |
/* The final PKCS#11 3.0 expects curve name encoded as PrintableString */ |
| 186 |
curve_name = d2i_ASN1_PRINTABLESTRING(NULL, &p, byteString.size()); |
| 187 |
|
| 188 |
if (strcmp((char *)curve_name->data, "edwards25519") == 0) |
| 189 |
{ |
| 190 |
return EVP_PKEY_ED25519; |
| 191 |
} |
| 192 |
|
| 193 |
if (strcmp((char *)curve_name->data, "curve25519") == 0) |
| 194 |
{ |
| 195 |
return EVP_PKEY_X25519; |
| 196 |
} |
| 197 |
|
| 198 |
if (strcmp((char *)curve_name->data, "edwards448") == 0) |
| 199 |
{ |
| 200 |
return EVP_PKEY_ED448; |
| 201 |
} |
| 202 |
|
| 203 |
if (strcmp((char *)curve_name->data, "curve448") == 0) |
| 204 |
{ |
| 205 |
return EVP_PKEY_X448; |
| 206 |
} |
| 207 |
} |
| 208 |
|
| 209 |
return NID_undef; |
| 210 |
} |
| 211 |
#endif |