Basic operations such as BIO's statement and release
--- Relationship according to OpenSSL DOC / CRYPTO / BIO / BIO_NEW.POD translation and its own understanding
(Author: DragonKing Mail: wzhah@263.net Posted: gdwzh.126.com openssl professional forum)
In the basic operating range of BIOs, they use BIO allocation and release operations, including:
BIO_NEW, BIO_SET, BIO_FREE, BIO_VFREE, BIO_FREE_ALL
Their statement is in the openssl / bio.h file, its statement is as follows:
BIO * BIO_NEW (BIO_METHOD * TYPE);
INT BIO_SET (BIO * A, BIO_METHOD * TYPE);
INT BIO_FREE (BIO * A);
Void Bio_vfree (BIO * a);
Void Bio_Free_all (BIO * A);
These functions are explained below.
[BIO_NEW]
This function creates and returns a corresponding new BIO, and calls the following BIO_SET () function to assign a value of the method of the BIO structure according to a given BIO_METHOD type, if you create or give the Method assignment failure, return NULL. The BIO example of creating a Memory type is as follows:
BIO * MEM = BIO_NEW (BIO_S_MEM ());
Some types of BIOs can be used directly, such as the Memory type BIO; and some of the BIO creation requires some initialization work, such as file BIO, in general, some functions are also provided to create And initialize this type of Bio.
What does this mean, give a simple example, you will understand:
For example, create a file BIO, use the following code:
BIO * IN = NULL;
IN = BIO_NEW (BIO_S_FILE ());
BIO_READ_FILENAME (IN, "RSA512.pem");
In this way, Bio IN can only be used, and if it is a Bio that creates a Memory type, just need to be as follows:
BIO * MEM = BIO_NEW (BIO_S_MEM ());
The Bio MEM can then be operated.
In addition, it is necessary to supplement (this may have already realized the two articles from the previous article). For the Source / Sink type BIO, its type creation function is typically in the form of BIO_S_ *, for the Filter type function, its type creates a function In the form of BIO_F_ *.
[BIO_SET]
This function is relatively simple, which is a new Bio_Method type for an existing BIO. In fact, it is simple to initialize the various members of the BIO and assign the parameter TYPE to the BIO. In fact, the BIO_NEW function is simply invoke the BIO_SET function to initialize the BIO_set function after using OpenSSL_Malloc to assign memory to the BIO. So, in general, unless you want to reset the Bio already existing, you don't need to call this function directly. Successful operation returns 1, otherwise it returns 0.
[BIO_FREE]
This function releases a single BIO memory and resource, successful operation returns 1. Failure returns 0. The BIO operation is not only the resource occupied by the BIO structure, but also releases the I / O resources in its lower layer, such as closing the release-related files, etc., which is different for different types of BIO. For details, please refer to each Type BIO itself's documentation and source files. It should be noted that BIO_FREE only releases a current Bio, if it is used to release a BIO chain, it may cause memory leaks, which should use the BIO_FREE_ALL function below. [BIO_VFREE]
This function function is exactly the same as BIO_FREE, but does not return a value. In fact, it simply calls the BIO_FREE function, but does not return the return value of the function, so its function implements the code only one statement.
[BIO_FREE_ALL]
This function releases this BIO chain, and even in this process, if the release process is released, the release process will not stop, and will continue to release the following BIO, which guarantees to avoid the occurrence of memory leaks. If you want to call this function to release a single Bio, then the effect is the same as BIO_FREE. In fact, the function is just simply traversing the entire Bio chain and calls Bio_Free release the BIO of each link.
With regard to the basic creation and release of BIO, you will introduce it here. You are welcome to have questions to gdwzh.126.com.