⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 doencode.c

📁 PGP—Pretty Good Privacy
💻 C
📖 第 1 页 / 共 3 页
字号:
int signfile(struct pgpmainBones *mainbPtr, PGPBoolean separateSignature,
        char *infile, char *outfile, PGPBoolean attemptCompression )
{
    PGPContextRef context = mainbPtr->pgpContext;
    struct pgpfileBones *filebPtr = mainbPtr->filebPtr;
    struct pgpargsBones *argsbPtr = mainbPtr->argsbPtr;
    struct pgpenvBones *envbPtr = mainbPtr->envbPtr;
    PGPEnv *env = pgpContextGetEnvironment( context );
    PGPBoolean mine=FALSE;

    PGPFileSpecRef infilespec = NULL, outfilespec = NULL;
    PGPKeySetRef keyringset = NULL;
    PGPKeyRef mykey=NULL;
    PGPError err,er2;
    PGPInt32 pri;
    char *passphrase;
    PGPBoolean compatible = envbPtr->compatible;

    PGPBoolean textmode = pgpenvGetInt( env, PGPENV_TEXTMODE, &pri, &err);
    PGPBoolean batchMode = pgpenvGetInt( env, PGPENV_BATCHMODE, &pri, &err );
    PGPBoolean armorout = pgpenvGetInt( env, PGPENV_ARMOR, &pri, &err);

    PGPBoolean clearsig = pgpenvGetInt( env, PGPENV_CLEARSIG, &pri, &err)
                && textmode && armorout && !argsbPtr->stripSigFlag;
        /* clear-sign only makes sense if file is text and armor is on */

    err = PGPNewFileSpecFromFullPath(context, infile, &infilespec);
    pgpAssertNoErr(err);
    err = PGPNewFileSpecFromFullPath(context, outfile, &outfilespec);
    pgpAssertNoErr(err);

    err = PGPOpenDefaultKeyRings( context, 0, &keyringset );

    if( IsPGPError( err )) {
        fprintf( filebPtr->pgpout, LANG("\nCan't open key rings\n"));
        goto done;
    }

    mainbPtr->workingRingSet=keyringset;
    /*mainbPtr->workingGroupSet=NULL;*/
    err = pgpGetMySigningKey( mainbPtr, &mykey );
    if( IsPGPError(err)) {
        pgpShowError(filebPtr, err,0,0); /*, __FILE__,__LINE__);*/
        goto done;
    }

    /* should we add all the passphrases we know of to the job options?*/
    err = pgpRewindPassphrase( envbPtr->passwds );
    pgpAssertNoErr(err);

    err = pgpGetValidPassphrase( mainbPtr, mykey, &passphrase, &mine );

    if( IsntPGPError( err ) ) {

        /* encode the file... */
        err = PGPEncode( context,
                PGPOInputFile( context, infilespec ),
                PGPOOutputFile( context, outfilespec ),

                argsbPtr->signFlag ? PGPOSignWithKey( context, mykey,
                        PGPOPassphrase( context, passphrase ),
                        PGPOLastOption( context )  ) : PGPONullOption(
                                context ),
                argsbPtr->signFlag ? PGPOClearSign( context, clearsig )
                        : PGPONullOption( context ),
                argsbPtr->stripSigFlag ? PGPODetachedSig( context,
                        PGPOLastOption( context ) ) : PGPONullOption(
                                context ),
                PGPODataIsASCII( context, textmode ),
                PGPOCompression( context, attemptCompression),
                PGPOAskUserForEntropy( context, !batchMode ),
                PGPOArmorOutput(context, armorout ),

                /* warn below validity,*/
                PGPOEventHandler( context, enchandler, (PGPUserValue)
                        mainbPtr),

                PGPOLastOption( context ) );

        if( !compatible && IsPGPError( err ) )
            pgpShowError(filebPtr,err,__FILE__,__LINE__);

        if (mine) {
            er2 = PGPFreeData( passphrase );
            pgpRemoveFromPointerList( mainbPtr->leaks, passphrase );
            pgpAssertNoErr(er2);
        }
    } else if( !compatible )
        pgpShowError(filebPtr,err,__FILE__,__LINE__);
done:
    if(keyringset) {
        PGPFreeKeySet( keyringset );
        mainbPtr->workingRingSet=NULL;
    }
    if(infilespec)
        PGPFreeFileSpec(infilespec);
    if(outfilespec)
        PGPFreeFileSpec(outfilespec);
    return err;
}

PGPError issueAlgorithmWarning(struct pgpmainBones * mainbPtr,
                      PGPKeySetRef keyset)
{
    struct pgpfileBones *filebPtr = mainbPtr->filebPtr;
    PGPKeyListRef keylist = NULL;
    PGPKeyIterRef keyiter = NULL;
    PGPKeyRef key = NULL;
    PGPError err;
    PGPUInt32 algorithm;
    PGPBoolean RSAKeys = FALSE, nonRSAKeys = FALSE;
    /* Warn if user is encrypting to both RSA and non-RSA keys. */

    err = PGPOrderKeySet( keyset, kPGPAnyOrdering, &keylist );
    pgpAssertNoErr(err);
    err = PGPNewKeyIter( keylist, &keyiter );
    pgpAssertNoErr(err);
    err = PGPKeyIterRewind( keyiter );
    pgpAssertNoErr(err);
    err = PGPKeyIterNext( keyiter, &key);
    /*if error, no keys found.*/

    while( key != NULL )
    {
        err = PGPGetKeyNumber(key, kPGPKeyPropAlgID, &algorithm);
        pgpAssertNoErr(err);

        switch (algorithm) {
            case kPGPPublicKeyAlgorithm_RSA:
            RSAKeys = TRUE;
            break;
            default:
            nonRSAKeys = TRUE;
        }

        err = PGPKeyIterNext( keyiter, &key);
        /*if err, no more keys*/
    }
    if(err == kPGPError_EndOfIteration)
        err = kPGPError_NoErr;

    if(keyiter)
        PGPFreeKeyIter( keyiter );
    if(keylist)
        PGPFreeKeyList( keylist );

    if ( RSAKeys && nonRSAKeys) {
        fprintf(filebPtr->pgpout,
LANG("WARNING: Encrypting to both RSA and non-RSA keys.\n"));
        fprintf(filebPtr->pgpout,
LANG("         PGP 2.6.2 users may not be able to decrypt this message.\n"));
    }
    return err;

}


int encryptFile(struct pgpmainBones *mainbPtr, const char **recipients,
        char *infile, char *outfile, PGPBoolean attemptCompression)
{
    PGPContextRef context = mainbPtr->pgpContext;
    struct pgpfileBones *filebPtr = mainbPtr->filebPtr;
    struct pgpargsBones *argsbPtr = mainbPtr->argsbPtr;
    struct pgpenvBones *envbPtr = mainbPtr->envbPtr;
    PGPEnv *env = pgpContextGetEnvironment( context );

    PGPFileSpecRef infilespec=NULL, outfilespec=NULL;
    PGPKeySetRef keyringset=NULL, resultset=NULL;
    PGPUInt32 numkeys;
    PGPKeyRef mykey=NULL;
    PGPError err,er2;
    PGPInt32 pri;
    char *passphrase;
    PGPBoolean mine=FALSE;
    PGPBoolean compatible = envbPtr->compatible;

    PGPBoolean textmode = pgpenvGetInt( env, PGPENV_TEXTMODE, &pri, &er2);
    PGPBoolean batchMode = pgpenvGetInt( env, PGPENV_BATCHMODE, &pri, &er2 );
    PGPBoolean armorout = pgpenvGetInt( env, PGPENV_ARMOR, &pri, &err);
    PGPBoolean encrypttoself = pgpenvGetInt( env, PGPENV_ENCRYPTTOSELF,
            &pri, &er2);
    PGPBoolean clearsig;

    clearsig = pgpenvGetInt( env, PGPENV_CLEARSIG, &pri, &er2);
    /* prevent certain feature interactions with clearsig.*/
    if( pri <= PGPENV_PRI_PUBDEFAULT )
        clearsig = FALSE;

    err = PGPNewFileSpecFromFullPath(context, infile, &infilespec);
    pgpAssertNoErr(err);
    err = PGPNewFileSpecFromFullPath(context, outfile, &outfilespec);
    pgpAssertNoErr(err);

    err = PGPOpenDefaultKeyRings( context, 0, &keyringset );

    if( IsPGPError( err )) {
        fprintf( filebPtr->pgpout, LANG("\nCan't open key rings\n"));
        PGPFreeFileSpec( infilespec );
        PGPFreeFileSpec( outfilespec );
        return -1;
    }

    if( argsbPtr->signFlag || encrypttoself) {
        mainbPtr->workingRingSet=keyringset;
        /*mainbPtr->workingGroupSet=NULL;*/
        err = pgpGetMySigningKey( mainbPtr, &mykey );
        if( IsPGPError(err) ) {
            pgpShowError(filebPtr, err, 0,0); /*__FILE__,__LINE__);*/
            goto done;
        }
        if(argsbPtr->signFlag)
            err = pgpGetValidPassphrase( mainbPtr, mykey, &passphrase,
                    &mine );

    }

    /* build a key set containing the public keys of the recipients...*/
    if( IsntPGPError(err)) {
        mainbPtr->workingRingSet = keyringset;

        err = pgpBuildKeySetFromUserIDStringList( mainbPtr, recipients,
                &resultset );

        if( IsPGPError(err) )
            goto done;
    }

    if( IsntPGPError(err) && encrypttoself )
    {
        PGPKeySetRef tmpset,myset;
        err = PGPNewSingletonKeySet( mykey, &myset );
        err = PGPUnionKeySets( myset, resultset, &tmpset );
        PGPFreeKeySet( resultset );
        PGPFreeKeySet( myset );
        resultset = tmpset;
    }

    if ( IsntPGPError(err) ) {
        PGPCountKeys( resultset, &numkeys );

        err = issueAlgorithmWarning(mainbPtr, resultset);
        pgpAssertNoErr(err);

        if( pgpenvGetInt( env, PGPENV_VERBOSE, &pri, &err) ) {
            fprintf( filebPtr->pgpout, LANG("\nnum keys : %d keys\n"),
                    numkeys);
            fprintf( filebPtr->pgpout, LANG("armor out: %d\n"), armorout );
            fprintf( filebPtr->pgpout, LANG("sign flag: %d\n"),
                    argsbPtr->signFlag  );
            if(argsbPtr->signFlag)
                fprintf( filebPtr->pgpout, LANG("clearsign: %d\n"),
                        clearsig );
            fprintf( filebPtr->pgpout, LANG("strip sig: %d\n"),
                    argsbPtr->stripSigFlag  );
            fprintf( filebPtr->pgpout, LANG("textmode : %d\n"), textmode );
            fprintf( filebPtr->pgpout, LANG("compressn: %d\n"),
                    attemptCompression);
            fprintf( filebPtr->pgpout, LANG("youreyeso: %d\n"),
                    envbPtr->moreFlag );
            fprintf( filebPtr->pgpout, LANG("batchMode: %d\n"), batchMode );
        }

        /* encrypt the file... */
        err = PGPEncode( context,
                PGPOInputFile( context, infilespec ),
                PGPOOutputFile( context, outfilespec ),

                argsbPtr->signFlag ? PGPOSignWithKey( context, mykey,
                        PGPOPassphrase( context, passphrase ),
                        PGPOLastOption( context )  ) : PGPONullOption(
                                context ),
                argsbPtr->signFlag ? PGPOClearSign( context, clearsig )
                : PGPONullOption( context ),
                argsbPtr->stripSigFlag ? PGPODetachedSig( context,
                        PGPOLastOption( context ) ) : PGPONullOption(
                        context ),
                PGPODataIsASCII( context, textmode ),
                PGPOCompression( context, attemptCompression),
                PGPOForYourEyesOnly( context, envbPtr->moreFlag ),
                PGPOAskUserForEntropy( context, !batchMode ),
                PGPOEncryptToKeySet( context, resultset ),
                PGPOArmorOutput(context, armorout ),

                /* warn below validity,*/
                PGPOEventHandler( context, enchandler, (PGPUserValue)
                        mainbPtr),
                PGPOLastOption( context ) );

        if( mine ) {
            PGPFreeData(passphrase);
            pgpRemoveFromPointerList( mainbPtr->leaks, passphrase );
        }
    }

    if( !compatible && IsPGPError( err ) )
        pgpShowError( filebPtr, err,__FILE__,__LINE__ );

done:
    if( resultset )
        PGPFreeKeySet( resultset );
    if( keyringset ) {
        PGPFreeKeySet( keyringset );
        mainbPtr->workingRingSet=NULL;
    }
    if( infilespec )
        PGPFreeFileSpec(infilespec);
    if( outfilespec )
        PGPFreeFileSpec(outfilespec);
    return err;
}

int conventionalEncryptFile(struct pgpmainBones *mainbPtr, char *infile,
        char *outfile, PGPBoolean attemptCompression)
{
    PGPContextRef context = mainbPtr->pgpContext;
    struct pgpfileBones *filebPtr = mainbPtr->filebPtr;
    struct pgpargsBones *argsbPtr = mainbPtr->argsbPtr;
    struct pgpenvBones *envbPtr = mainbPtr->envbPtr;
    PGPEnv *env = pgpContextGetEnvironment( context );
    PGPBoolean mine=FALSE, signMine=FALSE;

    PGPFileSpecRef infilespec = NULL, outfilespec = NULL;
    PGPKeySetRef keyringset=NULL;
    PGPKeyRef mykey=NULL;
    PGPError err,er2;
    PGPInt32 pri;
    char *passphrase, *signPassphrase;

    PGPBoolean textmode = pgpenvGetInt( env, PGPENV_TEXTMODE, &pri, &er2);
    PGPBoolean batchMode = pgpenvGetInt( env, PGPENV_BATCHMODE, &pri, &er2 );
    PGPBoolean armorout = pgpenvGetInt( env, PGPENV_ARMOR, &pri, &err);

    PGPBoolean compatible = envbPtr->compatible;

    PGPBoolean clearsig;

    clearsig = pgpenvGetInt( env, PGPENV_CLEARSIG, &pri, &er2);
    /* prevent certain feature interactions with clearsig.*/
    if( pri <= PGPENV_PRI_PUBDEFAULT )
        clearsig = FALSE;

    err = PGPNewFileSpecFromFullPath(context, infile, &infilespec);
    pgpAssertNoErr(err);
    err = PGPNewFileSpecFromFullPath(context, outfile, &outfilespec);
    pgpAssertNoErr(err);

    err = PGPOpenDefaultKeyRings( context, 0, &keyringset );

    if( IsPGPError( err )) {
        fprintf( filebPtr->pgpout, LANG("\nCan't open key rings\n"));
        PGPFreeFileSpec( infilespec );
        PGPFreeFileSpec( outfilespec );
        return -1;
    }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -