some const fixes and cleanup
[openssl.git] / crypto / asn1 / t_pkey.c
index 0a20311c5600daade6aa10d9f020841dd3712fda..86bd2e04e42dd5b1b3d97a4c0a22bbd454b1442e 100644 (file)
@@ -141,22 +141,18 @@ int RSA_print(BIO *bp, const RSA *x, int off)
                goto err;
                }
 
-       if (off)
-               {
-               if (off > 128) off=128;
-               memset(str,' ',off);
-               }
        if (x->d != NULL)
                {
-               if (off && (BIO_write(bp,str,off) <= 0)) goto err;
+               if(!BIO_indent(bp,off,128))
+                  goto err;
                if (BIO_printf(bp,"Private-Key: (%d bit)\n",BN_num_bits(x->n))
                        <= 0) goto err;
                }
 
        if (x->d == NULL)
-               sprintf(str,"Modulus (%d bit):",BN_num_bits(x->n));
+               BIO_snprintf(str,sizeof str,"Modulus (%d bit):",BN_num_bits(x->n));
        else
-               strcpy(str,"modulus:");
+               BUF_strlcpy(str,"modulus:",sizeof str);
        if (!print(bp,str,x->n,m,off)) goto err;
        s=(x->d == NULL)?"Exponent:":"publicExponent:";
        if (!print(bp,s,x->e,m,off)) goto err;
@@ -194,7 +190,6 @@ int DSA_print_fp(FILE *fp, const DSA *x, int off)
 
 int DSA_print(BIO *bp, const DSA *x, int off)
        {
-       char str[128];
        unsigned char *m=NULL;
        int ret=0;
        size_t buf_len=0,i;
@@ -221,14 +216,10 @@ int DSA_print(BIO *bp, const DSA *x, int off)
                goto err;
                }
 
-       if (off)
-               {
-               if (off > 128) off=128;
-               memset(str,' ',off);
-               }
        if (x->priv_key != NULL)
                {
-               if (off && (BIO_write(bp,str,off) <= 0)) goto err;
+               if(!BIO_indent(bp,off,128))
+                  goto err;
                if (BIO_printf(bp,"Private-Key: (%d bit)\n",BN_num_bits(x->p))
                        <= 0) goto err;
                }
@@ -284,7 +275,6 @@ int EC_KEY_print_fp(FILE *fp, const EC_KEY *x, int off)
 
 int ECPKParameters_print(BIO *bp, const EC_GROUP *x, int off)
        {
-       char str[128];
        unsigned char *buffer=NULL;
        size_t  buf_len=0, i;
        int     ret=0, reason=ERR_R_BIO_LIB;
@@ -310,14 +300,8 @@ int ECPKParameters_print(BIO *bp, const EC_GROUP *x, int off)
                /* the curve parameter are given by an asn1 OID */
                int nid;
 
-               if (off)
-                       {
-                       if (off > 128)
-                               off=128;
-                       memset(str, ' ', off);
-                       if (BIO_write(bp, str, off) <= 0)
-                               goto err;
-                       }
+               if (!BIO_indent(bp, off, 128))
+                       goto err;
 
                nid = EC_GROUP_get_nid(x);
                if (nid == 0)
@@ -405,13 +389,10 @@ int ECPKParameters_print(BIO *bp, const EC_GROUP *x, int off)
                        reason = ERR_R_MALLOC_FAILURE;
                        goto err;
                        }
-               if (off)
-                       {
-                       if (off > 128) off=128;
-                       memset(str,' ',off);
-                       if (BIO_write(bp, str, off) <= 0)
-                               goto err;
-                       }
+
+               if (!BIO_indent(bp, off, 128))
+                       goto err;
+
                /* print the 'short name' of the field type */
                if (BIO_printf(bp, "Field Type: %s\n", OBJ_nid2sn(tmp_nid))
                        <= 0)
@@ -420,18 +401,12 @@ int ECPKParameters_print(BIO *bp, const EC_GROUP *x, int off)
                if (is_char_two)
                        {
                        /* print the 'short name' of the base type OID */
-                       int basis_type = EC_GROUP_get_basis_type(x, NULL,
-                               NULL, NULL);
+                       int basis_type = EC_GROUP_get_basis_type(x);
                        if (basis_type == 0)
                                goto err;
 
-                       if (off)
-                               {
-                               if (off > 128) off=128;
-                               memset(str,' ',off);
-                               if (BIO_write(bp, str, off) <= 0)
-                                       goto err;
-                               }
+                       if (!BIO_indent(bp, off, 128))
+                               goto err;
 
                        if (BIO_printf(bp, "Basis Type: %s\n", 
                                OBJ_nid2sn(basis_type)) <= 0)
@@ -501,11 +476,10 @@ err:
 
 int EC_KEY_print(BIO *bp, const EC_KEY *x, int off)
        {
-       char str[128];
        unsigned char *buffer=NULL;
        size_t  buf_len=0, i;
        int     ret=0, reason=ERR_R_BIO_LIB;
-       BIGNUM  *pub_key=NULL;
+       BIGNUM  *pub_key=NULL, *order=NULL;
        BN_CTX  *ctx=NULL;
  
        if (!x || !x->group)
@@ -534,16 +508,17 @@ int EC_KEY_print(BIO *bp, const EC_KEY *x, int off)
                reason = ERR_R_MALLOC_FAILURE;
                goto err;
                }
-       if (off)
-               {
-               if (off > 128) off=128;
-               memset(str,' ',off);
-               }
+
        if (x->priv_key != NULL)
                {
-               if (off && (BIO_write(bp, str, off) <= 0)) goto err;
+               if (!BIO_indent(bp, off, 128))
+                       goto err;
+               if ((order = BN_new()) == NULL)
+                       goto err;
+               if (!EC_GROUP_get_order(x->group, order, NULL))
+                       goto err;
                if (BIO_printf(bp, "Private-Key: (%d bit)\n", 
-                       BN_num_bits(x->priv_key)) <= 0) goto err;
+                       BN_num_bits(order)) <= 0) goto err;
                }
   
        if ((x->priv_key != NULL) && !print(bp, "priv:", x->priv_key, 
@@ -560,6 +535,8 @@ err:
                ECerr(EC_F_EC_KEY_PRINT, reason);
        if (pub_key) 
                BN_free(pub_key);
+       if (order)
+               BN_free(order);
        if (ctx)
                BN_CTX_free(ctx);
        if (buffer != NULL)
@@ -572,18 +549,12 @@ static int print(BIO *bp, const char *number, BIGNUM *num, unsigned char *buf,
             int off)
        {
        int n,i;
-       char str[128];
        const char *neg;
 
        if (num == NULL) return(1);
-       neg=(num->neg)?"-":"";
-       if (off)
-               {
-               if (off > 128) off=128;
-               memset(str,' ',off);
-               if (BIO_write(bp,str,off) <= 0) return(0);
-               }
-
+       neg = (BN_get_sign(num))?"-":"";
+       if(!BIO_indent(bp,off,128))
+               return 0;
        if (BN_is_zero(num))
                {
                if (BIO_printf(bp, "%s 0\n", number) <= 0)
@@ -613,9 +584,9 @@ static int print(BIO *bp, const char *number, BIGNUM *num, unsigned char *buf,
                        {
                        if ((i%15) == 0)
                                {
-                               str[0]='\n';
-                               memset(&(str[1]),' ',off+4);
-                               if (BIO_write(bp,str,off+1+4) <= 0) return(0);
+                               if(BIO_puts(bp,"\n") <= 0
+                                  || !BIO_indent(bp,off+4,128))
+                                   return 0;
                                }
                        if (BIO_printf(bp,"%02x%s",buf[i],((i+1) == n)?"":":")
                                <= 0) return(0);
@@ -628,7 +599,7 @@ static int print(BIO *bp, const char *number, BIGNUM *num, unsigned char *buf,
 static int print_bin(BIO *fp, const char *name, const unsigned char *buf,
                size_t len, int off)
        {
-       int i;
+       size_t i;
        char str[128];
 
        if (buf == NULL)