Skip to content

Commit 566095d

Browse files
committed
implement checkMajorVersion
1 parent 3f800cf commit 566095d

File tree

4 files changed

+19
-18
lines changed

4 files changed

+19
-18
lines changed

dsa.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -196,9 +196,8 @@ func newDSA(params DSAParameters, X, Y BigInt) (C.GO_EVP_PKEY_PTR, error) {
196196
}
197197

198198
func newDSA1(params DSAParameters, X, Y BigInt) (C.GO_EVP_PKEY_PTR, error) {
199-
if vMajor != 1 {
200-
panic("incorrect vMajor version")
201-
}
199+
checkMajorVersion(1)
200+
202201
dsa := C.go_openssl_DSA_new()
203202
p, q, g := bigToBN(params.P), bigToBN(params.Q), bigToBN(params.G)
204203
var ret C.int
@@ -247,9 +246,8 @@ func newDSA1(params DSAParameters, X, Y BigInt) (C.GO_EVP_PKEY_PTR, error) {
247246
}
248247

249248
func newDSA3(params DSAParameters, X, Y BigInt) (C.GO_EVP_PKEY_PTR, error) {
250-
if vMajor != 3 {
251-
panic("incorrect vMajor version")
252-
}
249+
checkMajorVersion(3)
250+
253251
bld := C.go_openssl_OSSL_PARAM_BLD_new()
254252
if bld == nil {
255253
return nil, newOpenSSLError("OSSL_PARAM_BLD_new")

ecdh.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -127,9 +127,8 @@ func newECDHPkey(curve string, bytes []byte, isPrivate bool) (C.GO_EVP_PKEY_PTR,
127127
}
128128

129129
func newECDHPkey1(nid C.int, bytes []byte, isPrivate bool) (pkey C.GO_EVP_PKEY_PTR, err error) {
130-
if vMajor != 1 {
131-
panic("incorrect vMajor version")
132-
}
130+
checkMajorVersion(1)
131+
133132
key := C.go_openssl_EC_KEY_new_by_curve_name(nid)
134133
if key == nil {
135134
return nil, newOpenSSLError("EC_KEY_new_by_curve_name")
@@ -166,9 +165,8 @@ func newECDHPkey1(nid C.int, bytes []byte, isPrivate bool) (pkey C.GO_EVP_PKEY_P
166165
}
167166

168167
func newECDHPkey3(nid C.int, bytes []byte, isPrivate bool) (C.GO_EVP_PKEY_PTR, error) {
169-
if vMajor != 3 {
170-
panic("incorrect vMajor version")
171-
}
168+
checkMajorVersion(3)
169+
172170
bld := C.go_openssl_OSSL_PARAM_BLD_new()
173171
if bld == nil {
174172
return nil, newOpenSSLError("OSSL_PARAM_BLD_new")

ecdsa.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -149,9 +149,8 @@ func newECDSAKey(curve string, X, Y, D BigInt) (C.GO_EVP_PKEY_PTR, error) {
149149
}
150150

151151
func newECDSAKey1(nid C.int, bx, by, bd C.GO_BIGNUM_PTR) (pkey C.GO_EVP_PKEY_PTR, err error) {
152-
if vMajor != 1 {
153-
panic("incorrect vMajor version")
154-
}
152+
checkMajorVersion(1)
153+
155154
key := C.go_openssl_EC_KEY_new_by_curve_name(nid)
156155
if key == nil {
157156
return nil, newOpenSSLError("EC_KEY_new_by_curve_name failed")
@@ -171,9 +170,8 @@ func newECDSAKey1(nid C.int, bx, by, bd C.GO_BIGNUM_PTR) (pkey C.GO_EVP_PKEY_PTR
171170
}
172171

173172
func newECDSAKey3(nid C.int, bx, by, bd C.GO_BIGNUM_PTR) (C.GO_EVP_PKEY_PTR, error) {
174-
if vMajor != 3 {
175-
panic("incorrect vMajor version")
176-
}
173+
checkMajorVersion(3)
174+
177175
// Create the encoded public key public key from bx and by.
178176
pubBytes, err := generateAndEncodeEcPublicKey(nid, func(group C.GO_EC_GROUP_PTR) (C.GO_EC_POINT_PTR, error) {
179177
pt := C.go_openssl_EC_POINT_new(group)

openssl.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,13 @@ func errUnsupportedVersion() error {
7777
return errors.New("openssl: OpenSSL version: " + utoa(vMajor) + "." + utoa(vMinor) + "." + utoa(vPatch))
7878
}
7979

80+
// checkMajorVersion panics if the current major version is not expected.
81+
func checkMajorVersion(expected uint) {
82+
if vMajor != expected {
83+
panic("openssl: incorrect major version (" + strconv.Itoa(int(expected)) + "), expected " + strconv.Itoa(int(expected)))
84+
}
85+
}
86+
8087
type fail string
8188

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

0 commit comments

Comments
 (0)