📄 getserial.c
字号:
/* <LIC_AMD_STD> * Copyright (C) 2003-2005 Advanced Micro Devices, Inc. All Rights Reserved. * * Unless otherwise designated in writing, this software and any related * documentation are the confidential proprietary information of AMD. * THESE MATERIALS ARE PROVIDED "AS IS" WITHOUT ANY * UNLESS OTHERWISE NOTED IN WRITING, EXPRESS OR IMPLIED WARRANTY OF ANY * KIND, INCLUDING BUT NOT LIMITED TO WARRANTIES OF MERCHANTABILITY, * NONINFRINGEMENT, TITLE, FITNESS FOR ANY PARTICULAR PURPOSE AND IN NO * EVENT SHALL AMD OR ITS LICENSORS BE LIABLE FOR ANY DAMAGES WHATSOEVER. * * AMD does not assume any responsibility for any errors which may appear * in the Materials nor any responsibility to support or update the * Materials. AMD retains the right to modify the Materials at any time, * without notice, and is not obligated to provide such modified * Materials to you. AMD is not obligated to furnish, support, or make * any further information available to you. * </LIC_AMD_STD> *//* <CTL_AMD_STD> * </CTL_AMD_STD> *//* <DOC_AMD_STD> * </DOC_AMD_STD> */#include <stdio.h>#include <stdlib.h>#include <sys/types.h>#include <sys/stat.h>#include <fcntl.h>#include <unistd.h>#include <string.h>#include <time.h>#include <sys/socket.h>#include <netinet/in.h>#include <arpa/inet.h>#include <netdb.h>#include <sys/poll.h>#include "linux/nvram.h"#define SEC_NEEDKEYS#include "mae_interface.h"#include "secure_serial.h"#include "secure_serial_priv.h"#include "base64.h"static void rot13 (char *str){ register char cap; int i; for(i=0;i<strlen(str);i++) { cap = str[i] & 32; str[i] &= ~cap; str[i] = ((str[i] >= 'A') && (str[i] <= 'Z') ? ((str[i] - 'A' + 13) % 26 + 'A') : str[i]) | cap; }}// This returns a long which should be the same as returned by the// gethostid system call.long getMyHostid(char *serialNum){ SHA1_CTX context; u_int8_t digest[SHA1_DIGEST_LENGTH]; long *hostid = (long *)&digest[12];; SHA1Init(&context); SHA1Update(&context, serialNum, strlen(serialNum)); SHA1Final(digest,&context); return(*hostid); }char *getSerialNum(int *errorCode){ int sock, length, n; struct sockaddr_in server, from; struct hostent *hp; int i; struct pollfd fds; int pollrc; int retryCount; static netMsg sMsg; char eserialNum[SER_BUF_SIZE]; char *hash; long hid1, hid2; char localhost[] = "ybpnyubfg"; *errorCode = SERIAL_STATUS_OK; sock= socket(AF_INET, SOCK_DGRAM, 0); if (sock < 0) { *errorCode = SERIAL_SOCKET_ERROR; return(NULL); } server.sin_family = AF_INET; rot13(localhost); hp = gethostbyname(localhost); if (hp==0) { *errorCode = SERIAL_HOSTNAME_ERROR; return(NULL); } bcopy((char *)hp->h_addr,(char *)&server.sin_addr,hp->h_length); server.sin_port = htons(SEC_PORT); length=sizeof(struct sockaddr_in); bzero(eserialNum,SER_BUF_SIZE); // throw in random bits to hide command srand(time(NULL)); for(i=0;i<COMMAND_SIZE;i++) { eserialNum[i] = rand() & 0xff; } // set the command to send to sec server eserialNum[COMMAND_OFFSET]=GET_SERIAL_NUM; retryCount = 0; do { // make sure it doesn't loop forevery if (retryCount > RETRY_COUNT) { *errorCode = SERIAL_TOO_MANY_RETRY; return(NULL); } retryCount++; n=sendto(sock,eserialNum,COMMAND_SIZE,0, (const struct sockaddr *)&server,length); if (n < 0) { *errorCode = SERIAL_SOCKET_ERROR; close(sock); return(NULL); } usleep(10000); // sleep for 0.1 secs fds.fd=sock; fds.events |= POLLIN; pollrc=poll(&fds,1,1000); } while (!(fds.revents & POLLIN)); n = recvfrom(sock,&sMsg,sizeof(sMsg),0,(struct sockaddr *)&from, &length); if (n < 0) { *errorCode = SERIAL_SOCKET_ERROR; close(sock); return(NULL); } close(sock); hash = hashSerial(sMsg.serialNum,keyArray); if (hash == NULL) { *errorCode = SERIAL_HASH_NOT_FOUND; return(NULL); } if (strcmp(hash,sMsg.sig)) { *errorCode = SERIAL_HASH_MISMATCH; free(hash); return(NULL); } free(hash); hid1 = gethostid(); hid2 = getMyHostid(sMsg.serialNum); if (hid1 != hid2) { *errorCode = SERIAL_HOSTID_MISMATCH; return(NULL); } return(sMsg.serialNum); }char * hashSerial(char *serialNum,unsigned char keys[KEY_SIZE]){ serialMsg msg; SHA1_CTX context; u_int8_t digest[SHA1_DIGEST_LENGTH]; unsigned char *hash=NULL; int i; int rc; memset(&msg,0,sizeof(msg)); SHA1Init(&context); SHA1Update(&context, serialNum, strlen(serialNum)); SHA1Final(digest,&context); // this makes the key unique to a serial number for(i=0;i<SHA1_DIGEST_LENGTH;i++) { msg.frontKey[i] = keyArray[i] ^ digest[i%SHA1_DIGEST_LENGTH]; msg.endKey[i] = msg.frontKey[i]; } strncpy(msg.serialNum,serialNum,sizeof(msg.serialNum)); SHA1Init(&context); SHA1Update(&context, (u_int8_t *)&msg, sizeof(msg)); SHA1Final(digest,&context); rc = base64_encode(digest,sizeof(digest),&hash); if (rc < 0) return(NULL); return(hash); }int getDivxKey(u_int8_t **key, int *errorCode){ static u_int8_t divxKey[SHA1_DIGEST_LENGTH]; int sock, length, n; struct sockaddr_in server, from; struct hostent *hp; int i; static int found=0; char eserialNum[COMMAND_SIZE]; char localhost[] = "ybpnyubfg"; struct pollfd fds; int pollrc; int retryCount; *errorCode = SERIAL_STATUS_OK; (*key) = divxKey; if (found) return(0); sock= socket(AF_INET, SOCK_DGRAM, 0); if (sock < 0) { *errorCode = SERIAL_SOCKET_ERROR; return(-1); } server.sin_family = AF_INET; rot13(localhost); hp = gethostbyname(localhost); if (hp==0) { *errorCode = SERIAL_HOSTNAME_ERROR; return(-1); } bcopy((char *)hp->h_addr,(char *)&server.sin_addr,hp->h_length); server.sin_port = htons(SEC_PORT); length=sizeof(struct sockaddr_in); bzero(divxKey,SHA1_DIGEST_LENGTH); divxKey[0] = 'A'; // throw in random bits to hide command srand(time(NULL)); for(i=0;i<COMMAND_SIZE;i++) { eserialNum[i] = rand() & 0xff; } // set the command to send to sec server eserialNum[COMMAND_OFFSET]=GET_DIVX_KEY; retryCount = 0; do { // make sure it doesn't loop forevery if (retryCount > RETRY_COUNT) { *errorCode = SERIAL_TOO_MANY_RETRY; return(-1); } retryCount++; n=sendto(sock,eserialNum,COMMAND_SIZE,0,(const struct sockaddr *)&server,length); if (n < 0) { *errorCode = SERIAL_SOCKET_ERROR; close(sock); return(-1); } usleep(10000); // sleep for 0.1 secs fds.fd=sock; fds.events |= POLLIN; pollrc=poll(&fds,1,1000); } while (!(fds.revents & POLLIN)); if (pollrc < 0) { *errorCode = SERIAL_SOCKET_ERROR; return(-1); } n = recvfrom(sock,divxKey,SHA1_DIGEST_LENGTH,0,(struct sockaddr *)&from, &length); if (n < 0) { *errorCode = SERIAL_SOCKET_ERROR; close(sock); return(-1); } found=1; close(sock); return(0); }static char *getDeviceStr(char requestType,int *errorCode){ static u_int8_t retStr[STRING_MAX_LEN]; int sock, length, n; struct sockaddr_in server, from; struct hostent *hp; int i; static int found=0; char eserialNum[COMMAND_SIZE]; char localhost[] = "ybpnyubfg"; struct pollfd fds; int pollrc; int retryCount; memset(retStr,0,STRING_MAX_LEN); *errorCode = SERIAL_STATUS_OK; sock= socket(AF_INET, SOCK_DGRAM, 0); if (sock < 0) { *errorCode = SERIAL_SOCKET_ERROR; return(NULL); } server.sin_family = AF_INET; rot13(localhost); hp = gethostbyname(localhost); if (hp==0) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -