Revision: 6880
          http://skim-app.svn.sourceforge.net/skim-app/?rev=6880&view=rev
Author:   hofman
Date:     2010-11-09 18:18:26 +0000 (Tue, 09 Nov 2010)

Log Message:
-----------
code fixes to dSA verifier

Modified Paths:
--------------
    trunk/vendorsrc/andymatuschak/Sparkle/SUDSAVerifier.m

Modified: trunk/vendorsrc/andymatuschak/Sparkle/SUDSAVerifier.m
===================================================================
--- trunk/vendorsrc/andymatuschak/Sparkle/SUDSAVerifier.m       2010-11-07 
00:06:27 UTC (rev 6879)
+++ trunk/vendorsrc/andymatuschak/Sparkle/SUDSAVerifier.m       2010-11-09 
18:18:26 UTC (rev 6880)
@@ -17,7 +17,7 @@
 #import <openssl/rsa.h>
 #import <openssl/sha.h>
 
-long b64decode(unsigned char* str)
+static long b64decode(unsigned char* str)
 {
     unsigned char *cur, *start;
     int d, dlast, phase;
@@ -41,12 +41,12 @@
         -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1   /* F0-FF */
     };
        
-    d = dlast = phase = 0;
+    dlast = phase = 0;
     start = str;
     for (cur = str; *cur != '\0'; ++cur )
     {
                if(*cur == '\n' || *cur == '\r'){phase = dlast = 0; continue;}
-        d = table[(int)*cur];
+        d = table[(unsigned int)*cur];
         if(d != -1)
         {
             switch(phase)
@@ -77,26 +77,30 @@
     return str - start;
 }
 
-EVP_PKEY* load_dsa_key(char *key)
+static EVP_PKEY* load_dsa_key(char *key)
 {
        EVP_PKEY* pkey = NULL;
-       BIO *bio;
-       if((bio = BIO_new_mem_buf(key, (int)strlen(key))))
+       size_t keylen = strlen(key);
+       if (keylen <= INT_MAX)
        {
-               DSA* dsa_key = 0;
-               if(PEM_read_bio_DSA_PUBKEY(bio, &dsa_key, NULL, NULL))
+               BIO *bio;
+               if((bio = BIO_new_mem_buf(key, (int)keylen)))
                {
-                       if((pkey = EVP_PKEY_new()))
+                       DSA* dsa_key = 0;
+                       if(PEM_read_bio_DSA_PUBKEY(bio, &dsa_key, NULL, NULL))
                        {
-                               if(EVP_PKEY_assign_DSA(pkey, dsa_key) != 1)
+                               if((pkey = EVP_PKEY_new()))
                                {
-                                       DSA_free(dsa_key);
-                                       EVP_PKEY_free(pkey);
-                                       pkey = NULL;
+                                       if(EVP_PKEY_assign_DSA(pkey, dsa_key) 
!= 1)
+                                       {
+                                               DSA_free(dsa_key);
+                                               EVP_PKEY_free(pkey);
+                                               pkey = NULL;
+                                       }
                                }
                        }
+                       BIO_free(bio);
                }
-               BIO_free(bio);
        }
        return pkey;
 }
@@ -119,13 +123,17 @@
        }
        pkeyString = [pkeyTrimmedLines componentsJoinedByString:@"\n"]; // Put 
them back together.
 
+       NSMutableData* pkeyData = [[[pkeyString 
dataUsingEncoding:NSUTF8StringEncoding] mutableCopy] autorelease];
+       void *pkeyBytes = [pkeyData mutableBytes];
        EVP_PKEY* pkey = NULL;
-       pkey = load_dsa_key((char *)[pkeyString UTF8String]);
+       pkey = load_dsa_key(pkeyBytes);
        if (!pkey) { return NO; }
 
        // Now, the signature is in base64; we have to decode it into a binary 
stream.
-       unsigned char *signature = (unsigned char *)[encodedSignature 
UTF8String];
+       NSMutableData* signatureData = [[[encodedSignature 
dataUsingEncoding:NSUTF8StringEncoding] mutableCopy] autorelease];
+       void *signature = [signatureData mutableBytes];
        long length = b64decode(signature); // Decode the signature in-place 
and get the new length of the signature string.
+       if ((length < 0) || (length > 0x7FFFFFFF)) { return NO; } // test 
before cast below
        
        // We've got the signature, now get the file data.
        NSData *pathData = [NSData dataWithContentsOfFile:path];
@@ -137,14 +145,23 @@
        
        // Actually verify the signature on the file.
        EVP_MD_CTX ctx;
+       EVP_MD_CTX_init(&ctx);
        if(EVP_VerifyInit(&ctx, EVP_dss1()) == 1) // We're using DSA keys.
        {
                EVP_VerifyUpdate(&ctx, md, SHA_DIGEST_LENGTH);
-               result = (EVP_VerifyFinal(&ctx, signature, (unsigned 
int)length, pkey) == 1);
+               
+               result = EVP_VerifyFinal(&ctx, signature, (unsigned int)length, 
pkey);
        }
+       EVP_MD_CTX_cleanup(&ctx);
        
        EVP_PKEY_free(pkey);
-       return result;
+       
+       // Prevent these from being collected earlier than we want (our only 
reference is an inner pointer).
+       [pkeyData self];
+       [signatureData self];
+       [pathData self];
+       
+       return result == 1;
 }
 
 @end


This was sent by the SourceForge.net collaborative development platform, the 
world's largest Open Source development site.

------------------------------------------------------------------------------
The Next 800 Companies to Lead America's Growth: New Video Whitepaper
David G. Thomson, author of the best-selling book "Blueprint to a 
Billion" shares his insights and actions to help propel your 
business during the next growth cycle. Listen Now!
http://p.sf.net/sfu/SAP-dev2dev
_______________________________________________
Skim-app-commit mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/skim-app-commit

Reply via email to