예제 #1
0
/*
** Sign a block of data returning in result a bunch of bytes that are the
** signature. Returns zero on success, an error code on failure.
*/
SECStatus
SEC_SignData(SECItem *res, const unsigned char *buf, int len,
	     SECKEYPrivateKey *pk, SECOidTag algid)
{
    SECStatus rv;
    SGNContext *sgn;


    sgn = SGN_NewContext(algid, pk);

    if (sgn == NULL)
	return SECFailure;

    rv = SGN_Begin(sgn);
    if (rv != SECSuccess)
	goto loser;

    rv = SGN_Update(sgn, buf, len);
    if (rv != SECSuccess)
	goto loser;

    rv = SGN_End(sgn, res);

  loser:
    SGN_DestroyContext(sgn, PR_TRUE);
    return rv;
}
예제 #2
0
/**
 * Writes the passed buffer to the file fp and updates the signature contexts.
 *
 * @param  fpDest   The file pointer to write to.
 * @param  buffer   The buffer to write.
 * @param  size     The size of the buffer to write.
 * @param  ctxs     Pointer to the first element in an array of signature
 *                  contexts to update.
 * @param  ctxCount The number of signature contexts pointed to by ctxs
 * @param  err    The name of what is being written to in case of error.
 * @return  0 on success
 *         -2 on write error
 *         -3 on signature update error
*/
int
WriteAndUpdateSignatures(FILE *fpDest, void *buffer,
                         uint32_t size, SGNContext **ctxs,
                         uint32_t ctxCount,
                         const char *err)
{
  uint32_t k;
  if (!size) {
    return 0;
  }

  if (fwrite(buffer, size, 1, fpDest) != 1) {
    fprintf(stderr, "ERROR: Could not write %s\n", err);
    return -2;
  }

  for (k = 0; k < ctxCount; ++k) {
    if (SGN_Update(ctxs[k], buffer, size) != SECSuccess) {
      fprintf(stderr, "ERROR: Could not update signature context for %s\n", err);
      return -3;
    }
  }
  return 0;
}