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

📄 rdemo.c

📁 Netscape公司提供的安全套接字层
💻 C
📖 第 1 页 / 共 3 页
字号:
 */
static int GetPrivateKey (privateKey)
R_RSA_PRIVATE_KEY **privateKey;
{
  char command[80];
  
  while (1) {
    if (!KEYPAIR3_READY)
      GetCommand (command, sizeof (command), "  Public key 1 or 2?");
    else
      GetCommand (command, sizeof (command), "  Public key 1, 2 or 3?");

    switch (*command) {
    case '\0':
      return (1);
      
    case '1':
      *privateKey = &PRIVATE_KEY1;
      return (0);
      
    case '2':
      *privateKey = &PRIVATE_KEY2;
      return (0);
      
    case '3':
      if (!KEYPAIR3_READY)
        break;
      *privateKey = &PRIVATE_KEY3;
      return (0);
      
    default:
      if (KEYPAIR3_READY)
        PrintError ("ERROR: Please enter 1, 2 or 3.  Try again.", 0);
      else
        PrintError ("ERROR: Please enter 1 or 2.  Try again.", 0);
      break;
    }
  }
}

/* Ask the user to use MD2 or MD5 and point digestAlgorithm to the
     answer.
   Return 0 on success or 1 if user cancels by entering a blank.
 */
static int GetDigestAlgorithm (digestAlgorithm)
int *digestAlgorithm;
{
  char command[80];
  
  while (1) {
    GetCommand (command, sizeof (command), "  MD2 or MD5 (2 or 5)?");

    switch (*command) {
    case '\0':
      return (1);
      
    case '2':
      *digestAlgorithm = DA_MD2;
      return (0);
      
    case '5':
      *digestAlgorithm = DA_MD5;
      return (0);
      
    default:
      PrintError ("ERROR: Please enter 2 or 5.  Try again.", 0);
      break;
    }
  }
}

/* Ask the user to use DES, DESX, DES-EDE2, or DES-EDE3, and point
     encryptionAlgorithm to the answer.
   Return 0 on success or 1 if user cancels by entering a blank.
 */
static int GetEncryptionAlgorithm (encryptionAlgorithm)
int *encryptionAlgorithm;
{
  char command[80];
  
  while (1) {
    GetCommand
      (command, sizeof (command),
       "  DES, DESX, DES-EDE2 or DES-EDE3 (1, X, 2 or 3)?");

    switch (*command) {
    case '\0':
      return (1);
      
    case '1':
      *encryptionAlgorithm = EA_DES_CBC;
      return (0);
      
    case 'x':
    case 'X':
      *encryptionAlgorithm = EA_DESX_CBC;
      return (0);
      
    case '2':
      *encryptionAlgorithm = EA_DES_EDE2_CBC;
      return (0);
      
    case '3':
      *encryptionAlgorithm = EA_DES_EDE3_CBC;
      return (0);
      
    default:
      PrintError ("ERROR: Please enter 1, X, 2 or 3.  Try again.", 0);
      break;
    }
  }
}

/* Ask for the filename using the given prompt string and open it
     for reading.
   Return 0 on success or 1 if error or if user cancels by entering a blank.
 */
static int ReadInit (file, prompt) 
FILE **file;
char *prompt;
{
  char filename[256];
  
  while (1) {
    GetCommand (filename, sizeof (filename), prompt);
    if (! *filename)
      return (1);
    
    if ((*file = fopen (filename, "rb")) != NULL)
      /* successfully opened */
      break;
    
    PrintError ("ERROR: Cannot open a file with that name.  Try again.", 0);
  }

  return (0);
}

/* Read a block of up to length maxPartOutLen bytes from file, storing
     it in partOut and returning its length in partOutLen.
   Return 0 on success or 1 if error or end of file.
 */
static int ReadUpdate (file, partOut, partOutLen, maxPartOutLen)
FILE *file;
unsigned char *partOut;
unsigned int *partOutLen;
unsigned int maxPartOutLen;
{
  int status;
  
  /* fread () returns the number of items read in.
   */
  *partOutLen = fread (partOut, 1, maxPartOutLen, file);

  status = 0;
  if (ferror (file)) {
    PrintError ("ERROR: Cannot read file.", 0);
    status = 1;
  }
  if (*partOutLen == 0 && feof (file))
    status = 1;

  return (status);
}

/* Close file.
 */
static void ReadFinal (file)
FILE *file;
{
  fclose (file);
}

/* Read a file of up to length maxBlockLen bytes, storing it in
     block and returning its length in blockLen.
   Ask for the filename using the given prompt string.
   Return 0 on success or 1 if error or if user cancels by entering a blank.
 */
static int ReadBlock (block, blockLen, maxBlockLen, prompt) 
unsigned char *block;
unsigned int *blockLen;
unsigned int maxBlockLen;
char *prompt;
{
  FILE *file;
  int status;
  unsigned char *dummy;
  unsigned int dummyLen;

  if (ReadInit (&file, prompt))
    return (1);

  if ((status = ReadUpdate (file, block, blockLen, maxBlockLen)) == 0) {
    if (*blockLen == maxBlockLen)
      /* Read exactly maxBlockLen bytes, so reading one more will set 
           end of file if there were exactly maxBlockLen bytes in the file.
       */
      if (!ReadUpdate (file, dummy, &dummyLen, 1)) {
        PrintError ("ERROR: File is too large.", 0);
        status = 1;
      }
  }
    
  ReadFinal (file);

  return (status);
}

/* Ask for the filename using the given prompt string and open it
     for writing. 
   Return 0 on success or 1 if error or if user cancels by entering a blank.
 */
static int WriteInit (file, prompt) 
FILE **file;
char *prompt;
{
  char filename[256];
  
  while (1) {
    GetCommand (filename, sizeof (filename), prompt);
    if (! *filename)
      return (1);
    
    if (filename[0] == '-' && filename[1] == '\0') {
      /* use stdout */
      *file = stdout;
      break;
    }
    if ((*file = fopen (filename, "wb")) != NULL)
      /* successfully opened */
      break;
    
    PrintError ("ERROR: Cannot open a file with that name.  Try again.", 0);
  }

  return (0);
}

/* Write block of length partOutLen to a file.
   Return 0 on success or 1 if error.
 */
static int WriteUpdate (file, partOut, partOutLen)
FILE *file;
unsigned char *partOut;
unsigned int partOutLen;
{
  int status;

  status = 0;
  if (fwrite (partOut, 1, partOutLen, file) < partOutLen) {
    PrintError ("ERROR: Cannot write file.", 0);
    status = 1;
  }
  
  return (status);
}

/* Close file.
 */
static void WriteFinal (file)
FILE *file;
{
  if (file != stdout)
    fclose (file);
}

/* Write block of length blockLen to a file.
   Ask for the filename using the given prompt string.
   Return 0 on success or 1 if error or if user cancels by entering a blank.
 */
static int WriteBlock (block, blockLen, prompt) 
unsigned char *block;
unsigned int blockLen;
char *prompt;
{
  FILE *file;
  int status;

  if (WriteInit (&file, prompt))
    return (1);

  do {
    if ((status = WriteUpdate (file, block, blockLen)) != 0)
      break;
  
    if (file == stdout)
      /* Printing to screen, so print a new line. */
      printf ("\n");
  } while (0);

  WriteFinal (file);

  return (status);
}

static void PrintMessage (message)
char *message;
{
  if (!SILENT_PROMPT) {
    puts (message);
    fflush (stdout);
  }
}

/* If type is zero, simply print the task string, otherwise convert the
     type to a string and print task and type.
 */
static void PrintError (task, type)
char *task;
int type;
{
  char *typeString, buf[80];

  if (type == 0) {
    puts (task);
    return;
  }
  
  /* Convert the type to a string if it is recognized.
   */
  switch (type) {
  case RE_KEY:
    typeString = "Recovered DES key cannot decrypt encrypted content";
    break;
  case RE_LEN:
    typeString = "Encrypted key length or signature length is out of range";
    break;
  case RE_MODULUS_LEN:
    typeString = "Modulus length is out of range";
    break;
  case RE_PRIVATE_KEY:
    typeString = "Private key cannot encrypt message digest, or cannot decrypt encrypted key";
    break;
  case RE_PUBLIC_KEY:
    typeString = "Public key cannot encrypt data encryption key, or cannot decrypt signature";
    break;
  case RE_SIGNATURE:
    typeString = "Signature is incorrect";
    break;
    
  default:
    sprintf (buf, "Code 0x%04x", type);
    typeString = buf;
  }

  printf ("ERROR: %s while %s\n", typeString, task);  
  fflush (stdout);
}

static void GetCommand (command, maxCommandSize, prompt)
char *command;
unsigned int maxCommandSize;
char *prompt;
{
  unsigned int i;
  
  if (!SILENT_PROMPT) {
    printf ("%s (blank to cancel): \n", prompt);  
    fflush (stdout);
  }

  fgets (command, maxCommandSize, stdin);
  
  /* Replace the line terminator with a '\0'.
   */
  for (i = 0; command[i] != '\0'; i++) {
    if (command[i] == '\012' || command[i] == '\015' ||
        i == (maxCommandSize - 1)) {
      command[i] = '\0';
      return;
    }
  }
}

⌨️ 快捷键说明

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