Debian Codegraph @

lib/appmanifest/publictoken.go

raw symbols SCIP
1 //
2 // Copyright (c) SAS Institute Inc.
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16
17 package appmanifest
18
19 import (
20 "bytes"
21 "crypto"
22 "crypto/ecdsa"
23 "crypto/elliptic"
24 "crypto/rsa"
25 "encoding/binary"
26 "encoding/hex"
27 "errors"
28 "math/big"
29
30 "github.com/sassoftware/relic/v7/lib/certloader"
31 "github.com/sassoftware/relic/v7/lib/x509tools"
32 )
33
34 func PublisherIdentity(cert *certloader.Certificate) (string, string, error) {
35 issuer := cert.Issuer()
36 if issuer == nil {
37 return "", "", errors.New("unable to find issuer certificate in chain")
38 }
39 aki, err := x509tools.SubjectKeyID(issuer.PublicKey)
40 if err != nil {
41 return "", "", err
42 }
43 name := x509tools.FormatPkixName(cert.Leaf.RawSubject, x509tools.NameStyleMsOsco)
44 return name, hex.EncodeToString(aki), nil
45 }
46
47 const (
48 snkRsaPub = 0x06
49 snkRsaVersion = 0x02
50 // https://msdn.microsoft.com/en-us/library/windows/desktop/aa375549(v=vs.85).aspx
51 calgRsaSign = 0x2400 // CALG_RSA_SIGN
52 calgSha1 = 0x8004 // CALG_SHA1
53 calgEcdsa = 0x2203 // CALG_ECDSA
54 // bcrypt.h
55 bcryptRsaPubMagic = 0x31415352 // BCRYPT_RSAPUBLIC_MAGIC
56 bcryptEcdsaPubP256 = 0x31534345 // BCRYPT_ECDSA_PUBLIC_P256_MAGIC
57 bcryptEcdsaPubP384 = 0x33534345 // BCRYPT_ECDSA_PUBLIC_P384_MAGIC
58 bcryptEcdsaPubP521 = 0x35534345 // BCRYPT_ECDSA_PUBLIC_P521_MAGIC
59 )
60
61 type snkHeader struct {
62 PubAlgorithm uint32
63 HashAlgorithm uint32
64 BlobSize uint32
65 KeyType uint8
66 Version uint8
67 Reserved uint16
68 PubAlgorithm2 uint32
69 }
70
71 type blobRsaPub struct {
72 snkHeader
73 // BCRYPT_RSAKEY_BLOB
74 KeyMagic uint32
75 BitLength uint32
76 PubExponent uint32
77 } // N [BitLength/8]byte
78
79 type blobEcdsaPub struct {
80 snkHeader
81 // BCRYPT_ECCKEY_BLOB
82 KeyMagic uint32
83 ByteLength uint32
84 } // X, Y [ByteLength]byte
85
86 // Calculate the publicKeyToken from a public key. This involves mangling it
87 // into a .snk file format, then hashing it.
88 //
89 // http://www.developerfusion.com/article/84422/the-key-to-strong-names/
90 func PublicKeyToken(pubKey crypto.PublicKey) (string, error) {
91 snk, err := PublicKeyToSnk(pubKey)
92 if err != nil {
93 return "", err
94 }
95 d := crypto.SHA1.New()
96 d.Write(snk)
97 // token is the low 64 bits of sum decoded as a little-endian number, or in
98 // other words the last 8 bytes in reverse order
99 sum := d.Sum(nil)
100 token := make([]byte, 8)
101 for i := 0; i < 8; i++ {
102 token[i] = sum[19-i]
103 }
104 return hex.EncodeToString(token), nil
105 }
106
107 // Convert public key to "snk" format
108 func PublicKeyToSnk(pubKey crypto.PublicKey) ([]byte, error) {
109 var buf bytes.Buffer
110 switch k := pubKey.(type) {
111 case *rsa.PublicKey:
112 modulus := bigIntToLE(k.N)
113 if err := binary.Write(&buf, binary.LittleEndian, blobRsaPub{
114 snkHeader: snkHeader{
115 PubAlgorithm: calgRsaSign,
116 HashAlgorithm: calgSha1,
117 BlobSize: uint32(20 + len(modulus)),
118 KeyType: snkRsaPub,
119 Version: snkRsaVersion,
120 PubAlgorithm2: calgRsaSign,
121 },
122 KeyMagic: bcryptRsaPubMagic,
123 BitLength: uint32(8 * len(modulus)),
124 PubExponent: uint32(k.E),
125 }); err != nil {
126 return nil, nil
127 }
128 buf.Write(modulus)
129 case *ecdsa.PublicKey:
130 // TODO: This is a best guess based on piecing together various
131 // Microsoft documentation and header values, but some pieces are still
132 // missing. ECDSA isn't supported for strong name signing, and
133 // calcuating the publicKeyToken for SN is the only reason this
134 // function is even here.
135 var keyMagic uint32
136 switch k.Curve {
137 case elliptic.P256():
138 keyMagic = bcryptEcdsaPubP256
139 case elliptic.P384():
140 keyMagic = bcryptEcdsaPubP384
141 case elliptic.P521():
142 keyMagic = bcryptEcdsaPubP521
143 default:
144 return nil, errors.New("unsupported ECDSA curve")
145 }
146 // TODO: are these supposed to be big-endian? the documentation for
147 // BCRYPT_ECCKEY_BLOB says so, but it also said that about the RSA one
148 // and yet the SNK format actually uses little endian...
149 x := k.X.Bytes()
150 y := k.Y.Bytes()
151 if err := binary.Write(&buf, binary.LittleEndian, blobEcdsaPub{
152 snkHeader: snkHeader{
153 PubAlgorithm: calgEcdsa,
154 HashAlgorithm: calgSha1,
155 BlobSize: uint32(12 + 2*len(x)),
156 KeyType: snkRsaPub, // TODO
157 Version: snkRsaVersion, // TODO
158 PubAlgorithm2: calgEcdsa,
159 },
160 KeyMagic: keyMagic,
161 ByteLength: uint32(len(x)),
162 }); err != nil {
163 return nil, nil
164 }
165 buf.Write(x)
166 buf.Write(y)
167 default:
168 return nil, errors.New("unsupported key type for strong name signing")
169 }
170 return buf.Bytes(), nil
171 }
172
173 func bigIntToLE(x *big.Int) []byte {
174 b := x.Bytes()
175 for i := 0; i < len(b)/2; i++ {
176 j := len(b) - i - 1
177 b[i], b[j] = b[j], b[i]
178 }
179 return b
180 }