static int do_plaintext( IOBUF out, int ctb, PKT_plaintext *pt ) { int i, rc = 0; u32 n; byte buf[1000]; /* this buffer has the plaintext! */ int nbytes; write_header(out, ctb, calc_plaintext( pt ) ); iobuf_put(out, pt->mode ); iobuf_put(out, pt->namelen ); for(i=0; i < pt->namelen; i++ ) iobuf_put(out, pt->name[i] ); rc = write_32(out, pt->timestamp ); if (rc) return rc; n = 0; while( (nbytes=iobuf_read(pt->buf, buf, 1000)) != -1 ) { rc = iobuf_write (out, buf, nbytes); if (rc) break; n += nbytes; } wipememory(buf,1000); /* burn the buffer */ if( (ctb&0x40) && !pt->len ) iobuf_set_partial_block_mode(out, 0 ); /* turn off partial */ if( pt->len && n != pt->len ) log_error("do_plaintext(): wrote %lu bytes but expected %lu bytes\n", (ulong)n, (ulong)pt->len ); return rc; }
static int do_symkey_enc( IOBUF out, int ctb, PKT_symkey_enc *enc ) { int rc = 0; IOBUF a = iobuf_temp(); assert( enc->version == 4 ); switch( enc->s2k.mode ) { case 0: case 1: case 3: break; default: log_bug("do_symkey_enc: s2k=%d\n", enc->s2k.mode ); } iobuf_put( a, enc->version ); iobuf_put( a, enc->cipher_algo ); iobuf_put( a, enc->s2k.mode ); iobuf_put( a, enc->s2k.hash_algo ); if( enc->s2k.mode == 1 || enc->s2k.mode == 3 ) { iobuf_write(a, enc->s2k.salt, 8 ); if( enc->s2k.mode == 3 ) iobuf_put(a, enc->s2k.count); } if( enc->seskeylen ) iobuf_write(a, enc->seskey, enc->seskeylen ); write_header(out, ctb, iobuf_get_temp_length(a) ); rc = iobuf_write_temp( out, a ); iobuf_close(a); return rc; }
static int do_public_key( IOBUF out, int ctb, PKT_public_key *pk ) { int rc = 0; int n, i; IOBUF a = iobuf_temp(); if( !pk->version ) iobuf_put( a, 3 ); else iobuf_put( a, pk->version ); write_32(a, pk->timestamp ); if( pk->version < 4 ) { u16 ndays; if( pk->expiredate ) ndays = (u16)((pk->expiredate - pk->timestamp) / 86400L); else ndays = 0; write_16(a, ndays ); } iobuf_put(a, pk->pubkey_algo ); n = pubkey_get_npkey( pk->pubkey_algo ); if( !n ) write_fake_data( a, pk->pkey[0] ); for(i=0; i < n; i++ ) mpi_write(a, pk->pkey[i] ); write_header2(out, ctb, iobuf_get_temp_length(a), pk->hdrbytes, 1 ); if( iobuf_write_temp( out, a ) ) rc = G10ERR_WRITE_FILE; iobuf_close(a); return rc; }
static int do_public_key( IOBUF out, int ctb, PKT_public_key *pk ) { int rc = 0; int n, i; IOBUF a = iobuf_temp(); if ( !pk->version ) iobuf_put( a, 3 ); else iobuf_put( a, pk->version ); write_32(a, pk->timestamp ); if ( pk->version < 4 ) { u16 ndays; if ( pk->expiredate ) ndays = (u16)((pk->expiredate - pk->timestamp) / 86400L); else ndays = 0; write_16(a, ndays ); } iobuf_put (a, pk->pubkey_algo ); if ( pk->pubkey_algo == PUBKEY_ALGO_NTRU){ rc = sexp_write(a, pk->ntru_pkey); } else { n = pubkey_get_npkey ( pk->pubkey_algo ); if ( !n ) write_fake_data( a, pk->pkey[0] ); } if (!rc) { write_header2 (out, ctb, iobuf_get_temp_length(a), pk->hdrbytes); printf("write output\n"); rc = iobuf_write_temp ( out, a ); } printf("finished writing\n"); iobuf_close(a); return rc; }
static int do_pubkey_enc( IOBUF out, int ctb, PKT_pubkey_enc *enc ) { int rc = 0; int n, i; IOBUF a = iobuf_temp(); write_version( a, ctb ); if ( enc->throw_keyid ) { write_32(a, 0 ); /* Don't tell Eve who can decrypt the message. */ write_32(a, 0 ); } else { write_32(a, enc->keyid[0] ); write_32(a, enc->keyid[1] ); } iobuf_put(a,enc->pubkey_algo ); n = pubkey_get_nenc( enc->pubkey_algo ); if ( !n ) write_fake_data( a, enc->data[0] ); for (i=0; i < n && !rc ; i++ ) rc = mpi_write(a, enc->data[i] ); if (!rc) { write_header(out, ctb, iobuf_get_temp_length(a) ); rc = iobuf_write_temp( out, a ); } iobuf_close(a); return rc; }
static int do_compressed( IOBUF out, int ctb, PKT_compressed *cd ) { int rc = 0; /* We must use the old convention and don't use blockmode for the sake of PGP 2 compatibility. However if the new_ctb flag was set, CTB is already formatted as new style and write_header2 does create a partial length encoding using new the new style. */ write_header2(out, ctb, 0, 0); iobuf_put(out, cd->algorithm ); /* This is all. The caller has to write the real data */ return rc; }
static int do_encrypted_mdc( IOBUF out, int ctb, PKT_encrypted *ed ) { int rc = 0; u32 n; assert( ed->mdc_method ); /* Take version number and the following MDC packet in account. */ n = ed->len ? (ed->len + ed->extralen + 1 + 22) : 0; write_header(out, ctb, n ); iobuf_put(out, 1 ); /* version */ /* This is all. The caller has to write the real data */ return rc; }
/**************** * Take file and write it out with armor */ int enarmor_file( const char *fname ) { armor_filter_context_t *afx; IOBUF inp = NULL, out = NULL; int rc = 0; int c; afx = new_armor_context (); /* prepare iobufs */ inp = iobuf_open(fname); if (inp && is_secured_file (iobuf_get_fd (inp))) { iobuf_close (inp); inp = NULL; errno = EPERM; } if (!inp) { rc = gpg_error_from_syserror (); log_error(_("can't open `%s': %s\n"), fname? fname: "[stdin]", strerror(errno) ); goto leave; } if( (rc = open_outfile( fname, 1, &out )) ) goto leave; afx->what = 4; afx->hdrlines = "Comment: Use \"gpg --dearmor\" for unpacking\n"; push_armor_filter ( afx, out ); while( (c = iobuf_get(inp)) != -1 ) iobuf_put( out, c ); leave: if( rc ) iobuf_cancel(out); else iobuf_close(out); iobuf_close(inp); release_armor_context (afx); return rc; }
/**************** * write an mpi to out. */ int mpi_write( IOBUF out, MPI a ) { int rc; unsigned nbits = mpi_get_nbits(a); byte *p, *buf; unsigned n; if( nbits > MAX_EXTERN_MPI_BITS ) log_bug("mpi_encode: mpi too large (%u bits)\n", nbits); iobuf_put(out, (nbits >>8) ); iobuf_put(out, (nbits) ); p = buf = mpi_get_buffer( a, &n, NULL ); rc = iobuf_write( out, p, n ); xfree(buf); return rc; }
static int do_secret_key( IOBUF out, int ctb, PKT_secret_key *sk ) { int rc = 0; int i, nskey, npkey; IOBUF a = iobuf_temp(); /* Build in a self-enlarging buffer. */ /* Write the version number - if none is specified, use 3 */ if ( !sk->version ) iobuf_put ( a, 3 ); else iobuf_put ( a, sk->version ); write_32 (a, sk->timestamp ); /* v3 needs the expiration time. */ if ( sk->version < 4 ) { u16 ndays; if ( sk->expiredate ) ndays = (u16)((sk->expiredate - sk->timestamp) / 86400L); else ndays = 0; write_16(a, ndays); } iobuf_put (a, sk->pubkey_algo ); /* Get number of secret and public parameters. They are held in one array first the public ones, then the secret ones. */ nskey = pubkey_get_nskey ( sk->pubkey_algo ); npkey = pubkey_get_npkey ( sk->pubkey_algo ); /* If we don't have any public parameters - which is the case if we don't know the algorithm used - the parameters are stored as one blob in a faked (opaque) MPI. */ if ( !npkey ) { write_fake_data( a, sk->skey[0] ); goto leave; } assert ( npkey < nskey ); /* Writing the public parameters is easy. */ for (i=0; i < npkey; i++ ) if ((rc = mpi_write (a, sk->skey[i]))) goto leave; /* Build the header for protected (encrypted) secret parameters. */ if ( sk->is_protected ) { if ( is_RSA(sk->pubkey_algo) && sk->version < 4 && !sk->protect.s2k.mode ) { /* The simple rfc1991 (v3) way. */ iobuf_put (a, sk->protect.algo ); iobuf_write (a, sk->protect.iv, sk->protect.ivlen ); } else { /* OpenPGP protection according to rfc2440. */ iobuf_put(a, sk->protect.sha1chk? 0xfe : 0xff ); iobuf_put(a, sk->protect.algo ); if ( sk->protect.s2k.mode >= 1000 ) { /* These modes are not possible in OpenPGP, we use them to implement our extensions, 101 can be seen as a private/experimental extension (this is not specified in rfc2440 but the same scheme is used for all other algorithm identifiers) */ iobuf_put(a, 101 ); iobuf_put(a, sk->protect.s2k.hash_algo ); iobuf_write(a, "GNU", 3 ); iobuf_put(a, sk->protect.s2k.mode - 1000 ); } else { iobuf_put(a, sk->protect.s2k.mode ); iobuf_put(a, sk->protect.s2k.hash_algo ); } if ( sk->protect.s2k.mode == 1 || sk->protect.s2k.mode == 3 ) iobuf_write (a, sk->protect.s2k.salt, 8 ); if ( sk->protect.s2k.mode == 3 ) iobuf_put (a, sk->protect.s2k.count ); /* For our special modes 1001, 1002 we do not need an IV. */ if ( sk->protect.s2k.mode != 1001 && sk->protect.s2k.mode != 1002 ) iobuf_write (a, sk->protect.iv, sk->protect.ivlen ); } } else iobuf_put (a, 0 ); if ( sk->protect.s2k.mode == 1001 ) ; /* GnuPG extension - don't write a secret key at all. */ else if ( sk->protect.s2k.mode == 1002 ) { /* GnuPG extension - divert to OpenPGP smartcard. */ iobuf_put(a, sk->protect.ivlen ); /* Length of the serial number or 0 for no serial number. */ /* The serial number gets stored in the IV field. */ iobuf_write(a, sk->protect.iv, sk->protect.ivlen); } else if ( sk->is_protected && sk->version >= 4 ) { /* The secret key is protected - write it out as it is. */ byte *p; unsigned int ndatabits; assert (gcry_mpi_get_flag (sk->skey[npkey], GCRYMPI_FLAG_OPAQUE)); p = gcry_mpi_get_opaque (sk->skey[npkey], &ndatabits ); iobuf_write (a, p, (ndatabits+7)/8 ); } else if ( sk->is_protected ) { /* The secret key is protected the old v4 way. */ for ( ; i < nskey; i++ ) { byte *p; unsigned int ndatabits; assert (gcry_mpi_get_flag (sk->skey[i], GCRYMPI_FLAG_OPAQUE)); p = gcry_mpi_get_opaque (sk->skey[i], &ndatabits); iobuf_write (a, p, (ndatabits+7)/8); } write_16(a, sk->csum ); } else { /* Non-protected key. */ for ( ; i < nskey; i++ ) if ( (rc = mpi_write (a, sk->skey[i]))) goto leave; write_16 (a, sk->csum ); } leave: if (!rc) { /* Build the header of the packet - which we must do after writing all the other stuff, so that we know the length of the packet */ write_header2(out, ctb, iobuf_get_temp_length(a), sk->hdrbytes); /* And finally write it out the real stream */ rc = iobuf_write_temp( out, a ); } iobuf_close(a); /* Close the remporary buffer */ return rc; }
/* If keyblock_out is non-NULL, AND the exit code is zero, then it contains a pointer to the first keyblock found and exported. No other keyblocks are exported. The caller must free it. */ static int do_export_stream( IOBUF out, strlist_t users, int secret, KBNODE *keyblock_out, unsigned int options, int *any ) { int rc = 0; PACKET pkt; KBNODE keyblock = NULL; KBNODE kbctx, node; size_t ndesc, descindex; KEYDB_SEARCH_DESC *desc = NULL; subkey_list_t subkey_list = NULL; /* Track alreay processed subkeys. */ KEYDB_HANDLE kdbhd; strlist_t sl; int indent = 0; *any = 0; init_packet( &pkt ); kdbhd = keydb_new (secret); if (!users) { ndesc = 1; desc = xcalloc ( ndesc, sizeof *desc ); desc[0].mode = KEYDB_SEARCH_MODE_FIRST; } else { for (ndesc=0, sl=users; sl; sl = sl->next, ndesc++) ; desc = xmalloc ( ndesc * sizeof *desc); for (ndesc=0, sl=users; sl; sl = sl->next) { if (classify_user_id (sl->d, desc+ndesc)) ndesc++; else log_error (_("key \"%s\" not found: %s\n"), sl->d, g10_errstr (G10ERR_INV_USER_ID)); } /* It would be nice to see which of the given users did actually match one in the keyring. To implement this we need to have a found flag for each entry in desc and to set this we must check all those entries after a match to mark all matched one - currently we stop at the first match. To do this we need an extra flag to enable this feature so */ } #ifdef ENABLE_SELINUX_HACKS if (secret) { log_error (_("exporting secret keys not allowed\n")); rc = G10ERR_GENERAL; goto leave; } #endif while (!(rc = keydb_search2 (kdbhd, desc, ndesc, &descindex))) { int sha1_warned=0,skip_until_subkey=0; u32 sk_keyid[2]; if (!users) desc[0].mode = KEYDB_SEARCH_MODE_NEXT; /* Read the keyblock. */ rc = keydb_get_keyblock (kdbhd, &keyblock ); if( rc ) { log_error (_("error reading keyblock: %s\n"), g10_errstr(rc) ); goto leave; } if((node=find_kbnode(keyblock,PKT_SECRET_KEY))) { PKT_secret_key *sk=node->pkt->pkt.secret_key; keyid_from_sk(sk,sk_keyid); /* We can't apply GNU mode 1001 on an unprotected key. */ if( secret == 2 && !sk->is_protected ) { log_info(_("key %s: not protected - skipped\n"), keystr(sk_keyid)); continue; } /* No v3 keys with GNU mode 1001. */ if( secret == 2 && sk->version == 3 ) { log_info(_("key %s: PGP 2.x style key - skipped\n"), keystr(sk_keyid)); continue; } /* It does not make sense to export a key with a primary key on card using a non-key stub. We simply skip those keys when used with --export-secret-subkeys. */ if (secret == 2 && sk->is_protected && sk->protect.s2k.mode == 1002 ) { log_info(_("key %s: key material on-card - skipped\n"), keystr(sk_keyid)); continue; } } else { /* It's a public key export, so do the cleaning if requested. Note that both export-clean and export-minimal only apply to UID sigs (0x10, 0x11, 0x12, and 0x13). A designated revocation is never stripped, even with export-minimal set. */ if(options&EXPORT_CLEAN) clean_key(keyblock,opt.verbose,options&EXPORT_MINIMAL,NULL,NULL); } /* And write it. */ for( kbctx=NULL; (node = walk_kbnode( keyblock, &kbctx, 0 )); ) { if( skip_until_subkey ) { if(node->pkt->pkttype==PKT_PUBLIC_SUBKEY || node->pkt->pkttype==PKT_SECRET_SUBKEY) skip_until_subkey=0; else continue; } /* We used to use comment packets, but not any longer. In case we still have comments on a key, strip them here before we call build_packet(). */ if( node->pkt->pkttype == PKT_COMMENT ) continue; /* Make sure that ring_trust packets never get exported. */ if (node->pkt->pkttype == PKT_RING_TRUST) continue; /* If exact is set, then we only export what was requested (plus the primary key, if the user didn't specifically request it). */ if(desc[descindex].exact && (node->pkt->pkttype==PKT_PUBLIC_SUBKEY || node->pkt->pkttype==PKT_SECRET_SUBKEY)) { if (!exact_subkey_match_p (desc+descindex, node)) { /* Before skipping this subkey, check whether any other description wants an exact match on a subkey and include that subkey into the output too. Need to add this subkey to a list so that it won't get processed a second time. So the first step here is to check that list and skip in any case if the key is in that list. We need this whole mess because the import function is not able to merge secret keys and thus it is useless to output them as two separate keys and have import merge them. */ if (subkey_in_list_p (subkey_list, node)) skip_until_subkey = 1; /* Already processed this one. */ else { size_t j; for (j=0; j < ndesc; j++) if (j != descindex && desc[j].exact && exact_subkey_match_p (desc+j, node)) break; if (!(j < ndesc)) skip_until_subkey = 1; /* No other one matching. */ } } if(skip_until_subkey) continue; /* Mark this one as processed. */ { subkey_list_t tmp = new_subkey_list_item (node); tmp->next = subkey_list; subkey_list = tmp; } } if(node->pkt->pkttype==PKT_SIGNATURE) { /* do not export packets which are marked as not exportable */ if(!(options&EXPORT_LOCAL_SIGS) && !node->pkt->pkt.signature->flags.exportable) continue; /* not exportable */ /* Do not export packets with a "sensitive" revocation key unless the user wants us to. Note that we do export these when issuing the actual revocation (see revoke.c). */ if(!(options&EXPORT_SENSITIVE_REVKEYS) && node->pkt->pkt.signature->revkey) { int i; for(i=0;i<node->pkt->pkt.signature->numrevkeys;i++) if(node->pkt->pkt.signature->revkey[i]->class & 0x40) break; if(i<node->pkt->pkt.signature->numrevkeys) continue; } } /* Don't export attribs? */ if( !(options&EXPORT_ATTRIBUTES) && node->pkt->pkttype == PKT_USER_ID && node->pkt->pkt.user_id->attrib_data ) { /* Skip until we get to something that is not an attrib or a signature on an attrib */ while(kbctx->next && kbctx->next->pkt->pkttype==PKT_SIGNATURE) { kbctx=kbctx->next; } continue; } if( secret == 2 && node->pkt->pkttype == PKT_SECRET_KEY ) { /* We don't want to export the secret parts of the * primary key, this is done by using GNU protection mode 1001 */ int save_mode = node->pkt->pkt.secret_key->protect.s2k.mode; node->pkt->pkt.secret_key->protect.s2k.mode = 1001; if ((options&EXPORT_SEXP_FORMAT)) rc = build_sexp (out, node->pkt, &indent); else rc = build_packet (out, node->pkt); node->pkt->pkt.secret_key->protect.s2k.mode = save_mode; } else if (secret == 2 && node->pkt->pkttype == PKT_SECRET_SUBKEY && (opt.export_options&EXPORT_RESET_SUBKEY_PASSWD)) { /* If the subkey is protected reset the passphrase to export an unprotected subkey. This feature is useful in cases of a subkey copied to an unattended machine where a passphrase is not required. */ PKT_secret_key *sk_save, *sk; sk_save = node->pkt->pkt.secret_key; sk = copy_secret_key (NULL, sk_save); node->pkt->pkt.secret_key = sk; log_info (_("about to export an unprotected subkey\n")); switch (is_secret_key_protected (sk)) { case -1: rc = G10ERR_PUBKEY_ALGO; break; case 0: break; default: if (sk->protect.s2k.mode == 1001) ; /* No secret parts. */ else if( sk->protect.s2k.mode == 1002 ) ; /* Card key stub. */ else { rc = check_secret_key( sk, 0 ); } break; } if (rc) { node->pkt->pkt.secret_key = sk_save; free_secret_key (sk); log_error (_("failed to unprotect the subkey: %s\n"), g10_errstr (rc)); goto leave; } if ((options&EXPORT_SEXP_FORMAT)) rc = build_sexp (out, node->pkt, &indent); else rc = build_packet (out, node->pkt); node->pkt->pkt.secret_key = sk_save; free_secret_key (sk); } else { /* Warn the user if the secret key or any of the secret subkeys are protected with SHA1 and we have simple_sk_checksum set. */ if(!sha1_warned && opt.simple_sk_checksum && (node->pkt->pkttype==PKT_SECRET_KEY || node->pkt->pkttype==PKT_SECRET_SUBKEY) && node->pkt->pkt.secret_key->protect.sha1chk) { /* I hope this warning doesn't confuse people. */ log_info(_("WARNING: secret key %s does not have a " "simple SK checksum\n"),keystr(sk_keyid)); sha1_warned=1; } if ((options&EXPORT_SEXP_FORMAT)) rc = build_sexp (out, node->pkt, &indent); else rc = build_packet (out, node->pkt); } if( rc ) { log_error("build_packet(%d) failed: %s\n", node->pkt->pkttype, g10_errstr(rc) ); goto leave; } } if ((options&EXPORT_SEXP_FORMAT) && indent) { for (; indent; indent--) iobuf_put (out, ')'); iobuf_put (out, '\n'); } ++*any; if(keyblock_out) { *keyblock_out=keyblock; break; } }
/**************** * Copy data from INP to OUT and do some escaping if requested. * md is updated as required by rfc2440 */ int copy_clearsig_text( IOBUF out, IOBUF inp, gcry_md_hd_t md, int escape_dash, int escape_from) { unsigned int maxlen; byte *buffer = NULL; /* malloced buffer */ unsigned int bufsize; /* and size of this buffer */ unsigned int n; int truncated = 0; int pending_lf = 0; if( !escape_dash ) escape_from = 0; write_status_begin_signing (md); for(;;) { maxlen = MAX_LINELEN; n = iobuf_read_line( inp, &buffer, &bufsize, &maxlen ); if( !maxlen ) truncated++; if( !n ) break; /* read_line has returned eof */ /* update the message digest */ if( escape_dash ) { if( pending_lf ) { gcry_md_putc ( md, '\r' ); gcry_md_putc ( md, '\n' ); } gcry_md_write ( md, buffer, len_without_trailing_chars (buffer, n, " \t\r\n")); } else gcry_md_write ( md, buffer, n ); pending_lf = buffer[n-1] == '\n'; /* write the output */ if( ( escape_dash && *buffer == '-') || ( escape_from && n > 4 && !memcmp(buffer, "From ", 5 ) ) ) { iobuf_put( out, '-' ); iobuf_put( out, ' ' ); } #if 0 /*defined(HAVE_DOSISH_SYSTEM)*/ /* We don't use this anymore because my interpretation of rfc2440 7.1 * is that there is no conversion needed. If one decides to * clearsign a unix file on a DOS box he will get a mixed line endings. * If at some point it turns out, that a conversion is a nice feature * we can make an option out of it. */ /* make sure the lines do end in CR,LF */ if( n > 1 && ( (buffer[n-2] == '\r' && buffer[n-1] == '\n' ) || (buffer[n-2] == '\n' && buffer[n-1] == '\r'))) { iobuf_write( out, buffer, n-2 ); iobuf_put( out, '\r'); iobuf_put( out, '\n'); } else if( n && buffer[n-1] == '\n' ) { iobuf_write( out, buffer, n-1 ); iobuf_put( out, '\r'); iobuf_put( out, '\n'); } else iobuf_write( out, buffer, n ); #else iobuf_write( out, buffer, n ); #endif } /* at eof */ if( !pending_lf ) { /* make sure that the file ends with a LF */ iobuf_writestr( out, LF ); if( !escape_dash ) gcry_md_putc( md, '\n' ); } if( truncated ) log_info(_("input line longer than %d characters\n"), MAX_LINELEN ); return 0; /* okay */ }