| 1 |
/* cms.c - cryptographic message syntax main functions |
| 2 |
* Copyright (C) 2001, 2003, 2004, 2008, 2012, 2020 g10 Code GmbH |
| 3 |
* |
| 4 |
* This file is part of KSBA. |
| 5 |
* |
| 6 |
* KSBA is free software; you can redistribute it and/or modify |
| 7 |
* it under the terms of either |
| 8 |
* |
| 9 |
* - the GNU Lesser General Public License as published by the Free |
| 10 |
* Software Foundation; either version 3 of the License, or (at |
| 11 |
* your option) any later version. |
| 12 |
* |
| 13 |
* or |
| 14 |
* |
| 15 |
* - the GNU General Public License as published by the Free |
| 16 |
* Software Foundation; either version 2 of the License, or (at |
| 17 |
* your option) any later version. |
| 18 |
* |
| 19 |
* or both in parallel, as here. |
| 20 |
* |
| 21 |
* KSBA is distributed in the hope that it will be useful, but WITHOUT |
| 22 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
| 23 |
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public |
| 24 |
* License for more details. |
| 25 |
* |
| 26 |
* You should have received a copies of the GNU General Public License |
| 27 |
* and the GNU Lesser General Public License along with this program; |
| 28 |
* if not, see <http://www.gnu.org/licenses/>. |
| 29 |
*/ |
| 30 |
|
| 31 |
/* References: |
| 32 |
* RFC-5083 := CMS - Authenticated-Enveloped-Data |
| 33 |
* RFC-5084 := CMS - AES-GCM |
| 34 |
* RFC-5652 := Cryptographic Message Syntax (CMS) (aka STD0070) |
| 35 |
* SPHINX := CMS profile developed by the German BSI. |
| 36 |
* (see also https://lwn.net/2001/1011/a/german-smime.php3) |
| 37 |
* PKCS#7 := Original specification of CMS |
| 38 |
*/ |
| 39 |
|
| 40 |
#include <config.h> |
| 41 |
#include <stdio.h> |
| 42 |
#include <stdlib.h> |
| 43 |
#include <string.h> |
| 44 |
#include <assert.h> |
| 45 |
#include <errno.h> |
| 46 |
|
| 47 |
#include "util.h" |
| 48 |
|
| 49 |
#include "cms.h" |
| 50 |
#include "convert.h" |
| 51 |
#include "keyinfo.h" |
| 52 |
#include "der-encoder.h" |
| 53 |
#include "ber-help.h" |
| 54 |
#include "sexp-parse.h" |
| 55 |
#include "cert.h" |
| 56 |
#include "der-builder.h" |
| 57 |
#include "stringbuf.h" |
| 58 |
|
| 59 |
static gpg_error_t ct_parse_data (ksba_cms_t cms); |
| 60 |
static gpg_error_t ct_parse_signed_data (ksba_cms_t cms); |
| 61 |
static gpg_error_t ct_parse_enveloped_data (ksba_cms_t cms); |
| 62 |
static gpg_error_t ct_parse_digested_data (ksba_cms_t cms); |
| 63 |
static gpg_error_t ct_parse_encrypted_data (ksba_cms_t cms); |
| 64 |
static gpg_error_t ct_build_data (ksba_cms_t cms); |
| 65 |
static gpg_error_t ct_build_signed_data (ksba_cms_t cms); |
| 66 |
static gpg_error_t ct_build_enveloped_data (ksba_cms_t cms); |
| 67 |
static gpg_error_t ct_build_digested_data (ksba_cms_t cms); |
| 68 |
static gpg_error_t ct_build_encrypted_data (ksba_cms_t cms); |
| 69 |
|
| 70 |
static struct { |
| 71 |
const char *oid; |
| 72 |
ksba_content_type_t ct; |
| 73 |
gpg_error_t (*parse_handler)(ksba_cms_t); |
| 74 |
gpg_error_t (*build_handler)(ksba_cms_t); |
| 75 |
} content_handlers[] = { |
| 76 |
{ "1.2.840.113549.1.7.1", KSBA_CT_DATA, |
| 77 |
ct_parse_data , ct_build_data }, |
| 78 |
{ "1.2.840.113549.1.7.2", KSBA_CT_SIGNED_DATA, |
| 79 |
ct_parse_signed_data , ct_build_signed_data }, |
| 80 |
{ "1.2.840.113549.1.7.3", KSBA_CT_ENVELOPED_DATA, |
| 81 |
ct_parse_enveloped_data, ct_build_enveloped_data }, |
| 82 |
{ "1.2.840.113549.1.9.16.1.23", KSBA_CT_AUTHENVELOPED_DATA, |
| 83 |
ct_parse_enveloped_data, ct_build_enveloped_data }, |
| 84 |
{ "1.2.840.113549.1.7.5", KSBA_CT_DIGESTED_DATA, |
| 85 |
ct_parse_digested_data , ct_build_digested_data }, |
| 86 |
{ "1.2.840.113549.1.7.6", KSBA_CT_ENCRYPTED_DATA, |
| 87 |
ct_parse_encrypted_data, ct_build_encrypted_data }, |
| 88 |
{ "1.2.840.113549.1.9.16.1.2", KSBA_CT_AUTH_DATA }, |
| 89 |
{ "1.3.6.1.4.1.311.2.1.4", KSBA_CT_SPC_IND_DATA_CTX, |
| 90 |
ct_parse_data , ct_build_data }, |
| 91 |
{ "1.3.6.1.4.1.11591.2.3.1", KSBA_CT_OPENPGP_KEYBLOCK, |
| 92 |
ct_parse_data , ct_build_data }, |
| 93 |
{ NULL } |
| 94 |
}; |
| 95 |
|
| 96 |
static const char oidstr_contentType[] = "1.2.840.113549.1.9.3"; |
| 97 |
/*static char oid_contentType[9] = "\x2A\x86\x48\x86\xF7\x0D\x01\x09\x03";*/ |
| 98 |
|
| 99 |
static const char oidstr_messageDigest[] = "1.2.840.113549.1.9.4"; |
| 100 |
_KSBA_NONSTRING |
| 101 |
static const char oid_messageDigest[9] ="\x2A\x86\x48\x86\xF7\x0D\x01\x09\x04"; |
| 102 |
|
| 103 |
static const char oidstr_signingTime[] = "1.2.840.113549.1.9.5"; |
| 104 |
_KSBA_NONSTRING |
| 105 |
static const char oid_signingTime[9] = "\x2A\x86\x48\x86\xF7\x0D\x01\x09\x05"; |
| 106 |
|
| 107 |
static const char oidstr_smimeCapabilities[] = "1.2.840.113549.1.9.15"; |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
#if 0 /* Set to 1 to use this debug helper. */ |
| 112 |
static void |
| 113 |
log_sexp (const char *text, ksba_const_sexp_t p) |
| 114 |
{ |
| 115 |
int level = 0; |
| 116 |
|
| 117 |
gpgrt_log_debug ("%s: ", text); |
| 118 |
if (!p) |
| 119 |
gpgrt_log_printf ("[none]"); |
| 120 |
else |
| 121 |
{ |
| 122 |
for (;;) |
| 123 |
{ |
| 124 |
if (*p == '(') |
| 125 |
{ |
| 126 |
gpgrt_log_printf ("%c", *p); |
| 127 |
p++; |
| 128 |
level++; |
| 129 |
} |
| 130 |
else if (*p == ')') |
| 131 |
{ |
| 132 |
gpgrt_log_printf ("%c", *p); |
| 133 |
p++; |
| 134 |
if (--level <= 0 ) |
| 135 |
return; |
| 136 |
} |
| 137 |
else if (!digitp (p)) |
| 138 |
{ |
| 139 |
gpgrt_log_printf ("[invalid s-exp]"); |
| 140 |
return; |
| 141 |
} |
| 142 |
else |
| 143 |
{ |
| 144 |
char *endp; |
| 145 |
const unsigned char *s; |
| 146 |
unsigned long len, n; |
| 147 |
|
| 148 |
len = strtoul (p, &endp, 10); |
| 149 |
p = endp; |
| 150 |
if (*p != ':') |
| 151 |
{ |
| 152 |
gpgrt_log_printf ("[invalid s-exp]"); |
| 153 |
return; |
| 154 |
} |
| 155 |
p++; |
| 156 |
for (s=p,n=0; n < len; n++, s++) |
| 157 |
if ( !((*s >= 'a' && *s <= 'z') |
| 158 |
|| (*s >= 'A' && *s <= 'Z') |
| 159 |
|| (*s >= '0' && *s <= '9') |
| 160 |
|| *s == '-' || *s == '.')) |
| 161 |
break; |
| 162 |
if (n < len) |
| 163 |
{ |
| 164 |
gpgrt_log_printf ("#"); |
| 165 |
for (n=0; n < len; n++, p++) |
| 166 |
gpgrt_log_printf ("%02X", *p); |
| 167 |
gpgrt_log_printf ("#"); |
| 168 |
} |
| 169 |
else |
| 170 |
{ |
| 171 |
for (n=0; n < len; n++, p++) |
| 172 |
gpgrt_log_printf ("%c", *p); |
| 173 |
} |
| 174 |
} |
| 175 |
} |
| 176 |
} |
| 177 |
gpgrt_log_printf ("\n"); |
| 178 |
} |
| 179 |
#endif /* debug helper */ |
| 180 |
|
| 181 |
|
| 182 |
/* Helper for read_and_hash_cont(). */ |
| 183 |
static gpg_error_t |
| 184 |
read_hash_block (ksba_cms_t cms, unsigned long nleft) |
| 185 |
{ |
| 186 |
gpg_error_t err; |
| 187 |
char buffer[4096]; |
| 188 |
size_t n, nread; |
| 189 |
|
| 190 |
while (nleft) |
| 191 |
{ |
| 192 |
n = nleft < sizeof (buffer)? nleft : sizeof (buffer); |
| 193 |
err = ksba_reader_read (cms->reader, buffer, n, &nread); |
| 194 |
if (err) |
| 195 |
return err; |
| 196 |
nleft -= nread; |
| 197 |
if (cms->hash_fnc) |
| 198 |
cms->hash_fnc (cms->hash_fnc_arg, buffer, nread); |
| 199 |
if (cms->writer) |
| 200 |
err = ksba_writer_write (cms->writer, buffer, nread); |
| 201 |
if (err) |
| 202 |
return err; |
| 203 |
} |
| 204 |
return 0; |
| 205 |
} |
| 206 |
|
| 207 |
|
| 208 |
/* Copy all the bytes from the reader to the writer and hash them if a |
| 209 |
a hash function has been set. The writer may be NULL to just do |
| 210 |
the hashing */ |
| 211 |
static gpg_error_t |
| 212 |
read_and_hash_cont (ksba_cms_t cms) |
| 213 |
{ |
| 214 |
gpg_error_t err = 0; |
| 215 |
unsigned long nleft; |
| 216 |
struct tag_info ti; |
| 217 |
|
| 218 |
if (cms->inner_cont_ndef) |
| 219 |
{ |
| 220 |
for (;;) |
| 221 |
{ |
| 222 |
err = _ksba_ber_read_tl (cms->reader, &ti); |
| 223 |
if (err) |
| 224 |
return err; |
| 225 |
|
| 226 |
if (ti.class == CLASS_UNIVERSAL && ti.tag == TYPE_OCTET_STRING |
| 227 |
&& !ti.is_constructed) |
| 228 |
{ /* next chunk */ |
| 229 |
nleft = ti.length; |
| 230 |
err = read_hash_block (cms, nleft); |
| 231 |
if (err) |
| 232 |
return err; |
| 233 |
} |
| 234 |
else if (ti.class == CLASS_UNIVERSAL && ti.tag == TYPE_OCTET_STRING |
| 235 |
&& ti.is_constructed) |
| 236 |
{ /* next chunk is constructed */ |
| 237 |
for (;;) |
| 238 |
{ |
| 239 |
err = _ksba_ber_read_tl (cms->reader, &ti); |
| 240 |
if (err) |
| 241 |
return err; |
| 242 |
if (ti.class == CLASS_UNIVERSAL |
| 243 |
&& ti.tag == TYPE_OCTET_STRING |
| 244 |
&& !ti.is_constructed) |
| 245 |
{ |
| 246 |
nleft = ti.length; |
| 247 |
err = read_hash_block (cms, nleft); |
| 248 |
if (err) |
| 249 |
return err; |
| 250 |
} |
| 251 |
else if (ti.class == CLASS_UNIVERSAL && !ti.tag |
| 252 |
&& !ti.is_constructed) |
| 253 |
break; /* ready with this chunk */ |
| 254 |
else |
| 255 |
return gpg_error (GPG_ERR_ENCODING_PROBLEM); |
| 256 |
} |
| 257 |
} |
| 258 |
else if (ti.class == CLASS_UNIVERSAL && !ti.tag |
| 259 |
&& !ti.is_constructed) |
| 260 |
return 0; /* ready */ |
| 261 |
else |
| 262 |
return gpg_error (GPG_ERR_ENCODING_PROBLEM); |
| 263 |
} |
| 264 |
} |
| 265 |
else |
| 266 |
{ |
| 267 |
/* This is basically the same as above but we allow for |
| 268 |
arbitrary types. Not sure whether it is really needed but |
| 269 |
right in the beginning of gnupg 1.9 we had at least one |
| 270 |
message with didn't used octet strings. Not ethat we don't |
| 271 |
do proper NLEFT checking but well why should we validate |
| 272 |
these things? Well, it might be nice to have such a feature |
| 273 |
but then we should write a more general mechanism to do |
| 274 |
that. */ |
| 275 |
nleft = cms->inner_cont_len; |
| 276 |
/* First read the octet string but allow all types here */ |
| 277 |
err = _ksba_ber_read_tl (cms->reader, &ti); |
| 278 |
if (err) |
| 279 |
return err; |
| 280 |
if (nleft < ti.nhdr) |
| 281 |
return gpg_error (GPG_ERR_ENCODING_PROBLEM); |
| 282 |
nleft -= ti.nhdr; |
| 283 |
|
| 284 |
if (ti.class == CLASS_UNIVERSAL && ti.tag == TYPE_OCTET_STRING |
| 285 |
&& ti.is_constructed) |
| 286 |
{ /* Next chunk is constructed */ |
| 287 |
for (;;) |
| 288 |
{ |
| 289 |
err = _ksba_ber_read_tl (cms->reader, &ti); |
| 290 |
if (err) |
| 291 |
return err; |
| 292 |
if (ti.class == CLASS_UNIVERSAL |
| 293 |
&& ti.tag == TYPE_OCTET_STRING |
| 294 |
&& !ti.is_constructed) |
| 295 |
{ |
| 296 |
nleft = ti.length; |
| 297 |
err = read_hash_block (cms, nleft); |
| 298 |
if (err) |
| 299 |
return err; |
| 300 |
} |
| 301 |
else if (ti.class == CLASS_UNIVERSAL && !ti.tag |
| 302 |
&& !ti.is_constructed) |
| 303 |
break; /* Ready with this chunk */ |
| 304 |
else |
| 305 |
return gpg_error (GPG_ERR_ENCODING_PROBLEM); |
| 306 |
} |
| 307 |
} |
| 308 |
else if (ti.class == CLASS_UNIVERSAL && !ti.tag |
| 309 |
&& !ti.is_constructed) |
| 310 |
return 0; /* ready */ |
| 311 |
else |
| 312 |
{ |
| 313 |
err = read_hash_block (cms, nleft); |
| 314 |
if (err) |
| 315 |
return err; |
| 316 |
} |
| 317 |
} |
| 318 |
return 0; |
| 319 |
} |
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
/* Copy all the encrypted bytes from the reader to the writer. |
| 324 |
Handles indefinite length encoding */ |
| 325 |
static gpg_error_t |
| 326 |
read_encrypted_cont (ksba_cms_t cms) |
| 327 |
{ |
| 328 |
gpg_error_t err = 0; |
| 329 |
unsigned long nleft; |
| 330 |
char buffer[4096]; |
| 331 |
size_t n, nread; |
| 332 |
|
| 333 |
if (cms->inner_cont_ndef) |
| 334 |
{ |
| 335 |
struct tag_info ti; |
| 336 |
|
| 337 |
/* fixme: this ist mostly a duplicate of the code in |
| 338 |
read_and_hash_cont(). */ |
| 339 |
for (;;) |
| 340 |
{ |
| 341 |
err = _ksba_ber_read_tl (cms->reader, &ti); |
| 342 |
if (err) |
| 343 |
return err; |
| 344 |
|
| 345 |
if (ti.class == CLASS_UNIVERSAL && ti.tag == TYPE_OCTET_STRING |
| 346 |
&& !ti.is_constructed) |
| 347 |
{ /* next chunk */ |
| 348 |
nleft = ti.length; |
| 349 |
while (nleft) |
| 350 |
{ |
| 351 |
n = nleft < sizeof (buffer)? nleft : sizeof (buffer); |
| 352 |
err = ksba_reader_read (cms->reader, buffer, n, &nread); |
| 353 |
if (err) |
| 354 |
return err; |
| 355 |
nleft -= nread; |
| 356 |
err = ksba_writer_write (cms->writer, buffer, nread); |
| 357 |
if (err) |
| 358 |
return err; |
| 359 |
} |
| 360 |
} |
| 361 |
else if (ti.class == CLASS_UNIVERSAL && ti.tag == TYPE_OCTET_STRING |
| 362 |
&& ti.is_constructed) |
| 363 |
{ /* next chunk is constructed */ |
| 364 |
for (;;) |
| 365 |
{ |
| 366 |
err = _ksba_ber_read_tl (cms->reader, &ti); |
| 367 |
if (err) |
| 368 |
return err; |
| 369 |
if (ti.class == CLASS_UNIVERSAL |
| 370 |
&& ti.tag == TYPE_OCTET_STRING |
| 371 |
&& !ti.is_constructed) |
| 372 |
{ |
| 373 |
nleft = ti.length; |
| 374 |
while (nleft) |
| 375 |
{ |
| 376 |
n = nleft < sizeof (buffer)? nleft : sizeof (buffer); |
| 377 |
err = ksba_reader_read (cms->reader, buffer, n, &nread); |
| 378 |
if (err) |
| 379 |
return err; |
| 380 |
nleft -= nread; |
| 381 |
if (cms->writer) |
| 382 |
err = ksba_writer_write (cms->writer, buffer, nread); |
| 383 |
if (err) |
| 384 |
return err; |
| 385 |
} |
| 386 |
} |
| 387 |
else if (ti.class == CLASS_UNIVERSAL && !ti.tag |
| 388 |
&& !ti.is_constructed) |
| 389 |
break; /* ready with this chunk */ |
| 390 |
else |
| 391 |
return gpg_error (GPG_ERR_ENCODING_PROBLEM); |
| 392 |
} |
| 393 |
} |
| 394 |
else if (ti.class == CLASS_UNIVERSAL && !ti.tag |
| 395 |
&& !ti.is_constructed) |
| 396 |
return 0; /* ready */ |
| 397 |
else |
| 398 |
return gpg_error (GPG_ERR_ENCODING_PROBLEM); |
| 399 |
} |
| 400 |
} |
| 401 |
else |
| 402 |
{ |
| 403 |
nleft = cms->inner_cont_len; |
| 404 |
while (nleft) |
| 405 |
{ |
| 406 |
n = nleft < sizeof (buffer)? nleft : sizeof (buffer); |
| 407 |
err = ksba_reader_read (cms->reader, buffer, n, &nread); |
| 408 |
if (err) |
| 409 |
return err; |
| 410 |
nleft -= nread; |
| 411 |
err = ksba_writer_write (cms->writer, buffer, nread); |
| 412 |
if (err) |
| 413 |
return err; |
| 414 |
} |
| 415 |
} |
| 416 |
return 0; |
| 417 |
} |
| 418 |
|
| 419 |
/* copy data from reader to writer. Assume that it is an octet string |
| 420 |
and insert undefinite length headers where needed */ |
| 421 |
static gpg_error_t |
| 422 |
write_encrypted_cont (ksba_cms_t cms) |
| 423 |
{ |
| 424 |
gpg_error_t err = 0; |
| 425 |
char buffer[4096]; |
| 426 |
size_t nread; |
| 427 |
|
| 428 |
/* we do it the simple way: the parts are made up from the chunks we |
| 429 |
got from the read function. |
| 430 |
|
| 431 |
Fixme: We should write the tag here, and write a definite length |
| 432 |
header if everything fits into our local buffer. Actually pretty |
| 433 |
simple to do, but I am too lazy right now. */ |
| 434 |
while (!err && !(err = ksba_reader_read (cms->reader, buffer, |
| 435 |
sizeof buffer, &nread)) ) |
| 436 |
{ |
| 437 |
err = _ksba_ber_write_tl (cms->writer, TYPE_OCTET_STRING, |
| 438 |
CLASS_UNIVERSAL, 0, nread); |
| 439 |
if (!err) |
| 440 |
err = ksba_writer_write (cms->writer, buffer, nread); |
| 441 |
} |
| 442 |
if (gpg_err_code (err) == GPG_ERR_EOF) /* write the end tag */ |
| 443 |
err = _ksba_ber_write_tl (cms->writer, 0, 0, 0, 0); |
| 444 |
|
| 445 |
return err; |
| 446 |
} |
| 447 |
|
| 448 |
|
| 449 |
/* Figure out whether the data read from READER is a CMS object and |
| 450 |
return its content type. This function does only peek at the |
| 451 |
READER and tries to identify the type with best effort. Because of |
| 452 |
the ubiquity of the stupid and insecure pkcs#12 format, the |
| 453 |
function will also identify those files and return KSBA_CT_PKCS12; |
| 454 |
there is and will be no other pkcs#12 support in this library. */ |
| 455 |
ksba_content_type_t |
| 456 |
ksba_cms_identify (ksba_reader_t reader) |
| 457 |
{ |
| 458 |
struct tag_info ti; |
| 459 |
unsigned char buffer[24]; |
| 460 |
const unsigned char*p; |
| 461 |
size_t n, count; |
| 462 |
char *oid; |
| 463 |
int i; |
| 464 |
int maybe_p12 = 0; |
| 465 |
|
| 466 |
if (!reader) |
| 467 |
return KSBA_CT_NONE; /* oops */ |
| 468 |
|
| 469 |
/* This is a common example of a CMS object - it is obvious that we |
| 470 |
only need to read a few bytes to get to the OID: |
| 471 |
30 82 0B 59 06 09 2A 86 48 86 F7 0D 01 07 02 A0 82 0B 4A 30 82 0B 46 02 |
| 472 |
----------- ++++++++++++++++++++++++++++++++ |
| 473 |
SEQUENCE OID (signedData) |
| 474 |
(2 byte len) |
| 475 |
|
| 476 |
For a pkcs12 message we have this: |
| 477 |
|
| 478 |
30 82 08 59 02 01 03 30 82 08 1F 06 09 2A 86 48 86 F7 0D 01 07 01 A0 82 |
| 479 |
----------- ++++++++ ----------- ++++++++++++++++++++++++++++++++ |
| 480 |
SEQUENCE INTEGER SEQUENCE OID (data) |
| 481 |
|
| 482 |
This we need to read at least 22 bytes, we add 2 bytes to cope with |
| 483 |
length headers store with 4 bytes. |
| 484 |
*/ |
| 485 |
|
| 486 |
for (count = sizeof buffer; count; count -= n) |
| 487 |
{ |
| 488 |
if (ksba_reader_read (reader, buffer+sizeof (buffer)-count, count, &n)) |
| 489 |
return KSBA_CT_NONE; /* too short */ |
| 490 |
} |
| 491 |
n = sizeof buffer; |
| 492 |
if (ksba_reader_unread (reader, buffer, n)) |
| 493 |
return KSBA_CT_NONE; /* oops */ |
| 494 |
|
| 495 |
p = buffer; |
| 496 |
if (_ksba_ber_parse_tl (&p, &n, &ti)) |
| 497 |
return KSBA_CT_NONE; |
| 498 |
if ( !(ti.class == CLASS_UNIVERSAL && ti.tag == TYPE_SEQUENCE |
| 499 |
&& ti.is_constructed) ) |
| 500 |
return KSBA_CT_NONE; |
| 501 |
if (_ksba_ber_parse_tl (&p, &n, &ti)) |
| 502 |
return KSBA_CT_NONE; |
| 503 |
if ( ti.class == CLASS_UNIVERSAL && ti.tag == TYPE_INTEGER |
| 504 |
&& !ti.is_constructed && ti.length == 1 && n && *p == 3) |
| 505 |
{ |
| 506 |
maybe_p12 = 1; |
| 507 |
p++; |
| 508 |
n--; |
| 509 |
if (_ksba_ber_parse_tl (&p, &n, &ti)) |
| 510 |
return KSBA_CT_NONE; |
| 511 |
if ( !(ti.class == CLASS_UNIVERSAL && ti.tag == TYPE_SEQUENCE |
| 512 |
&& ti.is_constructed) ) |
| 513 |
return KSBA_CT_NONE; |
| 514 |
if (_ksba_ber_parse_tl (&p, &n, &ti)) |
| 515 |
return KSBA_CT_NONE; |
| 516 |
} |
| 517 |
if ( !(ti.class == CLASS_UNIVERSAL && ti.tag == TYPE_OBJECT_ID |
| 518 |
&& !ti.is_constructed && ti.length) || ti.length > n) |
| 519 |
return KSBA_CT_NONE; |
| 520 |
oid = ksba_oid_to_str (p, ti.length); |
| 521 |
if (!oid) |
| 522 |
return KSBA_CT_NONE; /* out of core */ |
| 523 |
for (i=0; content_handlers[i].oid; i++) |
| 524 |
{ |
| 525 |
if (!strcmp (content_handlers[i].oid, oid)) |
| 526 |
break; |
| 527 |
} |
| 528 |
ksba_free(oid); |
| 529 |
if (!content_handlers[i].oid) |
| 530 |
return KSBA_CT_NONE; /* unknown */ |
| 531 |
if (maybe_p12 && (content_handlers[i].ct == KSBA_CT_DATA |
| 532 |
|| content_handlers[i].ct == KSBA_CT_SIGNED_DATA)) |
| 533 |
return KSBA_CT_PKCS12; |
| 534 |
return content_handlers[i].ct; |
| 535 |
} |
| 536 |
|
| 537 |
|
| 538 |
|
| 539 |
/** |
| 540 |
* ksba_cms_new: |
| 541 |
* |
| 542 |
* Create a new and empty CMS object |
| 543 |
* |
| 544 |
* Return value: A CMS object or an error code. |
| 545 |
**/ |
| 546 |
gpg_error_t |
| 547 |
ksba_cms_new (ksba_cms_t *r_cms) |
| 548 |
{ |
| 549 |
*r_cms = xtrycalloc (1, sizeof **r_cms); |
| 550 |
if (!*r_cms) |
| 551 |
return gpg_error_from_errno (errno); |
| 552 |
return 0; |
| 553 |
} |
| 554 |
|
| 555 |
/* Release a list of value trees. */ |
| 556 |
static void |
| 557 |
release_value_tree (struct value_tree_s *tree) |
| 558 |
{ |
| 559 |
while (tree) |
| 560 |
{ |
| 561 |
struct value_tree_s *tmp = tree->next; |
| 562 |
_ksba_asn_release_nodes (tree->root); |
| 563 |
xfree (tree->image); |
| 564 |
xfree (tree); |
| 565 |
tree = tmp; |
| 566 |
} |
| 567 |
} |
| 568 |
|
| 569 |
/** |
| 570 |
* ksba_cms_release: |
| 571 |
* @cms: A CMS object |
| 572 |
* |
| 573 |
* Release a CMS object. |
| 574 |
**/ |
| 575 |
void |
| 576 |
ksba_cms_release (ksba_cms_t cms) |
| 577 |
{ |
| 578 |
if (!cms) |
| 579 |
return; |
| 580 |
xfree (cms->content.oid); |
| 581 |
while (cms->digest_algos) |
| 582 |
{ |
| 583 |
struct oidlist_s *ol = cms->digest_algos->next; |
| 584 |
xfree (cms->digest_algos->oid); |
| 585 |
xfree (cms->digest_algos); |
| 586 |
cms->digest_algos = ol; |
| 587 |
} |
| 588 |
while (cms->cert_list) |
| 589 |
{ |
| 590 |
struct certlist_s *cl = cms->cert_list->next; |
| 591 |
ksba_cert_release (cms->cert_list->cert); |
| 592 |
xfree (cms->cert_list->enc_val.algo); |
| 593 |
xfree (cms->cert_list->enc_val.value); |
| 594 |
xfree (cms->cert_list->enc_val.ecdh.e); |
| 595 |
xfree (cms->cert_list->enc_val.ecdh.wrap_algo); |
| 596 |
xfree (cms->cert_list->enc_val.ecdh.encr_algo); |
| 597 |
xfree (cms->cert_list); |
| 598 |
cms->cert_list = cl; |
| 599 |
} |
| 600 |
while (cms->cert_info_list) |
| 601 |
{ |
| 602 |
struct certlist_s *cl = cms->cert_info_list->next; |
| 603 |
ksba_cert_release (cms->cert_info_list->cert); |
| 604 |
xfree (cms->cert_info_list->enc_val.algo); |
| 605 |
xfree (cms->cert_info_list->enc_val.value); |
| 606 |
xfree (cms->cert_info_list); |
| 607 |
cms->cert_info_list = cl; |
| 608 |
} |
| 609 |
xfree (cms->inner_cont_oid); |
| 610 |
xfree (cms->encr_algo_oid); |
| 611 |
xfree (cms->encr_iv); |
| 612 |
xfree (cms->authdata.mac); |
| 613 |
xfree (cms->authdata.attr); |
| 614 |
while (cms->signer_info) |
| 615 |
{ |
| 616 |
struct signer_info_s *tmp = cms->signer_info->next; |
| 617 |
_ksba_asn_release_nodes (cms->signer_info->root); |
| 618 |
xfree (cms->signer_info->image); |
| 619 |
xfree (cms->signer_info->cache.digest_algo); |
| 620 |
xfree (cms->signer_info); |
| 621 |
cms->signer_info = tmp; |
| 622 |
} |
| 623 |
release_value_tree (cms->recp_info); |
| 624 |
while (cms->sig_val) |
| 625 |
{ |
| 626 |
struct sig_val_s *tmp = cms->sig_val->next; |
| 627 |
xfree (cms->sig_val->algo); |
| 628 |
xfree (cms->sig_val->value); |
| 629 |
xfree (cms->sig_val->ecc.r); |
| 630 |
xfree (cms->sig_val); |
| 631 |
cms->sig_val = tmp; |
| 632 |
} |
| 633 |
while (cms->capability_list) |
| 634 |
{ |
| 635 |
struct oidparmlist_s *tmp = cms->capability_list->next; |
| 636 |
xfree (cms->capability_list->oid); |
| 637 |
xfree (cms->capability_list); |
| 638 |
cms->capability_list = tmp; |
| 639 |
} |
| 640 |
while (cms->attribute_list) |
| 641 |
{ |
| 642 |
struct oidparmlist_s *tmp = cms->attribute_list->next; |
| 643 |
xfree (cms->attribute_list->oid); |
| 644 |
xfree (cms->attribute_list); |
| 645 |
cms->attribute_list = tmp; |
| 646 |
} |
| 647 |
|
| 648 |
xfree (cms); |
| 649 |
} |
| 650 |
|
| 651 |
|
| 652 |
gpg_error_t |
| 653 |
ksba_cms_set_reader_writer (ksba_cms_t cms, ksba_reader_t r, ksba_writer_t w) |
| 654 |
{ |
| 655 |
if (!cms || !(r || w)) |
| 656 |
return gpg_error (GPG_ERR_INV_VALUE); |
| 657 |
if ((r && cms->reader) || (w && cms->writer) ) |
| 658 |
return gpg_error (GPG_ERR_CONFLICT); /* already set */ |
| 659 |
|
| 660 |
cms->reader = r; |
| 661 |
cms->writer = w; |
| 662 |
return 0; |
| 663 |
} |
| 664 |
|
| 665 |
|
| 666 |
|
| 667 |
gpg_error_t |
| 668 |
ksba_cms_parse (ksba_cms_t cms, ksba_stop_reason_t *r_stopreason) |
| 669 |
{ |
| 670 |
gpg_error_t err; |
| 671 |
int i; |
| 672 |
|
| 673 |
if (!cms || !r_stopreason) |
| 674 |
return gpg_error (GPG_ERR_INV_VALUE); |
| 675 |
|
| 676 |
*r_stopreason = KSBA_SR_RUNNING; |
| 677 |
if (!cms->stop_reason) |
| 678 |
{ /* Initial state: start parsing */ |
| 679 |
err = _ksba_cms_parse_content_info (cms); |
| 680 |
if (err) |
| 681 |
return err; |
| 682 |
for (i=0; content_handlers[i].oid; i++) |
| 683 |
{ |
| 684 |
if (!strcmp (content_handlers[i].oid, cms->content.oid)) |
| 685 |
break; |
| 686 |
} |
| 687 |
if (!content_handlers[i].oid) |
| 688 |
return gpg_error (GPG_ERR_UNKNOWN_CMS_OBJ); |
| 689 |
if (!content_handlers[i].parse_handler) |
| 690 |
return gpg_error (GPG_ERR_UNSUPPORTED_CMS_OBJ); |
| 691 |
cms->content.ct = content_handlers[i].ct; |
| 692 |
cms->content.handler = content_handlers[i].parse_handler; |
| 693 |
cms->stop_reason = KSBA_SR_GOT_CONTENT; |
| 694 |
} |
| 695 |
else if (cms->content.handler) |
| 696 |
{ |
| 697 |
err = cms->content.handler (cms); |
| 698 |
if (err) |
| 699 |
return err; |
| 700 |
} |
| 701 |
else |
| 702 |
return gpg_error (GPG_ERR_UNSUPPORTED_CMS_OBJ); |
| 703 |
|
| 704 |
*r_stopreason = cms->stop_reason; |
| 705 |
return 0; |
| 706 |
} |
| 707 |
|
| 708 |
gpg_error_t |
| 709 |
ksba_cms_build (ksba_cms_t cms, ksba_stop_reason_t *r_stopreason) |
| 710 |
{ |
| 711 |
gpg_error_t err; |
| 712 |
|
| 713 |
if (!cms || !r_stopreason) |
| 714 |
return gpg_error (GPG_ERR_INV_VALUE); |
| 715 |
|
| 716 |
*r_stopreason = KSBA_SR_RUNNING; |
| 717 |
if (!cms->stop_reason) |
| 718 |
{ /* Initial state: check that the content handler is known */ |
| 719 |
if (!cms->writer) |
| 720 |
return gpg_error (GPG_ERR_MISSING_ACTION); |
| 721 |
if (!cms->content.handler) |
| 722 |
return gpg_error (GPG_ERR_MISSING_ACTION); |
| 723 |
if (!cms->inner_cont_oid) |
| 724 |
return gpg_error (GPG_ERR_MISSING_ACTION); |
| 725 |
cms->stop_reason = KSBA_SR_GOT_CONTENT; |
| 726 |
} |
| 727 |
else if (cms->content.handler) |
| 728 |
{ |
| 729 |
err = cms->content.handler (cms); |
| 730 |
if (err) |
| 731 |
return err; |
| 732 |
} |
| 733 |
else |
| 734 |
return gpg_error (GPG_ERR_UNSUPPORTED_CMS_OBJ); |
| 735 |
|
| 736 |
*r_stopreason = cms->stop_reason; |
| 737 |
return 0; |
| 738 |
} |
| 739 |
|
| 740 |
|
| 741 |
|
| 742 |
|
| 743 |
/* Return the content type. A WHAT of 0 returns the real content type |
| 744 |
whereas a 1 returns the inner content type. |
| 745 |
*/ |
| 746 |
ksba_content_type_t |
| 747 |
ksba_cms_get_content_type (ksba_cms_t cms, int what) |
| 748 |
{ |
| 749 |
int i; |
| 750 |
|
| 751 |
if (!cms) |
| 752 |
return 0; |
| 753 |
if (!what) |
| 754 |
return cms->content.ct; |
| 755 |
|
| 756 |
if (what == 1 && cms->inner_cont_oid) |
| 757 |
{ |
| 758 |
for (i=0; content_handlers[i].oid; i++) |
| 759 |
{ |
| 760 |
if (!strcmp (content_handlers[i].oid, cms->inner_cont_oid)) |
| 761 |
return content_handlers[i].ct; |
| 762 |
} |
| 763 |
} |
| 764 |
return 0; |
| 765 |
} |
| 766 |
|
| 767 |
|
| 768 |
/* Return the object ID of the current cms. This is a constant string |
| 769 |
valid as long as the context is valid and no new parse is |
| 770 |
started. */ |
| 771 |
const char * |
| 772 |
ksba_cms_get_content_oid (ksba_cms_t cms, int what) |
| 773 |
{ |
| 774 |
if (!cms) |
| 775 |
return NULL; |
| 776 |
if (!what) |
| 777 |
return cms->content.oid; |
| 778 |
if (what == 1) |
| 779 |
return cms->inner_cont_oid; |
| 780 |
if (what == 2) |
| 781 |
return cms->encr_algo_oid; |
| 782 |
return NULL; |
| 783 |
} |
| 784 |
|
| 785 |
|
| 786 |
/* Copy the initialization vector into iv and its len into ivlen. |
| 787 |
The caller should proncrvide a suitable large buffer */ |
| 788 |
gpg_error_t |
| 789 |
ksba_cms_get_content_enc_iv (ksba_cms_t cms, void *iv, |
| 790 |
size_t maxivlen, size_t *ivlen) |
| 791 |
{ |
| 792 |
if (!cms || !iv || !ivlen) |
| 793 |
return gpg_error (GPG_ERR_INV_VALUE); |
| 794 |
if (!cms->encr_ivlen) |
| 795 |
return gpg_error (GPG_ERR_NO_DATA); |
| 796 |
if (cms->encr_ivlen > maxivlen) |
| 797 |
return gpg_error (GPG_ERR_BUFFER_TOO_SHORT); |
| 798 |
memcpy (iv, cms->encr_iv, cms->encr_ivlen); |
| 799 |
*ivlen = cms->encr_ivlen; |
| 800 |
return 0; |
| 801 |
} |
| 802 |
|
| 803 |
|
| 804 |
/** |
| 805 |
* ksba_cert_get_digest_algo_list: |
| 806 |
* @cms: CMS object |
| 807 |
* @idx: enumerator |
| 808 |
* |
| 809 |
* Figure out the the digest algorithm used for the signature and |
| 810 |
* return its OID. Note that the algos returned are just hints on |
| 811 |
* what to hash. |
| 812 |
* |
| 813 |
* Return value: NULL for no more algorithms or a string valid as long |
| 814 |
* as the the cms object is valid. |
| 815 |
**/ |
| 816 |
const char * |
| 817 |
ksba_cms_get_digest_algo_list (ksba_cms_t cms, int idx) |
| 818 |
{ |
| 819 |
struct oidlist_s *ol; |
| 820 |
|
| 821 |
if (!cms) |
| 822 |
return NULL; |
| 823 |
|
| 824 |
for (ol=cms->digest_algos; ol && idx; ol = ol->next, idx-- ) |
| 825 |
; |
| 826 |
if (!ol) |
| 827 |
return NULL; |
| 828 |
return ol->oid; |
| 829 |
} |
| 830 |
|
| 831 |
|
| 832 |
/** |
| 833 |
* ksba_cms_get_issuer_serial: |
| 834 |
* @cms: CMS object |
| 835 |
* @idx: index number |
| 836 |
* @r_issuer: returns the issuer |
| 837 |
* @r_serial: returns the serial number |
| 838 |
* |
| 839 |
* This functions returns the issuer and serial number either from the |
| 840 |
* sid or the rid elements of a CMS object. |
| 841 |
* |
| 842 |
* Return value: 0 on success or an error code. An error code of -1 |
| 843 |
* is returned to indicate that there is no issuer with that idx, |
| 844 |
* GPG_ERR_NO_DATA is returned to indicate that there is no issuer at |
| 845 |
* all. |
| 846 |
**/ |
| 847 |
gpg_error_t |
| 848 |
ksba_cms_get_issuer_serial (ksba_cms_t cms, int idx, |
| 849 |
char **r_issuer, ksba_sexp_t *r_serial) |
| 850 |
{ |
| 851 |
gpg_error_t err; |
| 852 |
const char *issuer_path, *serial_path; |
| 853 |
AsnNode root; |
| 854 |
const unsigned char *image; |
| 855 |
AsnNode n; |
| 856 |
|
| 857 |
if (!cms) |
| 858 |
return gpg_error (GPG_ERR_INV_VALUE); |
| 859 |
if (idx < 0) |
| 860 |
return gpg_error (GPG_ERR_INV_INDEX); |
| 861 |
|
| 862 |
if (cms->signer_info) |
| 863 |
{ |
| 864 |
struct signer_info_s *si; |
| 865 |
|
| 866 |
for (si=cms->signer_info; si && idx; si = si->next, idx-- ) |
| 867 |
; |
| 868 |
if (!si) |
| 869 |
return -1; |
| 870 |
|
| 871 |
root = si->root; |
| 872 |
image = si->image; |
| 873 |
} |
| 874 |
else if (cms->recp_info) |
| 875 |
{ |
| 876 |
struct value_tree_s *tmp; |
| 877 |
|
| 878 |
for (tmp=cms->recp_info; tmp && idx; tmp=tmp->next, idx-- ) |
| 879 |
; |
| 880 |
if (!tmp) |
| 881 |
return -1; |
| 882 |
root = tmp->root; |
| 883 |
image = tmp->image; |
| 884 |
} |
| 885 |
else |
| 886 |
return gpg_error (GPG_ERR_NO_DATA); |
| 887 |
|
| 888 |
|
| 889 |
if (cms->signer_info) |
| 890 |
{ |
| 891 |
issuer_path = "SignerInfo.sid.issuerAndSerialNumber.issuer"; |
| 892 |
serial_path = "SignerInfo.sid.issuerAndSerialNumber.serialNumber"; |
| 893 |
} |
| 894 |
else if (cms->recp_info) |
| 895 |
{ |
| 896 |
/* Find the choice to use. */ |
| 897 |
n = _ksba_asn_find_node (root, "RecipientInfo.+"); |
| 898 |
if (!n || !n->name) |
| 899 |
return gpg_error (GPG_ERR_NO_VALUE); |
| 900 |
|
| 901 |
if (!strcmp (n->name, "ktri")) |
| 902 |
{ |
| 903 |
issuer_path = "ktri.rid.issuerAndSerialNumber.issuer"; |
| 904 |
serial_path = "ktri.rid.issuerAndSerialNumber.serialNumber"; |
| 905 |
} |
| 906 |
else if (!strcmp (n->name, "kari")) |
| 907 |
{ |
| 908 |
issuer_path = ("kari..recipientEncryptedKeys" |
| 909 |
"..rid.issuerAndSerialNumber.issuer"); |
| 910 |
serial_path = ("kari..recipientEncryptedKeys" |
| 911 |
"..rid.issuerAndSerialNumber.serialNumber"); |
| 912 |
} |
| 913 |
else if (!strcmp (n->name, "kekri")) |
| 914 |
return gpg_error (GPG_ERR_UNSUPPORTED_CMS_OBJ); |
| 915 |
else if (!strcmp (n->name, "pwri")) |
| 916 |
return gpg_error (GPG_ERR_UNSUPPORTED_CMS_OBJ); |
| 917 |
else |
| 918 |
return gpg_error (GPG_ERR_INV_CMS_OBJ); |
| 919 |
root = n; |
| 920 |
} |
| 921 |
|
| 922 |
if (r_issuer) |
| 923 |
{ |
| 924 |
n = _ksba_asn_find_node (root, issuer_path); |
| 925 |
if (!n || !n->down) |
| 926 |
return gpg_error (GPG_ERR_NO_VALUE); |
| 927 |
n = n->down; /* dereference the choice node */ |
| 928 |
|
| 929 |
if (n->off == -1) |
| 930 |
{ |
| 931 |
/* fputs ("get_issuer problem at node:\n", stderr); */ |
| 932 |
/* _ksba_asn_node_dump_all (n, stderr); */ |
| 933 |
return gpg_error (GPG_ERR_GENERAL); |
| 934 |
} |
| 935 |
err = _ksba_dn_to_str (image, n, r_issuer); |
| 936 |
if (err) |
| 937 |
return err; |
| 938 |
} |
| 939 |
|
| 940 |
if (r_serial) |
| 941 |
{ |
| 942 |
char numbuf[22]; |
| 943 |
int numbuflen; |
| 944 |
unsigned char *p; |
| 945 |
|
| 946 |
/* fixme: we do not release the r_issuer stuff on error */ |
| 947 |
n = _ksba_asn_find_node (root, serial_path); |
| 948 |
if (!n) |
| 949 |
return gpg_error (GPG_ERR_NO_VALUE); |
| 950 |
|
| 951 |
if (n->off == -1) |
| 952 |
{ |
| 953 |
/* fputs ("get_serial problem at node:\n", stderr); */ |
| 954 |
/* _ksba_asn_node_dump_all (n, stderr); */ |
| 955 |
return gpg_error (GPG_ERR_GENERAL); |
| 956 |
} |
| 957 |
|
| 958 |
if (n->len > MAX_SERIALNO_LENGTH) |
| 959 |
return gpg_error (GPG_ERR_INV_CERT_OBJ); |
| 960 |
sprintf (numbuf,"(%u:", (unsigned int)n->len); |
| 961 |
numbuflen = strlen (numbuf); |
| 962 |
p = xtrymalloc (numbuflen + n->len + 2); |
| 963 |
if (!p) |
| 964 |
return gpg_error (GPG_ERR_ENOMEM); |
| 965 |
strcpy (p, numbuf); |
| 966 |
memcpy (p+numbuflen, image + n->off + n->nhdr, n->len); |
| 967 |
p[numbuflen + n->len] = ')'; |
| 968 |
p[numbuflen + n->len + 1] = 0; |
| 969 |
*r_serial = p; |
| 970 |
} |
| 971 |
|
| 972 |
return 0; |
| 973 |
} |
| 974 |
|
| 975 |
|
| 976 |
|
| 977 |
/** |
| 978 |
* ksba_cms_get_digest_algo: |
| 979 |
* @cms: CMS object |
| 980 |
* @idx: index of signer |
| 981 |
* |
| 982 |
* Figure out the the digest algorithm used by the signer @idx return |
| 983 |
* its OID. This is the algorithm acually used to calculate the |
| 984 |
* signature. |
| 985 |
* |
| 986 |
* Return value: NULL for no such signer or a constn string valid as |
| 987 |
* long as the CMS object lives. |
| 988 |
**/ |
| 989 |
const char * |
| 990 |
ksba_cms_get_digest_algo (ksba_cms_t cms, int idx) |
| 991 |
{ |
| 992 |
AsnNode n; |
| 993 |
char *algo; |
| 994 |
struct signer_info_s *si; |
| 995 |
|
| 996 |
if (!cms) |
| 997 |
return NULL; |
| 998 |
if (!cms->signer_info) |
| 999 |
return NULL; |
| 1000 |
if (idx < 0) |
| 1001 |
return NULL; |
| 1002 |
|
| 1003 |
for (si=cms->signer_info; si && idx; si = si->next, idx-- ) |
| 1004 |
; |
| 1005 |
if (!si) |
| 1006 |
return NULL; |
| 1007 |
|
| 1008 |
if (si->cache.digest_algo) |
| 1009 |
return si->cache.digest_algo; |
| 1010 |
|
| 1011 |
n = _ksba_asn_find_node (si->root, "SignerInfo.digestAlgorithm.algorithm"); |
| 1012 |
algo = _ksba_oid_node_to_str (si->image, n); |
| 1013 |
if (algo) |
| 1014 |
{ |
| 1015 |
si->cache.digest_algo = algo; |
| 1016 |
} |
| 1017 |
return algo; |
| 1018 |
} |
| 1019 |
|
| 1020 |
|
| 1021 |
/** |
| 1022 |
* ksba_cms_get_cert: |
| 1023 |
* @cms: CMS object |
| 1024 |
* @idx: enumerator |
| 1025 |
* |
| 1026 |
* Get the certificate out of a CMS. The caller should use this in a |
| 1027 |
* loop to get all certificates. The returned certificate is a |
| 1028 |
* shallow copy of the original one; the caller must still use |
| 1029 |
* ksba_cert_release() to free it. |
| 1030 |
* |
| 1031 |
* Return value: A Certificate object or NULL for end of list or error |
| 1032 |
**/ |
| 1033 |
ksba_cert_t |
| 1034 |
ksba_cms_get_cert (ksba_cms_t cms, int idx) |
| 1035 |
{ |
| 1036 |
struct certlist_s *cl; |
| 1037 |
|
| 1038 |
if (!cms || idx < 0) |
| 1039 |
return NULL; |
| 1040 |
|
| 1041 |
for (cl=cms->cert_list; cl && idx; cl = cl->next, idx--) |
| 1042 |
; |
| 1043 |
if (!cl) |
| 1044 |
return NULL; |
| 1045 |
ksba_cert_ref (cl->cert); |
| 1046 |
return cl->cert; |
| 1047 |
} |
| 1048 |
|
| 1049 |
|
| 1050 |
/* Return the signed or unsigned attribute for SIGNER at IDX. Set |
| 1051 |
* UNPROTECTED to return the unsigned attributes of the signer. The |
| 1052 |
* caller must release the values returned at R_OID, R_DER, and |
| 1053 |
* R_DERLEN. On error or if no value was found a NULL is stored |
| 1054 |
* there. R_OID always receives a value (if not passed as NULL) but |
| 1055 |
* R_DER might receive NULL. |
| 1056 |
* |
| 1057 |
* An error code GPG_ERR_NOT_FOUND indicates that there is no signer |
| 1058 |
* with the signer index SIGNER. An error code GPG_ERR_EOF indicates |
| 1059 |
* that there is no attribute under the index IDX. |
| 1060 |
* |
| 1061 |
* To enumerate all signed attributes this pseudo code can be used: |
| 1062 |
* |
| 1063 |
* char *oid = NULL; |
| 1064 |
* unsigned char *der = NULL; |
| 1065 |
* size_t derlen; |
| 1066 |
* for (signer=0; signer >= 0; signer++) |
| 1067 |
* for (idx=0; idx >= 0; idx++) |
| 1068 |
* { |
| 1069 |
* ksba_free (oid); |
| 1070 |
* ksba_free (der); |
| 1071 |
* err = ksba_cms_get_attribute (cms, signer, idx, 0,&oid,&der,&derlen); |
| 1072 |
* if (gpg_err_code (err) == GPG_ERR_EOF) |
| 1073 |
* idx = -2; |
| 1074 |
* else if (gpg_err_code (err) == GPG_ERR_NOT_FOUND) |
| 1075 |
* idx = signer = -2; |
| 1076 |
* else |
| 1077 |
* handle_err_or_process_values (err, oid, der, derlen); |
| 1078 |
* } |
| 1079 |
* ksba_free (oid); |
| 1080 |
* ksba_free (der); |
| 1081 |
*/ |
| 1082 |
gpg_error_t |
| 1083 |
ksba_cms_get_attribute (ksba_cms_t cms, int signer, int idx, int unprotected, |
| 1084 |
char **r_oid, unsigned char **r_der, size_t *r_derlen) |
| 1085 |
{ |
| 1086 |
gpg_error_t err; |
| 1087 |
AsnNode topnode, node, n; |
| 1088 |
struct signer_info_s *si; |
| 1089 |
|
| 1090 |
if (r_oid) |
| 1091 |
*r_oid = NULL; |
| 1092 |
if (r_der) |
| 1093 |
*r_der = NULL; |
| 1094 |
if (r_derlen) |
| 1095 |
*r_derlen = 0; |
| 1096 |
|
| 1097 |
if (!cms) |
| 1098 |
return gpg_error (GPG_ERR_INV_VALUE); |
| 1099 |
if (!cms->signer_info) |
| 1100 |
return gpg_error (GPG_ERR_NO_DATA); |
| 1101 |
if (signer < 0 || idx < 0) |
| 1102 |
return gpg_error (GPG_ERR_INV_INDEX); |
| 1103 |
|
| 1104 |
for (si=cms->signer_info; si && signer; si = si->next, signer-- ) |
| 1105 |
; |
| 1106 |
if (!si) |
| 1107 |
return gpg_error (GPG_ERR_NOT_FOUND); /* No more signers */ |
| 1108 |
|
| 1109 |
node = _ksba_asn_find_node (si->root, |
| 1110 |
unprotected? "SignerInfo.unsignedAttrs" |
| 1111 |
/* */ : "SignerInfo.signedAttrs"); |
| 1112 |
topnode = node; |
| 1113 |
if (node && node->type == TYPE_TAG) |
| 1114 |
node = node->down; |
| 1115 |
else |
| 1116 |
node = NULL; /* Bad CMS: not a context tag - ignore this. */ |
| 1117 |
for (; node && idx >= 0; node = _ksba_asn_walk_tree (topnode, node)) |
| 1118 |
{ |
| 1119 |
if (node->type == TYPE_SEQUENCE |
| 1120 |
&& (n = node->down) && n->type == TYPE_OBJECT_ID |
| 1121 |
&& n->off != -1 && n->right && n->right->type == TYPE_SET_OF) |
| 1122 |
{ |
| 1123 |
if (idx--) |
| 1124 |
continue; /* Not yet at the desired index. */ |
| 1125 |
if (r_oid) |
| 1126 |
{ |
| 1127 |
*r_oid = _ksba_oid_to_str (si->image + n->off + n->nhdr, n->len); |
| 1128 |
if (!*r_oid) |
| 1129 |
return gpg_error_from_syserror (); |
| 1130 |
} |
| 1131 |
n = n->right; /* Point to the set. */ |
| 1132 |
if (n->off != -1 && n->len && r_der && r_derlen) |
| 1133 |
{ |
| 1134 |
*r_der = xtrymalloc (n->len); |
| 1135 |
if (!*r_der) |
| 1136 |
{ |
| 1137 |
err = gpg_error_from_syserror (); |
| 1138 |
if (r_oid) |
| 1139 |
{ |
| 1140 |
xfree (*r_oid); |
| 1141 |
*r_oid = NULL; |
| 1142 |
} |
| 1143 |
return err; |
| 1144 |
} |
| 1145 |
memcpy (*r_der, si->image + n->off + n->nhdr, n->len); |
| 1146 |
*r_derlen = n->len; |
| 1147 |
} |
| 1148 |
return 0; |
| 1149 |
} |
| 1150 |
} |
| 1151 |
|
| 1152 |
return gpg_error (GPG_ERR_EOF); /* No more signed attributes for signer. */ |
| 1153 |
} |
| 1154 |
|
| 1155 |
|
| 1156 |
/* In the case of signed data return the extension attribute |
| 1157 |
* messageDigest. In case of AUTHENVELOPEDDATA return either the MAC |
| 1158 |
* (with IDX 0) or the attributes (with IDX 1). Note that the parser |
| 1159 |
* currently returns a not-implemented error when it encounters |
| 1160 |
* attributes; we first need to have some solid sample data to |
| 1161 |
* implement that. */ |
| 1162 |
gpg_error_t |
| 1163 |
ksba_cms_get_message_digest (ksba_cms_t cms, int idx, |
| 1164 |
char **r_digest, size_t *r_digest_len) |
| 1165 |
{ |
| 1166 |
AsnNode nsiginfo, n; |
| 1167 |
struct signer_info_s *si; |
| 1168 |
|
| 1169 |
if (!cms || !r_digest || !r_digest_len) |
| 1170 |
return gpg_error (GPG_ERR_INV_VALUE); |
| 1171 |
|
| 1172 |
/* Hack to return the MAC/authtag value or the authAttr. */ |
| 1173 |
if (cms->content.ct == KSBA_CT_AUTHENVELOPED_DATA) |
| 1174 |
{ |
| 1175 |
if (!idx) /* Return authtag. */ |
| 1176 |
{ |
| 1177 |
if (!cms->authdata.mac || !cms->authdata.mac_len) |
| 1178 |
return gpg_error (GPG_ERR_NO_DATA); |
| 1179 |
|
| 1180 |
*r_digest = xtrymalloc (cms->authdata.mac_len); |
| 1181 |
if (!*r_digest) |
| 1182 |
return gpg_error_from_syserror (); |
| 1183 |
memcpy (*r_digest, cms->authdata.mac, cms->authdata.mac_len); |
| 1184 |
*r_digest_len = cms->authdata.mac_len; |
| 1185 |
} |
| 1186 |
else if (idx == 1) /* Return authAttr. */ |
| 1187 |
{ |
| 1188 |
if (!cms->authdata.attr || !cms->authdata.attr_len) |
| 1189 |
return gpg_error (GPG_ERR_NO_DATA); |
| 1190 |
|
| 1191 |
*r_digest = xtrymalloc (cms->authdata.attr_len); |
| 1192 |
if (!*r_digest) |
| 1193 |
return gpg_error_from_syserror (); |
| 1194 |
memcpy (*r_digest, cms->authdata.attr, cms->authdata.attr_len); |
| 1195 |
*r_digest_len = cms->authdata.attr_len; |
| 1196 |
} |
| 1197 |
else |
| 1198 |
return gpg_error (GPG_ERR_INV_INDEX); |
| 1199 |
|
| 1200 |
return 0; |
| 1201 |
} |
| 1202 |
|
| 1203 |
|
| 1204 |
if (!cms->signer_info) |
| 1205 |
return gpg_error (GPG_ERR_NO_DATA); |
| 1206 |
if (idx < 0) |
| 1207 |
return gpg_error (GPG_ERR_INV_INDEX); |
| 1208 |
|
| 1209 |
for (si=cms->signer_info; si && idx; si = si->next, idx-- ) |
| 1210 |
; |
| 1211 |
if (!si) |
| 1212 |
return -1; |
| 1213 |
|
| 1214 |
|
| 1215 |
*r_digest = NULL; |
| 1216 |
*r_digest_len = 0; |
| 1217 |
nsiginfo = _ksba_asn_find_node (si->root, "SignerInfo.signedAttrs"); |
| 1218 |
if (!nsiginfo) |
| 1219 |
return gpg_error (GPG_ERR_BUG); |
| 1220 |
|
| 1221 |
n = _ksba_asn_find_type_value (si->image, nsiginfo, 0, |
| 1222 |
oid_messageDigest, DIM(oid_messageDigest)); |
| 1223 |
if (!n) |
| 1224 |
return 0; /* this is okay, because the element is optional */ |
| 1225 |
|
| 1226 |
/* check that there is only one */ |
| 1227 |
if (_ksba_asn_find_type_value (si->image, nsiginfo, 1, |
| 1228 |
oid_messageDigest, DIM(oid_messageDigest))) |
| 1229 |
return gpg_error (GPG_ERR_DUP_VALUE); |
| 1230 |
|
| 1231 |
/* The value is is a SET OF OCTECT STRING but the set must have |
| 1232 |
excactly one OCTECT STRING. (rfc5652 11.2) */ |
| 1233 |
if ( !(n->type == TYPE_SET_OF && n->down |
| 1234 |
&& n->down->type == TYPE_OCTET_STRING && !n->down->right)) |
| 1235 |
return gpg_error (GPG_ERR_INV_CMS_OBJ); |
| 1236 |
n = n->down; |
| 1237 |
if (n->off == -1) |
| 1238 |
return gpg_error (GPG_ERR_BUG); |
| 1239 |
|
| 1240 |
*r_digest_len = n->len; |
| 1241 |
*r_digest = xtrymalloc (n->len); |
| 1242 |
if (!*r_digest) |
| 1243 |
return gpg_error (GPG_ERR_ENOMEM); |
| 1244 |
memcpy (*r_digest, si->image + n->off + n->nhdr, n->len); |
| 1245 |
return 0; |
| 1246 |
} |
| 1247 |
|
| 1248 |
|
| 1249 |
/* Return the extension attribute signing time, which may be empty for no |
| 1250 |
signing time available. */ |
| 1251 |
gpg_error_t |
| 1252 |
ksba_cms_get_signing_time (ksba_cms_t cms, int idx, ksba_isotime_t r_sigtime) |
| 1253 |
{ |
| 1254 |
AsnNode nsiginfo, n; |
| 1255 |
struct signer_info_s *si; |
| 1256 |
|
| 1257 |
if (!cms) |
| 1258 |
return gpg_error (GPG_ERR_INV_VALUE); |
| 1259 |
*r_sigtime = 0; |
| 1260 |
if (!cms->signer_info) |
| 1261 |
return gpg_error (GPG_ERR_NO_DATA); |
| 1262 |
if (idx < 0) |
| 1263 |
return gpg_error (GPG_ERR_INV_INDEX); |
| 1264 |
|
| 1265 |
for (si=cms->signer_info; si && idx; si = si->next, idx-- ) |
| 1266 |
; |
| 1267 |
if (!si) |
| 1268 |
return -1; |
| 1269 |
|
| 1270 |
*r_sigtime = 0; |
| 1271 |
nsiginfo = _ksba_asn_find_node (si->root, "SignerInfo.signedAttrs"); |
| 1272 |
if (!nsiginfo) |
| 1273 |
return 0; /* This is okay because signedAttribs are optional. */ |
| 1274 |
|
| 1275 |
n = _ksba_asn_find_type_value (si->image, nsiginfo, 0, |
| 1276 |
oid_signingTime, DIM(oid_signingTime)); |
| 1277 |
if (!n) |
| 1278 |
return 0; /* This is okay because signing time is optional. */ |
| 1279 |
|
| 1280 |
/* check that there is only one */ |
| 1281 |
if (_ksba_asn_find_type_value (si->image, nsiginfo, 1, |
| 1282 |
oid_signingTime, DIM(oid_signingTime))) |
| 1283 |
return gpg_error (GPG_ERR_DUP_VALUE); |
| 1284 |
|
| 1285 |
/* the value is is a SET OF CHOICE but the set must have |
| 1286 |
excactly one CHOICE of generalized or utctime. (rfc5652 11.3) */ |
| 1287 |
if ( !(n->type == TYPE_SET_OF && n->down |
| 1288 |
&& (n->down->type == TYPE_GENERALIZED_TIME |
| 1289 |
|| n->down->type == TYPE_UTC_TIME) |
| 1290 |
&& !n->down->right)) |
| 1291 |
return gpg_error (GPG_ERR_INV_CMS_OBJ); |
| 1292 |
n = n->down; |
| 1293 |
if (n->off == -1) |
| 1294 |
return gpg_error (GPG_ERR_BUG); |
| 1295 |
|
| 1296 |
return _ksba_asntime_to_iso (si->image + n->off + n->nhdr, n->len, |
| 1297 |
n->type == TYPE_UTC_TIME, r_sigtime); |
| 1298 |
} |
| 1299 |
|
| 1300 |
|
| 1301 |
/* Return a list of OIDs stored as signed attributes for the signature |
| 1302 |
number IDX. All the values (OIDs) for the the requested OID REQOID |
| 1303 |
are returned delimited by a linefeed. Caller must free that |
| 1304 |
list. -1 is returned when IDX is larger than the number of |
| 1305 |
signatures, GPG_ERR_No_Data is returned when there is no such |
| 1306 |
attribute for the given signer. */ |
| 1307 |
gpg_error_t |
| 1308 |
ksba_cms_get_sigattr_oids (ksba_cms_t cms, int idx, |
| 1309 |
const char *reqoid, char **r_value) |
| 1310 |
{ |
| 1311 |
gpg_error_t err; |
| 1312 |
AsnNode nsiginfo, n; |
| 1313 |
struct signer_info_s *si; |
| 1314 |
unsigned char *reqoidbuf; |
| 1315 |
size_t reqoidlen; |
| 1316 |
char *retstr = NULL; |
| 1317 |
int i; |
| 1318 |
|
| 1319 |
if (!cms || !r_value) |
| 1320 |
return gpg_error (GPG_ERR_INV_VALUE); |
| 1321 |
if (!cms->signer_info) |
| 1322 |
return gpg_error (GPG_ERR_NO_DATA); |
| 1323 |
if (idx < 0) |
| 1324 |
return gpg_error (GPG_ERR_INV_INDEX); |
| 1325 |
*r_value = NULL; |
| 1326 |
|
| 1327 |
for (si=cms->signer_info; si && idx; si = si->next, idx-- ) |
| 1328 |
; |
| 1329 |
if (!si) |
| 1330 |
return -1; /* no more signers */ |
| 1331 |
|
| 1332 |
nsiginfo = _ksba_asn_find_node (si->root, "SignerInfo.signedAttrs"); |
| 1333 |
if (!nsiginfo) |
| 1334 |
return -1; /* this is okay, because signedAttribs are optional */ |
| 1335 |
|
| 1336 |
err = ksba_oid_from_str (reqoid, &reqoidbuf, &reqoidlen); |
| 1337 |
if(err) |
| 1338 |
return err; |
| 1339 |
|
| 1340 |
for (i=0; (n = _ksba_asn_find_type_value (si->image, nsiginfo, |
| 1341 |
i, reqoidbuf, reqoidlen)); i++) |
| 1342 |
{ |
| 1343 |
char *line, *p; |
| 1344 |
|
| 1345 |
if ( !(n->type == TYPE_SET_OF && n->down |
| 1346 |
&& n->down->type == TYPE_OBJECT_ID && !n->down->right)) |
| 1347 |
{ |
| 1348 |
xfree (reqoidbuf); |
| 1349 |
xfree (retstr); |
| 1350 |
return gpg_error (GPG_ERR_INV_CMS_OBJ); |
| 1351 |
} |
| 1352 |
n = n->down; |
| 1353 |
if (n->off == -1) |
| 1354 |
{ |
| 1355 |
xfree (reqoidbuf); |
| 1356 |
xfree (retstr); |
| 1357 |
return gpg_error (GPG_ERR_BUG); |
| 1358 |
} |
| 1359 |
|
| 1360 |
p = _ksba_oid_node_to_str (si->image, n); |
| 1361 |
if (!p) |
| 1362 |
{ |
| 1363 |
xfree (reqoidbuf); |
| 1364 |
xfree (retstr); |
| 1365 |
return gpg_error (GPG_ERR_INV_CMS_OBJ); |
| 1366 |
} |
| 1367 |
|
| 1368 |
if (!retstr) |
| 1369 |
line = retstr = xtrymalloc (strlen (p) + 2); |
| 1370 |
else |
| 1371 |
{ |
| 1372 |
char *tmp = xtryrealloc (retstr, |
| 1373 |
strlen (retstr) + 1 + strlen (p) + 2); |
| 1374 |
if (!tmp) |
| 1375 |
line = NULL; |
| 1376 |
else |
| 1377 |
{ |
| 1378 |
retstr = tmp; |
| 1379 |
line = stpcpy (retstr + strlen (retstr), "\n"); |
| 1380 |
} |
| 1381 |
} |
| 1382 |
if (!line) |
| 1383 |
{ |
| 1384 |
xfree (reqoidbuf); |
| 1385 |
xfree (retstr); |
| 1386 |
xfree (p); |
| 1387 |
return gpg_error (GPG_ERR_ENOMEM); |
| 1388 |
} |
| 1389 |
strcpy (line, p); |
| 1390 |
xfree (p); |
| 1391 |
} |
| 1392 |
xfree (reqoidbuf); |
| 1393 |
if (!n && !i) |
| 1394 |
return -1; /* no such attribute */ |
| 1395 |
*r_value = retstr; |
| 1396 |
return 0; |
| 1397 |
} |
| 1398 |
|
| 1399 |
|
| 1400 |
/** |
| 1401 |
* ksba_cms_get_sig_val: |
| 1402 |
* @cms: CMS object |
| 1403 |
* @idx: index of signer |
| 1404 |
* |
| 1405 |
* Return the actual signature of signer @idx in a format suitable to |
| 1406 |
* be used as input to Libgcrypt's verification function. The caller |
| 1407 |
* must free the returned string. |
| 1408 |
* |
| 1409 |
* Return value: NULL or a string with a S-Exp. |
| 1410 |
**/ |
| 1411 |
ksba_sexp_t |
| 1412 |
ksba_cms_get_sig_val (ksba_cms_t cms, int idx) |
| 1413 |
{ |
| 1414 |
AsnNode n, n2; |
| 1415 |
gpg_error_t err; |
| 1416 |
ksba_sexp_t string; |
| 1417 |
struct signer_info_s *si; |
| 1418 |
|
| 1419 |
if (!cms) |
| 1420 |
return NULL; |
| 1421 |
if (!cms->signer_info) |
| 1422 |
return NULL; |
| 1423 |
if (idx < 0) |
| 1424 |
return NULL; |
| 1425 |
|
| 1426 |
for (si=cms->signer_info; si && idx; si = si->next, idx-- ) |
| 1427 |
; |
| 1428 |
if (!si) |
| 1429 |
return NULL; |
| 1430 |
|
| 1431 |
n = _ksba_asn_find_node (si->root, "SignerInfo.signatureAlgorithm"); |
| 1432 |
if (!n) |
| 1433 |
return NULL; |
| 1434 |
if (n->off == -1) |
| 1435 |
{ |
| 1436 |
/* fputs ("ksba_cms_get_sig_val problem at node:\n", stderr); */ |
| 1437 |
/* _ksba_asn_node_dump_all (n, stderr); */ |
| 1438 |
return NULL; |
| 1439 |
} |
| 1440 |
|
| 1441 |
n2 = n->right; /* point to the actual value */ |
| 1442 |
err = _ksba_sigval_to_sexp (si->image + n->off, |
| 1443 |
n->nhdr + n->len |
| 1444 |
+ ((!n2||n2->off == -1)? 0:(n2->nhdr+n2->len)), |
| 1445 |
&string); |
| 1446 |
if (err) |
| 1447 |
return NULL; |
| 1448 |
|
| 1449 |
return string; |
| 1450 |
} |
| 1451 |
|
| 1452 |
|
| 1453 |
/* Helper to dump a S-expression. */ |
| 1454 |
#if 0 |
| 1455 |
static void |
| 1456 |
dbg_print_sexp (ksba_const_sexp_t p) |
| 1457 |
{ |
| 1458 |
int level = 0; |
| 1459 |
|
| 1460 |
if (!p) |
| 1461 |
fputs ("[none]", stdout); |
| 1462 |
else |
| 1463 |
{ |
| 1464 |
for (;;) |
| 1465 |
{ |
| 1466 |
if (*p == '(') |
| 1467 |
{ |
| 1468 |
putchar (*p); |
| 1469 |
p++; |
| 1470 |
level++; |
| 1471 |
} |
| 1472 |
else if (*p == ')') |
| 1473 |
{ |
| 1474 |
putchar (*p); |
| 1475 |
p++; |
| 1476 |
if (--level <= 0 ) |
| 1477 |
{ |
| 1478 |
putchar ('\n'); |
| 1479 |
return; |
| 1480 |
} |
| 1481 |
} |
| 1482 |
else if (!digitp (p)) |
| 1483 |
{ |
| 1484 |
fputs ("[invalid s-exp]\n", stdout); |
| 1485 |
return; |
| 1486 |
} |
| 1487 |
else |
| 1488 |
{ |
| 1489 |
const unsigned char *s; |
| 1490 |
char *endp; |
| 1491 |
unsigned long len, n; |
| 1492 |
|
| 1493 |
len = strtoul (p, &endp, 10); |
| 1494 |
p = endp; |
| 1495 |
if (*p != ':') |
| 1496 |
{ |
| 1497 |
fputs ("[invalid s-exp]\n", stdout); |
| 1498 |
return; |
| 1499 |
} |
| 1500 |
p++; |
| 1501 |
for (s=p,n=0; n < len; n++, s++) |
| 1502 |
if ( !((*s >= 'a' && *s <= 'z') |
| 1503 |
|| (*s >= 'A' && *s <= 'Z') |
| 1504 |
|| (*s >= '0' && *s <= '9') |
| 1505 |
|| *s == '-' || *s == '.')) |
| 1506 |
break; |
| 1507 |
if (n < len) |
| 1508 |
{ |
| 1509 |
putchar('#'); |
| 1510 |
for (n=0; n < len; n++, p++) |
| 1511 |
printf ("%02X", *p); |
| 1512 |
putchar('#'); |
| 1513 |
} |
| 1514 |
else |
| 1515 |
{ |
| 1516 |
for (n=0; n < len; n++, p++) |
| 1517 |
putchar (*p); |
| 1518 |
} |
| 1519 |
} |
| 1520 |
} |
| 1521 |
} |
| 1522 |
putchar ('\n'); |
| 1523 |
} |
| 1524 |
#endif /* 0 */ |
| 1525 |
|
| 1526 |
|
| 1527 |
|
| 1528 |
/** |
| 1529 |
* ksba_cms_get_enc_val: |
| 1530 |
* @cms: CMS object |
| 1531 |
* @idx: index of recipient info |
| 1532 |
* |
| 1533 |
* Return the encrypted value (the session key) of recipient @idx in a |
| 1534 |
* format suitable to be used as input to Libgcrypt's decryption |
| 1535 |
* function. The caller must free the returned string. |
| 1536 |
* |
| 1537 |
* Return value: NULL or a string with a S-Exp. |
| 1538 |
**/ |
| 1539 |
ksba_sexp_t |
| 1540 |
ksba_cms_get_enc_val (ksba_cms_t cms, int idx) |
| 1541 |
{ |
| 1542 |
AsnNode root, n, n2; |
| 1543 |
gpg_error_t err; |
| 1544 |
ksba_sexp_t string = NULL; |
| 1545 |
struct value_tree_s *vt; |
| 1546 |
char *keyencralgo = NULL; /* Key encryption algo. */ |
| 1547 |
char *parm = NULL; /* Helper to get the parms of kencralgo. */ |
| 1548 |
size_t parmlen; |
| 1549 |
char *parm2 = NULL; |
| 1550 |
size_t parm2len; |
| 1551 |
char *parm3 = NULL; |
| 1552 |
size_t parm3len; |
| 1553 |
char *keywrapalgo = NULL; /* Key wrap algo. */ |
| 1554 |
char *keyderivealgo = NULL; /* Key derive algo. */ |
| 1555 |
struct tag_info ti; |
| 1556 |
const unsigned char *der; |
| 1557 |
size_t derlen; |
| 1558 |
|
| 1559 |
if (!cms) |
| 1560 |
return NULL; |
| 1561 |
if (!cms->recp_info) |
| 1562 |
return NULL; |
| 1563 |
if (idx < 0) |
| 1564 |
return NULL; |
| 1565 |
|
| 1566 |
for (vt=cms->recp_info; vt && idx; vt=vt->next, idx--) |
| 1567 |
; |
| 1568 |
if (!vt) |
| 1569 |
return NULL; /* No value at this IDX */ |
| 1570 |
|
| 1571 |
/* Find the choice to use. */ |
| 1572 |
root = _ksba_asn_find_node (vt->root, "RecipientInfo.+"); |
| 1573 |
if (!root || !root->name) |
| 1574 |
return NULL; |
| 1575 |
|
| 1576 |
if (!strcmp (root->name, "ktri")) |
| 1577 |
{ |
| 1578 |
n = _ksba_asn_find_node (root, "ktri.keyEncryptionAlgorithm"); |
| 1579 |
if (!n || n->off == -1) |
| 1580 |
return NULL; |
| 1581 |
n2 = n->right; /* point to the actual value */ |
| 1582 |
err = _ksba_encval_to_sexp |
| 1583 |
(vt->image + n->off, |
| 1584 |
n->nhdr + n->len + ((!n2||n2->off == -1)? 0:(n2->nhdr+n2->len)), |
| 1585 |
&string); |
| 1586 |
} |
| 1587 |
else if (!strcmp (root->name, "kari")) |
| 1588 |
{ |
| 1589 |
/* _ksba_asn_node_dump_all (root, stderr); */ |
| 1590 |
|
| 1591 |
/* Get the encrypted key. Result is in (DER,DERLEN) */ |
| 1592 |
n = _ksba_asn_find_node (root, ("kari..recipientEncryptedKeys" |
| 1593 |
"..encryptedKey")); |
| 1594 |
if (!n || n->off == -1) |
| 1595 |
{ |
| 1596 |
err = gpg_error (GPG_ERR_INV_KEYINFO); |
| 1597 |
goto leave; |
| 1598 |
} |
| 1599 |
|
| 1600 |
der = vt->image + n->off; |
| 1601 |
derlen = n->nhdr + n->len; |
| 1602 |
err = parse_octet_string (&der, &derlen, &ti); |
| 1603 |
if (err) |
| 1604 |
goto leave; |
| 1605 |
derlen = ti.length; |
| 1606 |
/* gpgrt_log_printhex (der, derlen, "%s: encryptedKey", __func__); */ |
| 1607 |
|
| 1608 |
/* Get the KEK algos. */ |
| 1609 |
n = _ksba_asn_find_node (root, "kari..keyEncryptionAlgorithm"); |
| 1610 |
if (!n || n->off == -1) |
| 1611 |
{ |
| 1612 |
err = gpg_error (GPG_ERR_INV_KEYINFO); |
| 1613 |
goto leave; |
| 1614 |
} |
| 1615 |
err = _ksba_parse_algorithm_identifier2 (vt->image + n->off, |
| 1616 |
n->nhdr + n->len, NULL, |
| 1617 |
&keyencralgo, &parm, &parmlen); |
| 1618 |
if (err) |
| 1619 |
goto leave; |
| 1620 |
if (!parm) |
| 1621 |
{ |
| 1622 |
err = gpg_error (GPG_ERR_INV_KEYINFO); |
| 1623 |
goto leave; |
| 1624 |
} |
| 1625 |
err = _ksba_parse_algorithm_identifier (parm, parmlen,NULL, &keywrapalgo); |
| 1626 |
if (err) |
| 1627 |
goto leave; |
| 1628 |
|
| 1629 |
/* gpgrt_log_debug ("%s: keyencralgo='%s'\n", __func__, keyencralgo); */ |
| 1630 |
/* gpgrt_log_debug ("%s: keywrapalgo='%s'\n", __func__, keywrapalgo); */ |
| 1631 |
|
| 1632 |
/* Get the ephemeral public key. */ |
| 1633 |
n = _ksba_asn_find_node (root, "kari..originator..originatorKey"); |
| 1634 |
if (!n || n->off == -1) |
| 1635 |
{ |
| 1636 |
err = gpg_error (GPG_ERR_INV_KEYINFO); |
| 1637 |
goto leave; |
| 1638 |
} |
| 1639 |
err = _ksba_encval_kari_to_sexp (vt->image + n->off, n->nhdr + n->len, |
| 1640 |
keyencralgo, keywrapalgo, der, derlen, |
| 1641 |
&string); |
| 1642 |
if (err) |
| 1643 |
goto leave; |
| 1644 |
|
| 1645 |
/* gpgrt_log_debug ("%s: encryptedKey:\n", __func__); */ |
| 1646 |
/* dbg_print_sexp (string); */ |
| 1647 |
} |
| 1648 |
else if (!strcmp (root->name, "kekri")) |
| 1649 |
return NULL; /*GPG_ERR_UNSUPPORTED_CMS_OBJ*/ |
| 1650 |
else if (!strcmp (root->name, "pwri")) |
| 1651 |
{ |
| 1652 |
/* _ksba_asn_node_dump_all (root, stderr); */ |
| 1653 |
|
| 1654 |
n = _ksba_asn_find_node (root, "pwri..keyEncryptionAlgorithm"); |
| 1655 |
if (!n || n->off == -1) |
| 1656 |
{ |
| 1657 |
err = gpg_error (GPG_ERR_INV_KEYINFO); |
| 1658 |
goto leave; |
| 1659 |
} |
| 1660 |
err = _ksba_parse_algorithm_identifier2 (vt->image + n->off, |
| 1661 |
n->nhdr + n->len, NULL, |
| 1662 |
&keyencralgo, &parm, &parmlen); |
| 1663 |
if (err) |
| 1664 |
goto leave; |
| 1665 |
if (strcmp (keyencralgo, "1.2.840.113549.1.9.16.3.9")) |
| 1666 |
{ |
| 1667 |
/* pwri requires this and only this OID. */ |
| 1668 |
err = gpg_error (GPG_ERR_INV_CMS_OBJ); |
| 1669 |
goto leave; |
| 1670 |
} |
| 1671 |
if (!parm) |
| 1672 |
{ |
| 1673 |
err = gpg_error (GPG_ERR_INV_KEYINFO); |
| 1674 |
goto leave; |
| 1675 |
} |
| 1676 |
/* gpgrt_log_printhex (parm, parmlen, "parms"); */ |
| 1677 |
err = _ksba_parse_algorithm_identifier2 (parm, parmlen, NULL, |
| 1678 |
&keywrapalgo, &parm2, &parm2len); |
| 1679 |
if (err) |
| 1680 |
goto leave; |
| 1681 |
|
| 1682 |
/* gpgrt_log_debug ("%s: keywrapalgo='%s'\n", __func__, keywrapalgo); */ |
| 1683 |
/* gpgrt_log_printhex (parm2, parm2len, "parm:"); */ |
| 1684 |
|
| 1685 |
n = _ksba_asn_find_node (root, "pwri..keyDerivationAlgorithm"); |
| 1686 |
if (!n || n->off == -1) |
| 1687 |
{ |
| 1688 |
/* Not found but that is okay becuase it is optional. */ |
| 1689 |
} |
| 1690 |
else |
| 1691 |
{ |
| 1692 |
err = _ksba_parse_algorithm_identifier3 (vt->image + n->off, |
| 1693 |
n->nhdr + n->len, 0xa0, NULL, |
| 1694 |
&keyderivealgo, |
| 1695 |
&parm3, &parm3len, NULL); |
| 1696 |
if (err) |
| 1697 |
goto leave; |
| 1698 |
} |
| 1699 |
|
| 1700 |
n = _ksba_asn_find_node (root, "pwri..encryptedKey"); |
| 1701 |
if (!n || n->off == -1) |
| 1702 |
{ |
| 1703 |
err = gpg_error (GPG_ERR_INV_KEYINFO); |
| 1704 |
goto leave; |
| 1705 |
} |
| 1706 |
der = vt->image + n->off; |
| 1707 |
derlen = n->nhdr + n->len; |
| 1708 |
err = parse_octet_string (&der, &derlen, &ti); |
| 1709 |
if (err) |
| 1710 |
goto leave; |
| 1711 |
derlen = ti.length; |
| 1712 |
/* gpgrt_log_printhex (der, derlen, "encryptedKey:"); */ |
| 1713 |
|
| 1714 |
/* Build the s-expression: |
| 1715 |
* (enc-val |
| 1716 |
* (pwri |
| 1717 |
* (derive-algo <oid>) --| both are optional |
| 1718 |
* (derive-parm <der>) --| |
| 1719 |
* (encr-algo <oid>) |
| 1720 |
* (encr-parm <iv>) |
| 1721 |
* (encr-key <key>))) -- this is the encrypted session key |
| 1722 |
*/ |
| 1723 |
{ |
| 1724 |
struct stringbuf sb; |
| 1725 |
|
| 1726 |
init_stringbuf (&sb, 200); |
| 1727 |
put_stringbuf (&sb, "(7:enc-val(4:pwri"); |
| 1728 |
if (keyderivealgo && parm3) |
| 1729 |
{ |
| 1730 |
put_stringbuf (&sb, "(11:derive-algo"); |
| 1731 |
put_stringbuf_sexp (&sb, keyderivealgo); |
| 1732 |
put_stringbuf (&sb, ")(11:derive-parm"); |
| 1733 |
put_stringbuf_mem_sexp (&sb, parm3, parm3len); |
| 1734 |
put_stringbuf (&sb, ")"); |
| 1735 |
} |
| 1736 |
put_stringbuf (&sb, "(9:encr-algo"); |
| 1737 |
put_stringbuf_sexp (&sb, keywrapalgo); |
| 1738 |
put_stringbuf (&sb, ")(9:encr-parm"); |
| 1739 |
put_stringbuf_mem_sexp (&sb, parm2, parm2len); |
| 1740 |
put_stringbuf (&sb, ")(8:encr-key"); |
| 1741 |
put_stringbuf_mem_sexp (&sb, der, derlen); |
| 1742 |
put_stringbuf (&sb, ")))"); |
| 1743 |
|
| 1744 |
string = get_stringbuf (&sb); |
| 1745 |
if (!string) |
| 1746 |
err = gpg_error_from_syserror (); |
| 1747 |
} |
| 1748 |
|
| 1749 |
} |
| 1750 |
else |
| 1751 |
return NULL; /*GPG_ERR_INV_CMS_OBJ*/ |
| 1752 |
|
| 1753 |
leave: |
| 1754 |
xfree (keyencralgo); |
| 1755 |
xfree (keywrapalgo); |
| 1756 |
xfree (keyderivealgo); |
| 1757 |
xfree (parm); |
| 1758 |
xfree (parm2); |
| 1759 |
xfree (parm3); |
| 1760 |
if (err) |
| 1761 |
{ |
| 1762 |
/* gpgrt_log_debug ("%s: error: %s\n", __func__, gpg_strerror (err)); */ |
| 1763 |
return NULL; |
| 1764 |
} |
| 1765 |
|
| 1766 |
return string; |
| 1767 |
} |
| 1768 |
|
| 1769 |
|
| 1770 |
|
| 1771 |
|
| 1772 |
|
| 1773 |
/* Provide a hash function so that we are able to hash the data */ |
| 1774 |
void |
| 1775 |
ksba_cms_set_hash_function (ksba_cms_t cms, |
| 1776 |
void (*hash_fnc)(void *, const void *, size_t), |
| 1777 |
void *hash_fnc_arg) |
| 1778 |
{ |
| 1779 |
if (cms) |
| 1780 |
{ |
| 1781 |
cms->hash_fnc = hash_fnc; |
| 1782 |
cms->hash_fnc_arg = hash_fnc_arg; |
| 1783 |
} |
| 1784 |
} |
| 1785 |
|
| 1786 |
|
| 1787 |
/* hash the signed attributes of the given signer */ |
| 1788 |
gpg_error_t |
| 1789 |
ksba_cms_hash_signed_attrs (ksba_cms_t cms, int idx) |
| 1790 |
{ |
| 1791 |
AsnNode n; |
| 1792 |
struct signer_info_s *si; |
| 1793 |
|
| 1794 |
if (!cms) |
| 1795 |
return gpg_error (GPG_ERR_INV_VALUE); |
| 1796 |
if (!cms->hash_fnc) |
| 1797 |
return gpg_error (GPG_ERR_MISSING_ACTION); |
| 1798 |
if (idx < 0) |
| 1799 |
return -1; |
| 1800 |
|
| 1801 |
for (si=cms->signer_info; si && idx; si = si->next, idx-- ) |
| 1802 |
; |
| 1803 |
if (!si) |
| 1804 |
return -1; |
| 1805 |
|
| 1806 |
n = _ksba_asn_find_node (si->root, "SignerInfo.signedAttrs"); |
| 1807 |
if (!n || n->off == -1) |
| 1808 |
return gpg_error (GPG_ERR_NO_VALUE); |
| 1809 |
|
| 1810 |
/* We don't hash the implicit tag [0] but a SET tag */ |
| 1811 |
cms->hash_fnc (cms->hash_fnc_arg, "\x31", 1); |
| 1812 |
cms->hash_fnc (cms->hash_fnc_arg, |
| 1813 |
si->image + n->off + 1, n->nhdr + n->len - 1); |
| 1814 |
|
| 1815 |
return 0; |
| 1816 |
} |
| 1817 |
|
| 1818 |
|
| 1819 |
/* |
| 1820 |
Code to create CMS structures |
| 1821 |
*/ |
| 1822 |
|
| 1823 |
|
| 1824 |
/** |
| 1825 |
* ksba_cms_set_content_type: |
| 1826 |
* @cms: A CMS object |
| 1827 |
* @what: 0 for content type, 1 for inner content type |
| 1828 |
* @type: Type constant |
| 1829 |
* |
| 1830 |
* Set the content type used for build operations. This should be the |
| 1831 |
* first operation before starting to create a CMS message. |
| 1832 |
* |
| 1833 |
* Return value: 0 on success or an error code |
| 1834 |
**/ |
| 1835 |
gpg_error_t |
| 1836 |
ksba_cms_set_content_type (ksba_cms_t cms, int what, ksba_content_type_t type) |
| 1837 |
{ |
| 1838 |
int i; |
| 1839 |
char *oid; |
| 1840 |
|
| 1841 |
if (!cms || what < 0 || what > 1 ) |
| 1842 |
return gpg_error (GPG_ERR_INV_VALUE); |
| 1843 |
|
| 1844 |
for (i=0; content_handlers[i].oid; i++) |
| 1845 |
{ |
| 1846 |
if (content_handlers[i].ct == type) |
| 1847 |
break; |
| 1848 |
} |
| 1849 |
if (!content_handlers[i].oid) |
| 1850 |
return gpg_error (GPG_ERR_UNKNOWN_CMS_OBJ); |
| 1851 |
if (!content_handlers[i].build_handler) |
| 1852 |
return gpg_error (GPG_ERR_UNSUPPORTED_CMS_OBJ); |
| 1853 |
oid = xtrystrdup (content_handlers[i].oid); |
| 1854 |
if (!oid) |
| 1855 |
return gpg_error (GPG_ERR_ENOMEM); |
| 1856 |
|
| 1857 |
if (!what) |
| 1858 |
{ |
| 1859 |
cms->content.oid = oid; |
| 1860 |
cms->content.ct = content_handlers[i].ct; |
| 1861 |
cms->content.handler = content_handlers[i].build_handler; |
| 1862 |
cms->auth_mode = (type == KSBA_CT_AUTHENVELOPED_DATA); |
| 1863 |
} |
| 1864 |
else |
| 1865 |
{ |
| 1866 |
cms->inner_cont_oid = oid; |
| 1867 |
} |
| 1868 |
|
| 1869 |
return 0; |
| 1870 |
} |
| 1871 |
|
| 1872 |
|
| 1873 |
/** |
| 1874 |
* ksba_cms_add_digest_algo: |
| 1875 |
* @cms: A CMS object |
| 1876 |
* @oid: A stringified object OID describing the hash algorithm |
| 1877 |
* |
| 1878 |
* Set the algorithm to be used for creating the hash. Note, that we |
| 1879 |
* currently can't do a per-signer hash. |
| 1880 |
* |
| 1881 |
* Return value: 0 on success or an error code |
| 1882 |
**/ |
| 1883 |
gpg_error_t |
| 1884 |
ksba_cms_add_digest_algo (ksba_cms_t cms, const char *oid) |
| 1885 |
{ |
| 1886 |
struct oidlist_s *ol; |
| 1887 |
|
| 1888 |
if (!cms || !oid) |
| 1889 |
return gpg_error (GPG_ERR_INV_VALUE); |
| 1890 |
|
| 1891 |
ol = xtrymalloc (sizeof *ol); |
| 1892 |
if (!ol) |
| 1893 |
return gpg_error (GPG_ERR_ENOMEM); |
| 1894 |
|
| 1895 |
ol->oid = xtrystrdup (oid); |
| 1896 |
if (!ol->oid) |
| 1897 |
{ |
| 1898 |
xfree (ol); |
| 1899 |
return gpg_error (GPG_ERR_ENOMEM); |
| 1900 |
} |
| 1901 |
ol->next = cms->digest_algos; |
| 1902 |
cms->digest_algos = ol; |
| 1903 |
return 0; |
| 1904 |
} |
| 1905 |
|
| 1906 |
|
| 1907 |
/** |
| 1908 |
* ksba_cms_add_signer: |
| 1909 |
* @cms: A CMS object |
| 1910 |
* @cert: A certificate used to describe the signer. |
| 1911 |
* |
| 1912 |
* This functions starts assembly of a new signed data content or adds |
| 1913 |
* another signer to the list of signers. |
| 1914 |
* |
| 1915 |
* Return value: 0 on success or an error code. |
| 1916 |
**/ |
| 1917 |
gpg_error_t |
| 1918 |
ksba_cms_add_signer (ksba_cms_t cms, ksba_cert_t cert) |
| 1919 |
{ |
| 1920 |
struct certlist_s *cl, *cl2; |
| 1921 |
|
| 1922 |
if (!cms) |
| 1923 |
return gpg_error (GPG_ERR_INV_VALUE); |
| 1924 |
|
| 1925 |
cl = xtrycalloc (1,sizeof *cl); |
| 1926 |
if (!cl) |
| 1927 |
return gpg_error (GPG_ERR_ENOMEM); |
| 1928 |
|
| 1929 |
ksba_cert_ref (cert); |
| 1930 |
cl->cert = cert; |
| 1931 |
if (!cms->cert_list) |
| 1932 |
cms->cert_list = cl; |
| 1933 |
else |
| 1934 |
{ |
| 1935 |
for (cl2=cms->cert_list; cl2->next; cl2 = cl2->next) |
| 1936 |
; |
| 1937 |
cl2->next = cl; |
| 1938 |
} |
| 1939 |
return 0; |
| 1940 |
} |
| 1941 |
|
| 1942 |
/** |
| 1943 |
* ksba_cms_add_cert: |
| 1944 |
* @cms: A CMS object |
| 1945 |
* @cert: A certificate to be send along with the signed data. |
| 1946 |
* |
| 1947 |
* This functions adds a certificate to the list of certificates send |
| 1948 |
* along with the signed data. Using this is optional but it is very |
| 1949 |
* common to include at least the certificate of the signer it self. |
| 1950 |
* |
| 1951 |
* Return value: 0 on success or an error code. |
| 1952 |
**/ |
| 1953 |
gpg_error_t |
| 1954 |
ksba_cms_add_cert (ksba_cms_t cms, ksba_cert_t cert) |
| 1955 |
{ |
| 1956 |
struct certlist_s *cl; |
| 1957 |
|
| 1958 |
if (!cms || !cert) |
| 1959 |
return gpg_error (GPG_ERR_INV_VALUE); |
| 1960 |
|
| 1961 |
/* first check whether this is a duplicate. */ |
| 1962 |
for (cl = cms->cert_info_list; cl; cl = cl->next) |
| 1963 |
{ |
| 1964 |
if (!_ksba_cert_cmp (cert, cl->cert)) |
| 1965 |
return 0; /* duplicate */ |
| 1966 |
} |
| 1967 |
|
| 1968 |
/* Okay, add it. */ |
| 1969 |
cl = xtrycalloc (1,sizeof *cl); |
| 1970 |
if (!cl) |
| 1971 |
return gpg_error (GPG_ERR_ENOMEM); |
| 1972 |
|
| 1973 |
ksba_cert_ref (cert); |
| 1974 |
cl->cert = cert; |
| 1975 |
cl->next = cms->cert_info_list; |
| 1976 |
cms->cert_info_list = cl; |
| 1977 |
return 0; |
| 1978 |
} |
| 1979 |
|
| 1980 |
|
| 1981 |
/* Add an S/MIME capability as an extended attribute to the message. |
| 1982 |
This function is to be called for each capability in turn. The |
| 1983 |
first capability added will receive the highest priority. CMS is |
| 1984 |
the context, OID the object identifier of the capability and if DER |
| 1985 |
is not NULL it is used as the DER-encoded parameters of the |
| 1986 |
capability; the length of that DER object is given in DERLEN. |
| 1987 |
DERLEN should be 0 if DER is NULL. |
| 1988 |
|
| 1989 |
The function returns 0 on success or an error code. |
| 1990 |
*/ |
| 1991 |
gpg_error_t |
| 1992 |
ksba_cms_add_smime_capability (ksba_cms_t cms, const char *oid, |
| 1993 |
const unsigned char *der, size_t derlen) |
| 1994 |
{ |
| 1995 |
gpg_error_t err; |
| 1996 |
struct oidparmlist_s *opl, *opl2; |
| 1997 |
|
| 1998 |
if (!cms || !oid) |
| 1999 |
return gpg_error (GPG_ERR_INV_VALUE); |
| 2000 |
|
| 2001 |
if (!der) |
| 2002 |
derlen = 0; |
| 2003 |
|
| 2004 |
opl = xtrymalloc (sizeof *opl + derlen - 1); |
| 2005 |
if (!opl) |
| 2006 |
return gpg_error_from_errno (errno); |
| 2007 |
opl->unprotected = 0; |
| 2008 |
opl->signeridx = 0; |
| 2009 |
opl->next = NULL; |
| 2010 |
opl->oid = xtrystrdup (oid); |
| 2011 |
if (!opl->oid) |
| 2012 |
{ |
| 2013 |
err = gpg_error_from_errno (errno); |
| 2014 |
xfree (opl); |
| 2015 |
return err; |
| 2016 |
} |
| 2017 |
opl->parmlen = derlen; |
| 2018 |
if (der) |
| 2019 |
memcpy (opl->parm, der, derlen); |
| 2020 |
|
| 2021 |
/* Append it to maintain the desired order. */ |
| 2022 |
if (!cms->capability_list) |
| 2023 |
cms->capability_list = opl; |
| 2024 |
else |
| 2025 |
{ |
| 2026 |
for (opl2=cms->capability_list; opl2->next; opl2 = opl2->next) |
| 2027 |
; |
| 2028 |
opl2->next = opl; |
| 2029 |
} |
| 2030 |
|
| 2031 |
return 0; |
| 2032 |
} |
| 2033 |
|
| 2034 |
/* Add an arbitrary attribute to the message. CMS is the context, OID |
| 2035 |
* the object identifier of the attribute and (DER,DERLEN) is the |
| 2036 |
* DER-encoded content which is put into a SET. Thus DER may be a |
| 2037 |
* straight concatenation of ASN.1 objects w/o the outer container. |
| 2038 |
* |
| 2039 |
* The attribute is store stored for the signer with IDX or with an |
| 2040 |
* IDX of -1 for all signers. The index of a signer is determined by |
| 2041 |
* the sequence of ksba_cms_add_signer() calls; the first signer has |
| 2042 |
* the index 0. |
| 2043 |
* |
| 2044 |
* If UNPROTECTED is set the attribute will be stored in the unsigned |
| 2045 |
* section. |
| 2046 |
* |
| 2047 |
* No merging of attributes is done, thus the caller should not call it |
| 2048 |
* twice with the same OID. Note that this function is a generalized |
| 2049 |
* version of ksba_cms_add_smime_capability. |
| 2050 |
* |
| 2051 |
* The function returns 0 on success or an error code. |
| 2052 |
*/ |
| 2053 |
gpg_error_t |
| 2054 |
ksba_cms_add_attribute (ksba_cms_t cms, int idx, |
| 2055 |
const char *oid, int unprotected, |
| 2056 |
const unsigned char *der, size_t derlen) |
| 2057 |
{ |
| 2058 |
gpg_error_t err; |
| 2059 |
struct oidparmlist_s *opl; |
| 2060 |
|
| 2061 |
if (!cms || !oid || unprotected < 0 || unprotected > 1 || !der || !derlen) |
| 2062 |
return gpg_error (GPG_ERR_INV_VALUE); |
| 2063 |
if (idx < -1) |
| 2064 |
return gpg_error (GPG_ERR_INV_INDEX); |
| 2065 |
else if (idx >= 0) |
| 2066 |
{ |
| 2067 |
struct certlist_s *cl; |
| 2068 |
int i; |
| 2069 |
|
| 2070 |
for (i=0, cl = cms->cert_list; cl; cl = cl->next, i++) |
| 2071 |
if (i == idx) |
| 2072 |
break; |
| 2073 |
if (!cl) |
| 2074 |
return gpg_error (GPG_ERR_INV_INDEX); |
| 2075 |
} |
| 2076 |
|
| 2077 |
opl = xtrymalloc (sizeof *opl + derlen - 1); |
| 2078 |
if (!opl) |
| 2079 |
return gpg_error_from_syserror (); |
| 2080 |
opl->unprotected = unprotected; |
| 2081 |
opl->signeridx = idx; |
| 2082 |
opl->oid = xtrystrdup (oid); |
| 2083 |
if (!opl->oid) |
| 2084 |
{ |
| 2085 |
err = gpg_error_from_syserror (); |
| 2086 |
xfree (opl); |
| 2087 |
return err; |
| 2088 |
} |
| 2089 |
opl->parmlen = derlen; |
| 2090 |
memcpy (opl->parm, der, derlen); |
| 2091 |
|
| 2092 |
opl->next = cms->attribute_list; |
| 2093 |
cms->attribute_list = opl; |
| 2094 |
|
| 2095 |
return 0; |
| 2096 |
} |
| 2097 |
|
| 2098 |
|
| 2099 |
/* If CMS is used for signed data, this function sets the message |
| 2100 |
* digest (DIGEST,DIGEST_LEN) into the signedAttributes of the signer |
| 2101 |
* with the index IDX. That index of the signer is determined by the |
| 2102 |
* sequence of ksba_cms_add_signer calls; the first signer has the |
| 2103 |
* index 0. CMS is the usual context. This function is to be used |
| 2104 |
* when the hash value of the data has been computed and before the |
| 2105 |
* create function requests the sign operation. |
| 2106 |
* |
| 2107 |
* If CMS is used for AUTHENVELOPEDDATA this function sets the |
| 2108 |
* authentication tag or MAC to (DIGEST,DIGEST_LEN). IDX must be 0 in |
| 2109 |
* this case. The function is to be used when the build function |
| 2110 |
* stopped with KSBA_SR_NEED_SIG. |
| 2111 |
* |
| 2112 |
* Return value: 0 on success or an error code */ |
| 2113 |
gpg_error_t |
| 2114 |
ksba_cms_set_message_digest (ksba_cms_t cms, int idx, |
| 2115 |
const unsigned char *digest, size_t digest_len) |
| 2116 |
{ |
| 2117 |
struct certlist_s *cl; |
| 2118 |
|
| 2119 |
if (!cms || !digest) |
| 2120 |
return gpg_error (GPG_ERR_INV_VALUE); |
| 2121 |
|
| 2122 |
/* Special processing for AUTHENVELOPEDDATA to set the MAC/authtag. */ |
| 2123 |
if (cms->content.ct == KSBA_CT_AUTHENVELOPED_DATA) |
| 2124 |
{ |
| 2125 |
/* (1024 is just an arbitrary value to catch a faulty caller). */ |
| 2126 |
if (!digest_len || digest_len > 1024) |
| 2127 |
return gpg_error (GPG_ERR_INV_VALUE); |
| 2128 |
if (idx != 0) |
| 2129 |
return gpg_error (GPG_ERR_INV_INDEX); |
| 2130 |
|
| 2131 |
xfree (cms->authdata.mac); |
| 2132 |
cms->authdata.mac_len = digest_len; |
| 2133 |
cms->authdata.mac = xtrymalloc (digest_len); |
| 2134 |
if (!cms->authdata.mac) |
| 2135 |
return gpg_error_from_syserror (); |
| 2136 |
memcpy (cms->authdata.mac, digest, digest_len); |
| 2137 |
|
| 2138 |
return 0; |
| 2139 |
} |
| 2140 |
|
| 2141 |
/* Standard processing for signed data. */ |
| 2142 |
if (!digest_len || digest_len > DIM(cl->msg_digest)) |
| 2143 |
return gpg_error (GPG_ERR_INV_VALUE); |
| 2144 |
if (idx < 0) |
| 2145 |
return gpg_error (GPG_ERR_INV_INDEX); |
| 2146 |
|
| 2147 |
for (cl=cms->cert_list; cl && idx; cl = cl->next, idx--) |
| 2148 |
; |
| 2149 |
if (!cl) |
| 2150 |
return gpg_error (GPG_ERR_INV_INDEX); /* no certificate to store it */ |
| 2151 |
cl->msg_digest_len = digest_len; |
| 2152 |
memcpy (cl->msg_digest, digest, digest_len); |
| 2153 |
return 0; |
| 2154 |
} |
| 2155 |
|
| 2156 |
/** |
| 2157 |
* ksba_cms_set_signing_time: |
| 2158 |
* @cms: A CMS object |
| 2159 |
* @idx: The index of the signer |
| 2160 |
* @sigtime: a time or an empty value to use the current time |
| 2161 |
* |
| 2162 |
* Set a signing time into the signedAttributes of the signer with |
| 2163 |
* the index IDX. The index of a signer is determined by the sequence |
| 2164 |
* of ksba_cms_add_signer() calls; the first signer has the index 0. |
| 2165 |
* |
| 2166 |
* Return value: 0 on success or an error code |
| 2167 |
**/ |
| 2168 |
gpg_error_t |
| 2169 |
ksba_cms_set_signing_time (ksba_cms_t cms, int idx, const ksba_isotime_t sigtime) |
| 2170 |
{ |
| 2171 |
struct certlist_s *cl; |
| 2172 |
|
| 2173 |
if (!cms) |
| 2174 |
return gpg_error (GPG_ERR_INV_VALUE); |
| 2175 |
if (idx < 0) |
| 2176 |
return gpg_error (GPG_ERR_INV_INDEX); |
| 2177 |
|
| 2178 |
for (cl=cms->cert_list; cl && idx; cl = cl->next, idx--) |
| 2179 |
; |
| 2180 |
if (!cl) |
| 2181 |
return gpg_error (GPG_ERR_INV_INDEX); /* no certificate to store it */ |
| 2182 |
|
| 2183 |
/* Fixme: We might want to check the validity of the passed time |
| 2184 |
string. */ |
| 2185 |
if (!*sigtime) |
| 2186 |
_ksba_current_time (cl->signing_time); |
| 2187 |
else |
| 2188 |
_ksba_copy_time (cl->signing_time, sigtime); |
| 2189 |
return 0; |
| 2190 |
} |
| 2191 |
|
| 2192 |
|
| 2193 |
/* Set the signature value as a canonical encoded s-expression. |
| 2194 |
* |
| 2195 |
* r_sig = (sig-val |
| 2196 |
* (<algo> |
| 2197 |
* (<param_name1> <mpi>) |
| 2198 |
* ... |
| 2199 |
* (<param_namen> <mpi>) |
| 2200 |
* )) |
| 2201 |
* |
| 2202 |
* <algo> must be given as a stringified OID or the special string |
| 2203 |
* "rsa". For ECC <algo> must either be "ecdsa" or the OID matching the used |
| 2204 |
* hash algorithm; the expected parameters are "r" and "s". |
| 2205 |
* |
| 2206 |
* Note that IDX is only used for consistency checks. |
| 2207 |
*/ |
| 2208 |
gpg_error_t |
| 2209 |
ksba_cms_set_sig_val (ksba_cms_t cms, int idx, ksba_const_sexp_t sigval) |
| 2210 |
{ |
| 2211 |
gpg_error_t err; |
| 2212 |
unsigned long n, namelen; |
| 2213 |
struct sig_val_s *sv, **sv_tail; |
| 2214 |
const unsigned char *s, *endp, *name; |
| 2215 |
int ecc; /* True for ECC algos. */ |
| 2216 |
int i; |
| 2217 |
|
| 2218 |
if (!cms) |
| 2219 |
return gpg_error (GPG_ERR_INV_VALUE); |
| 2220 |
if (idx < 0) |
| 2221 |
return gpg_error (GPG_ERR_INV_INDEX); /* only one signer for now */ |
| 2222 |
|
| 2223 |
/* log_sexp ("sigval:", sigval); */ |
| 2224 |
s = sigval; |
| 2225 |
if (*s != '(') |
| 2226 |
return gpg_error (GPG_ERR_INV_SEXP); |
| 2227 |
s++; |
| 2228 |
|
| 2229 |
for (i=0, sv_tail=&cms->sig_val; *sv_tail; sv_tail=&(*sv_tail)->next, i++) |
| 2230 |
; |
| 2231 |
if (i != idx) |
| 2232 |
return gpg_error (GPG_ERR_INV_INDEX); |
| 2233 |
|
| 2234 |
if (!(n = snext (&s))) |
| 2235 |
return gpg_error (GPG_ERR_INV_SEXP); |
| 2236 |
if (!smatch (&s, 7, "sig-val")) |
| 2237 |
return gpg_error (GPG_ERR_UNKNOWN_SEXP); |
| 2238 |
if (*s != '(') |
| 2239 |
return gpg_error (digitp (s)? GPG_ERR_UNKNOWN_SEXP : GPG_ERR_INV_SEXP); |
| 2240 |
s++; |
| 2241 |
|
| 2242 |
/* Break out the algorithm ID. */ |
| 2243 |
if (!(n = snext (&s))) |
| 2244 |
return gpg_error (GPG_ERR_INV_SEXP); |
| 2245 |
|
| 2246 |
sv = xtrycalloc (1, sizeof *sv); |
| 2247 |
if (!sv) |
| 2248 |
return gpg_error (GPG_ERR_ENOMEM); |
| 2249 |
|
| 2250 |
if (n==3 && s[0] == 'r' && s[1] == 's' && s[2] == 'a') |
| 2251 |
{ |
| 2252 |
sv->algo = xtrystrdup ("1.2.840.113549.1.1.1"); /* rsa */ |
| 2253 |
if (!sv->algo) |
| 2254 |
{ |
| 2255 |
xfree (sv); |
| 2256 |
return gpg_error (GPG_ERR_ENOMEM); |
| 2257 |
} |
| 2258 |
} |
| 2259 |
else if (n==5 && !memcmp (s, "ecdsa", 5)) |
| 2260 |
{ |
| 2261 |
/* Use a placeholder for later fixup. */ |
| 2262 |
sv->algo = xtrystrdup ("ecdsa"); |
| 2263 |
if (!sv->algo) |
| 2264 |
{ |
| 2265 |
xfree (sv); |
| 2266 |
return gpg_error (GPG_ERR_ENOMEM); |
| 2267 |
} |
| 2268 |
} |
| 2269 |
else |
| 2270 |
{ |
| 2271 |
sv->algo = xtrymalloc (n+1); |
| 2272 |
if (!sv->algo) |
| 2273 |
{ |
| 2274 |
xfree (sv); |
| 2275 |
return gpg_error (GPG_ERR_ENOMEM); |
| 2276 |
} |
| 2277 |
memcpy (sv->algo, s, n); |
| 2278 |
sv->algo[n] = 0; |
| 2279 |
} |
| 2280 |
s += n; |
| 2281 |
|
| 2282 |
ecc = (!strcmp (sv->algo, "ecdsa") /* placeholder */ |
| 2283 |
|| !strcmp (sv->algo, "1.2.840.10045.4.3.2") /* ecdsa-with-SHA256 */ |
| 2284 |
|| !strcmp (sv->algo, "1.2.840.10045.4.3.3") /* ecdsa-with-SHA384 */ |
| 2285 |
|| !strcmp (sv->algo, "1.2.840.10045.4.3.4") /* ecdsa-with-SHA512 */ |
| 2286 |
); |
| 2287 |
|
| 2288 |
xfree (sv->value); sv->value = NULL; |
| 2289 |
xfree (sv->ecc.r); sv->ecc.r = NULL; |
| 2290 |
|
| 2291 |
while (*s == '(') |
| 2292 |
{ |
| 2293 |
s++; |
| 2294 |
n = strtoul (s, (char**)&endp, 10); |
| 2295 |
s = endp; |
| 2296 |
if (!n || *s != ':') |
| 2297 |
{ |
| 2298 |
err = gpg_error (GPG_ERR_INV_SEXP); |
| 2299 |
goto leave; |
| 2300 |
} |
| 2301 |
s++; |
| 2302 |
name = s; |
| 2303 |
namelen = n; |
| 2304 |
s += n; |
| 2305 |
|
| 2306 |
if (!digitp(s)) |
| 2307 |
{ |
| 2308 |
err = gpg_error (GPG_ERR_UNKNOWN_SEXP); /* or invalid sexp */ |
| 2309 |
goto leave; |
| 2310 |
} |
| 2311 |
n = strtoul (s, (char**)&endp, 10); |
| 2312 |
s = endp; |
| 2313 |
if (!n || *s != ':') |
| 2314 |
{ |
| 2315 |
err = gpg_error (GPG_ERR_INV_SEXP); |
| 2316 |
goto leave; |
| 2317 |
} |
| 2318 |
s++; |
| 2319 |
|
| 2320 |
if (namelen == 1 && *name == 's') |
| 2321 |
{ |
| 2322 |
/* Store the "main" parameter into value. */ |
| 2323 |
xfree (sv->value); |
| 2324 |
sv->value = xtrymalloc (n); |
| 2325 |
if (!sv->value) |
| 2326 |
{ |
| 2327 |
err = gpg_error_from_syserror (); |
| 2328 |
goto leave; |
| 2329 |
} |
| 2330 |
memcpy (sv->value, s, n); |
| 2331 |
sv->valuelen = n; |
| 2332 |
} |
| 2333 |
else if (ecc && namelen == 1 && *name == 'r') |
| 2334 |
{ |
| 2335 |
xfree (sv->ecc.r); |
| 2336 |
sv->ecc.r = xtrymalloc (n); |
| 2337 |
if (!sv->ecc.r) |
| 2338 |
{ |
| 2339 |
err = gpg_error_from_syserror (); |
| 2340 |
goto leave; |
| 2341 |
} |
| 2342 |
memcpy (sv->ecc.r, s, n); |
| 2343 |
sv->ecc.rlen = n; |
| 2344 |
} |
| 2345 |
/* (We ignore all other parameter of the (key value) form.) */ |
| 2346 |
|
| 2347 |
s += n; |
| 2348 |
if ( *s != ')') |
| 2349 |
{ |
| 2350 |
err = gpg_error (GPG_ERR_UNKNOWN_SEXP); /* or invalid sexp */ |
| 2351 |
goto leave; |
| 2352 |
} |
| 2353 |
s++; |
| 2354 |
} |
| 2355 |
|
| 2356 |
/* Expect two closing parenthesis. */ |
| 2357 |
if (*s != ')') |
| 2358 |
{ |
| 2359 |
err = gpg_error (digitp (s)? GPG_ERR_UNKNOWN_SEXP : GPG_ERR_INV_SEXP); |
| 2360 |
goto leave; |
| 2361 |
} |
| 2362 |
s++; |
| 2363 |
if ( *s != ')') |
| 2364 |
{ |
| 2365 |
err = gpg_error (GPG_ERR_INV_SEXP); |
| 2366 |
goto leave; |
| 2367 |
} |
| 2368 |
|
| 2369 |
/* Check that we have all required data. */ |
| 2370 |
if (!sv->value) |
| 2371 |
{ |
| 2372 |
err = gpg_error (GPG_ERR_INV_SEXP); |
| 2373 |
goto leave; |
| 2374 |
} |
| 2375 |
if (ecc && (!sv->ecc.r || !sv->ecc.rlen)) |
| 2376 |
{ |
| 2377 |
err = gpg_error (GPG_ERR_INV_SEXP); |
| 2378 |
goto leave; |
| 2379 |
} |
| 2380 |
|
| 2381 |
*sv_tail = sv; |
| 2382 |
return 0; /* Success. */ |
| 2383 |
|
| 2384 |
leave: /* Note: This is an error-only label. */ |
| 2385 |
xfree (sv->value); |
| 2386 |
xfree (sv->algo); |
| 2387 |
xfree (sv->ecc.r); |
| 2388 |
xfree (sv); |
| 2389 |
return err; |
| 2390 |
} |
| 2391 |
|
| 2392 |
|
| 2393 |
/* Set the content encryption algorithm to OID and optionally set the |
| 2394 |
initialization vector to IV */ |
| 2395 |
gpg_error_t |
| 2396 |
ksba_cms_set_content_enc_algo (ksba_cms_t cms, |
| 2397 |
const char *oid, |
| 2398 |
const void *iv, size_t ivlen) |
| 2399 |
{ |
| 2400 |
if (!cms || !oid) |
| 2401 |
return gpg_error (GPG_ERR_INV_VALUE); |
| 2402 |
|
| 2403 |
xfree (cms->encr_iv); |
| 2404 |
cms->encr_iv = NULL; |
| 2405 |
cms->encr_ivlen = 0; |
| 2406 |
|
| 2407 |
cms->encr_algo_oid = xtrystrdup (oid); |
| 2408 |
if (!cms->encr_algo_oid) |
| 2409 |
return gpg_error (GPG_ERR_ENOMEM); |
| 2410 |
|
| 2411 |
if (iv) |
| 2412 |
{ |
| 2413 |
cms->encr_iv = xtrymalloc (ivlen); |
| 2414 |
if (!cms->encr_iv) |
| 2415 |
return gpg_error (GPG_ERR_ENOMEM); |
| 2416 |
memcpy (cms->encr_iv, iv, ivlen); |
| 2417 |
cms->encr_ivlen = ivlen; |
| 2418 |
} |
| 2419 |
return 0; |
| 2420 |
} |
| 2421 |
|
| 2422 |
|
| 2423 |
/* |
| 2424 |
* encval is expected to be a canonical encoded S-Exp of this form: |
| 2425 |
* (enc-val |
| 2426 |
* (<algo> |
| 2427 |
* (<param_name1> <mpi>) |
| 2428 |
* ... |
| 2429 |
* (<param_namen> <mpi>) |
| 2430 |
* (encr-algo <oid>) |
| 2431 |
* (wrap-algo <oid>) |
| 2432 |
* )) |
| 2433 |
* |
| 2434 |
* Note the <algo> must be given as a stringified OID or the special |
| 2435 |
* string "rsa". For RSA there is just one parameter named "a"; |
| 2436 |
* encr-algo and wrap-algo are also not used. For ECC <algo> must be |
| 2437 |
* "ecdh", the parameter "s" gives the encrypted key, "e" specified |
| 2438 |
* the ephemeral public key, and wrap-algo algo and encr-algo are the |
| 2439 |
* stringified OIDs for the ECDH algorithm parameters. */ |
| 2440 |
gpg_error_t |
| 2441 |
ksba_cms_set_enc_val (ksba_cms_t cms, int idx, ksba_const_sexp_t encval) |
| 2442 |
{ |
| 2443 |
/*FIXME: This shares most code with ...set_sig_val */ |
| 2444 |
struct certlist_s *cl; |
| 2445 |
const char *s, *endp, *name; |
| 2446 |
unsigned long n, namelen; |
| 2447 |
int ecdh = 0; /* We expect ECC parameters. */ |
| 2448 |
|
| 2449 |
if (!cms) |
| 2450 |
return gpg_error (GPG_ERR_INV_VALUE); |
| 2451 |
if (idx < 0) |
| 2452 |
return gpg_error (GPG_ERR_INV_INDEX); |
| 2453 |
for (cl=cms->cert_list; cl && idx; cl = cl->next, idx--) |
| 2454 |
; |
| 2455 |
if (!cl) |
| 2456 |
return gpg_error (GPG_ERR_INV_INDEX); /* No cert to store the value. */ |
| 2457 |
|
| 2458 |
/* log_sexp ("encval", encval); */ |
| 2459 |
s = encval; |
| 2460 |
if (*s != '(') |
| 2461 |
return gpg_error (GPG_ERR_INV_SEXP); |
| 2462 |
s++; |
| 2463 |
|
| 2464 |
n = strtoul (s, (char**)&endp, 10); |
| 2465 |
s = endp; |
| 2466 |
if (!n || *s!=':') |
| 2467 |
return gpg_error (GPG_ERR_INV_SEXP); /* we don't allow empty lengths */ |
| 2468 |
s++; |
| 2469 |
if (n != 7 || memcmp (s, "enc-val", 7)) |
| 2470 |
return gpg_error (GPG_ERR_UNKNOWN_SEXP); |
| 2471 |
s += 7; |
| 2472 |
if (*s != '(') |
| 2473 |
return gpg_error (digitp (s)? GPG_ERR_UNKNOWN_SEXP : GPG_ERR_INV_SEXP); |
| 2474 |
s++; |
| 2475 |
|
| 2476 |
/* break out the algorithm ID */ |
| 2477 |
n = strtoul (s, (char**)&endp, 10); |
| 2478 |
s = endp; |
| 2479 |
if (!n || *s != ':') |
| 2480 |
return gpg_error (GPG_ERR_INV_SEXP); /* we don't allow empty lengths */ |
| 2481 |
s++; |
| 2482 |
xfree (cl->enc_val.algo); |
| 2483 |
if (n==3 && !memcmp (s, "rsa", 3)) |
| 2484 |
{ /* kludge to allow "rsa" to be passed as algorithm name */ |
| 2485 |
cl->enc_val.algo = xtrystrdup ("1.2.840.113549.1.1.1"); |
| 2486 |
if (!cl->enc_val.algo) |
| 2487 |
return gpg_error (GPG_ERR_ENOMEM); |
| 2488 |
} |
| 2489 |
else if (n==4 && !memcmp (s, "ecdh", 4)) |
| 2490 |
{ |
| 2491 |
cl->enc_val.algo = xtrystrdup ("1.2.840.10045.2.1"); /* ecPublicKey */ |
| 2492 |
if (!cl->enc_val.algo) |
| 2493 |
return gpg_error (GPG_ERR_ENOMEM); |
| 2494 |
} |
| 2495 |
else |
| 2496 |
{ |
| 2497 |
cl->enc_val.algo = xtrymalloc (n+1); |
| 2498 |
if (!cl->enc_val.algo) |
| 2499 |
return gpg_error (GPG_ERR_ENOMEM); |
| 2500 |
memcpy (cl->enc_val.algo, s, n); |
| 2501 |
cl->enc_val.algo[n] = 0; |
| 2502 |
} |
| 2503 |
s += n; |
| 2504 |
|
| 2505 |
ecdh = !strcmp (cl->enc_val.algo, "1.2.840.10045.2.1"); |
| 2506 |
|
| 2507 |
xfree (cl->enc_val.value); cl->enc_val.value = NULL; |
| 2508 |
xfree (cl->enc_val.ecdh.e); cl->enc_val.ecdh.e = NULL; |
| 2509 |
xfree (cl->enc_val.ecdh.encr_algo); cl->enc_val.ecdh.encr_algo = NULL; |
| 2510 |
xfree (cl->enc_val.ecdh.wrap_algo); cl->enc_val.ecdh.wrap_algo = NULL; |
| 2511 |
|
| 2512 |
while (*s == '(') |
| 2513 |
{ |
| 2514 |
s++; |
| 2515 |
n = strtoul (s, (char**)&endp, 10); |
| 2516 |
s = endp; |
| 2517 |
if (!n || *s != ':') |
| 2518 |
return gpg_error (GPG_ERR_INV_SEXP); |
| 2519 |
s++; |
| 2520 |
name = s; |
| 2521 |
namelen = n; |
| 2522 |
s += n; |
| 2523 |
|
| 2524 |
if (!digitp(s)) |
| 2525 |
return gpg_error (GPG_ERR_UNKNOWN_SEXP); /* or invalid sexp */ |
| 2526 |
n = strtoul (s, (char**)&endp, 10); |
| 2527 |
s = endp; |
| 2528 |
if (!n || *s != ':') |
| 2529 |
return gpg_error (GPG_ERR_INV_SEXP); |
| 2530 |
s++; |
| 2531 |
|
| 2532 |
if (namelen == 1 && ((!ecdh && *name == 'a') || (ecdh && *name == 's'))) |
| 2533 |
{ |
| 2534 |
/* Store the "main" parameter into value. */ |
| 2535 |
xfree (cl->enc_val.value); |
| 2536 |
cl->enc_val.value = xtrymalloc (n); |
| 2537 |
if (!cl->enc_val.value) |
| 2538 |
return gpg_error (GPG_ERR_ENOMEM); |
| 2539 |
memcpy (cl->enc_val.value, s, n); |
| 2540 |
cl->enc_val.valuelen = n; |
| 2541 |
} |
| 2542 |
else if (!ecdh) |
| 2543 |
; /* Ignore all other parameters for RSA. */ |
| 2544 |
else if (namelen == 1 && *name == 'e') |
| 2545 |
{ |
| 2546 |
xfree (cl->enc_val.ecdh.e); |
| 2547 |
cl->enc_val.ecdh.e = xtrymalloc (n); |
| 2548 |
if (!cl->enc_val.ecdh.e) |
| 2549 |
return gpg_error (GPG_ERR_ENOMEM); |
| 2550 |
memcpy (cl->enc_val.ecdh.e, s, n); |
| 2551 |
cl->enc_val.ecdh.elen = n; |
| 2552 |
} |
| 2553 |
else if (namelen == 9 && !memcmp (name, "encr-algo", 9)) |
| 2554 |
{ |
| 2555 |
xfree (cl->enc_val.ecdh.encr_algo); |
| 2556 |
cl->enc_val.ecdh.encr_algo = xtrymalloc (n+1); |
| 2557 |
if (!cl->enc_val.ecdh.encr_algo) |
| 2558 |
return gpg_error (GPG_ERR_ENOMEM); |
| 2559 |
memcpy (cl->enc_val.ecdh.encr_algo, s, n); |
| 2560 |
cl->enc_val.ecdh.encr_algo[n] = 0; |
| 2561 |
} |
| 2562 |
else if (namelen == 9 && !memcmp (name, "wrap-algo", 9)) |
| 2563 |
{ |
| 2564 |
xfree (cl->enc_val.ecdh.wrap_algo); |
| 2565 |
cl->enc_val.ecdh.wrap_algo = xtrymalloc (n+1); |
| 2566 |
if (!cl->enc_val.ecdh.wrap_algo) |
| 2567 |
return gpg_error (GPG_ERR_ENOMEM); |
| 2568 |
memcpy (cl->enc_val.ecdh.wrap_algo, s, n); |
| 2569 |
cl->enc_val.ecdh.wrap_algo[n] = 0; |
| 2570 |
} |
| 2571 |
/* (We ignore all other parameter of the (key value) form.) */ |
| 2572 |
|
| 2573 |
s += n; |
| 2574 |
if ( *s != ')') |
| 2575 |
return gpg_error (GPG_ERR_UNKNOWN_SEXP); /* or invalid sexp */ |
| 2576 |
s++; |
| 2577 |
} |
| 2578 |
/* Expect two closing parenthesis. */ |
| 2579 |
if (*s != ')') |
| 2580 |
return gpg_error (digitp (s)? GPG_ERR_UNKNOWN_SEXP : GPG_ERR_INV_SEXP); |
| 2581 |
s++; |
| 2582 |
if ( *s != ')') |
| 2583 |
return gpg_error (GPG_ERR_INV_SEXP); |
| 2584 |
|
| 2585 |
/* Check that we have all required data. */ |
| 2586 |
if (!cl->enc_val.value) |
| 2587 |
return gpg_error (GPG_ERR_INV_SEXP); |
| 2588 |
if (ecdh && (!cl->enc_val.ecdh.e |
| 2589 |
|| !cl->enc_val.ecdh.elen |
| 2590 |
|| !cl->enc_val.ecdh.encr_algo |
| 2591 |
|| !cl->enc_val.ecdh.wrap_algo)) |
| 2592 |
return gpg_error (GPG_ERR_INV_SEXP); |
| 2593 |
|
| 2594 |
|
| 2595 |
return 0; |
| 2596 |
} |
| 2597 |
|
| 2598 |
|
| 2599 |
|
| 2600 |
|
| 2601 |
/** |
| 2602 |
* ksba_cms_add_recipient: |
| 2603 |
* @cms: A CMS object |
| 2604 |
* @cert: A certificate used to describe the recipient. |
| 2605 |
* |
| 2606 |
* This functions starts assembly of a new enveloped data content or adds |
| 2607 |
* another recipient to the list of recipients. |
| 2608 |
* |
| 2609 |
* Note: after successful completion of this function ownership of |
| 2610 |
* @cert is transferred to @cms. |
| 2611 |
* |
| 2612 |
* Return value: 0 on success or an error code. |
| 2613 |
**/ |
| 2614 |
gpg_error_t |
| 2615 |
ksba_cms_add_recipient (ksba_cms_t cms, ksba_cert_t cert) |
| 2616 |
{ |
| 2617 |
/* for now we use the same structure */ |
| 2618 |
return ksba_cms_add_signer (cms, cert); |
| 2619 |
} |
| 2620 |
|
| 2621 |
|
| 2622 |
|
| 2623 |
|
| 2624 |
/* |
| 2625 |
Content handler for parsing messages |
| 2626 |
*/ |
| 2627 |
|
| 2628 |
static gpg_error_t |
| 2629 |
ct_parse_data (ksba_cms_t cms) |
| 2630 |
{ |
| 2631 |
(void)cms; |
| 2632 |
return gpg_error (GPG_ERR_NOT_IMPLEMENTED); |
| 2633 |
} |
| 2634 |
|
| 2635 |
|
| 2636 |
static gpg_error_t |
| 2637 |
ct_parse_signed_data (ksba_cms_t cms) |
| 2638 |
{ |
| 2639 |
enum { |
| 2640 |
sSTART, |
| 2641 |
sGOT_HASH, |
| 2642 |
sIN_DATA, |
| 2643 |
sERROR |
| 2644 |
} state = sERROR; |
| 2645 |
ksba_stop_reason_t stop_reason = cms->stop_reason; |
| 2646 |
gpg_error_t err = 0; |
| 2647 |
|
| 2648 |
cms->stop_reason = KSBA_SR_RUNNING; |
| 2649 |
|
| 2650 |
/* Calculate state from last reason and do some checks */ |
| 2651 |
if (stop_reason == KSBA_SR_GOT_CONTENT) |
| 2652 |
{ |
| 2653 |
state = sSTART; |
| 2654 |
} |
| 2655 |
else if (stop_reason == KSBA_SR_NEED_HASH) |
| 2656 |
{ |
| 2657 |
state = sGOT_HASH; |
| 2658 |
} |
| 2659 |
else if (stop_reason == KSBA_SR_BEGIN_DATA) |
| 2660 |
{ |
| 2661 |
if (!cms->hash_fnc) |
| 2662 |
err = gpg_error (GPG_ERR_MISSING_ACTION); |
| 2663 |
else |
| 2664 |
state = sIN_DATA; |
| 2665 |
} |
| 2666 |
else if (stop_reason == KSBA_SR_END_DATA) |
| 2667 |
{ |
| 2668 |
state = sGOT_HASH; |
| 2669 |
} |
| 2670 |
else if (stop_reason == KSBA_SR_RUNNING) |
| 2671 |
err = gpg_error (GPG_ERR_INV_STATE); |
| 2672 |
else if (stop_reason) |
| 2673 |
err = gpg_error (GPG_ERR_BUG); |
| 2674 |
|
| 2675 |
if (err) |
| 2676 |
return err; |
| 2677 |
|
| 2678 |
/* Do the action */ |
| 2679 |
if (state == sSTART) |
| 2680 |
err = _ksba_cms_parse_signed_data_part_1 (cms); |
| 2681 |
else if (state == sGOT_HASH) |
| 2682 |
err = _ksba_cms_parse_signed_data_part_2 (cms); |
| 2683 |
else if (state == sIN_DATA) |
| 2684 |
err = read_and_hash_cont (cms); |
| 2685 |
else |
| 2686 |
err = gpg_error (GPG_ERR_INV_STATE); |
| 2687 |
|
| 2688 |
if (err) |
| 2689 |
return err; |
| 2690 |
|
| 2691 |
/* Calculate new stop reason */ |
| 2692 |
if (state == sSTART) |
| 2693 |
{ |
| 2694 |
if (cms->detached_data) |
| 2695 |
{ /* We use this stop reason to inform the caller about a |
| 2696 |
detached signatures. Actually there is no need for him |
| 2697 |
to hash the data now, he can do this also later. */ |
| 2698 |
stop_reason = KSBA_SR_NEED_HASH; |
| 2699 |
} |
| 2700 |
else |
| 2701 |
{ /* The user must now provide a hash function so that we can |
| 2702 |
hash the data in the next round */ |
| 2703 |
stop_reason = KSBA_SR_BEGIN_DATA; |
| 2704 |
} |
| 2705 |
} |
| 2706 |
else if (state == sIN_DATA) |
| 2707 |
stop_reason = KSBA_SR_END_DATA; |
| 2708 |
else if (state ==sGOT_HASH) |
| 2709 |
stop_reason = KSBA_SR_READY; |
| 2710 |
|
| 2711 |
cms->stop_reason = stop_reason; |
| 2712 |
return 0; |
| 2713 |
} |
| 2714 |
|
| 2715 |
|
| 2716 |
static gpg_error_t |
| 2717 |
ct_parse_enveloped_data (ksba_cms_t cms) |
| 2718 |
{ |
| 2719 |
enum { |
| 2720 |
sSTART, |
| 2721 |
sREST, |
| 2722 |
sINDATA, |
| 2723 |
sERROR |
| 2724 |
} state = sERROR; |
| 2725 |
ksba_stop_reason_t stop_reason = cms->stop_reason; |
| 2726 |
gpg_error_t err = 0; |
| 2727 |
|
| 2728 |
cms->stop_reason = KSBA_SR_RUNNING; |
| 2729 |
|
| 2730 |
/* Calculate state from last reason and do some checks */ |
| 2731 |
if (stop_reason == KSBA_SR_GOT_CONTENT) |
| 2732 |
{ |
| 2733 |
state = sSTART; |
| 2734 |
} |
| 2735 |
else if (stop_reason == KSBA_SR_DETACHED_DATA) |
| 2736 |
{ |
| 2737 |
state = sREST; |
| 2738 |
} |
| 2739 |
else if (stop_reason == KSBA_SR_BEGIN_DATA) |
| 2740 |
{ |
| 2741 |
state = sINDATA; |
| 2742 |
} |
| 2743 |
else if (stop_reason == KSBA_SR_END_DATA) |
| 2744 |
{ |
| 2745 |
state = sREST; |
| 2746 |
} |
| 2747 |
else if (stop_reason == KSBA_SR_RUNNING) |
| 2748 |
err = gpg_error (GPG_ERR_INV_STATE); |
| 2749 |
else if (stop_reason) |
| 2750 |
err = gpg_error (GPG_ERR_BUG); |
| 2751 |
|
| 2752 |
if (err) |
| 2753 |
return err; |
| 2754 |
|
| 2755 |
/* Do the action */ |
| 2756 |
if (state == sSTART) |
| 2757 |
err = _ksba_cms_parse_enveloped_data_part_1 (cms); |
| 2758 |
else if (state == sREST) |
| 2759 |
err = _ksba_cms_parse_enveloped_data_part_2 (cms); |
| 2760 |
else if (state == sINDATA) |
| 2761 |
err = read_encrypted_cont (cms); |
| 2762 |
else |
| 2763 |
err = gpg_error (GPG_ERR_INV_STATE); |
| 2764 |
|
| 2765 |
if (err) |
| 2766 |
return err; |
| 2767 |
|
| 2768 |
/* Calculate new stop reason */ |
| 2769 |
if (state == sSTART) |
| 2770 |
{ |
| 2771 |
stop_reason = cms->detached_data? KSBA_SR_DETACHED_DATA |
| 2772 |
: KSBA_SR_BEGIN_DATA; |
| 2773 |
} |
| 2774 |
else if (state == sINDATA) |
| 2775 |
stop_reason = KSBA_SR_END_DATA; |
| 2776 |
else if (state ==sREST) |
| 2777 |
stop_reason = KSBA_SR_READY; |
| 2778 |
|
| 2779 |
cms->stop_reason = stop_reason; |
| 2780 |
return 0; |
| 2781 |
} |
| 2782 |
|
| 2783 |
|
| 2784 |
static gpg_error_t |
| 2785 |
ct_parse_digested_data (ksba_cms_t cms) |
| 2786 |
{ |
| 2787 |
(void)cms; |
| 2788 |
return gpg_error (GPG_ERR_NOT_IMPLEMENTED); |
| 2789 |
} |
| 2790 |
|
| 2791 |
|
| 2792 |
static gpg_error_t |
| 2793 |
ct_parse_encrypted_data (ksba_cms_t cms) |
| 2794 |
{ |
| 2795 |
(void)cms; |
| 2796 |
return gpg_error (GPG_ERR_NOT_IMPLEMENTED); |
| 2797 |
} |
| 2798 |
|
| 2799 |
|
| 2800 |
|
| 2801 |
/* |
| 2802 |
Content handlers for building messages |
| 2803 |
*/ |
| 2804 |
|
| 2805 |
static gpg_error_t |
| 2806 |
ct_build_data (ksba_cms_t cms) |
| 2807 |
{ |
| 2808 |
(void)cms; |
| 2809 |
return gpg_error (GPG_ERR_NOT_IMPLEMENTED); |
| 2810 |
} |
| 2811 |
|
| 2812 |
|
| 2813 |
|
| 2814 |
/* Write everything up to the encapsulated data content type. */ |
| 2815 |
static gpg_error_t |
| 2816 |
build_signed_data_header (ksba_cms_t cms) |
| 2817 |
{ |
| 2818 |
gpg_error_t err; |
| 2819 |
unsigned char *buf; |
| 2820 |
const char *s; |
| 2821 |
size_t len; |
| 2822 |
int i; |
| 2823 |
|
| 2824 |
/* Write the outer contentInfo. */ |
| 2825 |
err = _ksba_ber_write_tl (cms->writer, TYPE_SEQUENCE, CLASS_UNIVERSAL, 1, 0); |
| 2826 |
if (err) |
| 2827 |
return err; |
| 2828 |
err = ksba_oid_from_str (cms->content.oid, &buf, &len); |
| 2829 |
if (err) |
| 2830 |
return err; |
| 2831 |
err = _ksba_ber_write_tl (cms->writer, |
| 2832 |
TYPE_OBJECT_ID, CLASS_UNIVERSAL, 0, len); |
| 2833 |
if (!err) |
| 2834 |
err = ksba_writer_write (cms->writer, buf, len); |
| 2835 |
xfree (buf); |
| 2836 |
if (err) |
| 2837 |
return err; |
| 2838 |
|
| 2839 |
err = _ksba_ber_write_tl (cms->writer, 0, CLASS_CONTEXT, 1, 0); |
| 2840 |
if (err) |
| 2841 |
return err; |
| 2842 |
|
| 2843 |
/* The SEQUENCE */ |
| 2844 |
err = _ksba_ber_write_tl (cms->writer, TYPE_SEQUENCE, CLASS_UNIVERSAL, 1, 0); |
| 2845 |
if (err) |
| 2846 |
return err; |
| 2847 |
|
| 2848 |
/* figure out the CMSVersion to be used */ |
| 2849 |
if (0 /* fixme: have_attribute_certificates |
| 2850 |
|| encapsulated_content != data |
| 2851 |
|| any_signer_info_is_version_3*/ ) |
| 2852 |
s = "\x03"; |
| 2853 |
else |
| 2854 |
s = "\x01"; |
| 2855 |
err = _ksba_ber_write_tl (cms->writer, TYPE_INTEGER, CLASS_UNIVERSAL, 0, 1); |
| 2856 |
if (err) |
| 2857 |
return err; |
| 2858 |
err = ksba_writer_write (cms->writer, s, 1); |
| 2859 |
if (err) |
| 2860 |
return err; |
| 2861 |
|
| 2862 |
/* SET OF DigestAlgorithmIdentifier */ |
| 2863 |
{ |
| 2864 |
unsigned char *value; |
| 2865 |
size_t valuelen; |
| 2866 |
ksba_writer_t tmpwrt; |
| 2867 |
|
| 2868 |
err = ksba_writer_new (&tmpwrt); |
| 2869 |
if (err) |
| 2870 |
return err; |
| 2871 |
err = ksba_writer_set_mem (tmpwrt, 512); |
| 2872 |
if (err) |
| 2873 |
{ |
| 2874 |
ksba_writer_release (tmpwrt); |
| 2875 |
return err; |
| 2876 |
} |
| 2877 |
|
| 2878 |
for (i=0; (s = ksba_cms_get_digest_algo_list (cms, i)); i++) |
| 2879 |
{ |
| 2880 |
int j; |
| 2881 |
const char *s2; |
| 2882 |
|
| 2883 |
/* (make sure not to write duplicates) */ |
| 2884 |
for (j=0; j < i && (s2=ksba_cms_get_digest_algo_list (cms, j)); j++) |
| 2885 |
{ |
| 2886 |
if (!strcmp (s, s2)) |
| 2887 |
break; |
| 2888 |
} |
| 2889 |
if (j == i) |
| 2890 |
{ |
| 2891 |
err = _ksba_der_write_algorithm_identifier (tmpwrt, s, NULL, 0); |
| 2892 |
if (err) |
| 2893 |
{ |
| 2894 |
ksba_writer_release (tmpwrt); |
| 2895 |
return err; |
| 2896 |
} |
| 2897 |
} |
| 2898 |
} |
| 2899 |
|
| 2900 |
value = ksba_writer_snatch_mem (tmpwrt, &valuelen); |
| 2901 |
ksba_writer_release (tmpwrt); |
| 2902 |
if (!value) |
| 2903 |
{ |
| 2904 |
err = gpg_error (GPG_ERR_ENOMEM); |
| 2905 |
return err; |
| 2906 |
} |
| 2907 |
err = _ksba_ber_write_tl (cms->writer, TYPE_SET, CLASS_UNIVERSAL, |
| 2908 |
1, valuelen); |
| 2909 |
if (!err) |
| 2910 |
err = ksba_writer_write (cms->writer, value, valuelen); |
| 2911 |
xfree (value); |
| 2912 |
if (err) |
| 2913 |
return err; |
| 2914 |
} |
| 2915 |
|
| 2916 |
|
| 2917 |
|
| 2918 |
/* Write the (inner) encapsulatedContentInfo */ |
| 2919 |
/* if we have a detached signature we don't need to use undefinite |
| 2920 |
length here - but it doesn't matter either */ |
| 2921 |
err = _ksba_ber_write_tl (cms->writer, TYPE_SEQUENCE, CLASS_UNIVERSAL, 1, 0); |
| 2922 |
if (err) |
| 2923 |
return err; |
| 2924 |
err = ksba_oid_from_str (cms->inner_cont_oid, &buf, &len); |
| 2925 |
if (err) |
| 2926 |
return err; |
| 2927 |
err = _ksba_ber_write_tl (cms->writer, |
| 2928 |
TYPE_OBJECT_ID, CLASS_UNIVERSAL, 0, len); |
| 2929 |
if (!err) |
| 2930 |
err = ksba_writer_write (cms->writer, buf, len); |
| 2931 |
xfree (buf); |
| 2932 |
if (err) |
| 2933 |
return err; |
| 2934 |
|
| 2935 |
if ( !cms->detached_data) |
| 2936 |
{ /* write the tag */ |
| 2937 |
err = _ksba_ber_write_tl (cms->writer, 0, CLASS_CONTEXT, 1, 0); |
| 2938 |
if (err) |
| 2939 |
return err; |
| 2940 |
} |
| 2941 |
|
| 2942 |
return err; |
| 2943 |
} |
| 2944 |
|
| 2945 |
/* Set the issuer/serial from the cert to the node. |
| 2946 |
mode 0: sid |
| 2947 |
mode 1: rid |
| 2948 |
*/ |
| 2949 |
static gpg_error_t |
| 2950 |
set_issuer_serial (AsnNode info, ksba_cert_t cert, int mode) |
| 2951 |
{ |
| 2952 |
gpg_error_t err; |
| 2953 |
AsnNode dst, src; |
| 2954 |
|
| 2955 |
if (!info || !cert) |
| 2956 |
return gpg_error (GPG_ERR_INV_VALUE); |
| 2957 |
|
| 2958 |
src = _ksba_asn_find_node (cert->root, |
| 2959 |
"Certificate.tbsCertificate.serialNumber"); |
| 2960 |
dst = _ksba_asn_find_node (info, |
| 2961 |
mode? |
| 2962 |
"rid.issuerAndSerialNumber.serialNumber": |
| 2963 |
"sid.issuerAndSerialNumber.serialNumber"); |
| 2964 |
err = _ksba_der_copy_tree (dst, src, cert->image); |
| 2965 |
if (err) |
| 2966 |
return err; |
| 2967 |
|
| 2968 |
src = _ksba_asn_find_node (cert->root, |
| 2969 |
"Certificate.tbsCertificate.issuer"); |
| 2970 |
dst = _ksba_asn_find_node (info, |
| 2971 |
mode? |
| 2972 |
"rid.issuerAndSerialNumber.issuer": |
| 2973 |
"sid.issuerAndSerialNumber.issuer"); |
| 2974 |
err = _ksba_der_copy_tree (dst, src, cert->image); |
| 2975 |
if (err) |
| 2976 |
return err; |
| 2977 |
|
| 2978 |
return 0; |
| 2979 |
} |
| 2980 |
|
| 2981 |
|
| 2982 |
/* Store the sequence of capabilities at NODE */ |
| 2983 |
static gpg_error_t |
| 2984 |
store_smime_capability_sequence (AsnNode node, |
| 2985 |
struct oidparmlist_s *capabilities) |
| 2986 |
{ |
| 2987 |
gpg_error_t err; |
| 2988 |
struct oidparmlist_s *cap, *cap2; |
| 2989 |
unsigned char *value; |
| 2990 |
size_t valuelen; |
| 2991 |
ksba_writer_t tmpwrt; |
| 2992 |
|
| 2993 |
err = ksba_writer_new (&tmpwrt); |
| 2994 |
if (err) |
| 2995 |
return err; |
| 2996 |
err = ksba_writer_set_mem (tmpwrt, 512); |
| 2997 |
if (err) |
| 2998 |
{ |
| 2999 |
ksba_writer_release (tmpwrt); |
| 3000 |
return err; |
| 3001 |
} |
| 3002 |
|
| 3003 |
for (cap=capabilities; cap; cap = cap->next) |
| 3004 |
{ |
| 3005 |
/* Note that we do not use the unprotected and signeridx fields |
| 3006 |
* here. */ |
| 3007 |
|
| 3008 |
/* We want to avoid writing duplicates. */ |
| 3009 |
for (cap2=capabilities; cap2 != cap; cap2 = cap2->next) |
| 3010 |
{ |
| 3011 |
if (!strcmp (cap->oid, cap2->oid) |
| 3012 |
&& cap->parmlen && cap->parmlen == cap2->parmlen |
| 3013 |
&& !memcmp (cap->parm, cap2->parm, cap->parmlen)) |
| 3014 |
break; /* Duplicate found. */ |
| 3015 |
} |
| 3016 |
if (cap2 == cap) |
| 3017 |
{ |
| 3018 |
/* RFC3851 requires that a missing parameter must not be |
| 3019 |
encoded as NULL. This is in contrast to all other usages |
| 3020 |
of the algorithm identifier where ist is allowed and in |
| 3021 |
some profiles (e.g. tmttv2) even explicitly suggested to |
| 3022 |
use NULL. */ |
| 3023 |
err = _ksba_der_write_algorithm_identifier |
| 3024 |
(tmpwrt, cap->oid, |
| 3025 |
cap->parmlen?cap->parm:(const void*)"", cap->parmlen); |
| 3026 |
if (err) |
| 3027 |
{ |
| 3028 |
ksba_writer_release (tmpwrt); |
| 3029 |
return err; |
| 3030 |
} |
| 3031 |
} |
| 3032 |
} |
| 3033 |
|
| 3034 |
value = ksba_writer_snatch_mem (tmpwrt, &valuelen); |
| 3035 |
if (!value) |
| 3036 |
err = gpg_error (GPG_ERR_ENOMEM); |
| 3037 |
if (!err) |
| 3038 |
err = _ksba_der_store_sequence (node, value, valuelen); |
| 3039 |
xfree (value); |
| 3040 |
ksba_writer_release (tmpwrt); |
| 3041 |
return err; |
| 3042 |
} |
| 3043 |
|
| 3044 |
|
| 3045 |
/* An object used to construct the signed attributes. */ |
| 3046 |
struct attrarray_s { |
| 3047 |
AsnNode root; |
| 3048 |
unsigned char *image; |
| 3049 |
size_t imagelen; |
| 3050 |
}; |
| 3051 |
|
| 3052 |
|
| 3053 |
/* Thank you ASN.1 committee for allowing us to employ a sort to make |
| 3054 |
that DER encoding even more complicate. */ |
| 3055 |
static int |
| 3056 |
compare_attrarray (const void *a_v, const void *b_v) |
| 3057 |
{ |
| 3058 |
const struct attrarray_s *a = a_v; |
| 3059 |
const struct attrarray_s *b = b_v; |
| 3060 |
const unsigned char *ap, *bp; |
| 3061 |
size_t an, bn; |
| 3062 |
|
| 3063 |
ap = a->image; |
| 3064 |
an = a->imagelen; |
| 3065 |
bp = b->image; |
| 3066 |
bn = b->imagelen; |
| 3067 |
for (; an && bn; an--, bn--, ap++, bp++ ) |
| 3068 |
if (*ap != *bp) |
| 3069 |
return *ap - *bp; |
| 3070 |
|
| 3071 |
return (an == bn)? 0 : (an > bn)? 1 : -1; |
| 3072 |
} |
| 3073 |
|
| 3074 |
|
| 3075 |
|
| 3076 |
|
| 3077 |
/* Write the END of data NULL tag and everything we can write before |
| 3078 |
the user can calculate the signature */ |
| 3079 |
static gpg_error_t |
| 3080 |
build_signed_data_attributes (ksba_cms_t cms) |
| 3081 |
{ |
| 3082 |
gpg_error_t err; |
| 3083 |
int signer; |
| 3084 |
ksba_asn_tree_t cms_tree = NULL; |
| 3085 |
struct certlist_s *certlist; |
| 3086 |
struct oidlist_s *digestlist; |
| 3087 |
struct signer_info_s *si, **si_tail; |
| 3088 |
struct oidparmlist_s *opl; |
| 3089 |
AsnNode root = NULL; |
| 3090 |
struct attrarray_s *attrarray = NULL; |
| 3091 |
unsigned int attrsize; |
| 3092 |
int attridx = 0; |
| 3093 |
int i; |
| 3094 |
|
| 3095 |
/* Write the End tag */ |
| 3096 |
err = _ksba_ber_write_tl (cms->writer, 0, 0, 0, 0); |
| 3097 |
if (err) |
| 3098 |
return err; |
| 3099 |
|
| 3100 |
if (cms->signer_info) |
| 3101 |
return gpg_error (GPG_ERR_CONFLICT); /* This list must be empty at |
| 3102 |
this point. */ |
| 3103 |
|
| 3104 |
/* Allocate four slots for the standard attributes: |
| 3105 |
* - msg_digest |
| 3106 |
* - inner_content_type |
| 3107 |
* - signing time (optional) |
| 3108 |
* - s/mime capabilities (optional) |
| 3109 |
*/ |
| 3110 |
attrsize = 4; |
| 3111 |
/* Add more slots for extra attributes. */ |
| 3112 |
for (opl = cms->attribute_list; opl; opl = opl->next) |
| 3113 |
attrsize++; |
| 3114 |
|
| 3115 |
attrarray = xtrycalloc (attrsize, sizeof *attrarray); |
| 3116 |
if (!attrarray) |
| 3117 |
{ |
| 3118 |
err = gpg_error_from_syserror (); |
| 3119 |
goto leave; |
| 3120 |
} |
| 3121 |
|
| 3122 |
|
| 3123 |
/* Write optional certificates */ |
| 3124 |
if (cms->cert_info_list) |
| 3125 |
{ |
| 3126 |
unsigned long totallen = 0; |
| 3127 |
const unsigned char *der; |
| 3128 |
size_t n; |
| 3129 |
|
| 3130 |
for (certlist = cms->cert_info_list; certlist; certlist = certlist->next) |
| 3131 |
{ |
| 3132 |
if (!ksba_cert_get_image (certlist->cert, &n)) |
| 3133 |
return gpg_error (GPG_ERR_GENERAL); /* User passed an |
| 3134 |
unitialized cert */ |
| 3135 |
totallen += n; |
| 3136 |
} |
| 3137 |
|
| 3138 |
err = _ksba_ber_write_tl (cms->writer, 0, CLASS_CONTEXT, 1, totallen); |
| 3139 |
if (err) |
| 3140 |
return err; |
| 3141 |
|
| 3142 |
for (certlist = cms->cert_info_list; certlist; certlist = certlist->next) |
| 3143 |
{ |
| 3144 |
if (!(der=ksba_cert_get_image (certlist->cert, &n))) |
| 3145 |
return gpg_error (GPG_ERR_BUG); |
| 3146 |
err = ksba_writer_write (cms->writer, der, n); |
| 3147 |
if (err ) |
| 3148 |
return err; |
| 3149 |
} |
| 3150 |
} |
| 3151 |
|
| 3152 |
/* If we ever support it, here is the right place to do it: |
| 3153 |
Write the optional CRLs */ |
| 3154 |
|
| 3155 |
/* Now we have to prepare the signer info. For now we will just build the |
| 3156 |
signedAttributes, so that the user can do the signature calculation */ |
| 3157 |
err = ksba_asn_create_tree ("cms", &cms_tree); |
| 3158 |
if (err) |
| 3159 |
return err; |
| 3160 |
|
| 3161 |
certlist = cms->cert_list; |
| 3162 |
if (!certlist) |
| 3163 |
{ |
| 3164 |
err = gpg_error (GPG_ERR_MISSING_VALUE); /* oops */ |
| 3165 |
goto leave; |
| 3166 |
} |
| 3167 |
digestlist = cms->digest_algos; |
| 3168 |
if (!digestlist) |
| 3169 |
{ |
| 3170 |
err = gpg_error (GPG_ERR_MISSING_VALUE); /* oops */ |
| 3171 |
goto leave; |
| 3172 |
} |
| 3173 |
|
| 3174 |
si_tail = &cms->signer_info; |
| 3175 |
for (signer=0; certlist; |
| 3176 |
signer++, certlist = certlist->next, digestlist = digestlist->next) |
| 3177 |
{ |
| 3178 |
AsnNode attr; |
| 3179 |
AsnNode n; |
| 3180 |
unsigned char *image; |
| 3181 |
size_t imagelen; |
| 3182 |
|
| 3183 |
for (i = 0; i < attridx; i++) |
| 3184 |
{ |
| 3185 |
_ksba_asn_release_nodes (attrarray[i].root); |
| 3186 |
xfree (attrarray[i].image); |
| 3187 |
} |
| 3188 |
attridx = 0; |
| 3189 |
memset (attrarray, 0, attrsize * sizeof *attrarray); |
| 3190 |
|
| 3191 |
if (!digestlist) |
| 3192 |
{ |
| 3193 |
err = gpg_error (GPG_ERR_MISSING_VALUE); /* oops */ |
| 3194 |
goto leave; |
| 3195 |
} |
| 3196 |
|
| 3197 |
if (!certlist->cert || !digestlist->oid) |
| 3198 |
{ |
| 3199 |
err = gpg_error (GPG_ERR_BUG); |
| 3200 |
goto leave; |
| 3201 |
} |
| 3202 |
|
| 3203 |
/* Include the pretty important message digest. */ |
| 3204 |
attr = _ksba_asn_expand_tree (cms_tree->parse_tree, |
| 3205 |
"CryptographicMessageSyntax.Attribute"); |
| 3206 |
if (!attr) |
| 3207 |
{ |
| 3208 |
err = gpg_error (GPG_ERR_ELEMENT_NOT_FOUND); |
| 3209 |
goto leave; |
| 3210 |
} |
| 3211 |
n = _ksba_asn_find_node (attr, "Attribute.attrType"); |
| 3212 |
if (!n) |
| 3213 |
{ |
| 3214 |
err = gpg_error (GPG_ERR_ELEMENT_NOT_FOUND); |
| 3215 |
goto leave; |
| 3216 |
} |
| 3217 |
err = _ksba_der_store_oid (n, oidstr_messageDigest); |
| 3218 |
if (err) |
| 3219 |
goto leave; |
| 3220 |
n = _ksba_asn_find_node (attr, "Attribute.attrValues"); |
| 3221 |
if (!n || !n->down) |
| 3222 |
return gpg_error (GPG_ERR_ELEMENT_NOT_FOUND); |
| 3223 |
n = n->down; /* fixme: ugly hack */ |
| 3224 |
assert (certlist && certlist->msg_digest_len); |
| 3225 |
err = _ksba_der_store_octet_string (n, certlist->msg_digest, |
| 3226 |
certlist->msg_digest_len); |
| 3227 |
if (err) |
| 3228 |
goto leave; |
| 3229 |
err = _ksba_der_encode_tree (attr, &image, &imagelen); |
| 3230 |
if (err) |
| 3231 |
goto leave; |
| 3232 |
attrarray[attridx].root = attr; |
| 3233 |
attrarray[attridx].image = image; |
| 3234 |
attrarray[attridx].imagelen = imagelen; |
| 3235 |
attridx++; |
| 3236 |
|
| 3237 |
/* Include the content-type attribute. */ |
| 3238 |
attr = _ksba_asn_expand_tree (cms_tree->parse_tree, |
| 3239 |
"CryptographicMessageSyntax.Attribute"); |
| 3240 |
if (!attr) |
| 3241 |
{ |
| 3242 |
err = gpg_error (GPG_ERR_ELEMENT_NOT_FOUND); |
| 3243 |
goto leave; |
| 3244 |
} |
| 3245 |
n = _ksba_asn_find_node (attr, "Attribute.attrType"); |
| 3246 |
if (!n) |
| 3247 |
{ |
| 3248 |
err = gpg_error (GPG_ERR_ELEMENT_NOT_FOUND); |
| 3249 |
goto leave; |
| 3250 |
} |
| 3251 |
err = _ksba_der_store_oid (n, oidstr_contentType); |
| 3252 |
if (err) |
| 3253 |
goto leave; |
| 3254 |
n = _ksba_asn_find_node (attr, "Attribute.attrValues"); |
| 3255 |
if (!n || !n->down) |
| 3256 |
{ |
| 3257 |
err = gpg_error (GPG_ERR_ELEMENT_NOT_FOUND); |
| 3258 |
goto leave; |
| 3259 |
} |
| 3260 |
n = n->down; /* fixme: ugly hack */ |
| 3261 |
err = _ksba_der_store_oid (n, cms->inner_cont_oid); |
| 3262 |
if (err) |
| 3263 |
goto leave; |
| 3264 |
err = _ksba_der_encode_tree (attr, &image, &imagelen); |
| 3265 |
if (err) |
| 3266 |
goto leave; |
| 3267 |
attrarray[attridx].root = attr; |
| 3268 |
attrarray[attridx].image = image; |
| 3269 |
attrarray[attridx].imagelen = imagelen; |
| 3270 |
attridx++; |
| 3271 |
|
| 3272 |
/* Include the signing time */ |
| 3273 |
if (*certlist->signing_time) |
| 3274 |
{ |
| 3275 |
attr = _ksba_asn_expand_tree (cms_tree->parse_tree, |
| 3276 |
"CryptographicMessageSyntax.Attribute"); |
| 3277 |
if (!attr) |
| 3278 |
{ |
| 3279 |
err = gpg_error (GPG_ERR_ELEMENT_NOT_FOUND); |
| 3280 |
goto leave; |
| 3281 |
} |
| 3282 |
n = _ksba_asn_find_node (attr, "Attribute.attrType"); |
| 3283 |
if (!n) |
| 3284 |
{ |
| 3285 |
err = gpg_error (GPG_ERR_ELEMENT_NOT_FOUND); |
| 3286 |
goto leave; |
| 3287 |
} |
| 3288 |
err = _ksba_der_store_oid (n, oidstr_signingTime); |
| 3289 |
if (err) |
| 3290 |
goto leave; |
| 3291 |
n = _ksba_asn_find_node (attr, "Attribute.attrValues"); |
| 3292 |
if (!n || !n->down) |
| 3293 |
{ |
| 3294 |
err = gpg_error (GPG_ERR_ELEMENT_NOT_FOUND); |
| 3295 |
goto leave; |
| 3296 |
} |
| 3297 |
n = n->down; /* fixme: ugly hack */ |
| 3298 |
err = _ksba_der_store_time (n, certlist->signing_time); |
| 3299 |
if (err) |
| 3300 |
goto leave; |
| 3301 |
err = _ksba_der_encode_tree (attr, &image, &imagelen); |
| 3302 |
if (err) |
| 3303 |
goto leave; |
| 3304 |
/* We will use the attributes again - so save them */ |
| 3305 |
attrarray[attridx].root = attr; |
| 3306 |
attrarray[attridx].image = image; |
| 3307 |
attrarray[attridx].imagelen = imagelen; |
| 3308 |
attridx++; |
| 3309 |
} |
| 3310 |
|
| 3311 |
/* Include the S/MIME capabilities with the first signer. */ |
| 3312 |
if (cms->capability_list && !signer) |
| 3313 |
{ |
| 3314 |
attr = _ksba_asn_expand_tree (cms_tree->parse_tree, |
| 3315 |
"CryptographicMessageSyntax.Attribute"); |
| 3316 |
if (!attr) |
| 3317 |
{ |
| 3318 |
err = gpg_error (GPG_ERR_ELEMENT_NOT_FOUND); |
| 3319 |
goto leave; |
| 3320 |
} |
| 3321 |
n = _ksba_asn_find_node (attr, "Attribute.attrType"); |
| 3322 |
if (!n) |
| 3323 |
{ |
| 3324 |
err = gpg_error (GPG_ERR_ELEMENT_NOT_FOUND); |
| 3325 |
goto leave; |
| 3326 |
} |
| 3327 |
err = _ksba_der_store_oid (n, oidstr_smimeCapabilities); |
| 3328 |
if (err) |
| 3329 |
goto leave; |
| 3330 |
n = _ksba_asn_find_node (attr, "Attribute.attrValues"); |
| 3331 |
if (!n || !n->down) |
| 3332 |
{ |
| 3333 |
err = gpg_error (GPG_ERR_ELEMENT_NOT_FOUND); |
| 3334 |
goto leave; |
| 3335 |
} |
| 3336 |
n = n->down; /* fixme: ugly hack */ |
| 3337 |
err = store_smime_capability_sequence (n, cms->capability_list); |
| 3338 |
if (err) |
| 3339 |
goto leave; |
| 3340 |
err = _ksba_der_encode_tree (attr, &image, &imagelen); |
| 3341 |
if (err) |
| 3342 |
goto leave; |
| 3343 |
attrarray[attridx].root = attr; |
| 3344 |
attrarray[attridx].image = image; |
| 3345 |
attrarray[attridx].imagelen = imagelen; |
| 3346 |
attridx++; |
| 3347 |
} |
| 3348 |
|
| 3349 |
for (opl = cms->attribute_list; opl; opl = opl->next) |
| 3350 |
{ |
| 3351 |
if (opl->unprotected) |
| 3352 |
continue; |
| 3353 |
if (!(opl->signeridx == -1 || opl->signeridx == signer)) |
| 3354 |
continue; |
| 3355 |
attr = _ksba_asn_expand_tree (cms_tree->parse_tree, |
| 3356 |
"CryptographicMessageSyntax.Attribute"); |
| 3357 |
if (!attr) |
| 3358 |
{ |
| 3359 |
err = gpg_error (GPG_ERR_ELEMENT_NOT_FOUND); |
| 3360 |
goto leave; |
| 3361 |
} |
| 3362 |
n = _ksba_asn_find_node (attr, "Attribute.attrType"); |
| 3363 |
if (!n) |
| 3364 |
{ |
| 3365 |
err = gpg_error (GPG_ERR_ELEMENT_NOT_FOUND); |
| 3366 |
goto leave; |
| 3367 |
} |
| 3368 |
err = _ksba_der_store_oid (n, opl->oid); |
| 3369 |
if (err) |
| 3370 |
goto leave; |
| 3371 |
n = _ksba_asn_find_node (attr, "Attribute.attrValues"); |
| 3372 |
if (!n || !n->down) |
| 3373 |
{ |
| 3374 |
err = gpg_error (GPG_ERR_ELEMENT_NOT_FOUND); |
| 3375 |
goto leave; |
| 3376 |
} |
| 3377 |
n = n->down; |
| 3378 |
/* gpgrt_log_printhex (opl->parm, opl->parmlen, */ |
| 3379 |
/* "signer %d, oid=%s der=", signer, opl->oid); */ |
| 3380 |
err = _ksba_der_store_set_of (n, opl->parm, opl->parmlen); |
| 3381 |
if (err) |
| 3382 |
goto leave; |
| 3383 |
|
| 3384 |
err = _ksba_der_encode_tree (attr, &image, &imagelen); |
| 3385 |
if (err) |
| 3386 |
goto leave; |
| 3387 |
|
| 3388 |
assert (attridx < attrsize); |
| 3389 |
attrarray[attridx].root = attr; |
| 3390 |
attrarray[attridx].image = image; |
| 3391 |
attrarray[attridx].imagelen = imagelen; |
| 3392 |
attridx++; |
| 3393 |
} |
| 3394 |
|
| 3395 |
/* Arggh. That silly ASN.1 DER encoding rules: We need to sort |
| 3396 |
the SET values. */ |
| 3397 |
qsort (attrarray, attridx, sizeof (struct attrarray_s), |
| 3398 |
compare_attrarray); |
| 3399 |
|
| 3400 |
/* Now copy them to an SignerInfo tree. This tree is not |
| 3401 |
complete but suitable for ksba_cms_hash_signed_attributes() */ |
| 3402 |
root = _ksba_asn_expand_tree (cms_tree->parse_tree, |
| 3403 |
"CryptographicMessageSyntax.SignerInfo"); |
| 3404 |
n = _ksba_asn_find_node (root, "SignerInfo.signedAttrs"); |
| 3405 |
if (!n || !n->down) |
| 3406 |
{ |
| 3407 |
err = gpg_error (GPG_ERR_ELEMENT_NOT_FOUND); |
| 3408 |
goto leave; |
| 3409 |
} |
| 3410 |
/* This is another ugly hack to move to the element we want */ |
| 3411 |
for (n = n->down->down; n && n->type != TYPE_SEQUENCE; n = n->right) |
| 3412 |
; |
| 3413 |
if (!n) |
| 3414 |
{ |
| 3415 |
err = gpg_error (GPG_ERR_ELEMENT_NOT_FOUND); |
| 3416 |
goto leave; |
| 3417 |
} |
| 3418 |
|
| 3419 |
assert (attridx <= attrsize); |
| 3420 |
for (i=0; i < attridx; i++) |
| 3421 |
{ |
| 3422 |
if (i) |
| 3423 |
{ |
| 3424 |
if ( !(n=_ksba_asn_insert_copy (n))) |
| 3425 |
{ |
| 3426 |
err = gpg_error (GPG_ERR_ENOMEM); |
| 3427 |
goto leave; |
| 3428 |
} |
| 3429 |
} |
| 3430 |
err = _ksba_der_copy_tree (n, attrarray[i].root, attrarray[i].image); |
| 3431 |
if (err) |
| 3432 |
goto leave; |
| 3433 |
_ksba_asn_release_nodes (attrarray[i].root); |
| 3434 |
free (attrarray[i].image); |
| 3435 |
attrarray[i].root = NULL; |
| 3436 |
attrarray[i].image = NULL; |
| 3437 |
} |
| 3438 |
|
| 3439 |
err = _ksba_der_encode_tree (root, &image, NULL); |
| 3440 |
if (err) |
| 3441 |
goto leave; |
| 3442 |
|
| 3443 |
si = xtrycalloc (1, sizeof *si); |
| 3444 |
if (!si) |
| 3445 |
return gpg_error (GPG_ERR_ENOMEM); |
| 3446 |
si->root = root; |
| 3447 |
root = NULL; |
| 3448 |
si->image = image; |
| 3449 |
/* Hmmm, we don't set the length of the image. */ |
| 3450 |
*si_tail = si; |
| 3451 |
si_tail = &si->next; |
| 3452 |
} |
| 3453 |
|
| 3454 |
leave: |
| 3455 |
_ksba_asn_release_nodes (root); |
| 3456 |
ksba_asn_tree_release (cms_tree); |
| 3457 |
for (i = 0; i < attridx; i++) |
| 3458 |
{ |
| 3459 |
_ksba_asn_release_nodes (attrarray[i].root); |
| 3460 |
xfree (attrarray[i].image); |
| 3461 |
} |
| 3462 |
|
| 3463 |
return err; |
| 3464 |
} |
| 3465 |
|
| 3466 |
|
| 3467 |
/* The user has calculated the signatures and we can therefore write |
| 3468 |
everything left over to do. */ |
| 3469 |
static gpg_error_t |
| 3470 |
build_signed_data_rest (ksba_cms_t cms) |
| 3471 |
{ |
| 3472 |
gpg_error_t err; |
| 3473 |
int signer; |
| 3474 |
ksba_asn_tree_t cms_tree = NULL; |
| 3475 |
struct certlist_s *certlist; |
| 3476 |
struct oidlist_s *digestlist; |
| 3477 |
struct signer_info_s *si; |
| 3478 |
struct sig_val_s *sv; |
| 3479 |
struct oidparmlist_s *opl; |
| 3480 |
ksba_writer_t tmpwrt = NULL; |
| 3481 |
AsnNode root = NULL; |
| 3482 |
ksba_der_t dbld = NULL; |
| 3483 |
struct attrarray_s *attrarray = NULL; |
| 3484 |
int attridx = 0; |
| 3485 |
unsigned int attrsize; |
| 3486 |
AsnNode attr = NULL; |
| 3487 |
int i; |
| 3488 |
|
| 3489 |
/* Now we can really write the signer info */ |
| 3490 |
err = ksba_asn_create_tree ("cms", &cms_tree); |
| 3491 |
if (err) |
| 3492 |
return err; |
| 3493 |
|
| 3494 |
certlist = cms->cert_list; |
| 3495 |
if (!certlist) |
| 3496 |
{ |
| 3497 |
err = gpg_error (GPG_ERR_MISSING_VALUE); /* oops */ |
| 3498 |
return err; |
| 3499 |
} |
| 3500 |
|
| 3501 |
/* To construct the set we use a temporary writer object. */ |
| 3502 |
err = ksba_writer_new (&tmpwrt); |
| 3503 |
if (err) |
| 3504 |
goto leave; |
| 3505 |
err = ksba_writer_set_mem (tmpwrt, 2048); |
| 3506 |
if (err) |
| 3507 |
goto leave; |
| 3508 |
|
| 3509 |
digestlist = cms->digest_algos; |
| 3510 |
si = cms->signer_info; |
| 3511 |
sv = cms->sig_val; |
| 3512 |
|
| 3513 |
for (signer=0; certlist; |
| 3514 |
signer++, |
| 3515 |
certlist = certlist->next, |
| 3516 |
digestlist = digestlist->next, |
| 3517 |
si = si->next, |
| 3518 |
sv = sv->next) |
| 3519 |
{ |
| 3520 |
AsnNode n, n2; |
| 3521 |
unsigned char *image; |
| 3522 |
size_t imagelen; |
| 3523 |
const char *oid; |
| 3524 |
|
| 3525 |
if (!digestlist || !si || !sv) |
| 3526 |
{ |
| 3527 |
err = gpg_error (GPG_ERR_MISSING_VALUE); /* oops */ |
| 3528 |
goto leave; |
| 3529 |
} |
| 3530 |
if (!certlist->cert || !digestlist->oid) |
| 3531 |
{ |
| 3532 |
err = gpg_error (GPG_ERR_BUG); |
| 3533 |
goto leave; |
| 3534 |
} |
| 3535 |
|
| 3536 |
root = _ksba_asn_expand_tree (cms_tree->parse_tree, |
| 3537 |
"CryptographicMessageSyntax.SignerInfo"); |
| 3538 |
|
| 3539 |
/* We store a version of 1 because we use the issuerAndSerialNumber */ |
| 3540 |
n = _ksba_asn_find_node (root, "SignerInfo.version"); |
| 3541 |
if (!n) |
| 3542 |
{ |
| 3543 |
err = gpg_error (GPG_ERR_ELEMENT_NOT_FOUND); |
| 3544 |
goto leave; |
| 3545 |
} |
| 3546 |
err = _ksba_der_store_integer (n, "\x00\x00\x00\x01\x01"); |
| 3547 |
if (err) |
| 3548 |
goto leave; |
| 3549 |
|
| 3550 |
/* Store the sid */ |
| 3551 |
n = _ksba_asn_find_node (root, "SignerInfo.sid"); |
| 3552 |
if (!n) |
| 3553 |
{ |
| 3554 |
err = gpg_error (GPG_ERR_ELEMENT_NOT_FOUND); |
| 3555 |
goto leave; |
| 3556 |
} |
| 3557 |
|
| 3558 |
err = set_issuer_serial (n, certlist->cert, 0); |
| 3559 |
if (err) |
| 3560 |
goto leave; |
| 3561 |
|
| 3562 |
/* store the digestAlgorithm */ |
| 3563 |
n = _ksba_asn_find_node (root, "SignerInfo.digestAlgorithm.algorithm"); |
| 3564 |
if (!n) |
| 3565 |
{ |
| 3566 |
err = gpg_error (GPG_ERR_ELEMENT_NOT_FOUND); |
| 3567 |
goto leave; |
| 3568 |
} |
| 3569 |
err = _ksba_der_store_oid (n, digestlist->oid); |
| 3570 |
if (err) |
| 3571 |
goto leave; |
| 3572 |
n = _ksba_asn_find_node (root, "SignerInfo.digestAlgorithm.parameters"); |
| 3573 |
if (!n) |
| 3574 |
{ |
| 3575 |
err = gpg_error (GPG_ERR_ELEMENT_NOT_FOUND); |
| 3576 |
goto leave; |
| 3577 |
} |
| 3578 |
err = _ksba_der_store_null (n); |
| 3579 |
if (err) |
| 3580 |
goto leave; |
| 3581 |
|
| 3582 |
/* and the signed attributes */ |
| 3583 |
n = _ksba_asn_find_node (root, "SignerInfo.signedAttrs"); |
| 3584 |
if (!n || !n->down) |
| 3585 |
{ |
| 3586 |
err = gpg_error (GPG_ERR_ELEMENT_NOT_FOUND); |
| 3587 |
goto leave; |
| 3588 |
} |
| 3589 |
assert (si->root); |
| 3590 |
assert (si->image); |
| 3591 |
n2 = _ksba_asn_find_node (si->root, "SignerInfo.signedAttrs"); |
| 3592 |
if (!n2 || !n2->down) |
| 3593 |
{ |
| 3594 |
err = gpg_error (GPG_ERR_ELEMENT_NOT_FOUND); |
| 3595 |
goto leave; |
| 3596 |
} |
| 3597 |
err = _ksba_der_copy_tree (n, n2, si->image); |
| 3598 |
if (err) |
| 3599 |
goto leave; |
| 3600 |
image = NULL; |
| 3601 |
|
| 3602 |
/* store the signatureAlgorithm */ |
| 3603 |
n = _ksba_asn_find_node (root, |
| 3604 |
"SignerInfo.signatureAlgorithm.algorithm"); |
| 3605 |
if (!n) |
| 3606 |
{ |
| 3607 |
err = gpg_error (GPG_ERR_ELEMENT_NOT_FOUND); |
| 3608 |
goto leave; |
| 3609 |
} |
| 3610 |
if (!sv->algo) |
| 3611 |
{ |
| 3612 |
err = gpg_error (GPG_ERR_MISSING_VALUE); |
| 3613 |
goto leave; |
| 3614 |
} |
| 3615 |
|
| 3616 |
if (!strcmp (sv->algo, "ecdsa")) |
| 3617 |
{ |
| 3618 |
/* Look at the digest algorithm and replace accordingly. */ |
| 3619 |
if (!strcmp (digestlist->oid, "2.16.840.1.101.3.4.2.1")) |
| 3620 |
oid = "1.2.840.10045.4.3.2"; /* ecdsa-with-SHA256 */ |
| 3621 |
else if (!strcmp (digestlist->oid, "2.16.840.1.101.3.4.2.2")) |
| 3622 |
oid = "1.2.840.10045.4.3.3"; /* ecdsa-with-SHA384 */ |
| 3623 |
else if (!strcmp (digestlist->oid, "2.16.840.1.101.3.4.2.3")) |
| 3624 |
oid = "1.2.840.10045.4.3.4"; /* ecdsa-with-SHA512 */ |
| 3625 |
else |
| 3626 |
{ |
| 3627 |
err = gpg_error (GPG_ERR_DIGEST_ALGO); |
| 3628 |
goto leave; |
| 3629 |
} |
| 3630 |
} |
| 3631 |
else |
| 3632 |
oid = sv->algo; |
| 3633 |
|
| 3634 |
err = _ksba_der_store_oid (n, oid); |
| 3635 |
if (err) |
| 3636 |
goto leave; |
| 3637 |
n = _ksba_asn_find_node (root, |
| 3638 |
"SignerInfo.signatureAlgorithm.parameters"); |
| 3639 |
if (!n) |
| 3640 |
{ |
| 3641 |
err = gpg_error (GPG_ERR_ELEMENT_NOT_FOUND); |
| 3642 |
goto leave; |
| 3643 |
} |
| 3644 |
err = _ksba_der_store_null (n); |
| 3645 |
if (err) |
| 3646 |
goto leave; |
| 3647 |
|
| 3648 |
/* store the signature */ |
| 3649 |
if (!sv->value) |
| 3650 |
{ |
| 3651 |
err = gpg_error (GPG_ERR_MISSING_VALUE); |
| 3652 |
goto leave; |
| 3653 |
} |
| 3654 |
n = _ksba_asn_find_node (root, "SignerInfo.signature"); |
| 3655 |
if (!n) |
| 3656 |
{ |
| 3657 |
err = gpg_error (GPG_ERR_ELEMENT_NOT_FOUND); |
| 3658 |
goto leave; |
| 3659 |
} |
| 3660 |
|
| 3661 |
if (sv->ecc.r) /* ECDSA */ |
| 3662 |
{ |
| 3663 |
unsigned char *tmpder; |
| 3664 |
size_t tmpderlen; |
| 3665 |
|
| 3666 |
_ksba_der_release (dbld); |
| 3667 |
dbld = _ksba_der_builder_new (0); |
| 3668 |
if (!dbld) |
| 3669 |
{ |
| 3670 |
err = gpg_error_from_syserror (); |
| 3671 |
goto leave; |
| 3672 |
} |
| 3673 |
_ksba_der_add_tag (dbld, 0, TYPE_SEQUENCE); |
| 3674 |
_ksba_der_add_int (dbld, sv->ecc.r, sv->ecc.rlen, 1); |
| 3675 |
_ksba_der_add_int (dbld, sv->value, sv->valuelen, 1); |
| 3676 |
_ksba_der_add_end (dbld); |
| 3677 |
|
| 3678 |
err = _ksba_der_builder_get (dbld, &tmpder, &tmpderlen); |
| 3679 |
if (err) |
| 3680 |
goto leave; |
| 3681 |
err = _ksba_der_store_octet_string (n, tmpder, tmpderlen); |
| 3682 |
xfree (tmpder); |
| 3683 |
if (err) |
| 3684 |
goto leave; |
| 3685 |
} |
| 3686 |
else /* RSA */ |
| 3687 |
{ |
| 3688 |
err = _ksba_der_store_octet_string (n, sv->value, sv->valuelen); |
| 3689 |
if (err) |
| 3690 |
goto leave; |
| 3691 |
} |
| 3692 |
|
| 3693 |
/* If we have any unsigned attributes we can now insert them |
| 3694 |
* directly into the tree. Note that the list may contain |
| 3695 |
* different (i.e. more) unsigned items than when the signed |
| 3696 |
* attributes were hashed. */ |
| 3697 |
for (attrsize = 0, opl = cms->attribute_list; opl; opl = opl->next) |
| 3698 |
{ |
| 3699 |
if (!opl->unprotected) |
| 3700 |
continue; |
| 3701 |
if (!(opl->signeridx == -1 || opl->signeridx == signer)) |
| 3702 |
continue; |
| 3703 |
attrsize++; |
| 3704 |
} |
| 3705 |
|
| 3706 |
if (attrsize) /* We have unsigned attributes - insert them. */ |
| 3707 |
{ |
| 3708 |
/* Allocate slots. */ |
| 3709 |
attrarray = xtrycalloc (attrsize, sizeof *attrarray); |
| 3710 |
if (!attrarray) |
| 3711 |
{ |
| 3712 |
err = gpg_error_from_syserror (); |
| 3713 |
goto leave; |
| 3714 |
} |
| 3715 |
|
| 3716 |
for (opl = cms->attribute_list; opl; opl = opl->next) |
| 3717 |
{ |
| 3718 |
if (!opl->unprotected) |
| 3719 |
continue; |
| 3720 |
if (!(opl->signeridx == -1 || opl->signeridx == signer)) |
| 3721 |
continue; |
| 3722 |
|
| 3723 |
attr = _ksba_asn_expand_tree (cms_tree->parse_tree, |
| 3724 |
"CryptographicMessageSyntax.Attribute"); |
| 3725 |
if (!attr) |
| 3726 |
{ |
| 3727 |
err = gpg_error (GPG_ERR_ELEMENT_NOT_FOUND); |
| 3728 |
goto leave; |
| 3729 |
} |
| 3730 |
n = _ksba_asn_find_node (attr, "Attribute.attrType"); |
| 3731 |
if (!n) |
| 3732 |
{ |
| 3733 |
err = gpg_error (GPG_ERR_ELEMENT_NOT_FOUND); |
| 3734 |
goto leave; |
| 3735 |
} |
| 3736 |
err = _ksba_der_store_oid (n, opl->oid); |
| 3737 |
if (err) |
| 3738 |
goto leave; |
| 3739 |
n = _ksba_asn_find_node (attr, "Attribute.attrValues"); |
| 3740 |
if (!n || !n->down) |
| 3741 |
{ |
| 3742 |
err = gpg_error (GPG_ERR_ELEMENT_NOT_FOUND); |
| 3743 |
goto leave; |
| 3744 |
} |
| 3745 |
n = n->down; |
| 3746 |
gpgrt_log_printhex (opl->parm, opl->parmlen, |
| 3747 |
"signer %d, oid=%s der=", signer, opl->oid); |
| 3748 |
err = _ksba_der_store_set_of (n, opl->parm, opl->parmlen); |
| 3749 |
if (err) |
| 3750 |
goto leave; |
| 3751 |
|
| 3752 |
err = _ksba_der_encode_tree (attr, &image, &imagelen); |
| 3753 |
if (err) |
| 3754 |
goto leave; |
| 3755 |
|
| 3756 |
assert (attridx < attrsize); |
| 3757 |
attrarray[attridx].root = attr; |
| 3758 |
attr = NULL; |
| 3759 |
attrarray[attridx].image = image; |
| 3760 |
attrarray[attridx].imagelen = imagelen; |
| 3761 |
attridx++; |
| 3762 |
} |
| 3763 |
|
| 3764 |
qsort (attrarray, attridx, sizeof (struct attrarray_s), |
| 3765 |
compare_attrarray); |
| 3766 |
/* Now insert them to an SignerInfo tree. */ |
| 3767 |
n = _ksba_asn_find_node (root, "SignerInfo.unsignedAttrs"); |
| 3768 |
if (!n || !n->down) |
| 3769 |
{ |
| 3770 |
err = gpg_error (GPG_ERR_ELEMENT_NOT_FOUND); |
| 3771 |
goto leave; |
| 3772 |
} |
| 3773 |
for (n = n->down->down; n && n->type != TYPE_SEQUENCE; n = n->right) |
| 3774 |
; |
| 3775 |
if (!n) |
| 3776 |
{ |
| 3777 |
err = gpg_error (GPG_ERR_ELEMENT_NOT_FOUND); |
| 3778 |
goto leave; |
| 3779 |
} |
| 3780 |
|
| 3781 |
assert (attridx <= attrsize); |
| 3782 |
for (i=0; i < attridx; i++) |
| 3783 |
{ |
| 3784 |
if (i) |
| 3785 |
{ |
| 3786 |
if ( !(n=_ksba_asn_insert_copy (n))) |
| 3787 |
{ |
| 3788 |
err = gpg_error (GPG_ERR_ENOMEM); |
| 3789 |
goto leave; |
| 3790 |
} |
| 3791 |
} |
| 3792 |
err = _ksba_der_copy_tree (n, attrarray[i].root, |
| 3793 |
attrarray[i].image); |
| 3794 |
if (err) |
| 3795 |
goto leave; |
| 3796 |
_ksba_asn_release_nodes (attrarray[i].root); |
| 3797 |
free (attrarray[i].image); |
| 3798 |
attrarray[i].root = NULL; |
| 3799 |
attrarray[i].image = NULL; |
| 3800 |
} |
| 3801 |
} /* End inserting unsigned attributes. */ |
| 3802 |
|
| 3803 |
/* Make the DER encoding and write it out. */ |
| 3804 |
err = _ksba_der_encode_tree (root, &image, &imagelen); |
| 3805 |
if (err) |
| 3806 |
goto leave; |
| 3807 |
|
| 3808 |
err = ksba_writer_write (tmpwrt, image, imagelen); |
| 3809 |
xfree (image); |
| 3810 |
if (err) |
| 3811 |
goto leave; |
| 3812 |
} |
| 3813 |
|
| 3814 |
/* Write out the SET filled with all signer infos */ |
| 3815 |
{ |
| 3816 |
unsigned char *value; |
| 3817 |
size_t valuelen; |
| 3818 |
|
| 3819 |
value = ksba_writer_snatch_mem (tmpwrt, &valuelen); |
| 3820 |
if (!value) |
| 3821 |
{ |
| 3822 |
err = gpg_error (GPG_ERR_ENOMEM); |
| 3823 |
goto leave; |
| 3824 |
} |
| 3825 |
err = _ksba_ber_write_tl (cms->writer, TYPE_SET, CLASS_UNIVERSAL, |
| 3826 |
1, valuelen); |
| 3827 |
if (!err) |
| 3828 |
err = ksba_writer_write (cms->writer, value, valuelen); |
| 3829 |
xfree (value); |
| 3830 |
if (err) |
| 3831 |
goto leave; |
| 3832 |
} |
| 3833 |
|
| 3834 |
/* Write 3 end tags */ |
| 3835 |
err = _ksba_ber_write_tl (cms->writer, 0, 0, 0, 0); |
| 3836 |
if (!err) |
| 3837 |
err = _ksba_ber_write_tl (cms->writer, 0, 0, 0, 0); |
| 3838 |
if (!err) |
| 3839 |
err = _ksba_ber_write_tl (cms->writer, 0, 0, 0, 0); |
| 3840 |
|
| 3841 |
leave: |
| 3842 |
ksba_asn_tree_release (cms_tree); |
| 3843 |
_ksba_asn_release_nodes (root); |
| 3844 |
_ksba_asn_release_nodes (attr); |
| 3845 |
ksba_writer_release (tmpwrt); |
| 3846 |
_ksba_der_release (dbld); |
| 3847 |
for (i = 0; i < attridx; i++) |
| 3848 |
{ |
| 3849 |
_ksba_asn_release_nodes (attrarray[i].root); |
| 3850 |
xfree (attrarray[i].image); |
| 3851 |
} |
| 3852 |
return err; |
| 3853 |
} |
| 3854 |
|
| 3855 |
|
| 3856 |
|
| 3857 |
|
| 3858 |
static gpg_error_t |
| 3859 |
ct_build_signed_data (ksba_cms_t cms) |
| 3860 |
{ |
| 3861 |
enum { |
| 3862 |
sSTART, |
| 3863 |
sDATAREADY, |
| 3864 |
sGOTSIG, |
| 3865 |
sERROR |
| 3866 |
} state = sERROR; |
| 3867 |
ksba_stop_reason_t stop_reason; |
| 3868 |
gpg_error_t err = 0; |
| 3869 |
|
| 3870 |
stop_reason = cms->stop_reason; |
| 3871 |
cms->stop_reason = KSBA_SR_RUNNING; |
| 3872 |
|
| 3873 |
/* Calculate state from last reason and do some checks */ |
| 3874 |
if (stop_reason == KSBA_SR_GOT_CONTENT) |
| 3875 |
{ |
| 3876 |
state = sSTART; |
| 3877 |
} |
| 3878 |
else if (stop_reason == KSBA_SR_BEGIN_DATA) |
| 3879 |
{ |
| 3880 |
/* fixme: check that the message digest has been set */ |
| 3881 |
state = sDATAREADY; |
| 3882 |
} |
| 3883 |
else if (stop_reason == KSBA_SR_END_DATA) |
| 3884 |
state = sDATAREADY; |
| 3885 |
else if (stop_reason == KSBA_SR_NEED_SIG) |
| 3886 |
{ |
| 3887 |
if (!cms->sig_val) /* No ksba_cms_set_sig_val () called */ |
| 3888 |
err = gpg_error (GPG_ERR_MISSING_ACTION); |
| 3889 |
state = sGOTSIG; |
| 3890 |
} |
| 3891 |
else if (stop_reason == KSBA_SR_RUNNING) |
| 3892 |
err = gpg_error (GPG_ERR_INV_STATE); |
| 3893 |
else if (stop_reason) |
| 3894 |
err = gpg_error (GPG_ERR_BUG); |
| 3895 |
|
| 3896 |
if (err) |
| 3897 |
return err; |
| 3898 |
|
| 3899 |
/* Do the action */ |
| 3900 |
if (state == sSTART) |
| 3901 |
{ |
| 3902 |
/* figure out whether a detached signature is requested */ |
| 3903 |
if (cms->cert_list && cms->cert_list->msg_digest_len) |
| 3904 |
cms->detached_data = 1; |
| 3905 |
else |
| 3906 |
cms->detached_data = 0; |
| 3907 |
/* and start encoding */ |
| 3908 |
err = build_signed_data_header (cms); |
| 3909 |
} |
| 3910 |
else if (state == sDATAREADY) |
| 3911 |
{ |
| 3912 |
if (!cms->detached_data) |
| 3913 |
err = _ksba_ber_write_tl (cms->writer, 0, 0, 0, 0); |
| 3914 |
if (!err) |
| 3915 |
err = build_signed_data_attributes (cms); |
| 3916 |
} |
| 3917 |
else if (state == sGOTSIG) |
| 3918 |
err = build_signed_data_rest (cms); |
| 3919 |
else |
| 3920 |
err = gpg_error (GPG_ERR_INV_STATE); |
| 3921 |
|
| 3922 |
if (err) |
| 3923 |
return err; |
| 3924 |
|
| 3925 |
/* Calculate new stop reason */ |
| 3926 |
if (state == sSTART) |
| 3927 |
{ |
| 3928 |
/* user should write the data and calculate the hash or do |
| 3929 |
nothing in case of END_DATA */ |
| 3930 |
stop_reason = cms->detached_data? KSBA_SR_END_DATA |
| 3931 |
: KSBA_SR_BEGIN_DATA; |
| 3932 |
} |
| 3933 |
else if (state == sDATAREADY) |
| 3934 |
stop_reason = KSBA_SR_NEED_SIG; |
| 3935 |
else if (state == sGOTSIG) |
| 3936 |
stop_reason = KSBA_SR_READY; |
| 3937 |
|
| 3938 |
cms->stop_reason = stop_reason; |
| 3939 |
return 0; |
| 3940 |
} |
| 3941 |
|
| 3942 |
|
| 3943 |
/* write everything up to the encryptedContentInfo including the tag */ |
| 3944 |
static gpg_error_t |
| 3945 |
build_enveloped_data_header (ksba_cms_t cms) |
| 3946 |
{ |
| 3947 |
gpg_error_t err; |
| 3948 |
int recpno; |
| 3949 |
struct certlist_s *certlist; |
| 3950 |
unsigned char *buf; |
| 3951 |
const char *s; |
| 3952 |
size_t len; |
| 3953 |
ksba_der_t dbld = NULL; |
| 3954 |
int any_ecdh = 0; |
| 3955 |
|
| 3956 |
/* See whether we have any ECDH recipients. */ |
| 3957 |
for (certlist = cms->cert_list; certlist; certlist = certlist->next) |
| 3958 |
if (certlist->enc_val.ecdh.e) |
| 3959 |
{ |
| 3960 |
any_ecdh = 1; |
| 3961 |
break; |
| 3962 |
} |
| 3963 |
|
| 3964 |
/* Write the outer contentInfo */ |
| 3965 |
/* fixme: code is shared with signed_data_header */ |
| 3966 |
err = _ksba_ber_write_tl (cms->writer, TYPE_SEQUENCE, CLASS_UNIVERSAL, 1, 0); |
| 3967 |
if (err) |
| 3968 |
return err; |
| 3969 |
err = ksba_oid_from_str (cms->content.oid, &buf, &len); |
| 3970 |
if (err) |
| 3971 |
return err; |
| 3972 |
err = _ksba_ber_write_tl (cms->writer, |
| 3973 |
TYPE_OBJECT_ID, CLASS_UNIVERSAL, 0, len); |
| 3974 |
if (!err) |
| 3975 |
err = ksba_writer_write (cms->writer, buf, len); |
| 3976 |
xfree (buf); |
| 3977 |
if (err) |
| 3978 |
return err; |
| 3979 |
|
| 3980 |
err = _ksba_ber_write_tl (cms->writer, 0, CLASS_CONTEXT, 1, 0); |
| 3981 |
if (err) |
| 3982 |
return err; |
| 3983 |
|
| 3984 |
/* The SEQUENCE */ |
| 3985 |
err = _ksba_ber_write_tl (cms->writer, TYPE_SEQUENCE, CLASS_UNIVERSAL, 1, 0); |
| 3986 |
if (err) |
| 3987 |
return err; |
| 3988 |
|
| 3989 |
/* figure out the CMSVersion to be used (from rfc2630): |
| 3990 |
version is the syntax version number. If originatorInfo is |
| 3991 |
present, then version shall be 2. If any of the RecipientInfo |
| 3992 |
structures included have a version other than 0, then the version |
| 3993 |
shall be 2. If unprotectedAttrs is present, then version shall |
| 3994 |
be 2. If originatorInfo is absent, all of the RecipientInfo |
| 3995 |
structures are version 0, and unprotectedAttrs is absent, then |
| 3996 |
version shall be 0. |
| 3997 |
|
| 3998 |
For SPHINX the version number must be 0. |
| 3999 |
*/ |
| 4000 |
|
| 4001 |
|
| 4002 |
s = any_ecdh? "\x02" :"\x00"; |
| 4003 |
err = _ksba_ber_write_tl (cms->writer, TYPE_INTEGER, CLASS_UNIVERSAL, 0, 1); |
| 4004 |
if (err) |
| 4005 |
return err; |
| 4006 |
err = ksba_writer_write (cms->writer, s, 1); |
| 4007 |
if (err) |
| 4008 |
return err; |
| 4009 |
|
| 4010 |
/* Note: originatorInfo is not yet implemented and must not be used |
| 4011 |
for SPHINX */ |
| 4012 |
|
| 4013 |
certlist = cms->cert_list; |
| 4014 |
if (!certlist) |
| 4015 |
{ |
| 4016 |
err = gpg_error (GPG_ERR_MISSING_VALUE); /* oops */ |
| 4017 |
goto leave; |
| 4018 |
} |
| 4019 |
|
| 4020 |
|
| 4021 |
dbld = _ksba_der_builder_new (0); |
| 4022 |
if (!dbld) |
| 4023 |
{ |
| 4024 |
err = gpg_error_from_syserror (); |
| 4025 |
goto leave; |
| 4026 |
} |
| 4027 |
|
| 4028 |
_ksba_der_add_tag (dbld, 0, TYPE_SET); |
| 4029 |
for (recpno=0; certlist; recpno++, certlist = certlist->next) |
| 4030 |
{ |
| 4031 |
const unsigned char *der; |
| 4032 |
size_t derlen; |
| 4033 |
|
| 4034 |
if (!certlist->cert) |
| 4035 |
{ |
| 4036 |
err = gpg_error (GPG_ERR_BUG); |
| 4037 |
goto leave; |
| 4038 |
} |
| 4039 |
|
| 4040 |
if (!certlist->enc_val.ecdh.e) /* RSA (ktri) */ |
| 4041 |
{ |
| 4042 |
_ksba_der_add_tag (dbld, 0, TYPE_SEQUENCE); |
| 4043 |
/* We store a version of 0 because we are only allowed to |
| 4044 |
* use the issuerAndSerialNumber for SPHINX */ |
| 4045 |
_ksba_der_add_ptr (dbld, 0, TYPE_INTEGER, "", 1); |
| 4046 |
/* rid.issuerAndSerialNumber */ |
| 4047 |
_ksba_der_add_tag (dbld, 0, TYPE_SEQUENCE); |
| 4048 |
/* rid.issuerAndSerialNumber.issuer */ |
| 4049 |
err = _ksba_cert_get_issuer_dn_ptr (certlist->cert, &der, &derlen); |
| 4050 |
if (err) |
| 4051 |
goto leave; |
| 4052 |
_ksba_der_add_der (dbld, der, derlen); |
| 4053 |
/* rid.issuerAndSerialNumber.serialNumber */ |
| 4054 |
err = _ksba_cert_get_serial_ptr (certlist->cert, &der, &derlen); |
| 4055 |
if (err) |
| 4056 |
goto leave; |
| 4057 |
_ksba_der_add_der (dbld, der, derlen); |
| 4058 |
_ksba_der_add_end (dbld); |
| 4059 |
|
| 4060 |
/* Store the keyEncryptionAlgorithm */ |
| 4061 |
_ksba_der_add_tag (dbld, 0, TYPE_SEQUENCE); |
| 4062 |
if (!certlist->enc_val.algo || !certlist->enc_val.value) |
| 4063 |
{ |
| 4064 |
err = gpg_error (GPG_ERR_MISSING_VALUE); |
| 4065 |
goto leave; |
| 4066 |
} |
| 4067 |
_ksba_der_add_oid (dbld, certlist->enc_val.algo); |
| 4068 |
/* Now store NULL for the optional parameters. From Peter |
| 4069 |
* Gutmann's X.509 style guide: |
| 4070 |
* |
| 4071 |
* Another pitfall to be aware of is that algorithms which |
| 4072 |
* have no parameters have this specified as a NULL value |
| 4073 |
* rather than omitting the parameters field entirely. The |
| 4074 |
* reason for this is that when the 1988 syntax for |
| 4075 |
* AlgorithmIdentifier was translated into the 1997 syntax, |
| 4076 |
* the OPTIONAL associated with the AlgorithmIdentifier |
| 4077 |
* parameters got lost. Later it was recovered via a defect |
| 4078 |
* report, but by then everyone thought that algorithm |
| 4079 |
* parameters were mandatory. Because of this the algorithm |
| 4080 |
* parameters should be specified as NULL, regardless of what |
| 4081 |
* you read elsewhere. |
| 4082 |
* |
| 4083 |
* The trouble is that things *never* get better, they just |
| 4084 |
* stay the same, only more so |
| 4085 |
* -- Terry Pratchett, "Eric" |
| 4086 |
* |
| 4087 |
* Although this is about signing, we always do it. Versions of |
| 4088 |
* Libksba before 1.0.6 had a bug writing out the NULL tag here, |
| 4089 |
* thus in reality we used to be correct according to the |
| 4090 |
* standards despite we didn't intended so. |
| 4091 |
*/ |
| 4092 |
_ksba_der_add_ptr (dbld, 0, TYPE_NULL, NULL, 0); |
| 4093 |
_ksba_der_add_end (dbld); |
| 4094 |
|
| 4095 |
/* Store the encryptedKey */ |
| 4096 |
if (!certlist->enc_val.value) |
| 4097 |
{ |
| 4098 |
err = gpg_error (GPG_ERR_MISSING_VALUE); |
| 4099 |
goto leave; |
| 4100 |
} |
| 4101 |
_ksba_der_add_ptr (dbld, 0, TYPE_OCTET_STRING, |
| 4102 |
certlist->enc_val.value, |
| 4103 |
certlist->enc_val.valuelen); |
| 4104 |
|
| 4105 |
} |
| 4106 |
else /* ECDH */ |
| 4107 |
{ |
| 4108 |
_ksba_der_add_tag (dbld, CLASS_CONTEXT, 1); /* kari */ |
| 4109 |
_ksba_der_add_ptr (dbld, 0, TYPE_INTEGER, "\x03", 1); |
| 4110 |
|
| 4111 |
_ksba_der_add_tag (dbld, CLASS_CONTEXT, 0); /* originator */ |
| 4112 |
_ksba_der_add_tag (dbld, CLASS_CONTEXT, 1); /* originatorKey */ |
| 4113 |
_ksba_der_add_tag (dbld, 0, TYPE_SEQUENCE); /* algorithm */ |
| 4114 |
_ksba_der_add_oid (dbld, certlist->enc_val.algo); |
| 4115 |
_ksba_der_add_end (dbld); |
| 4116 |
_ksba_der_add_bts (dbld, certlist->enc_val.ecdh.e, |
| 4117 |
certlist->enc_val.ecdh.elen, 0); |
| 4118 |
_ksba_der_add_end (dbld); /* end originatorKey */ |
| 4119 |
_ksba_der_add_end (dbld); /* end originator */ |
| 4120 |
|
| 4121 |
_ksba_der_add_tag (dbld, 0, TYPE_SEQUENCE); /* keyEncrAlgo */ |
| 4122 |
_ksba_der_add_oid (dbld, certlist->enc_val.ecdh.encr_algo); |
| 4123 |
_ksba_der_add_tag (dbld, 0, TYPE_SEQUENCE); |
| 4124 |
_ksba_der_add_oid (dbld, certlist->enc_val.ecdh.wrap_algo); |
| 4125 |
_ksba_der_add_end (dbld); |
| 4126 |
_ksba_der_add_end (dbld); /* end keyEncrAlgo */ |
| 4127 |
_ksba_der_add_tag (dbld, 0, TYPE_SEQUENCE); /* recpEncrKeys */ |
| 4128 |
_ksba_der_add_tag (dbld, 0, TYPE_SEQUENCE); /* recpEncrKey */ |
| 4129 |
|
| 4130 |
/* rid.issuerAndSerialNumber */ |
| 4131 |
_ksba_der_add_tag (dbld, 0, TYPE_SEQUENCE); |
| 4132 |
err = _ksba_cert_get_issuer_dn_ptr (certlist->cert, &der, &derlen); |
| 4133 |
if (err) |
| 4134 |
goto leave; |
| 4135 |
_ksba_der_add_der (dbld, der, derlen); |
| 4136 |
err = _ksba_cert_get_serial_ptr (certlist->cert, &der, &derlen); |
| 4137 |
if (err) |
| 4138 |
goto leave; |
| 4139 |
_ksba_der_add_der (dbld, der, derlen); |
| 4140 |
_ksba_der_add_end (dbld); |
| 4141 |
|
| 4142 |
/* encryptedKey */ |
| 4143 |
if (!certlist->enc_val.value) |
| 4144 |
{ |
| 4145 |
err = gpg_error (GPG_ERR_MISSING_VALUE); |
| 4146 |
goto leave; |
| 4147 |
} |
| 4148 |
_ksba_der_add_ptr (dbld, 0, TYPE_OCTET_STRING, |
| 4149 |
certlist->enc_val.value, |
| 4150 |
certlist->enc_val.valuelen); |
| 4151 |
|
| 4152 |
_ksba_der_add_end (dbld); /* end recpEncrKey */ |
| 4153 |
_ksba_der_add_end (dbld); /* end recpEncrKeys */ |
| 4154 |
} |
| 4155 |
|
| 4156 |
_ksba_der_add_end (dbld); /* End SEQUENCE (ktri or kari) */ |
| 4157 |
} |
| 4158 |
_ksba_der_add_end (dbld); /* End SET */ |
| 4159 |
|
| 4160 |
/* Write out the SET filled with all recipient infos */ |
| 4161 |
{ |
| 4162 |
unsigned char *image; |
| 4163 |
size_t imagelen; |
| 4164 |
|
| 4165 |
err = _ksba_der_builder_get (dbld, &image, &imagelen); |
| 4166 |
if (err) |
| 4167 |
goto leave; |
| 4168 |
err = ksba_writer_write (cms->writer, image, imagelen); |
| 4169 |
xfree (image); |
| 4170 |
if (err) |
| 4171 |
goto leave; |
| 4172 |
} |
| 4173 |
|
| 4174 |
/* Write the (inner) encryptedContentInfo */ |
| 4175 |
err = _ksba_ber_write_tl (cms->writer, TYPE_SEQUENCE, CLASS_UNIVERSAL, 1, 0); |
| 4176 |
if (err) |
| 4177 |
return err; |
| 4178 |
err = ksba_oid_from_str (cms->inner_cont_oid, &buf, &len); |
| 4179 |
if (err) |
| 4180 |
return err; |
| 4181 |
err = _ksba_ber_write_tl (cms->writer, |
| 4182 |
TYPE_OBJECT_ID, CLASS_UNIVERSAL, 0, len); |
| 4183 |
if (!err) |
| 4184 |
err = ksba_writer_write (cms->writer, buf, len); |
| 4185 |
xfree (buf); |
| 4186 |
if (err) |
| 4187 |
return err; |
| 4188 |
|
| 4189 |
/* and the encryptionAlgorithm */ |
| 4190 |
err = _ksba_der_write_algorithm_identifier (cms->writer, |
| 4191 |
cms->encr_algo_oid, |
| 4192 |
cms->encr_iv, |
| 4193 |
cms->encr_ivlen); |
| 4194 |
if (err) |
| 4195 |
return err; |
| 4196 |
|
| 4197 |
/* write the tag for the encrypted data, it is an implicit octect |
| 4198 |
string in constructed form and indefinite length */ |
| 4199 |
err = _ksba_ber_write_tl (cms->writer, 0, CLASS_CONTEXT, 1, 0); |
| 4200 |
if (err) |
| 4201 |
return err; |
| 4202 |
|
| 4203 |
/* Now the encrypted data should be written */ |
| 4204 |
|
| 4205 |
leave: |
| 4206 |
_ksba_der_release (dbld); |
| 4207 |
return err; |
| 4208 |
} |
| 4209 |
|
| 4210 |
|
| 4211 |
|
| 4212 |
/* Note that this function also handles authenveloped_data. */ |
| 4213 |
static gpg_error_t |
| 4214 |
ct_build_enveloped_data (ksba_cms_t cms) |
| 4215 |
{ |
| 4216 |
enum { |
| 4217 |
sSTART, |
| 4218 |
sINDATA, |
| 4219 |
sWAITTAG, |
| 4220 |
sREST, |
| 4221 |
sERROR |
| 4222 |
} state = sERROR; |
| 4223 |
ksba_stop_reason_t stop_reason; |
| 4224 |
gpg_error_t err = 0; |
| 4225 |
|
| 4226 |
stop_reason = cms->stop_reason; |
| 4227 |
cms->stop_reason = KSBA_SR_RUNNING; |
| 4228 |
|
| 4229 |
/* Calculate state from last reason and do some checks */ |
| 4230 |
if (stop_reason == KSBA_SR_GOT_CONTENT) |
| 4231 |
state = sSTART; |
| 4232 |
else if (stop_reason == KSBA_SR_BEGIN_DATA) |
| 4233 |
state = sINDATA; |
| 4234 |
else if (stop_reason == KSBA_SR_END_DATA) |
| 4235 |
state = cms->auth_mode? sWAITTAG : sREST; |
| 4236 |
else if (stop_reason == KSBA_SR_NEED_SIG) |
| 4237 |
{ |
| 4238 |
if (!cms->authdata.mac) /* ksba_cms_set_message_digest not called. */ |
| 4239 |
err = gpg_error (GPG_ERR_MISSING_ACTION); |
| 4240 |
state = sREST; |
| 4241 |
} |
| 4242 |
else if (stop_reason == KSBA_SR_RUNNING) |
| 4243 |
err = gpg_error (GPG_ERR_INV_STATE); |
| 4244 |
else if (stop_reason) |
| 4245 |
err = gpg_error (GPG_ERR_BUG); |
| 4246 |
|
| 4247 |
if (err) |
| 4248 |
return err; |
| 4249 |
|
| 4250 |
/* Do the action */ |
| 4251 |
if (state == sSTART) |
| 4252 |
err = build_enveloped_data_header (cms); |
| 4253 |
else if (state == sINDATA) |
| 4254 |
err = write_encrypted_cont (cms); |
| 4255 |
else if (state == sWAITTAG) |
| 4256 |
; /* Nothing to do here. */ |
| 4257 |
else if (state == sREST) |
| 4258 |
{ |
| 4259 |
/* SPHINX does not allow for unprotectedAttributes */ |
| 4260 |
|
| 4261 |
/* Write an end tag. */ |
| 4262 |
err = _ksba_ber_write_tl (cms->writer, 0, 0, 0, 0); |
| 4263 |
|
| 4264 |
/* In auth_mode write the tag. */ |
| 4265 |
if (!err && cms->auth_mode) |
| 4266 |
{ |
| 4267 |
err = _ksba_ber_write_tl (cms->writer, TYPE_OCTET_STRING, |
| 4268 |
CLASS_UNIVERSAL, 0, cms->authdata.mac_len); |
| 4269 |
if (!err) |
| 4270 |
err = ksba_writer_write (cms->writer, |
| 4271 |
cms->authdata.mac, cms->authdata.mac_len); |
| 4272 |
} |
| 4273 |
|
| 4274 |
/* Write remaining end tags */ |
| 4275 |
if (!err) |
| 4276 |
err = _ksba_ber_write_tl (cms->writer, 0, 0, 0, 0); |
| 4277 |
if (!err) |
| 4278 |
err = _ksba_ber_write_tl (cms->writer, 0, 0, 0, 0); |
| 4279 |
if (!err) |
| 4280 |
err = _ksba_ber_write_tl (cms->writer, 0, 0, 0, 0); |
| 4281 |
} |
| 4282 |
else |
| 4283 |
err = gpg_error (GPG_ERR_INV_STATE); |
| 4284 |
|
| 4285 |
if (err) |
| 4286 |
return err; |
| 4287 |
|
| 4288 |
/* Calculate new stop reason */ |
| 4289 |
if (state == sSTART) |
| 4290 |
{ /* user should now write the encrypted data */ |
| 4291 |
stop_reason = KSBA_SR_BEGIN_DATA; |
| 4292 |
} |
| 4293 |
else if (state == sINDATA) |
| 4294 |
{ /* tell the user that we wrote everything */ |
| 4295 |
stop_reason = KSBA_SR_END_DATA; |
| 4296 |
} |
| 4297 |
else if (state == sWAITTAG) |
| 4298 |
{ |
| 4299 |
stop_reason = KSBA_SR_NEED_SIG; |
| 4300 |
} |
| 4301 |
else if (state == sREST) |
| 4302 |
{ |
| 4303 |
stop_reason = KSBA_SR_READY; |
| 4304 |
} |
| 4305 |
|
| 4306 |
cms->stop_reason = stop_reason; |
| 4307 |
return 0; |
| 4308 |
} |
| 4309 |
|
| 4310 |
|
| 4311 |
static gpg_error_t |
| 4312 |
ct_build_digested_data (ksba_cms_t cms) |
| 4313 |
{ |
| 4314 |
(void)cms; |
| 4315 |
return gpg_error (GPG_ERR_NOT_IMPLEMENTED); |
| 4316 |
} |
| 4317 |
|
| 4318 |
|
| 4319 |
static gpg_error_t |
| 4320 |
ct_build_encrypted_data (ksba_cms_t cms) |
| 4321 |
{ |
| 4322 |
(void)cms; |
| 4323 |
return gpg_error (GPG_ERR_NOT_IMPLEMENTED); |
| 4324 |
} |