Ejemplo n.º 1
0
/**
 * cdk_seckey_to_sexp:
 * @sk: the secret key
 * @sexp: where to store the S-expression
 * @len: the length of sexp
 *
 * Convert a public key to an S-expression. sexp is allocated by this
 * function, but you have to cdk_free() it yourself.  The S-expression
 * is stored in canonical format as used by libgcrypt
 * (GCRYSEXP_FMT_CANON).
 **/
cdk_error_t
cdk_seckey_to_sexp (cdk_pkt_seckey_t sk, char **sexp, size_t * len)
{
  char *buf;
  size_t sexp_len;
  gcry_sexp_t sk_sexp;
  cdk_error_t rc;

  if (!sk || !sexp)
    return CDK_Inv_Value;
  
  rc = seckey_to_sexp (&sk_sexp, sk);
  if (rc)
    return rc;

  sexp_len = gcry_sexp_sprint (sk_sexp, GCRYSEXP_FMT_CANON, NULL, 0);
  if (!sexp_len)
    return CDK_Wrong_Format;

  buf = (char *) cdk_malloc (sexp_len);
  if (!buf)
    {
      gcry_sexp_release (sk_sexp);
      return CDK_Out_Of_Core;
    }

  sexp_len = gcry_sexp_sprint (sk_sexp, GCRYSEXP_FMT_CANON, buf, sexp_len);    
  gcry_sexp_release (sk_sexp);
  if (!sexp_len)
    {
      cdk_free (buf);
      return CDK_Wrong_Format;
    }

  if (len)
    *len = sexp_len;
  *sexp = buf;

  return CDK_Success;
}
Ejemplo n.º 2
0
/* Encode the given digest into a pkcs#1 compatible format. */
cdk_error_t
_cdk_digest_encode_pkcs1 (byte ** r_md, size_t * r_mdlen, int pk_algo,
                          const byte * md, int digest_algo, unsigned nbits)
{
  size_t dlen;

  if (!md || !r_md || !r_mdlen)
    return CDK_Inv_Value;

  dlen = _gnutls_hash_get_algo_len (digest_algo);
  if (dlen <= 0)
    return CDK_Inv_Algo;
  if (is_DSA (pk_algo))
    {                           /* DSS does not use a special encoding. */
      *r_md = cdk_malloc (dlen + 1);
      if (!*r_md)
        return CDK_Out_Of_Core;
      *r_mdlen = dlen;
      memcpy (*r_md, md, dlen);
      return 0;
    }
  else
    {
      const byte *asn;
      int asnlen;
      cdk_error_t rc;

      asnlen = _gnutls_get_digest_oid (digest_algo, &asn);
      if (asnlen < 0)
        return asnlen;

      rc = do_encode_md (r_md, r_mdlen, md, digest_algo, dlen,
                         nbits, asn, asnlen);
      return rc;
    }
  return 0;
}