pith/smkeys.c
/*
 * load_cert_for_key finds a certificate in pathdir that matches a private key
 * pkey. It returns its name in certfile, and the certificate in *pcert.
 * return value: success: different from zero, failure 0. If both certfile
 * and pcert are NULL, this function returns if there is certificate that
 * matches the given key.
 */
load_cert_for_key finds a certificate in pathdir that matches a private key
pkey. It returns its name in certfile, and the certificate in *pcert.
return value: success: different from zero, failure 0. If both certfile
and pcert are NULL, this function returns if there is certificate that
matches the given key.
load_cert_for_key finds a certificate in pathdir that matches a private key pkey . It returns its name in certfile and the certificate in * pcert . return value : success : different from zero failure 0 . If both certfile and pcert are NULL this function returns if there is certificate that matches the given key .
int
load_cert_for_key(char *pathdir, EVP_PKEY *pkey, char **certfile, X509 **pcert)
{
   DIR *dirp;
   struct dirent *d;
   int rv = 0;
   BIO *in;
   X509 *x;
   char buf[MAXPATH+1], pathcert[MAXPATH+1];
   if(pathdir == NULL || pkey == NULL)
    return 0;
   if(certfile) *certfile = NULL;
   if(pcert)    *pcert = NULL;
   if((dirp = opendir(pathdir)) != NULL){
      while(rv == 0 && (d=readdir(dirp)) != NULL){
        size_t ll;
	if((ll=strlen(d->d_name)) && ll > 4){
	   if(!strcmp(d->d_name+ll-4, ".crt")){
	     strncpy(buf, d->d_name, sizeof(buf));
	     buf[sizeof(buf)-1] = '\0';
	     build_path(pathcert, pathdir, buf, sizeof(pathcert));
	     if((in = BIO_new_file(pathcert, "r")) != NULL){
	        if((x = PEM_read_bio_X509(in, NULL, NULL, NULL)) != NULL){
		  if(X509_check_private_key(x, pkey) > 0){
		    rv = 1;
		    if(certfile) *certfile = cpystr(buf);
		    if(pcert)    *pcert = x;
		  }
		  else
		    X509_free(x);
		}
	        BIO_free(in);
	     }
	   }
        }
      }
      closedir(dirp);
   }
   return rv;
}
int load_cert_for_key ( char * pathdir , EVP_PKEY * pkey , char * * certfile , X509 * * pcert ) { DIR * dirp ; struct dirent * d ; int rv = 0 ; BIO * in ; X509 * x ; char buf [ MAXPATH + 1 ] , pathcert [ MAXPATH + 1 ] ; if ( pathdir == NULL || pkey == NULL ) return 0 ; if ( certfile ) * certfile = NULL ; if ( pcert ) * pcert = NULL ; if ( ( dirp = opendir ( pathdir ) ) != NULL ) { while ( rv == 0 && ( d = readdir ( dirp ) ) != NULL ) { size_t ll ; if ( ( ll = strlen ( d -> d_name ) ) && ll > 4 ) { if ( ! strcmp ( d -> d_name + ll - 4 , " " ) ) { strncpy ( buf , d -> d_name , sizeof ( buf ) ) ; buf [ sizeof ( buf ) - 1 ] = ' \0 ' ; build_path ( pathcert , pathdir , buf , sizeof ( pathcert ) ) ; if ( ( in = BIO_new_file ( pathcert , " " ) ) != NULL ) { if ( ( x = PEM_read_bio_X509 ( in , NULL , NULL , NULL ) ) != NULL ) { if ( X509_check_private_key ( x , pkey ) > 0 ) { rv = 1 ; if ( certfile ) * certfile = cpystr ( buf ) ; if ( pcert ) * pcert = x ; } else X509_free ( x ) ; } BIO_free ( in ) ; } } } } closedir ( dirp ) ; } return rv ; }
load_cert_for_key finds a certificate in pathdir that matches a private key
 pkey.
load_cert_for_key finds a certificate in pathdir that matches a private key pkey .