📄 main.cpp
字号:
/************************************************************************//* RA Main *//* CR *//* 01-25-2005 *//************************************************************************/#include <stdio.h>#include <stdlib.h>#include <string.h>#include <iostream>#include <sys/stat.h>#include <sys/types.h>#include "RAService.h"//#include "Communicate.h"#define SUCCESS 0void help(void){ cout << "Usage:" << endl; cout << " ra -help" << endl; cout << " ra -listen" << endl; cout << " ra -req" << endl;} int GenCertReq(CertReqInfo *certInfo, char **outb, int *outlen);//int handleRAMsg(CONTENT rmsg, CONTENT *smsg);string parseXML(string xml, string type, string keyword, string value);string locateInXML(NggObject *object, string type, string keyword, string value);/* * */int main(int argc, char *argv[]){ bool listen = false; // CONTENT rmsg, rmsg2, smsg; int ret = 0;/* Communicate *comm = new Communicate(); if(comm->m_flag == -1) // Init fail { cout << "Init fail!" << endl; return -1; }*/ CertReqInfo *certInfo; certInfo = (CertReqInfo *)malloc(sizeof(CertReqInfo)); certInfo->CountryName = NULL; certInfo->StateName = NULL; certInfo->CityName = NULL; certInfo->OrganizationName = NULL; certInfo->UnitName = NULL; certInfo->CommonName = NULL; certInfo->EmailAdd = NULL; certInfo->PKey = NULL; certInfo->notBefore = 0; certInfo->notAfter = 0; if(argc == 1) { help(); } else if(!strcmp(argv[1], "-help")) { help(); } else if(!strcmp(argv[1], "-listen")) { listen = true; } else if(!strcmp(argv[1], "-req")) // Generate certificate request by manual { bool over = false; while(!over) { char buf[64]; int inlen = 0; cout << "Please input Common Name:" << endl; gets(buf); inlen = strlen(buf); if(inlen > 0) { certInfo->CommonName = (char *)malloc(inlen); strcpy(certInfo->CommonName, buf); } cout << "Please input Organization Name:" << endl; gets(buf); if(inlen > 0) { certInfo->OrganizationName = (char *)malloc(inlen); strcpy(certInfo->OrganizationName, buf); } cout << "Please input Organization Unit Name:" << endl; gets(buf); if(inlen > 0) { certInfo->UnitName = (char *)malloc(inlen); strcpy(certInfo->UnitName, buf); } over = true; } int outlen = 1; GenCertReq(certInfo, NULL, &outlen); if(certInfo->CommonName != NULL) { free(certInfo->CommonName); certInfo->CommonName = NULL; } if(certInfo->OrganizationName != NULL) { free(certInfo->OrganizationName); certInfo->OrganizationName = NULL; } if(certInfo->UnitName != NULL) { free(certInfo->UnitName); certInfo->UnitName = NULL; } } while (listen) {/* // Get message from UM GUI rmsg = comm->raGetMessageFromADT(); cout << "========== Message from ADT begin ==========" << endl; cout << (char *)rmsg.buf << endl; cout << "========== Message from ADT end ==========" << endl << endl; // Handle received message ret = handleRAMsg(rmsg, &smsg); if(ret != SUCCESS) { printf("=== handleRAMsg fail! ===\n"); continue; } // Send message to CA comm->raSendMessageToCA(smsg); cout << "++++++++++ Message send to CA begin ++++++++++" << endl; cout << (char *)smsg.buf << endl; cout << "++++++++++ Message send to CA end ++++++++++" << endl << endl; // Get message from CA rmsg2 = comm->raGetMessageFromCA(); cout << "########## Message get from CA begin ##########" << endl; cout << (char *)rmsg2.buf << endl; cout << "########## Message get from CA end ##########" << endl << endl; // Send message to UM GUI comm->raSendMessageToADT(rmsg2); rmsg.type = 0; smsg.type = 0; memset(rmsg.buf, 0x0, SIZE); memset(smsg.buf, 0x0, SIZE);*/ sleep(1); }// delete comm; free(certInfo); return 1;} /*=================================================================== * Desc: * Auth: CR * Date: 01-25-2005 * ==================================================================*/int GenCertReq(CertReqInfo *certInfo, char **outb, int *outlen){ RAService *raserv; int ret = 0; RSA *rsa = NULL; X509_REQ *req = NULL; EVP_PKEY *pkey = NULL; long version = 0L; char comname[64]; char orgname[64]; char untname[64]; BIO* bio; bio = BIO_new(BIO_s_mem()); req = X509_REQ_new(); pkey = EVP_PKEY_new(); raserv = new RAService(); certInfo->CountryName = "CN"; // Must be like "CN", "US", etc. int bits = 1024; unsigned long e = 65537; rsa = RSA_generate_key(bits, e, NULL, NULL); if(rsa == NULL) { printf("generate rsa fail!\n"); return -1; } EVP_PKEY_assign_RSA(pkey, rsa);/* BIO_write(bio, certInfo->PKey, strlen(certInfo->PKey)); pkey = PEM_read_bio_PUBKEY(bio, NULL, NULL, NULL); if(pkey == NULL) { printf("PEM_read_bio_PUBKEY fail!\n"); return -1; }*/ ret = raserv->GenerateCertReq(req, certInfo, pkey); if(ret != 0) { printf("Generate certificate request fail!\n"); return -1; } FILE *fp1, *fp2, *fp3; char reqfile[32]; char pvkfile[32]; char pubfile[32]; memset(reqfile, 0x0, 32); memset(pvkfile, 0x0, 32); memset(pubfile, 0x0, 32); strcpy(reqfile, certInfo->CommonName); strcat(reqfile, "_req.pem\0"); strcpy(pvkfile, certInfo->CommonName); strcat(pvkfile, "_pvk.pem\0"); strcpy(pubfile, certInfo->CommonName); strcat(pubfile, "_pub.pem\0"); fp1 = fopen(reqfile, "w+"); if(fp1 == NULL) { printf("Open request file fail!\n"); return -1; } fp2 = fopen(pvkfile, "w"); if(fp2 == NULL) { printf("Open private key file fail!\n"); return -1; } fp3 = fopen(pubfile, "w"); if(fp3 == NULL) { printf("Open public key file fail!\n"); return -1; } PEM_write_X509_REQ(fp1, req); fclose(fp1);// CR [1/24/2005] struct stat fst; stat(reqfile, &fst); if(*outlen == 0) // To mem { if(fst.st_size > 0) { *outlen = fst.st_size; *outb = (char *)malloc(*outlen); memset(*outb, 0x0, *outlen); fp1 = fopen(reqfile, "r"); if(fp1 == NULL) { printf("Open request file fail!\n"); return -1; } fread(*outb, 1, *outlen - 1, fp1); fclose(fp1); } } else if(*outlen == 1) // To file { *outlen = 0; } // ret = PEM_write_PKCS8PrivateKey(fp2, pkey, NULL, NULL, 0, NULL, NULL); ret = PEM_write_PrivateKey(fp2, pkey, NULL, NULL, 0, NULL, NULL); unsigned char *pubbuf; int publen = 0; publen = i2d_PublicKey(pkey, NULL); pubbuf = (unsigned char *)malloc(publen); publen = i2d_PublicKey(pkey, &pubbuf); fwrite(pubbuf, 1, publen, fp3); free(pubbuf); fclose(fp2); fclose(fp3); EVP_PKEY_free(pkey); X509_REQ_free(req); return 0;}/*=================================================================== * Desc: * Auth: CR * Date: 01-25-2005 * ==================================================================*//*int handleRAMsg(CONTENT rmsg, CONTENT *smsg){ RAService *raserv = NULL; CertReqInfo *certInfo = NULL; char *outb = NULL; int outl = 0; int ret = 0; char *tmpbuf; int tmplen; int action_type; string parameter = rmsg.buf; string retparam = ""; string retval; action_type = rmsg.type; certInfo = (CertReqInfo *)malloc(sizeof(CertReqInfo)); certInfo->CountryName = NULL; certInfo->StateName = NULL; certInfo->CityName = NULL; certInfo->OrganizationName = NULL; certInfo->UnitName = NULL; certInfo->CommonName = NULL; certInfo->EmailAdd = NULL; certInfo->notBefore = 0; certInfo->notAfter = 0; raserv = new RAService(); if(raserv == NULL) { ret = -1; goto err; } switch(action_type) { case APPLY_CERT: //User certificate case APPLY_GW_CERT: //Gateway certificate { retval = parseXML(parameter, "get", "CommonName", ""); if(retval.length() == 0) { printf("=== handleRAMsg: Get CommonName from XML fail! ===\n"); ret = -1; goto err; } certInfo->CommonName = (char *)malloc(retval.length() + 1); memset(certInfo->CommonName, 0x0, retval.length() + 1); strncpy(certInfo->CommonName, retval.c_str(), retval.length()); retval = parseXML(parameter, "get", "OrganizationName", ""); if(retval.length() == 0) { printf("=== handleRAMsg: Get OrganizationName from XML fail! ===\n"); ret = -1; goto err; } certInfo->OrganizationName = (char *)malloc(retval.length()+ 1); memset(certInfo->OrganizationName, 0x0, retval.length() + 1); strncpy(certInfo->OrganizationName, retval.c_str(), retval.length()); retval = parseXML(parameter, "get", "OrganizationUnitName", ""); if(retval.length() == 0) { printf("=== handleRAMsg: Get OrganizationUnitName from XML fail! ===\n"); ret = -1; goto err; } certInfo->UnitName = (char *)malloc(retval.length() + 1); memset(certInfo->UnitName, 0x0, retval.length() + 1); strncpy(certInfo->UnitName, retval.c_str(), retval.length()); // Add by CR begin - 03.09.2005 retval = parseXML(parameter, "get", "PKey", ""); if(retval.length() == 0) { printf("=== handleRAMsg: Get PKey from XML fail! ===\n"); ret = -1; goto err; } certInfo->PKey = (char *)malloc(retval.length() + 1); memset(certInfo->PKey, 0x0, retval.length() + 1); strncpy(certInfo->PKey, retval.c_str(), retval.length()); // Add by CR end - 03.09.2005 ret = GenCertReq(certInfo, &outb, &outl); if(ret != 0) { printf("=== handleRAMsg: Generate certificate request fail! ==="); goto err; } smsg->type = action_type; retparam = parseXML(parameter, "set", "CertReq", outb); tmplen = retparam.length(); tmpbuf = (char *)malloc(tmplen); memcpy(tmpbuf, retparam.c_str(), tmplen); memcpy(smsg->buf, tmpbuf, tmplen); free(tmpbuf); } break; case UPDATE_CERT: // Update certificate case REVOKE_CERT: // Revoke certificate case REMOVE_FROM_CRL: case RECOVER_CERT:// case VERIFY_CERT: case IMPORT_THIRD_CRL: case EXPORT_CRL: { smsg->type = action_type; memcpy(smsg->buf, rmsg.buf, K); } break; default: ret = -1; break; }err: if(certInfo->CommonName != NULL) { free(certInfo->CommonName); certInfo->CommonName = NULL; } if(certInfo->OrganizationName != NULL) { free(certInfo->OrganizationName); certInfo->OrganizationName = NULL; } if(certInfo->UnitName != NULL) { free(certInfo->UnitName); certInfo->UnitName = NULL; } if(certInfo->CommonName != NULL) free(certInfo->CommonName); if(certInfo->OrganizationName != NULL) free(certInfo->OrganizationName); if(certInfo->UnitName != NULL) free(certInfo->UnitName); if(certInfo != NULL) free(certInfo); if(raserv != NULL) { delete raserv; raserv = NULL; } return ret;}*//*=================================================================== * Desc: * Auth: CR * Date: 01-25-2005 * ==================================================================*/string parseXML(string xml, string type, string keyword, string value){ NggObject *object = NggObject::fromXML(xml); string result; if(type == "get") result = locateInXML(object, type, keyword, value); else if(type == "set") { locateInXML(object, type, keyword, value); result = object->toXML(); } if (object) { delete object; object = NULL; } return result;}/*=================================================================== * Desc: * Auth: CR * Date: 01-25-2005 * ==================================================================*/string locateInXML(NggObject *object, string type, string keyword, string value){// assert(object != NULL); string result = ""; static bool found = false; int i = 0; unsigned int len = object->m_Contains.size(); do { if(len > 0) { NggObject *obj = object->m_Contains[i]; if ((result = locateInXML(obj, type, keyword, value)) != "") { found = true; break; } else i++; } else { string name = object->m_Name; if(name == keyword) { if(type == "get") { result = object->m_Value; cout << result << endl; } else if(type == "set") { object->m_Value = value; result = object->toXML(); } found = true; break; } else { result = ""; } } } while(i < len && !found); found = false; return result;}/* End of file */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -