用户上传真实的驱动签名打包 pfx(81 把私钥 / 79 张证书),界面只报「解析失败」。
根因不是口令,是 go-pkcs12 一遇到不认识的 bag 属性就整包报错,而 Windows
导出的 pfx 一定带私有属性(本例 OID 1.3.6.1.4.1.311.17.3.75)。
1) 本地改造 go-pkcs12(third_party/go-pkcs12 + go.mod replace)
未知属性改为跳过,等价于 openssl 的行为。不认识的属性不影响证书与私钥。
2) 解析失败不再是一句话:新增 PFXError{Reason,Hint,Detail},按魔数判断文件
到底是什么(pfx / pem / pkcs7 / der 证书 / der 私钥 / 压缩包),并把
「口令错」和「结构不对」分开判——否则「只有证书的 pfx」会被说成口令错,
用户改口令永远改不好。口令候选会试 原值 / 去首尾空白 / 空串。
顺带支持 PEM(证书+未加密私钥)直接上传。
3) 大包不再拆成 N 条记录:79 套就建 79 条记录没法用。改为一条记录 +
记下身份数,新增 GET /dev/sign-profiles/:id/identities 列全部身份,
加壳时用 cert_identity 指定第几套,签名那一刻才把那一套单独拆出来
(LoadMaterial 支持 identity,osslsigncode 只认文件里第一套)。
4) 前端:选中多身份证书时出现「签名身份」可搜索下拉(#序号 · CN · 到期,
过期标黄),默认第一套;证书下拉标注「N 套身份」。
5) cmd/pfxprobe:本地诊断工具,秒看文件形态/证书数/私钥数/每套到期。
实测(用户真实文件,口令 123456):
- 解析:79 张证书 / 81 把私钥 / 79 套身份(2 把私钥没有配对证书)
- 身份列表接口返回 79 条,含 NVIDIA Corporation / Take-Two 等
- 选第 43 套(NVIDIA)签名 → osslsigncode verify 产物 Subject 为
CN=NVIDIA Corporation,与面板所选一致
- 前后失败对照:改前「unknown attribute with OID 1.3.6.1.4.1.311.17.3.75」
64 lines
1.7 KiB
Go
64 lines
1.7 KiB
Go
// Copyright 2015 The Go Authors. All rights reserved.
|
|
// Use of this source code is governed by a BSD-style
|
|
// license that can be found in the LICENSE file.
|
|
|
|
package pkcs12
|
|
|
|
import (
|
|
"errors"
|
|
"unicode/utf16"
|
|
)
|
|
|
|
// bmpStringZeroTerminated returns s encoded in UCS-2 with a zero terminator.
|
|
func bmpStringZeroTerminated(s string) ([]byte, error) {
|
|
// References:
|
|
// https://tools.ietf.org/html/rfc7292#appendix-B.1
|
|
// The above RFC provides the info that BMPStrings are NULL terminated.
|
|
|
|
ret, err := bmpString(s)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return append(ret, 0, 0), nil
|
|
}
|
|
|
|
// bmpString returns s encoded in UCS-2
|
|
func bmpString(s string) ([]byte, error) {
|
|
// References:
|
|
// https://tools.ietf.org/html/rfc7292#appendix-B.1
|
|
// https://en.wikipedia.org/wiki/Plane_(Unicode)#Basic_Multilingual_Plane
|
|
// - non-BMP characters are encoded in UTF 16 by using a surrogate pair of 16-bit codes
|
|
// EncodeRune returns 0xfffd if the rune does not need special encoding
|
|
|
|
ret := make([]byte, 0, 2*len(s)+2)
|
|
|
|
for _, r := range s {
|
|
if t, _ := utf16.EncodeRune(r); t != 0xfffd {
|
|
return nil, errors.New("pkcs12: string contains characters that cannot be encoded in UCS-2")
|
|
}
|
|
ret = append(ret, byte(r/256), byte(r%256))
|
|
}
|
|
|
|
return ret, nil
|
|
}
|
|
|
|
func decodeBMPString(bmpString []byte) (string, error) {
|
|
if len(bmpString)%2 != 0 {
|
|
return "", errors.New("pkcs12: odd-length BMP string")
|
|
}
|
|
|
|
// strip terminator if present
|
|
if l := len(bmpString); l >= 2 && bmpString[l-1] == 0 && bmpString[l-2] == 0 {
|
|
bmpString = bmpString[:l-2]
|
|
}
|
|
|
|
s := make([]uint16, 0, len(bmpString)/2)
|
|
for len(bmpString) > 0 {
|
|
s = append(s, uint16(bmpString[0])<<8+uint16(bmpString[1]))
|
|
bmpString = bmpString[2:]
|
|
}
|
|
|
|
return string(utf16.Decode(s)), nil
|
|
}
|