home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / lib / xp / xp_sec.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  5.5 KB  |  215 lines

  1. /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
  2.  *
  3.  * The contents of this file are subject to the Netscape Public License
  4.  * Version 1.0 (the "NPL"); you may not use this file except in
  5.  * compliance with the NPL.  You may obtain a copy of the NPL at
  6.  * http://www.mozilla.org/NPL/
  7.  *
  8.  * Software distributed under the NPL is distributed on an "AS IS" basis,
  9.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
  10.  * for the specific language governing rights and limitations under the
  11.  * NPL.
  12.  *
  13.  * The Initial Developer of this code under the NPL is Netscape
  14.  * Communications Corporation.  Portions created by Netscape are
  15.  * Copyright (C) 1998 Netscape Communications Corporation.  All Rights
  16.  * Reserved.
  17.  */
  18.  
  19.  
  20. #include "xp.h"
  21. #include "cert.h"
  22. #include "xp_sec.h"
  23. #include "ssl.h"
  24.  
  25.  
  26. /* for XP_GetString() */
  27. #include "xpgetstr.h"
  28. extern int XP_SEC_GOT_RSA;
  29. extern int XP_SEC_HIGH_MESSAGE;
  30. extern int XP_SEC_INTERNATIONAL;
  31. extern int XP_SEC_LOW_MESSAGE;
  32. extern int XP_SEC_NO_MESSAGE;
  33.  
  34. extern int XP_PRETTY_CERT_INF0; 
  35.  
  36. /*
  37. ** Take basic security key information and return an allocated string
  38. ** that contains a "pretty printed" version.
  39. */
  40. char *XP_PrettySecurityStatus(int level, char *cipher, int keySize,
  41.                   int secretKeySize)
  42. {
  43.     int baseLen, len;
  44.     char *rv, *base=NULL;
  45.     char temp[100];
  46.  
  47.     switch (level) {
  48.       case SSL_SECURITY_STATUS_ON_HIGH:
  49. #ifdef FORTEZZA
  50.       case SSL_SECURITY_STATUS_FORTEZZA:
  51. #endif
  52.     StrAllocCopy(base, XP_GetString(XP_SEC_HIGH_MESSAGE)); /* Move message to xp_msg.i  */
  53.     if (!base) {
  54.         return NULL;
  55.     }
  56.     baseLen = XP_STRLEN(base);
  57.     break;
  58.  
  59.       case SSL_SECURITY_STATUS_ON_LOW:
  60.     StrAllocCopy(base, XP_GetString(XP_SEC_LOW_MESSAGE)); /* Move message to xp_msg.i  */
  61.     if (!base) {
  62.         return NULL;
  63.     }
  64.     baseLen = XP_STRLEN(base);
  65.     break;
  66.  
  67.       default:
  68.     StrAllocCopy(base, XP_GetString(XP_SEC_NO_MESSAGE));  /* Move message to xp_msg.i  */
  69.     return base;
  70.     }
  71.     temp[0] = '\0';
  72.  
  73.     if (keySize != secretKeySize) {
  74.     XP_SPRINTF(temp, " (%s, %d bit with %d secret).", cipher, keySize,
  75.            secretKeySize);
  76.     } else {
  77.     XP_SPRINTF(temp, " (%s, %d bit).", cipher, keySize);
  78.     }
  79.     len = baseLen + XP_STRLEN(temp);
  80.     rv = (char*) XP_ALLOC(len+1);
  81.     if (rv) {
  82.     XP_STRCPY(rv, base);
  83.     XP_STRCAT(rv, temp);
  84.     }
  85.     if (base)
  86.         XP_FREE(base);
  87.     return rv;
  88. }
  89.  
  90. static char *hex = "0123456789ABCDEF";
  91.  
  92. /*
  93. ** Convert a der-encoded integer to a hex printable string form
  94. */
  95. static char *Hexify(SECItem *i)
  96. {
  97.     unsigned char *cp, *end;
  98.     char *rv, *o;
  99.  
  100.     if (!i->len) {
  101.     return XP_STRDUP("00");
  102.     }
  103.  
  104.     rv = o = (char*) XP_ALLOC(i->len * 3);
  105.     if (!rv) return rv;
  106.  
  107.     cp = i->data;
  108.     end = cp + i->len;
  109.     while (cp < end) {
  110.     unsigned char ch = *cp++;
  111.     *o++ = hex[(ch >> 4) & 0xf];
  112.     *o++ = hex[ch & 0xf];
  113.     if (cp != end) {
  114.         *o++ = ':';
  115.     } else {
  116.         *o++ = 0;
  117.     }
  118.     }
  119.     return rv;
  120. }
  121.  
  122. /*
  123. ** Return a static string (NOT MALLOC'D) which describes what security
  124. ** version the application supports: domestic or international.
  125. **
  126. ** Bobj will hate me for this.
  127. */
  128. char *XP_SecurityVersion(int longForm)
  129. {
  130.     int domestic = SSL_IsDomestic();
  131.     if (domestic) {
  132.     return longForm ? "U.S." : "U";
  133.     }
  134.     return longForm ? XP_GetString(XP_SEC_INTERNATIONAL) : "I";
  135. }
  136.  
  137. /*
  138. ** Return a dynamically allocated string which describes what security
  139. ** version the security library supports. The returned string describes
  140. ** in totality the crypto capabilities of the library.
  141. */
  142.  
  143. #define GOT_RSA            "RSA Public Key Cryptography"
  144. #define GOT_MD2            "MD2"
  145. #define GOT_MD5            "MD5"
  146. #define GOT_RC2_CBC        "RC2-CBC"
  147. #define GOT_RC4            "RC4"
  148. #define GOT_DES_CBC        "DES-CBC"
  149. #define GOT_DES_EDE3_CBC    "DES-EDE3-CBC"
  150. #define GOT_IDEA_CBC        "IDEA-CBC"
  151. static char SEP[3] = { ',', ' ', '\0' };
  152.  
  153. char *XP_SecurityCapabilities(void)
  154. {
  155.     unsigned long c = SSL_SecurityCapabilities();
  156.     int len = 1;
  157.     char *rv;
  158.  
  159.     /* Figure out how long the returned string will be */
  160.     if (c & SSL_SC_RSA)
  161.     len += strlen(XP_GetString(XP_SEC_GOT_RSA)) + sizeof(SEP) - 1;
  162.     if (c & SSL_SC_MD2)
  163.     len += sizeof(GOT_MD2) + sizeof(SEP) - 1;
  164.     if (c & SSL_SC_MD5)
  165.     len += sizeof(GOT_MD5) + sizeof(SEP) - 1;
  166.     if (c & SSL_SC_RC2_CBC)
  167.     len += sizeof(GOT_RC2_CBC) + sizeof(SEP) - 1;
  168.     if (c & SSL_SC_RC4)
  169.     len += sizeof(GOT_RC4) + sizeof(SEP) - 1;
  170.     if (c & SSL_SC_DES_CBC)
  171.     len += sizeof(GOT_DES_CBC) + sizeof(SEP) - 1;
  172.     if (c & SSL_SC_DES_EDE3_CBC)
  173.     len += sizeof(GOT_DES_EDE3_CBC) + sizeof(SEP) - 1;
  174.     if (c & SSL_SC_IDEA_CBC)
  175.     len += sizeof(GOT_IDEA_CBC) + sizeof(SEP) - 1;
  176.  
  177.     /* Now construct the string */
  178.     rv = (char*) XP_ALLOC(len);
  179.     if (rv) {
  180.     rv[0] = 0;
  181.     if (c & SSL_SC_RSA) {
  182.         XP_STRCAT(rv, XP_GetString(XP_SEC_GOT_RSA));
  183.     }
  184.     if (c & SSL_SC_MD2) {
  185.         if (rv[0]) XP_STRCAT(rv, SEP);
  186.         XP_STRCAT(rv, GOT_MD2);
  187.     }
  188.     if (c & SSL_SC_MD5) {
  189.         if (rv[0]) XP_STRCAT(rv, SEP);
  190.         XP_STRCAT(rv, GOT_MD5);
  191.     }
  192.     if (c & SSL_SC_RC2_CBC) {
  193.         if (rv[0]) XP_STRCAT(rv, SEP);
  194.         XP_STRCAT(rv, GOT_RC2_CBC);
  195.     }
  196.     if (c & SSL_SC_RC4) {
  197.         if (rv[0]) XP_STRCAT(rv, SEP);
  198.         XP_STRCAT(rv, GOT_RC4);
  199.     }
  200.     if (c & SSL_SC_DES_CBC) {
  201.         if (rv[0]) XP_STRCAT(rv, SEP);
  202.         XP_STRCAT(rv, GOT_DES_CBC);
  203.     }
  204.     if (c & SSL_SC_DES_EDE3_CBC) {
  205.         if (rv[0]) XP_STRCAT(rv, SEP);
  206.         XP_STRCAT(rv, GOT_DES_EDE3_CBC);
  207.     }
  208.     if (c & SSL_SC_IDEA_CBC) {
  209.         if (rv[0]) XP_STRCAT(rv, SEP);
  210.         XP_STRCAT(rv, GOT_IDEA_CBC);
  211.     }
  212.     }
  213.     return rv;
  214. }
  215.