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

📄 keyedit.c

📁 vc环境下的pgp源码
💻 C
📖 第 1 页 / 共 2 页
字号:
/*____________________________________________________________________________
    Keyedit.c

    Copyright(C) 1998,1999 Network Associates, Inc.
    All rights reserved.

	PGP 6.5 Command Line 

    use the PGP SDK to edit the keyring.

    $Id: keyedit.c,v 1.11.6.1 1999/06/14 21:56:17 sluu Exp $
____________________________________________________________________________*/

#include <stdio.h>
#include <string.h>

#include "pgpBase.h"
#include "pgpKeys.h"
#include "pgpErrors.h"
#include "pgpUtilities.h"
#include "pgpSDKPrefs.h"

#include "usuals.h"
#include "pgp.h"
#include "globals.h"
#include "fileio.h"
#include "prototypes.h"
#include "language.h"


/* figure out the secret ring file name from the
   public ring file name. */

PGPError pgpGetCorrespondingSecretRingName( struct pgpfileBones *filebPtr,
        const char *pubringfile, char *secringfile )
{
    char *base;
    strcpy( secringfile, pubringfile );
    base = fileTail( secringfile );
    if( strcmp( base, "pubring.pkr" )==0 )
    {
        /* this depends on a side-effect of the above fileTail() call,
           that base points to an address _within_ the secringfile string. */

        base[0]='\0';
        strcat( base, "secring.skr" );
    }

    forceExtension( filebPtr, secringfile, filebPtr->SKR_EXTENSION );
    if( fileExists( secringfile ))
        return kPGPError_NoErr;
    else
        return kPGPError_FileNotFound;
}

PGPError pgpGetCorrespondingPublicRingName( struct pgpfileBones *filebPtr,
        const char *secringfile, char *pubringfile )
{
    char *base;
    strcpy( pubringfile, secringfile );
    base = fileTail( pubringfile );
    if( strcmp( base, "pubring.pkr" )==0 )
    {
        /* this depends on a side-effect of the above fileTail() call,
           that base points to an address _within_ the pubringfile string. */

        base[0]='\0';
        strcat( base, "secring.skr" );
    }

    forceExtension( filebPtr, pubringfile, filebPtr->SKR_EXTENSION );
    if( fileExists( pubringfile ))
        return kPGPError_NoErr;
    else
        return kPGPError_FileNotFound;
}

/*
   Try to get a valid passphrase.  Always trying getting it from the
   list of saved passphrases first.  If not in batchmode, maybe ask
   the user.  Perhaps we could redesign this to have the passphrase list
   use reference counts?
 */
PGPError pgpGetValidPassphrase( struct pgpmainBones *mainbPtr, PGPKeyRef
        key, char **passphrasePtr, PGPBoolean *needsfree )
{
    PGPContextRef context = mainbPtr->pgpContext;
    struct pgpfileBones *filebPtr = mainbPtr->filebPtr;
    struct pgpenvBones *envbPtr = filebPtr->envbPtr;
    PGPEnv *env = envbPtr->m_env;
    PGPBoolean valid;
    PGPInt32 attempts,pri;
    PGPError err;
    PGPBoolean quietmode = pgpenvGetInt( env, PGPENV_NOOUT, &pri, &err);
    PGPBoolean batchmode = pgpenvGetInt( env, PGPENV_BATCHMODE, &pri, &err );

    *needsfree=FALSE;

    pgpRewindPassphrase( envbPtr->passwds );
    for(;;) {
        pgpNextPassphrase( envbPtr->passwds, passphrasePtr );
        if(*passphrasePtr == NULL)
            break;
        valid = PGPPassphraseIsValid( key,
                PGPOPassphrase( context, *passphrasePtr ),
                PGPOLastOption( context ));
        if(valid) {
            if (!quietmode)
                fprintf( filebPtr->pgpout, LANG("\nPassphrase is good\n"));
            return kPGPError_NoErr;
        } else {
            if(batchmode)
                fprintf( filebPtr->pgpout,
                        LANG("\nError:  Bad pass phrase.\n"));
        }
    }

    if(!batchmode)
      for( attempts=0; attempts < 3; attempts++ )
      {
          fprintf( filebPtr->pgpout, LANG(
"\nYou need a pass phrase to unlock your secret key.") );
          pgpShowKeyUserID( filebPtr, key );

          err = pgpPassphraseDialogCmdline( mainbPtr, FALSE, NULL,
                  passphrasePtr);

          if( IsPGPError(err) )
              return err;

          *needsfree = (*passphrasePtr != NULL);

          valid = PGPPassphraseIsValid( key,
                  PGPOPassphrase( context, *passphrasePtr ),
                  PGPOLastOption( context ));

          if(valid) {
              if (!quietmode)
                  fprintf( filebPtr->pgpout, LANG("\nPassphrase is good\n"));

              /* add to the passphrase list...*/
              pgpAppendToPassphraseList( envbPtr->passwds, *passphrasePtr );

              return kPGPError_NoErr;
          }
          else {
              fprintf( filebPtr->pgpout,
                      LANG("\nError:  Bad pass phrase.\n"));
              PGPFreeData( *passphrasePtr );
              pgpRemoveFromPointerList( mainbPtr->leaks, *passphrasePtr );
              *passphrasePtr=NULL;
              *needsfree=FALSE;
          }
      }
    if(*needsfree) {
        PGPFreeData( *passphrasePtr );
        pgpRemoveFromPointerList( mainbPtr->leaks, *passphrasePtr );
    }
    *passphrasePtr=NULL;
    *needsfree=FALSE;
    return kPGPError_BadPassphrase;
}

/*
  inputs: a KeyIter, positioned at a particular key of interest.
  returns the Userid that matches the character string.
 */
PGPError pgpGetKeyIterMatchingUserid( PGPKeyIterRef keyiter, const char
        *searchstr, PGPUserIDRef *useridPtr )
{
    char useridstr[ kPGPMaxUserIDSize ];
    PGPSize actual;
    PGPError err;

    err = PGPKeyIterRewindUserID( keyiter );
    pgpAssertNoErr(err);
    err = PGPKeyIterNextUserID( keyiter, useridPtr);
    pgpAssertNoErr(err);

    while( *useridPtr ) {
        err = PGPGetUserIDStringBuffer( *useridPtr, kPGPUserIDPropName,
                kPGPMaxUserIDSize, useridstr, &actual);
        if( strcmp( useridstr, searchstr ) == 0)
            return kPGPError_NoErr;
        err = PGPKeyIterNextUserID( keyiter, useridPtr);
        /*if err, there are no more*/
    }

   *useridPtr = NULL;
   return kPGPError_ItemNotFound;
}

PGPError pgpEditPublicTrustParameter( struct pgpfileBones *filebPtr,
        const char *useridstr, PGPKeyRef key)
{
    PGPError err;
    char anstr[ 6 ];
    PGPInt32 ann;
    char *str;

    /* print the key info*/

    /* check each of the certs*/

    pgpShowKeyValidity( filebPtr, key );

    /* Questionable certification from:*/
    /* (KeyID: 7A4A5505)*/
    /* Questionable certification from:*/
    /* (KeyID: BD236602)*/

    err = pgpGetKeyTrustString( key, &str );
    pgpAssertNoErr(err);
    fprintf( filebPtr->pgpout,
            LANG("\nCurrent trust for this key's owner is: %s\n"), str);
    fprintf(filebPtr->pgpout, LANG("\
\nMake a determination in your own mind whether this key actually\n\
belongs to the person whom you think it belongs to, based on available\n\
evidence.  If you think it does, then based on your estimate of\n\
that person's integrity and competence in key management, answer\n\
the following question:\n\n"));

        fprintf(filebPtr->pgpout, LANG("Would you trust \"%s\"\n\
to act as an introducer and certify other people's public keys to you?\n\
(1=I don't know (default). 2=No. 3=Usually. 4=Yes, always.) ? "), useridstr );

    fflush(filebPtr->pgpout);
    pgpTtyGetString(anstr, 6, filebPtr->pgpout);

    ann = atoi( anstr );
    switch( ann ) {
        case 4:
            err = PGPSetKeyTrust( key, kPGPKeyTrust_Complete );
            break;
        case 3:
            err = PGPSetKeyTrust( key, kPGPKeyTrust_Marginal );
            break;
        case 2:
            err = PGPSetKeyTrust( key, kPGPKeyTrust_Never );
            break;
        case 1:
        default:
            err = PGPSetKeyTrust( key, kPGPKeyTrust_Unknown );
            break;
    }
    if( IsPGPError(err) )
        pgpShowError( filebPtr, err,__FILE__,__LINE__ );
    return err;
}

static
PGPBoolean	IsDefaultPublicKeyring(PGPContextRef context, char *pszPubringfile)
{
	PGPError		err = kPGPError_NoErr;
	PGPFileSpecRef	pubringSpec = kPGPInvalidRef;
	char			*pubringName = NULL; 
	PGPBoolean		bDefault = FALSE;

	err = PGPsdkPrefGetFileSpec( context, kPGPsdkPref_PublicKeyring,
            &pubringSpec);

	if(IsPGPError(err))
		return FALSE;

    err = PGPGetFullPathFromFileSpec( pubringSpec, &pubringName );
    if(IsPGPError(err))
		goto done;
	if(strcmp(pubringName, pszPubringfile) == 0)
	{
		bDefault = TRUE;
	}

done:
	if(pubringName != NULL)
		PGPFreeData(pubringName);
	if(pubringSpec != kPGPInvalidRef)
		PGPFreeFileSpec(pubringSpec);
	return bDefault;
}


/*
   Takes an optional ring FileSpec that is the public keyring.  User may
   only specify a public keyring (or NULL), but we need
     1. to look on the corresponding secret keyring for the secret key;
     2. to open the keyrings mutable.
 */

PGPError pgpOpenKeyringsFromPubringSpec( struct pgpmainBones *mainbPtr,
        PGPFileSpecRef pubFileSpec, PGPKeySetRef *keyRingSet , 
	PGPUInt32 openFlags)
{
    PGPContextRef context = mainbPtr->pgpContext;
    struct pgpfileBones *filebPtr = mainbPtr->filebPtr;
    PGPError err,er2;

    if(pubFileSpec) {
        char secringfile[ MAX_PATH+1 ];
        char *pubringfile;
        PGPFileSpecRef secFileSpec;

        err = PGPGetFullPathFromFileSpec( pubFileSpec, &pubringfile );
        pgpAssertNoErr(err);

		if(!IsDefaultPublicKeyring(context, pubringfile))
		{
			err = pgpGetCorrespondingSecretRingName( filebPtr, pubringfile,
					secringfile );
			if (IsPGPError(err)) goto done;

			err = PGPNewFileSpecFromFullPath(context, secringfile, &secFileSpec);
			pgpAssertNoErr(err);

			err = PGPOpenKeyRingPair( context, openFlags, 
					pubFileSpec, secFileSpec, keyRingSet );
			er2 = PGPFreeFileSpec( secFileSpec );
			pgpAssertNoErr(er2);
		}
		else
		{
			err = PGPOpenDefaultKeyRings(context, openFlags, keyRingSet);
		}
    done:
        if( IsPGPError(err) ) {
            fprintf(filebPtr->pgpout,
                    LANG("Can't open key ring file '%s'\n"), pubringfile);
        }
	
	er2 = PGPFreeData( pubringfile );
	pgpAssertNoErr(er2);
    } else {
        fprintf(filebPtr->pgpout, LANG("in default key ring\n\n"));

        err = PGPOpenDefaultKeyRings( context,
		openFlags, keyRingSet);

        if( IsPGPError(err) ) {
            fprintf(filebPtr->pgpout, LANG("Can't open default key rings\n"));
        }
    }
    return err;
}

PGPError pgpOpenKeyringsIfSecringSpec( struct pgpmainBones *mainbPtr,
        PGPFileSpecRef fileSpec, PGPKeySetRef *keyRingSet, PGPBoolean
	*isprivate, PGPUInt32 openFlags)
{

⌨️ 快捷键说明

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