| 1 |
/* |
| 2 |
* Copyright (c) 2004 - 2016 Kungliga Tekniska Högskolan |
| 3 |
* (Royal Institute of Technology, Stockholm, Sweden). |
| 4 |
* All rights reserved. |
| 5 |
* |
| 6 |
* Redistribution and use in source and binary forms, with or without |
| 7 |
* modification, are permitted provided that the following conditions |
| 8 |
* are met: |
| 9 |
* |
| 10 |
* 1. Redistributions of source code must retain the above copyright |
| 11 |
* notice, this list of conditions and the following disclaimer. |
| 12 |
* |
| 13 |
* 2. Redistributions in binary form must reproduce the above copyright |
| 14 |
* notice, this list of conditions and the following disclaimer in the |
| 15 |
* documentation and/or other materials provided with the distribution. |
| 16 |
* |
| 17 |
* 3. Neither the name of the Institute nor the names of its contributors |
| 18 |
* may be used to endorse or promote products derived from this software |
| 19 |
* without specific prior written permission. |
| 20 |
* |
| 21 |
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND |
| 22 |
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 23 |
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 24 |
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE |
| 25 |
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 26 |
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 27 |
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 28 |
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 29 |
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 30 |
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 31 |
* SUCH DAMAGE. |
| 32 |
*/ |
| 33 |
|
| 34 |
#include "hx_locl.h" |
| 35 |
|
| 36 |
/*- |
| 37 |
* RFC5758 specifies no parameters for ecdsa-with-SHA<N> signatures |
| 38 |
* RFC5754 specifies NULL parameters for sha<N>WithRSAEncryption signatures |
| 39 |
* |
| 40 |
* XXX: Make sure that the parameters are either NULL in both the tbs and the |
| 41 |
* signature, or absent from both the tbs and the signature. |
| 42 |
*/ |
| 43 |
|
| 44 |
static const heim_octet_string null_entry_oid = { 2, rk_UNCONST("\x05\x00") }; |
| 45 |
|
| 46 |
static const unsigned sha512_oid_tree[] = { 2, 16, 840, 1, 101, 3, 4, 2, 3 }; |
| 47 |
const AlgorithmIdentifier _hx509_signature_sha512_data = { |
| 48 |
{ 9, rk_UNCONST(sha512_oid_tree) }, rk_UNCONST(&null_entry_oid) |
| 49 |
}; |
| 50 |
|
| 51 |
static const unsigned sha384_oid_tree[] = { 2, 16, 840, 1, 101, 3, 4, 2, 2 }; |
| 52 |
const AlgorithmIdentifier _hx509_signature_sha384_data = { |
| 53 |
{ 9, rk_UNCONST(sha384_oid_tree) }, rk_UNCONST(&null_entry_oid) |
| 54 |
}; |
| 55 |
|
| 56 |
static const unsigned sha256_oid_tree[] = { 2, 16, 840, 1, 101, 3, 4, 2, 1 }; |
| 57 |
const AlgorithmIdentifier _hx509_signature_sha256_data = { |
| 58 |
{ 9, rk_UNCONST(sha256_oid_tree) }, rk_UNCONST(&null_entry_oid) |
| 59 |
}; |
| 60 |
|
| 61 |
static const unsigned sha1_oid_tree[] = { 1, 3, 14, 3, 2, 26 }; |
| 62 |
const AlgorithmIdentifier _hx509_signature_sha1_data = { |
| 63 |
{ 6, rk_UNCONST(sha1_oid_tree) }, rk_UNCONST(&null_entry_oid) |
| 64 |
}; |
| 65 |
|
| 66 |
static const unsigned md5_oid_tree[] = { 1, 2, 840, 113549, 2, 5 }; |
| 67 |
const AlgorithmIdentifier _hx509_signature_md5_data = { |
| 68 |
{ 6, rk_UNCONST(md5_oid_tree) }, rk_UNCONST(&null_entry_oid) |
| 69 |
}; |
| 70 |
|
| 71 |
static const unsigned rsa_with_sha512_oid[] ={ 1, 2, 840, 113549, 1, 1, 13 }; |
| 72 |
const AlgorithmIdentifier _hx509_signature_rsa_with_sha512_data = { |
| 73 |
{ 7, rk_UNCONST(rsa_with_sha512_oid) }, rk_UNCONST(&null_entry_oid) |
| 74 |
}; |
| 75 |
|
| 76 |
static const unsigned rsa_with_sha384_oid[] ={ 1, 2, 840, 113549, 1, 1, 12 }; |
| 77 |
const AlgorithmIdentifier _hx509_signature_rsa_with_sha384_data = { |
| 78 |
{ 7, rk_UNCONST(rsa_with_sha384_oid) }, rk_UNCONST(&null_entry_oid) |
| 79 |
}; |
| 80 |
|
| 81 |
static const unsigned rsa_with_sha256_oid[] ={ 1, 2, 840, 113549, 1, 1, 11 }; |
| 82 |
const AlgorithmIdentifier _hx509_signature_rsa_with_sha256_data = { |
| 83 |
{ 7, rk_UNCONST(rsa_with_sha256_oid) }, rk_UNCONST(&null_entry_oid) |
| 84 |
}; |
| 85 |
|
| 86 |
static const unsigned rsa_with_sha1_oid[] ={ 1, 2, 840, 113549, 1, 1, 5 }; |
| 87 |
const AlgorithmIdentifier _hx509_signature_rsa_with_sha1_data = { |
| 88 |
{ 7, rk_UNCONST(rsa_with_sha1_oid) }, rk_UNCONST(&null_entry_oid) |
| 89 |
}; |
| 90 |
|
| 91 |
static const unsigned rsa_with_md5_oid[] ={ 1, 2, 840, 113549, 1, 1, 4 }; |
| 92 |
const AlgorithmIdentifier _hx509_signature_rsa_with_md5_data = { |
| 93 |
{ 7, rk_UNCONST(rsa_with_md5_oid) }, rk_UNCONST(&null_entry_oid) |
| 94 |
}; |
| 95 |
|
| 96 |
static const unsigned rsa_oid[] ={ 1, 2, 840, 113549, 1, 1, 1 }; |
| 97 |
const AlgorithmIdentifier _hx509_signature_rsa_data = { |
| 98 |
{ 7, rk_UNCONST(rsa_oid) }, NULL |
| 99 |
}; |
| 100 |
|
| 101 |
static const unsigned rsa_pkcs1_x509_oid[] ={ 1, 2, 752, 43, 16, 1 }; |
| 102 |
const AlgorithmIdentifier _hx509_signature_rsa_pkcs1_x509_data = { |
| 103 |
{ 6, rk_UNCONST(rsa_pkcs1_x509_oid) }, NULL |
| 104 |
}; |
| 105 |
|
| 106 |
static const unsigned des_rsdi_ede3_cbc_oid[] ={ 1, 2, 840, 113549, 3, 7 }; |
| 107 |
const AlgorithmIdentifier _hx509_des_rsdi_ede3_cbc_oid = { |
| 108 |
{ 6, rk_UNCONST(des_rsdi_ede3_cbc_oid) }, NULL |
| 109 |
}; |
| 110 |
|
| 111 |
static const unsigned aes128_cbc_oid[] ={ 2, 16, 840, 1, 101, 3, 4, 1, 2 }; |
| 112 |
const AlgorithmIdentifier _hx509_crypto_aes128_cbc_data = { |
| 113 |
{ 9, rk_UNCONST(aes128_cbc_oid) }, NULL |
| 114 |
}; |
| 115 |
|
| 116 |
static const unsigned aes256_cbc_oid[] ={ 2, 16, 840, 1, 101, 3, 4, 1, 42 }; |
| 117 |
const AlgorithmIdentifier _hx509_crypto_aes256_cbc_data = { |
| 118 |
{ 9, rk_UNCONST(aes256_cbc_oid) }, NULL |
| 119 |
}; |
| 120 |
|
| 121 |
/* |
| 122 |
* |
| 123 |
*/ |
| 124 |
|
| 125 |
static BIGNUM * |
| 126 |
heim_int2BN(const heim_integer *i) |
| 127 |
{ |
| 128 |
BIGNUM *bn; |
| 129 |
|
| 130 |
bn = BN_bin2bn(i->data, i->length, NULL); |
| 131 |
BN_set_negative(bn, i->negative); |
| 132 |
return bn; |
| 133 |
} |
| 134 |
|
| 135 |
/* |
| 136 |
* |
| 137 |
*/ |
| 138 |
|
| 139 |
int |
| 140 |
_hx509_set_digest_alg(DigestAlgorithmIdentifier *id, |
| 141 |
const heim_oid *oid, |
| 142 |
const void *param, size_t length) |
| 143 |
{ |
| 144 |
int ret; |
| 145 |
if (param) { |
| 146 |
id->parameters = malloc(sizeof(*id->parameters)); |
| 147 |
if (id->parameters == NULL) |
| 148 |
return ENOMEM; |
| 149 |
id->parameters->data = malloc(length); |
| 150 |
if (id->parameters->data == NULL) { |
| 151 |
free(id->parameters); |
| 152 |
id->parameters = NULL; |
| 153 |
return ENOMEM; |
| 154 |
} |
| 155 |
memcpy(id->parameters->data, param, length); |
| 156 |
id->parameters->length = length; |
| 157 |
} else |
| 158 |
id->parameters = NULL; |
| 159 |
ret = der_copy_oid(oid, &id->algorithm); |
| 160 |
if (ret) { |
| 161 |
if (id->parameters) { |
| 162 |
free(id->parameters->data); |
| 163 |
free(id->parameters); |
| 164 |
id->parameters = NULL; |
| 165 |
} |
| 166 |
return ret; |
| 167 |
} |
| 168 |
return 0; |
| 169 |
} |
| 170 |
|
| 171 |
/* |
| 172 |
* |
| 173 |
*/ |
| 174 |
|
| 175 |
static int |
| 176 |
rsa_verify_signature(hx509_context context, |
| 177 |
const struct signature_alg *sig_alg, |
| 178 |
const Certificate *signer, |
| 179 |
const AlgorithmIdentifier *alg, |
| 180 |
const heim_octet_string *data, |
| 181 |
const heim_octet_string *sig) |
| 182 |
{ |
| 183 |
const SubjectPublicKeyInfo *spi; |
| 184 |
DigestInfo di; |
| 185 |
unsigned char *to; |
| 186 |
int tosize, retsize; |
| 187 |
int ret; |
| 188 |
RSA *rsa; |
| 189 |
size_t size; |
| 190 |
const unsigned char *p; |
| 191 |
|
| 192 |
memset(&di, 0, sizeof(di)); |
| 193 |
|
| 194 |
spi = &signer->tbsCertificate.subjectPublicKeyInfo; |
| 195 |
|
| 196 |
p = spi->subjectPublicKey.data; |
| 197 |
size = spi->subjectPublicKey.length / 8; |
| 198 |
|
| 199 |
rsa = d2i_RSAPublicKey(NULL, &p, size); |
| 200 |
if (rsa == NULL) { |
| 201 |
ret = ENOMEM; |
| 202 |
hx509_set_error_string(context, 0, ret, "out of memory"); |
| 203 |
goto out; |
| 204 |
} |
| 205 |
|
| 206 |
tosize = RSA_size(rsa); |
| 207 |
to = malloc(tosize); |
| 208 |
if (to == NULL) { |
| 209 |
ret = ENOMEM; |
| 210 |
hx509_set_error_string(context, 0, ret, "out of memory"); |
| 211 |
goto out; |
| 212 |
} |
| 213 |
|
| 214 |
retsize = RSA_public_decrypt(sig->length, (unsigned char *)sig->data, |
| 215 |
to, rsa, RSA_PKCS1_PADDING); |
| 216 |
if (retsize <= 0) { |
| 217 |
ret = HX509_CRYPTO_SIG_INVALID_FORMAT; |
| 218 |
hx509_set_error_string(context, 0, ret, |
| 219 |
"RSA public decrypt failed: %d", retsize); |
| 220 |
free(to); |
| 221 |
goto out; |
| 222 |
} |
| 223 |
if (retsize > tosize) |
| 224 |
_hx509_abort("internal rsa decryption failure: ret > tosize"); |
| 225 |
|
| 226 |
if (sig_alg->flags & RA_RSA_USES_DIGEST_INFO) { |
| 227 |
|
| 228 |
ret = decode_DigestInfo(to, retsize, &di, &size); |
| 229 |
free(to); |
| 230 |
if (ret) { |
| 231 |
goto out; |
| 232 |
} |
| 233 |
|
| 234 |
/* Check for extra data inside the sigature */ |
| 235 |
if (size != (size_t)retsize) { |
| 236 |
ret = HX509_CRYPTO_SIG_INVALID_FORMAT; |
| 237 |
hx509_set_error_string(context, 0, ret, "size from decryption mismatch"); |
| 238 |
goto out; |
| 239 |
} |
| 240 |
|
| 241 |
if (sig_alg->digest_alg && |
| 242 |
der_heim_oid_cmp(&di.digestAlgorithm.algorithm, |
| 243 |
&sig_alg->digest_alg->algorithm) != 0) |
| 244 |
{ |
| 245 |
ret = HX509_CRYPTO_OID_MISMATCH; |
| 246 |
hx509_set_error_string(context, 0, ret, "object identifier in RSA sig mismatch"); |
| 247 |
goto out; |
| 248 |
} |
| 249 |
|
| 250 |
/* verify that the parameters are NULL or the NULL-type */ |
| 251 |
if (di.digestAlgorithm.parameters != NULL && |
| 252 |
(di.digestAlgorithm.parameters->length != 2 || |
| 253 |
memcmp(di.digestAlgorithm.parameters->data, "\x05\x00", 2) != 0)) |
| 254 |
{ |
| 255 |
ret = HX509_CRYPTO_SIG_INVALID_FORMAT; |
| 256 |
hx509_set_error_string(context, 0, ret, "Extra parameters inside RSA signature"); |
| 257 |
goto out; |
| 258 |
} |
| 259 |
|
| 260 |
ret = _hx509_verify_signature(context, |
| 261 |
NULL, |
| 262 |
&di.digestAlgorithm, |
| 263 |
data, |
| 264 |
&di.digest); |
| 265 |
if (ret) |
| 266 |
goto out; |
| 267 |
|
| 268 |
} else { |
| 269 |
if ((size_t)retsize != data->length || |
| 270 |
ct_memcmp(to, data->data, retsize) != 0) |
| 271 |
{ |
| 272 |
ret = HX509_CRYPTO_SIG_INVALID_FORMAT; |
| 273 |
hx509_set_error_string(context, 0, ret, "RSA Signature incorrect"); |
| 274 |
goto out; |
| 275 |
} |
| 276 |
free(to); |
| 277 |
ret = 0; |
| 278 |
} |
| 279 |
|
| 280 |
out: |
| 281 |
free_DigestInfo(&di); |
| 282 |
if (rsa) |
| 283 |
RSA_free(rsa); |
| 284 |
return ret; |
| 285 |
} |
| 286 |
|
| 287 |
static int |
| 288 |
rsa_create_signature(hx509_context context, |
| 289 |
const struct signature_alg *sig_alg, |
| 290 |
const hx509_private_key signer, |
| 291 |
const AlgorithmIdentifier *alg, |
| 292 |
const heim_octet_string *data, |
| 293 |
AlgorithmIdentifier *signatureAlgorithm, |
| 294 |
heim_octet_string *sig) |
| 295 |
{ |
| 296 |
const AlgorithmIdentifier *digest_alg; |
| 297 |
heim_octet_string indata; |
| 298 |
const heim_oid *sig_oid; |
| 299 |
size_t size; |
| 300 |
int ret; |
| 301 |
|
| 302 |
if (signer->ops && der_heim_oid_cmp(signer->ops->key_oid, ASN1_OID_ID_PKCS1_RSAENCRYPTION) != 0) |
| 303 |
return HX509_ALG_NOT_SUPP; |
| 304 |
|
| 305 |
if (alg) |
| 306 |
sig_oid = &alg->algorithm; |
| 307 |
else |
| 308 |
sig_oid = signer->signature_alg; |
| 309 |
|
| 310 |
if (der_heim_oid_cmp(sig_oid, ASN1_OID_ID_PKCS1_SHA512WITHRSAENCRYPTION) == 0) { |
| 311 |
digest_alg = hx509_signature_sha512(); |
| 312 |
} else if (der_heim_oid_cmp(sig_oid, ASN1_OID_ID_PKCS1_SHA384WITHRSAENCRYPTION) == 0) { |
| 313 |
digest_alg = hx509_signature_sha384(); |
| 314 |
} else if (der_heim_oid_cmp(sig_oid, ASN1_OID_ID_PKCS1_SHA256WITHRSAENCRYPTION) == 0) { |
| 315 |
digest_alg = hx509_signature_sha256(); |
| 316 |
} else if (der_heim_oid_cmp(sig_oid, ASN1_OID_ID_PKCS1_SHA1WITHRSAENCRYPTION) == 0) { |
| 317 |
digest_alg = hx509_signature_sha1(); |
| 318 |
} else if (der_heim_oid_cmp(sig_oid, ASN1_OID_ID_PKCS1_MD5WITHRSAENCRYPTION) == 0) { |
| 319 |
digest_alg = hx509_signature_md5(); |
| 320 |
} else if (der_heim_oid_cmp(sig_oid, ASN1_OID_ID_PKCS1_MD5WITHRSAENCRYPTION) == 0) { |
| 321 |
digest_alg = hx509_signature_md5(); |
| 322 |
} else if (der_heim_oid_cmp(sig_oid, ASN1_OID_ID_DSA_WITH_SHA1) == 0) { |
| 323 |
digest_alg = hx509_signature_sha1(); |
| 324 |
} else if (der_heim_oid_cmp(sig_oid, ASN1_OID_ID_PKCS1_RSAENCRYPTION) == 0) { |
| 325 |
digest_alg = hx509_signature_sha1(); |
| 326 |
} else if (der_heim_oid_cmp(sig_oid, ASN1_OID_ID_HEIM_RSA_PKCS1_X509) == 0) { |
| 327 |
digest_alg = NULL; |
| 328 |
} else |
| 329 |
return HX509_ALG_NOT_SUPP; |
| 330 |
|
| 331 |
if (signatureAlgorithm) { |
| 332 |
ret = _hx509_set_digest_alg(signatureAlgorithm, sig_oid, |
| 333 |
"\x05\x00", 2); |
| 334 |
if (ret) { |
| 335 |
hx509_clear_error_string(context); |
| 336 |
return ret; |
| 337 |
} |
| 338 |
} |
| 339 |
|
| 340 |
if (digest_alg) { |
| 341 |
DigestInfo di; |
| 342 |
memset(&di, 0, sizeof(di)); |
| 343 |
|
| 344 |
ret = _hx509_create_signature(context, |
| 345 |
NULL, |
| 346 |
digest_alg, |
| 347 |
data, |
| 348 |
&di.digestAlgorithm, |
| 349 |
&di.digest); |
| 350 |
if (ret) |
| 351 |
return ret; |
| 352 |
ASN1_MALLOC_ENCODE(DigestInfo, |
| 353 |
indata.data, |
| 354 |
indata.length, |
| 355 |
&di, |
| 356 |
&size, |
| 357 |
ret); |
| 358 |
free_DigestInfo(&di); |
| 359 |
if (ret) { |
| 360 |
hx509_set_error_string(context, 0, ret, "out of memory"); |
| 361 |
return ret; |
| 362 |
} |
| 363 |
if (indata.length != size) |
| 364 |
_hx509_abort("internal ASN.1 encoder error"); |
| 365 |
} else { |
| 366 |
indata = *data; |
| 367 |
} |
| 368 |
|
| 369 |
sig->length = RSA_size(signer->private_key.rsa); |
| 370 |
sig->data = malloc(sig->length); |
| 371 |
if (sig->data == NULL) { |
| 372 |
der_free_octet_string(&indata); |
| 373 |
hx509_set_error_string(context, 0, ENOMEM, "out of memory"); |
| 374 |
return ENOMEM; |
| 375 |
} |
| 376 |
|
| 377 |
ret = RSA_private_encrypt(indata.length, indata.data, |
| 378 |
sig->data, |
| 379 |
signer->private_key.rsa, |
| 380 |
RSA_PKCS1_PADDING); |
| 381 |
if (indata.data != data->data) |
| 382 |
der_free_octet_string(&indata); |
| 383 |
if (ret <= 0) { |
| 384 |
ret = HX509_CMS_FAILED_CREATE_SIGATURE; |
| 385 |
hx509_set_error_string(context, 0, ret, |
| 386 |
"RSA private encrypt failed: %d", ret); |
| 387 |
return ret; |
| 388 |
} |
| 389 |
if (sig->length > (size_t)ret) { |
| 390 |
size = sig->length - ret; |
| 391 |
memmove((uint8_t *)sig->data + size, sig->data, ret); |
| 392 |
memset(sig->data, 0, size); |
| 393 |
} else if (sig->length < (size_t)ret) |
| 394 |
_hx509_abort("RSA signature prelen longer the output len"); |
| 395 |
|
| 396 |
return 0; |
| 397 |
} |
| 398 |
|
| 399 |
static int |
| 400 |
rsa_private_key_import(hx509_context context, |
| 401 |
const AlgorithmIdentifier *keyai, |
| 402 |
const void *data, |
| 403 |
size_t len, |
| 404 |
hx509_key_format_t format, |
| 405 |
hx509_private_key private_key) |
| 406 |
{ |
| 407 |
switch (format) { |
| 408 |
case HX509_KEY_FORMAT_DER: { |
| 409 |
const unsigned char *p = data; |
| 410 |
|
| 411 |
private_key->private_key.rsa = |
| 412 |
d2i_RSAPrivateKey(NULL, &p, len); |
| 413 |
if (private_key->private_key.rsa == NULL) { |
| 414 |
hx509_set_error_string(context, 0, HX509_PARSING_KEY_FAILED, |
| 415 |
"Failed to parse RSA key"); |
| 416 |
return HX509_PARSING_KEY_FAILED; |
| 417 |
} |
| 418 |
private_key->signature_alg = ASN1_OID_ID_PKCS1_SHA1WITHRSAENCRYPTION; |
| 419 |
break; |
| 420 |
|
| 421 |
} |
| 422 |
default: |
| 423 |
return HX509_CRYPTO_KEY_FORMAT_UNSUPPORTED; |
| 424 |
} |
| 425 |
|
| 426 |
return 0; |
| 427 |
} |
| 428 |
|
| 429 |
static int |
| 430 |
rsa_private_key2SPKI(hx509_context context, |
| 431 |
hx509_private_key private_key, |
| 432 |
SubjectPublicKeyInfo *spki) |
| 433 |
{ |
| 434 |
int len, ret; |
| 435 |
|
| 436 |
memset(spki, 0, sizeof(*spki)); |
| 437 |
|
| 438 |
len = i2d_RSAPublicKey(private_key->private_key.rsa, NULL); |
| 439 |
|
| 440 |
spki->subjectPublicKey.data = malloc(len); |
| 441 |
if (spki->subjectPublicKey.data == NULL) { |
| 442 |
hx509_set_error_string(context, 0, ENOMEM, "malloc - out of memory"); |
| 443 |
return ENOMEM; |
| 444 |
} |
| 445 |
spki->subjectPublicKey.length = len * 8; |
| 446 |
|
| 447 |
ret = _hx509_set_digest_alg(&spki->algorithm, |
| 448 |
ASN1_OID_ID_PKCS1_RSAENCRYPTION, |
| 449 |
"\x05\x00", 2); |
| 450 |
if (ret) { |
| 451 |
hx509_set_error_string(context, 0, ret, "malloc - out of memory"); |
| 452 |
free(spki->subjectPublicKey.data); |
| 453 |
spki->subjectPublicKey.data = NULL; |
| 454 |
spki->subjectPublicKey.length = 0; |
| 455 |
return ret; |
| 456 |
} |
| 457 |
|
| 458 |
{ |
| 459 |
unsigned char *pp = spki->subjectPublicKey.data; |
| 460 |
i2d_RSAPublicKey(private_key->private_key.rsa, &pp); |
| 461 |
} |
| 462 |
|
| 463 |
return 0; |
| 464 |
} |
| 465 |
|
| 466 |
static int |
| 467 |
rsa_generate_private_key(hx509_context context, |
| 468 |
struct hx509_generate_private_context *ctx, |
| 469 |
hx509_private_key private_key) |
| 470 |
{ |
| 471 |
BIGNUM *e; |
| 472 |
int ret; |
| 473 |
unsigned long bits; |
| 474 |
|
| 475 |
static const int default_rsa_e = 65537; |
| 476 |
static const int default_rsa_bits = 2048; |
| 477 |
|
| 478 |
private_key->private_key.rsa = RSA_new(); |
| 479 |
if (private_key->private_key.rsa == NULL) { |
| 480 |
hx509_set_error_string(context, 0, HX509_PARSING_KEY_FAILED, |
| 481 |
"Failed to generate RSA key"); |
| 482 |
return HX509_PARSING_KEY_FAILED; |
| 483 |
} |
| 484 |
|
| 485 |
e = BN_new(); |
| 486 |
BN_set_word(e, default_rsa_e); |
| 487 |
|
| 488 |
bits = default_rsa_bits; |
| 489 |
|
| 490 |
if (ctx->num_bits) |
| 491 |
bits = ctx->num_bits; |
| 492 |
|
| 493 |
ret = RSA_generate_key_ex(private_key->private_key.rsa, bits, e, NULL); |
| 494 |
BN_free(e); |
| 495 |
if (ret != 1) { |
| 496 |
hx509_set_error_string(context, 0, HX509_PARSING_KEY_FAILED, |
| 497 |
"Failed to generate RSA key"); |
| 498 |
return HX509_PARSING_KEY_FAILED; |
| 499 |
} |
| 500 |
private_key->signature_alg = ASN1_OID_ID_PKCS1_SHA1WITHRSAENCRYPTION; |
| 501 |
|
| 502 |
return 0; |
| 503 |
} |
| 504 |
|
| 505 |
static int |
| 506 |
rsa_private_key_export(hx509_context context, |
| 507 |
const hx509_private_key key, |
| 508 |
hx509_key_format_t format, |
| 509 |
heim_octet_string *data) |
| 510 |
{ |
| 511 |
int ret; |
| 512 |
|
| 513 |
data->data = NULL; |
| 514 |
data->length = 0; |
| 515 |
|
| 516 |
switch (format) { |
| 517 |
case HX509_KEY_FORMAT_DER: |
| 518 |
|
| 519 |
ret = i2d_RSAPrivateKey(key->private_key.rsa, NULL); |
| 520 |
if (ret <= 0) { |
| 521 |
ret = EINVAL; |
| 522 |
hx509_set_error_string(context, 0, ret, |
| 523 |
"Private key is not exportable"); |
| 524 |
return ret; |
| 525 |
} |
| 526 |
|
| 527 |
data->data = malloc(ret); |
| 528 |
if (data->data == NULL) { |
| 529 |
ret = ENOMEM; |
| 530 |
hx509_set_error_string(context, 0, ret, "malloc out of memory"); |
| 531 |
return ret; |
| 532 |
} |
| 533 |
data->length = ret; |
| 534 |
|
| 535 |
{ |
| 536 |
unsigned char *p = data->data; |
| 537 |
i2d_RSAPrivateKey(key->private_key.rsa, &p); |
| 538 |
} |
| 539 |
break; |
| 540 |
default: |
| 541 |
return HX509_CRYPTO_KEY_FORMAT_UNSUPPORTED; |
| 542 |
} |
| 543 |
|
| 544 |
return 0; |
| 545 |
} |
| 546 |
|
| 547 |
static BIGNUM * |
| 548 |
rsa_get_internal(hx509_context context, |
| 549 |
hx509_private_key key, |
| 550 |
const char *type) |
| 551 |
{ |
| 552 |
if (strcasecmp(type, "rsa-modulus") == 0) { |
| 553 |
return BN_dup(key->private_key.rsa->n); |
| 554 |
} else if (strcasecmp(type, "rsa-exponent") == 0) { |
| 555 |
return BN_dup(key->private_key.rsa->e); |
| 556 |
} else |
| 557 |
return NULL; |
| 558 |
} |
| 559 |
|
| 560 |
|
| 561 |
|
| 562 |
static hx509_private_key_ops rsa_private_key_ops = { |
| 563 |
"RSA PRIVATE KEY", |
| 564 |
ASN1_OID_ID_PKCS1_RSAENCRYPTION, |
| 565 |
NULL, |
| 566 |
rsa_private_key2SPKI, |
| 567 |
rsa_private_key_export, |
| 568 |
rsa_private_key_import, |
| 569 |
rsa_generate_private_key, |
| 570 |
rsa_get_internal |
| 571 |
}; |
| 572 |
|
| 573 |
/* |
| 574 |
* |
| 575 |
*/ |
| 576 |
|
| 577 |
static int |
| 578 |
dsa_verify_signature(hx509_context context, |
| 579 |
const struct signature_alg *sig_alg, |
| 580 |
const Certificate *signer, |
| 581 |
const AlgorithmIdentifier *alg, |
| 582 |
const heim_octet_string *data, |
| 583 |
const heim_octet_string *sig) |
| 584 |
{ |
| 585 |
const SubjectPublicKeyInfo *spi; |
| 586 |
DSAPublicKey pk; |
| 587 |
DSAParams param; |
| 588 |
size_t size; |
| 589 |
DSA *dsa; |
| 590 |
int ret; |
| 591 |
|
| 592 |
spi = &signer->tbsCertificate.subjectPublicKeyInfo; |
| 593 |
|
| 594 |
dsa = DSA_new(); |
| 595 |
if (dsa == NULL) { |
| 596 |
hx509_set_error_string(context, 0, ENOMEM, "out of memory"); |
| 597 |
return ENOMEM; |
| 598 |
} |
| 599 |
|
| 600 |
ret = decode_DSAPublicKey(spi->subjectPublicKey.data, |
| 601 |
spi->subjectPublicKey.length / 8, |
| 602 |
&pk, &size); |
| 603 |
if (ret) |
| 604 |
goto out; |
| 605 |
|
| 606 |
dsa->pub_key = heim_int2BN(&pk); |
| 607 |
|
| 608 |
free_DSAPublicKey(&pk); |
| 609 |
|
| 610 |
if (dsa->pub_key == NULL) { |
| 611 |
ret = ENOMEM; |
| 612 |
hx509_set_error_string(context, 0, ret, "out of memory"); |
| 613 |
goto out; |
| 614 |
} |
| 615 |
|
| 616 |
if (spi->algorithm.parameters == NULL) { |
| 617 |
ret = HX509_CRYPTO_SIG_INVALID_FORMAT; |
| 618 |
hx509_set_error_string(context, 0, ret, "DSA parameters missing"); |
| 619 |
goto out; |
| 620 |
} |
| 621 |
|
| 622 |
ret = decode_DSAParams(spi->algorithm.parameters->data, |
| 623 |
spi->algorithm.parameters->length, |
| 624 |
¶m, |
| 625 |
&size); |
| 626 |
if (ret) { |
| 627 |
hx509_set_error_string(context, 0, ret, "DSA parameters failed to decode"); |
| 628 |
goto out; |
| 629 |
} |
| 630 |
|
| 631 |
dsa->p = heim_int2BN(¶m.p); |
| 632 |
dsa->q = heim_int2BN(¶m.q); |
| 633 |
dsa->g = heim_int2BN(¶m.g); |
| 634 |
|
| 635 |
free_DSAParams(¶m); |
| 636 |
|
| 637 |
if (dsa->p == NULL || dsa->q == NULL || dsa->g == NULL) { |
| 638 |
ret = ENOMEM; |
| 639 |
hx509_set_error_string(context, 0, ret, "out of memory"); |
| 640 |
goto out; |
| 641 |
} |
| 642 |
|
| 643 |
ret = DSA_verify(-1, data->data, data->length, |
| 644 |
(unsigned char*)sig->data, sig->length, |
| 645 |
dsa); |
| 646 |
if (ret == 1) |
| 647 |
ret = 0; |
| 648 |
else if (ret == 0 || ret == -1) { |
| 649 |
ret = HX509_CRYPTO_BAD_SIGNATURE; |
| 650 |
hx509_set_error_string(context, 0, ret, "BAD DSA sigature"); |
| 651 |
} else { |
| 652 |
ret = HX509_CRYPTO_SIG_INVALID_FORMAT; |
| 653 |
hx509_set_error_string(context, 0, ret, "Invalid format of DSA sigature"); |
| 654 |
} |
| 655 |
|
| 656 |
out: |
| 657 |
DSA_free(dsa); |
| 658 |
|
| 659 |
return ret; |
| 660 |
} |
| 661 |
|
| 662 |
#if 0 |
| 663 |
static int |
| 664 |
dsa_parse_private_key(hx509_context context, |
| 665 |
const void *data, |
| 666 |
size_t len, |
| 667 |
hx509_private_key private_key) |
| 668 |
{ |
| 669 |
const unsigned char *p = data; |
| 670 |
|
| 671 |
private_key->private_key.dsa = |
| 672 |
d2i_DSAPrivateKey(NULL, &p, len); |
| 673 |
if (private_key->private_key.dsa == NULL) |
| 674 |
return EINVAL; |
| 675 |
private_key->signature_alg = ASN1_OID_ID_DSA_WITH_SHA1; |
| 676 |
|
| 677 |
return 0; |
| 678 |
/* else */ |
| 679 |
hx509_set_error_string(context, 0, HX509_PARSING_KEY_FAILED, |
| 680 |
"No support to parse DSA keys"); |
| 681 |
return HX509_PARSING_KEY_FAILED; |
| 682 |
} |
| 683 |
#endif |
| 684 |
|
| 685 |
static int |
| 686 |
evp_md_create_signature(hx509_context context, |
| 687 |
const struct signature_alg *sig_alg, |
| 688 |
const hx509_private_key signer, |
| 689 |
const AlgorithmIdentifier *alg, |
| 690 |
const heim_octet_string *data, |
| 691 |
AlgorithmIdentifier *signatureAlgorithm, |
| 692 |
heim_octet_string *sig) |
| 693 |
{ |
| 694 |
size_t sigsize = EVP_MD_size(sig_alg->evp_md()); |
| 695 |
EVP_MD_CTX *ctx; |
| 696 |
|
| 697 |
memset(sig, 0, sizeof(*sig)); |
| 698 |
|
| 699 |
if (signatureAlgorithm) { |
| 700 |
int ret; |
| 701 |
ret = _hx509_set_digest_alg(signatureAlgorithm, |
| 702 |
sig_alg->sig_oid, "\x05\x00", 2); |
| 703 |
if (ret) |
| 704 |
return ret; |
| 705 |
} |
| 706 |
|
| 707 |
|
| 708 |
sig->data = malloc(sigsize); |
| 709 |
if (sig->data == NULL) { |
| 710 |
sig->length = 0; |
| 711 |
return ENOMEM; |
| 712 |
} |
| 713 |
sig->length = sigsize; |
| 714 |
|
| 715 |
ctx = EVP_MD_CTX_create(); |
| 716 |
EVP_DigestInit_ex(ctx, sig_alg->evp_md(), NULL); |
| 717 |
EVP_DigestUpdate(ctx, data->data, data->length); |
| 718 |
EVP_DigestFinal_ex(ctx, sig->data, NULL); |
| 719 |
EVP_MD_CTX_destroy(ctx); |
| 720 |
|
| 721 |
|
| 722 |
return 0; |
| 723 |
} |
| 724 |
|
| 725 |
static int |
| 726 |
evp_md_verify_signature(hx509_context context, |
| 727 |
const struct signature_alg *sig_alg, |
| 728 |
const Certificate *signer, |
| 729 |
const AlgorithmIdentifier *alg, |
| 730 |
const heim_octet_string *data, |
| 731 |
const heim_octet_string *sig) |
| 732 |
{ |
| 733 |
unsigned char digest[EVP_MAX_MD_SIZE]; |
| 734 |
EVP_MD_CTX *ctx; |
| 735 |
size_t sigsize = EVP_MD_size(sig_alg->evp_md()); |
| 736 |
|
| 737 |
if (sig->length != sigsize || sigsize > sizeof(digest)) { |
| 738 |
hx509_set_error_string(context, 0, HX509_CRYPTO_SIG_INVALID_FORMAT, |
| 739 |
"SHA256 sigature have wrong length"); |
| 740 |
return HX509_CRYPTO_SIG_INVALID_FORMAT; |
| 741 |
} |
| 742 |
|
| 743 |
ctx = EVP_MD_CTX_create(); |
| 744 |
EVP_DigestInit_ex(ctx, sig_alg->evp_md(), NULL); |
| 745 |
EVP_DigestUpdate(ctx, data->data, data->length); |
| 746 |
EVP_DigestFinal_ex(ctx, digest, NULL); |
| 747 |
EVP_MD_CTX_destroy(ctx); |
| 748 |
|
| 749 |
if (ct_memcmp(digest, sig->data, sigsize) != 0) { |
| 750 |
hx509_set_error_string(context, 0, HX509_CRYPTO_BAD_SIGNATURE, |
| 751 |
"Bad %s sigature", sig_alg->name); |
| 752 |
return HX509_CRYPTO_BAD_SIGNATURE; |
| 753 |
} |
| 754 |
|
| 755 |
return 0; |
| 756 |
} |
| 757 |
|
| 758 |
#ifdef HAVE_HCRYPTO_W_OPENSSL |
| 759 |
extern const struct signature_alg ecdsa_with_sha512_alg; |
| 760 |
extern const struct signature_alg ecdsa_with_sha384_alg; |
| 761 |
extern const struct signature_alg ecdsa_with_sha256_alg; |
| 762 |
extern const struct signature_alg ecdsa_with_sha1_alg; |
| 763 |
#endif |
| 764 |
|
| 765 |
static const struct signature_alg heim_rsa_pkcs1_x509 = { |
| 766 |
"rsa-pkcs1-x509", |
| 767 |
ASN1_OID_ID_HEIM_RSA_PKCS1_X509, |
| 768 |
&_hx509_signature_rsa_pkcs1_x509_data, |
| 769 |
ASN1_OID_ID_PKCS1_RSAENCRYPTION, |
| 770 |
NULL, |
| 771 |
PROVIDE_CONF|REQUIRE_SIGNER|SIG_PUBLIC_SIG, |
| 772 |
0, |
| 773 |
NULL, |
| 774 |
rsa_verify_signature, |
| 775 |
rsa_create_signature, |
| 776 |
0 |
| 777 |
}; |
| 778 |
|
| 779 |
static const struct signature_alg pkcs1_rsa_sha1_alg = { |
| 780 |
"rsa", |
| 781 |
ASN1_OID_ID_PKCS1_RSAENCRYPTION, |
| 782 |
&_hx509_signature_rsa_with_sha1_data, |
| 783 |
ASN1_OID_ID_PKCS1_RSAENCRYPTION, |
| 784 |
NULL, |
| 785 |
PROVIDE_CONF|REQUIRE_SIGNER|RA_RSA_USES_DIGEST_INFO|SIG_PUBLIC_SIG|SELF_SIGNED_OK, |
| 786 |
0, |
| 787 |
NULL, |
| 788 |
rsa_verify_signature, |
| 789 |
rsa_create_signature, |
| 790 |
0 |
| 791 |
}; |
| 792 |
|
| 793 |
static const struct signature_alg rsa_with_sha512_alg = { |
| 794 |
"rsa-with-sha512", |
| 795 |
ASN1_OID_ID_PKCS1_SHA512WITHRSAENCRYPTION, |
| 796 |
&_hx509_signature_rsa_with_sha512_data, |
| 797 |
ASN1_OID_ID_PKCS1_RSAENCRYPTION, |
| 798 |
&_hx509_signature_sha512_data, |
| 799 |
PROVIDE_CONF|REQUIRE_SIGNER|RA_RSA_USES_DIGEST_INFO|SIG_PUBLIC_SIG|SELF_SIGNED_OK, |
| 800 |
0, |
| 801 |
NULL, |
| 802 |
rsa_verify_signature, |
| 803 |
rsa_create_signature, |
| 804 |
0 |
| 805 |
}; |
| 806 |
|
| 807 |
static const struct signature_alg rsa_with_sha384_alg = { |
| 808 |
"rsa-with-sha384", |
| 809 |
ASN1_OID_ID_PKCS1_SHA384WITHRSAENCRYPTION, |
| 810 |
&_hx509_signature_rsa_with_sha384_data, |
| 811 |
ASN1_OID_ID_PKCS1_RSAENCRYPTION, |
| 812 |
&_hx509_signature_sha384_data, |
| 813 |
PROVIDE_CONF|REQUIRE_SIGNER|RA_RSA_USES_DIGEST_INFO|SIG_PUBLIC_SIG|SELF_SIGNED_OK, |
| 814 |
0, |
| 815 |
NULL, |
| 816 |
rsa_verify_signature, |
| 817 |
rsa_create_signature, |
| 818 |
0 |
| 819 |
}; |
| 820 |
|
| 821 |
static const struct signature_alg rsa_with_sha256_alg = { |
| 822 |
"rsa-with-sha256", |
| 823 |
ASN1_OID_ID_PKCS1_SHA256WITHRSAENCRYPTION, |
| 824 |
&_hx509_signature_rsa_with_sha256_data, |
| 825 |
ASN1_OID_ID_PKCS1_RSAENCRYPTION, |
| 826 |
&_hx509_signature_sha256_data, |
| 827 |
PROVIDE_CONF|REQUIRE_SIGNER|RA_RSA_USES_DIGEST_INFO|SIG_PUBLIC_SIG|SELF_SIGNED_OK, |
| 828 |
0, |
| 829 |
NULL, |
| 830 |
rsa_verify_signature, |
| 831 |
rsa_create_signature, |
| 832 |
0 |
| 833 |
}; |
| 834 |
|
| 835 |
static const struct signature_alg rsa_with_sha1_alg = { |
| 836 |
"rsa-with-sha1", |
| 837 |
ASN1_OID_ID_PKCS1_SHA1WITHRSAENCRYPTION, |
| 838 |
&_hx509_signature_rsa_with_sha1_data, |
| 839 |
ASN1_OID_ID_PKCS1_RSAENCRYPTION, |
| 840 |
&_hx509_signature_sha1_data, |
| 841 |
PROVIDE_CONF|REQUIRE_SIGNER|RA_RSA_USES_DIGEST_INFO|SIG_PUBLIC_SIG|SELF_SIGNED_OK, |
| 842 |
0, |
| 843 |
NULL, |
| 844 |
rsa_verify_signature, |
| 845 |
rsa_create_signature, |
| 846 |
0 |
| 847 |
}; |
| 848 |
|
| 849 |
static const struct signature_alg rsa_with_sha1_alg_secsig = { |
| 850 |
"rsa-with-sha1", |
| 851 |
ASN1_OID_ID_SECSIG_SHA_1WITHRSAENCRYPTION, |
| 852 |
&_hx509_signature_rsa_with_sha1_data, |
| 853 |
ASN1_OID_ID_PKCS1_RSAENCRYPTION, |
| 854 |
&_hx509_signature_sha1_data, |
| 855 |
PROVIDE_CONF|REQUIRE_SIGNER|RA_RSA_USES_DIGEST_INFO|SIG_PUBLIC_SIG|SELF_SIGNED_OK, |
| 856 |
0, |
| 857 |
NULL, |
| 858 |
rsa_verify_signature, |
| 859 |
rsa_create_signature, |
| 860 |
0 |
| 861 |
}; |
| 862 |
|
| 863 |
static const struct signature_alg rsa_with_md5_alg = { |
| 864 |
"rsa-with-md5", |
| 865 |
ASN1_OID_ID_PKCS1_MD5WITHRSAENCRYPTION, |
| 866 |
&_hx509_signature_rsa_with_md5_data, |
| 867 |
ASN1_OID_ID_PKCS1_RSAENCRYPTION, |
| 868 |
&_hx509_signature_md5_data, |
| 869 |
PROVIDE_CONF|REQUIRE_SIGNER|RA_RSA_USES_DIGEST_INFO|SIG_PUBLIC_SIG|WEAK_SIG_ALG, |
| 870 |
1230739889, |
| 871 |
NULL, |
| 872 |
rsa_verify_signature, |
| 873 |
rsa_create_signature, |
| 874 |
0 |
| 875 |
}; |
| 876 |
|
| 877 |
static const struct signature_alg dsa_sha1_alg = { |
| 878 |
"dsa-with-sha1", |
| 879 |
ASN1_OID_ID_DSA_WITH_SHA1, |
| 880 |
NULL, |
| 881 |
ASN1_OID_ID_DSA, |
| 882 |
&_hx509_signature_sha1_data, |
| 883 |
PROVIDE_CONF|REQUIRE_SIGNER|SIG_PUBLIC_SIG, |
| 884 |
0, |
| 885 |
NULL, |
| 886 |
dsa_verify_signature, |
| 887 |
/* create_signature */ NULL, |
| 888 |
0 |
| 889 |
}; |
| 890 |
|
| 891 |
static const struct signature_alg sha512_alg = { |
| 892 |
"sha-512", |
| 893 |
ASN1_OID_ID_SHA512, |
| 894 |
&_hx509_signature_sha512_data, |
| 895 |
NULL, |
| 896 |
NULL, |
| 897 |
SIG_DIGEST, |
| 898 |
0, |
| 899 |
EVP_sha512, |
| 900 |
evp_md_verify_signature, |
| 901 |
evp_md_create_signature, |
| 902 |
0 |
| 903 |
}; |
| 904 |
|
| 905 |
static const struct signature_alg sha384_alg = { |
| 906 |
"sha-384", |
| 907 |
ASN1_OID_ID_SHA384, |
| 908 |
&_hx509_signature_sha384_data, |
| 909 |
NULL, |
| 910 |
NULL, |
| 911 |
SIG_DIGEST, |
| 912 |
0, |
| 913 |
EVP_sha384, |
| 914 |
evp_md_verify_signature, |
| 915 |
evp_md_create_signature, |
| 916 |
0 |
| 917 |
}; |
| 918 |
|
| 919 |
static const struct signature_alg sha256_alg = { |
| 920 |
"sha-256", |
| 921 |
ASN1_OID_ID_SHA256, |
| 922 |
&_hx509_signature_sha256_data, |
| 923 |
NULL, |
| 924 |
NULL, |
| 925 |
SIG_DIGEST, |
| 926 |
0, |
| 927 |
EVP_sha256, |
| 928 |
evp_md_verify_signature, |
| 929 |
evp_md_create_signature, |
| 930 |
0 |
| 931 |
}; |
| 932 |
|
| 933 |
static const struct signature_alg sha1_alg = { |
| 934 |
"sha1", |
| 935 |
ASN1_OID_ID_SECSIG_SHA_1, |
| 936 |
&_hx509_signature_sha1_data, |
| 937 |
NULL, |
| 938 |
NULL, |
| 939 |
SIG_DIGEST, |
| 940 |
0, |
| 941 |
EVP_sha1, |
| 942 |
evp_md_verify_signature, |
| 943 |
evp_md_create_signature, |
| 944 |
0 |
| 945 |
}; |
| 946 |
|
| 947 |
static const struct signature_alg md5_alg = { |
| 948 |
"rsa-md5", |
| 949 |
ASN1_OID_ID_RSA_DIGEST_MD5, |
| 950 |
&_hx509_signature_md5_data, |
| 951 |
NULL, |
| 952 |
NULL, |
| 953 |
SIG_DIGEST|WEAK_SIG_ALG, |
| 954 |
0, |
| 955 |
EVP_md5, |
| 956 |
evp_md_verify_signature, |
| 957 |
NULL, |
| 958 |
0 |
| 959 |
}; |
| 960 |
|
| 961 |
/* |
| 962 |
* Order matter in this structure, "best" first for each "key |
| 963 |
* compatible" type (type is ECDSA, RSA, DSA, none, etc) |
| 964 |
*/ |
| 965 |
|
| 966 |
static const struct signature_alg *sig_algs[] = { |
| 967 |
#ifdef HAVE_HCRYPTO_W_OPENSSL |
| 968 |
&ecdsa_with_sha512_alg, |
| 969 |
&ecdsa_with_sha384_alg, |
| 970 |
&ecdsa_with_sha256_alg, |
| 971 |
&ecdsa_with_sha1_alg, |
| 972 |
#endif |
| 973 |
&rsa_with_sha512_alg, |
| 974 |
&rsa_with_sha384_alg, |
| 975 |
&rsa_with_sha256_alg, |
| 976 |
&rsa_with_sha1_alg, |
| 977 |
&rsa_with_sha1_alg_secsig, |
| 978 |
&pkcs1_rsa_sha1_alg, |
| 979 |
&rsa_with_md5_alg, |
| 980 |
&heim_rsa_pkcs1_x509, |
| 981 |
&dsa_sha1_alg, |
| 982 |
&sha512_alg, |
| 983 |
&sha384_alg, |
| 984 |
&sha256_alg, |
| 985 |
&sha1_alg, |
| 986 |
&md5_alg, |
| 987 |
NULL |
| 988 |
}; |
| 989 |
|
| 990 |
const struct signature_alg * |
| 991 |
_hx509_find_sig_alg(const heim_oid *oid) |
| 992 |
{ |
| 993 |
unsigned int i; |
| 994 |
for (i = 0; sig_algs[i]; i++) |
| 995 |
if (der_heim_oid_cmp(sig_algs[i]->sig_oid, oid) == 0) |
| 996 |
return sig_algs[i]; |
| 997 |
return NULL; |
| 998 |
} |
| 999 |
|
| 1000 |
static const AlgorithmIdentifier * |
| 1001 |
alg_for_privatekey(const hx509_private_key pk, int type) |
| 1002 |
{ |
| 1003 |
const heim_oid *keytype; |
| 1004 |
unsigned int i; |
| 1005 |
|
| 1006 |
if (pk->ops == NULL) |
| 1007 |
return NULL; |
| 1008 |
|
| 1009 |
keytype = pk->ops->key_oid; |
| 1010 |
|
| 1011 |
for (i = 0; sig_algs[i]; i++) { |
| 1012 |
if (sig_algs[i]->key_oid == NULL) |
| 1013 |
continue; |
| 1014 |
if (der_heim_oid_cmp(sig_algs[i]->key_oid, keytype) != 0) |
| 1015 |
continue; |
| 1016 |
if (pk->ops->available && |
| 1017 |
pk->ops->available(pk, sig_algs[i]->sig_alg) == 0) |
| 1018 |
continue; |
| 1019 |
if (type == HX509_SELECT_PUBLIC_SIG) |
| 1020 |
return sig_algs[i]->sig_alg; |
| 1021 |
if (type == HX509_SELECT_DIGEST) |
| 1022 |
return sig_algs[i]->digest_alg; |
| 1023 |
|
| 1024 |
return NULL; |
| 1025 |
} |
| 1026 |
return NULL; |
| 1027 |
} |
| 1028 |
|
| 1029 |
/* |
| 1030 |
* |
| 1031 |
*/ |
| 1032 |
#ifdef HAVE_HCRYPTO_W_OPENSSL |
| 1033 |
extern hx509_private_key_ops ecdsa_private_key_ops; |
| 1034 |
#endif |
| 1035 |
|
| 1036 |
static struct hx509_private_key_ops *private_algs[] = { |
| 1037 |
&rsa_private_key_ops, |
| 1038 |
#ifdef HAVE_HCRYPTO_W_OPENSSL |
| 1039 |
&ecdsa_private_key_ops, |
| 1040 |
#endif |
| 1041 |
NULL |
| 1042 |
}; |
| 1043 |
|
| 1044 |
hx509_private_key_ops * |
| 1045 |
hx509_find_private_alg(const heim_oid *oid) |
| 1046 |
{ |
| 1047 |
int i; |
| 1048 |
for (i = 0; private_algs[i]; i++) { |
| 1049 |
if (private_algs[i]->key_oid == NULL) |
| 1050 |
continue; |
| 1051 |
if (der_heim_oid_cmp(private_algs[i]->key_oid, oid) == 0) |
| 1052 |
return private_algs[i]; |
| 1053 |
} |
| 1054 |
return NULL; |
| 1055 |
} |
| 1056 |
|
| 1057 |
/* |
| 1058 |
* Check if the algorithm `alg' have a best before date, and if it |
| 1059 |
* des, make sure the its before the time `t'. |
| 1060 |
*/ |
| 1061 |
|
| 1062 |
int |
| 1063 |
_hx509_signature_is_weak(hx509_context context, const AlgorithmIdentifier *alg) |
| 1064 |
{ |
| 1065 |
const struct signature_alg *md; |
| 1066 |
|
| 1067 |
md = _hx509_find_sig_alg(&alg->algorithm); |
| 1068 |
if (md == NULL) { |
| 1069 |
hx509_clear_error_string(context); |
| 1070 |
return HX509_SIG_ALG_NO_SUPPORTED; |
| 1071 |
} |
| 1072 |
if (md->flags & WEAK_SIG_ALG) { |
| 1073 |
hx509_set_error_string(context, 0, HX509_CRYPTO_ALGORITHM_BEST_BEFORE, |
| 1074 |
"Algorithm %s is weak", md->name); |
| 1075 |
return HX509_CRYPTO_ALGORITHM_BEST_BEFORE; |
| 1076 |
} |
| 1077 |
return 0; |
| 1078 |
} |
| 1079 |
|
| 1080 |
int |
| 1081 |
_hx509_self_signed_valid(hx509_context context, |
| 1082 |
const AlgorithmIdentifier *alg) |
| 1083 |
{ |
| 1084 |
const struct signature_alg *md; |
| 1085 |
|
| 1086 |
md = _hx509_find_sig_alg(&alg->algorithm); |
| 1087 |
if (md == NULL) { |
| 1088 |
hx509_clear_error_string(context); |
| 1089 |
return HX509_SIG_ALG_NO_SUPPORTED; |
| 1090 |
} |
| 1091 |
if ((md->flags & SELF_SIGNED_OK) == 0) { |
| 1092 |
hx509_set_error_string(context, 0, HX509_CRYPTO_ALGORITHM_BEST_BEFORE, |
| 1093 |
"Algorithm %s not trusted for self signatures", |
| 1094 |
md->name); |
| 1095 |
return HX509_CRYPTO_ALGORITHM_BEST_BEFORE; |
| 1096 |
} |
| 1097 |
return 0; |
| 1098 |
} |
| 1099 |
|
| 1100 |
|
| 1101 |
int |
| 1102 |
_hx509_verify_signature(hx509_context context, |
| 1103 |
const hx509_cert cert, |
| 1104 |
const AlgorithmIdentifier *alg, |
| 1105 |
const heim_octet_string *data, |
| 1106 |
const heim_octet_string *sig) |
| 1107 |
{ |
| 1108 |
const struct signature_alg *md; |
| 1109 |
const Certificate *signer = NULL; |
| 1110 |
|
| 1111 |
if (cert) |
| 1112 |
signer = _hx509_get_cert(cert); |
| 1113 |
|
| 1114 |
md = _hx509_find_sig_alg(&alg->algorithm); |
| 1115 |
if (md == NULL) { |
| 1116 |
hx509_clear_error_string(context); |
| 1117 |
return HX509_SIG_ALG_NO_SUPPORTED; |
| 1118 |
} |
| 1119 |
if (signer && (md->flags & PROVIDE_CONF) == 0) { |
| 1120 |
hx509_clear_error_string(context); |
| 1121 |
return HX509_CRYPTO_SIG_NO_CONF; |
| 1122 |
} |
| 1123 |
if (signer == NULL && (md->flags & REQUIRE_SIGNER)) { |
| 1124 |
hx509_clear_error_string(context); |
| 1125 |
return HX509_CRYPTO_SIGNATURE_WITHOUT_SIGNER; |
| 1126 |
} |
| 1127 |
if (md->key_oid && signer) { |
| 1128 |
const SubjectPublicKeyInfo *spi; |
| 1129 |
spi = &signer->tbsCertificate.subjectPublicKeyInfo; |
| 1130 |
|
| 1131 |
if (der_heim_oid_cmp(&spi->algorithm.algorithm, md->key_oid) != 0) { |
| 1132 |
hx509_clear_error_string(context); |
| 1133 |
return HX509_SIG_ALG_DONT_MATCH_KEY_ALG; |
| 1134 |
} |
| 1135 |
} |
| 1136 |
return (*md->verify_signature)(context, md, signer, alg, data, sig); |
| 1137 |
} |
| 1138 |
|
| 1139 |
int |
| 1140 |
_hx509_create_signature(hx509_context context, |
| 1141 |
const hx509_private_key signer, |
| 1142 |
const AlgorithmIdentifier *alg, |
| 1143 |
const heim_octet_string *data, |
| 1144 |
AlgorithmIdentifier *signatureAlgorithm, |
| 1145 |
heim_octet_string *sig) |
| 1146 |
{ |
| 1147 |
const struct signature_alg *md; |
| 1148 |
|
| 1149 |
md = _hx509_find_sig_alg(&alg->algorithm); |
| 1150 |
if (md == NULL) { |
| 1151 |
hx509_set_error_string(context, 0, HX509_SIG_ALG_NO_SUPPORTED, |
| 1152 |
"algorithm no supported"); |
| 1153 |
return HX509_SIG_ALG_NO_SUPPORTED; |
| 1154 |
} |
| 1155 |
|
| 1156 |
if (signer && (md->flags & PROVIDE_CONF) == 0) { |
| 1157 |
hx509_set_error_string(context, 0, HX509_SIG_ALG_NO_SUPPORTED, |
| 1158 |
"algorithm provides no conf"); |
| 1159 |
return HX509_CRYPTO_SIG_NO_CONF; |
| 1160 |
} |
| 1161 |
|
| 1162 |
return (*md->create_signature)(context, md, signer, alg, data, |
| 1163 |
signatureAlgorithm, sig); |
| 1164 |
} |
| 1165 |
|
| 1166 |
int |
| 1167 |
_hx509_create_signature_bitstring(hx509_context context, |
| 1168 |
const hx509_private_key signer, |
| 1169 |
const AlgorithmIdentifier *alg, |
| 1170 |
const heim_octet_string *data, |
| 1171 |
AlgorithmIdentifier *signatureAlgorithm, |
| 1172 |
heim_bit_string *sig) |
| 1173 |
{ |
| 1174 |
heim_octet_string os; |
| 1175 |
int ret; |
| 1176 |
|
| 1177 |
ret = _hx509_create_signature(context, signer, alg, |
| 1178 |
data, signatureAlgorithm, &os); |
| 1179 |
if (ret) |
| 1180 |
return ret; |
| 1181 |
sig->data = os.data; |
| 1182 |
sig->length = os.length * 8; |
| 1183 |
return 0; |
| 1184 |
} |
| 1185 |
|
| 1186 |
int |
| 1187 |
_hx509_public_encrypt(hx509_context context, |
| 1188 |
const heim_octet_string *cleartext, |
| 1189 |
const Certificate *cert, |
| 1190 |
heim_oid *encryption_oid, |
| 1191 |
heim_octet_string *ciphertext) |
| 1192 |
{ |
| 1193 |
const SubjectPublicKeyInfo *spi; |
| 1194 |
unsigned char *to; |
| 1195 |
int tosize; |
| 1196 |
int ret; |
| 1197 |
RSA *rsa; |
| 1198 |
size_t size; |
| 1199 |
const unsigned char *p; |
| 1200 |
|
| 1201 |
ciphertext->data = NULL; |
| 1202 |
ciphertext->length = 0; |
| 1203 |
|
| 1204 |
spi = &cert->tbsCertificate.subjectPublicKeyInfo; |
| 1205 |
|
| 1206 |
p = spi->subjectPublicKey.data; |
| 1207 |
size = spi->subjectPublicKey.length / 8; |
| 1208 |
|
| 1209 |
rsa = d2i_RSAPublicKey(NULL, &p, size); |
| 1210 |
if (rsa == NULL) { |
| 1211 |
hx509_set_error_string(context, 0, ENOMEM, "out of memory"); |
| 1212 |
return ENOMEM; |
| 1213 |
} |
| 1214 |
|
| 1215 |
tosize = RSA_size(rsa); |
| 1216 |
to = malloc(tosize); |
| 1217 |
if (to == NULL) { |
| 1218 |
RSA_free(rsa); |
| 1219 |
hx509_set_error_string(context, 0, ENOMEM, "out of memory"); |
| 1220 |
return ENOMEM; |
| 1221 |
} |
| 1222 |
|
| 1223 |
ret = RSA_public_encrypt(cleartext->length, |
| 1224 |
(unsigned char *)cleartext->data, |
| 1225 |
to, rsa, RSA_PKCS1_PADDING); |
| 1226 |
RSA_free(rsa); |
| 1227 |
if (ret <= 0) { |
| 1228 |
free(to); |
| 1229 |
hx509_set_error_string(context, 0, HX509_CRYPTO_RSA_PUBLIC_ENCRYPT, |
| 1230 |
"RSA public encrypt failed with %d", ret); |
| 1231 |
return HX509_CRYPTO_RSA_PUBLIC_ENCRYPT; |
| 1232 |
} |
| 1233 |
if (ret > tosize) |
| 1234 |
_hx509_abort("internal rsa decryption failure: ret > tosize"); |
| 1235 |
|
| 1236 |
ciphertext->length = ret; |
| 1237 |
ciphertext->data = to; |
| 1238 |
|
| 1239 |
ret = der_copy_oid(ASN1_OID_ID_PKCS1_RSAENCRYPTION, encryption_oid); |
| 1240 |
if (ret) { |
| 1241 |
der_free_octet_string(ciphertext); |
| 1242 |
hx509_set_error_string(context, 0, ENOMEM, "out of memory"); |
| 1243 |
return ENOMEM; |
| 1244 |
} |
| 1245 |
|
| 1246 |
return 0; |
| 1247 |
} |
| 1248 |
|
| 1249 |
int |
| 1250 |
hx509_private_key_private_decrypt(hx509_context context, |
| 1251 |
const heim_octet_string *ciphertext, |
| 1252 |
const heim_oid *encryption_oid, |
| 1253 |
hx509_private_key p, |
| 1254 |
heim_octet_string *cleartext) |
| 1255 |
{ |
| 1256 |
int ret; |
| 1257 |
|
| 1258 |
cleartext->data = NULL; |
| 1259 |
cleartext->length = 0; |
| 1260 |
|
| 1261 |
if (p->private_key.rsa == NULL) { |
| 1262 |
hx509_set_error_string(context, 0, HX509_PRIVATE_KEY_MISSING, |
| 1263 |
"Private RSA key missing"); |
| 1264 |
return HX509_PRIVATE_KEY_MISSING; |
| 1265 |
} |
| 1266 |
|
| 1267 |
cleartext->length = RSA_size(p->private_key.rsa); |
| 1268 |
cleartext->data = malloc(cleartext->length); |
| 1269 |
if (cleartext->data == NULL) { |
| 1270 |
hx509_set_error_string(context, 0, ENOMEM, "out of memory"); |
| 1271 |
return ENOMEM; |
| 1272 |
} |
| 1273 |
ret = RSA_private_decrypt(ciphertext->length, ciphertext->data, |
| 1274 |
cleartext->data, |
| 1275 |
p->private_key.rsa, |
| 1276 |
RSA_PKCS1_PADDING); |
| 1277 |
if (ret <= 0) { |
| 1278 |
der_free_octet_string(cleartext); |
| 1279 |
hx509_set_error_string(context, 0, HX509_CRYPTO_RSA_PRIVATE_DECRYPT, |
| 1280 |
"Failed to decrypt using private key: %d", ret); |
| 1281 |
return HX509_CRYPTO_RSA_PRIVATE_DECRYPT; |
| 1282 |
} |
| 1283 |
if (cleartext->length < (size_t)ret) |
| 1284 |
_hx509_abort("internal rsa decryption failure: ret > tosize"); |
| 1285 |
|
| 1286 |
cleartext->length = ret; |
| 1287 |
|
| 1288 |
return 0; |
| 1289 |
} |
| 1290 |
|
| 1291 |
|
| 1292 |
int |
| 1293 |
hx509_parse_private_key(hx509_context context, |
| 1294 |
const AlgorithmIdentifier *keyai, |
| 1295 |
const void *data, |
| 1296 |
size_t len, |
| 1297 |
hx509_key_format_t format, |
| 1298 |
hx509_private_key *private_key) |
| 1299 |
{ |
| 1300 |
struct hx509_private_key_ops *ops; |
| 1301 |
int ret; |
| 1302 |
|
| 1303 |
*private_key = NULL; |
| 1304 |
|
| 1305 |
ops = hx509_find_private_alg(&keyai->algorithm); |
| 1306 |
if (ops == NULL) { |
| 1307 |
hx509_clear_error_string(context); |
| 1308 |
return HX509_SIG_ALG_NO_SUPPORTED; |
| 1309 |
} |
| 1310 |
|
| 1311 |
ret = hx509_private_key_init(private_key, ops, NULL); |
| 1312 |
if (ret) { |
| 1313 |
hx509_set_error_string(context, 0, ret, "out of memory"); |
| 1314 |
return ret; |
| 1315 |
} |
| 1316 |
|
| 1317 |
ret = (*ops->import)(context, keyai, data, len, format, *private_key); |
| 1318 |
if (ret) |
| 1319 |
hx509_private_key_free(private_key); |
| 1320 |
|
| 1321 |
return ret; |
| 1322 |
} |
| 1323 |
|
| 1324 |
/* |
| 1325 |
* |
| 1326 |
*/ |
| 1327 |
|
| 1328 |
int |
| 1329 |
hx509_private_key2SPKI(hx509_context context, |
| 1330 |
hx509_private_key private_key, |
| 1331 |
SubjectPublicKeyInfo *spki) |
| 1332 |
{ |
| 1333 |
const struct hx509_private_key_ops *ops = private_key->ops; |
| 1334 |
if (ops == NULL || ops->get_spki == NULL) { |
| 1335 |
hx509_set_error_string(context, 0, HX509_UNIMPLEMENTED_OPERATION, |
| 1336 |
"Private key have no key2SPKI function"); |
| 1337 |
return HX509_UNIMPLEMENTED_OPERATION; |
| 1338 |
} |
| 1339 |
return (*ops->get_spki)(context, private_key, spki); |
| 1340 |
} |
| 1341 |
|
| 1342 |
int |
| 1343 |
_hx509_generate_private_key_init(hx509_context context, |
| 1344 |
const heim_oid *oid, |
| 1345 |
struct hx509_generate_private_context **ctx) |
| 1346 |
{ |
| 1347 |
*ctx = NULL; |
| 1348 |
|
| 1349 |
if (der_heim_oid_cmp(oid, ASN1_OID_ID_PKCS1_RSAENCRYPTION) != 0) { |
| 1350 |
hx509_set_error_string(context, 0, EINVAL, |
| 1351 |
"private key not an RSA key"); |
| 1352 |
return EINVAL; |
| 1353 |
} |
| 1354 |
|
| 1355 |
*ctx = calloc(1, sizeof(**ctx)); |
| 1356 |
if (*ctx == NULL) { |
| 1357 |
hx509_set_error_string(context, 0, ENOMEM, "out of memory"); |
| 1358 |
return ENOMEM; |
| 1359 |
} |
| 1360 |
(*ctx)->key_oid = oid; |
| 1361 |
|
| 1362 |
return 0; |
| 1363 |
} |
| 1364 |
|
| 1365 |
int |
| 1366 |
_hx509_generate_private_key_is_ca(hx509_context context, |
| 1367 |
struct hx509_generate_private_context *ctx) |
| 1368 |
{ |
| 1369 |
ctx->isCA = 1; |
| 1370 |
return 0; |
| 1371 |
} |
| 1372 |
|
| 1373 |
int |
| 1374 |
_hx509_generate_private_key_bits(hx509_context context, |
| 1375 |
struct hx509_generate_private_context *ctx, |
| 1376 |
unsigned long bits) |
| 1377 |
{ |
| 1378 |
ctx->num_bits = bits; |
| 1379 |
return 0; |
| 1380 |
} |
| 1381 |
|
| 1382 |
|
| 1383 |
void |
| 1384 |
_hx509_generate_private_key_free(struct hx509_generate_private_context **ctx) |
| 1385 |
{ |
| 1386 |
free(*ctx); |
| 1387 |
*ctx = NULL; |
| 1388 |
} |
| 1389 |
|
| 1390 |
int |
| 1391 |
_hx509_generate_private_key(hx509_context context, |
| 1392 |
struct hx509_generate_private_context *ctx, |
| 1393 |
hx509_private_key *private_key) |
| 1394 |
{ |
| 1395 |
struct hx509_private_key_ops *ops; |
| 1396 |
int ret; |
| 1397 |
|
| 1398 |
*private_key = NULL; |
| 1399 |
|
| 1400 |
ops = hx509_find_private_alg(ctx->key_oid); |
| 1401 |
if (ops == NULL) { |
| 1402 |
hx509_clear_error_string(context); |
| 1403 |
return HX509_SIG_ALG_NO_SUPPORTED; |
| 1404 |
} |
| 1405 |
|
| 1406 |
ret = hx509_private_key_init(private_key, ops, NULL); |
| 1407 |
if (ret) { |
| 1408 |
hx509_set_error_string(context, 0, ret, "out of memory"); |
| 1409 |
return ret; |
| 1410 |
} |
| 1411 |
|
| 1412 |
ret = (*ops->generate_private_key)(context, ctx, *private_key); |
| 1413 |
if (ret) |
| 1414 |
hx509_private_key_free(private_key); |
| 1415 |
|
| 1416 |
return ret; |
| 1417 |
} |
| 1418 |
|
| 1419 |
/* |
| 1420 |
* |
| 1421 |
*/ |
| 1422 |
|
| 1423 |
const AlgorithmIdentifier * |
| 1424 |
hx509_signature_sha512(void) |
| 1425 |
{ return &_hx509_signature_sha512_data; } |
| 1426 |
|
| 1427 |
const AlgorithmIdentifier * |
| 1428 |
hx509_signature_sha384(void) |
| 1429 |
{ return &_hx509_signature_sha384_data; } |
| 1430 |
|
| 1431 |
const AlgorithmIdentifier * |
| 1432 |
hx509_signature_sha256(void) |
| 1433 |
{ return &_hx509_signature_sha256_data; } |
| 1434 |
|
| 1435 |
const AlgorithmIdentifier * |
| 1436 |
hx509_signature_sha1(void) |
| 1437 |
{ return &_hx509_signature_sha1_data; } |
| 1438 |
|
| 1439 |
const AlgorithmIdentifier * |
| 1440 |
hx509_signature_md5(void) |
| 1441 |
{ return &_hx509_signature_md5_data; } |
| 1442 |
|
| 1443 |
const AlgorithmIdentifier * |
| 1444 |
hx509_signature_rsa_with_sha512(void) |
| 1445 |
{ return &_hx509_signature_rsa_with_sha512_data; } |
| 1446 |
|
| 1447 |
const AlgorithmIdentifier * |
| 1448 |
hx509_signature_rsa_with_sha384(void) |
| 1449 |
{ return &_hx509_signature_rsa_with_sha384_data; } |
| 1450 |
|
| 1451 |
const AlgorithmIdentifier * |
| 1452 |
hx509_signature_rsa_with_sha256(void) |
| 1453 |
{ return &_hx509_signature_rsa_with_sha256_data; } |
| 1454 |
|
| 1455 |
const AlgorithmIdentifier * |
| 1456 |
hx509_signature_rsa_with_sha1(void) |
| 1457 |
{ return &_hx509_signature_rsa_with_sha1_data; } |
| 1458 |
|
| 1459 |
const AlgorithmIdentifier * |
| 1460 |
hx509_signature_rsa_with_md5(void) |
| 1461 |
{ return &_hx509_signature_rsa_with_md5_data; } |
| 1462 |
|
| 1463 |
const AlgorithmIdentifier * |
| 1464 |
hx509_signature_rsa(void) |
| 1465 |
{ return &_hx509_signature_rsa_data; } |
| 1466 |
|
| 1467 |
const AlgorithmIdentifier * |
| 1468 |
hx509_signature_rsa_pkcs1_x509(void) |
| 1469 |
{ return &_hx509_signature_rsa_pkcs1_x509_data; } |
| 1470 |
|
| 1471 |
const AlgorithmIdentifier * |
| 1472 |
hx509_crypto_des_rsdi_ede3_cbc(void) |
| 1473 |
{ return &_hx509_des_rsdi_ede3_cbc_oid; } |
| 1474 |
|
| 1475 |
const AlgorithmIdentifier * |
| 1476 |
hx509_crypto_aes128_cbc(void) |
| 1477 |
{ return &_hx509_crypto_aes128_cbc_data; } |
| 1478 |
|
| 1479 |
const AlgorithmIdentifier * |
| 1480 |
hx509_crypto_aes256_cbc(void) |
| 1481 |
{ return &_hx509_crypto_aes256_cbc_data; } |
| 1482 |
|
| 1483 |
/* |
| 1484 |
* |
| 1485 |
*/ |
| 1486 |
|
| 1487 |
const AlgorithmIdentifier * _hx509_crypto_default_sig_alg = |
| 1488 |
&_hx509_signature_rsa_with_sha256_data; |
| 1489 |
const AlgorithmIdentifier * _hx509_crypto_default_digest_alg = |
| 1490 |
&_hx509_signature_sha256_data; |
| 1491 |
const AlgorithmIdentifier * _hx509_crypto_default_secret_alg = |
| 1492 |
&_hx509_crypto_aes128_cbc_data; |
| 1493 |
|
| 1494 |
/* |
| 1495 |
* |
| 1496 |
*/ |
| 1497 |
|
| 1498 |
int |
| 1499 |
hx509_private_key_init(hx509_private_key *key, |
| 1500 |
hx509_private_key_ops *ops, |
| 1501 |
void *keydata) |
| 1502 |
{ |
| 1503 |
*key = calloc(1, sizeof(**key)); |
| 1504 |
if (*key == NULL) |
| 1505 |
return ENOMEM; |
| 1506 |
(*key)->ref = 1; |
| 1507 |
(*key)->ops = ops; |
| 1508 |
(*key)->private_key.keydata = keydata; |
| 1509 |
return 0; |
| 1510 |
} |
| 1511 |
|
| 1512 |
hx509_private_key |
| 1513 |
_hx509_private_key_ref(hx509_private_key key) |
| 1514 |
{ |
| 1515 |
if (key->ref == 0) |
| 1516 |
_hx509_abort("key refcount <= 0 on ref"); |
| 1517 |
key->ref++; |
| 1518 |
if (key->ref == UINT_MAX) |
| 1519 |
_hx509_abort("key refcount == UINT_MAX on ref"); |
| 1520 |
return key; |
| 1521 |
} |
| 1522 |
|
| 1523 |
const char * |
| 1524 |
_hx509_private_pem_name(hx509_private_key key) |
| 1525 |
{ |
| 1526 |
return key->ops->pemtype; |
| 1527 |
} |
| 1528 |
|
| 1529 |
int |
| 1530 |
hx509_private_key_free(hx509_private_key *key) |
| 1531 |
{ |
| 1532 |
if (key == NULL || *key == NULL) |
| 1533 |
return 0; |
| 1534 |
|
| 1535 |
if ((*key)->ref == 0) |
| 1536 |
_hx509_abort("key refcount == 0 on free"); |
| 1537 |
if (--(*key)->ref > 0) |
| 1538 |
return 0; |
| 1539 |
|
| 1540 |
if ((*key)->ops && der_heim_oid_cmp((*key)->ops->key_oid, ASN1_OID_ID_PKCS1_RSAENCRYPTION) == 0) { |
| 1541 |
if ((*key)->private_key.rsa) |
| 1542 |
RSA_free((*key)->private_key.rsa); |
| 1543 |
} else if ((*key)->ops && der_heim_oid_cmp((*key)->ops->key_oid, |
| 1544 |
ASN1_OID_ID_ECPUBLICKEY) == 0 && |
| 1545 |
(*key)->private_key.ecdsa != NULL) { |
| 1546 |
_hx509_private_eckey_free((*key)->private_key.ecdsa); |
| 1547 |
} |
| 1548 |
(*key)->private_key.rsa = NULL; |
| 1549 |
free(*key); |
| 1550 |
*key = NULL; |
| 1551 |
return 0; |
| 1552 |
} |
| 1553 |
|
| 1554 |
void |
| 1555 |
hx509_private_key_assign_rsa(hx509_private_key key, void *ptr) |
| 1556 |
{ |
| 1557 |
if (key->private_key.rsa) |
| 1558 |
RSA_free(key->private_key.rsa); |
| 1559 |
key->private_key.rsa = ptr; |
| 1560 |
key->signature_alg = ASN1_OID_ID_PKCS1_SHA1WITHRSAENCRYPTION; |
| 1561 |
key->md = &pkcs1_rsa_sha1_alg; |
| 1562 |
} |
| 1563 |
|
| 1564 |
int |
| 1565 |
_hx509_private_key_oid(hx509_context context, |
| 1566 |
const hx509_private_key key, |
| 1567 |
heim_oid *data) |
| 1568 |
{ |
| 1569 |
int ret; |
| 1570 |
ret = der_copy_oid(key->ops->key_oid, data); |
| 1571 |
if (ret) |
| 1572 |
hx509_set_error_string(context, 0, ret, "malloc out of memory"); |
| 1573 |
return ret; |
| 1574 |
} |
| 1575 |
|
| 1576 |
int |
| 1577 |
_hx509_private_key_exportable(hx509_private_key key) |
| 1578 |
{ |
| 1579 |
if (key->ops->export == NULL) |
| 1580 |
return 0; |
| 1581 |
return 1; |
| 1582 |
} |
| 1583 |
|
| 1584 |
BIGNUM * |
| 1585 |
_hx509_private_key_get_internal(hx509_context context, |
| 1586 |
hx509_private_key key, |
| 1587 |
const char *type) |
| 1588 |
{ |
| 1589 |
if (key->ops->get_internal == NULL) |
| 1590 |
return NULL; |
| 1591 |
return (*key->ops->get_internal)(context, key, type); |
| 1592 |
} |
| 1593 |
|
| 1594 |
int |
| 1595 |
_hx509_private_key_export(hx509_context context, |
| 1596 |
const hx509_private_key key, |
| 1597 |
hx509_key_format_t format, |
| 1598 |
heim_octet_string *data) |
| 1599 |
{ |
| 1600 |
if (key->ops->export == NULL) { |
| 1601 |
hx509_clear_error_string(context); |
| 1602 |
return HX509_UNIMPLEMENTED_OPERATION; |
| 1603 |
} |
| 1604 |
return (*key->ops->export)(context, key, format, data); |
| 1605 |
} |
| 1606 |
|
| 1607 |
/* |
| 1608 |
* |
| 1609 |
*/ |
| 1610 |
|
| 1611 |
struct hx509cipher { |
| 1612 |
const char *name; |
| 1613 |
int flags; |
| 1614 |
#define CIPHER_WEAK 1 |
| 1615 |
const heim_oid *oid; |
| 1616 |
const AlgorithmIdentifier *(*ai_func)(void); |
| 1617 |
const EVP_CIPHER *(*evp_func)(void); |
| 1618 |
int (*get_params)(hx509_context, const hx509_crypto, |
| 1619 |
const heim_octet_string *, heim_octet_string *); |
| 1620 |
int (*set_params)(hx509_context, const heim_octet_string *, |
| 1621 |
hx509_crypto, heim_octet_string *); |
| 1622 |
}; |
| 1623 |
|
| 1624 |
struct hx509_crypto_data { |
| 1625 |
char *name; |
| 1626 |
int flags; |
| 1627 |
#define ALLOW_WEAK 1 |
| 1628 |
|
| 1629 |
#define PADDING_NONE 2 |
| 1630 |
#define PADDING_PKCS7 4 |
| 1631 |
#define PADDING_FLAGS (2|4) |
| 1632 |
const struct hx509cipher *cipher; |
| 1633 |
const EVP_CIPHER *c; |
| 1634 |
heim_octet_string key; |
| 1635 |
heim_oid oid; |
| 1636 |
void *param; |
| 1637 |
}; |
| 1638 |
|
| 1639 |
/* |
| 1640 |
* |
| 1641 |
*/ |
| 1642 |
|
| 1643 |
static unsigned private_rc2_40_oid_data[] = { 127, 1 }; |
| 1644 |
|
| 1645 |
static heim_oid asn1_oid_private_rc2_40 = |
| 1646 |
{ 2, private_rc2_40_oid_data }; |
| 1647 |
|
| 1648 |
/* |
| 1649 |
* |
| 1650 |
*/ |
| 1651 |
|
| 1652 |
static int |
| 1653 |
CMSCBCParam_get(hx509_context context, const hx509_crypto crypto, |
| 1654 |
const heim_octet_string *ivec, heim_octet_string *param) |
| 1655 |
{ |
| 1656 |
size_t size; |
| 1657 |
int ret; |
| 1658 |
|
| 1659 |
assert(crypto->param == NULL); |
| 1660 |
if (ivec == NULL) |
| 1661 |
return 0; |
| 1662 |
|
| 1663 |
ASN1_MALLOC_ENCODE(CMSCBCParameter, param->data, param->length, |
| 1664 |
ivec, &size, ret); |
| 1665 |
if (ret == 0 && size != param->length) |
| 1666 |
_hx509_abort("Internal asn1 encoder failure"); |
| 1667 |
if (ret) |
| 1668 |
hx509_clear_error_string(context); |
| 1669 |
return ret; |
| 1670 |
} |
| 1671 |
|
| 1672 |
static int |
| 1673 |
CMSCBCParam_set(hx509_context context, const heim_octet_string *param, |
| 1674 |
hx509_crypto crypto, heim_octet_string *ivec) |
| 1675 |
{ |
| 1676 |
int ret; |
| 1677 |
if (ivec == NULL) |
| 1678 |
return 0; |
| 1679 |
|
| 1680 |
ret = decode_CMSCBCParameter(param->data, param->length, ivec, NULL); |
| 1681 |
if (ret) |
| 1682 |
hx509_clear_error_string(context); |
| 1683 |
|
| 1684 |
return ret; |
| 1685 |
} |
| 1686 |
|
| 1687 |
struct _RC2_params { |
| 1688 |
int maximum_effective_key; |
| 1689 |
}; |
| 1690 |
|
| 1691 |
static int |
| 1692 |
CMSRC2CBCParam_get(hx509_context context, const hx509_crypto crypto, |
| 1693 |
const heim_octet_string *ivec, heim_octet_string *param) |
| 1694 |
{ |
| 1695 |
CMSRC2CBCParameter rc2params; |
| 1696 |
const struct _RC2_params *p = crypto->param; |
| 1697 |
int maximum_effective_key = 128; |
| 1698 |
size_t size; |
| 1699 |
int ret; |
| 1700 |
|
| 1701 |
memset(&rc2params, 0, sizeof(rc2params)); |
| 1702 |
|
| 1703 |
if (p) |
| 1704 |
maximum_effective_key = p->maximum_effective_key; |
| 1705 |
|
| 1706 |
switch(maximum_effective_key) { |
| 1707 |
case 40: |
| 1708 |
rc2params.rc2ParameterVersion = 160; |
| 1709 |
break; |
| 1710 |
case 64: |
| 1711 |
rc2params.rc2ParameterVersion = 120; |
| 1712 |
break; |
| 1713 |
case 128: |
| 1714 |
rc2params.rc2ParameterVersion = 58; |
| 1715 |
break; |
| 1716 |
} |
| 1717 |
rc2params.iv = *ivec; |
| 1718 |
|
| 1719 |
ASN1_MALLOC_ENCODE(CMSRC2CBCParameter, param->data, param->length, |
| 1720 |
&rc2params, &size, ret); |
| 1721 |
if (ret == 0 && size != param->length) |
| 1722 |
_hx509_abort("Internal asn1 encoder failure"); |
| 1723 |
|
| 1724 |
return ret; |
| 1725 |
} |
| 1726 |
|
| 1727 |
static int |
| 1728 |
CMSRC2CBCParam_set(hx509_context context, const heim_octet_string *param, |
| 1729 |
hx509_crypto crypto, heim_octet_string *ivec) |
| 1730 |
{ |
| 1731 |
CMSRC2CBCParameter rc2param; |
| 1732 |
struct _RC2_params *p; |
| 1733 |
size_t size; |
| 1734 |
int ret; |
| 1735 |
|
| 1736 |
ret = decode_CMSRC2CBCParameter(param->data, param->length, |
| 1737 |
&rc2param, &size); |
| 1738 |
if (ret) { |
| 1739 |
hx509_clear_error_string(context); |
| 1740 |
return ret; |
| 1741 |
} |
| 1742 |
|
| 1743 |
p = calloc(1, sizeof(*p)); |
| 1744 |
if (p == NULL) { |
| 1745 |
free_CMSRC2CBCParameter(&rc2param); |
| 1746 |
hx509_clear_error_string(context); |
| 1747 |
return ENOMEM; |
| 1748 |
} |
| 1749 |
switch(rc2param.rc2ParameterVersion) { |
| 1750 |
case 160: |
| 1751 |
crypto->c = EVP_rc2_40_cbc(); |
| 1752 |
p->maximum_effective_key = 40; |
| 1753 |
break; |
| 1754 |
case 120: |
| 1755 |
crypto->c = EVP_rc2_64_cbc(); |
| 1756 |
p->maximum_effective_key = 64; |
| 1757 |
break; |
| 1758 |
case 58: |
| 1759 |
crypto->c = EVP_rc2_cbc(); |
| 1760 |
p->maximum_effective_key = 128; |
| 1761 |
break; |
| 1762 |
default: |
| 1763 |
free(p); |
| 1764 |
free_CMSRC2CBCParameter(&rc2param); |
| 1765 |
return HX509_CRYPTO_SIG_INVALID_FORMAT; |
| 1766 |
} |
| 1767 |
if (ivec) |
| 1768 |
ret = der_copy_octet_string(&rc2param.iv, ivec); |
| 1769 |
free_CMSRC2CBCParameter(&rc2param); |
| 1770 |
if (ret) { |
| 1771 |
free(p); |
| 1772 |
hx509_clear_error_string(context); |
| 1773 |
} else |
| 1774 |
crypto->param = p; |
| 1775 |
|
| 1776 |
return ret; |
| 1777 |
} |
| 1778 |
|
| 1779 |
/* |
| 1780 |
* |
| 1781 |
*/ |
| 1782 |
|
| 1783 |
static const struct hx509cipher ciphers[] = { |
| 1784 |
{ |
| 1785 |
"rc2-cbc", |
| 1786 |
CIPHER_WEAK, |
| 1787 |
ASN1_OID_ID_PKCS3_RC2_CBC, |
| 1788 |
NULL, |
| 1789 |
EVP_rc2_cbc, |
| 1790 |
CMSRC2CBCParam_get, |
| 1791 |
CMSRC2CBCParam_set |
| 1792 |
}, |
| 1793 |
{ |
| 1794 |
"rc2-cbc", |
| 1795 |
CIPHER_WEAK, |
| 1796 |
ASN1_OID_ID_RSADSI_RC2_CBC, |
| 1797 |
NULL, |
| 1798 |
EVP_rc2_cbc, |
| 1799 |
CMSRC2CBCParam_get, |
| 1800 |
CMSRC2CBCParam_set |
| 1801 |
}, |
| 1802 |
{ |
| 1803 |
"rc2-40-cbc", |
| 1804 |
CIPHER_WEAK, |
| 1805 |
&asn1_oid_private_rc2_40, |
| 1806 |
NULL, |
| 1807 |
EVP_rc2_40_cbc, |
| 1808 |
CMSRC2CBCParam_get, |
| 1809 |
CMSRC2CBCParam_set |
| 1810 |
}, |
| 1811 |
{ |
| 1812 |
"des-ede3-cbc", |
| 1813 |
0, |
| 1814 |
ASN1_OID_ID_PKCS3_DES_EDE3_CBC, |
| 1815 |
NULL, |
| 1816 |
EVP_des_ede3_cbc, |
| 1817 |
CMSCBCParam_get, |
| 1818 |
CMSCBCParam_set |
| 1819 |
}, |
| 1820 |
{ |
| 1821 |
"des-ede3-cbc", |
| 1822 |
0, |
| 1823 |
ASN1_OID_ID_RSADSI_DES_EDE3_CBC, |
| 1824 |
hx509_crypto_des_rsdi_ede3_cbc, |
| 1825 |
EVP_des_ede3_cbc, |
| 1826 |
CMSCBCParam_get, |
| 1827 |
CMSCBCParam_set |
| 1828 |
}, |
| 1829 |
{ |
| 1830 |
"aes-128-cbc", |
| 1831 |
0, |
| 1832 |
ASN1_OID_ID_AES_128_CBC, |
| 1833 |
hx509_crypto_aes128_cbc, |
| 1834 |
EVP_aes_128_cbc, |
| 1835 |
CMSCBCParam_get, |
| 1836 |
CMSCBCParam_set |
| 1837 |
}, |
| 1838 |
{ |
| 1839 |
"aes-192-cbc", |
| 1840 |
0, |
| 1841 |
ASN1_OID_ID_AES_192_CBC, |
| 1842 |
NULL, |
| 1843 |
EVP_aes_192_cbc, |
| 1844 |
CMSCBCParam_get, |
| 1845 |
CMSCBCParam_set |
| 1846 |
}, |
| 1847 |
{ |
| 1848 |
"aes-256-cbc", |
| 1849 |
0, |
| 1850 |
ASN1_OID_ID_AES_256_CBC, |
| 1851 |
hx509_crypto_aes256_cbc, |
| 1852 |
EVP_aes_256_cbc, |
| 1853 |
CMSCBCParam_get, |
| 1854 |
CMSCBCParam_set |
| 1855 |
} |
| 1856 |
}; |
| 1857 |
|
| 1858 |
static const struct hx509cipher * |
| 1859 |
find_cipher_by_oid(const heim_oid *oid) |
| 1860 |
{ |
| 1861 |
size_t i; |
| 1862 |
|
| 1863 |
for (i = 0; i < sizeof(ciphers)/sizeof(ciphers[0]); i++) |
| 1864 |
if (der_heim_oid_cmp(oid, ciphers[i].oid) == 0) |
| 1865 |
return &ciphers[i]; |
| 1866 |
|
| 1867 |
return NULL; |
| 1868 |
} |
| 1869 |
|
| 1870 |
static const struct hx509cipher * |
| 1871 |
find_cipher_by_name(const char *name) |
| 1872 |
{ |
| 1873 |
size_t i; |
| 1874 |
|
| 1875 |
for (i = 0; i < sizeof(ciphers)/sizeof(ciphers[0]); i++) |
| 1876 |
if (strcasecmp(name, ciphers[i].name) == 0) |
| 1877 |
return &ciphers[i]; |
| 1878 |
|
| 1879 |
return NULL; |
| 1880 |
} |
| 1881 |
|
| 1882 |
|
| 1883 |
const heim_oid * |
| 1884 |
hx509_crypto_enctype_by_name(const char *name) |
| 1885 |
{ |
| 1886 |
const struct hx509cipher *cipher; |
| 1887 |
|
| 1888 |
cipher = find_cipher_by_name(name); |
| 1889 |
if (cipher == NULL) |
| 1890 |
return NULL; |
| 1891 |
return cipher->oid; |
| 1892 |
} |
| 1893 |
|
| 1894 |
int |
| 1895 |
hx509_crypto_init(hx509_context context, |
| 1896 |
const char *provider, |
| 1897 |
const heim_oid *enctype, |
| 1898 |
hx509_crypto *crypto) |
| 1899 |
{ |
| 1900 |
const struct hx509cipher *cipher; |
| 1901 |
|
| 1902 |
*crypto = NULL; |
| 1903 |
|
| 1904 |
cipher = find_cipher_by_oid(enctype); |
| 1905 |
if (cipher == NULL) { |
| 1906 |
hx509_set_error_string(context, 0, HX509_ALG_NOT_SUPP, |
| 1907 |
"Algorithm not supported"); |
| 1908 |
return HX509_ALG_NOT_SUPP; |
| 1909 |
} |
| 1910 |
|
| 1911 |
*crypto = calloc(1, sizeof(**crypto)); |
| 1912 |
if (*crypto == NULL) { |
| 1913 |
hx509_clear_error_string(context); |
| 1914 |
return ENOMEM; |
| 1915 |
} |
| 1916 |
|
| 1917 |
(*crypto)->flags = PADDING_PKCS7; |
| 1918 |
(*crypto)->cipher = cipher; |
| 1919 |
(*crypto)->c = (*cipher->evp_func)(); |
| 1920 |
|
| 1921 |
if (der_copy_oid(enctype, &(*crypto)->oid)) { |
| 1922 |
hx509_crypto_destroy(*crypto); |
| 1923 |
*crypto = NULL; |
| 1924 |
hx509_clear_error_string(context); |
| 1925 |
return ENOMEM; |
| 1926 |
} |
| 1927 |
|
| 1928 |
return 0; |
| 1929 |
} |
| 1930 |
|
| 1931 |
const char * |
| 1932 |
hx509_crypto_provider(hx509_crypto crypto) |
| 1933 |
{ |
| 1934 |
return "unknown"; |
| 1935 |
} |
| 1936 |
|
| 1937 |
void |
| 1938 |
hx509_crypto_destroy(hx509_crypto crypto) |
| 1939 |
{ |
| 1940 |
if (crypto->name) |
| 1941 |
free(crypto->name); |
| 1942 |
if (crypto->key.data) |
| 1943 |
free(crypto->key.data); |
| 1944 |
if (crypto->param) |
| 1945 |
free(crypto->param); |
| 1946 |
der_free_oid(&crypto->oid); |
| 1947 |
memset(crypto, 0, sizeof(*crypto)); |
| 1948 |
free(crypto); |
| 1949 |
} |
| 1950 |
|
| 1951 |
int |
| 1952 |
hx509_crypto_set_key_name(hx509_crypto crypto, const char *name) |
| 1953 |
{ |
| 1954 |
return 0; |
| 1955 |
} |
| 1956 |
|
| 1957 |
void |
| 1958 |
hx509_crypto_allow_weak(hx509_crypto crypto) |
| 1959 |
{ |
| 1960 |
crypto->flags |= ALLOW_WEAK; |
| 1961 |
} |
| 1962 |
|
| 1963 |
void |
| 1964 |
hx509_crypto_set_padding(hx509_crypto crypto, int padding_type) |
| 1965 |
{ |
| 1966 |
switch (padding_type) { |
| 1967 |
case HX509_CRYPTO_PADDING_PKCS7: |
| 1968 |
crypto->flags &= ~PADDING_FLAGS; |
| 1969 |
crypto->flags |= PADDING_PKCS7; |
| 1970 |
break; |
| 1971 |
case HX509_CRYPTO_PADDING_NONE: |
| 1972 |
crypto->flags &= ~PADDING_FLAGS; |
| 1973 |
crypto->flags |= PADDING_NONE; |
| 1974 |
break; |
| 1975 |
default: |
| 1976 |
_hx509_abort("Invalid padding"); |
| 1977 |
} |
| 1978 |
} |
| 1979 |
|
| 1980 |
int |
| 1981 |
hx509_crypto_set_key_data(hx509_crypto crypto, const void *data, size_t length) |
| 1982 |
{ |
| 1983 |
if (EVP_CIPHER_key_length(crypto->c) > (int)length) |
| 1984 |
return HX509_CRYPTO_INTERNAL_ERROR; |
| 1985 |
|
| 1986 |
if (crypto->key.data) { |
| 1987 |
free(crypto->key.data); |
| 1988 |
crypto->key.data = NULL; |
| 1989 |
crypto->key.length = 0; |
| 1990 |
} |
| 1991 |
crypto->key.data = malloc(length); |
| 1992 |
if (crypto->key.data == NULL) |
| 1993 |
return ENOMEM; |
| 1994 |
memcpy(crypto->key.data, data, length); |
| 1995 |
crypto->key.length = length; |
| 1996 |
|
| 1997 |
return 0; |
| 1998 |
} |
| 1999 |
|
| 2000 |
int |
| 2001 |
hx509_crypto_set_random_key(hx509_crypto crypto, heim_octet_string *key) |
| 2002 |
{ |
| 2003 |
if (crypto->key.data) { |
| 2004 |
free(crypto->key.data); |
| 2005 |
crypto->key.length = 0; |
| 2006 |
} |
| 2007 |
|
| 2008 |
crypto->key.length = EVP_CIPHER_key_length(crypto->c); |
| 2009 |
crypto->key.data = malloc(crypto->key.length); |
| 2010 |
if (crypto->key.data == NULL) { |
| 2011 |
crypto->key.length = 0; |
| 2012 |
return ENOMEM; |
| 2013 |
} |
| 2014 |
if (RAND_bytes(crypto->key.data, crypto->key.length) <= 0) { |
| 2015 |
free(crypto->key.data); |
| 2016 |
crypto->key.data = NULL; |
| 2017 |
crypto->key.length = 0; |
| 2018 |
return HX509_CRYPTO_INTERNAL_ERROR; |
| 2019 |
} |
| 2020 |
if (key) |
| 2021 |
return der_copy_octet_string(&crypto->key, key); |
| 2022 |
else |
| 2023 |
return 0; |
| 2024 |
} |
| 2025 |
|
| 2026 |
int |
| 2027 |
hx509_crypto_set_params(hx509_context context, |
| 2028 |
hx509_crypto crypto, |
| 2029 |
const heim_octet_string *param, |
| 2030 |
heim_octet_string *ivec) |
| 2031 |
{ |
| 2032 |
return (*crypto->cipher->set_params)(context, param, crypto, ivec); |
| 2033 |
} |
| 2034 |
|
| 2035 |
int |
| 2036 |
hx509_crypto_get_params(hx509_context context, |
| 2037 |
hx509_crypto crypto, |
| 2038 |
const heim_octet_string *ivec, |
| 2039 |
heim_octet_string *param) |
| 2040 |
{ |
| 2041 |
return (*crypto->cipher->get_params)(context, crypto, ivec, param); |
| 2042 |
} |
| 2043 |
|
| 2044 |
int |
| 2045 |
hx509_crypto_random_iv(hx509_crypto crypto, heim_octet_string *ivec) |
| 2046 |
{ |
| 2047 |
ivec->length = EVP_CIPHER_iv_length(crypto->c); |
| 2048 |
ivec->data = malloc(ivec->length); |
| 2049 |
if (ivec->data == NULL) { |
| 2050 |
ivec->length = 0; |
| 2051 |
return ENOMEM; |
| 2052 |
} |
| 2053 |
|
| 2054 |
if (RAND_bytes(ivec->data, ivec->length) <= 0) { |
| 2055 |
free(ivec->data); |
| 2056 |
ivec->data = NULL; |
| 2057 |
ivec->length = 0; |
| 2058 |
return HX509_CRYPTO_INTERNAL_ERROR; |
| 2059 |
} |
| 2060 |
return 0; |
| 2061 |
} |
| 2062 |
|
| 2063 |
int |
| 2064 |
hx509_crypto_encrypt(hx509_crypto crypto, |
| 2065 |
const void *data, |
| 2066 |
const size_t length, |
| 2067 |
const heim_octet_string *ivec, |
| 2068 |
heim_octet_string **ciphertext) |
| 2069 |
{ |
| 2070 |
EVP_CIPHER_CTX evp; |
| 2071 |
size_t padsize, bsize; |
| 2072 |
int ret; |
| 2073 |
|
| 2074 |
*ciphertext = NULL; |
| 2075 |
|
| 2076 |
if ((crypto->cipher->flags & CIPHER_WEAK) && |
| 2077 |
(crypto->flags & ALLOW_WEAK) == 0) |
| 2078 |
return HX509_CRYPTO_ALGORITHM_BEST_BEFORE; |
| 2079 |
|
| 2080 |
assert(EVP_CIPHER_iv_length(crypto->c) == (int)ivec->length); |
| 2081 |
|
| 2082 |
EVP_CIPHER_CTX_init(&evp); |
| 2083 |
|
| 2084 |
ret = EVP_CipherInit_ex(&evp, crypto->c, NULL, |
| 2085 |
crypto->key.data, ivec->data, 1); |
| 2086 |
if (ret != 1) { |
| 2087 |
EVP_CIPHER_CTX_cleanup(&evp); |
| 2088 |
ret = HX509_CRYPTO_INTERNAL_ERROR; |
| 2089 |
goto out; |
| 2090 |
} |
| 2091 |
|
| 2092 |
*ciphertext = calloc(1, sizeof(**ciphertext)); |
| 2093 |
if (*ciphertext == NULL) { |
| 2094 |
ret = ENOMEM; |
| 2095 |
goto out; |
| 2096 |
} |
| 2097 |
|
| 2098 |
assert(crypto->flags & PADDING_FLAGS); |
| 2099 |
|
| 2100 |
bsize = EVP_CIPHER_block_size(crypto->c); |
| 2101 |
padsize = 0; |
| 2102 |
|
| 2103 |
if (crypto->flags & PADDING_NONE) { |
| 2104 |
if (bsize != 1 && (length % bsize) != 0) |
| 2105 |
return HX509_CMS_PADDING_ERROR; |
| 2106 |
} else if (crypto->flags & PADDING_PKCS7) { |
| 2107 |
if (bsize != 1) |
| 2108 |
padsize = bsize - (length % bsize); |
| 2109 |
} |
| 2110 |
|
| 2111 |
(*ciphertext)->length = length + padsize; |
| 2112 |
(*ciphertext)->data = malloc(length + padsize); |
| 2113 |
if ((*ciphertext)->data == NULL) { |
| 2114 |
ret = ENOMEM; |
| 2115 |
goto out; |
| 2116 |
} |
| 2117 |
|
| 2118 |
memcpy((*ciphertext)->data, data, length); |
| 2119 |
if (padsize) { |
| 2120 |
size_t i; |
| 2121 |
unsigned char *p = (*ciphertext)->data; |
| 2122 |
p += length; |
| 2123 |
for (i = 0; i < padsize; i++) |
| 2124 |
*p++ = padsize; |
| 2125 |
} |
| 2126 |
|
| 2127 |
ret = EVP_Cipher(&evp, (*ciphertext)->data, |
| 2128 |
(*ciphertext)->data, |
| 2129 |
length + padsize); |
| 2130 |
if (ret != 1) { |
| 2131 |
ret = HX509_CRYPTO_INTERNAL_ERROR; |
| 2132 |
goto out; |
| 2133 |
} |
| 2134 |
ret = 0; |
| 2135 |
|
| 2136 |
out: |
| 2137 |
if (ret) { |
| 2138 |
if (*ciphertext) { |
| 2139 |
if ((*ciphertext)->data) { |
| 2140 |
free((*ciphertext)->data); |
| 2141 |
} |
| 2142 |
free(*ciphertext); |
| 2143 |
*ciphertext = NULL; |
| 2144 |
} |
| 2145 |
} |
| 2146 |
EVP_CIPHER_CTX_cleanup(&evp); |
| 2147 |
|
| 2148 |
return ret; |
| 2149 |
} |
| 2150 |
|
| 2151 |
int |
| 2152 |
hx509_crypto_decrypt(hx509_crypto crypto, |
| 2153 |
const void *data, |
| 2154 |
const size_t length, |
| 2155 |
heim_octet_string *ivec, |
| 2156 |
heim_octet_string *clear) |
| 2157 |
{ |
| 2158 |
EVP_CIPHER_CTX evp; |
| 2159 |
void *idata = NULL; |
| 2160 |
int ret; |
| 2161 |
|
| 2162 |
clear->data = NULL; |
| 2163 |
clear->length = 0; |
| 2164 |
|
| 2165 |
if ((crypto->cipher->flags & CIPHER_WEAK) && |
| 2166 |
(crypto->flags & ALLOW_WEAK) == 0) |
| 2167 |
return HX509_CRYPTO_ALGORITHM_BEST_BEFORE; |
| 2168 |
|
| 2169 |
if (ivec && EVP_CIPHER_iv_length(crypto->c) < (int)ivec->length) |
| 2170 |
return HX509_CRYPTO_INTERNAL_ERROR; |
| 2171 |
|
| 2172 |
if (crypto->key.data == NULL) |
| 2173 |
return HX509_CRYPTO_INTERNAL_ERROR; |
| 2174 |
|
| 2175 |
if (ivec) |
| 2176 |
idata = ivec->data; |
| 2177 |
|
| 2178 |
EVP_CIPHER_CTX_init(&evp); |
| 2179 |
|
| 2180 |
ret = EVP_CipherInit_ex(&evp, crypto->c, NULL, |
| 2181 |
crypto->key.data, idata, 0); |
| 2182 |
if (ret != 1) { |
| 2183 |
EVP_CIPHER_CTX_cleanup(&evp); |
| 2184 |
return HX509_CRYPTO_INTERNAL_ERROR; |
| 2185 |
} |
| 2186 |
|
| 2187 |
clear->length = length; |
| 2188 |
clear->data = malloc(length); |
| 2189 |
if (clear->data == NULL) { |
| 2190 |
EVP_CIPHER_CTX_cleanup(&evp); |
| 2191 |
clear->length = 0; |
| 2192 |
return ENOMEM; |
| 2193 |
} |
| 2194 |
|
| 2195 |
if (EVP_Cipher(&evp, clear->data, data, length) != 1) { |
| 2196 |
return HX509_CRYPTO_INTERNAL_ERROR; |
| 2197 |
} |
| 2198 |
EVP_CIPHER_CTX_cleanup(&evp); |
| 2199 |
|
| 2200 |
if ((crypto->flags & PADDING_PKCS7) && EVP_CIPHER_block_size(crypto->c) > 1) { |
| 2201 |
int padsize; |
| 2202 |
unsigned char *p; |
| 2203 |
int j, bsize = EVP_CIPHER_block_size(crypto->c); |
| 2204 |
|
| 2205 |
if ((int)clear->length < bsize) { |
| 2206 |
ret = HX509_CMS_PADDING_ERROR; |
| 2207 |
goto out; |
| 2208 |
} |
| 2209 |
|
| 2210 |
p = clear->data; |
| 2211 |
p += clear->length - 1; |
| 2212 |
padsize = *p; |
| 2213 |
if (padsize > bsize) { |
| 2214 |
ret = HX509_CMS_PADDING_ERROR; |
| 2215 |
goto out; |
| 2216 |
} |
| 2217 |
clear->length -= padsize; |
| 2218 |
for (j = 0; j < padsize; j++) { |
| 2219 |
if (*p-- != padsize) { |
| 2220 |
ret = HX509_CMS_PADDING_ERROR; |
| 2221 |
goto out; |
| 2222 |
} |
| 2223 |
} |
| 2224 |
} |
| 2225 |
|
| 2226 |
return 0; |
| 2227 |
|
| 2228 |
out: |
| 2229 |
if (clear->data) |
| 2230 |
free(clear->data); |
| 2231 |
clear->data = NULL; |
| 2232 |
clear->length = 0; |
| 2233 |
return ret; |
| 2234 |
} |
| 2235 |
|
| 2236 |
typedef int (*PBE_string2key_func)(hx509_context, |
| 2237 |
const char *, |
| 2238 |
const heim_octet_string *, |
| 2239 |
hx509_crypto *, heim_octet_string *, |
| 2240 |
heim_octet_string *, |
| 2241 |
const heim_oid *, const EVP_MD *); |
| 2242 |
|
| 2243 |
static int |
| 2244 |
PBE_string2key(hx509_context context, |
| 2245 |
const char *password, |
| 2246 |
const heim_octet_string *parameters, |
| 2247 |
hx509_crypto *crypto, |
| 2248 |
heim_octet_string *key, heim_octet_string *iv, |
| 2249 |
const heim_oid *enc_oid, |
| 2250 |
const EVP_MD *md) |
| 2251 |
{ |
| 2252 |
PKCS12_PBEParams p12params; |
| 2253 |
int passwordlen; |
| 2254 |
hx509_crypto c; |
| 2255 |
int iter, saltlen, ret; |
| 2256 |
unsigned char *salt; |
| 2257 |
|
| 2258 |
passwordlen = password ? strlen(password) : 0; |
| 2259 |
|
| 2260 |
if (parameters == NULL) |
| 2261 |
return HX509_ALG_NOT_SUPP; |
| 2262 |
|
| 2263 |
ret = decode_PKCS12_PBEParams(parameters->data, |
| 2264 |
parameters->length, |
| 2265 |
&p12params, NULL); |
| 2266 |
if (ret) |
| 2267 |
goto out; |
| 2268 |
|
| 2269 |
if (p12params.iterations) |
| 2270 |
iter = *p12params.iterations; |
| 2271 |
else |
| 2272 |
iter = 1; |
| 2273 |
salt = p12params.salt.data; |
| 2274 |
saltlen = p12params.salt.length; |
| 2275 |
|
| 2276 |
if (!PKCS12_key_gen (password, passwordlen, salt, saltlen, |
| 2277 |
PKCS12_KEY_ID, iter, key->length, key->data, md)) { |
| 2278 |
ret = HX509_CRYPTO_INTERNAL_ERROR; |
| 2279 |
goto out; |
| 2280 |
} |
| 2281 |
|
| 2282 |
if (!PKCS12_key_gen (password, passwordlen, salt, saltlen, |
| 2283 |
PKCS12_IV_ID, iter, iv->length, iv->data, md)) { |
| 2284 |
ret = HX509_CRYPTO_INTERNAL_ERROR; |
| 2285 |
goto out; |
| 2286 |
} |
| 2287 |
|
| 2288 |
ret = hx509_crypto_init(context, NULL, enc_oid, &c); |
| 2289 |
if (ret) |
| 2290 |
goto out; |
| 2291 |
|
| 2292 |
hx509_crypto_allow_weak(c); |
| 2293 |
|
| 2294 |
ret = hx509_crypto_set_key_data(c, key->data, key->length); |
| 2295 |
if (ret) { |
| 2296 |
hx509_crypto_destroy(c); |
| 2297 |
goto out; |
| 2298 |
} |
| 2299 |
|
| 2300 |
*crypto = c; |
| 2301 |
out: |
| 2302 |
free_PKCS12_PBEParams(&p12params); |
| 2303 |
return ret; |
| 2304 |
} |
| 2305 |
|
| 2306 |
static const heim_oid * |
| 2307 |
find_string2key(const heim_oid *oid, |
| 2308 |
const EVP_CIPHER **c, |
| 2309 |
const EVP_MD **md, |
| 2310 |
PBE_string2key_func *s2k) |
| 2311 |
{ |
| 2312 |
if (der_heim_oid_cmp(oid, ASN1_OID_ID_PBEWITHSHAAND40BITRC2_CBC) == 0) { |
| 2313 |
*c = EVP_rc2_40_cbc(); |
| 2314 |
if (*c == NULL) |
| 2315 |
return NULL; |
| 2316 |
*md = EVP_sha1(); |
| 2317 |
if (*md == NULL) |
| 2318 |
return NULL; |
| 2319 |
*s2k = PBE_string2key; |
| 2320 |
return &asn1_oid_private_rc2_40; |
| 2321 |
} else if (der_heim_oid_cmp(oid, ASN1_OID_ID_PBEWITHSHAAND128BITRC2_CBC) == 0) { |
| 2322 |
*c = EVP_rc2_cbc(); |
| 2323 |
if (*c == NULL) |
| 2324 |
return NULL; |
| 2325 |
*md = EVP_sha1(); |
| 2326 |
if (*md == NULL) |
| 2327 |
return NULL; |
| 2328 |
*s2k = PBE_string2key; |
| 2329 |
return ASN1_OID_ID_PKCS3_RC2_CBC; |
| 2330 |
#if 0 |
| 2331 |
} else if (der_heim_oid_cmp(oid, ASN1_OID_ID_PBEWITHSHAAND40BITRC4) == 0) { |
| 2332 |
*c = EVP_rc4_40(); |
| 2333 |
if (*c == NULL) |
| 2334 |
return NULL; |
| 2335 |
*md = EVP_sha1(); |
| 2336 |
if (*md == NULL) |
| 2337 |
return NULL; |
| 2338 |
*s2k = PBE_string2key; |
| 2339 |
return NULL; |
| 2340 |
} else if (der_heim_oid_cmp(oid, ASN1_OID_ID_PBEWITHSHAAND128BITRC4) == 0) { |
| 2341 |
*c = EVP_rc4(); |
| 2342 |
if (*c == NULL) |
| 2343 |
return NULL; |
| 2344 |
*md = EVP_sha1(); |
| 2345 |
if (*md == NULL) |
| 2346 |
return NULL; |
| 2347 |
*s2k = PBE_string2key; |
| 2348 |
return ASN1_OID_ID_PKCS3_RC4; |
| 2349 |
#endif |
| 2350 |
} else if (der_heim_oid_cmp(oid, ASN1_OID_ID_PBEWITHSHAAND3_KEYTRIPLEDES_CBC) == 0) { |
| 2351 |
*c = EVP_des_ede3_cbc(); |
| 2352 |
if (*c == NULL) |
| 2353 |
return NULL; |
| 2354 |
*md = EVP_sha1(); |
| 2355 |
if (*md == NULL) |
| 2356 |
return NULL; |
| 2357 |
*s2k = PBE_string2key; |
| 2358 |
return ASN1_OID_ID_PKCS3_DES_EDE3_CBC; |
| 2359 |
} |
| 2360 |
|
| 2361 |
return NULL; |
| 2362 |
} |
| 2363 |
|
| 2364 |
/* |
| 2365 |
* |
| 2366 |
*/ |
| 2367 |
|
| 2368 |
int |
| 2369 |
_hx509_pbe_encrypt(hx509_context context, |
| 2370 |
hx509_lock lock, |
| 2371 |
const AlgorithmIdentifier *ai, |
| 2372 |
const heim_octet_string *content, |
| 2373 |
heim_octet_string *econtent) |
| 2374 |
{ |
| 2375 |
hx509_clear_error_string(context); |
| 2376 |
return EINVAL; |
| 2377 |
} |
| 2378 |
|
| 2379 |
/* |
| 2380 |
* |
| 2381 |
*/ |
| 2382 |
|
| 2383 |
int |
| 2384 |
_hx509_pbe_decrypt(hx509_context context, |
| 2385 |
hx509_lock lock, |
| 2386 |
const AlgorithmIdentifier *ai, |
| 2387 |
const heim_octet_string *econtent, |
| 2388 |
heim_octet_string *content) |
| 2389 |
{ |
| 2390 |
const struct _hx509_password *pw; |
| 2391 |
heim_octet_string key, iv; |
| 2392 |
const heim_oid *enc_oid; |
| 2393 |
const EVP_CIPHER *c; |
| 2394 |
const EVP_MD *md; |
| 2395 |
PBE_string2key_func s2k; |
| 2396 |
int ret = 0; |
| 2397 |
size_t i; |
| 2398 |
|
| 2399 |
memset(&key, 0, sizeof(key)); |
| 2400 |
memset(&iv, 0, sizeof(iv)); |
| 2401 |
|
| 2402 |
memset(content, 0, sizeof(*content)); |
| 2403 |
|
| 2404 |
enc_oid = find_string2key(&ai->algorithm, &c, &md, &s2k); |
| 2405 |
if (enc_oid == NULL) { |
| 2406 |
hx509_set_error_string(context, 0, HX509_ALG_NOT_SUPP, |
| 2407 |
"String to key algorithm not supported"); |
| 2408 |
ret = HX509_ALG_NOT_SUPP; |
| 2409 |
goto out; |
| 2410 |
} |
| 2411 |
|
| 2412 |
key.length = EVP_CIPHER_key_length(c); |
| 2413 |
key.data = malloc(key.length); |
| 2414 |
if (key.data == NULL) { |
| 2415 |
ret = ENOMEM; |
| 2416 |
hx509_clear_error_string(context); |
| 2417 |
goto out; |
| 2418 |
} |
| 2419 |
|
| 2420 |
iv.length = EVP_CIPHER_iv_length(c); |
| 2421 |
iv.data = malloc(iv.length); |
| 2422 |
if (iv.data == NULL) { |
| 2423 |
ret = ENOMEM; |
| 2424 |
hx509_clear_error_string(context); |
| 2425 |
goto out; |
| 2426 |
} |
| 2427 |
|
| 2428 |
pw = _hx509_lock_get_passwords(lock); |
| 2429 |
|
| 2430 |
ret = HX509_CRYPTO_INTERNAL_ERROR; |
| 2431 |
for (i = 0; i < pw->len + 1; i++) { |
| 2432 |
hx509_crypto crypto; |
| 2433 |
const char *password; |
| 2434 |
|
| 2435 |
if (i < pw->len) |
| 2436 |
password = pw->val[i]; |
| 2437 |
else if (i < pw->len + 1) |
| 2438 |
password = ""; |
| 2439 |
else |
| 2440 |
password = NULL; |
| 2441 |
|
| 2442 |
ret = (*s2k)(context, password, ai->parameters, &crypto, |
| 2443 |
&key, &iv, enc_oid, md); |
| 2444 |
if (ret) |
| 2445 |
goto out; |
| 2446 |
|
| 2447 |
ret = hx509_crypto_decrypt(crypto, |
| 2448 |
econtent->data, |
| 2449 |
econtent->length, |
| 2450 |
&iv, |
| 2451 |
content); |
| 2452 |
hx509_crypto_destroy(crypto); |
| 2453 |
if (ret == 0) |
| 2454 |
goto out; |
| 2455 |
|
| 2456 |
} |
| 2457 |
out: |
| 2458 |
if (key.data) |
| 2459 |
der_free_octet_string(&key); |
| 2460 |
if (iv.data) |
| 2461 |
der_free_octet_string(&iv); |
| 2462 |
return ret; |
| 2463 |
} |
| 2464 |
|
| 2465 |
/* |
| 2466 |
* |
| 2467 |
*/ |
| 2468 |
|
| 2469 |
|
| 2470 |
static int |
| 2471 |
match_keys_rsa(hx509_cert c, hx509_private_key private_key) |
| 2472 |
{ |
| 2473 |
const Certificate *cert; |
| 2474 |
const SubjectPublicKeyInfo *spi; |
| 2475 |
RSAPublicKey pk; |
| 2476 |
RSA *rsa; |
| 2477 |
size_t size; |
| 2478 |
int ret; |
| 2479 |
|
| 2480 |
if (private_key->private_key.rsa == NULL) |
| 2481 |
return 0; |
| 2482 |
|
| 2483 |
rsa = private_key->private_key.rsa; |
| 2484 |
if (rsa->d == NULL || rsa->p == NULL || rsa->q == NULL) |
| 2485 |
return 0; |
| 2486 |
|
| 2487 |
cert = _hx509_get_cert(c); |
| 2488 |
spi = &cert->tbsCertificate.subjectPublicKeyInfo; |
| 2489 |
|
| 2490 |
rsa = RSA_new(); |
| 2491 |
if (rsa == NULL) |
| 2492 |
return 0; |
| 2493 |
|
| 2494 |
ret = decode_RSAPublicKey(spi->subjectPublicKey.data, |
| 2495 |
spi->subjectPublicKey.length / 8, |
| 2496 |
&pk, &size); |
| 2497 |
if (ret) { |
| 2498 |
RSA_free(rsa); |
| 2499 |
return 0; |
| 2500 |
} |
| 2501 |
rsa->n = heim_int2BN(&pk.modulus); |
| 2502 |
rsa->e = heim_int2BN(&pk.publicExponent); |
| 2503 |
|
| 2504 |
free_RSAPublicKey(&pk); |
| 2505 |
|
| 2506 |
rsa->d = BN_dup(private_key->private_key.rsa->d); |
| 2507 |
rsa->p = BN_dup(private_key->private_key.rsa->p); |
| 2508 |
rsa->q = BN_dup(private_key->private_key.rsa->q); |
| 2509 |
rsa->dmp1 = BN_dup(private_key->private_key.rsa->dmp1); |
| 2510 |
rsa->dmq1 = BN_dup(private_key->private_key.rsa->dmq1); |
| 2511 |
rsa->iqmp = BN_dup(private_key->private_key.rsa->iqmp); |
| 2512 |
|
| 2513 |
if (rsa->n == NULL || rsa->e == NULL || |
| 2514 |
rsa->d == NULL || rsa->p == NULL|| rsa->q == NULL || |
| 2515 |
rsa->dmp1 == NULL || rsa->dmq1 == NULL) { |
| 2516 |
RSA_free(rsa); |
| 2517 |
return 0; |
| 2518 |
} |
| 2519 |
|
| 2520 |
ret = RSA_check_key(rsa); |
| 2521 |
RSA_free(rsa); |
| 2522 |
|
| 2523 |
return ret == 1; |
| 2524 |
} |
| 2525 |
|
| 2526 |
static int |
| 2527 |
match_keys_ec(hx509_cert c, hx509_private_key private_key) |
| 2528 |
{ |
| 2529 |
return 1; /* XXX use EC_KEY_check_key */ |
| 2530 |
} |
| 2531 |
|
| 2532 |
|
| 2533 |
int |
| 2534 |
_hx509_match_keys(hx509_cert c, hx509_private_key key) |
| 2535 |
{ |
| 2536 |
if (!key->ops) |
| 2537 |
return 0; |
| 2538 |
if (der_heim_oid_cmp(key->ops->key_oid, ASN1_OID_ID_PKCS1_RSAENCRYPTION) == 0) |
| 2539 |
return match_keys_rsa(c, key); |
| 2540 |
if (der_heim_oid_cmp(key->ops->key_oid, ASN1_OID_ID_ECPUBLICKEY) == 0) |
| 2541 |
return match_keys_ec(c, key); |
| 2542 |
return 0; |
| 2543 |
|
| 2544 |
} |
| 2545 |
|
| 2546 |
|
| 2547 |
static const heim_oid * |
| 2548 |
find_keytype(const hx509_private_key key) |
| 2549 |
{ |
| 2550 |
const struct signature_alg *md; |
| 2551 |
|
| 2552 |
if (key == NULL) |
| 2553 |
return NULL; |
| 2554 |
|
| 2555 |
md = _hx509_find_sig_alg(key->signature_alg); |
| 2556 |
if (md == NULL) |
| 2557 |
return NULL; |
| 2558 |
return md->key_oid; |
| 2559 |
} |
| 2560 |
|
| 2561 |
int |
| 2562 |
hx509_crypto_select(const hx509_context context, |
| 2563 |
int type, |
| 2564 |
const hx509_private_key source, |
| 2565 |
hx509_peer_info peer, |
| 2566 |
AlgorithmIdentifier *selected) |
| 2567 |
{ |
| 2568 |
const AlgorithmIdentifier *def = NULL; |
| 2569 |
size_t i, j; |
| 2570 |
int ret, bits; |
| 2571 |
|
| 2572 |
memset(selected, 0, sizeof(*selected)); |
| 2573 |
|
| 2574 |
if (type == HX509_SELECT_DIGEST) { |
| 2575 |
bits = SIG_DIGEST; |
| 2576 |
if (source) |
| 2577 |
def = alg_for_privatekey(source, type); |
| 2578 |
if (def == NULL) |
| 2579 |
def = _hx509_crypto_default_digest_alg; |
| 2580 |
} else if (type == HX509_SELECT_PUBLIC_SIG) { |
| 2581 |
bits = SIG_PUBLIC_SIG; |
| 2582 |
/* XXX depend on `source´ and `peer´ */ |
| 2583 |
if (source) |
| 2584 |
def = alg_for_privatekey(source, type); |
| 2585 |
if (def == NULL) |
| 2586 |
def = _hx509_crypto_default_sig_alg; |
| 2587 |
} else if (type == HX509_SELECT_SECRET_ENC) { |
| 2588 |
bits = SIG_SECRET; |
| 2589 |
def = _hx509_crypto_default_secret_alg; |
| 2590 |
} else { |
| 2591 |
hx509_set_error_string(context, 0, EINVAL, |
| 2592 |
"Unknown type %d of selection", type); |
| 2593 |
return EINVAL; |
| 2594 |
} |
| 2595 |
|
| 2596 |
if (peer) { |
| 2597 |
const heim_oid *keytype = NULL; |
| 2598 |
|
| 2599 |
keytype = find_keytype(source); |
| 2600 |
|
| 2601 |
for (i = 0; i < peer->len; i++) { |
| 2602 |
for (j = 0; sig_algs[j]; j++) { |
| 2603 |
if ((sig_algs[j]->flags & bits) != bits) |
| 2604 |
continue; |
| 2605 |
if (der_heim_oid_cmp(sig_algs[j]->sig_oid, |
| 2606 |
&peer->val[i].algorithm) != 0) |
| 2607 |
continue; |
| 2608 |
if (keytype && sig_algs[j]->key_oid && |
| 2609 |
der_heim_oid_cmp(keytype, sig_algs[j]->key_oid)) |
| 2610 |
continue; |
| 2611 |
|
| 2612 |
/* found one, use that */ |
| 2613 |
ret = copy_AlgorithmIdentifier(&peer->val[i], selected); |
| 2614 |
if (ret) |
| 2615 |
hx509_clear_error_string(context); |
| 2616 |
return ret; |
| 2617 |
} |
| 2618 |
if (bits & SIG_SECRET) { |
| 2619 |
const struct hx509cipher *cipher; |
| 2620 |
|
| 2621 |
cipher = find_cipher_by_oid(&peer->val[i].algorithm); |
| 2622 |
if (cipher == NULL) |
| 2623 |
continue; |
| 2624 |
if (cipher->ai_func == NULL) |
| 2625 |
continue; |
| 2626 |
ret = copy_AlgorithmIdentifier(cipher->ai_func(), selected); |
| 2627 |
if (ret) |
| 2628 |
hx509_clear_error_string(context); |
| 2629 |
return ret; |
| 2630 |
} |
| 2631 |
} |
| 2632 |
} |
| 2633 |
|
| 2634 |
/* use default */ |
| 2635 |
ret = copy_AlgorithmIdentifier(def, selected); |
| 2636 |
if (ret) |
| 2637 |
hx509_clear_error_string(context); |
| 2638 |
return ret; |
| 2639 |
} |
| 2640 |
|
| 2641 |
int |
| 2642 |
hx509_crypto_available(hx509_context context, |
| 2643 |
int type, |
| 2644 |
hx509_cert source, |
| 2645 |
AlgorithmIdentifier **val, |
| 2646 |
unsigned int *plen) |
| 2647 |
{ |
| 2648 |
const heim_oid *keytype = NULL; |
| 2649 |
unsigned int len, i; |
| 2650 |
void *ptr; |
| 2651 |
int bits, ret; |
| 2652 |
|
| 2653 |
*val = NULL; |
| 2654 |
|
| 2655 |
if (type == HX509_SELECT_ALL) { |
| 2656 |
bits = SIG_DIGEST | SIG_PUBLIC_SIG | SIG_SECRET; |
| 2657 |
} else if (type == HX509_SELECT_DIGEST) { |
| 2658 |
bits = SIG_DIGEST; |
| 2659 |
} else if (type == HX509_SELECT_PUBLIC_SIG) { |
| 2660 |
bits = SIG_PUBLIC_SIG; |
| 2661 |
} else { |
| 2662 |
hx509_set_error_string(context, 0, EINVAL, |
| 2663 |
"Unknown type %d of available", type); |
| 2664 |
return EINVAL; |
| 2665 |
} |
| 2666 |
|
| 2667 |
if (source) |
| 2668 |
keytype = find_keytype(_hx509_cert_private_key(source)); |
| 2669 |
|
| 2670 |
len = 0; |
| 2671 |
for (i = 0; sig_algs[i]; i++) { |
| 2672 |
if ((sig_algs[i]->flags & bits) == 0) |
| 2673 |
continue; |
| 2674 |
if (sig_algs[i]->sig_alg == NULL) |
| 2675 |
continue; |
| 2676 |
if (keytype && sig_algs[i]->key_oid && |
| 2677 |
der_heim_oid_cmp(sig_algs[i]->key_oid, keytype)) |
| 2678 |
continue; |
| 2679 |
|
| 2680 |
/* found one, add that to the list */ |
| 2681 |
ptr = realloc(*val, sizeof(**val) * (len + 1)); |
| 2682 |
if (ptr == NULL) |
| 2683 |
goto out; |
| 2684 |
*val = ptr; |
| 2685 |
|
| 2686 |
ret = copy_AlgorithmIdentifier(sig_algs[i]->sig_alg, &(*val)[len]); |
| 2687 |
if (ret) |
| 2688 |
goto out; |
| 2689 |
len++; |
| 2690 |
} |
| 2691 |
|
| 2692 |
/* Add AES */ |
| 2693 |
if (bits & SIG_SECRET) { |
| 2694 |
|
| 2695 |
for (i = 0; i < sizeof(ciphers)/sizeof(ciphers[0]); i++) { |
| 2696 |
|
| 2697 |
if (ciphers[i].flags & CIPHER_WEAK) |
| 2698 |
continue; |
| 2699 |
if (ciphers[i].ai_func == NULL) |
| 2700 |
continue; |
| 2701 |
|
| 2702 |
ptr = realloc(*val, sizeof(**val) * (len + 1)); |
| 2703 |
if (ptr == NULL) |
| 2704 |
goto out; |
| 2705 |
*val = ptr; |
| 2706 |
|
| 2707 |
ret = copy_AlgorithmIdentifier((ciphers[i].ai_func)(), &(*val)[len]); |
| 2708 |
if (ret) |
| 2709 |
goto out; |
| 2710 |
len++; |
| 2711 |
} |
| 2712 |
} |
| 2713 |
|
| 2714 |
*plen = len; |
| 2715 |
return 0; |
| 2716 |
|
| 2717 |
out: |
| 2718 |
for (i = 0; i < len; i++) |
| 2719 |
free_AlgorithmIdentifier(&(*val)[i]); |
| 2720 |
free(*val); |
| 2721 |
*val = NULL; |
| 2722 |
hx509_set_error_string(context, 0, ENOMEM, "out of memory"); |
| 2723 |
return ENOMEM; |
| 2724 |
} |
| 2725 |
|
| 2726 |
void |
| 2727 |
hx509_crypto_free_algs(AlgorithmIdentifier *val, |
| 2728 |
unsigned int len) |
| 2729 |
{ |
| 2730 |
unsigned int i; |
| 2731 |
for (i = 0; i < len; i++) |
| 2732 |
free_AlgorithmIdentifier(&val[i]); |
| 2733 |
free(val); |
| 2734 |
} |