src/lib/crypto.c
int crypto_sign_add_signer(SIGNATURE *sig, DIGEST *digest, X509_KEYPAIR *keypair)
{
   SignerInfo *si = NULL;
   unsigned char *buf = NULL;
   unsigned int len;

   si = SignerInfo_new();

   if (!si) {
      /* Allocation failed in OpenSSL */
      return false;
   }

   /* Set the ASN.1 structure version number */
   ASN1_INTEGER_set(si->version, BACULA_ASN1_VERSION);

   /* Set the digest algorithm identifier */
   switch (digest->type) {
   case CRYPTO_DIGEST_MD5:
      si->digestAlgorithm = OBJ_nid2obj(NID_md5);
      break;
   case CRYPTO_DIGEST_SHA1:
      si->digestAlgorithm = OBJ_nid2obj(NID_sha1);
      break;
#ifdef HAVE_SHA2
   case CRYPTO_DIGEST_SHA256:
      si->digestAlgorithm = OBJ_nid2obj(NID_sha256);
      break;
   case CRYPTO_DIGEST_SHA512:
      si->digestAlgorithm = OBJ_nid2obj(NID_sha512);
      break;
#endif
   default:
      /* This should never happen */
      goto err;
   }

   /* Drop the string allocated by OpenSSL, and add our subjectKeyIdentifier */
   ASN1_OCTET_STRING_free(si->subjectKeyIdentifier);
   si->subjectKeyIdentifier = ASN1_OCTET_STRING_dup(keypair->keyid);

   /* Set our signature algorithm. We currently require RSA */
   assert(EVP_PKEY_base_id(keypair->pubkey) == EVP_PKEY_RSA);
   /* This is slightly evil. Reach into the MD structure and grab the key type */
   si->signatureAlgorithm = OBJ_nid2obj(EVP_MD_pkey_type(EVP_MD_CTX_md(digest->ctx)));

   /* Finalize/Sign our Digest */
   len = EVP_PKEY_size(keypair->privkey);
   buf = (unsigned char *) malloc(len);
   if (!EVP_SignFinal(digest->ctx, buf, &len, keypair->privkey)) {
      openssl_post_errors(M_ERROR, _("Signature creation failed"));
      goto err;
   }

   /* Add the signature to the SignerInfo structure */
   if (!ASN1_OCTET_STRING_set(si->signature, buf, len)) {
      /* Allocation failed in OpenSSL */
      goto err;
   }

   /* No longer needed */
   free(buf);

   /* Push the new SignerInfo structure onto the stack */
   sk_SignerInfo_push(sig->sigData->signerInfo, si);

   return true;

err:
   if (si) {
      SignerInfo_free(si);
   }
   if (buf) {
      free(buf);
   }

   return false;
}
/*
 * Add a new signer
 *  Returns: true on success
 *           false on failure
 */
Add a new signer
Returns: true on success
false on failure
Add a new signer Returns : true on success false on failure
int crypto_sign_add_signer(SIGNATURE *sig, DIGEST *digest, X509_KEYPAIR *keypair)
{
   SignerInfo *si = NULL;
   unsigned char *buf = NULL;
   unsigned int len;
   si = SignerInfo_new();
   if (!si) {
      return false;
   }
   ASN1_INTEGER_set(si->version, BACULA_ASN1_VERSION);
   switch (digest->type) {
   case CRYPTO_DIGEST_MD5:
      si->digestAlgorithm = OBJ_nid2obj(NID_md5);
      break;
   case CRYPTO_DIGEST_SHA1:
      si->digestAlgorithm = OBJ_nid2obj(NID_sha1);
      break;
#ifdef HAVE_SHA2
   case CRYPTO_DIGEST_SHA256:
      si->digestAlgorithm = OBJ_nid2obj(NID_sha256);
      break;
   case CRYPTO_DIGEST_SHA512:
      si->digestAlgorithm = OBJ_nid2obj(NID_sha512);
      break;
#endif
   default:
      goto err;
   }
   ASN1_OCTET_STRING_free(si->subjectKeyIdentifier);
   si->subjectKeyIdentifier = ASN1_OCTET_STRING_dup(keypair->keyid);
   assert(EVP_PKEY_base_id(keypair->pubkey) == EVP_PKEY_RSA);
   si->signatureAlgorithm = OBJ_nid2obj(EVP_MD_pkey_type(EVP_MD_CTX_md(digest->ctx)));
   len = EVP_PKEY_size(keypair->privkey);
   buf = (unsigned char *) malloc(len);
   if (!EVP_SignFinal(digest->ctx, buf, &len, keypair->privkey)) {
      openssl_post_errors(M_ERROR, _("Signature creation failed"));
      goto err;
   }
   if (!ASN1_OCTET_STRING_set(si->signature, buf, len)) {
      goto err;
   }
   free(buf);
   sk_SignerInfo_push(sig->sigData->signerInfo, si);
   return true;
err:
   if (si) {
      SignerInfo_free(si);
   }
   if (buf) {
      free(buf);
   }
   return false;
}
int crypto_sign_add_signer ( SIGNATURE * sig , DIGEST * digest , X509_KEYPAIR * keypair ) { SignerInfo * si = NULL ; unsigned char * buf = NULL ; unsigned int len ; si = SignerInfo_new ( ) ; if ( ! si ) { return false ; } ASN1_INTEGER_set ( si -> version , BACULA_ASN1_VERSION ) ; switch ( digest -> type ) { case CRYPTO_DIGEST_MD5 : si -> digestAlgorithm = OBJ_nid2obj ( NID_md5 ) ; break ; case CRYPTO_DIGEST_SHA1 : si -> digestAlgorithm = OBJ_nid2obj ( NID_sha1 ) ; break ; #ifdef HAVE_SHA2 case CRYPTO_DIGEST_SHA256 : si -> digestAlgorithm = OBJ_nid2obj ( NID_sha256 ) ; break ; case CRYPTO_DIGEST_SHA512 : si -> digestAlgorithm = OBJ_nid2obj ( NID_sha512 ) ; break ; #endif default : goto err ; } ASN1_OCTET_STRING_free ( si -> subjectKeyIdentifier ) ; si -> subjectKeyIdentifier = ASN1_OCTET_STRING_dup ( keypair -> keyid ) ; assert ( EVP_PKEY_base_id ( keypair -> pubkey ) == EVP_PKEY_RSA ) ; si -> signatureAlgorithm = OBJ_nid2obj ( EVP_MD_pkey_type ( EVP_MD_CTX_md ( digest -> ctx ) ) ) ; len = EVP_PKEY_size ( keypair -> privkey ) ; buf = ( unsigned char * ) malloc ( len ) ; if ( ! EVP_SignFinal ( digest -> ctx , buf , & len , keypair -> privkey ) ) { openssl_post_errors ( M_ERROR , _ ( " " ) ) ; goto err ; } if ( ! ASN1_OCTET_STRING_set ( si -> signature , buf , len ) ) { goto err ; } free ( buf ) ; sk_SignerInfo_push ( sig -> sigData -> signerInfo , si ) ; return true ; err : if ( si ) { SignerInfo_free ( si ) ; } if ( buf ) { free ( buf ) ; } return false ; }
Add a new signer
  Returns: true on success
           false on failure
Add a new signer Returns : true on success false on failure
/* Allocation failed in OpenSSL */ /* Set the ASN.1 structure version number */ /* Set the digest algorithm identifier */ /* This should never happen */ /* Drop the string allocated by OpenSSL, and add our subjectKeyIdentifier */ /* Set our signature algorithm. We currently require RSA */ /* This is slightly evil. Reach into the MD structure and grab the key type */ /* Finalize/Sign our Digest */ /* Add the signature to the SignerInfo structure */ /* Allocation failed in OpenSSL */ /* No longer needed */ /* Push the new SignerInfo structure onto the stack */