| 1 |
|
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 |
|
|---|
| 21 |
|
|---|
| 22 |
|
|---|
| 23 |
|
|---|
| 24 |
|
|---|
| 25 |
|
|---|
| 26 |
#include <openpgpsdk/signature.h> |
|---|
| 27 |
#include <openpgpsdk/readerwriter.h> |
|---|
| 28 |
#include <openpgpsdk/crypto.h> |
|---|
| 29 |
#include <openpgpsdk/create.h> |
|---|
| 30 |
#include <openpgpsdk/literal.h> |
|---|
| 31 |
#include <openpgpsdk/partial.h> |
|---|
| 32 |
#include <openpgpsdk/writer_armoured.h> |
|---|
| 33 |
|
|---|
| 34 |
#include <assert.h> |
|---|
| 35 |
#include <string.h> |
|---|
| 36 |
#include <fcntl.h> |
|---|
| 37 |
#include <unistd.h> |
|---|
| 38 |
|
|---|
| 39 |
#include <openpgpsdk/final.h> |
|---|
| 40 |
|
|---|
| 41 |
#include <openssl/dsa.h> |
|---|
| 42 |
|
|---|
| 43 |
static int debug=0; |
|---|
| 44 |
#define MAXBUF 1024 |
|---|
| 45 |
|
|---|
| 46 |
|
|---|
| 47 |
|
|---|
| 48 |
|
|---|
| 49 |
struct ops_create_signature |
|---|
| 50 |
{ |
|---|
| 51 |
ops_hash_t hash; |
|---|
| 52 |
ops_signature_t sig; |
|---|
| 53 |
ops_memory_t *mem; |
|---|
| 54 |
ops_create_info_t *info; |
|---|
| 55 |
unsigned hashed_count_offset; |
|---|
| 56 |
unsigned hashed_data_length; |
|---|
| 57 |
unsigned unhashed_count_offset; |
|---|
| 58 |
}; |
|---|
| 59 |
|
|---|
| 60 |
|
|---|
| 61 |
|
|---|
| 62 |
|
|---|
| 63 |
|
|---|
| 64 |
|
|---|
| 65 |
|
|---|
| 66 |
|
|---|
| 67 |
ops_create_signature_t *ops_create_signature_new() |
|---|
| 68 |
{ return ops_mallocz(sizeof(ops_create_signature_t)); } |
|---|
| 69 |
|
|---|
| 70 |
|
|---|
| 71 |
|
|---|
| 72 |
|
|---|
| 73 |
|
|---|
| 74 |
|
|---|
| 75 |
|
|---|
| 76 |
void ops_create_signature_delete(ops_create_signature_t *sig) |
|---|
| 77 |
{ |
|---|
| 78 |
ops_create_info_delete(sig->info); |
|---|
| 79 |
sig->info=NULL; |
|---|
| 80 |
free(sig); |
|---|
| 81 |
} |
|---|
| 82 |
|
|---|
| 83 |
static unsigned char prefix_md5[]={ 0x30,0x20,0x30,0x0C,0x06,0x08,0x2A,0x86, |
|---|
| 84 |
0x48,0x86,0xF7,0x0D,0x02,0x05,0x05,0x00, |
|---|
| 85 |
0x04,0x10 }; |
|---|
| 86 |
|
|---|
| 87 |
static unsigned char prefix_sha1[]={ 0x30,0x21,0x30,0x09,0x06,0x05,0x2b,0x0E, |
|---|
| 88 |
0x03,0x02,0x1A,0x05,0x00,0x04,0x14 }; |
|---|
| 89 |
|
|---|
| 90 |
static unsigned char prefix_sha256[]={ 0x30,0x31,0x30,0x0d,0x06,0x09,0x60,0x86, |
|---|
| 91 |
0x48,0x01,0x65,0x03,0x04,0x02,0x01,0x05, |
|---|
| 92 |
0x00,0x04,0x20 }; |
|---|
| 93 |
|
|---|
| 94 |
|
|---|
| 95 |
|
|---|
| 96 |
|
|---|
| 97 |
|
|---|
| 98 |
|
|---|
| 99 |
|
|---|
| 100 |
|
|---|
| 101 |
|
|---|
| 102 |
|
|---|
| 103 |
ops_boolean_t encode_hash_buf(const unsigned char *M, size_t mLen, |
|---|
| 104 |
const ops_hash_algorithm_t hash_alg, |
|---|
| 105 |
unsigned char* EM) |
|---|
| 106 |
{ |
|---|
| 107 |
|
|---|
| 108 |
|
|---|
| 109 |
unsigned i; |
|---|
| 110 |
int n; |
|---|
| 111 |
ops_hash_t hash; |
|---|
| 112 |
int hash_sz=0; |
|---|
| 113 |
|
|---|
| 114 |
int prefix_sz=0; |
|---|
| 115 |
unsigned padding_sz=0; |
|---|
| 116 |
unsigned encoded_msg_sz=0; |
|---|
| 117 |
unsigned char* prefix=NULL; |
|---|
| 118 |
|
|---|
| 119 |
assert(hash_alg == OPS_HASH_SHA1); |
|---|
| 120 |
|
|---|
| 121 |
|
|---|
| 122 |
|
|---|
| 123 |
ops_hash_any(&hash, hash_alg); |
|---|
| 124 |
hash.init(&hash); |
|---|
| 125 |
hash.add(&hash, M, mLen); |
|---|
| 126 |
|
|---|
| 127 |
|
|---|
| 128 |
|
|---|
| 129 |
|
|---|
| 130 |
|
|---|
| 131 |
switch(hash_alg) |
|---|
| 132 |
{ |
|---|
| 133 |
case OPS_HASH_SHA1: |
|---|
| 134 |
prefix=prefix_sha1; |
|---|
| 135 |
prefix_sz=sizeof prefix_sha1; |
|---|
| 136 |
hash_sz=OPS_SHA1_HASH_SIZE; |
|---|
| 137 |
|
|---|
| 138 |
|
|---|
| 139 |
|
|---|
| 140 |
padding_sz=90; |
|---|
| 141 |
break; |
|---|
| 142 |
|
|---|
| 143 |
default: |
|---|
| 144 |
assert(0); |
|---|
| 145 |
} |
|---|
| 146 |
|
|---|
| 147 |
|
|---|
| 148 |
|
|---|
| 149 |
|
|---|
| 150 |
|
|---|
| 151 |
EM[0]=0x00; |
|---|
| 152 |
EM[1]=0x01; |
|---|
| 153 |
|
|---|
| 154 |
for (i=0; i<padding_sz; i++) |
|---|
| 155 |
EM[2+i]=0xFF; |
|---|
| 156 |
|
|---|
| 157 |
i+=2; |
|---|
| 158 |
|
|---|
| 159 |
EM[i++]=0x00; |
|---|
| 160 |
|
|---|
| 161 |
memcpy(&EM[i], prefix, prefix_sz); |
|---|
| 162 |
i+=prefix_sz; |
|---|
| 163 |
|
|---|
| 164 |
|
|---|
| 165 |
|
|---|
| 166 |
n=hash.finish(&hash, &EM[i]); |
|---|
| 167 |
assert(n == hash_sz); |
|---|
| 168 |
|
|---|
| 169 |
encoded_msg_sz=i+hash_sz-1; |
|---|
| 170 |
|
|---|
| 171 |
|
|---|
| 172 |
|
|---|
| 173 |
if (debug) |
|---|
| 174 |
{ |
|---|
| 175 |
fprintf(stderr, "Encoded Message: \n"); |
|---|
| 176 |
for (i=0; i<encoded_msg_sz; i++) |
|---|
| 177 |
fprintf(stderr, "%2x ", EM[i]); |
|---|
| 178 |
fprintf(stderr, "\n"); |
|---|
| 179 |
} |
|---|
| 180 |
|
|---|
| 181 |
return ops_true; |
|---|
| 182 |
} |
|---|
| 183 |
|
|---|
| 184 |
|
|---|
| 185 |
|
|---|
| 186 |
static void rsa_sign(ops_hash_t *hash, const ops_rsa_public_key_t *rsa, |
|---|
| 187 |
const ops_rsa_secret_key_t *srsa, ops_create_info_t *opt) |
|---|
| 188 |
{ |
|---|
| 189 |
unsigned char hashbuf[8192]; |
|---|
| 190 |
unsigned char sigbuf[8192]; |
|---|
| 191 |
unsigned keysize; |
|---|
| 192 |
unsigned hashsize; |
|---|
| 193 |
unsigned n; |
|---|
| 194 |
unsigned t; |
|---|
| 195 |
BIGNUM *bn; |
|---|
| 196 |
|
|---|
| 197 |
|
|---|
| 198 |
hashsize=20+sizeof prefix_sha1; |
|---|
| 199 |
|
|---|
| 200 |
keysize=BN_num_bytes(rsa->n); |
|---|
| 201 |
assert(keysize <= sizeof hashbuf); |
|---|
| 202 |
assert(10+hashsize <= keysize); |
|---|
| 203 |
|
|---|
| 204 |
hashbuf[0]=0; |
|---|
| 205 |
hashbuf[1]=1; |
|---|
| 206 |
if (debug) |
|---|
| 207 |
printf("rsa_sign: PS is %d\n", keysize-hashsize-1-2); |
|---|
| 208 |
for(n=2 ; n < keysize-hashsize-1 ; ++n) |
|---|
| 209 |
hashbuf[n]=0xff; |
|---|
| 210 |
hashbuf[n++]=0; |
|---|
| 211 |
|
|---|
| 212 |
memcpy(&hashbuf[n], prefix_sha1, sizeof prefix_sha1); |
|---|
| 213 |
n+=sizeof prefix_sha1; |
|---|
| 214 |
|
|---|
| 215 |
t=hash->finish(hash, &hashbuf[n]); |
|---|
| 216 |
assert(t == 20); |
|---|
| 217 |
|
|---|
| 218 |
ops_write(&hashbuf[n], 2, opt); |
|---|
| 219 |
|
|---|
| 220 |
n+=t; |
|---|
| 221 |
assert(n == keysize); |
|---|
| 222 |
|
|---|
| 223 |
t=ops_rsa_private_encrypt(sigbuf, hashbuf, keysize, srsa, rsa); |
|---|
| 224 |
bn=BN_bin2bn(sigbuf, t, NULL); |
|---|
| 225 |
ops_write_mpi(bn, opt); |
|---|
| 226 |
BN_free(bn); |
|---|
| 227 |
} |
|---|
| 228 |
|
|---|
| 229 |
static void dsa_sign(ops_hash_t *hash, const ops_dsa_public_key_t *dsa, |
|---|
| 230 |
const ops_dsa_secret_key_t *sdsa, ops_create_info_t *cinfo) |
|---|
| 231 |
{ |
|---|
| 232 |
unsigned char hashbuf[8192]; |
|---|
| 233 |
unsigned hashsize; |
|---|
| 234 |
unsigned t; |
|---|
| 235 |
|
|---|
| 236 |
|
|---|
| 237 |
|
|---|
| 238 |
|
|---|
| 239 |
|
|---|
| 240 |
hashsize=20; |
|---|
| 241 |
|
|---|
| 242 |
|
|---|
| 243 |
t=hash->finish(hash, &hashbuf[0]); |
|---|
| 244 |
assert(t == 20); |
|---|
| 245 |
|
|---|
| 246 |
ops_write(&hashbuf[0], 2, cinfo); |
|---|
| 247 |
|
|---|
| 248 |
|
|---|
| 249 |
DSA_SIG* dsasig; |
|---|
| 250 |
dsasig=ops_dsa_sign(hashbuf, hashsize, sdsa, dsa); |
|---|
| 251 |
|
|---|
| 252 |
|
|---|
| 253 |
ops_write_mpi(dsasig->r, cinfo); |
|---|
| 254 |
ops_write_mpi(dsasig->s, cinfo); |
|---|
| 255 |
DSA_SIG_free(dsasig); |
|---|
| 256 |
} |
|---|
| 257 |
|
|---|
| 258 |
static ops_boolean_t rsa_verify(ops_hash_algorithm_t type, |
|---|
| 259 |
const unsigned char *hash, size_t hash_length, |
|---|
| 260 |
const ops_rsa_signature_t *sig, |
|---|
| 261 |
const ops_rsa_public_key_t *rsa) |
|---|
| 262 |
{ |
|---|
| 263 |
unsigned char sigbuf[8192]; |
|---|
| 264 |
unsigned char hashbuf_from_sig[8192]; |
|---|
| 265 |
unsigned n; |
|---|
| 266 |
unsigned keysize; |
|---|
| 267 |
unsigned char *prefix; |
|---|
| 268 |
int plen; |
|---|
| 269 |
|
|---|
| 270 |
keysize=BN_num_bytes(rsa->n); |
|---|
| 271 |
|
|---|
| 272 |
assert(keysize <= sizeof hashbuf_from_sig); |
|---|
| 273 |
assert((unsigned)BN_num_bits(sig->sig) <= 8*sizeof sigbuf); |
|---|
| 274 |
BN_bn2bin(sig->sig, sigbuf); |
|---|
| 275 |
|
|---|
| 276 |
n=ops_rsa_public_decrypt(hashbuf_from_sig, sigbuf, BN_num_bytes(sig->sig), |
|---|
| 277 |
rsa); |
|---|
| 278 |
int debug_len_decrypted=n; |
|---|
| 279 |
|
|---|
| 280 |
if(n != keysize) |
|---|
| 281 |
return ops_false; |
|---|
| 282 |
|
|---|
| 283 |
|
|---|
| 284 |
|
|---|
| 285 |
if(hashbuf_from_sig[0] != 0 || hashbuf_from_sig[1] != 1) |
|---|
| 286 |
return ops_false; |
|---|
| 287 |
|
|---|
| 288 |
switch(type) |
|---|
| 289 |
{ |
|---|
| 290 |
case OPS_HASH_MD5: prefix=prefix_md5; plen=sizeof prefix_md5; break; |
|---|
| 291 |
case OPS_HASH_SHA1: prefix=prefix_sha1; plen=sizeof prefix_sha1; break; |
|---|
| 292 |
case OPS_HASH_SHA256: prefix=prefix_sha256; plen=sizeof prefix_sha256; break; |
|---|
| 293 |
default: assert(0); break; |
|---|
| 294 |
} |
|---|
| 295 |
|
|---|
| 296 |
if(keysize-plen-hash_length < 10) |
|---|
| 297 |
return ops_false; |
|---|
| 298 |
|
|---|
| 299 |
for(n=2 ; n < keysize-plen-hash_length-1 ; ++n) |
|---|
| 300 |
if(hashbuf_from_sig[n] != 0xff) |
|---|
| 301 |
return ops_false; |
|---|
| 302 |
|
|---|
| 303 |
if(hashbuf_from_sig[n++] != 0) |
|---|
| 304 |
return ops_false; |
|---|
| 305 |
|
|---|
| 306 |
if (debug) |
|---|
| 307 |
{ |
|---|
| 308 |
int zz; |
|---|
| 309 |
|
|---|
| 310 |
printf("\n"); |
|---|
| 311 |
printf("hashbuf_from_sig\n"); |
|---|
| 312 |
for (zz=0; zz<debug_len_decrypted; zz++) |
|---|
| 313 |
printf("%02x ", hashbuf_from_sig[n+zz]); |
|---|
| 314 |
printf("\n"); |
|---|
| 315 |
printf("prefix\n"); |
|---|
| 316 |
for (zz=0; zz<plen; zz++) |
|---|
| 317 |
printf("%02x ", prefix[zz]); |
|---|
| 318 |
printf("\n"); |
|---|
| 319 |
|
|---|
| 320 |
printf("\n"); |
|---|
| 321 |
printf("hash from sig\n"); |
|---|
| 322 |
unsigned uu; |
|---|
| 323 |
for (uu=0; uu<hash_length; uu++) |
|---|
| 324 |
printf("%02x ", hashbuf_from_sig[n+plen+uu]); |
|---|
| 325 |
printf("\n"); |
|---|
| 326 |
printf("hash passed in (should match hash from sig)\n"); |
|---|
| 327 |
for (uu=0; uu<hash_length; uu++) |
|---|
| 328 |
printf("%02x ", hash[uu]); |
|---|
| 329 |
printf("\n"); |
|---|
| 330 |
} |
|---|
| 331 |
if(memcmp(&hashbuf_from_sig[n], prefix, plen) |
|---|
| 332 |
|| memcmp(&hashbuf_from_sig[n+plen], hash, hash_length)) |
|---|
| 333 |
return ops_false; |
|---|
| 334 |
|
|---|
| 335 |
return ops_true; |
|---|
| 336 |
} |
|---|
| 337 |
|
|---|
| 338 |
static void hash_add_key(ops_hash_t *hash, const ops_public_key_t *key) |
|---|
| 339 |
{ |
|---|
| 340 |
ops_memory_t *mem=ops_memory_new(); |
|---|
| 341 |
size_t l; |
|---|
| 342 |
|
|---|
| 343 |
ops_build_public_key(mem, key, ops_false); |
|---|
| 344 |
|
|---|
| 345 |
l=ops_memory_get_length(mem); |
|---|
| 346 |
ops_hash_add_int(hash, 0x99, 1); |
|---|
| 347 |
ops_hash_add_int(hash, l, 2); |
|---|
| 348 |
hash->add(hash, ops_memory_get_data(mem), l); |
|---|
| 349 |
|
|---|
| 350 |
ops_memory_free(mem); |
|---|
| 351 |
} |
|---|
| 352 |
|
|---|
| 353 |
static void initialise_hash(ops_hash_t *hash, const ops_signature_t *sig) |
|---|
| 354 |
{ |
|---|
| 355 |
ops_hash_any(hash, sig->info.hash_algorithm); |
|---|
| 356 |
hash->init(hash); |
|---|
| 357 |
} |
|---|
| 358 |
|
|---|
| 359 |
static void init_key_signature(ops_hash_t *hash, const ops_signature_t *sig, |
|---|
| 360 |
const ops_public_key_t *key) |
|---|
| 361 |
{ |
|---|
| 362 |
initialise_hash(hash, sig); |
|---|
| 363 |
hash_add_key(hash, key); |
|---|
| 364 |
} |
|---|
| 365 |
|
|---|
| 366 |
static void hash_add_trailer(ops_hash_t *hash, const ops_signature_t *sig, |
|---|
| 367 |
const unsigned char *raw_packet) |
|---|
| 368 |
{ |
|---|
| 369 |
if(sig->info.version == OPS_V4) |
|---|
| 370 |
{ |
|---|
| 371 |
if(raw_packet) |
|---|
| 372 |
hash->add(hash, raw_packet+sig->v4_hashed_data_start, |
|---|
| 373 |
sig->info.v4_hashed_data_length); |
|---|
| 374 |
ops_hash_add_int(hash, sig->info.version, 1); |
|---|
| 375 |
ops_hash_add_int(hash, 0xff, 1); |
|---|
| 376 |
ops_hash_add_int(hash, sig->info.v4_hashed_data_length, 4); |
|---|
| 377 |
} |
|---|
| 378 |
else |
|---|
| 379 |
{ |
|---|
| 380 |
ops_hash_add_int(hash, sig->info.type, 1); |
|---|
| 381 |
ops_hash_add_int(hash, sig->info.creation_time, 4); |
|---|
| 382 |
} |
|---|
| 383 |
} |
|---|
| 384 |
|
|---|
| 385 |
|
|---|
| 386 |
|
|---|
| 387 |
|
|---|
| 388 |
|
|---|
| 389 |
|
|---|
| 390 |
|
|---|
| 391 |
|
|---|
| 392 |
|
|---|
| 393 |
|
|---|
| 394 |
ops_boolean_t ops_check_signature(const unsigned char *hash, unsigned length, |
|---|
| 395 |
const ops_signature_t *sig, |
|---|
| 396 |
const ops_public_key_t *signer) |
|---|
| 397 |
{ |
|---|
| 398 |
ops_boolean_t ret; |
|---|
| 399 |
|
|---|
| 400 |
|
|---|
| 401 |
|
|---|
| 402 |
|
|---|
| 403 |
|
|---|
| 404 |
|
|---|
| 405 |
|
|---|
| 406 |
switch(sig->info.key_algorithm) |
|---|
| 407 |
{ |
|---|
| 408 |
case OPS_PKA_DSA: |
|---|
| 409 |
ret=ops_dsa_verify(hash, length, &sig->info.signature.dsa, |
|---|
| 410 |
&signer->key.dsa); |
|---|
| 411 |
break; |
|---|
| 412 |
|
|---|
| 413 |
case OPS_PKA_RSA: |
|---|
| 414 |
ret=rsa_verify(sig->info.hash_algorithm, hash, length, |
|---|
| 415 |
&sig->info.signature.rsa, &signer->key.rsa); |
|---|
| 416 |
break; |
|---|
| 417 |
|
|---|
| 418 |
default: |
|---|
| 419 |
assert(0); |
|---|
| 420 |
} |
|---|
| 421 |
|
|---|
| 422 |
return ret; |
|---|
| 423 |
} |
|---|
| 424 |
|
|---|
| 425 |
static ops_boolean_t hash_and_check_signature(ops_hash_t *hash, |
|---|
| 426 |
const ops_signature_t *sig, |
|---|
| 427 |
const ops_public_key_t *signer) |
|---|
| 428 |
{ |
|---|
| 429 |
int n; |
|---|
| 430 |
unsigned char hashout[OPS_MAX_HASH_SIZE]; |
|---|
| 431 |
|
|---|
| 432 |
n=hash->finish(hash, hashout); |
|---|
| 433 |
|
|---|
| 434 |
return ops_check_signature(hashout, n, sig, signer); |
|---|
| 435 |
} |
|---|
| 436 |
|
|---|
| 437 |
static ops_boolean_t finalise_signature(ops_hash_t *hash, |
|---|
| 438 |
const ops_signature_t *sig, |
|---|
| 439 |
const ops_public_key_t *signer, |
|---|
| 440 |
const unsigned char *raw_packet) |
|---|
| 441 |
{ |
|---|
| 442 |
hash_add_trailer(hash, sig, raw_packet); |
|---|
| 443 |
return hash_and_check_signature(hash, sig, signer); |
|---|
| 444 |
} |
|---|
| 445 |
|
|---|
| 446 |
|
|---|
| 447 |
|
|---|
| 448 |
|
|---|
| 449 |
|
|---|
| 450 |
|
|---|
| 451 |
|
|---|
| 452 |
|
|---|
| 453 |
|
|---|
| 454 |
|
|---|
| 455 |
|
|---|
| 456 |
|
|---|
| 457 |
|
|---|
| 458 |
ops_boolean_t |
|---|
| 459 |
ops_check_user_id_certification_signature(const ops_public_key_t *key, |
|---|
| 460 |
const ops_user_id_t *id, |
|---|
| 461 |
const ops_signature_t *sig, |
|---|
| 462 |
const ops_public_key_t *signer, |
|---|
| 463 |
const unsigned char *raw_packet) |
|---|
| 464 |
{ |
|---|
| 465 |
ops_hash_t hash; |
|---|
| 466 |
size_t user_id_len=strlen((char *)id->user_id); |
|---|
| 467 |
|
|---|
| 468 |
init_key_signature(&hash, sig, key); |
|---|
| 469 |
|
|---|
| 470 |
if(sig->info.version == OPS_V4) |
|---|
| 471 |
{ |
|---|
| 472 |
ops_hash_add_int(&hash, 0xb4, 1); |
|---|
| 473 |
ops_hash_add_int(&hash, user_id_len, 4); |
|---|
| 474 |
} |
|---|
| 475 |
hash.add(&hash, id->user_id, user_id_len); |
|---|
| 476 |
|
|---|
| 477 |
return finalise_signature(&hash, sig, signer, raw_packet); |
|---|
| 478 |
} |
|---|
| 479 |
|
|---|
| 480 |
|
|---|
| 481 |
|
|---|
| 482 |
|
|---|
| 483 |
|
|---|
| 484 |
|
|---|
| 485 |
|
|---|
| 486 |
|
|---|
| 487 |
|
|---|
| 488 |
|
|---|
| 489 |
|
|---|
| 490 |
|
|---|
| 491 |
|
|---|
| 492 |
ops_boolean_t |
|---|
| 493 |
ops_check_user_attribute_certification_signature(const ops_public_key_t *key, |
|---|
| 494 |
const ops_user_attribute_t *attribute, |
|---|
| 495 |
const ops_signature_t *sig, |
|---|
| 496 |
const ops_public_key_t *signer, |
|---|
| 497 |
const unsigned char *raw_packet) |
|---|
| 498 |
{ |
|---|
| 499 |
ops_hash_t hash; |
|---|
| 500 |
|
|---|
| 501 |
init_key_signature(&hash, sig, key); |
|---|
| 502 |
|
|---|
| 503 |
if(sig->info.version == OPS_V4) |
|---|
| 504 |
{ |
|---|
| 505 |
ops_hash_add_int(&hash, 0xd1, 1); |
|---|
| 506 |
ops_hash_add_int(&hash, attribute->data.len, 4); |
|---|
| 507 |
} |
|---|
| 508 |
hash.add(&hash, attribute->data.contents, attribute->data.len); |
|---|
| 509 |
|
|---|
| 510 |
return finalise_signature(&hash, sig, signer, raw_packet); |
|---|
| 511 |
} |
|---|
| 512 |
|
|---|
| 513 |
|
|---|
| 514 |
|
|---|
| 515 |
|
|---|
| 516 |
|
|---|
| 517 |
|
|---|
| 518 |
|
|---|
| 519 |
|
|---|
| 520 |
|
|---|
| 521 |
|
|---|
| 522 |
|
|---|
| 523 |
|
|---|
| 524 |
|
|---|
| 525 |
ops_boolean_t |
|---|
| 526 |
ops_check_subkey_signature(const ops_public_key_t *key, |
|---|
| 527 |
const ops_public_key_t *subkey, |
|---|
| 528 |
const ops_signature_t *sig, |
|---|
| 529 |
const ops_public_key_t *signer, |
|---|
| 530 |
const unsigned char *raw_packet) |
|---|
| 531 |
{ |
|---|
| 532 |
ops_hash_t hash; |
|---|
| 533 |
|
|---|
| 534 |
init_key_signature(&hash, sig, key); |
|---|
| 535 |
hash_add_key(&hash, subkey); |
|---|
| 536 |
|
|---|
| 537 |
return finalise_signature(&hash, sig, signer, raw_packet); |
|---|
| 538 |
} |
|---|
| 539 |
|
|---|
| 540 |
|
|---|
| 541 |
|
|---|
| 542 |
|
|---|
| 543 |
|
|---|
| 544 |
|
|---|
| 545 |
|
|---|
| 546 |
|
|---|
| 547 |
|
|---|
| 548 |
|
|---|
| 549 |
|
|---|
| 550 |
|
|---|
| 551 |
ops_boolean_t |
|---|
| 552 |
ops_check_direct_signature(const ops_public_key_t *key, |
|---|
| 553 |
const ops_signature_t *sig, |
|---|
| 554 |
const ops_public_key_t *signer, |
|---|
| 555 |
const unsigned char *raw_packet) |
|---|
| 556 |
{ |
|---|
| 557 |
ops_hash_t hash; |
|---|
| 558 |
|
|---|
| 559 |
init_key_signature(&hash, sig, key); |
|---|
| 560 |
return finalise_signature(&hash, sig, signer, raw_packet); |
|---|
| 561 |
} |
|---|
| 562 |
|
|---|
| 563 |
|
|---|
| 564 |
|
|---|
| 565 |
|
|---|
| 566 |
|
|---|
| 567 |
|
|---|
| 568 |
|
|---|
| 569 |
|
|---|
| 570 |
|
|---|
| 571 |
|
|---|
| 572 |
|
|---|
| 573 |
|
|---|
| 574 |
|
|---|
| 575 |
ops_boolean_t |
|---|
| 576 |
ops_check_hash_signature(ops_hash_t *hash, const ops_signature_t *sig, |
|---|
| 577 |
const ops_public_key_t *signer) |
|---|
| 578 |
{ |
|---|
| 579 |
if(sig->info.hash_algorithm != hash->algorithm) |
|---|
| 580 |
return ops_false; |
|---|
| 581 |
|
|---|
| 582 |
return finalise_signature(hash, sig, signer, NULL); |
|---|
| 583 |
} |
|---|
| 584 |
|
|---|
| 585 |
static void start_signature_in_mem(ops_create_signature_t *sig) |
|---|
| 586 |
{ |
|---|
| 587 |
|
|---|
| 588 |
|
|---|
| 589 |
sig->mem=ops_memory_new(); |
|---|
| 590 |
ops_memory_init(sig->mem, 100); |
|---|
| 591 |
ops_writer_set_memory(sig->info, sig->mem); |
|---|
| 592 |
|
|---|
| 593 |
|
|---|
| 594 |
ops_write_scalar(sig->sig.info.version, 1, sig->info); |
|---|
| 595 |
ops_write_scalar(sig->sig.info.type, 1, sig->info); |
|---|
| 596 |
ops_write_scalar(sig->sig.info.key_algorithm, 1, sig->info); |
|---|
| 597 |
ops_write_scalar(sig->sig.info.hash_algorithm, 1, sig->info); |
|---|
| 598 |
|
|---|
| 599 |
|
|---|
| 600 |
sig->hashed_count_offset=ops_memory_get_length(sig->mem); |
|---|
| 601 |
ops_write_scalar(0, 2, sig->info); |
|---|
| 602 |
} |
|---|
| 603 |
|
|---|
| 604 |
|
|---|
| 605 |
|
|---|
| 606 |
|
|---|
| 607 |
|
|---|
| 608 |
|
|---|
| 609 |
|
|---|
| 610 |
|
|---|
| 611 |
|
|---|
| 612 |
|
|---|
| 613 |
|
|---|
| 614 |
void ops_signature_start_key_signature(ops_create_signature_t *sig, |
|---|
| 615 |
const ops_public_key_t *key, |
|---|
| 616 |
const ops_user_id_t *id, |
|---|
| 617 |
ops_sig_type_t type) |
|---|
| 618 |
{ |
|---|
| 619 |
sig->info=ops_create_info_new(); |
|---|
| 620 |
|
|---|
| 621 |
|
|---|
| 622 |
|
|---|
| 623 |
|
|---|
| 624 |
sig->sig.info.version=OPS_V4; |
|---|
| 625 |
sig->sig.info.hash_algorithm=OPS_HASH_SHA1; |
|---|
| 626 |
sig->sig.info.key_algorithm=key->algorithm; |
|---|
| 627 |
sig->sig.info.type=type; |
|---|
| 628 |
|
|---|
| 629 |
sig->hashed_data_length=-1; |
|---|
| 630 |
|
|---|
| 631 |
init_key_signature(&sig->hash, &sig->sig, key); |
|---|
| 632 |
|
|---|
| 633 |
ops_hash_add_int(&sig->hash, 0xb4, 1); |
|---|
| 634 |
ops_hash_add_int(&sig->hash, strlen((char *)id->user_id), 4); |
|---|
| 635 |
sig->hash.add(&sig->hash, id->user_id, strlen((char *)id->user_id)); |
|---|
| 636 |
|
|---|
| 637 |
start_signature_in_mem(sig); |
|---|
| 638 |
} |
|---|
| 639 |
|
|---|
| 640 |
|
|---|
| 641 |
|
|---|
| 642 |
|
|---|
| 643 |
|
|---|
| 644 |
|
|---|
| 645 |
|
|---|
| 646 |
|
|---|
| 647 |
|
|---|
| 648 |
|
|---|
| 649 |
|
|---|
| 650 |
|
|---|
| 651 |
static void ops_signature_start_signature(ops_create_signature_t *sig, |
|---|
| 652 |
const ops_secret_key_t *key, |
|---|
| 653 |
const ops_hash_algorithm_t hash, |
|---|
| 654 |
const ops_sig_type_t type) |
|---|
| 655 |
{ |
|---|
| 656 |
sig->info=ops_create_info_new(); |
|---|
| 657 |
|
|---|
| 658 |
|
|---|
| 659 |
|
|---|
| 660 |
|
|---|
| 661 |
sig->sig.info.version=OPS_V4; |
|---|
| 662 |
sig->sig.info.key_algorithm=key->public_key.algorithm; |
|---|
| 663 |
sig->sig.info.hash_algorithm=hash; |
|---|
| 664 |
sig->sig.info.type=type; |
|---|
| 665 |
|
|---|
| 666 |
sig->hashed_data_length=-1; |
|---|
| 667 |
|
|---|
| 668 |
if (debug) |
|---|
| 669 |
{ fprintf(stderr, "initialising hash for sig in mem\n"); } |
|---|
| 670 |
initialise_hash(&sig->hash, &sig->sig); |
|---|
| 671 |
start_signature_in_mem(sig); |
|---|
| 672 |
} |
|---|
| 673 |
|
|---|
| 674 |
|
|---|
| 675 |
|
|---|
| 676 |
|
|---|
| 677 |
|
|---|
| 678 |
void ops_signature_start_cleartext_signature(ops_create_signature_t *sig, |
|---|
| 679 |
const ops_secret_key_t *key, |
|---|
| 680 |
const ops_hash_algorithm_t hash, |
|---|
| 681 |
const ops_sig_type_t type) |
|---|
| 682 |
{ |
|---|
| 683 |
ops_signature_start_signature(sig, key, hash, type); |
|---|
| 684 |
} |
|---|
| 685 |
|
|---|
| 686 |
|
|---|
| 687 |
|
|---|
| 688 |
|
|---|
| 689 |
|
|---|
| 690 |
void ops_signature_start_message_signature(ops_create_signature_t *sig, |
|---|
| 691 |
const ops_secret_key_t *key, |
|---|
| 692 |
const ops_hash_algorithm_t hash, |
|---|
| 693 |
const ops_sig_type_t type) |
|---|
| 694 |
{ |
|---|
| 695 |
ops_signature_start_signature(sig, key, hash, type); |
|---|
| 696 |
} |
|---|
| 697 |
|
|---|
| 698 |
|
|---|
| 699 |
|
|---|
| 700 |
|
|---|
| 701 |
|
|---|
| 702 |
|
|---|
| 703 |
|
|---|
| 704 |
|
|---|
| 705 |
|
|---|
| 706 |
|
|---|
| 707 |
void ops_signature_add_data(ops_create_signature_t *sig, const void *buf, |
|---|
| 708 |
size_t length) |
|---|
| 709 |
{ |
|---|
| 710 |
if (debug) |
|---|
| 711 |
{ fprintf(stderr, "ops_signature_add_data adds to hash\n"); } |
|---|
| 712 |
sig->hash.add(&sig->hash, buf, length); |
|---|
| 713 |
} |
|---|
| 714 |
|
|---|
| 715 |
|
|---|
| 716 |
|
|---|
| 717 |
|
|---|
| 718 |
|
|---|
| 719 |
|
|---|
| 720 |
|
|---|
| 721 |
|
|---|
| 722 |
|
|---|
| 723 |
ops_boolean_t ops_signature_hashed_subpackets_end(ops_create_signature_t *sig) |
|---|
| 724 |
{ |
|---|
| 725 |
sig->hashed_data_length=ops_memory_get_length(sig->mem) |
|---|
| 726 |
-sig->hashed_count_offset-2; |
|---|
| 727 |
ops_memory_place_int(sig->mem, sig->hashed_count_offset, |
|---|
| 728 |
sig->hashed_data_length, 2); |
|---|
| 729 |
|
|---|
| 730 |
sig->unhashed_count_offset=ops_memory_get_length(sig->mem); |
|---|
| 731 |
return ops_write_scalar(0, 2, sig->info); |
|---|
| 732 |
} |
|---|
| 733 |
|
|---|
| 734 |
|
|---|
| 735 |
|
|---|
| 736 |
|
|---|
| 737 |
|
|---|
| 738 |
|
|---|
| 739 |
|
|---|
| 740 |
|
|---|
| 741 |
|
|---|
| 742 |
|
|---|
| 743 |
|
|---|
| 744 |
|
|---|
| 745 |
|
|---|
| 746 |
ops_boolean_t ops_write_signature(ops_create_signature_t *sig, |
|---|
| 747 |
const ops_public_key_t *key, |
|---|
| 748 |
const ops_secret_key_t *skey, |
|---|
| 749 |
ops_create_info_t *info) |
|---|
| 750 |
{ |
|---|
| 751 |
ops_boolean_t rtn=ops_false; |
|---|
| 752 |
size_t l=ops_memory_get_length(sig->mem); |
|---|
| 753 |
|
|---|
| 754 |
|
|---|
| 755 |
switch (skey->public_key.algorithm) |
|---|
| 756 |
{ |
|---|
| 757 |
case OPS_PKA_RSA: |
|---|
| 758 |
case OPS_PKA_RSA_ENCRYPT_ONLY: |
|---|
| 759 |
case OPS_PKA_RSA_SIGN_ONLY: |
|---|
| 760 |
assert(skey->key.rsa.d); |
|---|
| 761 |
break; |
|---|
| 762 |
|
|---|
| 763 |
case OPS_PKA_DSA: |
|---|
| 764 |
assert(skey->key.dsa.x); |
|---|
| 765 |
break; |
|---|
| 766 |
|
|---|
| 767 |
default: |
|---|
| 768 |
fprintf(stderr, "Unsupported algorithm %d\n", |
|---|
| 769 |
skey->public_key.algorithm); |
|---|
| 770 |
assert(0); |
|---|
| 771 |
} |
|---|
| 772 |
|
|---|
| 773 |
assert(sig->hashed_data_length != (unsigned)-1); |
|---|
| 774 |
|
|---|
| 775 |
ops_memory_place_int(sig->mem, sig->unhashed_count_offset, |
|---|
| 776 |
l-sig->unhashed_count_offset-2, 2); |
|---|
| 777 |
|
|---|
| 778 |
|
|---|
| 779 |
|
|---|
| 780 |
if (debug) |
|---|
| 781 |
{ fprintf(stderr, "--- Adding packet to hash from version number to" |
|---|
| 782 |
" hashed subpkts\n"); } |
|---|
| 783 |
|
|---|
| 784 |
sig->hash.add(&sig->hash, ops_memory_get_data(sig->mem), |
|---|
| 785 |
sig->unhashed_count_offset); |
|---|
| 786 |
|
|---|
| 787 |
|
|---|
| 788 |
ops_hash_add_int(&sig->hash, sig->sig.info.version, 1); |
|---|
| 789 |
ops_hash_add_int(&sig->hash, 0xff, 1); |
|---|
| 790 |
|
|---|
| 791 |
ops_hash_add_int(&sig->hash, sig->hashed_data_length+6, 4); |
|---|
| 792 |
|
|---|
| 793 |
if (debug) |
|---|
| 794 |
{ fprintf(stderr, "--- Finished adding packet to hash from version" |
|---|
| 795 |
" number to hashed subpkts\n"); } |
|---|
| 796 |
|
|---|
| 797 |
|
|---|
| 798 |
|
|---|
| 799 |
switch(skey->public_key.algorithm) |
|---|
| 800 |
{ |
|---|
| 801 |
case OPS_PKA_RSA: |
|---|
| 802 |
case OPS_PKA_RSA_ENCRYPT_ONLY: |
|---|
| 803 |
case OPS_PKA_RSA_SIGN_ONLY: |
|---|
| 804 |
rsa_sign(&sig->hash, &key->key.rsa, &skey->key.rsa, sig->info); |
|---|
| 805 |
break; |
|---|
| 806 |
|
|---|
| 807 |
case OPS_PKA_DSA: |
|---|
| 808 |
dsa_sign(&sig->hash, &key->key.dsa, &skey->key.dsa, sig->info); |
|---|
| 809 |
break; |
|---|
| 810 |
|
|---|
| 811 |
default: |
|---|
| 812 |
fprintf(stderr, "Unsupported algorithm %d\n", |
|---|
| 813 |
skey->public_key.algorithm); |
|---|
| 814 |
assert(0); |
|---|
| 815 |
} |
|---|
| 816 |
|
|---|
| 817 |
rtn=ops_write_ptag(OPS_PTAG_CT_SIGNATURE, info); |
|---|
| 818 |
if (rtn) |
|---|
| 819 |
{ |
|---|
| 820 |
l=ops_memory_get_length(sig->mem); |
|---|
| 821 |
rtn = ops_write_length(l, info) |
|---|
| 822 |
&& ops_write(ops_memory_get_data(sig->mem), l, info); |
|---|
| 823 |
} |
|---|
| 824 |
|
|---|
| 825 |
ops_memory_free(sig->mem); |
|---|
| 826 |
|
|---|
| 827 |
if (!rtn) |
|---|
| 828 |
OPS_ERROR(&info->errors, OPS_E_W, "Cannot write signature"); |
|---|
| 829 |
return rtn; |
|---|
| 830 |
} |
|---|
| 831 |
|
|---|
| 832 |
|
|---|
| 833 |
|
|---|
| 834 |
|
|---|
| 835 |
|
|---|
| 836 |
|
|---|
| 837 |
|
|---|
| 838 |
|
|---|
| 839 |
|
|---|
| 840 |
ops_boolean_t ops_signature_add_creation_time(ops_create_signature_t *sig, |
|---|
| 841 |
time_t when) |
|---|
| 842 |
{ |
|---|
| 843 |
return ops_write_ss_header(5, OPS_PTAG_SS_CREATION_TIME, sig->info) |
|---|
| 844 |
&& ops_write_scalar(when, 4, sig->info); |
|---|
| 845 |
} |
|---|
| 846 |
|
|---|
| 847 |
|
|---|
| 848 |
|
|---|
| 849 |
|
|---|
| 850 |
|
|---|
| 851 |
|
|---|
| 852 |
|
|---|
| 853 |
|
|---|
| 854 |
|
|---|
| 855 |
|
|---|
| 856 |
ops_boolean_t |
|---|
| 857 |
ops_signature_add_issuer_key_id(ops_create_signature_t *sig, |
|---|
| 858 |
const unsigned char keyid[OPS_KEY_ID_SIZE]) |
|---|
| 859 |
{ |
|---|
| 860 |
return ops_write_ss_header(OPS_KEY_ID_SIZE+1, OPS_PTAG_SS_ISSUER_KEY_ID, |
|---|
| 861 |
sig->info) |
|---|
| 862 |
&& ops_write(keyid, OPS_KEY_ID_SIZE, sig->info); |
|---|
| 863 |
} |
|---|
| 864 |
|
|---|
| 865 |
|
|---|
| 866 |
|
|---|
| 867 |
|
|---|
| 868 |
|
|---|
| 869 |
|
|---|
| 870 |
|
|---|
| 871 |
|
|---|
| 872 |
|
|---|
| 873 |
void ops_signature_add_primary_user_id(ops_create_signature_t *sig, |
|---|
| 874 |
ops_boolean_t primary) |
|---|
| 875 |
{ |
|---|
| 876 |
ops_write_ss_header(2, OPS_PTAG_SS_PRIMARY_USER_ID, sig->info); |
|---|
| 877 |
ops_write_scalar(primary, 1, sig->info); |
|---|
| 878 |
} |
|---|
| 879 |
|
|---|
| 880 |
|
|---|
| 881 |
|
|---|
| 882 |
|
|---|
| 883 |
|
|---|
| 884 |
|
|---|
| 885 |
|
|---|
| 886 |
|
|---|
| 887 |
|
|---|
| 888 |
ops_hash_t *ops_signature_get_hash(ops_create_signature_t *sig) |
|---|
| 889 |
{ return &sig->hash; } |
|---|
| 890 |
|
|---|
| 891 |
static int open_output_file(ops_create_info_t **cinfo, |
|---|
| 892 |
const char* input_filename, |
|---|
| 893 |
const char* output_filename, |
|---|
| 894 |
const ops_boolean_t use_armour, |
|---|
| 895 |
const ops_boolean_t overwrite) |
|---|
| 896 |
{ |
|---|
| 897 |
int fd_out; |
|---|
| 898 |
|
|---|
| 899 |
|
|---|
| 900 |
|
|---|
| 901 |
if (output_filename) |
|---|
| 902 |
fd_out=ops_setup_file_write(cinfo, output_filename, overwrite); |
|---|
| 903 |
else |
|---|
| 904 |
{ |
|---|
| 905 |
char *myfilename=NULL; |
|---|
| 906 |
unsigned filenamelen=strlen(input_filename)+4+1; |
|---|
| 907 |
myfilename=ops_mallocz(filenamelen); |
|---|
| 908 |
if (use_armour) |
|---|
| 909 |
snprintf(myfilename, filenamelen, "%s.asc", input_filename); |
|---|
| 910 |
else |
|---|
| 911 |
snprintf(myfilename, filenamelen, "%s.gpg", input_filename); |
|---|
| 912 |
fd_out=ops_setup_file_write(cinfo, myfilename, overwrite); |
|---|
| 913 |
free(myfilename); |
|---|
| 914 |
} |
|---|
| 915 |
|
|---|
| 916 |
return fd_out; |
|---|
| 917 |
} |
|---|
| 918 |
|
|---|
| 919 |
|
|---|
| 920 |
|
|---|
| 921 |
|
|---|
| 922 |
|
|---|
| 923 |
|
|---|
| 924 |
|
|---|
| 925 |
|
|---|
| 926 |
|
|---|
| 927 |
|
|---|
| 928 |
|
|---|
| 929 |
|
|---|
| 930 |
|
|---|
| 931 |
|
|---|
| 932 |
|
|---|
| 933 |
|
|---|
| 934 |
|
|---|
| 935 |
|
|---|
| 936 |
|
|---|
| 937 |
|
|---|
| 938 |
|
|---|
| 939 |
ops_boolean_t ops_sign_file_as_cleartext(const char* input_filename, |
|---|
| 940 |
const char* output_filename, |
|---|
| 941 |
const ops_secret_key_t *skey, |
|---|
| 942 |
const ops_boolean_t overwrite) |
|---|
| 943 |
{ |
|---|
| 944 |
|
|---|
| 945 |
|
|---|
| 946 |
|
|---|
| 947 |
unsigned char keyid[OPS_KEY_ID_SIZE]; |
|---|
| 948 |
ops_create_signature_t *sig=NULL; |
|---|
| 949 |
|
|---|
| 950 |
int fd_in=0; |
|---|
| 951 |
int fd_out=0; |
|---|
| 952 |
ops_create_info_t *cinfo=NULL; |
|---|
| 953 |
unsigned char buf[MAXBUF]; |
|---|
| 954 |
|
|---|
| 955 |
ops_boolean_t rtn=ops_false; |
|---|
| 956 |
ops_boolean_t use_armour=ops_true; |
|---|
| 957 |
|
|---|
| 958 |
|
|---|
| 959 |
fd_in=open(input_filename, O_RDONLY | O_BINARY); |
|---|
| 960 |
if(fd_in < 0) |
|---|
| 961 |
{ |
|---|
| 962 |
return ops_false; |
|---|
| 963 |
} |
|---|
| 964 |
|
|---|
| 965 |
|
|---|
| 966 |
|
|---|
| 967 |
fd_out=open_output_file(&cinfo, input_filename, output_filename, use_armour, |
|---|
| 968 |
overwrite); |
|---|
| 969 |
|
|---|
| 970 |
if (fd_out < 0) |
|---|
| 971 |
{ |
|---|
| 972 |
close(fd_in); |
|---|
| 973 |
return ops_false; |
|---|
| 974 |
} |
|---|
| 975 |
|
|---|
| 976 |
|
|---|
| 977 |
sig=ops_create_signature_new(); |
|---|
| 978 |
if (!sig) |
|---|
| 979 |
{ |
|---|
| 980 |
close (fd_in); |
|---|
| 981 |
ops_teardown_file_write(cinfo, fd_out); |
|---|
| 982 |
return ops_false; |
|---|
| 983 |
} |
|---|
| 984 |
|
|---|
| 985 |
|
|---|
| 986 |
ops_signature_start_cleartext_signature(sig, skey, |
|---|
| 987 |
OPS_HASH_SHA1, OPS_SIG_BINARY); |
|---|
| 988 |
if (!ops_writer_push_clearsigned(cinfo, sig)) |
|---|
| 989 |
return ops_false; |
|---|
| 990 |
|
|---|
| 991 |
|
|---|
| 992 |
|
|---|
| 993 |
for (;;) |
|---|
| 994 |
{ |
|---|
| 995 |
int n=0; |
|---|
| 996 |
|
|---|
| 997 |
n=read(fd_in, buf, sizeof(buf)); |
|---|
| 998 |
if (!n) |
|---|
| 999 |
break; |
|---|
| 1000 |
assert(n>=0); |
|---|
| 1001 |
ops_write(buf, n, cinfo); |
|---|
| 1002 |
} |
|---|
| 1003 |
close(fd_in); |
|---|
| 1004 |
|
|---|
| 1005 |
|
|---|
| 1006 |
|
|---|
| 1007 |
|
|---|
| 1008 |
rtn = ops_writer_switch_to_armoured_signature(cinfo) |
|---|
| 1009 |
&& ops_signature_add_creation_time(sig, time(NULL)); |
|---|
| 1010 |
if (!rtn) |
|---|
| 1011 |
{ |
|---|
| 1012 |
ops_teardown_file_write(cinfo, fd_out); |
|---|
| 1013 |
return ops_false; |
|---|
| 1014 |
} |
|---|
| 1015 |
|
|---|
| 1016 |
ops_keyid(keyid, &skey->public_key); |
|---|
| 1017 |
|
|---|
| 1018 |
rtn = ops_signature_add_issuer_key_id(sig, keyid) |
|---|
| 1019 |
&& ops_signature_hashed_subpackets_end(sig) |
|---|
| 1020 |
&& ops_write_signature(sig, &skey->public_key, skey, cinfo); |
|---|
| 1021 |
|
|---|
| 1022 |
ops_teardown_file_write(cinfo, fd_out); |
|---|
| 1023 |
|
|---|
| 1024 |
if (!rtn) |
|---|
| 1025 |
OPS_ERROR(&cinfo->errors, OPS_E_W, "Cannot sign file as cleartext"); |
|---|
| 1026 |
return rtn; |
|---|
| 1027 |
} |
|---|
| 1028 |
|
|---|
| 1029 |
|
|---|
| 1030 |
|
|---|
| 1031 |
|
|---|
| 1032 |
|
|---|
| 1033 |
|
|---|
| 1034 |
|
|---|
| 1035 |
|
|---|
| 1036 |
|
|---|
| 1037 |
|
|---|
| 1038 |
|
|---|
| 1039 |
|
|---|
| 1040 |
|
|---|
| 1041 |
|
|---|
| 1042 |
|
|---|
| 1043 |
|
|---|
| 1044 |
|
|---|
| 1045 |
|
|---|
| 1046 |
|
|---|
| 1047 |
|
|---|
| 1048 |
|
|---|
| 1049 |
|
|---|
| 1050 |
|
|---|
| 1051 |
|
|---|
| 1052 |
|
|---|
| 1053 |
|
|---|
| 1054 |
|
|---|
| 1055 |
|
|---|
| 1056 |
|
|---|
| 1057 |
|
|---|
| 1058 |
ops_boolean_t ops_sign_buf_as_cleartext(const char* cleartext, const size_t len, |
|---|
| 1059 |
ops_memory_t** signed_cleartext, |
|---|
| 1060 |
const ops_secret_key_t *skey) |
|---|
| 1061 |
{ |
|---|
| 1062 |
ops_boolean_t rtn=ops_false; |
|---|
| 1063 |
|
|---|
| 1064 |
|
|---|
| 1065 |
|
|---|
| 1066 |
|
|---|
| 1067 |
unsigned char keyid[OPS_KEY_ID_SIZE]; |
|---|
| 1068 |
ops_create_signature_t *sig=NULL; |
|---|
| 1069 |
|
|---|
| 1070 |
ops_create_info_t *cinfo=NULL; |
|---|
| 1071 |
|
|---|
| 1072 |
assert(*signed_cleartext == NULL); |
|---|
| 1073 |
|
|---|
| 1074 |
|
|---|
| 1075 |
sig=ops_create_signature_new(); |
|---|
| 1076 |
if (!sig) |
|---|
| 1077 |
return ops_false; |
|---|
| 1078 |
|
|---|
| 1079 |
|
|---|
| 1080 |
ops_signature_start_cleartext_signature(sig, skey, OPS_HASH_SHA1, |
|---|
| 1081 |
OPS_SIG_BINARY); |
|---|
| 1082 |
|
|---|
| 1083 |
|
|---|
| 1084 |
ops_setup_memory_write(&cinfo, signed_cleartext, len); |
|---|
| 1085 |
|
|---|
| 1086 |
|
|---|
| 1087 |
|
|---|
| 1088 |
|
|---|
| 1089 |
|
|---|
| 1090 |
rtn = ops_writer_push_clearsigned(cinfo, sig) |
|---|
| 1091 |
&& ops_write(cleartext, len, cinfo) |
|---|
| 1092 |
&& ops_writer_switch_to_armoured_signature(cinfo) |
|---|
| 1093 |
&& ops_signature_add_creation_time(sig, time(NULL)); |
|---|
| 1094 |
|
|---|
| 1095 |
if (!rtn) |
|---|
| 1096 |
return ops_false; |
|---|
| 1097 |
|
|---|
| 1098 |
ops_keyid(keyid, &skey->public_key); |
|---|
| 1099 |
|
|---|
| 1100 |
rtn = ops_signature_add_issuer_key_id(sig, keyid) |
|---|
| 1101 |
&& ops_signature_hashed_subpackets_end(sig) |
|---|
| 1102 |
&& ops_write_signature(sig, &skey->public_key, skey, cinfo) |
|---|
| 1103 |
&& ops_writer_close(cinfo); |
|---|
| 1104 |
|
|---|
| 1105 |
|
|---|
| 1106 |
ops_create_info_delete(cinfo); |
|---|
| 1107 |
|
|---|
| 1108 |
return rtn; |
|---|
| 1109 |
} |
|---|
| 1110 |
|
|---|
| 1111 |
|
|---|
| 1112 |
|
|---|
| 1113 |
|
|---|
| 1114 |
|
|---|
| 1115 |
|
|---|
| 1116 |
|
|---|
| 1117 |
|
|---|
| 1118 |
|
|---|
| 1119 |
|
|---|
| 1120 |
|
|---|
| 1121 |
|
|---|
| 1122 |
|
|---|
| 1123 |
|
|---|
| 1124 |
|
|---|
| 1125 |
|
|---|
| 1126 |
|
|---|
| 1127 |
|
|---|
| 1128 |
|
|---|
| 1129 |
|
|---|
| 1130 |
|
|---|
| 1131 |
|
|---|
| 1132 |
|
|---|
| 1133 |
|
|---|
| 1134 |
|
|---|
| 1135 |
ops_boolean_t ops_sign_file(const char* input_filename, |
|---|
| 1136 |
const char* output_filename, |
|---|
| 1137 |
const ops_secret_key_t *skey, |
|---|
| 1138 |
const ops_boolean_t use_armour, |
|---|
| 1139 |
const ops_boolean_t overwrite) |
|---|
| 1140 |
{ |
|---|
| 1141 |
|
|---|
| 1142 |
|
|---|
| 1143 |
|
|---|
| 1144 |
unsigned char keyid[OPS_KEY_ID_SIZE]; |
|---|
| 1145 |
ops_create_signature_t *sig=NULL; |
|---|
| 1146 |
|
|---|
| 1147 |
int fd_out=0; |
|---|
| 1148 |
ops_create_info_t *cinfo=NULL; |
|---|
| 1149 |
|
|---|
| 1150 |
ops_hash_algorithm_t hash_alg=OPS_HASH_SHA1; |
|---|
| 1151 |
ops_sig_type_t sig_type=OPS_SIG_BINARY; |
|---|
| 1152 |
|
|---|
| 1153 |
ops_memory_t* mem_buf=NULL; |
|---|
| 1154 |
ops_hash_t* hash=NULL; |
|---|
| 1155 |
|
|---|
| 1156 |
|
|---|
| 1157 |
|
|---|
| 1158 |
int errnum; |
|---|
| 1159 |
mem_buf=ops_write_mem_from_file(input_filename, &errnum); |
|---|
| 1160 |
if (errnum) |
|---|
| 1161 |
return ops_false; |
|---|
| 1162 |
|
|---|
| 1163 |
|
|---|
| 1164 |
|
|---|
| 1165 |
fd_out=open_output_file(&cinfo, input_filename, output_filename, use_armour, |
|---|
| 1166 |
overwrite); |
|---|
| 1167 |
|
|---|
| 1168 |
if (fd_out < 0) |
|---|
| 1169 |
{ |
|---|
| 1170 |
ops_memory_free(mem_buf); |
|---|
| 1171 |
return ops_false; |
|---|
| 1172 |
} |
|---|
| 1173 |
|
|---|
| 1174 |
|
|---|
| 1175 |
sig=ops_create_signature_new(); |
|---|
| 1176 |
ops_signature_start_message_signature(sig, skey, hash_alg, sig_type); |
|---|
| 1177 |
|
|---|
| 1178 |
|
|---|
| 1179 |
if (use_armour) |
|---|
| 1180 |
ops_writer_push_armoured_message(cinfo); |
|---|
| 1181 |
|
|---|
| 1182 |
if (debug) |
|---|
| 1183 |
{ fprintf(stderr, "** Writing out one pass sig\n"); } |
|---|
| 1184 |
|
|---|
| 1185 |
|
|---|
| 1186 |
ops_write_one_pass_sig(skey, hash_alg, sig_type, cinfo); |
|---|
| 1187 |
|
|---|
| 1188 |
|
|---|
| 1189 |
hash=ops_signature_get_hash(sig); |
|---|
| 1190 |
hash->add(hash, ops_memory_get_data(mem_buf), |
|---|
| 1191 |
ops_memory_get_length(mem_buf)); |
|---|
| 1192 |
|
|---|
| 1193 |
|
|---|
| 1194 |
|
|---|
| 1195 |
if (debug) |
|---|
| 1196 |
fprintf(stderr,"** Writing out data now\n"); |
|---|
| 1197 |
|
|---|
| 1198 |
ops_write_literal_data_from_buf(ops_memory_get_data(mem_buf), |
|---|
| 1199 |
ops_memory_get_length(mem_buf), |
|---|
| 1200 |
OPS_LDT_BINARY, cinfo); |
|---|
| 1201 |
|
|---|
| 1202 |
if (debug) |
|---|
| 1203 |
fprintf(stderr, "** After Writing out data now\n"); |
|---|
| 1204 |
|
|---|
| 1205 |
|
|---|
| 1206 |
|
|---|
| 1207 |
|
|---|
| 1208 |
|
|---|
| 1209 |
ops_signature_add_creation_time(sig, time(NULL)); |
|---|
| 1210 |
|
|---|
| 1211 |
ops_keyid(keyid, &skey->public_key); |
|---|
| 1212 |
ops_signature_add_issuer_key_id(sig, keyid); |
|---|
| 1213 |
|
|---|
| 1214 |
ops_signature_hashed_subpackets_end(sig); |
|---|
| 1215 |
|
|---|
| 1216 |
|
|---|
| 1217 |
ops_write_signature(sig, &skey->public_key, skey, cinfo); |
|---|
| 1218 |
|
|---|
| 1219 |
ops_teardown_file_write(cinfo, fd_out); |
|---|
| 1220 |
|
|---|
| 1221 |
|
|---|
| 1222 |
ops_create_signature_delete(sig); |
|---|
| 1223 |
ops_memory_free(mem_buf); |
|---|
| 1224 |
|
|---|
| 1225 |
return ops_true; |
|---|
| 1226 |
} |
|---|
| 1227 |
|
|---|
| 1228 |
|
|---|
| 1229 |
|
|---|
| 1230 |
|
|---|
| 1231 |
|
|---|
| 1232 |
|
|---|
| 1233 |
|
|---|
| 1234 |
|
|---|
| 1235 |
|
|---|
| 1236 |
|
|---|
| 1237 |
|
|---|
| 1238 |
|
|---|
| 1239 |
|
|---|
| 1240 |
|
|---|
| 1241 |
|
|---|
| 1242 |
|
|---|
| 1243 |
|
|---|
| 1244 |
|
|---|
| 1245 |
|
|---|
| 1246 |
|
|---|
| 1247 |
|
|---|
| 1248 |
|
|---|
| 1249 |
|
|---|
| 1250 |
|
|---|
| 1251 |
|
|---|
| 1252 |
|
|---|
| 1253 |
|
|---|
| 1254 |
|
|---|
| 1255 |
|
|---|
| 1256 |
|
|---|
| 1257 |
|
|---|
| 1258 |
|
|---|
| 1259 |
|
|---|
| 1260 |
|
|---|
| 1261 |
|
|---|
| 1262 |
|
|---|
| 1263 |
ops_memory_t* ops_sign_buf(const void* input, const size_t input_len, |
|---|
| 1264 |
const ops_sig_type_t sig_type, |
|---|
| 1265 |
const ops_secret_key_t *skey, |
|---|
| 1266 |
const ops_boolean_t use_armour, |
|---|
| 1267 |
ops_boolean_t include_data) |
|---|
| 1268 |
{ |
|---|
| 1269 |
|
|---|
| 1270 |
|
|---|
| 1271 |
|
|---|
| 1272 |
unsigned char keyid[OPS_KEY_ID_SIZE]; |
|---|
| 1273 |
ops_create_signature_t *sig=NULL; |
|---|
| 1274 |
|
|---|
| 1275 |
ops_create_info_t *cinfo=NULL; |
|---|
| 1276 |
ops_memory_t *mem=ops_memory_new(); |
|---|
| 1277 |
|
|---|
| 1278 |
ops_hash_algorithm_t hash_alg=OPS_HASH_SHA1; |
|---|
| 1279 |
ops_literal_data_type_t ld_type; |
|---|
| 1280 |
ops_hash_t* hash=NULL; |
|---|
| 1281 |
|
|---|
| 1282 |
|
|---|
| 1283 |
if (sig_type == OPS_SIG_BINARY) |
|---|
| 1284 |
ld_type=OPS_LDT_BINARY; |
|---|
| 1285 |
else |
|---|
| 1286 |
ld_type=OPS_LDT_TEXT; |
|---|
| 1287 |
|
|---|
| 1288 |
|
|---|
| 1289 |
sig=ops_create_signature_new(); |
|---|
| 1290 |
ops_signature_start_message_signature(sig, skey, hash_alg, sig_type); |
|---|
| 1291 |
|
|---|
| 1292 |
|
|---|
| 1293 |
ops_setup_memory_write(&cinfo, &mem, input_len); |
|---|
| 1294 |
|
|---|
| 1295 |
|
|---|
| 1296 |
if (use_armour) |
|---|
| 1297 |
ops_writer_push_armoured_message(cinfo); |
|---|
| 1298 |
|
|---|
| 1299 |
if (debug) |
|---|
| 1300 |
fprintf(stderr, "** Writing out one pass sig\n"); |
|---|
| 1301 |
|
|---|
| 1302 |
|
|---|
| 1303 |
ops_write_one_pass_sig(skey, hash_alg, sig_type, cinfo); |
|---|
| 1304 |
|
|---|
| 1305 |
|
|---|
| 1306 |
hash=ops_signature_get_hash(sig); |
|---|
| 1307 |
hash->add(hash, input, input_len); |
|---|
| 1308 |
|
|---|
| 1309 |
|
|---|
| 1310 |
|
|---|
| 1311 |
if (debug) |
|---|
| 1312 |
fprintf(stderr,"** Writing out data now\n"); |
|---|
| 1313 |
|
|---|
| 1314 |
if(include_data) |
|---|
| 1315 |
ops_write_literal_data_from_buf(input, input_len, ld_type, cinfo); |
|---|
| 1316 |
|
|---|
| 1317 |
if (debug) |
|---|
| 1318 |
fprintf(stderr,"** After Writing out data now\n"); |
|---|
| 1319 |
|
|---|
| 1320 |
|
|---|
| 1321 |
|
|---|
| 1322 |
|
|---|
| 1323 |
|
|---|
| 1324 |
ops_signature_add_creation_time(sig, time(NULL)); |
|---|
| 1325 |
|
|---|
| 1326 |
ops_keyid(keyid, &skey->public_key); |
|---|
| 1327 |
ops_signature_add_issuer_key_id(sig, keyid); |
|---|
| 1328 |
|
|---|
| 1329 |
ops_signature_hashed_subpackets_end(sig); |
|---|
| 1330 |
|
|---|
| 1331 |
|
|---|
| 1332 |
ops_write_signature(sig, &skey->public_key, skey, cinfo); |
|---|
| 1333 |
|
|---|
| 1334 |
|
|---|
| 1335 |
ops_writer_close(cinfo); |
|---|
| 1336 |
free(cinfo) ; |
|---|
| 1337 |
ops_create_signature_delete(sig); |
|---|
| 1338 |
|
|---|
| 1339 |
return mem; |
|---|
| 1340 |
} |
|---|
| 1341 |
|
|---|
| 1342 |
typedef struct { |
|---|
| 1343 |
ops_create_signature_t *signature; |
|---|
| 1344 |
const ops_secret_key_t *skey; |
|---|
| 1345 |
ops_hash_algorithm_t hash_alg; |
|---|
| 1346 |
ops_sig_type_t sig_type; |
|---|
| 1347 |
} signature_arg_t; |
|---|
| 1348 |
|
|---|
| 1349 |
static ops_boolean_t stream_signature_writer(const unsigned char *src, |
|---|
| 1350 |
unsigned length, |
|---|
| 1351 |
ops_error_t **errors, |
|---|
| 1352 |
ops_writer_info_t *winfo) |
|---|
| 1353 |
{ |
|---|
| 1354 |
signature_arg_t* arg = ops_writer_get_arg(winfo); |
|---|
| 1355 |
|
|---|
| 1356 |
|
|---|
| 1357 |
ops_hash_t* hash = ops_signature_get_hash(arg->signature); |
|---|
| 1358 |
hash->add(hash, src, length); |
|---|
| 1359 |
|
|---|
| 1360 |
return ops_stacked_write(src, length, errors, winfo); |
|---|
| 1361 |
} |
|---|
| 1362 |
|
|---|
| 1363 |
static ops_boolean_t stream_signature_write_trailer(ops_create_info_t *cinfo, |
|---|
| 1364 |
void *data) |
|---|
| 1365 |
{ |
|---|
| 1366 |
signature_arg_t* arg = data; |
|---|
| 1367 |
unsigned char keyid[OPS_KEY_ID_SIZE]; |
|---|
| 1368 |
|
|---|
| 1369 |
|
|---|
| 1370 |
|
|---|
| 1371 |
|
|---|
| 1372 |
ops_signature_add_creation_time(arg->signature,time(NULL)); |
|---|
| 1373 |
ops_keyid(keyid, &arg->skey->public_key); |
|---|
| 1374 |
ops_signature_add_issuer_key_id(arg->signature, keyid); |
|---|
| 1375 |
ops_signature_hashed_subpackets_end(arg->signature); |
|---|
| 1376 |
|
|---|
| 1377 |
|
|---|
| 1378 |
return ops_write_signature(arg->signature, &arg->skey->public_key, |
|---|
| 1379 |
arg->skey, cinfo); |
|---|
| 1380 |
} |
|---|
| 1381 |
|
|---|
| 1382 |
static void stream_signature_destroyer(ops_writer_info_t *winfo) |
|---|
| 1383 |
{ |
|---|
| 1384 |
signature_arg_t* arg = ops_writer_get_arg(winfo); |
|---|
| 1385 |
ops_create_signature_delete(arg->signature); |
|---|
| 1386 |
free(arg); |
|---|
| 1387 |
} |
|---|
| 1388 |
|
|---|
| 1389 |
|
|---|
| 1390 |
|
|---|
| 1391 |
|
|---|
| 1392 |
|
|---|
| 1393 |
|
|---|
| 1394 |
|
|---|
| 1395 |
|
|---|
| 1396 |
|
|---|
| 1397 |
|
|---|
| 1398 |
|
|---|
| 1399 |
|
|---|
| 1400 |
|
|---|
| 1401 |
|
|---|
| 1402 |
|
|---|
| 1403 |
ops_boolean_t ops_writer_push_signed(ops_create_info_t *cinfo, |
|---|
| 1404 |
const ops_sig_type_t sig_type, |
|---|
| 1405 |
const ops_secret_key_t *skey) |
|---|
| 1406 |
{ |
|---|
| 1407 |
|
|---|
| 1408 |
|
|---|
| 1409 |
|
|---|
| 1410 |
|
|---|
| 1411 |
|
|---|
| 1412 |
signature_arg_t *signature_arg = ops_mallocz(sizeof *signature_arg); |
|---|
| 1413 |
signature_arg->signature = ops_create_signature_new(); |
|---|
| 1414 |
signature_arg->hash_alg = OPS_HASH_SHA1; |
|---|
| 1415 |
signature_arg->skey = skey; |
|---|
| 1416 |
signature_arg->sig_type = sig_type; |
|---|
| 1417 |
ops_signature_start_message_signature(signature_arg->signature, |
|---|
| 1418 |
signature_arg->skey, |
|---|
| 1419 |
signature_arg->hash_alg, |
|---|
| 1420 |
signature_arg->sig_type); |
|---|
| 1421 |
|
|---|
| 1422 |
if (!ops_write_one_pass_sig(signature_arg->skey, |
|---|
| 1423 |
signature_arg->hash_alg, |
|---|
| 1424 |
signature_arg->sig_type, |
|---|
| 1425 |
cinfo)) |
|---|
| 1426 |
return ops_false; |
|---|
| 1427 |
|
|---|
| 1428 |
ops_writer_push_partial_with_trailer(0, cinfo, OPS_PTAG_CT_LITERAL_DATA, |
|---|
| 1429 |
write_literal_header, NULL, |
|---|
| 1430 |
stream_signature_write_trailer, |
|---|
| 1431 |
signature_arg); |
|---|
| 1432 |
|
|---|
| 1433 |
ops_writer_push(cinfo, stream_signature_writer, NULL, |
|---|
| 1434 |
stream_signature_destroyer,signature_arg); |
|---|
| 1435 |
return ops_true; |
|---|
| 1436 |
} |
|---|
| 1437 |
|
|---|
| 1438 |
|
|---|
| 1439 |
|
|---|