dear all
i have made a client server. the server acts as CA the client sends a
certificate request and the CA reply with a certificate
my problem is i got an error with memory issue .also in order to
troubleshoot this program i have put the CA self signed certificate in a
file and also the certificate of the client in a pem file also i change the
client certificate into an int and this make something unreadable
thx allot for help
-- 
Warmest regards and best wishes for a good health,*urs sincerely *
*mero*
//============================================================================
// 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()
{
	clock_t start, end;
	double msecs;

	start = clock();

	Client clientest;
    Server servertest;

    X509 *cert;
    cert = servertest.CreateCertificate(clientest.MakeSignedCertReq(1,20,90));

    clientest.SetCert(cert);
    clientest.CertConverter();

	end = clock();
	msecs = ((double) (end - start)) * 1000 / CLOCKS_PER_SEC;
	cout<<"time is "<<msecs<<"msec"<<endl;
	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)
 {
	 SetPublicKey();
	 //include the public key in the req
	 X509_REQ_set_pubkey(m_myCertReq,m_puk);
	 //set the subject name of the request
	 m_name=X509_REQ_get_subject_name(m_myCertReq);
	 //set the request
	 X509_NAME_add_entry_by_txt(m_name,"C",MBSTRING_ASC, (const unsigned char *)"UK", -1, -1, 0);
	 X509_NAME_add_entry_by_txt(m_name,"CN",MBSTRING_ASC, (const unsigned char *)"OpenSSL Group", -1, -1, 0);
	 //sign the req
	 X509_REQ_sign(m_myCertReq,m_puk,EVP_sha1());
	 return m_myCertReq;
}

 void
 Client::SetCert(X509 *cert)
 {
	 cout << "writing certificate\n";
	 BIO *out = NULL;
	 const char szPath[10] = "x509.pem";
	 out = BIO_new_file(szPath,"wb");
	 m_myCert =  cert;

	 int len;
	 unsigned char *buf, *p;

	 len = i2d_X509(cert, NULL);

	 cout << "cert length =" << len << endl;
     buf = (unsigned char *)OPENSSL_malloc(len);
     p = buf;
     i2d_X509(cert, &p);

     cout << "cert=";
     for(int i=0; i<len; i++)
    	 cout << buf[i];

     cout << endl;

	 if(!PEM_write_bio_X509 (out , cert))
		 cout << "error writing certificate\n";
}

 int
 Client::CertConverter()
 {
	 int len = i2d_X509(m_myCert, NULL);
	 unsigned char *buf, *p;
	 buf = (unsigned char *)OPENSSL_malloc(len);
	 p = buf;
	 i2d_X509(m_myCert, &p);
	 unsigned char certarray[len];
	 for (int i = 0 ; i<len ; i++)
	 {
		 certarray[i] = *(p-len+i);
	 }
	 cout << "client cert is";
	 for (int j = 0 ; j<len ; j++)
	 {
		 cout << certarray[j]<<endl;
	 }
	 return 0;
 }
/*
 * 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);

   int CertConverter ();

   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();
	  m_issuerName = X509_NAME_new();
	  GenerateMyKeyPairs();
	  CreateMyCertificate();
	  //SetPublicKey();
 }

 Server::~Server()
 {
	  X509_free(m_myCert);
	  RSA_free(m_caKeyPairs);
	  X509_NAME_free(m_issuerName);
 }

 X509*
 Server::CreateCertificate(X509_REQ* req)
 {
	 cout<<"hello i began"<<endl;
	 X509 *m_req_reply;
	 m_req_reply = X509_new();

	 {int len = i2d_X509(m_req_reply, NULL);
	 cout << "cert length =" << len << endl;}

	 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);

	 cout << "cert subject name:" << X509_get_subject_name(m_req_reply) << endl;

	 {int len = i2d_X509(m_req_reply, NULL);
	 cout << "cert length =" << len << endl;}

	 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);

	 {int len = i2d_X509(m_req_reply, NULL);
	 cout << "cert length =" << len << endl;}


	 X509_sign(m_req_reply, m_pukey, EVP_sha1());

	 {int len = i2d_X509(m_req_reply, NULL);
	 cout << "cert length =" << len << endl;}

	 return m_req_reply;
 }

 void
 Server::CreateMyCertificate()
 {
/*
	  if (m_issuerName == NULL) cout << "error in name\n";

	  if (!X509_NAME_add_entry_by_txt(m_issuerName, "C", MBSTRING_ASC,
	                         (const unsigned char *)"UK", -1, -1, 0))
		  cout << "error in name\n";
	  if (!X509_NAME_add_entry_by_txt(m_issuerName, "O", MBSTRING_ASC,
			  (const unsigned char *)"Disorganized Organization", -1, -1, 0))
		  cout << "error in name\n";
	  if (!X509_NAME_add_entry_by_txt(m_issuerName, "CN", MBSTRING_ASC,
			  (const unsigned char *)"Joe Bloggs", -1, -1, 0))
		  cout << "error in name\n";
*/
     // we use rsa pairs and assign it into evp_key
	 SetPublicKey();
	 // properties of the certificate
	 //set the serial number
	 ASN1_INTEGER_set(X509_get_serialNumber(m_myCert), 1);
	 //set the time validity
	 X509_gmtime_adj(X509_get_notBefore(m_myCert), 0);
	 X509_gmtime_adj(X509_get_notAfter(m_myCert), 31536000L);
	 //set the public key of the cert to be signed
	 X509_set_pubkey(m_myCert, m_pukey);
     //this is a self-signed certificate, we set the name of the issuer to the name of the subject
	 m_issuerName = X509_get_subject_name(m_myCert);

	 X509_NAME_add_entry_by_txt(m_issuerName, "C",  MBSTRING_ASC,
	                            (unsigned char *)"CA", -1, -1, 0);
	 X509_NAME_add_entry_by_txt(m_issuerName, "O",  MBSTRING_ASC,
	                            (unsigned char *)"MyCompany Inc.", -1, -1, 0);
	 X509_NAME_add_entry_by_txt(m_issuerName, "CN", MBSTRING_ASC,
	                            (unsigned char *)"localhost", -1, -1, 0);

     //set the issuer name
	 X509_set_issuer_name(m_myCert, m_issuerName);
	 //sign the cert
	 X509_sign(m_myCert, m_pukey, EVP_sha1());

	 //print the self signed certificate
	 FILE * fkey;
	 fkey = fopen("key.pem", "wb");
	 PEM_write_PrivateKey(
	     fkey,                  /* write the key to the file we've opened */
	     m_pukey,               /* our key from earlier */
	     EVP_des_ede3_cbc(), /* default cipher for encrypting the key on disk */
	     (unsigned char *)"replace_me",       /* passphrase required for decrypting the key on disk */
	     10,                 /* length of the passphrase string */
	     NULL,               /* callback for requesting a password */
	     NULL                /* data to pass to the callback */
	 );
	 FILE * fcert;
	 fcert = fopen("cert.pem", "wb");
	 PEM_write_X509(
	     fcert,   /* write the certificate to the file we've opened */
	     m_myCert /* our certificate */
	 );
 }

 void
 Server::GenerateMyKeyPairs()
 {
	 m_caKeyPairs = RSA_generate_key(2048,RSA_F4 , NULL , NULL);
}

 void
 Server::SetPublicKey()
 {
	 EVP_PKEY_assign_RSA(m_pukey,m_caKeyPairs);
}

 int
 Server::VerifyCert(X509* clientcert)
 {
	 CheckCert (m_myCert,clientcert);
	 return 0;
 }

 int
 Server::CheckCert(X509* servercert, X509* clientcert)
 {
	 int i = 0;
	// i = X509_verify_cert(
	 return 0;
 }
/*
 * server.cc
 *
 *  Created on: Sep 17, 2014
 *      Author: amirale32
 */




/*
 * 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  "client.h"
#include <openssl/x509.h>

using namespace std;
 class Server
 {
 public:

	 Server();
	 ~Server();

	 X509 *CreateCertificate (X509_REQ *req);
	 void CreateMyCertificate();

	 void GenerateMyKeyPairs ( );
	 void SetPublicKey ();

	 int VerifyCert (X509 *clientcert);
	 int CheckCert (X509 *servercert , X509 *clientcert);

 private:

	 X509       *m_myCert;
	 RSA        *m_caKeyPairs;
	 EVP_PKEY   *m_pukey;
	 X509_NAME  *m_issuerName;
 };



#endif /* SERVER_H_ */

Reply via email to