Hi Chris,
Well that didn't help much. Using the ca-key and ca-cert, I want to generate client keys from code. Then I want to authenticate the client when it tries to connect to the server. I have included the key generation and other required code below.
Here is what I did:
1) Generate CA key and certificate as you have specified
2) Generate Server key and certificate as specified
3) Use the CA key and CA certificate in the below code to generate Client certificate and key
// Key generation
RsaKey genkey;
RNG rng;
int ret;
FILE* fp;
InitRng(&rng);
InitRsaKey(&genkey, 0);
ret = MakeRsaKey(&genkey,1024,65537,&rng);
byte der[4096];
byte pem[4096];
int derSz = RsaKeyToDer(&genkey,der,sizeof(der));
if(derSz < 0)
printf("DER error\n");
int pemSz = DerToPem(der,derSz,pem,sizeof(pem),PRIVATEKEY_TYPE);
if(pemSz < 0)
printf("PEM error\n");
fp = fopen("client-key.pem","w+");
fwrite(pem,1,sizeof(pem),fp);
fclose(fp);
fp = fopen("client-key.der","w+");
fwrite(der,1,sizeof(der),fp);
fclose(fp);
printf("Now time for certificate..\n");
// Certificate Generation and signing by CA certificate
RsaKey caKey;
Cert myCert;
byte derCert[4096];
byte pemCert[4096];
FILE* derFile;
FILE* pemFile;
int certSz;
byte tmp[2048];
size_t bytes;
word32 idx = 0;
FILE* file = fopen("ca-key.der", "rb");
if (!file)
return -412;
bytes = fread(tmp, 1, sizeof(tmp), file);
InitRsaKey(&caKey, 0);
ret = RsaPrivateKeyDecode(tmp, &idx, &caKey, (word32)bytes);
if (ret != 0)
{
printf("Problem decoding private key\n");
return -413;
}
InitCert(&myCert);
strncpy(myCert.subject.country, "US", CTC_NAME_SIZE);
strncpy(myCert.subject.state, "Arizona", CTC_NAME_SIZE);
strncpy(myCert.subject.locality, "Tucson", CTC_NAME_SIZE);
strncpy(myCert.subject.org, "Certificate Authority", CTC_NAME_SIZE);
strncpy(myCert.subject.unit, "UA", CTC_NAME_SIZE);
strncpy(myCert.subject.commonName, "Client", CTC_NAME_SIZE);
strncpy(myCert.subject.email, "ca@provenance.edu", CTC_NAME_SIZE);
myCert.selfSigned = 0;
myCert.daysValid = 30;
ret = SetIssuer(&myCert,"ca-cert.pem");
if(ret < 0)
printf("Problem setting issuer..\n");
certSz = MakeCert(&myCert, derCert, sizeof(derCert), &genkey, &rng);
if (certSz < 0)
{
printf("Problem with Make cert\n");
return -407;
}
certSz = SignCert(&myCert, derCert, sizeof(derCert), &caKey, &rng);
if (certSz < 0)
{
printf("Problem with signing certificate\n");
return -408;
}
int someSz = -1;
someSz = DerToPem(derCert,certSz,pemCert,sizeof(pemCert),CERT_TYPE);
if (someSz < 0)
{
printf("Problem in conversion from DER to PEM\n");
return -409;
}
else
printf("Size is %d\n",someSz);
FILE* certfp;
certfp = fopen("client-cert.pem","w+");
fwrite(pemCert,1,sizeof(pemCert),certfp);
fclose(certfp);
certfp = fopen("client-cert.der","w+");
fwrite(derCert,1,sizeof(derCert),certfp);
fclose(certfp);
4) In EchoClient, I add the following code block before creating socket file descriptor:
if (CyaSSL_CTX_use_certificate_file(ctx,"./client-cert.pem",SSL_FILETYPE_PEM)!= SSL_SUCCESS) {
fprintf(stderr, "Error loading ./clientcert.pem, please check the file.\n");
exit(EXIT_FAILURE);
}
if (CyaSSL_CTX_use_PrivateKey_file(ctx,"./client-key.pem",
SSL_FILETYPE_PEM) != SSL_SUCCESS) {
fprintf(stderr, "Error loading ./clientkey.pem, please check the file.\n");
exit(EXIT_FAILURE);
}
CyaSSL_CTX_set_verify(ctx, SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
5) In EchoServer, I add the following line before the socket file descriptor:
CyaSSL_CTX_set_verify(ctx,SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT,0);
As soon as I type something in echo client and press Enter, I get read error -155.
If I create the client key and client certificate using openssl (i.e., the way we created server key and server certificate), everything works.
So I believe there is something wrong in my key and certificate generation code, but i'm unable to figure it out.
Nitin