Skip to content

Commit d70ecb7

Browse files
committed
Update GmSSL v3.2.0 compatibility
1 parent 81e5569 commit d70ecb7

5 files changed

Lines changed: 67 additions & 10 deletions

File tree

.github/workflows/ci.yml

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
name: CI
2+
3+
on:
4+
push:
5+
branches: [main, master]
6+
pull_request:
7+
branches: [main, master]
8+
workflow_dispatch:
9+
10+
jobs:
11+
test:
12+
name: Go ${{ matrix.go-version }} on ${{ matrix.os }}
13+
runs-on: ${{ matrix.os }}
14+
strategy:
15+
fail-fast: false
16+
matrix:
17+
os: [ubuntu-22.04, macos-14]
18+
go-version: ["1.21", "1.22", "1.23"]
19+
20+
steps:
21+
- name: Checkout
22+
uses: actions/checkout@v4
23+
24+
- name: Setup Go
25+
uses: actions/setup-go@v5
26+
with:
27+
go-version: ${{ matrix.go-version }}
28+
29+
- name: Install build dependencies
30+
run: |
31+
if [ "$RUNNER_OS" = "Linux" ]; then
32+
sudo apt-get update
33+
sudo apt-get install -y build-essential cmake
34+
else
35+
brew install cmake
36+
fi
37+
38+
- name: Build and install GmSSL v3.2.0
39+
run: |
40+
git clone --depth 1 --branch v3.2.0 https://github.com/guanzhi/GmSSL.git "$RUNNER_TEMP/GmSSL"
41+
cmake -S "$RUNNER_TEMP/GmSSL" -B "$RUNNER_TEMP/GmSSL/build" \
42+
-DCMAKE_BUILD_TYPE=Release
43+
cmake --build "$RUNNER_TEMP/GmSSL/build" --parallel 4
44+
sudo cmake --install "$RUNNER_TEMP/GmSSL/build"
45+
if [ "$RUNNER_OS" = "Linux" ]; then
46+
sudo ldconfig
47+
fi
48+
49+
- name: Run tests
50+
env:
51+
CGO_CFLAGS: -I/usr/local/include
52+
CGO_LDFLAGS: -L/usr/local/lib -Wl,-rpath,/usr/local/lib -lgmssl
53+
LD_LIBRARY_PATH: /usr/local/lib
54+
DYLD_LIBRARY_PATH: /usr/local/lib
55+
run: go test ./...

cert.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -197,10 +197,16 @@ func (cert *Sm2Certificate) GetSubject() ([]byte, map[string]string, error) {
197197
func (cert *Sm2Certificate) GetSubjectPublicKey() (*Sm2Key, error) {
198198

199199
ret := new(Sm2Key)
200+
var x509_key C.X509_KEY
200201

201-
if C.x509_cert_get_subject_public_key(cert.cert, cert.certlen, &ret.sm2_key) != 1 {
202+
if C.x509_cert_get_subject_public_key(cert.cert, cert.certlen, &x509_key) != 1 {
202203
return nil, errors.New("Libgmssl inner error")
203204
}
205+
defer C.x509_key_cleanup(&x509_key)
206+
if x509_key.algor != C.OID_ec_public_key || x509_key.algor_param != C.OID_sm2 {
207+
return nil, errors.New("Certificate subject public key is not an SM2 key")
208+
}
209+
C.memcpy(unsafe.Pointer(&ret.sm2_key), unsafe.Pointer(&x509_key.u), C.sizeof_SM2_KEY)
204210
ret.has_private_key = false
205211

206212
return ret, nil
@@ -218,4 +224,3 @@ func (cert *Sm2Certificate) VerifyByCaCertificate(ca_cert *Sm2Certificate, sm2_i
218224
return true
219225
}
220226

221-

sm2.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,7 @@ func (sm2 *Sm2Key) Decrypt(in []byte) ([]byte, error) {
195195

196196
type Sm2Signature struct {
197197
sm2_sign_ctx C.SM2_SIGN_CTX
198+
sm2_verify_ctx C.SM2_VERIFY_CTX
198199
sign bool
199200
}
200201

@@ -216,7 +217,7 @@ func NewSm2Signature(sm2 *Sm2Key, id string, sign bool) (*Sm2Signature, error) {
216217
return nil, errors.New("Libgmssl inner error")
217218
}
218219
} else {
219-
if C.sm2_verify_init(&ret.sm2_sign_ctx, &sm2.sm2_key, id_str, C.strlen(id_str)) != 1 {
220+
if C.sm2_verify_init(&ret.sm2_verify_ctx, &sm2.sm2_key, id_str, C.strlen(id_str)) != 1 {
220221
return nil, errors.New("Libgmssl inner error")
221222
}
222223
}
@@ -234,7 +235,7 @@ func (sig *Sm2Signature) Update(data []byte) error {
234235
return errors.New("Libgmssl inner error")
235236
}
236237
} else {
237-
if C.sm2_verify_update(&sig.sm2_sign_ctx, (*C.uchar)(unsafe.Pointer(&data[0])), C.size_t(len(data))) != 1 {
238+
if C.sm2_verify_update(&sig.sm2_verify_ctx, (*C.uchar)(unsafe.Pointer(&data[0])), C.size_t(len(data))) != 1 {
238239
return errors.New("Libgmssl inner error")
239240
}
240241
}
@@ -257,11 +258,10 @@ func (sig *Sm2Signature) Verify(signature []byte) bool {
257258
if sig.sign != false {
258259
return false
259260
}
260-
if C.sm2_verify_finish(&sig.sm2_sign_ctx, (*C.uchar)(unsafe.Pointer(&signature[0])), C.size_t(len(signature))) != 1 {
261+
if C.sm2_verify_finish(&sig.sm2_verify_ctx, (*C.uchar)(unsafe.Pointer(&signature[0])), C.size_t(len(signature))) != 1 {
261262
return false
262263
}
263264
return true
264265
}
265266

266267

267-

sm3.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,11 +120,10 @@ func Sm3Pbkdf2(pass string, salt []byte, iter uint, keylen uint) ([]byte, error)
120120

121121
key := make([]byte, keylen)
122122

123-
C.pbkdf2_hmac_sm3_genkey(pass_str, C.strlen(pass_str),
123+
C.sm3_pbkdf2(pass_str, C.strlen(pass_str),
124124
(*C.uchar)(unsafe.Pointer(&salt[0])), C.size_t(len(salt)),
125125
C.size_t(iter), C.size_t(keylen),
126126
(*C.uchar)(unsafe.Pointer(&key[0])))
127127

128128
return key, nil
129129
}
130-

sm4.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ package gmssl
1414
#include <string.h>
1515
#include <gmssl/sm4.h>
1616
#include <gmssl/mem.h>
17-
#include <gmssl/aead.h>
1817
#include <gmssl/error.h>
1918
*/
2019
import "C"
@@ -351,4 +350,3 @@ func (gcm *Sm4Gcm) Finish() ([]byte, error) {
351350
}
352351
return outbuf[:outlen], nil
353352
}
354-

0 commit comments

Comments
 (0)