Logo Search packages:      
Sourcecode: ldns version File versions  Download package

ldns_status ldns_key2buffer_str ( ldns_buffer output,
const ldns_key k 
)

Converts the data in the DNS packet to presentation format (as char *) and appends it to the given buffer

Parameters:
[in] output pointer to the buffer to append the data to
[in] k the pointer to the private key to convert
Returns:
status

Definition at line 1064 of file host2str.c.

References error(), ldns_buffer_status(), ldns_buffer_status_ok(), ldns_key_algorithm(), ldns_rdf_deep_free(), ldns_rdf_new_frm_data(), and LDNS_RDF_TYPE_B64.

{
      ldns_status status = LDNS_STATUS_OK;
      unsigned char  *bignum;
#ifdef HAVE_SSL
      /* not used when ssl is not defined */
      ldns_rdf *b64_bignum = NULL;
      uint16_t i;
#endif /* HAVE_SSL */

      if (!k) {
            return LDNS_STATUS_ERR;
      }

      bignum = LDNS_XMALLOC(unsigned char, LDNS_MAX_KEYLEN);
      if (!bignum) {
            return LDNS_STATUS_ERR;
      }
      
      if (ldns_buffer_status_ok(output)) {
#ifdef HAVE_SSL
            switch(ldns_key_algorithm(k)) {
                  case LDNS_SIGN_RSASHA1:
                  case LDNS_SIGN_RSAMD5:
                        /* copied by looking at dnssec-keygen output */
                        /* header */
                        ldns_buffer_printf(output,"Private-key-format: v1.2\n");
                        if (ldns_key_algorithm(k) == LDNS_SIGN_RSAMD5) {
                              ldns_buffer_printf(output,"Algorithm: 1 (RSA)\n");
                        } else if (ldns_key_algorithm(k) == LDNS_SIGN_RSASHA1) {
                              ldns_buffer_printf(output,"Algorithm: 5 (RSASHA1)\n");
                        }

                        /* print to buf, convert to bin, convert to b64,
                         * print to buf */
                        ldns_buffer_printf(output, "Modulus: "); 
                        i = (uint16_t)BN_bn2bin(ldns_key_rsa_key(k)->n, bignum);
                        if (i > LDNS_MAX_KEYLEN) {
                              goto error;
                        }
                        b64_bignum =  ldns_rdf_new_frm_data(LDNS_RDF_TYPE_B64, i, bignum);
                        if (ldns_rdf2buffer_str(output, b64_bignum) != LDNS_STATUS_OK) {
                              goto error;
                        }
                        ldns_rdf_deep_free(b64_bignum);
                        ldns_buffer_printf(output, "\n"); 
                        
                        ldns_buffer_printf(output, "PublicExponent: "); 
                        i = (uint16_t)BN_bn2bin(ldns_key_rsa_key(k)->e, bignum);
                        if (i > LDNS_MAX_KEYLEN) {
                              goto error;
                        }
                        b64_bignum =  ldns_rdf_new_frm_data(LDNS_RDF_TYPE_B64, i, bignum);
                        if (ldns_rdf2buffer_str(output, b64_bignum) != LDNS_STATUS_OK) {
                              goto error;
                        }
                        ldns_rdf_deep_free(b64_bignum);
                        ldns_buffer_printf(output, "\n"); 

                        ldns_buffer_printf(output, "PrivateExponent: "); 
                        i = (uint16_t)BN_bn2bin(ldns_key_rsa_key(k)->d, bignum);
                        if (i > LDNS_MAX_KEYLEN) {
                              goto error;
                        }
                        b64_bignum =  ldns_rdf_new_frm_data(LDNS_RDF_TYPE_B64, i, bignum);
                        if (ldns_rdf2buffer_str(output, b64_bignum) != LDNS_STATUS_OK) {
                              goto error;
                        }
                        ldns_rdf_deep_free(b64_bignum);
                        ldns_buffer_printf(output, "\n"); 

                        ldns_buffer_printf(output, "Prime1: "); 
                        i = (uint16_t)BN_bn2bin(ldns_key_rsa_key(k)->p, bignum);
                        if (i > LDNS_MAX_KEYLEN) {
                              goto error;
                        }
                        b64_bignum =  ldns_rdf_new_frm_data(LDNS_RDF_TYPE_B64, i, bignum);
                        if (ldns_rdf2buffer_str(output, b64_bignum) != LDNS_STATUS_OK) {
                              goto error;
                        }
                        ldns_rdf_deep_free(b64_bignum);
                        ldns_buffer_printf(output, "\n"); 

                        ldns_buffer_printf(output, "Prime2: ");
                        i = (uint16_t)BN_bn2bin(ldns_key_rsa_key(k)->q, bignum);
                        if (i > LDNS_MAX_KEYLEN) {
                              goto error;
                        }
                        b64_bignum =  ldns_rdf_new_frm_data(LDNS_RDF_TYPE_B64, i, bignum);
                        if (ldns_rdf2buffer_str(output, b64_bignum) != LDNS_STATUS_OK) {
                              goto error;
                        }
                        ldns_rdf_deep_free(b64_bignum);
                        ldns_buffer_printf(output, "\n"); 

                        ldns_buffer_printf(output, "Exponent1: ");
                        i = (uint16_t)BN_bn2bin(ldns_key_rsa_key(k)->dmp1, bignum);
                        if (i > LDNS_MAX_KEYLEN) {
                              goto error;
                        }
                        b64_bignum =  ldns_rdf_new_frm_data(LDNS_RDF_TYPE_B64, i, bignum);
                        if (ldns_rdf2buffer_str(output, b64_bignum) != LDNS_STATUS_OK) {
                              goto error;
                        }
                        ldns_rdf_deep_free(b64_bignum);
                        ldns_buffer_printf(output, "\n"); 

                        ldns_buffer_printf(output, "Exponent2: "); 
                        i = (uint16_t)BN_bn2bin(ldns_key_rsa_key(k)->dmq1, bignum);
                        if (i > LDNS_MAX_KEYLEN) {
                              goto error;
                        }
                        b64_bignum =  ldns_rdf_new_frm_data(LDNS_RDF_TYPE_B64, i, bignum);
                        if (ldns_rdf2buffer_str(output, b64_bignum) != LDNS_STATUS_OK) {
                              goto error;
                        }
                        ldns_rdf_deep_free(b64_bignum);
                        ldns_buffer_printf(output, "\n"); 

                        ldns_buffer_printf(output, "Coefficient: "); 
                        i = (uint16_t)BN_bn2bin(ldns_key_rsa_key(k)->iqmp, bignum);
                        if (i > LDNS_MAX_KEYLEN) {
                              goto error;
                        }
                        b64_bignum =  ldns_rdf_new_frm_data(LDNS_RDF_TYPE_B64, i, bignum);
                        if (ldns_rdf2buffer_str(output, b64_bignum) != LDNS_STATUS_OK) {
                              goto error;
                        }
                        ldns_rdf_deep_free(b64_bignum);
                        ldns_buffer_printf(output, "\n"); 
                        break;
                  case LDNS_SIGN_DSA:
                        ldns_buffer_printf(output,"Private-key-format: v1.2\n");
                        ldns_buffer_printf(output,"Algorithm: 3 (DSA)\n");

                        /* print to buf, convert to bin, convert to b64,
                         * print to buf */
                        ldns_buffer_printf(output, "Prime(p): "); 
                        i = (uint16_t)BN_bn2bin(ldns_key_dsa_key(k)->p, bignum);
                        if (i > LDNS_MAX_KEYLEN) {
                              goto error;
                        }
                        b64_bignum =  ldns_rdf_new_frm_data(LDNS_RDF_TYPE_B64, i, bignum);
                        if (ldns_rdf2buffer_str(output, b64_bignum) != LDNS_STATUS_OK) {
                              goto error;
                        }
                        ldns_rdf_deep_free(b64_bignum);
                        ldns_buffer_printf(output, "\n"); 

                        ldns_buffer_printf(output, "Subprime(q): "); 
                        i = (uint16_t)BN_bn2bin(ldns_key_dsa_key(k)->q, bignum);
                        if (i > LDNS_MAX_KEYLEN) {
                              goto error;
                        }
                        b64_bignum =  ldns_rdf_new_frm_data(LDNS_RDF_TYPE_B64, i, bignum);
                        if (ldns_rdf2buffer_str(output, b64_bignum) != LDNS_STATUS_OK) {
                              goto error;
                        }
                        ldns_rdf_deep_free(b64_bignum);
                        ldns_buffer_printf(output, "\n"); 

                        ldns_buffer_printf(output, "Base(g): "); 
                        i = (uint16_t)BN_bn2bin(ldns_key_dsa_key(k)->g, bignum);
                        if (i > LDNS_MAX_KEYLEN) {
                              goto error;
                        }
                        b64_bignum =  ldns_rdf_new_frm_data(LDNS_RDF_TYPE_B64, i, bignum);
                        if (ldns_rdf2buffer_str(output, b64_bignum) != LDNS_STATUS_OK) {
                              goto error;
                        }
                        ldns_rdf_deep_free(b64_bignum);
                        ldns_buffer_printf(output, "\n"); 

                        ldns_buffer_printf(output, "Private_value(x): "); 
                        i = (uint16_t)BN_bn2bin(ldns_key_dsa_key(k)->priv_key, bignum);
                        if (i > LDNS_MAX_KEYLEN) {
                              goto error;
                        }
                        b64_bignum =  ldns_rdf_new_frm_data(LDNS_RDF_TYPE_B64, i, bignum);
                        if (ldns_rdf2buffer_str(output, b64_bignum) != LDNS_STATUS_OK) {
                              goto error;
                        }
                        ldns_rdf_deep_free(b64_bignum);
                        ldns_buffer_printf(output, "\n"); 

                        ldns_buffer_printf(output, "Public_value(y): "); 
                        i = (uint16_t)BN_bn2bin(ldns_key_dsa_key(k)->pub_key, bignum);
                        if (i > LDNS_MAX_KEYLEN) {
                              goto error;
                        }
                        b64_bignum =  ldns_rdf_new_frm_data(LDNS_RDF_TYPE_B64, i, bignum);
                        if (ldns_rdf2buffer_str(output, b64_bignum) != LDNS_STATUS_OK) {
                              goto error;
                        }
                        ldns_rdf_deep_free(b64_bignum);
                        ldns_buffer_printf(output, "\n"); 
                        break;
                  case LDNS_SIGN_HMACMD5:
                        /* is the filefmt specified for TSIG.. don't know */
                        goto error;
            }
#endif /* HAVE_SSL */
      } else {
#ifdef HAVE_SSL
            LDNS_FREE(b64_bignum);
#endif
            LDNS_FREE(bignum);
            return ldns_buffer_status(output);
      }
      LDNS_FREE(bignum);
      return status;

#ifdef HAVE_SSL
      /* compiles warn the label isn't used */
error:
      LDNS_FREE(bignum);
      return LDNS_STATUS_ERR;
#endif /* HAVE_SSL */
      
}


Generated by  Doxygen 1.6.0   Back to index