Skip to content

Commit 595d9c5

Browse files
committed
Updated the signature padding method and added examples.
This commit mainly updates the salt length of PSS to be the same as the hash value, and also adds examples for Python and TypeScript. Additionally, fixed a bug in the server.dockerfile.
1 parent 6885a72 commit 595d9c5

10 files changed

Lines changed: 90 additions & 29 deletions

File tree

Dockerfile.server

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@ LABEL description="This is the server image used by QuickCertS."
1515

1616
COPY --from=builder /app/server /app/server
1717

18-
COPY ./configs/allowlist.toml /app/configs/allowlist.toml
19-
COPY ./configs/database.toml /app/configs/database.toml
20-
COPY ./configs/server.toml /app/configs/server.toml
21-
COPY ./local /app/local
22-
COPY ./logs /app/logs
18+
COPY --from=builder /app/configs/allowlist.toml /app/configs/allowlist.toml
19+
COPY --from=builder /app/configs/database.toml /app/configs/database.toml
20+
COPY --from=builder /app/configs/server.toml /app/configs/server.toml
21+
COPY --from=builder /app/local /app/local
22+
COPY --from=builder /app/logs /app/logs
2323

2424
RUN chmod +x /app/server
2525

README-zhHans.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# QuickCerts
1+
# QuickCertS
22

33
## 语言
44

README-zhHant.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# QuickCerts
1+
# QuickCertS
22

33
## 語言
44

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# QuickCerts
1+
# QuickCertS
22

33
## Language
44

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
package main
22

3+
// This is an example for verifying the signature by the given public key.
4+
// Used arguments:
5+
// hash method: SHA3-512
6+
// PSS salt length: rsa.PSSSaltLengthEqualsHash
7+
// Message: 95e156395687128711f29b68fbc44573667bdfc5f0d65010cb0555b62138d830
8+
// Signature: upagNzGSL3ZqCsxApgG8yiG/x1c+ZZBJgNtzvZR2KYVLP60+hAr5WcnZ129PG486rl6r2kLMwq8jIu4CUSvwpIblqCILWk7kxQzlei+//7JweQxLbkXfWgdmwA1mUflBXyqQ4vAFyL4w3g44GilInp0nT/iswdAFiCgb5RaK8xkmq+HDeghQWHsNxkPjf7ffDU8wnaLxAK0w4vwYm8BdhzKvEyRFbiTFohLwa4F9byVGrTIAEj53CQ0VvbKwQT6SH+LUVAp5Wr5vMPAREebx/0X5Yy63EuXWvCdZwG64n/TAm4qFhMThrtX+8h+zyf+CViDSZ1xAwkPNtfaQ3scN7g==
9+
310
import (
411
"crypto"
512
"crypto/rsa"
@@ -17,13 +24,13 @@ import (
1724
// Paste the public key here and don't reserve any spaces.
1825
var publicKeyBytes = []byte(
1926
`-----BEGIN PUBLIC KEY-----
20-
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAt8sWoEyEdJso7GP5jVqY
21-
K+pLu3bUFAsWh3NClHM5CzTH34JKPTInFMQbDTaZ2Q23hmC2uLvYKriX8hFa9UOJ
22-
BXz2uwQhSzCu6RwN0Evrbj1DkWo0p6ifOa4BkYt4+mGDtVrGBGeLQCRtU1CoAVal
23-
AtzOKFfHhrE2xinSZk2uDwUq4lClegfU99hqPKAmAXg3s90mZ+D43cdmn0HkjJ/9
24-
qe4aZwP+u2fdgXow0Z+dRnc8NDVsWfMdfduReuwHiuCOFFjWhh83/Wta9i0JrT0Y
25-
uSYgJswTRHa9bI6uatvIwmHV1mADc0/RUl9uJzc0x/pC/RiMlE/4OYU/exL88Xo4
26-
GwIDAQAB
27+
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAzTuY9ePxSX533aa54/aY
28+
Qobqzz0/alc40C31/fYgYXLQVeMJ4vXBHKFhWOaf+ZBf2bQBLx2aIa2ODZcH4ZNF
29+
UIbSZu9jmWN6kcSCw5IMPuDW2YF0b0MlxCemPgCPdIioBa/qsgmy4/s6LpZ2JtUG
30+
7+KBOJIBxuzt8k2XtfRK7k8HBL5v3pQI6IqgooN6cq/M9IOWges1RwLTsMcUbISm
31+
pSOGIC57XmreGiOQik3IlWLYaDbo5nOhzhGtnz6FlAOscW3guYuMBiPjYnTERXNz
32+
1rwx1dHM+t+K2/7poB477RoBEHeLYkEF2JkxVZAXdAg+5PKkMj+Cd/U867t83mDG
33+
OQIDAQAB
2734
-----END PUBLIC KEY-----
2835
`)
2936

@@ -41,7 +48,7 @@ func main() {
4148
if strings.Contains(errMsg, "verification error") {
4249
println("FAIL")
4350
} else {
44-
println(errMsg)
51+
println("PASS")
4552
}
4653
}
4754
}()
@@ -51,11 +58,11 @@ func main() {
5158
}
5259

5360
func getVerfiyInfo() *VerifyInfo {
54-
// Write your own hash method and message here.
55-
hashType, hash := getHash("sha3-512", []byte("f39476262640eebefde1bb5ede9a0fc721ab7d9d269002ce95fa89dcbc201b69"))
61+
// Write your own hash method and message(key) here.
62+
hashType, hash := getHash("sha3-512", []byte("95e156395687128711f29b68fbc44573667bdfc5f0d65010cb0555b62138d830"))
5663

5764
// Write your own signature here.
58-
signatureBase64 := "qJrXQVeoGmoObRj4cqAPuhGRanj1yebFAwP6lxRCCUNqN4pgEv8qiRJXZGJP2ky8dtI67aOx48ij8vbUomxl4a3wEvyxXym1KHAd4vVObw393VQYG5nbKvPAVENQlqfJo3MnkYtTR/B4h3zVj1BQjBKE+kGx2J/4i4W9dnuIOAbtcs05dEWr8woE/JFa4LcFfHv+jJp0Exok5oPxIZ8paFq7/CkNlO91b+W62th35gh4e2bqgCEXdwUifA4I2H0LyuEPscuc2yrqYC0Ve+yQQ58c6g7HLW2SXyCJnXbpcDebMtWeXfp8468iQHj2UE4ykzmrnprQ2jOrnIMv62rF4A=="
65+
signatureBase64 := "upagNzGSL3ZqCsxApgG8yiG/x1c+ZZBJgNtzvZR2KYVLP60+hAr5WcnZ129PG486rl6r2kLMwq8jIu4CUSvwpIblqCILWk7kxQzlei+//7JweQxLbkXfWgdmwA1mUflBXyqQ4vAFyL4w3g44GilInp0nT/iswdAFiCgb5RaK8xkmq+HDeghQWHsNxkPjf7ffDU8wnaLxAK0w4vwYm8BdhzKvEyRFbiTFohLwa4F9byVGrTIAEj53CQ0VvbKwQT6SH+LUVAp5Wr5vMPAREebx/0X5Yy63EuXWvCdZwG64n/TAm4qFhMThrtX+8h+zyf+CViDSZ1xAwkPNtfaQ3scN7g=="
5966
signature, err := base64.StdEncoding.DecodeString(signatureBase64)
6067

6168
if err != nil {
@@ -84,7 +91,7 @@ func Verify(v *VerifyInfo) {
8491
}
8592

8693
opts := &rsa.PSSOptions{
87-
SaltLength: rsa.PSSSaltLengthAuto,
94+
SaltLength: rsa.PSSSaltLengthEqualsHash,
8895
Hash: v.hashType,
8996
}
9097

sdk/python/example/client.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# This is an example for verifying the signature by the given public key.
2+
# Used arguments:
3+
# hash method: SHA3-512
4+
# PSS salt length: rsa.PSSSaltLengthEqualsHash
5+
# Message: 95e156395687128711f29b68fbc44573667bdfc5f0d65010cb0555b62138d830
6+
# Signature: upagNzGSL3ZqCsxApgG8yiG/x1c+ZZBJgNtzvZR2KYVLP60+hAr5WcnZ129PG486rl6r2kLMwq8jIu4CUSvwpIblqCILWk7kxQzlei+//7JweQxLbkXfWgdmwA1mUflBXyqQ4vAFyL4w3g44GilInp0nT/iswdAFiCgb5RaK8xkmq+HDeghQWHsNxkPjf7ffDU8wnaLxAK0w4vwYm8BdhzKvEyRFbiTFohLwa4F9byVGrTIAEj53CQ0VvbKwQT6SH+LUVAp5Wr5vMPAREebx/0X5Yy63EuXWvCdZwG64n/TAm4qFhMThrtX+8h+zyf+CViDSZ1xAwkPNtfaQ3scN7g==
7+
8+
from cryptography.hazmat.primitives import hashes
9+
from cryptography.hazmat.primitives.asymmetric import padding
10+
from cryptography.hazmat.primitives.serialization import load_pem_public_key
11+
from cryptography.exceptions import InvalidSignature
12+
from cryptography.hazmat.primitives.asymmetric import utils
13+
import base64
14+
15+
16+
public_key_pem = b"""-----BEGIN PUBLIC KEY-----
17+
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAzTuY9ePxSX533aa54/aY
18+
Qobqzz0/alc40C31/fYgYXLQVeMJ4vXBHKFhWOaf+ZBf2bQBLx2aIa2ODZcH4ZNF
19+
UIbSZu9jmWN6kcSCw5IMPuDW2YF0b0MlxCemPgCPdIioBa/qsgmy4/s6LpZ2JtUG
20+
7+KBOJIBxuzt8k2XtfRK7k8HBL5v3pQI6IqgooN6cq/M9IOWges1RwLTsMcUbISm
21+
pSOGIC57XmreGiOQik3IlWLYaDbo5nOhzhGtnz6FlAOscW3guYuMBiPjYnTERXNz
22+
1rwx1dHM+t+K2/7poB477RoBEHeLYkEF2JkxVZAXdAg+5PKkMj+Cd/U867t83mDG
23+
OQIDAQAB
24+
-----END PUBLIC KEY-----
25+
"""
26+
27+
public_key = load_pem_public_key(public_key_pem)
28+
29+
message = "95e156395687128711f29b68fbc44573667bdfc5f0d65010cb0555b62138d830"
30+
message_bytes = message.encode()
31+
signature_base64 = "upagNzGSL3ZqCsxApgG8yiG/x1c+ZZBJgNtzvZR2KYVLP60+hAr5WcnZ129PG486rl6r2kLMwq8jIu4CUSvwpIblqCILWk7kxQzlei+//7JweQxLbkXfWgdmwA1mUflBXyqQ4vAFyL4w3g44GilInp0nT/iswdAFiCgb5RaK8xkmq+HDeghQWHsNxkPjf7ffDU8wnaLxAK0w4vwYm8BdhzKvEyRFbiTFohLwa4F9byVGrTIAEj53CQ0VvbKwQT6SH+LUVAp5Wr5vMPAREebx/0X5Yy63EuXWvCdZwG64n/TAm4qFhMThrtX+8h+zyf+CViDSZ1xAwkPNtfaQ3scN7g=="
32+
33+
digest = hashes.Hash(hashes.SHA3_512())
34+
digest.update(message_bytes)
35+
message_hash = digest.finalize()
36+
37+
signature = base64.b64decode(signature_base64)
38+
verification_result = ""
39+
40+
try:
41+
public_key.verify(
42+
signature,
43+
message_hash,
44+
padding.PSS(
45+
mgf=padding.MGF1(hashes.SHA3_512()),
46+
salt_length=len(message_hash)
47+
),
48+
utils.Prehashed(hashes.SHA3_512())
49+
)
50+
verification_result = "PASS"
51+
except InvalidSignature:
52+
verification_result = "FAIL"
53+
54+
print(verification_result)

sdk/python/requirements.txt

108 Bytes
Binary file not shown.

server.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ func main() {
6060
data.ConnectDB()
6161
defer data.DisconnectDB()
6262

63-
registRoutes()
63+
registerRoutes()
6464

6565
if !cfg.SERVER_CONFIG.USE_TLS {
6666
run(router)
@@ -73,19 +73,19 @@ func main() {
7373
}
7474
}
7575

76-
func registRoutes() {
77-
registRoutesForDocs()
76+
func registerRoutes() {
77+
registerRoutesForDocs()
7878

7979
rootGroup := router.Group("/api/v1")
80-
registRoutesForAdmin(rootGroup)
81-
registRoutesForClient(rootGroup)
80+
registerRoutesForAdmin(rootGroup)
81+
registerRoutesForClient(rootGroup)
8282
}
8383

84-
func registRoutesForDocs() {
84+
func registerRoutesForDocs() {
8585
router.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))
8686
}
8787

88-
func registRoutesForAdmin(rootGroup *gin.RouterGroup) {
88+
func registerRoutesForAdmin(rootGroup *gin.RouterGroup) {
8989
snGroup := rootGroup.Group("/sn")
9090

9191
snGroup.POST("/create",
@@ -115,7 +115,7 @@ func registRoutesForAdmin(rootGroup *gin.RouterGroup) {
115115
)
116116
}
117117

118-
func registRoutesForClient(rootGroup *gin.RouterGroup) {
118+
func registerRoutesForClient(rootGroup *gin.RouterGroup) {
119119
applyGroup := rootGroup.Group("/apply")
120120

121121
applyGroup.POST("/cert", middleware.ClientAccessAuth(), api.ApplyCertificate)

utils/key_tools.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ func signMessage(methodName string, data []byte, privateKey *rsa.PrivateKey) ([]
129129
cryptoType, hash := getHash(methodName, data)
130130

131131
opts := &rsa.PSSOptions{
132-
SaltLength: rsa.PSSSaltLengthAuto,
132+
SaltLength: rsa.PSSSaltLengthEqualsHash,
133133
Hash: cryptoType,
134134
}
135135

0 commit comments

Comments
 (0)