Ejemplo n.º 1
0
void SymInfo::modType( SYMTYPE a )
{
	SYMTYPE temp = type;

	if ( _isReg == true ) 
		type = a;

	else if ( a > type && !isFixed ) 
		type = a;
	else {
		cerr << "Type of " << getName( ) << " NOT changed from " 
			<< e2str( temp ) << " to " << e2str( a ) << endl;
	}
}
Ejemplo n.º 2
0
/* Group of code that is called more then once in keypairgen_putcert() */
int getProvInfo(HCRYPTPROV hProv)
{
   BYTE  pbData[500];
   int   cbData;
   DWORD provType;
   DWORD err;
   int   errors = 0;

   printf("Provider info:\n");

   // Get the PP_NAME param
   if(!CryptGetProvParam(hProv, PP_NAME, NULL, &cbData, 0)) {
       err = GetLastError();
       printf("ERR: CryptGetProvParam(PP_NAME length): %s (0x%0x)\n", e2str(err), err);
       errors++;
   }
   else if(!CryptGetProvParam(hProv, PP_NAME, pbData, &cbData, 0)) {
       err = GetLastError();
       printf("ERR: CryptGetProvParam(PP_NAME): %s (0x%0x)\n", e2str(err), err);
       errors++;
   }
   else
      printf("  PP_NAME: %s\n", pbData);

   // Get the PP_UNIQUE_CONTAINER param
   if(!CryptGetProvParam(hProv, PP_UNIQUE_CONTAINER, NULL, &cbData, 0)) {
       err = GetLastError();
       printf("ERR: CryptGetProvParam(PP_UNIQUE_CONTAINER length): %s (0x%0x)\n", e2str(err), err);
       errors++;
   }
   else if(!CryptGetProvParam(hProv, PP_UNIQUE_CONTAINER, pbData, &cbData, 0)) {
       err = GetLastError();
       printf("ERR: CryptGetProvParam(PP_UNIQUE_CONTAINER): %s (0x%0x)\n", e2str(err), err);
       errors++;
   }
   else
      printf("  PP_UNIQUE_CONTAINER: %s\n", pbData);

   // Get the PP_PROVTYPE param
   if(!CryptGetProvParam(hProv, PP_PROVTYPE, NULL, &provType, 0)) {
       err = GetLastError();
       printf("ERR: CryptGetProvParam(PP_PROVTYPE): %s (0x%0x)\n", e2str(err), err);
       errors++;
   }
   else
      printf("  PP_PROVTYPE: 0x%0x\n", provType);

   return errors;
}
Ejemplo n.º 3
0
int listProvAlgos(const char *containerNumber)
{
   char              provName[PROVIDER_BUFFER_SIZE];
   char              contName[CONTAINER_BUFFER_SIZE];
   DWORD             dwKeyUsage;
   HCRYPTPROV        hProv = 0;
   DWORD             dwFlags = CRYPT_FIRST;
   PROV_ENUMALGS_EX  xProvAlgEx;
   DWORD             dwDataLen = sizeof(xProvAlgEx);
   DWORD             err;

   /* Get the provider name, container name and keyusage (SIGN or KEYEX) */
   if (listMyCerts(containerNumber, provName, contName, &dwKeyUsage))
       return 1;
   if (dwKeyUsage == -1) {
       printf("Container %d not present, exiting\n", atoi(containerNumber));
       return 1;
   }

   /* Acquire the provider handle */
   if(!CryptAcquireContext(&hProv, contName, provName, PROV_RSA_FULL, 0)) {
       err = GetLastError();
       printf("ERR: CryptAcquireContext: %s (0x%0x)\n", e2str(err), err);
       return 1;
   }

   printf("Algo's for key \"%s\" in provider \"%s\":\n", contName, provName);

   while(CryptGetProvParam(hProv, PP_ENUMALGS_EX, (BYTE *) &xProvAlgEx, &dwDataLen, dwFlags))
   {
       printf(" AlgID: 0x%0x, Keylen default: %d, min: %d, max: %d, prots: %d, name: %s\n",
           xProvAlgEx.aiAlgid, xProvAlgEx.dwDefaultLen, xProvAlgEx.dwMinLen,
           xProvAlgEx.dwMaxLen, xProvAlgEx.dwProtocols, xProvAlgEx.szName);

       dwFlags = 0;
       dwDataLen = dwDataLen = sizeof(xProvAlgEx);
   }
   err = GetLastError();
   if (err != ERROR_NO_MORE_ITEMS)
       printf("ERR: last CryptGetProvParam() call returned %s (0x%0x)\n", e2str(err), err);

   if(!CryptReleaseContext(hProv, 0)) {
       err = GetLastError();
       printf("ERR: CryptReleaseContext(): %s (0x%0x)\n", e2str(err), err);
   }

   return 0;
}
Ejemplo n.º 4
0
void SymInfo::declare( const string &name, ostream &out ) const
{
 
	if ( isTemp == true )
		return;

	assert ( Type( ) != FUNC || !_isReg );

	if ( isStatic( ) )
		out << "static ";

	if ( isConst( ) )
		out << " const ";

	if ( isUnsigned( ) )
		out << " unsigned ";

	//output the type
	if ( Type( ) == NOTYPE ) {
		if ( getSize( ) == 4 )
			out << e2str( LONG ) << ' ';
		else if ( getSize( ) == 2 )
			out << e2str( SINT ) << ' ';
		else if ( getSize( ) == 1 )
			out << e2str( CHAR ) << ' ';
		else
			out << e2str( LONG ) << ' ';
	}
	else
		out << e2str( Type( ) ) << ' ';

	out << name;
	if ( isArray( ) )
		out << "[ " << getSize( ) / sizeOfType( getType( ) ) << " ]";

        if ( initValue.size( ) ) {
		out << "= ";
		if ( Type( ) == FLOAT )
			out << convert2Float( atoi( initValue ) );
		else if ( Type( ) == CHAR )
			cout << getChar( atoi( initValue ) );
		else
                	out << initValue;
	}
        out << ';';
}
Ejemplo n.º 5
0
void
process_fddi(register u_char *u, register const struct pcap_pkthdr *h,
    register const u_char *p)
{
	register struct fddi_header *fh;
	register struct ether_arp *ea;
	register u_char *sea, *sha;
	register time_t t;
	u_int32_t sia;

	fh = (struct fddi_header *)p;
	ea = (struct ether_arp *)(fh + 1);

	if (!swapped) {
		bit_reverse(fh->src, 6);
		bit_reverse(fh->dst, 6);
	}
	if (!sanity_fddi(fh, ea, h->caplen))
		return;

	/* Source MAC hardware ethernet address */
	sea = (u_char *)fh->src;

	/* Source ARP ethernet address */
	sha = (u_char *)SHA(ea);

	/* Source ARP ip address */
	BCOPY(SPA(ea), &sia, 4);

	/* Watch for bogons */
	if (isbogon(sia)) {
		dosyslog(LOG_INFO, "bogon", sia, sea, sha);
		return;
	}

	/* Watch for ethernet broadcast */
	if (MEMCMP(sea, zero, 6) == 0 || MEMCMP(sea, allones, 6) == 0 ||
	    MEMCMP(sha, zero, 6) == 0 || MEMCMP(sha, allones, 6) == 0) {
		dosyslog(LOG_INFO, "ethernet broadcast", sia, sea, sha);
		return;
	}

	/* Double check ethernet addresses */
	if (MEMCMP(sea, sha, 6) != 0) {
		dosyslog(LOG_INFO, "ethernet mismatch", sia, sea, sha);
		return;
	}

	/* Got a live one */
	t = h->ts.tv_sec;
	can_checkpoint = 0;
	if (!ent_add(sia, sea, t, NULL))
		syslog(LOG_ERR, "ent_add(%s, %s, %ld) failed",
		    intoa(sia), e2str(sea), t);
	can_checkpoint = 1;
}
Ejemplo n.º 6
0
void
dumpone(register u_int32_t a, register u_char *e, register time_t t,
    register char *h)
{
	(void)fprintf(dumpf, "%s\t%s", e2str(e), intoa(a));
	if (t != 0 || h != NULL)
		(void)fprintf(dumpf, "\t%u", (u_int32_t)t);
	if (h != NULL && *h != '\0')
		(void)fprintf(dumpf, "\t%s", h);
	(void)putc('\n', dumpf);
}
Ejemplo n.º 7
0
/* syslog() helper routine */
void
dosyslog(register int p, register char *s, register u_int32_t a,
    register u_char *ea, register u_char *ha)
{
	char xbuf[64];

	/* No report until we're initialized */
	if (initializing)
		return;

	/* Display both ethernet addresses if they don't match */
	(void)strcpy(xbuf, e2str(ea));
	if (ha != NULL && MEMCMP(ea, ha, 6) != 0) {
		(void)strcat(xbuf, " (");
		(void)strcat(xbuf, e2str(ha));
		(void)strcat(xbuf, ")");
	}

	if (debug)
		fprintf(stderr, "%s: %s %s %s\n", prog, s, intoa(a), xbuf);
	else
		syslog(p, "%s %s %s", s, intoa(a), xbuf);
}
Ejemplo n.º 8
0
void checkSig(unsigned char *tucHashBuf,
			  unsigned char *tucSignature, DWORD dwSignatureLen,
			  unsigned char *tucPubKeyBlob, DWORD dwPubKeyBlobLen)
{
	HCRYPTPROV hProv;
	HCRYPTKEY hPubKey;
	HCRYPTHASH hHash;
	DWORD err;
	int errors = 0;

	if(!CryptAcquireContext(&hProv, NULL, NULL, PROV_RSA_FULL, 0))
		ERR_LOG_RET("CryptAcquireContext()");

	if (!CryptImportKey(hProv, tucPubKeyBlob, dwPubKeyBlobLen, 0, 0, &hPubKey))
		ERR_LOG_RET("CryptImportKey()");

	if (!CryptCreateHash(hProv, CALG_MD5, 0, 0, &hHash))
		ERR_LOG_RET("CryptCreateHash(CALG_MD5)");

	if (!CryptSetHashParam(hHash, HP_HASHVAL, tucHashBuf, 0))
		ERR_LOG_RET("CryptSetHashParam(HP_HASHVAL)");

	if (!CryptVerifySignature(hHash, tucSignature, dwSignatureLen, hPubKey, NULL, 0))
	{
		err = GetLastError();
		printf("ERR (line %d): CryptVerifySignature() returned %s (0x%0x)\n", __LINE__, e2str(err), err);
		errors++;
	}

	if (!CryptDestroyHash(hHash))
		ERR_LOG_RET("CryptDestroyHash()");

	if (!CryptDestroyKey(hPubKey))
		ERR_LOG_RET("CryptDestroyKey()");

	CryptReleaseContext(hProv, 0);

done:
	return;
}
Ejemplo n.º 9
0
/* If containernumber == NULL, list the containers.
 * If containernumber is a string containing the number of the container,
 *         fill in the provider name, container name and keyspec.
 */
long listMyCerts(const char *containerNumber, char *providerName, char *containerName, DWORD *pKeySpec)
{
   HCERTSTORE        hCertStore;
   PCCERT_CONTEXT    pCertContext = NULL;
   int               i = 1;
   int               iContainerNr;

   if (containerNumber != NULL) {
       iContainerNr = atoi(containerNumber);
       providerName[0] = '\0';
       containerName[0] = '\0';
       *pKeySpec = -1;
   }

   if (hCertStore = CertOpenStore(CERT_STORE_PROV_SYSTEM,
       0, 0, CERT_SYSTEM_STORE_CURRENT_USER, L"MY"))
   {
       if (containerNumber == NULL)
            printf("Listing certs in MY cert store:\n");
   }
   else
   {
       int err = GetLastError();
       printf("CertOpenStore: %s (0x%0x)\n", e2str(err), err);
       return err;
   }

   // Retrieve each of the certificates in the store.
   while(pCertContext= CertEnumCertificatesInStore(hCertStore, pCertContext)) {
      char buf[400], provName[200], contName[200];
      DWORD size = sizeof(buf);
      CRYPT_KEY_PROV_INFO *prov_info;

      if (CertGetCertificateContextProperty(pCertContext,
         CERT_KEY_PROV_INFO_PROP_ID, buf, &size)) {
            prov_info = (CRYPT_KEY_PROV_INFO *) buf;

            unicode_to_ascii(prov_info->pwszProvName, provName, sizeof(provName));
            unicode_to_ascii(prov_info->pwszContainerName, contName, sizeof(contName));

            if (containerNumber == NULL) {
                printf("%d.  %S: \t%S  (%s)\n",
                   i, prov_info->pwszProvName, prov_info->pwszContainerName,
                   prov_info->dwKeySpec == AT_KEYEXCHANGE ? "AT_KEYEXCHANGE" : "AT_SIGNATURE");
            }
            else if (i == iContainerNr) {
                strcpy_s(providerName,PROVIDER_BUFFER_SIZE, provName);
                strcpy_s(containerName,CONTAINER_BUFFER_SIZE, contName);
                *pKeySpec = prov_info->dwKeySpec;
                break;
            }

            i++;
      }
      else {
            int err = GetLastError();
            printf("- Error doing CertEnumCertificatesInStore: %s (0x%0x)\n",
               e2str(err), err);
      }
   }

   CertCloseStore(hCertStore, CERT_CLOSE_STORE_CHECK_FLAG);

   if (containerNumber == NULL)
       printf("  done\n");


   return 0;
}
Ejemplo n.º 10
0
int testSign(const char *containerNumber)
{
   char              provName[PROVIDER_BUFFER_SIZE];
   char              contName[CONTAINER_BUFFER_SIZE];
   DWORD             dwKeyUsage;
   HCRYPTPROV        hProv = 0;
   HCRYPTKEY         hKey;
   HCRYPTHASH        hHash;
   BYTE              pbData[200];
   BYTE              sig[512];
   DWORD             siglen;
   DWORD             dwAlgId;
   BYTE              data[500];
   DWORD             dwDataLen;
   long              err;
   int               errors = 0, count = 0;

   printf("\n*** Testing signatures ***\n");

   /* Get the provider name, container name and keyusage (SIGN or KEYEX) */
   if (listMyCerts(containerNumber, provName, contName, &dwKeyUsage))
       return 1;
   if (dwKeyUsage == -1) {
       printf("Container %d not present, exiting\n", atoi(containerNumber));
       return 1;
   }

   /* Acquire the provider handle */
   if(!CryptAcquireContext(&hProv, contName, provName, PROV_RSA_FULL, 0)) {
       err = GetLastError();
       printf("ERR: CryptAcquireContext: %s (0x%0x)\n", e2str(err), err);
       return 1;
   }

   // Done by Office2007
   if (!CryptGetUserKey(hProv, AT_SIGNATURE, &hKey))
   {
       err = GetLastError();
       printf("ERR: CryptUserKey: %s (0x%0x)\n", e2str(err), err);
       errors++;
       goto done;
   }

   // Init hash
   if (!CryptCreateHash(hProv, CALG_SHA1, 0, 0, &hHash)) {
       err = GetLastError();
       printf("ERR: CryptCreateHash: %s (0x%0x)\n", e2str(err), err);
       errors++;
       goto done;
   }

   // Done by Office2007
   dwDataLen = sizeof(dwAlgId);
   if (!CryptGetKeyParam(hKey, KP_ALGID, (BYTE *) &dwAlgId, &dwDataLen, 0)) {
       err = GetLastError();
       printf("ERR: CryptGetKeyParam: %s (0x%0x)\n", e2str(err), err);
       errors++;
       goto done;
   }
   if (dwAlgId != CALG_RSA_KEYX) {
       printf("ERR: CryptGetKeyParam() should return CALG_RSA_KEYX instead of 0x%0x\n", dwAlgId);
       errors++;
   }

   // Done by Office2007
   if (!CryptExportKey(hKey, 0, PUBLICKEYBLOB, 0, NULL, &dwDataLen)) {
       err = GetLastError();
       printf("ERR: CryptExportKey: %s (0x%0x)\n", e2str(err), err);
       errors++;
       goto done;
   }
   if (!CryptExportKey(hKey, 0, PUBLICKEYBLOB, 0, data, &dwDataLen)) {
       err = GetLastError();
       printf("ERR: CryptExportKey: %s (0x%0x)\n", e2str(err), err);
       errors++;
       goto done;
   }

   memset(pbData, 0x31, sizeof(pbData));

   // Hash data -- first part
   if (!CryptHashData(hHash, pbData, 50, 0)) {
       err = GetLastError();
       printf("ERR: CryptHashData(1): %s (0x%0x)\n", e2str(err), err);
       errors++;
       goto done;
   }

   // Hash data -- second part
   if (!CryptHashData(hHash, pbData + 50, sizeof(pbData) - 50, 0)) {
       err = GetLastError();
       printf("ERR: CryptHashData(1): %s (0x%0x)\n", e2str(err), err);
       errors++;
       goto done;
   }

   // Sign hash (get length)
   siglen = 0;
   if (!CryptSignHash(hHash, dwKeyUsage, NULL, 0, NULL, &siglen)) {
       err = GetLastError();
       printf("ERR: CryptSignHash(HP_HASHSIZE): %s (0x%0x)\n", e2str(err), err);
       errors++;
       goto done;
   }

   // Sign hash
   if (!CryptSignHash(hHash, dwKeyUsage, NULL, 0, sig, &siglen)) {
       err = GetLastError();
       printf("ERR: CryptSignHash(): %s (0x%0x)\n", e2str(err), err);
       errors++;
       goto done;
   }

   // Destroy hash
   if (!CryptDestroyHash(hHash)) {
       err = GetLastError();
       printf("ERR: CryptDestroyHash(): %s (0x%0x)\n", e2str(err), err);
       errors++;
   }

   // Done by Office2007
   if (!CryptDestroyKey(hKey)) {
       err = GetLastError();
       printf("ERR: CryptDestroyKey(): %s (0x%0x)\n", e2str(err), err);
       errors++;
   }

done:
   /* Release the provider handle */
   if(!CryptReleaseContext(hProv, 0)) {
       err = GetLastError();
       printf("ERR: CryptReleaseContext(): %s (0x%0x)\n", e2str(err), err);
       errors++;
   }

   printf("Done, %d error(s)\n\n", errors);

   return errors;
}
Ejemplo n.º 11
0
/* Perform sanity checks on arp/rarp packet, return true if ok */
int
sanity_fddi(register struct fddi_header *fh, register struct ether_arp *ea,
    register int len)
{
	u_char *shost = fh->src;
	u_short type, hrd, pro, op;

	/* This rather clunky copy stuff is needed because the fddi header
	 * has an odd (i.e. not even) length, causing memory alignment
	 * errors when attempts are made to access the arp header fields
	 * as shorts */
	BCOPY(fh->snap.snap_type, &type, sizeof(u_short));
	BCOPY(&(ea->arp_hrd), &hrd, sizeof(hrd));
	BCOPY(&(ea->arp_pro), &pro, sizeof(pro));
	BCOPY(&(ea->arp_op), &op, sizeof(op));
	type = ntohs(type);
	hrd = ntohs(hrd);
	pro = ntohs(pro);
	op = ntohs(op);

	if (len < sizeof(*fh) + sizeof(*ea)) {
		syslog(LOG_ERR, "short (want %d)\n", sizeof(*fh) + sizeof(*ea));
		return(0);
	}

	/* XXX sysv r4 seems to use hardware format 6 */
	if (hrd != ARPHRD_ETHER && hrd != 6) {
		syslog(LOG_ERR, "%s sent bad hardware format 0x%x\n",
		    e2str(shost), hrd);
		return(0);
	}


	/* XXX hds X terminals sometimes send trailer arp replies */
	if (pro != ETHERTYPE_IP &&
	    pro != ETHERTYPE_TRAIL &&
	    pro != ETHERTYPE_APOLLO) {
		syslog(LOG_ERR, "%s sent packet not ETHERTYPE_IP (0x%x)\n",
		    e2str(shost), pro);
		return(0);
	}

	if (ea->arp_hln != 6 || ea->arp_pln != 4) {
		syslog(LOG_ERR, "%s sent bad addr len (hard %d, prot %d)\n",
		    e2str(shost), ea->arp_hln, ea->arp_pln);
		return(0);
	}

	/*
	 * We're only interested in arp requests, arp replies
	 * and reverse arp replies
	 */
	if (type == ETHERTYPE_ARP) {
		if (op != ARPOP_REQUEST &&
		    op != ARPOP_REPLY) {
			syslog(LOG_ERR, "%s sent wrong arp op %d\n",
			     e2str(shost), op);
			return(0);
		}
	} else if (type == ETHERTYPE_REVARP) {
		if (op == REVARP_REQUEST) {
			/* no useful information here */
			return(0);
		} else if (op != REVARP_REPLY) {
			if (debug)
				syslog(LOG_ERR, "%s sent wrong revarp op %d\n",
				    e2str(shost), op);
			return(0);
		}
	} else {
		syslog(LOG_ERR, "%s sent bad type 0x%x\n",
		    e2str(shost), type);
		return(0);
	}
	return(1);
}
Ejemplo n.º 12
0
/* Perform sanity checks on an ethernet arp/rarp packet, return true if ok */
int
sanity_ether(register struct ether_header *eh, register struct ether_arp *ea,
    register int len)
{
	/* XXX use bsd style ether_header to avoid messy ifdef's */
	struct bsd_ether_header {
		u_char  ether_dhost[6];
		u_char  ether_shost[6];
		u_short ether_type;
	};
	register u_char *shost = ((struct bsd_ether_header *)eh)->ether_shost;

	eh->ether_type = ntohs(eh->ether_type);
	ea->arp_hrd = ntohs(ea->arp_hrd);
	ea->arp_pro = ntohs(ea->arp_pro);
	ea->arp_op = ntohs(ea->arp_op);

	if (len < sizeof(*eh) + sizeof(*ea)) {
		syslog(LOG_ERR, "short (want %d)\n", sizeof(*eh) + sizeof(*ea));
		return(0);
	}

	/* XXX sysv r4 seems to use hardware format 6 */
	if (ea->arp_hrd != ARPHRD_ETHER && ea->arp_hrd != 6) {
		syslog(LOG_ERR, "%s sent bad hardware format 0x%x\n",
		    e2str(shost), ea->arp_hrd);
		return(0);
	}

	/* XXX hds X terminals sometimes send trailer arp replies */
	if (ea->arp_pro != ETHERTYPE_IP && ea->arp_pro != ETHERTYPE_TRAIL) {
		syslog(LOG_ERR, "%s sent packet not ETHERTYPE_IP (0x%x)\n",
		    e2str(shost), ea->arp_pro);
		return(0);
	}

	if (ea->arp_hln != 6 || ea->arp_pln != 4) {
		syslog(LOG_ERR, "%s sent bad addr len (hard %d, prot %d)\n",
		    e2str(shost), ea->arp_hln, ea->arp_pln);
		return(0);
	}

	/*
	 * We're only interested in arp requests, arp replies
	 * and reverse arp replies
	 */
	if (eh->ether_type == ETHERTYPE_ARP) {
		if (ea->arp_op != ARPOP_REQUEST &&
		    ea->arp_op != ARPOP_REPLY) {
			syslog(LOG_ERR, "%s sent wrong arp op %d\n",
			     e2str(shost), ea->arp_op);
			return(0);
		}
	} else if (eh->ether_type == ETHERTYPE_REVARP) {
		if (ea->arp_op == REVARP_REQUEST) {
			/* no useful information here */
			return(0);
		} else if (ea->arp_op != REVARP_REPLY) {
			if (debug)
				syslog(LOG_ERR, "%s sent wrong revarp op %d\n",
				    e2str(shost), ea->arp_op);
			return(0);
		}
	} else {
		syslog(LOG_ERR, "%s sent bad type 0x%x\n",
		    e2str(shost), eh->ether_type);
		return(0);
	}

	return(1);
}