Skip to content

Commit

Permalink
implement checkMajorVersion
Browse files Browse the repository at this point in the history
  • Loading branch information
qmuntal committed Sep 3, 2024
1 parent 3f800cf commit 566095d
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 18 deletions.
10 changes: 4 additions & 6 deletions dsa.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,9 +196,8 @@ func newDSA(params DSAParameters, X, Y BigInt) (C.GO_EVP_PKEY_PTR, error) {
}

func newDSA1(params DSAParameters, X, Y BigInt) (C.GO_EVP_PKEY_PTR, error) {
if vMajor != 1 {
panic("incorrect vMajor version")
}
checkMajorVersion(1)

dsa := C.go_openssl_DSA_new()
p, q, g := bigToBN(params.P), bigToBN(params.Q), bigToBN(params.G)
var ret C.int
Expand Down Expand Up @@ -247,9 +246,8 @@ func newDSA1(params DSAParameters, X, Y BigInt) (C.GO_EVP_PKEY_PTR, error) {
}

func newDSA3(params DSAParameters, X, Y BigInt) (C.GO_EVP_PKEY_PTR, error) {
if vMajor != 3 {
panic("incorrect vMajor version")
}
checkMajorVersion(3)

bld := C.go_openssl_OSSL_PARAM_BLD_new()
if bld == nil {
return nil, newOpenSSLError("OSSL_PARAM_BLD_new")
Expand Down
10 changes: 4 additions & 6 deletions ecdh.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,8 @@ func newECDHPkey(curve string, bytes []byte, isPrivate bool) (C.GO_EVP_PKEY_PTR,
}

func newECDHPkey1(nid C.int, bytes []byte, isPrivate bool) (pkey C.GO_EVP_PKEY_PTR, err error) {
if vMajor != 1 {
panic("incorrect vMajor version")
}
checkMajorVersion(1)

key := C.go_openssl_EC_KEY_new_by_curve_name(nid)
if key == nil {
return nil, newOpenSSLError("EC_KEY_new_by_curve_name")
Expand Down Expand Up @@ -166,9 +165,8 @@ func newECDHPkey1(nid C.int, bytes []byte, isPrivate bool) (pkey C.GO_EVP_PKEY_P
}

func newECDHPkey3(nid C.int, bytes []byte, isPrivate bool) (C.GO_EVP_PKEY_PTR, error) {
if vMajor != 3 {
panic("incorrect vMajor version")
}
checkMajorVersion(3)

bld := C.go_openssl_OSSL_PARAM_BLD_new()
if bld == nil {
return nil, newOpenSSLError("OSSL_PARAM_BLD_new")
Expand Down
10 changes: 4 additions & 6 deletions ecdsa.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,9 +149,8 @@ func newECDSAKey(curve string, X, Y, D BigInt) (C.GO_EVP_PKEY_PTR, error) {
}

func newECDSAKey1(nid C.int, bx, by, bd C.GO_BIGNUM_PTR) (pkey C.GO_EVP_PKEY_PTR, err error) {
if vMajor != 1 {
panic("incorrect vMajor version")
}
checkMajorVersion(1)

key := C.go_openssl_EC_KEY_new_by_curve_name(nid)
if key == nil {
return nil, newOpenSSLError("EC_KEY_new_by_curve_name failed")
Expand All @@ -171,9 +170,8 @@ func newECDSAKey1(nid C.int, bx, by, bd C.GO_BIGNUM_PTR) (pkey C.GO_EVP_PKEY_PTR
}

func newECDSAKey3(nid C.int, bx, by, bd C.GO_BIGNUM_PTR) (C.GO_EVP_PKEY_PTR, error) {
if vMajor != 3 {
panic("incorrect vMajor version")
}
checkMajorVersion(3)

// Create the encoded public key public key from bx and by.
pubBytes, err := generateAndEncodeEcPublicKey(nid, func(group C.GO_EC_GROUP_PTR) (C.GO_EC_POINT_PTR, error) {
pt := C.go_openssl_EC_POINT_new(group)
Expand Down
7 changes: 7 additions & 0 deletions openssl.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,13 @@ func errUnsupportedVersion() error {
return errors.New("openssl: OpenSSL version: " + utoa(vMajor) + "." + utoa(vMinor) + "." + utoa(vPatch))
}

// checkMajorVersion panics if the current major version is not expected.
func checkMajorVersion(expected uint) {
if vMajor != expected {
panic("openssl: incorrect major version (" + strconv.Itoa(int(expected)) + "), expected " + strconv.Itoa(int(expected)))
}
}

type fail string

func (e fail) Error() string { return "openssl: " + string(e) + " failed" }
Expand Down

0 comments on commit 566095d

Please sign in to comment.