We are the students of Computer Engg. We have undertaken a project on Extending PHP 4.0. We plan to add some cryptographic routines to PHP. We understand that, presently, PHP has functions mcrypt() and mhash() to perform trivial cryptographic functions. We also understand that PHP has no functions to create private-public keys, digitally sign documents and create digital certificates.
Presently we have written "C" functions to implement the following:
- Create private-public key pairs
- Create message digests
- Digital Signatures
- Digital Certificates
Our functions use the crypto library (available in Linux as Libcrypto) to actually implement the functions.
We would now like to add this functionality to PHP. We understand that PHP can be extended in 3 ways:
- External modules
- Internal modules
- Zend Engine
We would like to use the first method so that our functions are available by the use of the 'dl()' function.
We would like to know, how to proceed from here onwards. We would be glad if you can provide us with a step-by-step method to do the extension. For this we are attaching a function 'ephp_genkey(...)' code. This function generates a RSA private-public key pair and writes it to 2 files specified in the argument list.
Thanking you in anticipation
Yours Sincerely,
Govind K. Dhume
Saifadam Pathan
Attachment -- The Function Code:
/*------------------------------------------------
* Function Name :
* ephp_genkey
*
* Working :
* This function accepts the keylength and generates
* a RSA key pair. The pair is returned in a RSA structure
* The keys are then stored in 2 files. arg1(Private Key)
* arg2(Public Key)
*------------------------------------------------*/
RSA *ephp_genkey(int keylength,char *privkeyfile, char *pubkeyfile)
{
RSA *rsakey; /* RSA structure */
int status;
BIO *privkey = NULL; /* BIO attached to privkeyfile */
BIO *pubkey = NULL; /* BIO attached to pubkeyfile */
privkey = BIO_new_file(privkeyfile,"w"); /* creates privkeyfile to hold private key */
if(privkey == NULL)
{
printf("\n\n ERROR : while creating '%s'",privkeyfile);
return NULL;
}
pubkey = BIO_new_file(pubkeyfile,"w"); /* creates pubkeyfile to hold public key */
if(pubkey == NULL)
{
printf("\n\n ERROR : while creating '%s'",pubkeyfile);
return NULL;
}
rsakey = RSA_generate_key(keylength,RSA_F4,NULL,NULL);
/* The above function generates a RSA key pair and
* stores it in a rsa structure.
* arg2 (3) -> modulus
* arg3 and arg4 stand for some callback function.
* Here NULL is used as arg3 and arg4 */
if(rsakey == NULL)
{
printf("ERROR : Keys could not be generated");
exit(0);
}
status=PEM_write_bio_RSAPrivateKey(privkey,rsakey,NULL,NULL,0,NULL,NULL);
/* The above function writes the private key to the BIO attached
* to private key file. */
if(status == 0)
printf("ERROR : Writing Private Key");
status = PEM_write_bio_RSA_PUBKEY(pubkey,rsakey);
/* The above function write the public key to the BIO attached
* to the public key file. */
if(status == 0)
printf("ERROR : Writing Public Key");
return rsakey;
}