Ejemplo n.º 1
0
void CA_LIST_ENTRY_free ( CA_LIST_ENTRY *ca ) {

	if ( !ca ) return;

	if ( ca->ca_id )
	{
		PKI_log(PKI_LOG_INFO, "MEM::Freeing %s CA config", ca->ca_id );
		PKI_Free ( ca->ca_id );
	}

	if ( ca->ca_cert ) PKI_X509_CERT_free ( ca->ca_cert );
	if ( ca->cid ) CA_ENTRY_CERTID_free ( ca->cid );
	if ( ca->ca_url ) URL_free ( ca->ca_url );
	if ( ca->crl_url ) URL_free ( ca->crl_url );

	if ( ca->crl_list )
	{
		X509_REVOKED *r = NULL;

		while ((r = sk_X509_REVOKED_pop ( ca->crl_list )) != NULL) 
		{
			X509_REVOKED_free ( r );
		}
	}

	if ( ca->nextUpdate ) PKI_TIME_free ( ca->nextUpdate );
	if ( ca->lastUpdate ) PKI_TIME_free ( ca->lastUpdate );

	if ( ca->token_name ) PKI_Free ( ca->token_name );
	if ( ca->token ) PKI_TOKEN_free ( ca->token );

	PKI_Free ( ca );

	return;
}
Ejemplo n.º 2
0
PKI_X509_CERT * PKI_X509_CERT_new (const PKI_X509_CERT    * ca_cert, 
                                   const PKI_X509_KEYPAIR * kPair,
                                   const PKI_X509_REQ     * req,
                                   const char             * subj_s, 
                                   const char             * serial_s,
                                   uint64_t                 validity,
                                   const PKI_X509_PROFILE * conf,
                                   const PKI_ALGOR        * algor,
                                   const PKI_CONFIG       * oids,
                                   HSM *hsm ) {
  PKI_X509_CERT *ret = NULL;
  PKI_X509_CERT_VALUE *val = NULL;
  PKI_X509_NAME *subj = NULL;
  PKI_X509_NAME *issuer = NULL;
  PKI_DIGEST_ALG *digest = NULL;
  PKI_X509_KEYPAIR_VALUE *signingKey = NULL;
  PKI_TOKEN *tk = NULL;

  PKI_X509_KEYPAIR_VALUE  *certPubKeyVal = NULL;

  int rv = 0;
  int ver = 2;

  int64_t notBeforeVal = 0;

  ASN1_INTEGER *serial = NULL;

  char *ver_s = NULL;

  /* Check if the REQUIRED PKEY has been passed */
  if (!kPair || !kPair->value) {
    PKI_ERROR(PKI_ERR_PARAM_NULL, NULL);
    return (NULL);
  };

  signingKey = kPair->value;

  /* TODO: This has to be fixed, to work on every option */
  if ( subj_s )
  {
    subj = PKI_X509_NAME_new ( subj_s );
  }
  else if (conf || req)
  {
    char *tmp_s = NULL;

    // Let's use the configuration option first
    if (conf) {

      // Get the value of the DN, if present
      if ((tmp_s = PKI_CONFIG_get_value( conf, 
                                 "/profile/subject/dn")) != NULL ) {
        // Builds from the DN in the config  
        subj = PKI_X509_NAME_new(tmp_s);
        PKI_Free ( tmp_s );
      }
    }

    // If we still do not have a name, let's check
    // the request for one
    if (req && !subj) {

      const PKI_X509_NAME * req_subj = NULL;

      // Copy the name from the request
      if ((req_subj = PKI_X509_REQ_get_data(req, 
				    PKI_X509_DATA_SUBJECT)) != NULL) {
        subj = PKI_X509_NAME_dup(req_subj);
      }
    }

    // If no name is provided, let's use an empty one
    // TODO: Shall we remove this and fail instead ?
    if (!subj) subj = PKI_X509_NAME_new( "" );
  }
  else
  {
    struct utsname myself;
    char tmp_name[1024];

    if (uname(&myself) < 0) {
      subj = PKI_X509_NAME_new( "" );
    } else {
      sprintf( tmp_name, "CN=%s", myself.nodename );
      subj = PKI_X509_NAME_new( tmp_name );
    }
  }

  if (!subj) {
    PKI_ERROR(PKI_ERR_X509_CERT_CREATE_SUBJECT, subj_s );
    goto err;
  }

  if( ca_cert ) {
    const PKI_X509_NAME *ca_subject = NULL;

    /* Let's get the ca_cert subject and dup that data */
    // ca_subject = (PKI_X509_NAME *) 
    //     X509_get_subject_name( (X509 *) ca_cert );
    ca_subject = PKI_X509_CERT_get_data( ca_cert, 
        PKI_X509_DATA_SUBJECT );

    if( ca_subject ) {
      issuer = (PKI_X509_NAME *) X509_NAME_dup((X509_NAME *)ca_subject);
    } else {
      PKI_ERROR(PKI_ERR_X509_CERT_CREATE_ISSUER, NULL);
      goto err;
    }

  } else {
    issuer = (PKI_X509_NAME *) X509_NAME_dup((X509_NAME *) subj);
  }

  if( !issuer ) {
    PKI_ERROR(PKI_ERR_X509_CERT_CREATE_ISSUER, NULL);
    goto err;
  }

  if(( ret = PKI_X509_CERT_new_null()) == NULL ) {
    PKI_ERROR(PKI_ERR_OBJECT_CREATE, NULL);
    goto err;
  }

  /* Alloc memory structure for the Certificate */
  if((ret->value = ret->cb->create()) == NULL ) {
    PKI_ERROR(PKI_ERR_OBJECT_CREATE, NULL);
    return (NULL);
  }

  val = ret->value;

  if(( ver_s = PKI_CONFIG_get_value( conf, "/profile/version")) != NULL ) {
    ver = atoi( ver_s ) - 1;
    if ( ver < 0 ) 
      ver = 0;
    PKI_Free ( ver_s );
  } else {
    ver = 2;
  };

  if (!X509_set_version(val,ver)) {
    PKI_ERROR(PKI_ERR_X509_CERT_CREATE_VERSION, NULL);
    goto err;
  }

  if (serial_s) {
    char * tmp_s = (char *) serial_s;
    serial = s2i_ASN1_INTEGER(NULL, tmp_s);
  } else {
    // If cacert we assume it is a normal cert - let's create a
    // random serial number, otherwise - it's a self-signed, use
    // the usual 'fake' 0
    if ( ca_cert ) {
      unsigned char bytes[11];
      RAND_bytes(bytes, sizeof(bytes));
      bytes[0] = 0;

      serial = PKI_INTEGER_new_bin(bytes, sizeof(bytes));
    } else {
      serial = s2i_ASN1_INTEGER( NULL, "0");
    };
  };

  if(!X509_set_serialNumber( val, serial )) {
    PKI_ERROR(PKI_ERR_X509_CERT_CREATE_SERIAL, serial_s);
    goto err;
  }

  /* Set the issuer Name */
  // rv = X509_set_issuer_name((X509 *) ret, (X509_NAME *) issuer);
  if(!X509_set_issuer_name( val, (X509_NAME *) issuer)) {
    PKI_ERROR(PKI_ERR_X509_CERT_CREATE_ISSUER, NULL);
    goto err;
  }

  /* Set the subject Name */
  if(!X509_set_subject_name(val, (X509_NAME *) subj)) {
    PKI_ERROR(PKI_ERR_X509_CERT_CREATE_SUBJECT, NULL);
    goto err;
  }

  /* Set the start date (notBefore) */
  if (conf)
  {
    int years = 0;
    int days  = 0;
    int hours = 0;
    int mins  = 0;
    int secs  = 0;

    char *tmp_s = NULL;

    if(( tmp_s = PKI_CONFIG_get_value( conf, 
        "/profile/notBefore/years")) != NULL ) {
      years = atoi( tmp_s );
      PKI_Free ( tmp_s );
    };

    if(( tmp_s = PKI_CONFIG_get_value( conf, 
        "/profile/notBefore/days")) != NULL ) {
      days = atoi( tmp_s );
      PKI_Free ( tmp_s );
    };

    if(( tmp_s = PKI_CONFIG_get_value( conf, 
        "/profile/notBefore/hours")) != NULL ) {
      hours = atoi( tmp_s );
      PKI_Free ( tmp_s );
    };

    if(( tmp_s = PKI_CONFIG_get_value( conf, 
        "/profile/notBefore/minutes")) != NULL ) {
      mins = atoi( tmp_s );
      PKI_Free ( tmp_s );
    };

    if(( tmp_s = PKI_CONFIG_get_value( conf, 
        "/profile/notBefore/seconds")) != NULL ) {
      secs = atoi( tmp_s );
      PKI_Free ( tmp_s );
    };

    notBeforeVal =   secs +
            ( mins * 60 ) + 
            ( hours * 3600 ) + 
            ( days   * 3600 * 24 ) + 
            ( years * 3600 * 24 * 365 );
  };

  /* Set the validity (notAfter) */
  if( conf && validity == 0 )
  {
    long long years = 0;
    long long days  = 0;
    long long hours = 0;
    long long mins  = 0;
    long long secs  = 0;

    char *tmp_s = NULL;

    if(( tmp_s = PKI_CONFIG_get_value( conf, 
        "/profile/validity/years")) != NULL ) {
      years = atoll( tmp_s );
      PKI_Free ( tmp_s );
    };

    if(( tmp_s = PKI_CONFIG_get_value( conf, 
        "/profile/validity/days")) != NULL ) {
      days = atoll( tmp_s );
      PKI_Free ( tmp_s );
    };

    if(( tmp_s = PKI_CONFIG_get_value( conf, 
        "/profile/validity/hours")) != NULL ) {
      hours = atoll( tmp_s );
      PKI_Free ( tmp_s );
    };

    if(( tmp_s = PKI_CONFIG_get_value( conf, 
        "/profile/validity/minutes")) != NULL ) {
      mins = atoll( tmp_s );
      PKI_Free ( tmp_s );
    };

    if(( tmp_s = PKI_CONFIG_get_value( conf, 
        "/profile/validity/minutes")) != NULL ) {
      secs = atoll( tmp_s );
      PKI_Free ( tmp_s );
    };

    validity =   (unsigned long long) secs +
          (unsigned long long) ( mins   * 60 ) + 
          (unsigned long long) ( hours * 3600 ) + 
          (unsigned long long) ( days   * 3600 * 24 ) + 
          (unsigned long long) ( years * 3600 * 24 * 365 );
  };

  if (validity <= 0) validity = 30 * 3600 * 24;

#if ( LIBPKI_OS_BITS == LIBPKI_OS32 )
  long notBeforeVal32 = (long) notBeforeVal;
  if (X509_gmtime_adj(X509_get_notBefore(val), notBeforeVal32 ) == NULL)
  {
#else
  if (X509_gmtime_adj(X509_get_notBefore(val), notBeforeVal ) == NULL)
  {
#endif
    PKI_ERROR(PKI_ERR_X509_CERT_CREATE_NOTBEFORE, NULL);
    goto err;
  }

  /* Set the end date in a year */
  if (X509_gmtime_adj(X509_get_notAfter(val),(long int) validity) == NULL)
  {
    PKI_DEBUG("ERROR: can not set notAfter field!");
    goto err;
  }

  /* Copy the PKEY if it is in the request, otherwise use the
     public part of the PKI_X509_CERT */
  if (req)
  {
    certPubKeyVal = (PKI_X509_KEYPAIR_VALUE *) 
       PKI_X509_REQ_get_data(req, PKI_X509_DATA_KEYPAIR_VALUE);

    if( !certPubKeyVal ) {
      PKI_DEBUG("ERROR, can not get pubkey from req!");
      goto err;
    }
  }
  else
  {
    /* Self Signed -- Same Public Key! */
    certPubKeyVal = signingKey;
  }

  if (!ca_cert && conf)
  {
    char *tmp_s = NULL;

    if(( tmp_s = PKI_X509_PROFILE_get_value( conf, 
        "/profile/keyParams/algorithm")) != NULL )
    {
      PKI_ALGOR *myAlg = NULL;
      PKI_DIGEST_ALG *dgst = NULL;

      if((myAlg = PKI_ALGOR_get_by_name( tmp_s )) != NULL )
      {
        if(!algor) algor = myAlg;

        if((dgst = PKI_ALGOR_get_digest( myAlg )) != NULL )
        {
          PKI_DEBUG("Got Signing Algorithm: %s, %s",
            PKI_DIGEST_ALG_get_parsed(dgst), PKI_ALGOR_get_parsed(myAlg));
          digest = dgst;
        }
        else
        {
          PKI_DEBUG("Can not parse digest algorithm from %s", tmp_s);
        }
      }
      else
      {
        PKI_DEBUG("Can not parse key algorithm from %s", tmp_s);
      }
      PKI_Free ( tmp_s );
    }
  }

  if (conf)
  {
    PKI_KEYPARAMS *kParams = NULL;
    PKI_SCHEME_ID scheme;

    scheme = PKI_ALGOR_get_scheme( algor );

    kParams = PKI_KEYPARAMS_new(scheme, conf);
    if (kParams)
    {
      /* Sets the point compression */
      switch ( kParams->scheme )
      {
#ifdef ENABLE_ECDSA
        case PKI_SCHEME_ECDSA:
            if ( (int) kParams->ec.form > 0 )
            {
# if OPENSSL_VERSION_NUMBER < 0x1010000fL
              EC_KEY_set_conv_form(certPubKeyVal->pkey.ec, 
              			   (point_conversion_form_t) kParams->ec.form);
# else
              EC_KEY_set_conv_form(EVP_PKEY_get0_EC_KEY(certPubKeyVal), 
              (point_conversion_form_t) kParams->ec.form);
# endif
            }
          if ( kParams->ec.asn1flags > -1 )
          {
# if OPENSSL_VERSION_NUMBER < 0x1010000fL
            EC_KEY_set_asn1_flag(certPubKeyVal->pkey.ec,
              kParams->ec.asn1flags );
# else
            EC_KEY_set_asn1_flag(EVP_PKEY_get0_EC_KEY(certPubKeyVal),
              kParams->ec.asn1flags );
# endif
          }
          break;
#endif
        case PKI_SCHEME_RSA:
        case PKI_SCHEME_DSA:
          break;

        default:
          // Nothing to do
          PKI_ERROR(PKI_ERR_GENERAL, "Signing Scheme Uknown %d!", kParams->scheme);
          break;
      }
    }
  }

  if (!X509_set_pubkey(val, certPubKeyVal))
  {
    PKI_DEBUG("ERROR, can not set pubkey in cert!");
    goto err;
  }

  if (conf)
  {
    if((tk = PKI_TOKEN_new_null()) == NULL )
    {
      PKI_ERROR(PKI_ERR_MEMORY_ALLOC, NULL);
      goto err;
    }

    PKI_TOKEN_set_cert(tk, ret);

    if (ca_cert) {
      PKI_TOKEN_set_cacert(tk, (PKI_X509_CERT *)ca_cert);
    } else {
      PKI_TOKEN_set_cacert(tk, (PKI_X509_CERT *)ret);
    }

    if (req) PKI_TOKEN_set_req(tk, (PKI_X509_REQ *)req );
    if (kPair) PKI_TOKEN_set_keypair ( tk, (PKI_X509_KEYPAIR *)kPair );

    rv = PKI_X509_EXTENSIONS_cert_add_profile(conf, oids, ret, tk);
    if (rv != PKI_OK)
    {
      PKI_DEBUG( "ERROR, can not set extensions!");

      tk->cert = NULL;
      tk->cacert = NULL;
      tk->req = NULL;
      tk->keypair = NULL;

      PKI_TOKEN_free ( tk );

      goto err;
    }

    // Cleanup for the token (used only to add extensions)
    tk->cert = NULL;
    tk->cacert = NULL;
    tk->req = NULL;
    tk->keypair = NULL;
    PKI_TOKEN_free ( tk );
  }

  if (!digest)
  {
    if (!algor)
    {
      PKI_log_debug("Getting the Digest Algorithm from the CA cert");

      // Let's get the Digest Algorithm from the CA Cert
      if (ca_cert)
      {
        if((algor = PKI_X509_CERT_get_data(ca_cert,
              PKI_X509_DATA_ALGORITHM )) == NULL)
        {
          PKI_log_err("Can not retrieve DATA algorithm from CA cert");
        }
      }
    }

    // If we have an Algor from either the passed argument or
    // the CA Certificate, extract the digest from it. Otherwise
    // get the digest from the signing key
    if (algor)
    {
      if((digest = PKI_ALGOR_get_digest(algor)) == NULL )
      {
        PKI_log_err("Can not get digest from algor");
      }
    }

    // Check, if still no digest, let's try from the signing Key
    if (digest == NULL)
    {
      if ((digest = PKI_DIGEST_ALG_get_by_key( kPair )) == NULL)
      {
        PKI_log_err("Can not infer digest algor from the key pair");
      }
    }
  }

  // No Digest Here ? We failed...
  if (digest == NULL)
  {
    PKI_log_err("PKI_X509_CERT_new()::Can not get the digest!");
    return( NULL );
  }

  // Sign the data
  if (PKI_X509_sign(ret, digest, kPair) == PKI_ERR)
  {
    PKI_log_err ("Can not sign certificate [%s]",
      ERR_error_string(ERR_get_error(), NULL ));
    PKI_X509_CERT_free ( ret );
    return NULL;
  }

#if ( OPENSSL_VERSION_NUMBER >= 0x0090900f )

# if OPENSSL_VERSION_NUMBER < 0x1010000fL
  PKI_X509_CERT_VALUE *cVal = (PKI_X509_CERT_VALUE *) ret->value;

  if (cVal && cVal->cert_info)
  {
    PKI_log_debug("Signature = %s", 
      PKI_ALGOR_get_parsed(cVal->cert_info->signature));
  }
# endif

  //  PKI_X509_CINF_FULL *cFull = NULL;
  //  cFull = (PKI_X509_CINF_FULL *) cVal->cert_info;
  //  cFull->enc.modified = 1;
#endif

  return ret;

err:

  if (ret) PKI_X509_CERT_free(ret);
  if (subj) PKI_X509_NAME_free(subj);
  if (issuer) PKI_X509_NAME_free(issuer);

  return NULL;
}

/*!
 * \brief Signs a PKI_X509_CERT
 */

int PKI_X509_CERT_sign(PKI_X509_CERT *cert, PKI_X509_KEYPAIR *kp,
    PKI_DIGEST_ALG *digest) {

  const PKI_ALGOR *alg = NULL;

  if( !cert || !cert->value || !kp || !kp->value ) {
    PKI_ERROR(PKI_ERR_PARAM_NULL, NULL);
    return PKI_ERR;
  }

  if(!digest) {
    if((alg = PKI_X509_CERT_get_data(cert, PKI_X509_DATA_ALGORITHM))!=NULL) {
      digest = PKI_ALGOR_get_digest ( alg );
    }
  }

  if(!digest) {
    if((digest = PKI_DIGEST_ALG_get_by_key(kp)) == NULL) {
      PKI_log_err("PKI_X509_CERT_new()::Can not get digest algor "
          "from key");
      return PKI_ERR;
    }
  }

  if( PKI_X509_sign(cert, digest, kp) == PKI_ERR) {
    PKI_log_err ("PKI_X509_CERT_new()::Can not sign certificate [%s]",
      ERR_error_string(ERR_get_error(), NULL ));
    return PKI_ERR;
  }

  return PKI_OK;
};

/*!
 * \brief Signs a PKI_X509_CERT by using a configured PKI_TOKEN
 */

int PKI_X509_CERT_sign_tk ( PKI_X509_CERT *cert, PKI_TOKEN *tk,
    PKI_DIGEST_ALG *digest) {

  PKI_X509_KEYPAIR *kp = NULL;

  if( !cert || !cert->value || !tk ) {
    PKI_ERROR(PKI_ERR_PARAM_NULL, NULL);
    return PKI_ERR;
  };

  if( PKI_TOKEN_login( tk ) == PKI_ERR ) {
    PKI_ERROR(PKI_ERR_HSM_LOGIN, NULL);
    return PKI_ERR;
  };

  if((kp = PKI_TOKEN_get_keypair( tk )) == NULL ) {
    return PKI_ERR;
  };

  return PKI_X509_CERT_sign ( cert, kp, digest );
};
Ejemplo n.º 3
0
int main (int argc, char *argv[] ) {

	PKI_TOKEN *tk = NULL;
	PKI_X509_PROFILE *prof =  NULL;
	// PKI_OID *oid = NULL;

	PKI_X509_CRL *crl = NULL;
	PKI_X509_CRL_ENTRY *entry = NULL;
	PKI_X509_CRL_ENTRY_STACK *sk = NULL;

	printf("\n\nlibpki Test - Massimiliano Pala <*****@*****.**>\n");
	printf("(c) 2006 by Massimiliano Pala and OpenCA Project\n");
	printf("OpenCA Licensed Software\n\n");

	if(( PKI_log_init (PKI_LOG_TYPE_STDERR, PKI_LOG_NOTICE, NULL,
			PKI_LOG_FLAGS_ENABLE_DEBUG, NULL )) == PKI_ERR ) {
		exit(1);
	}

	if((tk = PKI_TOKEN_new_null()) == NULL ) {
		printf("ERROR, can not allocate token!\n\n");
		exit(1);
	}

	if(( PKI_TOKEN_init( tk, "etc" , "Default" )) == PKI_ERR) {
		printf("ERROR, can not configure token!\n\n");
		exit(1);
	}

	if((PKI_TOKEN_set_algor ( tk, PKI_ALGOR_RSA_SHA256 )) == PKI_ERR ) {
                printf("ERROR, can not set the RSA crypto scheme!\n");
                return (0);
        }

        if((PKI_TOKEN_new_keypair ( tk, 1024, NULL )) == PKI_ERR) {
                printf("ERROR, can not generate new keypair!\n");
                return (0);
        }

	printf("* Self Signing certificate .... ");
        if((PKI_TOKEN_self_sign( tk, NULL, "23429", 24*3600, "User" )) == PKI_ERR ) {
                printf("ERROR, can not self sign certificate!\n");
                return(0);
        }

	printf("Generating a new CRL ENTRY ... ");
	if((entry = PKI_X509_CRL_ENTRY_new_serial ( "12345678", 
			CRL_REASON_KEY_COMPROMISE, NULL, NULL )) 
								== NULL ) {
		printf("ERROR!\n");
		exit(1);
	}
	printf("Ok\n");

	sk = PKI_STACK_X509_CRL_ENTRY_new();
	PKI_STACK_X509_CRL_ENTRY_push( sk, entry );

	printf("Generating new CRL ... ");
	if((crl = PKI_TOKEN_issue_crl (tk, "3", 
				PKI_VALIDITY_ONE_WEEK, sk, "crl")) == NULL ) {
		printf("ERROR, can not generate new CRL!\n");
		exit(1);
	}
	printf("Ok\n");

	if( tk ) PKI_TOKEN_free ( tk );
	if( prof ) PKI_X509_PROFILE_free ( prof );
	if( crl )  PKI_X509_CRL_free ( crl );

	PKI_log_end();

	printf("\n\n[ Test Ended Succesfully ]\n\n");

	return (0);
}
Ejemplo n.º 4
0
PKI_X509_CRL *PKI_X509_CRL_new(const PKI_X509_KEYPAIR *k,
             const PKI_X509_CERT *cert, 
             const char * crlNumber_s,
             unsigned long validity,
             const PKI_X509_CRL_ENTRY_STACK *sk, 
             const PKI_X509_PROFILE *profile,
             const PKI_CONFIG *oids,
             HSM *hsm) {

  PKI_X509_CRL *ret = NULL;
  PKI_X509_CRL_VALUE *val = NULL;
  ASN1_INTEGER *crlNumber = NULL;
  ASN1_TIME *time = NULL;
  int rv = PKI_OK;
  int i = 0;

  char * tmp_s = NULL;

  PKI_X509_CRL_ENTRY *entry = NULL;

  PKI_DIGEST_ALG *dgst = NULL;

  long long lastUpdateVal  = 0;
  long long nextUpdateVal  = 0;

  /* Checks for the Key and its internal value */
  if( !k || !k->value ) return NULL;

  /* checks for the certificate and its internal value */
  if( !cert || !cert->value ) return ( NULL );

  if(( ret = PKI_X509_CRL_new_null()) == NULL ) {
    PKI_ERROR(PKI_ERR_OBJECT_CREATE, NULL);
    goto err;
  }

  /* Alloc memory structure for the Certificate */
  if((ret->value = ret->cb->create()) == NULL ) {
    PKI_ERROR(PKI_ERR_OBJECT_CREATE, NULL);
    goto err;
  }

  val = ret->value;

  if ( !crlNumber_s && profile ) {

    if(( tmp_s = PKI_CONFIG_get_value( profile, 
        "/profile/crlNumber")) != NULL ) {
      crlNumber = PKI_INTEGER_new_char ( tmp_s );
      PKI_Free ( tmp_s );
    };
  } else if ( crlNumber_s ) {
    crlNumber = PKI_INTEGER_new_char( crlNumber_s );

    // Let's add the CRLSerial extension
    X509_CRL_add1_ext_i2d(val, NID_crl_number, crlNumber, 0, 0);
  };

  /* Set the start date (notBefore) */
  if (profile)
  {
    int years = 0;
    int days  = 0;
    int hours = 0;
    int mins  = 0;
    int secs  = 0;

    if(( tmp_s = PKI_CONFIG_get_value( profile, 
        "/profile/notBefore/years")) != NULL ) {
      years = atoi( tmp_s );
      PKI_Free ( tmp_s );
    };

    if(( tmp_s = PKI_CONFIG_get_value( profile, 
        "/profile/notBefore/days")) != NULL ) {
      days = atoi( tmp_s );
      PKI_Free ( tmp_s );
    };

    if(( tmp_s = PKI_CONFIG_get_value( profile, 
        "/profile/notBefore/hours")) != NULL ) {
      hours = atoi( tmp_s );
      PKI_Free ( tmp_s );
    };

    if(( tmp_s = PKI_CONFIG_get_value( profile, 
        "/profile/notBefore/minutes")) != NULL ) {
      mins = atoi( tmp_s );
      PKI_Free ( tmp_s );
    };

    if(( tmp_s = PKI_CONFIG_get_value( profile, 
        "/profile/notBefore/minutes")) != NULL ) {
      secs = atoi( tmp_s );
      PKI_Free ( tmp_s );
    };

    lastUpdateVal = secs +
            ( mins * 60 ) + 
            ( hours * 3600 ) + 
            ( days   * 3600 * 24 ) + 
            ( years * 3600 * 24 * 365 );
  } 
  else 
  {
    // Sets lastUpdate to current time
    lastUpdateVal = 0;
  };

  if ( profile && validity <= 0 ) {
    long long years = 0;
    long long days = 0;
    long long hours = 0;
    long long mins = 0;
    long long secs = 0;

    if((tmp_s = PKI_CONFIG_get_value ( profile,
          "/profile/validity/years")) != NULL ) {
      years = atoll( tmp_s );
      PKI_Free(tmp_s);
    }

    if((tmp_s = PKI_CONFIG_get_value ( profile,
          "/profile/validity/days")) != NULL ) {
      days = atoll( tmp_s );
      PKI_Free( tmp_s );
    }

    if((tmp_s = PKI_CONFIG_get_value ( profile,
          "/profile/validity/hours")) != NULL ) {
      hours = atoll( tmp_s );
      PKI_Free( tmp_s );
    }

    if((tmp_s = PKI_CONFIG_get_value ( profile,
          "/profile/validity/mins")) != NULL ) {
      mins = atoll( tmp_s );
      PKI_Free ( tmp_s );
    }

    if((tmp_s = PKI_CONFIG_get_value ( profile,
          "/profile/validity/secs")) != NULL ) {
      secs = atoll( tmp_s );
      PKI_Free ( tmp_s );
    }

    nextUpdateVal = secs + 
        60 * ( mins + 
          60 * (hours + 
            24 * ( days + 
              365 * years 
                 )
               )
            );
  } 
  else
  {
    nextUpdateVal = (long long) validity;
  };

  /* Generates a new time for lastUpdate field */
  if((time = PKI_TIME_new( lastUpdateVal )) == NULL ) {
    PKI_ERROR(PKI_ERR_MEMORY_ALLOC, NULL);
    goto err;
  };

  /* Set the Last Update field */
  if(X509_CRL_set_lastUpdate( val, time ) == 0 ) {
    PKI_log_err ( "ERROR, can not set lastUpdate field in CRL");
    goto err;
  }
  PKI_TIME_free ( time );
  time = NULL; // Memory

  /* Generates a new time for lastUpdate field */
  if((time = PKI_TIME_new( nextUpdateVal )) == NULL ) {
    PKI_ERROR(PKI_ERR_MEMORY_ALLOC, NULL);
    goto err;
  };

  /* Set the nextUpdate field */
  if(X509_CRL_set_nextUpdate( val, time ) == 0 ) {
    PKI_log_err ( "ERROR, can not set lastUpdate field in CRL");
    goto err;
  }
  PKI_TIME_free ( time );
  time = NULL; // Memory

  /* Now we need to add the CRL issuer name and details */
  if (X509_CRL_set_issuer_name( val, 
        X509_get_subject_name(cert->value)) == 0) {
    PKI_log_debug( "Can not set CRL issuer name");
    goto err;
  }

  if ( sk ) {
    /* Adds the list of revoked certificates */
    for(i=0; i < PKI_STACK_X509_CRL_ENTRY_elements(sk); i++ ) {
      PKI_log_debug("CRL::ADDING ENTRY %d\n", i );

      entry = PKI_STACK_X509_CRL_ENTRY_get_num(sk, i);
      if(!entry) break;

      X509_CRL_add0_revoked(val, entry);
    };
  }

  /* Sorts the CRL entries */
  X509_CRL_sort ( val );

  /*
  if((ret = PKI_X509_new_value( PKI_DATATYPE_X509_CRL, val, hsm)) == NULL ) {
    PKI_ERROR(PKI_ERR_MEMORY_ALLOC, NULL);
    X509_CRL_free ( val );
    return ( NULL );
  }
  */

  /* Get the extensions from the profile */
  if( profile ) {
    PKI_TOKEN * tk;

    if((tk = PKI_TOKEN_new_null()) == NULL ) {
      PKI_log_err ( "Memory allocation failure");
      PKI_X509_CRL_free ( ret );
      return NULL;
    }

    PKI_TOKEN_set_cert(tk, (PKI_X509_CERT *)cert);
    PKI_TOKEN_set_keypair(tk, (PKI_X509_KEYPAIR *)k);

    if(PKI_X509_EXTENSIONS_crl_add_profile( profile, oids, ret, tk) == 0 ) {
          PKI_log_debug( "ERROR, can not set extensions!");
      PKI_X509_CRL_free ( ret );

      tk->cert = NULL;
      tk->keypair = NULL;
      PKI_TOKEN_free ( tk );

      return ( NULL );
    }
    tk->cert = NULL;
    tk->keypair = NULL;
    PKI_TOKEN_free ( tk );
  }

  /* Get the Digest Algorithm */
  if( (dgst = PKI_DIGEST_ALG_get_by_key( k )) == NULL ) {
    PKI_log_err("Can not get digest algor from keypair!");
    goto err;
  }
  
  rv = PKI_X509_sign ( ret, dgst, k );

  if ( rv == PKI_ERR ) {
    PKI_log_debug ("ERROR, can not sign CRL!");
    goto err;
  }

  return( ret );

err:

  if ( time ) PKI_TIME_free ( time );
  if ( ret ) PKI_X509_CRL_free ( ret );
  return NULL;
}
Ejemplo n.º 5
0
int gen_X509_tk(int scheme, int bits, char *file ) {

	PKI_TOKEN *tk = NULL;
	PKI_X509_KEYPAIR *p = NULL;
	PKI_X509_CERT *r = NULL;
	PKI_ALGOR_ID algor = PKI_ALGOR_UNKNOWN;

	switch (scheme) {
		case PKI_SCHEME_RSA:
			printf("  * Generating RSA Key and Certificate:\n");
			algor = PKI_ALGOR_RSA_SHA1;
			break;
		case PKI_SCHEME_DSA:
			printf("  * Generating DSA Key and Certificate:\n");
			algor = PKI_ALGOR_DSA_SHA1;
			break;
		case PKI_SCHEME_ECDSA:
			printf("  * Generating ECDSA Key and Certificate: \n");
			algor = PKI_ALGOR_ECDSA_SHA1;
			break;
		default:
			printf("Unrecognized format!\n");
			return (0);
	}

	printf("    - generating a new token ... " );
	if((tk = PKI_TOKEN_new_null()) == NULL ) {
		printf("ERROR::Can not generate a new Token!\n\n");
		return(0);
	}
	printf("Ok.\n");

	printf("    - setting token algorithm (%d) ... ", algor );
	if((PKI_TOKEN_set_algor ( tk, algor )) == PKI_ERR ) {
		printf("ERROR::Can not set the token algorithm!\n\n");
		return (0);
	}
	printf("Ok.\n");

	printf("    - generating new Keypair (%d bits) ... ", bits );
	if((PKI_TOKEN_new_keypair ( tk, bits, NULL )) == PKI_ERR) {
		printf("ERROR::can not generate a new Keypair!\n\n");
		return (0);
	}
	printf("Ok.\n");

	printf("    - generating a self-signed cert ... " );
	if((PKI_TOKEN_self_sign( tk, NULL, "01", 24*3600, NULL )) == PKI_ERR ) {
		printf("ERROR::Can not generate a new self-signed cert!\n\n");
		return(0);
	}
	printf("Ok.\n");

	/* Assign the Certificate to the Token---when freeing the PKI_TOKEN
	   the cert memory is also freed */
	/*
	if((PKI_TOKEN_set_cert( tk, r )) == 0 ) {
		printf("ERROR!\n");
		return(0);
	}
	*/

	/*
	if(!PKI_X509_CERT_write_file( r, PKI_FORMAT_PEM, file )) {
		fprintf( stderr, "<file write error %s> ", file);
	}
	*/

	// if( tk ) PKI_TOKEN_free ( tk );
	// if( r ) PKI_X509_CERT_free ( r );
	// if( p ) PKI_KEYPAIR_free( p );

	printf("\n");

	if( tk ) PKI_TOKEN_free ( tk );

	return 1;
}