Off-by-one-error corrected.
[openssl.git] / crypto / cryptlib.c
index 5da4828f88a2640b68c03811a54de1438cfa2c52..26c1da7074f985802a3192827d7ce227b6aae1e7 100644 (file)
@@ -1,4 +1,57 @@
 /* crypto/cryptlib.c */
+/* ====================================================================
+ * Copyright (c) 1998-2002 The OpenSSL Project.  All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer. 
+ *
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in
+ *    the documentation and/or other materials provided with the
+ *    distribution.
+ *
+ * 3. All advertising materials mentioning features or use of this
+ *    software must display the following acknowledgment:
+ *    "This product includes software developed by the OpenSSL Project
+ *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
+ *
+ * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
+ *    endorse or promote products derived from this software without
+ *    prior written permission. For written permission, please contact
+ *    openssl-core@openssl.org.
+ *
+ * 5. Products derived from this software may not be called "OpenSSL"
+ *    nor may "OpenSSL" appear in their names without prior written
+ *    permission of the OpenSSL Project.
+ *
+ * 6. Redistributions of any form whatsoever must retain the following
+ *    acknowledgment:
+ *    "This product includes software developed by the OpenSSL Project
+ *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
+ * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
+ * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
+ * OF THE POSSIBILITY OF SUCH DAMAGE.
+ * ====================================================================
+ *
+ * This product includes cryptographic software written by Eric Young
+ * (eay@cryptsoft.com).  This product includes software written by Tim
+ * Hudson (tjh@cryptsoft.com).
+ *
+ */
 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  * All rights reserved.
  *
  * copied and put under another distribution licence
  * [including the GNU Public Licence.]
  */
+/* ====================================================================
+ * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
+ * ECDH support in OpenSSL originally developed by 
+ * SUN MICROSYSTEMS, INC., and contributed to the OpenSSL project.
+ */
 
 #include <stdio.h>
 #include <string.h>
@@ -74,7 +132,7 @@ static const char* lock_names[CRYPTO_NUM_LOCKS] =
        {
        "<<ERROR>>",
        "err",
-       "err_hash",
+       "ex_data",
        "x509",
        "x509_info",
        "x509_pkey",
@@ -89,7 +147,9 @@ static const char* lock_names[CRYPTO_NUM_LOCKS] =
        "ssl_session",
        "ssl_sess_cert",
        "ssl",
+       "ssl_method",
        "rand",
+       "rand2",
        "debug_malloc",
        "BIO",
        "gethostbyname",
@@ -102,7 +162,11 @@ static const char* lock_names[CRYPTO_NUM_LOCKS] =
        "dynlock",
        "engine",
        "ui",
-#if CRYPTO_NUM_LOCKS != 30
+       "ecdsa",
+       "ec",
+       "ecdh",
+       "bn",
+#if CRYPTO_NUM_LOCKS != 36
 # error "Inconsistency between crypto.h and cryptlib.c"
 #endif
        };
@@ -229,7 +293,10 @@ void CRYPTO_destroy_dynlockid(int i)
        CRYPTO_w_lock(CRYPTO_LOCK_DYNLOCK);
 
        if (dyn_locks == NULL || i >= sk_CRYPTO_dynlock_num(dyn_locks))
+               {
+               CRYPTO_w_unlock(CRYPTO_LOCK_DYNLOCK);
                return;
+               }
        pointer = sk_CRYPTO_dynlock_value(dyn_locks, i);
        if (pointer != NULL)
                {
@@ -397,16 +464,15 @@ void CRYPTO_lock(int mode, int type, const char *file, int line)
 #endif
        if (type < 0)
                {
-               int i = -type - 1;
                struct CRYPTO_dynlock_value *pointer
-                       = CRYPTO_get_dynlock_value(i);
+                       = CRYPTO_get_dynlock_value(type);
 
                if (pointer && dynlock_lock_callback)
                        {
                        dynlock_lock_callback(mode, pointer, file, line);
                        }
 
-               CRYPTO_destroy_dynlockid(i);
+               CRYPTO_destroy_dynlockid(type);
                }
        else
                if (locking_callback != NULL)
@@ -432,7 +498,6 @@ int CRYPTO_add_lock(int *pointer, int amount, int type, const char *file,
                        CRYPTO_get_lock_name(type),
                        file,line);
 #endif
-               *pointer=ret;
                }
        else
                {
@@ -458,7 +523,7 @@ const char *CRYPTO_get_lock_name(int type)
                return("dynamic");
        else if (type < CRYPTO_NUM_LOCKS)
                return(lock_names[type]);
-       else if (type-CRYPTO_NUM_LOCKS >= sk_num(app_locks))
+       else if (type-CRYPTO_NUM_LOCKS > sk_num(app_locks))
                return("ERROR");
        else
                return(sk_value(app_locks,type-CRYPTO_NUM_LOCKS));