dear all hope all are well. i have made a client server code the server is the certificate authority and the client send a certificate request the server got the request and reply with a certificate i have tried to put the certificate in a file in a pem format in order to make sure the certificate has been created but nothing created. and there are no compilation error
please just try the code i can't see anything wrong with it please help 2- if i want to send the generated certificate to another client and make a verification code for that how can i made something like that i have searched in crypto https://www.openssl.org/docs/crypto/x509.html# but i couldn't find any function to do that thx allot for help -- Warmest regards and best wishes for a good health,*urs sincerely * *mero*
/* * server.h * * Created on: Sep 17, 2014 * Author: amirale32 */ #ifndef SERVER_H_ #define SERVER_H_ #include <stdlib.h> #include <iostream> #include <stdio.h> #include "openssl/asn1.h" #include "openssl/ssl.h" #include "openssl/rsa.h" #include "openssl/conf.h" #include "openssl/x509.h" #include "client.h" using namespace std; class Server { public: Server(); ~Server(); X509 *CreateCertificate (X509_REQ *req); void CreateMyCertificate(); void GenerateMyKeyPairs ( ); void SetPublicKey (); private: X509 *m_myCert; RSA *m_caKeyPairs; EVP_PKEY *m_pukey; //Client *m_client; }; #endif /* SERVER_H_ */
//============================================================================ // Name : certificate.cpp // Author : Amir // Version : // Copyright : Your copyright notice // Description : Hello World in C++, Ansi-style //============================================================================ #include <iostream> #include "server.h" #include "client.h" using namespace std; int main() { Client clientest; Server servertest; X509 *cert; cert = servertest.CreateCertificate(clientest.MakeSignedCertReq(1,20,90)); clientest.SetCert(cert); return 0; }
/* * client.cc * * Created on: Sep 17, 2014 * Author: amirale32 */ #include "client.h" Client :: Client() { m_myCertReq = X509_REQ_new(); m_myCert = X509_new(); m_name = X509_NAME_new(); m_rsa_keyPair = RSA_new(); m_puk = EVP_PKEY_new(); GenerateRSAKeyPair(); SetPublicKey(); } Client :: ~Client() { X509_REQ_free(m_myCertReq); X509_free(m_myCert); X509_NAME_free(m_name); RSA_free(m_rsa_keyPair); EVP_PKEY_free(m_puk); } void Client :: GenerateRSAKeyPair ( ) { m_rsa_keyPair = RSA_generate_key(2048,RSA_F4,NULL,NULL); } void Client::SetPublicKey() { EVP_PKEY_assign_RSA(m_puk,m_rsa_keyPair); } X509_REQ* Client::MakeSignedCertReq(int bits, int serial, int days) { X509_REQ_set_pubkey(m_myCertReq,m_puk); m_name=X509_REQ_get_subject_name(m_myCertReq); //X509_NAME_add_entry_by_txt(name,"C",MBSTRING_ASC, "UK", -1, -1, 0); //X509_NAME_add_entry_by_txt(name,"CN",MBSTRING_ASC, "OpenSSL Group", -1, -1, 0); X509_REQ_sign(m_myCertReq,m_puk,EVP_md5()); return m_myCertReq; } void Client::SetCert(X509 *cert) { FILE *out = NULL; m_myCert = cert; PEM_write_X509 (out , m_myCert); }
/* * client.h * * Created on: Sep 17, 2014 * Author: amirale32 */ #ifndef CLIENT_H_ #define CLIENT_H_ #include <stdlib.h> #include <stdio.h> #include "openssl/rsa.h" #include "openssl/conf.h" #include "openssl/x509.h" #include "openssl/pem.h" #include "server.h" class Client { public: Client(); ~Client(); void GenerateRSAKeyPair (); void SetPublicKey (); X509_REQ *MakeSignedCertReq(int bits, int serial, int days); void SetCert (X509 *cert); private: X509_REQ *m_myCertReq; X509 *m_myCert; X509_NAME *m_name; RSA *m_rsa_keyPair; EVP_PKEY *m_puk; }; #endif /* CLIENT_H_ */
#include "server.h" Server::Server() { m_myCert = X509_new(); m_caKeyPairs = RSA_new(); m_pukey = EVP_PKEY_new(); GenerateMyKeyPairs(); CreateMyCertificate(); //SetPublicKey(); } Server::~Server() { X509_free(m_myCert); RSA_free(m_caKeyPairs); } X509* Server::CreateCertificate(X509_REQ* req) { cout<<"hello i began"; X509 *m_req_reply; m_req_reply = X509_new(); X509_NAME *subject = NULL; EVP_PKEY *pkey = NULL; X509_NAME *issuerSubject = X509_get_subject_name(m_myCert); X509_set_issuer_name(m_req_reply, issuerSubject); //xn_req = X509_REQ_get_subject_name(req); X509_set_subject_name(m_req_reply, subject); pkey = X509_REQ_get_pubkey(req); //rv = X509_set_pubkey(reqreply, pkey); X509_gmtime_adj(X509_get_notBefore(m_req_reply), 0); X509_gmtime_adj(X509_get_notAfter(m_req_reply), 36400); X509_sign(m_req_reply, pkey, EVP_md5()); return m_req_reply; } void Server::CreateMyCertificate() { EVP_PKEY_assign_RSA(m_pukey, m_caKeyPairs); ASN1_INTEGER_set(X509_get_serialNumber(m_myCert), 1); X509_gmtime_adj(X509_get_notBefore(m_myCert), 0); X509_gmtime_adj(X509_get_notAfter(m_myCert), 31536000L); X509_set_pubkey(m_myCert, m_pukey); X509_NAME * name; name = X509_get_subject_name(m_myCert); X509_set_issuer_name(m_myCert, name); X509_sign(m_myCert, m_pukey, EVP_md5()); } void Server::GenerateMyKeyPairs() { m_caKeyPairs = RSA_generate_key(2048,RSA_F4 , NULL , NULL); } void Server::SetPublicKey() { EVP_PKEY_assign_RSA(m_pukey,m_caKeyPairs); } /* * server.cc * * Created on: Sep 17, 2014 * Author: amirale32 */