optimized parser->success()
authorAndreas Steffen <andreas.steffen@strongswan.org>
Sat, 26 Apr 2008 11:08:36 +0000 (11:08 -0000)
committerAndreas Steffen <andreas.steffen@strongswan.org>
Sat, 26 Apr 2008 11:08:36 +0000 (11:08 -0000)
src/libstrongswan/crypto/pkcs7.c
src/libstrongswan/crypto/pkcs9.c
src/libstrongswan/plugins/gmp/gmp_rsa_private_key.c
src/libstrongswan/plugins/gmp/gmp_rsa_public_key.c
src/libstrongswan/plugins/x509/x509_ac.c
src/libstrongswan/plugins/x509/x509_cert.c
src/libstrongswan/plugins/x509/x509_crl.c
src/libstrongswan/plugins/x509/x509_ocsp_response.c

index e5c39db..159b3eb 100644 (file)
@@ -275,7 +275,7 @@ static bool parse_signedData(private_pkcs7_t *this, x509_t *cacert)
        int digest_alg = OID_UNKNOWN;
        int enc_alg    = OID_UNKNOWN;
        int signerInfos = 0;
-       bool success = TRUE;
+       bool success = FALSE;
 
        chunk_t encrypted_digest = chunk_empty;
 
@@ -304,13 +304,11 @@ static bool parse_signedData(private_pkcs7_t *this, x509_t *cacert)
 
                                if (data == NULL)
                                {
-                                       success = FALSE;
                                        goto end;
                                }
                                if (!data->parse_data(data))
                                {
                                        data->destroy(data);
-                                       success = FALSE;
                                        goto end;
                                }
                                pureData = data->get_data(data);
@@ -356,9 +354,9 @@ static bool parse_signedData(private_pkcs7_t *this, x509_t *cacert)
                                encrypted_digest = object;
                }
        }
+       success = parser->success(parser);
 
 end:
-       success &= parser->success(parser);
        parser->destroy(parser);
        if (!success)
        {
@@ -601,10 +599,9 @@ static bool parse_envelopedData(private_pkcs7_t *this, chunk_t serialNumber,
                                break;
                }
        }
-       success = TRUE;
+       success = parser->success(parser);
 
 end:
-       success &= parser->success(parser);
        parser->destroy(parser);
        if (!success)
        {
@@ -980,7 +977,7 @@ static bool parse_contentInfo(chunk_t blob, u_int level0, private_pkcs7_t *cInfo
        asn1_parser_t *parser;
        chunk_t object;
        int objectID;
-       bool success = TRUE;
+       bool success = FALSE;
 
        parser = asn1_parser_create(contentInfoObjects, PKCS7_INFO_TYPE, blob);
        parser->set_top_level(parser, level0);
@@ -994,7 +991,6 @@ static bool parse_contentInfo(chunk_t blob, u_int level0, private_pkcs7_t *cInfo
                        ||  cInfo->type > OID_PKCS7_ENCRYPTED_DATA)
                        {
                                DBG1("unknown pkcs7 content type");
-                               success = FALSE;
                                goto end;
                        }
                }
@@ -1003,9 +999,9 @@ static bool parse_contentInfo(chunk_t blob, u_int level0, private_pkcs7_t *cInfo
                        cInfo->content = chunk_clone(object);
                }
        }
+       success = parser->success(parser);
 
 end:
-       success &= parser->success(parser);
        parser->destroy(parser);
        return success;
 }
index 42101b0..6b28ae3 100644 (file)
@@ -399,7 +399,7 @@ static bool parse_attributes(chunk_t chunk, int level0, private_pkcs9_t* this)
        chunk_t object;
        int objectID;
        int oid = OID_UNKNOWN;
-       bool success = TRUE;
+       bool success = FALSE;
 
        parser = asn1_parser_create(attributesObjects, ATTRIBUTE_OBJ_ROOF, chunk);
        parser->set_top_level(parser, level0);
@@ -433,16 +433,15 @@ static bool parse_attributes(chunk_t chunk, int level0, private_pkcs9_t* this)
                                                                                parser->get_level(parser)+1,
                                                                                oid_names[oid].name))
                                                {
-                                                       success = FALSE;
                                                        goto end;
                                                }
                                        }
                                }
                }
        }
+       success = parser->success(parser);
 
 end:
-       success &= parser->success(parser);
        parser->destroy(parser);
        return success;
 }
index e28ec3a..3d6053e 100644 (file)
@@ -680,7 +680,7 @@ static gmp_rsa_private_key_t *load(chunk_t blob)
        asn1_parser_t *parser;
        chunk_t object;
        int objectID ;
-       bool success = TRUE;
+       bool success = FALSE;
 
        private_gmp_rsa_private_key_t *this = gmp_rsa_private_key_create_empty();
        
@@ -732,9 +732,9 @@ static gmp_rsa_private_key_t *load(chunk_t blob)
                                break;
                }
        }
+       success = parser->success(parser);
 
 end:
-       success &= parser->success(parser);
        parser->destroy(parser);
        chunk_clear(&blob);
 
index ac48865..b36ddd0 100644 (file)
  */
 extern chunk_t gmp_mpz_to_asn1(const mpz_t value);
 
-/**
- * ASN.1 definition of RSApublicKey
- */
-static const asn1Object_t pubkeyObjects[] = {
-       { 0, "RSAPublicKey",            ASN1_SEQUENCE,     ASN1_OBJ  }, /*  0 */
-       { 1,   "modulus",                       ASN1_INTEGER,      ASN1_BODY }, /*  1 */
-       { 1,   "publicExponent",        ASN1_INTEGER,      ASN1_BODY }, /*  2 */
-};
-#define PUB_KEY_RSA_PUBLIC_KEY         0
-#define PUB_KEY_MODULUS                                1
-#define PUB_KEY_EXPONENT                       2
-#define PUB_KEY_ROOF                           3
-
-/**
- * ASN.1 definition of digestInfo
- */
-static const asn1Object_t digestInfoObjects[] = {
-       { 0, "digestInfo",                      ASN1_SEQUENCE,          ASN1_OBJ  }, /*  0 */
-       { 1,   "digestAlgorithm",       ASN1_EOC,                       ASN1_RAW  }, /*  1 */
-       { 1,   "digest",                        ASN1_OCTET_STRING,      ASN1_BODY }, /*  2 */
-};
-#define DIGEST_INFO                                    0
-#define DIGEST_INFO_ALGORITHM          1
-#define DIGEST_INFO_DIGEST                     2
-#define DIGEST_INFO_ROOF                       3
-
 typedef struct private_gmp_rsa_public_key_t private_gmp_rsa_public_key_t;
 
 /**
@@ -137,6 +111,19 @@ static chunk_t rsavp1(private_gmp_rsa_public_key_t *this, chunk_t data)
 }
 
 /**
+ * ASN.1 definition of digestInfo
+ */
+static const asn1Object_t digestInfoObjects[] = {
+       { 0, "digestInfo",                      ASN1_SEQUENCE,          ASN1_OBJ  }, /*  0 */
+       { 1,   "digestAlgorithm",       ASN1_EOC,                       ASN1_RAW  }, /*  1 */
+       { 1,   "digest",                        ASN1_OCTET_STRING,      ASN1_BODY }, /*  2 */
+};
+#define DIGEST_INFO                                    0
+#define DIGEST_INFO_ALGORITHM          1
+#define DIGEST_INFO_DIGEST                     2
+#define DIGEST_INFO_ROOF                       3
+
+/**
  * Verification of an EMPSA PKCS1 signature described in PKCS#1
  */
 static bool verify_emsa_pkcs1_signature(private_gmp_rsa_public_key_t *this,
@@ -463,6 +450,19 @@ gmp_rsa_public_key_t *gmp_rsa_public_key_create_from_n_e(mpz_t n, mpz_t e)
 }
 
 /**
+ * ASN.1 definition of RSApublicKey
+ */
+static const asn1Object_t pubkeyObjects[] = {
+       { 0, "RSAPublicKey",            ASN1_SEQUENCE,     ASN1_OBJ  }, /*  0 */
+       { 1,   "modulus",                       ASN1_INTEGER,      ASN1_BODY }, /*  1 */
+       { 1,   "publicExponent",        ASN1_INTEGER,      ASN1_BODY }, /*  2 */
+};
+#define PUB_KEY_RSA_PUBLIC_KEY         0
+#define PUB_KEY_MODULUS                                1
+#define PUB_KEY_EXPONENT                       2
+#define PUB_KEY_ROOF                           3
+
+/**
  * Load a public key from an ASN1 encoded blob
  */
 static gmp_rsa_public_key_t *load(chunk_t blob)
@@ -470,7 +470,7 @@ static gmp_rsa_public_key_t *load(chunk_t blob)
        asn1_parser_t *parser;
        chunk_t object;
        int objectID;
-       bool success;
+       bool success = FALSE;
 
        private_gmp_rsa_public_key_t *this = gmp_rsa_public_key_create_empty();
 
@@ -491,9 +491,8 @@ static gmp_rsa_public_key_t *load(chunk_t blob)
                                break;
                }
        }
-
-       free(blob.ptr);
        success = parser->success(parser);
+       free(blob.ptr);
        parser->destroy(parser);
 
        if (!success)
index ee4edb7..e650405 100644 (file)
@@ -359,7 +359,7 @@ static bool parse_certificate(private_x509_ac_t *this)
        int type     = OID_UNKNOWN;
        int extn_oid = OID_UNKNOWN;
        int sig_alg  = OID_UNKNOWN;
-       bool success = TRUE;
+       bool success = FALSE;
        bool critical;
 
        parser = asn1_parser_create(acObjects, AC_OBJ_ROOF, this->encoding);
@@ -379,14 +379,12 @@ static bool parse_certificate(private_x509_ac_t *this)
                                if (this->version != 2)
                                {
                                        DBG1("v%d attribute certificates are not supported", this->version);
-                                       success = FALSE;
                                        goto end;
                                }
                                break;
                        case AC_OBJ_HOLDER_ISSUER:
                                if (!parse_directoryName(object, level, FALSE, &this->holderIssuer))
                                {
-                                       success = FALSE;
                                        goto end;
                                }
                                break;
@@ -396,14 +394,12 @@ static bool parse_certificate(private_x509_ac_t *this)
                        case AC_OBJ_ENTITY_NAME:
                                if (!parse_directoryName(object, level, TRUE, &this->entityName))
                                {
-                                       success = FALSE;
                                        goto end;
                                }
                                break;
                        case AC_OBJ_ISSUER_NAME:
                                if (!parse_directoryName(object, level, FALSE, &this->issuerName))
                                {
-                                       success = FALSE;
                                        goto end;
                                }
                                break;
@@ -492,9 +488,9 @@ static bool parse_certificate(private_x509_ac_t *this)
                                break;
                }
        }
+       success = parser->success(parser);
 
 end:
-       success &= parser->success(parser);
        parser->destroy(parser);
        return success;
 }
index b8046bc..3da7b2a 100644 (file)
@@ -231,7 +231,7 @@ static bool parse_otherName(chunk_t blob, int level0)
        chunk_t object;
        int objectID;
        int oid = OID_UNKNOWN;
-       bool success = TRUE;
+       bool success = FALSE;
 
        parser = asn1_parser_create(otherNameObjects,ON_OBJ_ROOF, blob);
        parser->set_top_level(parser, level0);
@@ -249,7 +249,6 @@ static bool parse_otherName(chunk_t blob, int level0)
                                        if (!asn1_parse_simple_object(&object, ASN1_UTF8STRING,
                                                                parser->get_level(parser)+1, "xmppAddr"))
                                        {
-                                               success = FALSE;
                                                goto end;
                                        }
                                }
@@ -258,9 +257,9 @@ static bool parse_otherName(chunk_t blob, int level0)
                                break;
                }
        }
+       success = parser->success(parser);
 
 end:
-       success &= parser->success(parser);
        parser->destroy(parser);
        return success;
 }
@@ -736,7 +735,7 @@ static bool parse_certificate(private_x509_cert_t *this)
        int extn_oid = OID_UNKNOWN;
        int key_alg  = OID_UNKNOWN;
        int sig_alg  = OID_UNKNOWN;
-       bool success = TRUE;
+       bool success = FALSE;
        bool critical;
        
        parser = asn1_parser_create(certObjects, X509_OBJ_ROOF, this->encoding);
@@ -795,7 +794,6 @@ static bool parse_certificate(private_x509_cert_t *this)
                                                        break;
                                                default:
                                                        DBG1("parsing key type %d failed", key_alg);
-                                                       success = FALSE;
                                                        goto end;
                                        }
                                }
@@ -845,7 +843,9 @@ static bool parse_certificate(private_x509_cert_t *this)
                                        case OID_NS_COMMENT:
                                                if (!asn1_parse_simple_object(&object, ASN1_IA5STRING, 
                                                                                        level, oid_names[extn_oid].name))
-                                                       return FALSE;
+                                               {
+                                                       goto end;
+                                               }
                                                break;
                                        default:
                                                break;
@@ -857,7 +857,6 @@ static bool parse_certificate(private_x509_cert_t *this)
                                if (this->algorithm != sig_alg)
                                {
                                        DBG1("  signature algorithms do not agree");
-                                       success = FALSE;
                                        goto end;
                                }
                                break;
@@ -868,9 +867,9 @@ static bool parse_certificate(private_x509_cert_t *this)
                                break;
                }
        }
+       success = parser->success(parser);
 
 end:
-       success &= parser->success(parser);
        parser->destroy(parser);
        return success;
 }
index 6862caa..ade3e0f 100644 (file)
@@ -200,7 +200,7 @@ static bool parse(private_x509_crl_t *this)
        chunk_t userCertificate = chunk_empty;
        int objectID;
        int sig_alg = OID_UNKNOWN;
-       bool success = TRUE;
+       bool success = FALSE;
        bool critical;
        revoked_t *revoked = NULL;
 
@@ -276,7 +276,6 @@ static bool parse(private_x509_crl_t *this)
                                                if (!asn1_parse_simple_object(&object, ASN1_INTEGER,
                                                                                                          level, "crlNumber"))
                                                {
-                                                       success = FALSE;
                                                        goto end;
                                                }
                                                this->crlNumber = object;
@@ -289,7 +288,6 @@ static bool parse(private_x509_crl_t *this)
                                if (this->algorithm != sig_alg)
                                {
                                        DBG1("  signature algorithms do not agree");
-                                       success = FALSE;
                                        goto end;
                                }
                                break;
@@ -301,9 +299,9 @@ static bool parse(private_x509_crl_t *this)
                                break;
                }
        }
+       success = parser->success(parser);
 
 end:
-       success &= parser->success(parser);
        parser->destroy(parser);
        return success;
 }
index cdac6af..bcfc0f5 100644 (file)
@@ -309,7 +309,7 @@ static bool parse_singleResponse(private_x509_ocsp_response_t *this,
        asn1_parser_t *parser;
        chunk_t object;
        int objectID;
-       bool success;
+       bool success = FALSE;
 
        single_response_t *response;
        
@@ -378,7 +378,6 @@ static bool parse_singleResponse(private_x509_ocsp_response_t *this,
        }
        success = parser->success(parser);
        parser->destroy(parser);
-       
        if (success)
        {
                if (this->usableUntil == UNDEFINED_TIME)
@@ -410,7 +409,7 @@ static bool parse_responses(private_x509_ocsp_response_t *this,
        asn1_parser_t *parser;
        chunk_t object;
        int objectID;
-       bool success = TRUE;
+       bool success = FALSE;
        
        parser = asn1_parser_create(responsesObjects, RESPONSES_ROOF, blob);
        parser->set_top_level(parser, level0);
@@ -423,7 +422,6 @@ static bool parse_responses(private_x509_ocsp_response_t *this,
                                if (!parse_singleResponse(this, object,
                                                                                  parser->get_level(parser)+1))
                                {
-                                       success = FALSE;
                                        goto end;
                                }
                                break;
@@ -431,9 +429,9 @@ static bool parse_responses(private_x509_ocsp_response_t *this,
                                break;
                }
        }
+       success = parser->success(parser);
 
 end:
-       success &= parser->success(parser);
        parser->destroy(parser);
        return success;
 }
@@ -499,7 +497,7 @@ static bool parse_basicOCSPResponse(private_x509_ocsp_response_t *this,
        int extn_oid = OID_UNKNOWN;
        u_int responses_level = level0;
        certificate_t *cert;
-       bool success = TRUE;
+       bool success = FALSE;
        bool critical;
        
        parser = asn1_parser_create(basicResponseObjects, BASIC_RESPONSE_ROOF, blob);
@@ -519,7 +517,6 @@ static bool parse_basicOCSPResponse(private_x509_ocsp_response_t *this,
                                if (version != OCSP_BASIC_RESPONSE_VERSION)
                                {
                                        DBG1("  ocsp ResponseData version %d not supported", version);
-                                       success = FALSE;
                                        goto end;
                                }
                                break;
@@ -575,11 +572,10 @@ static bool parse_basicOCSPResponse(private_x509_ocsp_response_t *this,
                        }
                }
        }
+       success = parser->success(parser);
 
 end:
-       success &= parser->success(parser);
        parser->destroy(parser);
-
        if (success)
        {
                if (!this->responderId)
@@ -655,9 +651,9 @@ static bool parse_OCSPResponse(private_x509_ocsp_response_t *this)
                                break;
                }
        }
+       success = parser->success(parser);
 
 end:
-       success &= parser->success(parser);
        parser->destroy(parser);
        return success;
 }