contrib/ldns/dnssec_sign.c
ldns_rdf *
ldns_sign_public_dsa(ldns_buffer *to_sign, DSA *key)
{
	unsigned char *sha1_hash;
	ldns_rdf *sigdata_rdf;
	ldns_buffer *b64sig;

	DSA_SIG *sig;
	uint8_t *data;
	size_t pad;

	b64sig = ldns_buffer_new(LDNS_MAX_PACKETLEN);
	if (!b64sig) {
		return NULL;
	}

	sha1_hash = SHA1((unsigned char*)ldns_buffer_begin(to_sign),
				  ldns_buffer_position(to_sign), NULL);
	if (!sha1_hash) {
		ldns_buffer_free(b64sig);
		return NULL;
	}

	sig = DSA_do_sign(sha1_hash, SHA_DIGEST_LENGTH, key);
        if(!sig) {
		ldns_buffer_free(b64sig);
		return NULL;
        }

	data = LDNS_XMALLOC(uint8_t, 1 + 2 * SHA_DIGEST_LENGTH);
        if(!data) {
		ldns_buffer_free(b64sig);
                DSA_SIG_free(sig);
		return NULL;
        }

	data[0] = 1;
	pad = 20 - (size_t) BN_num_bytes(sig->r);
	if (pad > 0) {
		memset(data + 1, 0, pad);
	}
	BN_bn2bin(sig->r, (unsigned char *) (data + 1) + pad);

	pad = 20 - (size_t) BN_num_bytes(sig->s);
	if (pad > 0) {
		memset(data + 1 + SHA_DIGEST_LENGTH, 0, pad);
	}
	BN_bn2bin(sig->s, (unsigned char *) (data + 1 + SHA_DIGEST_LENGTH + pad));

	sigdata_rdf = ldns_rdf_new_frm_data(LDNS_RDF_TYPE_B64,
								 1 + 2 * SHA_DIGEST_LENGTH,
								 data);

	ldns_buffer_free(b64sig);
	LDNS_FREE(data);
        DSA_SIG_free(sig);

	return sigdata_rdf;
}
/**
 * Sign data with DSA
 *
 * \param[in] to_sign The ldns_buffer containing raw data that is
 *                    to be signed
 * \param[in] key The DSA key structure to sign with
 * \return ldns_rdf for the RRSIG ldns_rr
 */
Sign data with DSA
\param[in] to_sign The ldns_buffer containing raw data that is
to be signed
\param[in] key The DSA key structure to sign with
\return ldns_rdf for the RRSIG ldns_rr
Sign data with DSA \ param [ in ] to_sign The ldns_buffer containing raw data that is to be signed \ param [ in ] key The DSA key structure to sign with \ return ldns_rdf for the RRSIG ldns_rr
ldns_rdf *
ldns_sign_public_dsa(ldns_buffer *to_sign, DSA *key)
{
	unsigned char *sha1_hash;
	ldns_rdf *sigdata_rdf;
	ldns_buffer *b64sig;
	DSA_SIG *sig;
	uint8_t *data;
	size_t pad;
	b64sig = ldns_buffer_new(LDNS_MAX_PACKETLEN);
	if (!b64sig) {
		return NULL;
	}
	sha1_hash = SHA1((unsigned char*)ldns_buffer_begin(to_sign),
				  ldns_buffer_position(to_sign), NULL);
	if (!sha1_hash) {
		ldns_buffer_free(b64sig);
		return NULL;
	}
	sig = DSA_do_sign(sha1_hash, SHA_DIGEST_LENGTH, key);
        if(!sig) {
		ldns_buffer_free(b64sig);
		return NULL;
        }
	data = LDNS_XMALLOC(uint8_t, 1 + 2 * SHA_DIGEST_LENGTH);
        if(!data) {
		ldns_buffer_free(b64sig);
                DSA_SIG_free(sig);
		return NULL;
        }
	data[0] = 1;
	pad = 20 - (size_t) BN_num_bytes(sig->r);
	if (pad > 0) {
		memset(data + 1, 0, pad);
	}
	BN_bn2bin(sig->r, (unsigned char *) (data + 1) + pad);
	pad = 20 - (size_t) BN_num_bytes(sig->s);
	if (pad > 0) {
		memset(data + 1 + SHA_DIGEST_LENGTH, 0, pad);
	}
	BN_bn2bin(sig->s, (unsigned char *) (data + 1 + SHA_DIGEST_LENGTH + pad));
	sigdata_rdf = ldns_rdf_new_frm_data(LDNS_RDF_TYPE_B64,
								 1 + 2 * SHA_DIGEST_LENGTH,
								 data);
	ldns_buffer_free(b64sig);
	LDNS_FREE(data);
        DSA_SIG_free(sig);
	return sigdata_rdf;
}
ldns_rdf * ldns_sign_public_dsa ( ldns_buffer * to_sign , DSA * key ) { unsigned char * sha1_hash ; ldns_rdf * sigdata_rdf ; ldns_buffer * b64sig ; DSA_SIG * sig ; uint8_t * data ; size_t pad ; b64sig = ldns_buffer_new ( LDNS_MAX_PACKETLEN ) ; if ( ! b64sig ) { return NULL ; } sha1_hash = SHA1 ( ( unsigned char * ) ldns_buffer_begin ( to_sign ) , ldns_buffer_position ( to_sign ) , NULL ) ; if ( ! sha1_hash ) { ldns_buffer_free ( b64sig ) ; return NULL ; } sig = DSA_do_sign ( sha1_hash , SHA_DIGEST_LENGTH , key ) ; if ( ! sig ) { ldns_buffer_free ( b64sig ) ; return NULL ; } data = LDNS_XMALLOC ( uint8_t , 1 + 2 * SHA_DIGEST_LENGTH ) ; if ( ! data ) { ldns_buffer_free ( b64sig ) ; DSA_SIG_free ( sig ) ; return NULL ; } data [ 0 ] = 1 ; pad = 20 - ( size_t ) BN_num_bytes ( sig -> r ) ; if ( pad > 0 ) { memset ( data + 1 , 0 , pad ) ; } BN_bn2bin ( sig -> r , ( unsigned char * ) ( data + 1 ) + pad ) ; pad = 20 - ( size_t ) BN_num_bytes ( sig -> s ) ; if ( pad > 0 ) { memset ( data + 1 + SHA_DIGEST_LENGTH , 0 , pad ) ; } BN_bn2bin ( sig -> s , ( unsigned char * ) ( data + 1 + SHA_DIGEST_LENGTH + pad ) ) ; sigdata_rdf = ldns_rdf_new_frm_data ( LDNS_RDF_TYPE_B64 , 1 + 2 * SHA_DIGEST_LENGTH , data ) ; ldns_buffer_free ( b64sig ) ; LDNS_FREE ( data ) ; DSA_SIG_free ( sig ) ; return sigdata_rdf ; }
Sign data with DSA
 \param[in] to_sign The ldns_buffer containing raw data that is
                    to be signed
 \param[in] key The DSA key structure to sign with
 \return ldns_rdf for the RRSIG ldns_rr
Sign data with DSA \ param [ in ] to_sign The ldns_buffer containing raw data that is to be signed \ param [ in ] key The DSA key structure to sign with \ return ldns_rdf for the RRSIG ldns_rr