Fix null pointer errors.
[openssl.git] / crypto / ocsp / ocsp_prn.c
index 649a9cec913bb921bc9dce73f377bde5953fb3b6..87608ff399b1802956dbe2e782a5682ffeebcc05 100644 (file)
@@ -85,22 +85,22 @@ static int ocsp_certid_print(BIO *bp, OCSP_CERTID* a, int indent)
 typedef struct
        {
        long t;
-       char *m;
+       const char *m;
        } OCSP_TBLSTR;
 
-static char *table2string(long s, OCSP_TBLSTR *ts, int len)
+static const char *table2string(long s, const OCSP_TBLSTR *ts, int len)
 {
-       OCSP_TBLSTR *p;
+       const OCSP_TBLSTR *p;
        for (p=ts; p < ts + len; p++)
                if (p->t == s)
                         return p->m;
        return "(UNKNOWN)";
 }
 
-static char* ocspResponseStatus2string(long s)
+const char *OCSP_response_status_str(long s)
         {
-       static OCSP_TBLSTR rstat_tbl[] = {
-               { OCSP_RESPONSE_STATUS_SUCCESSFULL, "successful" },
+       static const OCSP_TBLSTR rstat_tbl[] = {
+               { OCSP_RESPONSE_STATUS_SUCCESSFUL, "successful" },
                { OCSP_RESPONSE_STATUS_MALFORMEDREQUEST, "malformedrequest" },
                { OCSP_RESPONSE_STATUS_INTERNALERROR, "internalerror" },
                { OCSP_RESPONSE_STATUS_TRYLATER, "trylater" },
@@ -109,18 +109,18 @@ static char* ocspResponseStatus2string(long s)
        return table2string(s, rstat_tbl, 6);
        } 
 
-static char* ocspCertStatus2string(long s)
+const char *OCSP_cert_status_str(long s)
         {
-       static OCSP_TBLSTR cstat_tbl[] = {
+       static const OCSP_TBLSTR cstat_tbl[] = {
                { V_OCSP_CERTSTATUS_GOOD, "good" },
                { V_OCSP_CERTSTATUS_REVOKED, "revoked" },
                { V_OCSP_CERTSTATUS_UNKNOWN, "unknown" } };
        return table2string(s, cstat_tbl, 3);
        } 
 
-static char * cRLReason2string(long s)
+const char *OCSP_crl_reason_str(long s)
         {
-       OCSP_TBLSTR reason_tbl[] = {
+       static const OCSP_TBLSTR reason_tbl[] = {
          { OCSP_REVOKED_STATUS_UNSPECIFIED, "unspecified" },
           { OCSP_REVOKED_STATUS_KEYCOMPROMISE, "keyCompromise" },
           { OCSP_REVOKED_STATUS_CACOMPROMISE, "cACompromise" },
@@ -153,28 +153,26 @@ int OCSP_REQUEST_print(BIO *bp, OCSP_REQUEST* o, unsigned long flags)
        if (BIO_write(bp,"\n    Requestor List:\n",21) <= 0) goto err;
        for (i = 0; i < sk_OCSP_ONEREQ_num(inf->requestList); i++)
                {
-               if (!sk_OCSP_ONEREQ_value(inf->requestList, i)) continue;
                one = sk_OCSP_ONEREQ_value(inf->requestList, i);
                cid = one->reqCert;
                ocsp_certid_print(bp, cid, 8);
                if (!X509V3_extensions_print(bp,
-                                       "OCSP Request Single Extensions",
-                                       one->singleRequestExtensions, flags, 4))
+                                       "Request Single Extensions",
+                                       one->singleRequestExtensions, flags, 8))
                                                        goto err;
                }
-       if (!X509V3_extensions_print(bp, "OCSP Request Extensions",
+       if (!X509V3_extensions_print(bp, "Request Extensions",
                        inf->requestExtensions, flags, 4))
                                                        goto err;
        if (sig)
                {
                X509_signature_print(bp, sig->signatureAlgorithm, sig->signature);
-               }
-
-       for (i=0; i<sk_X509_num(sig->certs); i++)
-               if (sk_X509_value(sig->certs,i) != NULL) {
+               for (i=0; i<sk_X509_num(sig->certs); i++)
+                       {
                        X509_print(bp, sk_X509_value(sig->certs,i));
                        PEM_write_bio_X509(bp,sk_X509_value(sig->certs,i));
-       }
+                       }
+               }
        return 1;
 err:
        return 0;
@@ -182,9 +180,8 @@ err:
 
 int OCSP_RESPONSE_print(BIO *bp, OCSP_RESPONSE* o, unsigned long flags)
         {
-       int i;
+       int i, ret = 0;
        long l;
-       unsigned char *p;
        OCSP_CERTID *cid = NULL;
        OCSP_BASICRESP *br = NULL;
        OCSP_RESPID *rid = NULL;
@@ -196,8 +193,8 @@ int OCSP_RESPONSE_print(BIO *bp, OCSP_RESPONSE* o, unsigned long flags)
 
        if (BIO_puts(bp,"OCSP Response Data:\n") <= 0) goto err;
        l=ASN1_ENUMERATED_get(o->responseStatus);
-       if (BIO_printf(bp,"    OCSP Response Status: %s (0x%x)\n",
-                      ocspResponseStatus2string(l), l) <= 0) goto err;
+       if (BIO_printf(bp,"    OCSP Response Status: %s (0x%lx)\n",
+                      OCSP_response_status_str(l), l) <= 0) goto err;
        if (rb == NULL) return 1;
         if (BIO_puts(bp,"    Response Type: ") <= 0)
                goto err;
@@ -208,9 +205,9 @@ int OCSP_RESPONSE_print(BIO *bp, OCSP_RESPONSE* o, unsigned long flags)
                BIO_puts(bp," (unknown response type)\n");
                return 1;
                }
-       p = ASN1_STRING_data(rb->response);
+
        i = ASN1_STRING_length(rb->response);
-       if (!(d2i_OCSP_BASICRESP(&br, &p, i))) goto err;
+       if (!(br = OCSP_response_get1_basic(o))) goto err;
        rd = br->tbsResponseData;
        l=ASN1_INTEGER_get(rd->version);
        if (BIO_printf(bp,"\n    Version: %lu (0x%lx)\n",
@@ -238,8 +235,8 @@ int OCSP_RESPONSE_print(BIO *bp, OCSP_RESPONSE* o, unsigned long flags)
                cid = single->certId;
                if(ocsp_certid_print(bp, cid, 4) <= 0) goto err;
                cst = single->certStatus;
-               if (BIO_printf(bp,"\n    Cert Status: %s",
-                              ocspCertStatus2string(cst->type)) <= 0)
+               if (BIO_printf(bp,"    Cert Status: %s",
+                              OCSP_cert_status_str(cst->type)) <= 0)
                        goto err;
                if (cst->type == V_OCSP_CERTSTATUS_REVOKED)
                        {
@@ -253,8 +250,8 @@ int OCSP_RESPONSE_print(BIO *bp, OCSP_RESPONSE* o, unsigned long flags)
                                {
                                l=ASN1_ENUMERATED_get(rev->revocationReason);
                                if (BIO_printf(bp, 
-                                        "\n    Revocation Reason: %s (0x%x)",
-                                              cRLReason2string(l), l) <= 0)
+                                        "\n    Revocation Reason: %s (0x%lx)",
+                                              OCSP_crl_reason_str(l), l) <= 0)
                                        goto err;
                                }
                        }
@@ -267,24 +264,27 @@ int OCSP_RESPONSE_print(BIO *bp, OCSP_RESPONSE* o, unsigned long flags)
                        if (!ASN1_GENERALIZEDTIME_print(bp,single->nextUpdate))
                                goto err;
                        }
-               if (!BIO_write(bp,"\n",1)) goto err;
+               if (BIO_write(bp,"\n",1) <= 0) goto err;
                if (!X509V3_extensions_print(bp,
-                                       "OCSP Basic Response Single Extensions",
-                                       single->singleExtensions, flags, 4))
+                                       "Response Single Extensions",
+                                       single->singleExtensions, flags, 8))
                                                        goto err;
+               if (BIO_write(bp,"\n",1) <= 0) goto err;
                }
-       if (!X509V3_extensions_print(bp, "OCSP Basic Response Extensions",
+       if (!X509V3_extensions_print(bp, "Response Extensions",
                                        rd->responseExtensions, flags, 4))
+                                                       goto err;
        if(X509_signature_print(bp, br->signatureAlgorithm, br->signature) <= 0)
                                                        goto err;
 
        for (i=0; i<sk_X509_num(br->certs); i++)
-               if (sk_X509_value(br->certs,i) != NULL) {
-                       X509_print(bp, sk_X509_value(br->certs,i));
-                       PEM_write_bio_X509(bp,sk_X509_value(br->certs,i));
-       }
+               {
+               X509_print(bp, sk_X509_value(br->certs,i));
+               PEM_write_bio_X509(bp,sk_X509_value(br->certs,i));
+               }
 
-       return 1;
+       ret = 1;
 err:
-       return 0;
+       OCSP_BASICRESP_free(br);
+       return ret;
        }