| 1 |
/* |
| 2 |
* Copyright (c) 2003 - 2007 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 |
* @page page_cms CMS/PKCS7 message functions. |
| 38 |
* |
| 39 |
* CMS is defined in RFC 3369 and is an continuation of the RSA Labs |
| 40 |
* standard PKCS7. The basic messages in CMS is |
| 41 |
* |
| 42 |
* - SignedData |
| 43 |
* Data signed with private key (RSA, DSA, ECDSA) or secret |
| 44 |
* (symmetric) key |
| 45 |
* - EnvelopedData |
| 46 |
* Data encrypted with private key (RSA) |
| 47 |
* - EncryptedData |
| 48 |
* Data encrypted with secret (symmetric) key. |
| 49 |
* - ContentInfo |
| 50 |
* Wrapper structure including type and data. |
| 51 |
* |
| 52 |
* |
| 53 |
* See the library functions here: @ref hx509_cms |
| 54 |
*/ |
| 55 |
|
| 56 |
#define ALLOC(X, N) (X) = calloc((N), sizeof(*(X))) |
| 57 |
#define ALLOC_SEQ(X, N) do { (X)->len = (N); ALLOC((X)->val, (N)); } while(0) |
| 58 |
|
| 59 |
/** |
| 60 |
* Wrap data and oid in a ContentInfo and encode it. |
| 61 |
* |
| 62 |
* @param oid type of the content. |
| 63 |
* @param buf data to be wrapped. If a NULL pointer is passed in, the |
| 64 |
* optional content field in the ContentInfo is not going be filled |
| 65 |
* in. |
| 66 |
* @param res the encoded buffer, the result should be freed with |
| 67 |
* der_free_octet_string(). |
| 68 |
* |
| 69 |
* @return Returns an hx509 error code. |
| 70 |
* |
| 71 |
* @ingroup hx509_cms |
| 72 |
*/ |
| 73 |
|
| 74 |
int |
| 75 |
hx509_cms_wrap_ContentInfo(const heim_oid *oid, |
| 76 |
const heim_octet_string *buf, |
| 77 |
heim_octet_string *res) |
| 78 |
{ |
| 79 |
ContentInfo ci; |
| 80 |
size_t size; |
| 81 |
int ret; |
| 82 |
|
| 83 |
memset(res, 0, sizeof(*res)); |
| 84 |
memset(&ci, 0, sizeof(ci)); |
| 85 |
|
| 86 |
ret = der_copy_oid(oid, &ci.contentType); |
| 87 |
if (ret) |
| 88 |
return ret; |
| 89 |
if (buf) { |
| 90 |
ALLOC(ci.content, 1); |
| 91 |
if (ci.content == NULL) { |
| 92 |
free_ContentInfo(&ci); |
| 93 |
return ENOMEM; |
| 94 |
} |
| 95 |
ci.content->data = malloc(buf->length); |
| 96 |
if (ci.content->data == NULL) { |
| 97 |
free_ContentInfo(&ci); |
| 98 |
return ENOMEM; |
| 99 |
} |
| 100 |
memcpy(ci.content->data, buf->data, buf->length); |
| 101 |
ci.content->length = buf->length; |
| 102 |
} |
| 103 |
|
| 104 |
ASN1_MALLOC_ENCODE(ContentInfo, res->data, res->length, &ci, &size, ret); |
| 105 |
free_ContentInfo(&ci); |
| 106 |
if (ret) |
| 107 |
return ret; |
| 108 |
if (res->length != size) |
| 109 |
_hx509_abort("internal ASN.1 encoder error"); |
| 110 |
|
| 111 |
return 0; |
| 112 |
} |
| 113 |
|
| 114 |
/** |
| 115 |
* Decode an ContentInfo and unwrap data and oid it. |
| 116 |
* |
| 117 |
* @param in the encoded buffer. |
| 118 |
* @param oid type of the content. |
| 119 |
* @param out data to be wrapped. |
| 120 |
* @param have_data since the data is optional, this flags show dthe |
| 121 |
* diffrence between no data and the zero length data. |
| 122 |
* |
| 123 |
* @return Returns an hx509 error code. |
| 124 |
* |
| 125 |
* @ingroup hx509_cms |
| 126 |
*/ |
| 127 |
|
| 128 |
int |
| 129 |
hx509_cms_unwrap_ContentInfo(const heim_octet_string *in, |
| 130 |
heim_oid *oid, |
| 131 |
heim_octet_string *out, |
| 132 |
int *have_data) |
| 133 |
{ |
| 134 |
ContentInfo ci; |
| 135 |
size_t size; |
| 136 |
int ret; |
| 137 |
|
| 138 |
memset(oid, 0, sizeof(*oid)); |
| 139 |
memset(out, 0, sizeof(*out)); |
| 140 |
|
| 141 |
ret = decode_ContentInfo(in->data, in->length, &ci, &size); |
| 142 |
if (ret) |
| 143 |
return ret; |
| 144 |
|
| 145 |
ret = der_copy_oid(&ci.contentType, oid); |
| 146 |
if (ret) { |
| 147 |
free_ContentInfo(&ci); |
| 148 |
return ret; |
| 149 |
} |
| 150 |
if (ci.content) { |
| 151 |
ret = der_copy_octet_string(ci.content, out); |
| 152 |
if (ret) { |
| 153 |
der_free_oid(oid); |
| 154 |
free_ContentInfo(&ci); |
| 155 |
return ret; |
| 156 |
} |
| 157 |
} else |
| 158 |
memset(out, 0, sizeof(*out)); |
| 159 |
|
| 160 |
if (have_data) |
| 161 |
*have_data = (ci.content != NULL) ? 1 : 0; |
| 162 |
|
| 163 |
free_ContentInfo(&ci); |
| 164 |
|
| 165 |
return 0; |
| 166 |
} |
| 167 |
|
| 168 |
#define CMS_ID_SKI 0 |
| 169 |
#define CMS_ID_NAME 1 |
| 170 |
|
| 171 |
static int |
| 172 |
fill_CMSIdentifier(const hx509_cert cert, |
| 173 |
int type, |
| 174 |
CMSIdentifier *id) |
| 175 |
{ |
| 176 |
int ret; |
| 177 |
|
| 178 |
switch (type) { |
| 179 |
case CMS_ID_SKI: |
| 180 |
id->element = choice_CMSIdentifier_subjectKeyIdentifier; |
| 181 |
ret = _hx509_find_extension_subject_key_id(_hx509_get_cert(cert), |
| 182 |
&id->u.subjectKeyIdentifier); |
| 183 |
if (ret == 0) |
| 184 |
break; |
| 185 |
/* FALLTHROUGH */ |
| 186 |
case CMS_ID_NAME: { |
| 187 |
hx509_name name; |
| 188 |
|
| 189 |
id->element = choice_CMSIdentifier_issuerAndSerialNumber; |
| 190 |
ret = hx509_cert_get_issuer(cert, &name); |
| 191 |
if (ret) |
| 192 |
return ret; |
| 193 |
ret = hx509_name_to_Name(name, &id->u.issuerAndSerialNumber.issuer); |
| 194 |
hx509_name_free(&name); |
| 195 |
if (ret) |
| 196 |
return ret; |
| 197 |
|
| 198 |
ret = hx509_cert_get_serialnumber(cert, &id->u.issuerAndSerialNumber.serialNumber); |
| 199 |
break; |
| 200 |
} |
| 201 |
default: |
| 202 |
_hx509_abort("CMS fill identifier with unknown type"); |
| 203 |
} |
| 204 |
return ret; |
| 205 |
} |
| 206 |
|
| 207 |
static int |
| 208 |
unparse_CMSIdentifier(hx509_context context, |
| 209 |
CMSIdentifier *id, |
| 210 |
char **str) |
| 211 |
{ |
| 212 |
int ret = -1; |
| 213 |
|
| 214 |
*str = NULL; |
| 215 |
switch (id->element) { |
| 216 |
case choice_CMSIdentifier_issuerAndSerialNumber: { |
| 217 |
IssuerAndSerialNumber *iasn; |
| 218 |
char *serial, *name; |
| 219 |
|
| 220 |
iasn = &id->u.issuerAndSerialNumber; |
| 221 |
|
| 222 |
ret = _hx509_Name_to_string(&iasn->issuer, &name); |
| 223 |
if(ret) |
| 224 |
return ret; |
| 225 |
ret = der_print_hex_heim_integer(&iasn->serialNumber, &serial); |
| 226 |
if (ret) { |
| 227 |
free(name); |
| 228 |
return ret; |
| 229 |
} |
| 230 |
ret = asprintf(str, "certificate issued by %s with serial number %s", |
| 231 |
name, serial); |
| 232 |
free(name); |
| 233 |
free(serial); |
| 234 |
break; |
| 235 |
} |
| 236 |
case choice_CMSIdentifier_subjectKeyIdentifier: { |
| 237 |
KeyIdentifier *ki = &id->u.subjectKeyIdentifier; |
| 238 |
char *keyid; |
| 239 |
ssize_t len; |
| 240 |
|
| 241 |
len = hex_encode(ki->data, ki->length, &keyid); |
| 242 |
if (len < 0) |
| 243 |
return ENOMEM; |
| 244 |
|
| 245 |
ret = asprintf(str, "certificate with id %s", keyid); |
| 246 |
free(keyid); |
| 247 |
break; |
| 248 |
} |
| 249 |
default: |
| 250 |
ret = asprintf(str, "certificate have unknown CMSidentifier type"); |
| 251 |
break; |
| 252 |
} |
| 253 |
/* |
| 254 |
* In the following if, we check ret and *str which should be returned/set |
| 255 |
* by asprintf(3) in every branch of the switch statement. |
| 256 |
*/ |
| 257 |
if (ret == -1 || *str == NULL) |
| 258 |
return ENOMEM; |
| 259 |
return 0; |
| 260 |
} |
| 261 |
|
| 262 |
static int |
| 263 |
find_CMSIdentifier(hx509_context context, |
| 264 |
CMSIdentifier *client, |
| 265 |
hx509_certs certs, |
| 266 |
time_t time_now, |
| 267 |
hx509_cert *signer_cert, |
| 268 |
int match) |
| 269 |
{ |
| 270 |
hx509_query q; |
| 271 |
hx509_cert cert; |
| 272 |
Certificate c; |
| 273 |
int ret; |
| 274 |
|
| 275 |
memset(&c, 0, sizeof(c)); |
| 276 |
_hx509_query_clear(&q); |
| 277 |
|
| 278 |
*signer_cert = NULL; |
| 279 |
|
| 280 |
switch (client->element) { |
| 281 |
case choice_CMSIdentifier_issuerAndSerialNumber: |
| 282 |
q.serial = &client->u.issuerAndSerialNumber.serialNumber; |
| 283 |
q.issuer_name = &client->u.issuerAndSerialNumber.issuer; |
| 284 |
q.match = HX509_QUERY_MATCH_SERIALNUMBER|HX509_QUERY_MATCH_ISSUER_NAME; |
| 285 |
break; |
| 286 |
case choice_CMSIdentifier_subjectKeyIdentifier: |
| 287 |
q.subject_id = &client->u.subjectKeyIdentifier; |
| 288 |
q.match = HX509_QUERY_MATCH_SUBJECT_KEY_ID; |
| 289 |
break; |
| 290 |
default: |
| 291 |
hx509_set_error_string(context, 0, HX509_CMS_NO_RECIPIENT_CERTIFICATE, |
| 292 |
"unknown CMS identifier element"); |
| 293 |
return HX509_CMS_NO_RECIPIENT_CERTIFICATE; |
| 294 |
} |
| 295 |
|
| 296 |
q.match |= match; |
| 297 |
|
| 298 |
q.match |= HX509_QUERY_MATCH_TIME; |
| 299 |
if (time_now) |
| 300 |
q.timenow = time_now; |
| 301 |
else |
| 302 |
q.timenow = time(NULL); |
| 303 |
|
| 304 |
ret = hx509_certs_find(context, certs, &q, &cert); |
| 305 |
if (ret == HX509_CERT_NOT_FOUND) { |
| 306 |
char *str; |
| 307 |
|
| 308 |
ret = unparse_CMSIdentifier(context, client, &str); |
| 309 |
if (ret == 0) { |
| 310 |
hx509_set_error_string(context, 0, |
| 311 |
HX509_CMS_NO_RECIPIENT_CERTIFICATE, |
| 312 |
"Failed to find %s", str); |
| 313 |
} else |
| 314 |
hx509_clear_error_string(context); |
| 315 |
return HX509_CMS_NO_RECIPIENT_CERTIFICATE; |
| 316 |
} else if (ret) { |
| 317 |
hx509_set_error_string(context, HX509_ERROR_APPEND, |
| 318 |
HX509_CMS_NO_RECIPIENT_CERTIFICATE, |
| 319 |
"Failed to find CMS id in cert store"); |
| 320 |
return HX509_CMS_NO_RECIPIENT_CERTIFICATE; |
| 321 |
} |
| 322 |
|
| 323 |
*signer_cert = cert; |
| 324 |
|
| 325 |
return 0; |
| 326 |
} |
| 327 |
|
| 328 |
/** |
| 329 |
* Decode and unencrypt EnvelopedData. |
| 330 |
* |
| 331 |
* Extract data and parameteres from from the EnvelopedData. Also |
| 332 |
* supports using detached EnvelopedData. |
| 333 |
* |
| 334 |
* @param context A hx509 context. |
| 335 |
* @param certs Certificate that can decrypt the EnvelopedData |
| 336 |
* encryption key. |
| 337 |
* @param flags HX509_CMS_UE flags to control the behavior. |
| 338 |
* @param data pointer the structure the contains the DER/BER encoded |
| 339 |
* EnvelopedData stucture. |
| 340 |
* @param length length of the data that data point to. |
| 341 |
* @param encryptedContent in case of detached signature, this |
| 342 |
* contains the actual encrypted data, othersize its should be NULL. |
| 343 |
* @param time_now set the current time, if zero the library uses now as the date. |
| 344 |
* @param contentType output type oid, should be freed with der_free_oid(). |
| 345 |
* @param content the data, free with der_free_octet_string(). |
| 346 |
* |
| 347 |
* @return an hx509 error code. |
| 348 |
* |
| 349 |
* @ingroup hx509_cms |
| 350 |
*/ |
| 351 |
|
| 352 |
int |
| 353 |
hx509_cms_unenvelope(hx509_context context, |
| 354 |
hx509_certs certs, |
| 355 |
int flags, |
| 356 |
const void *data, |
| 357 |
size_t length, |
| 358 |
const heim_octet_string *encryptedContent, |
| 359 |
time_t time_now, |
| 360 |
heim_oid *contentType, |
| 361 |
heim_octet_string *content) |
| 362 |
{ |
| 363 |
heim_octet_string key; |
| 364 |
EnvelopedData ed; |
| 365 |
hx509_cert cert; |
| 366 |
AlgorithmIdentifier *ai; |
| 367 |
const heim_octet_string *enccontent; |
| 368 |
heim_octet_string *params, params_data; |
| 369 |
heim_octet_string ivec; |
| 370 |
size_t size; |
| 371 |
int ret, matched = 0, findflags = 0; |
| 372 |
size_t i; |
| 373 |
|
| 374 |
|
| 375 |
memset(&key, 0, sizeof(key)); |
| 376 |
memset(&ed, 0, sizeof(ed)); |
| 377 |
memset(&ivec, 0, sizeof(ivec)); |
| 378 |
memset(content, 0, sizeof(*content)); |
| 379 |
memset(contentType, 0, sizeof(*contentType)); |
| 380 |
|
| 381 |
if ((flags & HX509_CMS_UE_DONT_REQUIRE_KU_ENCIPHERMENT) == 0) |
| 382 |
findflags |= HX509_QUERY_KU_ENCIPHERMENT; |
| 383 |
|
| 384 |
ret = decode_EnvelopedData(data, length, &ed, &size); |
| 385 |
if (ret) { |
| 386 |
hx509_set_error_string(context, 0, ret, |
| 387 |
"Failed to decode EnvelopedData"); |
| 388 |
return ret; |
| 389 |
} |
| 390 |
|
| 391 |
if (ed.recipientInfos.len == 0) { |
| 392 |
ret = HX509_CMS_NO_RECIPIENT_CERTIFICATE; |
| 393 |
hx509_set_error_string(context, 0, ret, |
| 394 |
"No recipient info in enveloped data"); |
| 395 |
goto out; |
| 396 |
} |
| 397 |
|
| 398 |
enccontent = ed.encryptedContentInfo.encryptedContent; |
| 399 |
if (enccontent == NULL) { |
| 400 |
if (encryptedContent == NULL) { |
| 401 |
ret = HX509_CMS_NO_DATA_AVAILABLE; |
| 402 |
hx509_set_error_string(context, 0, ret, |
| 403 |
"Content missing from encrypted data"); |
| 404 |
goto out; |
| 405 |
} |
| 406 |
enccontent = encryptedContent; |
| 407 |
} else if (encryptedContent != NULL) { |
| 408 |
ret = HX509_CMS_NO_DATA_AVAILABLE; |
| 409 |
hx509_set_error_string(context, 0, ret, |
| 410 |
"Both internal and external encrypted data"); |
| 411 |
goto out; |
| 412 |
} |
| 413 |
|
| 414 |
cert = NULL; |
| 415 |
for (i = 0; i < ed.recipientInfos.len; i++) { |
| 416 |
KeyTransRecipientInfo *ri; |
| 417 |
char *str; |
| 418 |
int ret2; |
| 419 |
|
| 420 |
ri = &ed.recipientInfos.val[i]; |
| 421 |
|
| 422 |
ret = find_CMSIdentifier(context, &ri->rid, certs, |
| 423 |
time_now, &cert, |
| 424 |
HX509_QUERY_PRIVATE_KEY|findflags); |
| 425 |
if (ret) |
| 426 |
continue; |
| 427 |
|
| 428 |
matched = 1; /* found a matching certificate, let decrypt */ |
| 429 |
|
| 430 |
ret = _hx509_cert_private_decrypt(context, |
| 431 |
&ri->encryptedKey, |
| 432 |
&ri->keyEncryptionAlgorithm.algorithm, |
| 433 |
cert, &key); |
| 434 |
|
| 435 |
hx509_cert_free(cert); |
| 436 |
if (ret == 0) |
| 437 |
break; /* succuessfully decrypted cert */ |
| 438 |
cert = NULL; |
| 439 |
ret2 = unparse_CMSIdentifier(context, &ri->rid, &str); |
| 440 |
if (ret2 == 0) { |
| 441 |
hx509_set_error_string(context, HX509_ERROR_APPEND, ret, |
| 442 |
"Failed to decrypt with %s", str); |
| 443 |
free(str); |
| 444 |
} |
| 445 |
} |
| 446 |
|
| 447 |
if (!matched) { |
| 448 |
ret = HX509_CMS_NO_RECIPIENT_CERTIFICATE; |
| 449 |
hx509_set_error_string(context, 0, ret, |
| 450 |
"No private key matched any certificate"); |
| 451 |
goto out; |
| 452 |
} |
| 453 |
|
| 454 |
if (cert == NULL) { |
| 455 |
ret = HX509_CMS_NO_RECIPIENT_CERTIFICATE; |
| 456 |
hx509_set_error_string(context, HX509_ERROR_APPEND, ret, |
| 457 |
"No private key decrypted the transfer key"); |
| 458 |
goto out; |
| 459 |
} |
| 460 |
|
| 461 |
ret = der_copy_oid(&ed.encryptedContentInfo.contentType, contentType); |
| 462 |
if (ret) { |
| 463 |
hx509_set_error_string(context, 0, ret, |
| 464 |
"Failed to copy EnvelopedData content oid"); |
| 465 |
goto out; |
| 466 |
} |
| 467 |
|
| 468 |
ai = &ed.encryptedContentInfo.contentEncryptionAlgorithm; |
| 469 |
if (ai->parameters) { |
| 470 |
params_data.data = ai->parameters->data; |
| 471 |
params_data.length = ai->parameters->length; |
| 472 |
params = ¶ms_data; |
| 473 |
} else |
| 474 |
params = NULL; |
| 475 |
|
| 476 |
{ |
| 477 |
hx509_crypto crypto; |
| 478 |
|
| 479 |
ret = hx509_crypto_init(context, NULL, &ai->algorithm, &crypto); |
| 480 |
if (ret) |
| 481 |
goto out; |
| 482 |
|
| 483 |
if (flags & HX509_CMS_UE_ALLOW_WEAK) |
| 484 |
hx509_crypto_allow_weak(crypto); |
| 485 |
|
| 486 |
if (params) { |
| 487 |
ret = hx509_crypto_set_params(context, crypto, params, &ivec); |
| 488 |
if (ret) { |
| 489 |
hx509_crypto_destroy(crypto); |
| 490 |
goto out; |
| 491 |
} |
| 492 |
} |
| 493 |
|
| 494 |
ret = hx509_crypto_set_key_data(crypto, key.data, key.length); |
| 495 |
if (ret) { |
| 496 |
hx509_crypto_destroy(crypto); |
| 497 |
hx509_set_error_string(context, 0, ret, |
| 498 |
"Failed to set key for decryption " |
| 499 |
"of EnvelopedData"); |
| 500 |
goto out; |
| 501 |
} |
| 502 |
|
| 503 |
ret = hx509_crypto_decrypt(crypto, |
| 504 |
enccontent->data, |
| 505 |
enccontent->length, |
| 506 |
ivec.length ? &ivec : NULL, |
| 507 |
content); |
| 508 |
hx509_crypto_destroy(crypto); |
| 509 |
if (ret) { |
| 510 |
hx509_set_error_string(context, 0, ret, |
| 511 |
"Failed to decrypt EnvelopedData"); |
| 512 |
goto out; |
| 513 |
} |
| 514 |
} |
| 515 |
|
| 516 |
out: |
| 517 |
|
| 518 |
free_EnvelopedData(&ed); |
| 519 |
der_free_octet_string(&key); |
| 520 |
if (ivec.length) |
| 521 |
der_free_octet_string(&ivec); |
| 522 |
if (ret) { |
| 523 |
der_free_oid(contentType); |
| 524 |
der_free_octet_string(content); |
| 525 |
} |
| 526 |
|
| 527 |
return ret; |
| 528 |
} |
| 529 |
|
| 530 |
/** |
| 531 |
* Encrypt end encode EnvelopedData. |
| 532 |
* |
| 533 |
* Encrypt and encode EnvelopedData. The data is encrypted with a |
| 534 |
* random key and the the random key is encrypted with the |
| 535 |
* certificates private key. This limits what private key type can be |
| 536 |
* used to RSA. |
| 537 |
* |
| 538 |
* @param context A hx509 context. |
| 539 |
* @param flags flags to control the behavior. |
| 540 |
* - HX509_CMS_EV_NO_KU_CHECK - Don't check KU on certificate |
| 541 |
* - HX509_CMS_EV_ALLOW_WEAK - Allow weak crytpo |
| 542 |
* - HX509_CMS_EV_ID_NAME - prefer issuer name and serial number |
| 543 |
* @param cert Certificate to encrypt the EnvelopedData encryption key |
| 544 |
* with. |
| 545 |
* @param data pointer the data to encrypt. |
| 546 |
* @param length length of the data that data point to. |
| 547 |
* @param encryption_type Encryption cipher to use for the bulk data, |
| 548 |
* use NULL to get default. |
| 549 |
* @param contentType type of the data that is encrypted |
| 550 |
* @param content the output of the function, |
| 551 |
* free with der_free_octet_string(). |
| 552 |
* |
| 553 |
* @return an hx509 error code. |
| 554 |
* |
| 555 |
* @ingroup hx509_cms |
| 556 |
*/ |
| 557 |
|
| 558 |
int |
| 559 |
hx509_cms_envelope_1(hx509_context context, |
| 560 |
int flags, |
| 561 |
hx509_cert cert, |
| 562 |
const void *data, |
| 563 |
size_t length, |
| 564 |
const heim_oid *encryption_type, |
| 565 |
const heim_oid *contentType, |
| 566 |
heim_octet_string *content) |
| 567 |
{ |
| 568 |
KeyTransRecipientInfo *ri; |
| 569 |
heim_octet_string ivec; |
| 570 |
heim_octet_string key; |
| 571 |
hx509_crypto crypto = NULL; |
| 572 |
int ret, cmsidflag; |
| 573 |
EnvelopedData ed; |
| 574 |
size_t size; |
| 575 |
|
| 576 |
memset(&ivec, 0, sizeof(ivec)); |
| 577 |
memset(&key, 0, sizeof(key)); |
| 578 |
memset(&ed, 0, sizeof(ed)); |
| 579 |
memset(content, 0, sizeof(*content)); |
| 580 |
|
| 581 |
if (encryption_type == NULL) |
| 582 |
encryption_type = &asn1_oid_id_aes_256_cbc; |
| 583 |
|
| 584 |
if ((flags & HX509_CMS_EV_NO_KU_CHECK) == 0) { |
| 585 |
ret = _hx509_check_key_usage(context, cert, 1 << 2, TRUE); |
| 586 |
if (ret) |
| 587 |
goto out; |
| 588 |
} |
| 589 |
|
| 590 |
ret = hx509_crypto_init(context, NULL, encryption_type, &crypto); |
| 591 |
if (ret) |
| 592 |
goto out; |
| 593 |
|
| 594 |
if (flags & HX509_CMS_EV_ALLOW_WEAK) |
| 595 |
hx509_crypto_allow_weak(crypto); |
| 596 |
|
| 597 |
ret = hx509_crypto_set_random_key(crypto, &key); |
| 598 |
if (ret) { |
| 599 |
hx509_set_error_string(context, 0, ret, |
| 600 |
"Create random key for EnvelopedData content"); |
| 601 |
goto out; |
| 602 |
} |
| 603 |
|
| 604 |
ret = hx509_crypto_random_iv(crypto, &ivec); |
| 605 |
if (ret) { |
| 606 |
hx509_set_error_string(context, 0, ret, |
| 607 |
"Failed to create a random iv"); |
| 608 |
goto out; |
| 609 |
} |
| 610 |
|
| 611 |
ret = hx509_crypto_encrypt(crypto, |
| 612 |
data, |
| 613 |
length, |
| 614 |
&ivec, |
| 615 |
&ed.encryptedContentInfo.encryptedContent); |
| 616 |
if (ret) { |
| 617 |
hx509_set_error_string(context, 0, ret, |
| 618 |
"Failed to encrypt EnvelopedData content"); |
| 619 |
goto out; |
| 620 |
} |
| 621 |
|
| 622 |
{ |
| 623 |
AlgorithmIdentifier *enc_alg; |
| 624 |
enc_alg = &ed.encryptedContentInfo.contentEncryptionAlgorithm; |
| 625 |
ret = der_copy_oid(encryption_type, &enc_alg->algorithm); |
| 626 |
if (ret) { |
| 627 |
hx509_set_error_string(context, 0, ret, |
| 628 |
"Failed to set crypto oid " |
| 629 |
"for EnvelopedData"); |
| 630 |
goto out; |
| 631 |
} |
| 632 |
ALLOC(enc_alg->parameters, 1); |
| 633 |
if (enc_alg->parameters == NULL) { |
| 634 |
ret = ENOMEM; |
| 635 |
hx509_set_error_string(context, 0, ret, |
| 636 |
"Failed to allocate crypto paramaters " |
| 637 |
"for EnvelopedData"); |
| 638 |
goto out; |
| 639 |
} |
| 640 |
|
| 641 |
ret = hx509_crypto_get_params(context, |
| 642 |
crypto, |
| 643 |
&ivec, |
| 644 |
enc_alg->parameters); |
| 645 |
if (ret) { |
| 646 |
goto out; |
| 647 |
} |
| 648 |
} |
| 649 |
|
| 650 |
ALLOC_SEQ(&ed.recipientInfos, 1); |
| 651 |
if (ed.recipientInfos.val == NULL) { |
| 652 |
ret = ENOMEM; |
| 653 |
hx509_set_error_string(context, 0, ret, |
| 654 |
"Failed to allocate recipients info " |
| 655 |
"for EnvelopedData"); |
| 656 |
goto out; |
| 657 |
} |
| 658 |
|
| 659 |
ri = &ed.recipientInfos.val[0]; |
| 660 |
|
| 661 |
if (flags & HX509_CMS_EV_ID_NAME) { |
| 662 |
ri->version = 0; |
| 663 |
cmsidflag = CMS_ID_NAME; |
| 664 |
} else { |
| 665 |
ri->version = 2; |
| 666 |
cmsidflag = CMS_ID_SKI; |
| 667 |
} |
| 668 |
|
| 669 |
ret = fill_CMSIdentifier(cert, cmsidflag, &ri->rid); |
| 670 |
if (ret) { |
| 671 |
hx509_set_error_string(context, 0, ret, |
| 672 |
"Failed to set CMS identifier info " |
| 673 |
"for EnvelopedData"); |
| 674 |
goto out; |
| 675 |
} |
| 676 |
|
| 677 |
ret = hx509_cert_public_encrypt(context, |
| 678 |
&key, cert, |
| 679 |
&ri->keyEncryptionAlgorithm.algorithm, |
| 680 |
&ri->encryptedKey); |
| 681 |
if (ret) { |
| 682 |
hx509_set_error_string(context, HX509_ERROR_APPEND, ret, |
| 683 |
"Failed to encrypt transport key for " |
| 684 |
"EnvelopedData"); |
| 685 |
goto out; |
| 686 |
} |
| 687 |
|
| 688 |
/* |
| 689 |
* |
| 690 |
*/ |
| 691 |
|
| 692 |
ed.version = 0; |
| 693 |
ed.originatorInfo = NULL; |
| 694 |
|
| 695 |
ret = der_copy_oid(contentType, &ed.encryptedContentInfo.contentType); |
| 696 |
if (ret) { |
| 697 |
hx509_set_error_string(context, 0, ret, |
| 698 |
"Failed to copy content oid for " |
| 699 |
"EnvelopedData"); |
| 700 |
goto out; |
| 701 |
} |
| 702 |
|
| 703 |
ed.unprotectedAttrs = NULL; |
| 704 |
|
| 705 |
ASN1_MALLOC_ENCODE(EnvelopedData, content->data, content->length, |
| 706 |
&ed, &size, ret); |
| 707 |
if (ret) { |
| 708 |
hx509_set_error_string(context, 0, ret, |
| 709 |
"Failed to encode EnvelopedData"); |
| 710 |
goto out; |
| 711 |
} |
| 712 |
if (size != content->length) |
| 713 |
_hx509_abort("internal ASN.1 encoder error"); |
| 714 |
|
| 715 |
out: |
| 716 |
if (crypto) |
| 717 |
hx509_crypto_destroy(crypto); |
| 718 |
if (ret) |
| 719 |
der_free_octet_string(content); |
| 720 |
der_free_octet_string(&key); |
| 721 |
der_free_octet_string(&ivec); |
| 722 |
free_EnvelopedData(&ed); |
| 723 |
|
| 724 |
return ret; |
| 725 |
} |
| 726 |
|
| 727 |
static int |
| 728 |
any_to_certs(hx509_context context, const SignedData *sd, hx509_certs certs) |
| 729 |
{ |
| 730 |
int ret; |
| 731 |
size_t i; |
| 732 |
|
| 733 |
if (sd->certificates == NULL) |
| 734 |
return 0; |
| 735 |
|
| 736 |
for (i = 0; i < sd->certificates->len; i++) { |
| 737 |
heim_error_t error; |
| 738 |
hx509_cert c; |
| 739 |
|
| 740 |
c = hx509_cert_init_data(context, |
| 741 |
sd->certificates->val[i].data, |
| 742 |
sd->certificates->val[i].length, |
| 743 |
&error); |
| 744 |
if (c == NULL) { |
| 745 |
ret = heim_error_get_code(error); |
| 746 |
heim_release(error); |
| 747 |
return ret; |
| 748 |
} |
| 749 |
ret = hx509_certs_add(context, certs, c); |
| 750 |
hx509_cert_free(c); |
| 751 |
if (ret) |
| 752 |
return ret; |
| 753 |
} |
| 754 |
|
| 755 |
return 0; |
| 756 |
} |
| 757 |
|
| 758 |
static const Attribute * |
| 759 |
find_attribute(const CMSAttributes *attr, const heim_oid *oid) |
| 760 |
{ |
| 761 |
size_t i; |
| 762 |
for (i = 0; i < attr->len; i++) |
| 763 |
if (der_heim_oid_cmp(&attr->val[i].type, oid) == 0) |
| 764 |
return &attr->val[i]; |
| 765 |
return NULL; |
| 766 |
} |
| 767 |
|
| 768 |
/** |
| 769 |
* Decode SignedData and verify that the signature is correct. |
| 770 |
* |
| 771 |
* @param context A hx509 context. |
| 772 |
* @param ctx a hx509 verify context. |
| 773 |
* @param flags to control the behaivor of the function. |
| 774 |
* - HX509_CMS_VS_NO_KU_CHECK - Don't check KeyUsage |
| 775 |
* - HX509_CMS_VS_ALLOW_DATA_OID_MISMATCH - allow oid mismatch |
| 776 |
* - HX509_CMS_VS_ALLOW_ZERO_SIGNER - no signer, see below. |
| 777 |
* @param data pointer to CMS SignedData encoded data. |
| 778 |
* @param length length of the data that data point to. |
| 779 |
* @param signedContent external data used for signature. |
| 780 |
* @param pool certificate pool to build certificates paths. |
| 781 |
* @param contentType free with der_free_oid(). |
| 782 |
* @param content the output of the function, free with |
| 783 |
* der_free_octet_string(). |
| 784 |
* @param signer_certs list of the cerficates used to sign this |
| 785 |
* request, free with hx509_certs_free(). |
| 786 |
* |
| 787 |
* @return an hx509 error code. |
| 788 |
* |
| 789 |
* @ingroup hx509_cms |
| 790 |
*/ |
| 791 |
|
| 792 |
int |
| 793 |
hx509_cms_verify_signed(hx509_context context, |
| 794 |
hx509_verify_ctx ctx, |
| 795 |
unsigned int flags, |
| 796 |
const void *data, |
| 797 |
size_t length, |
| 798 |
const heim_octet_string *signedContent, |
| 799 |
hx509_certs pool, |
| 800 |
heim_oid *contentType, |
| 801 |
heim_octet_string *content, |
| 802 |
hx509_certs *signer_certs) |
| 803 |
{ |
| 804 |
SignerInfo *signer_info; |
| 805 |
hx509_cert cert = NULL; |
| 806 |
hx509_certs certs = NULL; |
| 807 |
SignedData sd; |
| 808 |
size_t size; |
| 809 |
int ret, found_valid_sig; |
| 810 |
size_t i; |
| 811 |
|
| 812 |
*signer_certs = NULL; |
| 813 |
content->data = NULL; |
| 814 |
content->length = 0; |
| 815 |
contentType->length = 0; |
| 816 |
contentType->components = NULL; |
| 817 |
|
| 818 |
memset(&sd, 0, sizeof(sd)); |
| 819 |
|
| 820 |
ret = decode_SignedData(data, length, &sd, &size); |
| 821 |
if (ret) { |
| 822 |
hx509_set_error_string(context, 0, ret, |
| 823 |
"Failed to decode SignedData"); |
| 824 |
goto out; |
| 825 |
} |
| 826 |
|
| 827 |
if (sd.encapContentInfo.eContent == NULL && signedContent == NULL) { |
| 828 |
ret = HX509_CMS_NO_DATA_AVAILABLE; |
| 829 |
hx509_set_error_string(context, 0, ret, |
| 830 |
"No content data in SignedData"); |
| 831 |
goto out; |
| 832 |
} |
| 833 |
if (sd.encapContentInfo.eContent && signedContent) { |
| 834 |
ret = HX509_CMS_NO_DATA_AVAILABLE; |
| 835 |
hx509_set_error_string(context, 0, ret, |
| 836 |
"Both external and internal SignedData"); |
| 837 |
goto out; |
| 838 |
} |
| 839 |
|
| 840 |
if (sd.encapContentInfo.eContent) |
| 841 |
ret = der_copy_octet_string(sd.encapContentInfo.eContent, content); |
| 842 |
else |
| 843 |
ret = der_copy_octet_string(signedContent, content); |
| 844 |
if (ret) { |
| 845 |
hx509_set_error_string(context, 0, ret, "malloc: out of memory"); |
| 846 |
goto out; |
| 847 |
} |
| 848 |
|
| 849 |
ret = hx509_certs_init(context, "MEMORY:cms-cert-buffer", |
| 850 |
0, NULL, &certs); |
| 851 |
if (ret) |
| 852 |
goto out; |
| 853 |
|
| 854 |
ret = hx509_certs_init(context, "MEMORY:cms-signer-certs", |
| 855 |
0, NULL, signer_certs); |
| 856 |
if (ret) |
| 857 |
goto out; |
| 858 |
|
| 859 |
/* XXX Check CMS version */ |
| 860 |
|
| 861 |
ret = any_to_certs(context, &sd, certs); |
| 862 |
if (ret) |
| 863 |
goto out; |
| 864 |
|
| 865 |
if (pool) { |
| 866 |
ret = hx509_certs_merge(context, certs, pool); |
| 867 |
if (ret) |
| 868 |
goto out; |
| 869 |
} |
| 870 |
|
| 871 |
for (found_valid_sig = 0, i = 0; i < sd.signerInfos.len; i++) { |
| 872 |
heim_octet_string signed_data = { 0, 0 }; |
| 873 |
const heim_oid *match_oid; |
| 874 |
heim_oid decode_oid; |
| 875 |
|
| 876 |
signer_info = &sd.signerInfos.val[i]; |
| 877 |
match_oid = NULL; |
| 878 |
|
| 879 |
if (signer_info->signature.length == 0) { |
| 880 |
ret = HX509_CMS_MISSING_SIGNER_DATA; |
| 881 |
hx509_set_error_string(context, 0, ret, |
| 882 |
"SignerInfo %d in SignedData " |
| 883 |
"missing sigature", i); |
| 884 |
continue; |
| 885 |
} |
| 886 |
|
| 887 |
ret = find_CMSIdentifier(context, &signer_info->sid, certs, |
| 888 |
_hx509_verify_get_time(ctx), &cert, |
| 889 |
HX509_QUERY_KU_DIGITALSIGNATURE); |
| 890 |
if (ret) { |
| 891 |
/** |
| 892 |
* If HX509_CMS_VS_NO_KU_CHECK is set, allow more liberal |
| 893 |
* search for matching certificates by not considering |
| 894 |
* KeyUsage bits on the certificates. |
| 895 |
*/ |
| 896 |
if ((flags & HX509_CMS_VS_NO_KU_CHECK) == 0) |
| 897 |
continue; |
| 898 |
|
| 899 |
ret = find_CMSIdentifier(context, &signer_info->sid, certs, |
| 900 |
_hx509_verify_get_time(ctx), &cert, |
| 901 |
0); |
| 902 |
if (ret) |
| 903 |
continue; |
| 904 |
|
| 905 |
} |
| 906 |
|
| 907 |
if (signer_info->signedAttrs) { |
| 908 |
const Attribute *attr; |
| 909 |
|
| 910 |
CMSAttributes sa; |
| 911 |
heim_octet_string os; |
| 912 |
|
| 913 |
sa.val = signer_info->signedAttrs->val; |
| 914 |
sa.len = signer_info->signedAttrs->len; |
| 915 |
|
| 916 |
/* verify that sigature exists */ |
| 917 |
attr = find_attribute(&sa, &asn1_oid_id_pkcs9_messageDigest); |
| 918 |
if (attr == NULL) { |
| 919 |
ret = HX509_CRYPTO_SIGNATURE_MISSING; |
| 920 |
hx509_set_error_string(context, 0, ret, |
| 921 |
"SignerInfo have signed attributes " |
| 922 |
"but messageDigest (signature) " |
| 923 |
"is missing"); |
| 924 |
goto next_sigature; |
| 925 |
} |
| 926 |
if (attr->value.len != 1) { |
| 927 |
ret = HX509_CRYPTO_SIGNATURE_MISSING; |
| 928 |
hx509_set_error_string(context, 0, ret, |
| 929 |
"SignerInfo have more then one " |
| 930 |
"messageDigest (signature)"); |
| 931 |
goto next_sigature; |
| 932 |
} |
| 933 |
|
| 934 |
ret = decode_MessageDigest(attr->value.val[0].data, |
| 935 |
attr->value.val[0].length, |
| 936 |
&os, |
| 937 |
&size); |
| 938 |
if (ret) { |
| 939 |
hx509_set_error_string(context, 0, ret, |
| 940 |
"Failed to decode " |
| 941 |
"messageDigest (signature)"); |
| 942 |
goto next_sigature; |
| 943 |
} |
| 944 |
|
| 945 |
ret = _hx509_verify_signature(context, |
| 946 |
NULL, |
| 947 |
&signer_info->digestAlgorithm, |
| 948 |
content, |
| 949 |
&os); |
| 950 |
der_free_octet_string(&os); |
| 951 |
if (ret) { |
| 952 |
hx509_set_error_string(context, HX509_ERROR_APPEND, ret, |
| 953 |
"Failed to verify messageDigest"); |
| 954 |
goto next_sigature; |
| 955 |
} |
| 956 |
|
| 957 |
/* |
| 958 |
* Fetch content oid inside signedAttrs or set it to |
| 959 |
* id-pkcs7-data. |
| 960 |
*/ |
| 961 |
attr = find_attribute(&sa, &asn1_oid_id_pkcs9_contentType); |
| 962 |
if (attr == NULL) { |
| 963 |
match_oid = &asn1_oid_id_pkcs7_data; |
| 964 |
} else { |
| 965 |
if (attr->value.len != 1) { |
| 966 |
ret = HX509_CMS_DATA_OID_MISMATCH; |
| 967 |
hx509_set_error_string(context, 0, ret, |
| 968 |
"More then one oid in signedAttrs"); |
| 969 |
goto next_sigature; |
| 970 |
|
| 971 |
} |
| 972 |
ret = decode_ContentType(attr->value.val[0].data, |
| 973 |
attr->value.val[0].length, |
| 974 |
&decode_oid, |
| 975 |
&size); |
| 976 |
if (ret) { |
| 977 |
hx509_set_error_string(context, 0, ret, |
| 978 |
"Failed to decode " |
| 979 |
"oid in signedAttrs"); |
| 980 |
goto next_sigature; |
| 981 |
} |
| 982 |
match_oid = &decode_oid; |
| 983 |
} |
| 984 |
|
| 985 |
ASN1_MALLOC_ENCODE(CMSAttributes, |
| 986 |
signed_data.data, |
| 987 |
signed_data.length, |
| 988 |
&sa, |
| 989 |
&size, ret); |
| 990 |
if (ret) { |
| 991 |
if (match_oid == &decode_oid) |
| 992 |
der_free_oid(&decode_oid); |
| 993 |
hx509_clear_error_string(context); |
| 994 |
goto next_sigature; |
| 995 |
} |
| 996 |
if (size != signed_data.length) |
| 997 |
_hx509_abort("internal ASN.1 encoder error"); |
| 998 |
|
| 999 |
} else { |
| 1000 |
signed_data.data = content->data; |
| 1001 |
signed_data.length = content->length; |
| 1002 |
match_oid = &asn1_oid_id_pkcs7_data; |
| 1003 |
} |
| 1004 |
|
| 1005 |
/** |
| 1006 |
* If HX509_CMS_VS_ALLOW_DATA_OID_MISMATCH, allow |
| 1007 |
* encapContentInfo mismatch with the oid in signedAttributes |
| 1008 |
* (or if no signedAttributes where use, pkcs7-data oid). |
| 1009 |
* This is only needed to work with broken CMS implementations |
| 1010 |
* that doesn't follow CMS signedAttributes rules. |
| 1011 |
*/ |
| 1012 |
|
| 1013 |
if (der_heim_oid_cmp(match_oid, &sd.encapContentInfo.eContentType) && |
| 1014 |
(flags & HX509_CMS_VS_ALLOW_DATA_OID_MISMATCH) == 0) { |
| 1015 |
ret = HX509_CMS_DATA_OID_MISMATCH; |
| 1016 |
hx509_set_error_string(context, 0, ret, |
| 1017 |
"Oid in message mismatch from the expected"); |
| 1018 |
} |
| 1019 |
if (match_oid == &decode_oid) |
| 1020 |
der_free_oid(&decode_oid); |
| 1021 |
|
| 1022 |
if (ret == 0) { |
| 1023 |
ret = hx509_verify_signature(context, |
| 1024 |
cert, |
| 1025 |
&signer_info->signatureAlgorithm, |
| 1026 |
&signed_data, |
| 1027 |
&signer_info->signature); |
| 1028 |
if (ret) |
| 1029 |
hx509_set_error_string(context, HX509_ERROR_APPEND, ret, |
| 1030 |
"Failed to verify signature in " |
| 1031 |
"CMS SignedData"); |
| 1032 |
} |
| 1033 |
if (signed_data.data != NULL && content->data != signed_data.data) { |
| 1034 |
free(signed_data.data); |
| 1035 |
signed_data.data = NULL; |
| 1036 |
} |
| 1037 |
if (ret) |
| 1038 |
goto next_sigature; |
| 1039 |
|
| 1040 |
/** |
| 1041 |
* If HX509_CMS_VS_NO_VALIDATE flags is set, do not verify the |
| 1042 |
* signing certificates and leave that up to the caller. |
| 1043 |
*/ |
| 1044 |
|
| 1045 |
if ((flags & HX509_CMS_VS_NO_VALIDATE) == 0) { |
| 1046 |
ret = hx509_verify_path(context, ctx, cert, certs); |
| 1047 |
if (ret) |
| 1048 |
goto next_sigature; |
| 1049 |
} |
| 1050 |
|
| 1051 |
ret = hx509_certs_add(context, *signer_certs, cert); |
| 1052 |
if (ret) |
| 1053 |
goto next_sigature; |
| 1054 |
|
| 1055 |
found_valid_sig++; |
| 1056 |
|
| 1057 |
next_sigature: |
| 1058 |
if (cert) |
| 1059 |
hx509_cert_free(cert); |
| 1060 |
cert = NULL; |
| 1061 |
} |
| 1062 |
/** |
| 1063 |
* If HX509_CMS_VS_ALLOW_ZERO_SIGNER is set, allow empty |
| 1064 |
* SignerInfo (no signatures). If SignedData have no signatures, |
| 1065 |
* the function will return 0 with signer_certs set to NULL. Zero |
| 1066 |
* signers is allowed by the standard, but since its only useful |
| 1067 |
* in corner cases, it make into a flag that the caller have to |
| 1068 |
* turn on. |
| 1069 |
*/ |
| 1070 |
if (sd.signerInfos.len == 0 && (flags & HX509_CMS_VS_ALLOW_ZERO_SIGNER)) { |
| 1071 |
if (*signer_certs) |
| 1072 |
hx509_certs_free(signer_certs); |
| 1073 |
} else if (found_valid_sig == 0) { |
| 1074 |
if (ret == 0) { |
| 1075 |
ret = HX509_CMS_SIGNER_NOT_FOUND; |
| 1076 |
hx509_set_error_string(context, 0, ret, |
| 1077 |
"No signers where found"); |
| 1078 |
} |
| 1079 |
goto out; |
| 1080 |
} |
| 1081 |
|
| 1082 |
ret = der_copy_oid(&sd.encapContentInfo.eContentType, contentType); |
| 1083 |
if (ret) { |
| 1084 |
hx509_clear_error_string(context); |
| 1085 |
goto out; |
| 1086 |
} |
| 1087 |
|
| 1088 |
out: |
| 1089 |
free_SignedData(&sd); |
| 1090 |
if (certs) |
| 1091 |
hx509_certs_free(&certs); |
| 1092 |
if (ret) { |
| 1093 |
if (content->data) |
| 1094 |
der_free_octet_string(content); |
| 1095 |
if (*signer_certs) |
| 1096 |
hx509_certs_free(signer_certs); |
| 1097 |
der_free_oid(contentType); |
| 1098 |
der_free_octet_string(content); |
| 1099 |
} |
| 1100 |
|
| 1101 |
return ret; |
| 1102 |
} |
| 1103 |
|
| 1104 |
static int |
| 1105 |
add_one_attribute(Attribute **attr, |
| 1106 |
unsigned int *len, |
| 1107 |
const heim_oid *oid, |
| 1108 |
heim_octet_string *data) |
| 1109 |
{ |
| 1110 |
void *d; |
| 1111 |
int ret; |
| 1112 |
|
| 1113 |
d = realloc(*attr, sizeof((*attr)[0]) * (*len + 1)); |
| 1114 |
if (d == NULL) |
| 1115 |
return ENOMEM; |
| 1116 |
(*attr) = d; |
| 1117 |
|
| 1118 |
ret = der_copy_oid(oid, &(*attr)[*len].type); |
| 1119 |
if (ret) |
| 1120 |
return ret; |
| 1121 |
|
| 1122 |
ALLOC_SEQ(&(*attr)[*len].value, 1); |
| 1123 |
if ((*attr)[*len].value.val == NULL) { |
| 1124 |
der_free_oid(&(*attr)[*len].type); |
| 1125 |
return ENOMEM; |
| 1126 |
} |
| 1127 |
|
| 1128 |
(*attr)[*len].value.val[0].data = data->data; |
| 1129 |
(*attr)[*len].value.val[0].length = data->length; |
| 1130 |
|
| 1131 |
*len += 1; |
| 1132 |
|
| 1133 |
return 0; |
| 1134 |
} |
| 1135 |
|
| 1136 |
/** |
| 1137 |
* Decode SignedData and verify that the signature is correct. |
| 1138 |
* |
| 1139 |
* @param context A hx509 context. |
| 1140 |
* @param flags |
| 1141 |
* @param eContentType the type of the data. |
| 1142 |
* @param data data to sign |
| 1143 |
* @param length length of the data that data point to. |
| 1144 |
* @param digest_alg digest algorithm to use, use NULL to get the |
| 1145 |
* default or the peer determined algorithm. |
| 1146 |
* @param cert certificate to use for sign the data. |
| 1147 |
* @param peer info about the peer the message to send the message to, |
| 1148 |
* like what digest algorithm to use. |
| 1149 |
* @param anchors trust anchors that the client will use, used to |
| 1150 |
* polulate the certificates included in the message |
| 1151 |
* @param pool certificates to use in try to build the path to the |
| 1152 |
* trust anchors. |
| 1153 |
* @param signed_data the output of the function, free with |
| 1154 |
* der_free_octet_string(). |
| 1155 |
* |
| 1156 |
* @return Returns an hx509 error code. |
| 1157 |
* |
| 1158 |
* @ingroup hx509_cms |
| 1159 |
*/ |
| 1160 |
|
| 1161 |
int |
| 1162 |
hx509_cms_create_signed_1(hx509_context context, |
| 1163 |
int flags, |
| 1164 |
const heim_oid *eContentType, |
| 1165 |
const void *data, size_t length, |
| 1166 |
const AlgorithmIdentifier *digest_alg, |
| 1167 |
hx509_cert cert, |
| 1168 |
hx509_peer_info peer, |
| 1169 |
hx509_certs anchors, |
| 1170 |
hx509_certs pool, |
| 1171 |
heim_octet_string *signed_data) |
| 1172 |
{ |
| 1173 |
hx509_certs certs; |
| 1174 |
int ret = 0; |
| 1175 |
|
| 1176 |
signed_data->data = NULL; |
| 1177 |
signed_data->length = 0; |
| 1178 |
|
| 1179 |
ret = hx509_certs_init(context, "MEMORY:certs", 0, NULL, &certs); |
| 1180 |
if (ret) |
| 1181 |
return ret; |
| 1182 |
ret = hx509_certs_add(context, certs, cert); |
| 1183 |
if (ret) |
| 1184 |
goto out; |
| 1185 |
|
| 1186 |
ret = hx509_cms_create_signed(context, flags, eContentType, data, length, |
| 1187 |
digest_alg, certs, peer, anchors, pool, |
| 1188 |
signed_data); |
| 1189 |
|
| 1190 |
out: |
| 1191 |
hx509_certs_free(&certs); |
| 1192 |
return ret; |
| 1193 |
} |
| 1194 |
|
| 1195 |
struct sigctx { |
| 1196 |
SignedData sd; |
| 1197 |
const AlgorithmIdentifier *digest_alg; |
| 1198 |
const heim_oid *eContentType; |
| 1199 |
heim_octet_string content; |
| 1200 |
hx509_peer_info peer; |
| 1201 |
int cmsidflag; |
| 1202 |
int leafonly; |
| 1203 |
hx509_certs certs; |
| 1204 |
hx509_certs anchors; |
| 1205 |
hx509_certs pool; |
| 1206 |
}; |
| 1207 |
|
| 1208 |
static int |
| 1209 |
sig_process(hx509_context context, void *ctx, hx509_cert cert) |
| 1210 |
{ |
| 1211 |
struct sigctx *sigctx = ctx; |
| 1212 |
heim_octet_string buf, sigdata = { 0, NULL }; |
| 1213 |
SignerInfo *signer_info = NULL; |
| 1214 |
AlgorithmIdentifier digest; |
| 1215 |
size_t size; |
| 1216 |
void *ptr; |
| 1217 |
int ret; |
| 1218 |
SignedData *sd = &sigctx->sd; |
| 1219 |
hx509_path path; |
| 1220 |
|
| 1221 |
memset(&digest, 0, sizeof(digest)); |
| 1222 |
memset(&path, 0, sizeof(path)); |
| 1223 |
|
| 1224 |
if (_hx509_cert_private_key(cert) == NULL) { |
| 1225 |
hx509_set_error_string(context, 0, HX509_PRIVATE_KEY_MISSING, |
| 1226 |
"Private key missing for signing"); |
| 1227 |
return HX509_PRIVATE_KEY_MISSING; |
| 1228 |
} |
| 1229 |
|
| 1230 |
if (sigctx->digest_alg) { |
| 1231 |
ret = copy_AlgorithmIdentifier(sigctx->digest_alg, &digest); |
| 1232 |
if (ret) |
| 1233 |
hx509_clear_error_string(context); |
| 1234 |
} else { |
| 1235 |
ret = hx509_crypto_select(context, HX509_SELECT_DIGEST, |
| 1236 |
_hx509_cert_private_key(cert), |
| 1237 |
sigctx->peer, &digest); |
| 1238 |
} |
| 1239 |
if (ret) |
| 1240 |
goto out; |
| 1241 |
|
| 1242 |
/* |
| 1243 |
* Allocate on more signerInfo and do the signature processing |
| 1244 |
*/ |
| 1245 |
|
| 1246 |
ptr = realloc(sd->signerInfos.val, |
| 1247 |
(sd->signerInfos.len + 1) * sizeof(sd->signerInfos.val[0])); |
| 1248 |
if (ptr == NULL) { |
| 1249 |
ret = ENOMEM; |
| 1250 |
goto out; |
| 1251 |
} |
| 1252 |
sd->signerInfos.val = ptr; |
| 1253 |
|
| 1254 |
signer_info = &sd->signerInfos.val[sd->signerInfos.len]; |
| 1255 |
|
| 1256 |
memset(signer_info, 0, sizeof(*signer_info)); |
| 1257 |
|
| 1258 |
signer_info->version = 1; |
| 1259 |
|
| 1260 |
ret = fill_CMSIdentifier(cert, sigctx->cmsidflag, &signer_info->sid); |
| 1261 |
if (ret) { |
| 1262 |
hx509_clear_error_string(context); |
| 1263 |
goto out; |
| 1264 |
} |
| 1265 |
|
| 1266 |
signer_info->signedAttrs = NULL; |
| 1267 |
signer_info->unsignedAttrs = NULL; |
| 1268 |
|
| 1269 |
ret = copy_AlgorithmIdentifier(&digest, &signer_info->digestAlgorithm); |
| 1270 |
if (ret) { |
| 1271 |
hx509_clear_error_string(context); |
| 1272 |
goto out; |
| 1273 |
} |
| 1274 |
|
| 1275 |
/* |
| 1276 |
* If it isn't pkcs7-data send signedAttributes |
| 1277 |
*/ |
| 1278 |
|
| 1279 |
if (der_heim_oid_cmp(sigctx->eContentType, &asn1_oid_id_pkcs7_data) != 0) { |
| 1280 |
CMSAttributes sa; |
| 1281 |
heim_octet_string sig; |
| 1282 |
|
| 1283 |
ALLOC(signer_info->signedAttrs, 1); |
| 1284 |
if (signer_info->signedAttrs == NULL) { |
| 1285 |
ret = ENOMEM; |
| 1286 |
goto out; |
| 1287 |
} |
| 1288 |
|
| 1289 |
ret = _hx509_create_signature(context, |
| 1290 |
NULL, |
| 1291 |
&digest, |
| 1292 |
&sigctx->content, |
| 1293 |
NULL, |
| 1294 |
&sig); |
| 1295 |
if (ret) |
| 1296 |
goto out; |
| 1297 |
|
| 1298 |
ASN1_MALLOC_ENCODE(MessageDigest, |
| 1299 |
buf.data, |
| 1300 |
buf.length, |
| 1301 |
&sig, |
| 1302 |
&size, |
| 1303 |
ret); |
| 1304 |
der_free_octet_string(&sig); |
| 1305 |
if (ret) { |
| 1306 |
hx509_clear_error_string(context); |
| 1307 |
goto out; |
| 1308 |
} |
| 1309 |
if (size != buf.length) |
| 1310 |
_hx509_abort("internal ASN.1 encoder error"); |
| 1311 |
|
| 1312 |
ret = add_one_attribute(&signer_info->signedAttrs->val, |
| 1313 |
&signer_info->signedAttrs->len, |
| 1314 |
&asn1_oid_id_pkcs9_messageDigest, |
| 1315 |
&buf); |
| 1316 |
if (ret) { |
| 1317 |
free(buf.data); |
| 1318 |
hx509_clear_error_string(context); |
| 1319 |
goto out; |
| 1320 |
} |
| 1321 |
|
| 1322 |
|
| 1323 |
ASN1_MALLOC_ENCODE(ContentType, |
| 1324 |
buf.data, |
| 1325 |
buf.length, |
| 1326 |
sigctx->eContentType, |
| 1327 |
&size, |
| 1328 |
ret); |
| 1329 |
if (ret) |
| 1330 |
goto out; |
| 1331 |
if (size != buf.length) |
| 1332 |
_hx509_abort("internal ASN.1 encoder error"); |
| 1333 |
|
| 1334 |
ret = add_one_attribute(&signer_info->signedAttrs->val, |
| 1335 |
&signer_info->signedAttrs->len, |
| 1336 |
&asn1_oid_id_pkcs9_contentType, |
| 1337 |
&buf); |
| 1338 |
if (ret) { |
| 1339 |
free(buf.data); |
| 1340 |
hx509_clear_error_string(context); |
| 1341 |
goto out; |
| 1342 |
} |
| 1343 |
|
| 1344 |
sa.val = signer_info->signedAttrs->val; |
| 1345 |
sa.len = signer_info->signedAttrs->len; |
| 1346 |
|
| 1347 |
ASN1_MALLOC_ENCODE(CMSAttributes, |
| 1348 |
sigdata.data, |
| 1349 |
sigdata.length, |
| 1350 |
&sa, |
| 1351 |
&size, |
| 1352 |
ret); |
| 1353 |
if (ret) { |
| 1354 |
hx509_clear_error_string(context); |
| 1355 |
goto out; |
| 1356 |
} |
| 1357 |
if (size != sigdata.length) |
| 1358 |
_hx509_abort("internal ASN.1 encoder error"); |
| 1359 |
} else { |
| 1360 |
sigdata.data = sigctx->content.data; |
| 1361 |
sigdata.length = sigctx->content.length; |
| 1362 |
} |
| 1363 |
|
| 1364 |
{ |
| 1365 |
AlgorithmIdentifier sigalg; |
| 1366 |
|
| 1367 |
ret = hx509_crypto_select(context, HX509_SELECT_PUBLIC_SIG, |
| 1368 |
_hx509_cert_private_key(cert), sigctx->peer, |
| 1369 |
&sigalg); |
| 1370 |
if (ret) |
| 1371 |
goto out; |
| 1372 |
|
| 1373 |
ret = _hx509_create_signature(context, |
| 1374 |
_hx509_cert_private_key(cert), |
| 1375 |
&sigalg, |
| 1376 |
&sigdata, |
| 1377 |
&signer_info->signatureAlgorithm, |
| 1378 |
&signer_info->signature); |
| 1379 |
free_AlgorithmIdentifier(&sigalg); |
| 1380 |
if (ret) |
| 1381 |
goto out; |
| 1382 |
} |
| 1383 |
|
| 1384 |
sigctx->sd.signerInfos.len++; |
| 1385 |
signer_info = NULL; |
| 1386 |
|
| 1387 |
/* |
| 1388 |
* Provide best effort path |
| 1389 |
*/ |
| 1390 |
if (sigctx->certs) { |
| 1391 |
unsigned int i; |
| 1392 |
|
| 1393 |
if (sigctx->pool && sigctx->leafonly == 0) { |
| 1394 |
_hx509_calculate_path(context, |
| 1395 |
HX509_CALCULATE_PATH_NO_ANCHOR, |
| 1396 |
time(NULL), |
| 1397 |
sigctx->anchors, |
| 1398 |
0, |
| 1399 |
cert, |
| 1400 |
sigctx->pool, |
| 1401 |
&path); |
| 1402 |
} else |
| 1403 |
_hx509_path_append(context, &path, cert); |
| 1404 |
|
| 1405 |
for (i = 0; i < path.len; i++) { |
| 1406 |
/* XXX remove dups */ |
| 1407 |
ret = hx509_certs_add(context, sigctx->certs, path.val[i]); |
| 1408 |
if (ret) { |
| 1409 |
hx509_clear_error_string(context); |
| 1410 |
goto out; |
| 1411 |
} |
| 1412 |
} |
| 1413 |
} |
| 1414 |
|
| 1415 |
out: |
| 1416 |
if (signer_info) |
| 1417 |
free_SignerInfo(signer_info); |
| 1418 |
if (sigdata.data != sigctx->content.data) |
| 1419 |
der_free_octet_string(&sigdata); |
| 1420 |
_hx509_path_free(&path); |
| 1421 |
free_AlgorithmIdentifier(&digest); |
| 1422 |
|
| 1423 |
return ret; |
| 1424 |
} |
| 1425 |
|
| 1426 |
static int |
| 1427 |
cert_process(hx509_context context, void *ctx, hx509_cert cert) |
| 1428 |
{ |
| 1429 |
struct sigctx *sigctx = ctx; |
| 1430 |
const unsigned int i = sigctx->sd.certificates->len; |
| 1431 |
void *ptr; |
| 1432 |
int ret; |
| 1433 |
|
| 1434 |
ptr = realloc(sigctx->sd.certificates->val, |
| 1435 |
(i + 1) * sizeof(sigctx->sd.certificates->val[0])); |
| 1436 |
if (ptr == NULL) |
| 1437 |
return ENOMEM; |
| 1438 |
sigctx->sd.certificates->val = ptr; |
| 1439 |
|
| 1440 |
ret = hx509_cert_binary(context, cert, |
| 1441 |
&sigctx->sd.certificates->val[i]); |
| 1442 |
if (ret == 0) |
| 1443 |
sigctx->sd.certificates->len++; |
| 1444 |
|
| 1445 |
return ret; |
| 1446 |
} |
| 1447 |
|
| 1448 |
static int |
| 1449 |
cmp_AlgorithmIdentifier(const AlgorithmIdentifier *p, const AlgorithmIdentifier *q) |
| 1450 |
{ |
| 1451 |
return der_heim_oid_cmp(&p->algorithm, &q->algorithm); |
| 1452 |
} |
| 1453 |
|
| 1454 |
int |
| 1455 |
hx509_cms_create_signed(hx509_context context, |
| 1456 |
int flags, |
| 1457 |
const heim_oid *eContentType, |
| 1458 |
const void *data, size_t length, |
| 1459 |
const AlgorithmIdentifier *digest_alg, |
| 1460 |
hx509_certs certs, |
| 1461 |
hx509_peer_info peer, |
| 1462 |
hx509_certs anchors, |
| 1463 |
hx509_certs pool, |
| 1464 |
heim_octet_string *signed_data) |
| 1465 |
{ |
| 1466 |
unsigned int i, j; |
| 1467 |
hx509_name name; |
| 1468 |
int ret; |
| 1469 |
size_t size; |
| 1470 |
struct sigctx sigctx; |
| 1471 |
|
| 1472 |
memset(&sigctx, 0, sizeof(sigctx)); |
| 1473 |
memset(&name, 0, sizeof(name)); |
| 1474 |
|
| 1475 |
if (eContentType == NULL) |
| 1476 |
eContentType = &asn1_oid_id_pkcs7_data; |
| 1477 |
|
| 1478 |
sigctx.digest_alg = digest_alg; |
| 1479 |
sigctx.content.data = rk_UNCONST(data); |
| 1480 |
sigctx.content.length = length; |
| 1481 |
sigctx.eContentType = eContentType; |
| 1482 |
sigctx.peer = peer; |
| 1483 |
/** |
| 1484 |
* Use HX509_CMS_SIGNATURE_ID_NAME to preferred use of issuer name |
| 1485 |
* and serial number if possible. Otherwise subject key identifier |
| 1486 |
* will preferred. |
| 1487 |
*/ |
| 1488 |
if (flags & HX509_CMS_SIGNATURE_ID_NAME) |
| 1489 |
sigctx.cmsidflag = CMS_ID_NAME; |
| 1490 |
else |
| 1491 |
sigctx.cmsidflag = CMS_ID_SKI; |
| 1492 |
|
| 1493 |
/** |
| 1494 |
* Use HX509_CMS_SIGNATURE_LEAF_ONLY to only request leaf |
| 1495 |
* certificates to be added to the SignedData. |
| 1496 |
*/ |
| 1497 |
sigctx.leafonly = (flags & HX509_CMS_SIGNATURE_LEAF_ONLY) ? 1 : 0; |
| 1498 |
|
| 1499 |
/** |
| 1500 |
* Use HX509_CMS_NO_CERTS to make the SignedData contain no |
| 1501 |
* certificates, overrides HX509_CMS_SIGNATURE_LEAF_ONLY. |
| 1502 |
*/ |
| 1503 |
|
| 1504 |
if ((flags & HX509_CMS_SIGNATURE_NO_CERTS) == 0) { |
| 1505 |
ret = hx509_certs_init(context, "MEMORY:certs", 0, NULL, &sigctx.certs); |
| 1506 |
if (ret) |
| 1507 |
return ret; |
| 1508 |
} |
| 1509 |
|
| 1510 |
sigctx.anchors = anchors; |
| 1511 |
sigctx.pool = pool; |
| 1512 |
|
| 1513 |
sigctx.sd.version = CMSVersion_v3; |
| 1514 |
|
| 1515 |
der_copy_oid(eContentType, &sigctx.sd.encapContentInfo.eContentType); |
| 1516 |
|
| 1517 |
/** |
| 1518 |
* Use HX509_CMS_SIGNATURE_DETACHED to create detached signatures. |
| 1519 |
*/ |
| 1520 |
if ((flags & HX509_CMS_SIGNATURE_DETACHED) == 0) { |
| 1521 |
ALLOC(sigctx.sd.encapContentInfo.eContent, 1); |
| 1522 |
if (sigctx.sd.encapContentInfo.eContent == NULL) { |
| 1523 |
hx509_clear_error_string(context); |
| 1524 |
ret = ENOMEM; |
| 1525 |
goto out; |
| 1526 |
} |
| 1527 |
|
| 1528 |
sigctx.sd.encapContentInfo.eContent->data = malloc(length); |
| 1529 |
if (sigctx.sd.encapContentInfo.eContent->data == NULL) { |
| 1530 |
hx509_clear_error_string(context); |
| 1531 |
ret = ENOMEM; |
| 1532 |
goto out; |
| 1533 |
} |
| 1534 |
memcpy(sigctx.sd.encapContentInfo.eContent->data, data, length); |
| 1535 |
sigctx.sd.encapContentInfo.eContent->length = length; |
| 1536 |
} |
| 1537 |
|
| 1538 |
/** |
| 1539 |
* Use HX509_CMS_SIGNATURE_NO_SIGNER to create no sigInfo (no |
| 1540 |
* signatures). |
| 1541 |
*/ |
| 1542 |
if ((flags & HX509_CMS_SIGNATURE_NO_SIGNER) == 0) { |
| 1543 |
ret = hx509_certs_iter_f(context, certs, sig_process, &sigctx); |
| 1544 |
if (ret) |
| 1545 |
goto out; |
| 1546 |
} |
| 1547 |
|
| 1548 |
if (sigctx.sd.signerInfos.len) { |
| 1549 |
|
| 1550 |
/* |
| 1551 |
* For each signerInfo, collect all different digest types. |
| 1552 |
*/ |
| 1553 |
for (i = 0; i < sigctx.sd.signerInfos.len; i++) { |
| 1554 |
AlgorithmIdentifier *di = |
| 1555 |
&sigctx.sd.signerInfos.val[i].digestAlgorithm; |
| 1556 |
|
| 1557 |
for (j = 0; j < sigctx.sd.digestAlgorithms.len; j++) |
| 1558 |
if (cmp_AlgorithmIdentifier(di, &sigctx.sd.digestAlgorithms.val[j]) == 0) |
| 1559 |
break; |
| 1560 |
if (j == sigctx.sd.digestAlgorithms.len) { |
| 1561 |
ret = add_DigestAlgorithmIdentifiers(&sigctx.sd.digestAlgorithms, di); |
| 1562 |
if (ret) { |
| 1563 |
hx509_clear_error_string(context); |
| 1564 |
goto out; |
| 1565 |
} |
| 1566 |
} |
| 1567 |
} |
| 1568 |
} |
| 1569 |
|
| 1570 |
/* |
| 1571 |
* Add certs we think are needed, build as part of sig_process |
| 1572 |
*/ |
| 1573 |
if (sigctx.certs) { |
| 1574 |
ALLOC(sigctx.sd.certificates, 1); |
| 1575 |
if (sigctx.sd.certificates == NULL) { |
| 1576 |
hx509_clear_error_string(context); |
| 1577 |
ret = ENOMEM; |
| 1578 |
goto out; |
| 1579 |
} |
| 1580 |
|
| 1581 |
ret = hx509_certs_iter_f(context, sigctx.certs, cert_process, &sigctx); |
| 1582 |
if (ret) |
| 1583 |
goto out; |
| 1584 |
} |
| 1585 |
|
| 1586 |
ASN1_MALLOC_ENCODE(SignedData, |
| 1587 |
signed_data->data, signed_data->length, |
| 1588 |
&sigctx.sd, &size, ret); |
| 1589 |
if (ret) { |
| 1590 |
hx509_clear_error_string(context); |
| 1591 |
goto out; |
| 1592 |
} |
| 1593 |
if (signed_data->length != size) |
| 1594 |
_hx509_abort("internal ASN.1 encoder error"); |
| 1595 |
|
| 1596 |
out: |
| 1597 |
hx509_certs_free(&sigctx.certs); |
| 1598 |
free_SignedData(&sigctx.sd); |
| 1599 |
|
| 1600 |
return ret; |
| 1601 |
} |
| 1602 |
|
| 1603 |
int |
| 1604 |
hx509_cms_decrypt_encrypted(hx509_context context, |
| 1605 |
hx509_lock lock, |
| 1606 |
const void *data, |
| 1607 |
size_t length, |
| 1608 |
heim_oid *contentType, |
| 1609 |
heim_octet_string *content) |
| 1610 |
{ |
| 1611 |
heim_octet_string cont; |
| 1612 |
CMSEncryptedData ed; |
| 1613 |
AlgorithmIdentifier *ai; |
| 1614 |
int ret; |
| 1615 |
|
| 1616 |
memset(content, 0, sizeof(*content)); |
| 1617 |
memset(&cont, 0, sizeof(cont)); |
| 1618 |
|
| 1619 |
ret = decode_CMSEncryptedData(data, length, &ed, NULL); |
| 1620 |
if (ret) { |
| 1621 |
hx509_set_error_string(context, 0, ret, |
| 1622 |
"Failed to decode CMSEncryptedData"); |
| 1623 |
return ret; |
| 1624 |
} |
| 1625 |
|
| 1626 |
if (ed.encryptedContentInfo.encryptedContent == NULL) { |
| 1627 |
ret = HX509_CMS_NO_DATA_AVAILABLE; |
| 1628 |
hx509_set_error_string(context, 0, ret, |
| 1629 |
"No content in EncryptedData"); |
| 1630 |
goto out; |
| 1631 |
} |
| 1632 |
|
| 1633 |
ret = der_copy_oid(&ed.encryptedContentInfo.contentType, contentType); |
| 1634 |
if (ret) { |
| 1635 |
hx509_clear_error_string(context); |
| 1636 |
goto out; |
| 1637 |
} |
| 1638 |
|
| 1639 |
ai = &ed.encryptedContentInfo.contentEncryptionAlgorithm; |
| 1640 |
if (ai->parameters == NULL) { |
| 1641 |
ret = HX509_ALG_NOT_SUPP; |
| 1642 |
hx509_clear_error_string(context); |
| 1643 |
goto out; |
| 1644 |
} |
| 1645 |
|
| 1646 |
ret = _hx509_pbe_decrypt(context, |
| 1647 |
lock, |
| 1648 |
ai, |
| 1649 |
ed.encryptedContentInfo.encryptedContent, |
| 1650 |
&cont); |
| 1651 |
if (ret) |
| 1652 |
goto out; |
| 1653 |
|
| 1654 |
*content = cont; |
| 1655 |
|
| 1656 |
out: |
| 1657 |
if (ret) { |
| 1658 |
if (cont.data) |
| 1659 |
free(cont.data); |
| 1660 |
} |
| 1661 |
free_CMSEncryptedData(&ed); |
| 1662 |
return ret; |
| 1663 |
} |