New options to the -verify program which can be used for chain verification.
[openssl.git] / apps / x509.c
index f56244c5c7e9aea31cf856405d1341189d753071..6469761f9e63218f8d48a6116ad21b43388896c0 100644 (file)
@@ -101,6 +101,7 @@ static char *x509_usage[]={
 " -purpose        - print out certificate purposes\n",
 " -dates          - both Before and After dates\n",
 " -modulus        - print the RSA key modulus\n",
+" -pubkey         - output the public key\n",
 " -fingerprint    - print the certificate fingerprint\n",
 " -alias          - output certificate alias\n",
 " -noout          - no certificate output\n",
@@ -135,14 +136,9 @@ static int sign (X509 *x, EVP_PKEY *pkey,int days,const EVP_MD *digest,
 static int x509_certify (X509_STORE *ctx,char *CAfile,const EVP_MD *digest,
                         X509 *x,X509 *xca,EVP_PKEY *pkey,char *serial,
                         int create,int days, LHASH *conf, char *section);
-static int efunc(X509_PURPOSE *pt, void *arg);
+static int purpose_print(BIO *bio, X509 *cert, X509_PURPOSE *pt);
 static int reqfile=0;
 
-typedef struct {
-BIO *bio;
-X509 *cert;
-} X509_PPRINT;
-
 int MAIN(int argc, char **argv)
        {
        int ret=1;
@@ -161,7 +157,7 @@ int MAIN(int argc, char **argv)
        int noout=0,sign_flag=0,CA_flag=0,CA_createserial=0;
        int trustout=0,clrtrust=0,clrnotrust=0,aliasout=0;
        int C=0;
-       int x509req=0,days=DEF_DAYS,modulus=0;
+       int x509req=0,days=DEF_DAYS,modulus=0,pubkey=0;
        int pprint = 0;
        char **pp;
        X509_STORE *ctx=NULL;
@@ -324,6 +320,8 @@ int MAIN(int argc, char **argv)
                        serial= ++num;
                else if (strcmp(*argv,"-modulus") == 0)
                        modulus= ++num;
+               else if (strcmp(*argv,"-pubkey") == 0)
+                       pubkey= ++num;
                else if (strcmp(*argv,"-x509toreq") == 0)
                        x509req= ++num;
                else if (strcmp(*argv,"-text") == 0)
@@ -606,11 +604,14 @@ bad:
                                }
                        else if (pprint == i)
                                {
-                               X509_PPRINT ptmp;
-                               ptmp.bio = STDout;
-                               ptmp.cert = x;
+                               X509_PURPOSE *ptmp;
+                               int j;
                                BIO_printf(STDout, "Certificate purposes:\n");
-                               X509_PURPOSE_enum(efunc, &ptmp);
+                               for(j = 0; j < X509_PURPOSE_get_count(); j++)
+                                       {
+                                       ptmp = X509_PURPOSE_iget(j);
+                                       purpose_print(STDout, x, ptmp);
+                                       }
                                }
                        else
                                if (modulus == i)
@@ -639,6 +640,21 @@ bad:
                                BIO_printf(STDout,"\n");
                                EVP_PKEY_free(pkey);
                                }
+                       else
+                               if (pubkey == i)
+                               {
+                               EVP_PKEY *pkey;
+
+                               pkey=X509_get_pubkey(x);
+                               if (pkey == NULL)
+                                       {
+                                       BIO_printf(bio_err,"Error getting public key\n");
+                                       ERR_print_errors(bio_err);
+                                       goto end;
+                                       }
+                               PEM_write_bio_PUBKEY(STDout, pkey);
+                               EVP_PKEY_free(pkey);
+                               }
                        else
                                if (C == i)
                                {
@@ -1209,20 +1225,18 @@ err:
        return(0);
        }
 
-static int efunc(X509_PURPOSE *pt, void *arg)
+static int purpose_print(BIO *bio, X509 *cert, X509_PURPOSE *pt)
 {
-       X509_PPRINT *ptmp;
        int id, i, idret;
        char *pname;
-       ptmp = arg;
        id = X509_PURPOSE_get_id(pt);
-       pname = X509_PURPOSE_get_name(pt);
+       pname = X509_PURPOSE_iget_name(pt);
        for(i = 0; i < 2; i++) {
-               idret = X509_check_purpose(ptmp->cert, id, i);
-               BIO_printf(ptmp->bio, "%s%s : ", pname, i ? " CA" : ""); 
-               if(idret == 1) BIO_printf(ptmp->bio, "Yes\n");
-               else if (idret == 0) BIO_printf(ptmp->bio, "No\n");
-               else BIO_printf(ptmp->bio, "Yes (WARNING code=%d)\n", idret);
+               idret = X509_check_purpose(cert, id, i);
+               BIO_printf(bio, "%s%s : ", pname, i ? " CA" : ""); 
+               if(idret == 1) BIO_printf(bio, "Yes\n");
+               else if (idret == 0) BIO_printf(bio, "No\n");
+               else BIO_printf(bio, "Yes (WARNING code=%d)\n", idret);
        }
        return 1;
 }