@@ -4,7 +4,8 @@ import trailofbits.crypto.common
44// BIGNUM *BN_new(void);
55class BN_new extends CustomAllocator {
66 BN_new ( ) {
7- this .getQualifiedName ( ) = "BN_new" and (
7+ this .getQualifiedName ( ) = "BN_new" and
8+ (
89 dealloc instanceof BN_free or
910 dealloc instanceof BN_clear_free
1011 )
@@ -14,7 +15,8 @@ class BN_new extends CustomAllocator {
1415// BIGNUM *BN_secure_new(void);
1516class BN_secure_new extends CustomAllocator {
1617 BN_secure_new ( ) {
17- this .getQualifiedName ( ) = "BN_secure_new" and (
18+ this .getQualifiedName ( ) = "BN_secure_new" and
19+ (
1820 dealloc instanceof BN_free or
1921 dealloc instanceof BN_clear_free
2022 )
@@ -23,24 +25,16 @@ class BN_secure_new extends CustomAllocator {
2325
2426// void BN_free(BIGNUM *a);
2527class BN_free extends CustomDeallocator {
26- BN_free ( ) {
27- this .getQualifiedName ( ) = "BN_free"
28- }
28+ BN_free ( ) { this .getQualifiedName ( ) = "BN_free" }
2929
30- override int getPointer ( ) {
31- result = 0
32- }
30+ override int getPointer ( ) { result = 0 }
3331}
3432
3533// void BN_clear_free(BIGNUM *a);
3634class BN_clear_free extends CustomDeallocator {
37- BN_clear_free ( ) {
38- this .getQualifiedName ( ) = "BN_clear_free"
39- }
35+ BN_clear_free ( ) { this .getQualifiedName ( ) = "BN_clear_free" }
4036
41- override int getPointer ( ) {
42- result = 0
43- }
37+ override int getPointer ( ) { result = 0 }
4438}
4539
4640// void BN_clear(BIGNUM *a);
@@ -50,18 +44,63 @@ class BN_clear extends FunctionCall {
5044 Expr getBignum ( ) { result = this .getArgument ( 0 ) }
5145}
5246
53- // int BN_rand(BIGNUM *rnd, int bits, int top, int bottom);
47+ // int BN_rand(BIGNUM *rnd, int bits, int top, int bottom); (and variants)
48+ /// Reference: https://docs.openssl.org/master/man3/BN_rand/#synopsis
5449class BN_rand extends FunctionCall {
55- BN_rand ( ) { this . getTarget ( ) . getName ( ) = "BN_rand" }
56-
57- Expr getBignum ( ) {
58- result = this .getArgument ( 0 )
50+ BN_rand ( ) {
51+ this . getTarget ( ) . getName ( ) . matches ( "BN\\_rand%" ) or
52+ this . getTarget ( ) . getName ( ) . matches ( "BN\\_priv\\_rand%" ) or
53+ this .getTarget ( ) . getName ( ) . matches ( "BN\\_pseudo\\_rand%" )
5954 }
55+
56+ Expr getBignum ( ) { result = this .getArgument ( 0 ) }
6057}
6158
6259class BIGNUM extends FunctionCall {
63- BIGNUM ( ) {
60+ BIGNUM ( ) {
6461 this .getTarget ( ) instanceof BN_new or
6562 this .getTarget ( ) instanceof BN_secure_new
6663 }
6764}
65+
66+ // BN_CTX *BN_CTX_new(void);
67+ class BN_CTX_new extends CustomAllocator {
68+ BN_CTX_new ( ) {
69+ this .getQualifiedName ( ) .matches ( "BN\\_CTX_new%" )
70+ or
71+ this .getQualifiedName ( ) .matches ( "BN\\_CTX\\_secure\\_new%" ) and
72+ dealloc instanceof BN_CTX_free
73+ }
74+ }
75+
76+ // void BN_CTX_free(BN_CTX *c);
77+ class BN_CTX_free extends CustomDeallocator {
78+ BN_CTX_free ( ) { this .getQualifiedName ( ) = "BN_CTX_free" }
79+
80+ override int getPointer ( ) { result = 0 }
81+ }
82+
83+ // void BN_CTX_start(BN_CTX *ctx);
84+ class BN_CTX_start extends FunctionCall {
85+ BN_CTX_start ( ) { this .getTarget ( ) .getName ( ) = "BN_CTX_start" }
86+
87+ Expr getContext ( ) { result = this .getArgument ( 0 ) }
88+ }
89+
90+ // void BN_CTX_end(BN_CTX *ctx);
91+ class BN_CTX_end extends FunctionCall {
92+ BN_CTX_end ( ) { this .getTarget ( ) .getName ( ) = "BN_CTX_end" }
93+
94+ Expr getContext ( ) { result = this .getArgument ( 0 ) }
95+ }
96+
97+ // BIGNUM *BN_CTX_get(BN_CTX *ctx);
98+ class BN_CTX_get extends FunctionCall {
99+ BN_CTX_get ( ) { this .getTarget ( ) .getName ( ) = "BN_CTX_get" }
100+
101+ Expr getContext ( ) { result = this .getArgument ( 0 ) }
102+ }
103+
104+ class BN_CTX extends FunctionCall {
105+ BN_CTX ( ) { this .getTarget ( ) instanceof BN_CTX_new }
106+ }
0 commit comments