| 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 | // Implements a useful subset of the xmldsig specification for creating |
| 18 | // signatures over XML documents. |
| 19 | package xmldsigscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/xmldsig`/ |
| 20 | |
| 21 | import ( |
| 22 | "cryptoscip-go gomod github.com/golang/go/src go1.18 crypto/" |
| 23 | "crypto/ecdsascip-go gomod github.com/golang/go/src go1.18 `crypto/ecdsa`/" |
| 24 | "crypto/randscip-go gomod github.com/golang/go/src go1.18 `crypto/rand`/" |
| 25 | "crypto/rsascip-go gomod github.com/golang/go/src go1.18 `crypto/rsa`/" |
| 26 | "crypto/x509scip-go gomod github.com/golang/go/src go1.18 `crypto/x509`/" |
| 27 | "encoding/base64scip-go gomod github.com/golang/go/src go1.18 `encoding/base64`/" |
| 28 | "errorsscip-go gomod github.com/golang/go/src go1.18 errors/" |
| 29 | "fmtscip-go gomod github.com/golang/go/src go1.18 fmt/" |
| 30 | "math/bigscip-go gomod github.com/golang/go/src go1.18 `math/big`/" |
| 31 | |
| 32 | "github.com/sassoftware/relic/v7/lib/x509toolsscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/x509tools`/" |
| 33 | |
| 34 | "github.com/beevik/etreescip-go gomod github.com/beevik/etree v1.1.0 `github.com/beevik/etree`/" |
| 35 | ) |
| 36 | |
| 37 | type SignOptionsscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/xmldsig`/SignOptions# struct { |
| 38 | // Use non-standard namespace for SHA-256 found in Microsoft ClickOnce manifests |
| 39 | MsCompatHashNamesscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/xmldsig`/SignOptions#MsCompatHashNames. bool |
| 40 | // Use REC namespace for c14n method instead of the finalized one |
| 41 | UseRecC14nscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/xmldsig`/SignOptions#UseRecC14n. bool |
| 42 | // Add the X509 certificate chain to the KeyInfo |
| 43 | IncludeX509scip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/xmldsig`/SignOptions#IncludeX509. bool |
| 44 | // Add a KeyValue element with the public key |
| 45 | IncludeKeyValuescip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/xmldsig`/SignOptions#IncludeKeyValue. bool |
| 46 | } |
| 47 | |
| 48 | func (s SignOptionsscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/xmldsig`/SignOptions#) c14nNamespacescip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/xmldsig`/SignOptions#c14nNamespace().() string { |
| 49 | if s.UseRecC14nscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/xmldsig`/SignOptions#UseRecC14n. { |
| 50 | return AlgXMLExcC14nRecscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/xmldsig`/AlgXMLExcC14nRec. |
| 51 | } else { |
| 52 | return AlgXMLExcC14nscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/xmldsig`/AlgXMLExcC14n. |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | // Create an enveloped signature from the document rooted at "root", replacing |
| 57 | // any existing signature and adding it as a last child of "parent". |
| 58 | func Signscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/xmldsig`/Sign().(root, parent *etreescip-go gomod github.com/beevik/etree v1.1.0 `github.com/beevik/etree`/.Elementscip-go gomod github.com/beevik/etree v1.1.0 `github.com/beevik/etree`/Element#, hash cryptoscip-go gomod github.com/golang/go/src go1.18 crypto/.Hashscip-go gomod github.com/golang/go/src go1.18 crypto/Hash#, privKey cryptoscip-go gomod github.com/golang/go/src go1.18 crypto/.Signerscip-go gomod github.com/golang/go/src go1.18 crypto/Signer#, certs []*x509scip-go gomod github.com/golang/go/src go1.18 `crypto/x509`/.Certificatescip-go gomod github.com/golang/go/src go1.18 `crypto/x509`/Certificate#, opts SignOptionsscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/xmldsig`/SignOptions#) error { |
| 59 | pubKey := privKey.Publicscip-go gomod github.com/golang/go/src go1.18 crypto/Signer#Public().() |
| 60 | if len(certs) < 1 || !x509toolsscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/x509tools`/.SameKeyscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/x509tools`/SameKey().(pubKey, certs[0].PublicKeyscip-go gomod github.com/golang/go/src go1.18 `crypto/x509`/Certificate#PublicKey.) { |
| 61 | return errorsscip-go gomod github.com/golang/go/src go1.18 errors/.Newscip-go gomod github.com/golang/go/src go1.18 errors/New().("xmldsig: first certificate must match private key") |
| 62 | } |
| 63 | RemoveElementsscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/xmldsig`/RemoveElements().(parent, "Signature") |
| 64 | // canonicalize the enveloping document and digest it |
| 65 | refDigest, err := hashCanonscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/xmldsig`/hashCanon().(root, hash) |
| 66 | if err != nil { |
| 67 | return err |
| 68 | } |
| 69 | hashAlg, sigAlg, err := hashAlgsscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/xmldsig`/hashAlgs().(hash, pubKey, opts) |
| 70 | if err != nil { |
| 71 | return err |
| 72 | } |
| 73 | // build a signedinfo that references the enveloping document |
| 74 | signature := parent.CreateElementscip-go gomod github.com/beevik/etree v1.1.0 `github.com/beevik/etree`/Element#CreateElement().("Signature") |
| 75 | signature.CreateAttrscip-go gomod github.com/beevik/etree v1.1.0 `github.com/beevik/etree`/Element#CreateAttr().("xmlns", NsXMLDsigscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/xmldsig`/NsXMLDsig.) |
| 76 | signedinfo := buildSignedInfoscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/xmldsig`/buildSignedInfo().(signature, "", hashAlg, sigAlg, refDigest, opts) |
| 77 | return finishSignaturescip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/xmldsig`/finishSignature().(signature, signedinfo, hash, privKey, certs, opts) |
| 78 | } |
| 79 | |
| 80 | // Build an enveloping Signature document around the given Object element |
| 81 | func SignEnvelopingscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/xmldsig`/SignEnveloping().(object *etreescip-go gomod github.com/beevik/etree v1.1.0 `github.com/beevik/etree`/.Elementscip-go gomod github.com/beevik/etree v1.1.0 `github.com/beevik/etree`/Element#, hash cryptoscip-go gomod github.com/golang/go/src go1.18 crypto/.Hashscip-go gomod github.com/golang/go/src go1.18 crypto/Hash#, privKey cryptoscip-go gomod github.com/golang/go/src go1.18 crypto/.Signerscip-go gomod github.com/golang/go/src go1.18 crypto/Signer#, certs []*x509scip-go gomod github.com/golang/go/src go1.18 `crypto/x509`/.Certificatescip-go gomod github.com/golang/go/src go1.18 `crypto/x509`/Certificate#, opts SignOptionsscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/xmldsig`/SignOptions#) (*etreescip-go gomod github.com/beevik/etree v1.1.0 `github.com/beevik/etree`/.Elementscip-go gomod github.com/beevik/etree v1.1.0 `github.com/beevik/etree`/Element#, error) { |
| 82 | pubKey := privKey.Publicscip-go gomod github.com/golang/go/src go1.18 crypto/Signer#Public().() |
| 83 | if len(certs) < 1 || !x509toolsscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/x509tools`/.SameKeyscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/x509tools`/SameKey().(pubKey, certs[0].PublicKeyscip-go gomod github.com/golang/go/src go1.18 `crypto/x509`/Certificate#PublicKey.) { |
| 84 | return nil, errorsscip-go gomod github.com/golang/go/src go1.18 errors/.Newscip-go gomod github.com/golang/go/src go1.18 errors/New().("xmldsig: first certificate must match private key") |
| 85 | } |
| 86 | // insert the object into the signature element before canonicalizing so |
| 87 | // that the namespace gets pushed down properly |
| 88 | signature := etreescip-go gomod github.com/beevik/etree v1.1.0 `github.com/beevik/etree`/.NewElementscip-go gomod github.com/beevik/etree v1.1.0 `github.com/beevik/etree`/NewElement().("Signature") |
| 89 | signature.CreateAttrscip-go gomod github.com/beevik/etree v1.1.0 `github.com/beevik/etree`/Element#CreateAttr().("xmlns", NsXMLDsigscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/xmldsig`/NsXMLDsig.) |
| 90 | signature.AddChildscip-go gomod github.com/beevik/etree v1.1.0 `github.com/beevik/etree`/Element#AddChild().(object) |
| 91 | refDigest, err := hashCanonscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/xmldsig`/hashCanon().(object, hash) |
| 92 | if err != nil { |
| 93 | return nil, err |
| 94 | } |
| 95 | hashAlg, sigAlg, err := hashAlgsscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/xmldsig`/hashAlgs().(hash, pubKey, opts) |
| 96 | if err != nil { |
| 97 | return nil, err |
| 98 | } |
| 99 | if object.Tagscip-go gomod github.com/beevik/etree v1.1.0 `github.com/beevik/etree`/Element#Tag. != "Object" { |
| 100 | return nil, errorsscip-go gomod github.com/golang/go/src go1.18 errors/.Newscip-go gomod github.com/golang/go/src go1.18 errors/New().("object must have tag \"Object\"") |
| 101 | } |
| 102 | refId := object.SelectAttrValuescip-go gomod github.com/beevik/etree v1.1.0 `github.com/beevik/etree`/Element#SelectAttrValue().("Id", "") |
| 103 | if refId == "" { |
| 104 | return nil, errorsscip-go gomod github.com/golang/go/src go1.18 errors/.Newscip-go gomod github.com/golang/go/src go1.18 errors/New().("object lacks an Id attribute") |
| 105 | } |
| 106 | // build a signedinfo that references the enveloping document |
| 107 | signedinfo := buildSignedInfoscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/xmldsig`/buildSignedInfo().(signature, refId, hashAlg, sigAlg, refDigest, opts) |
| 108 | // canonicalize the signedinfo section and sign it |
| 109 | if err := finishSignaturescip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/xmldsig`/finishSignature().(signature, signedinfo, hash, privKey, certs, opts); err != nil { |
| 110 | return nil, err |
| 111 | } |
| 112 | signature.RemoveChildscip-go gomod github.com/beevik/etree v1.1.0 `github.com/beevik/etree`/Element#RemoveChild().(object) |
| 113 | signature.AddChildscip-go gomod github.com/beevik/etree v1.1.0 `github.com/beevik/etree`/Element#AddChild().(object) |
| 114 | return signature, nil |
| 115 | } |
| 116 | |
| 117 | func buildSignedInfoscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/xmldsig`/buildSignedInfo().(signature *etreescip-go gomod github.com/beevik/etree v1.1.0 `github.com/beevik/etree`/.Elementscip-go gomod github.com/beevik/etree v1.1.0 `github.com/beevik/etree`/Element#, refId, hashAlg, sigAlg string, refDigest []byte, opts SignOptionsscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/xmldsig`/SignOptions#) *etreescip-go gomod github.com/beevik/etree v1.1.0 `github.com/beevik/etree`/.Elementscip-go gomod github.com/beevik/etree v1.1.0 `github.com/beevik/etree`/Element# { |
| 118 | signedinfo := signature.CreateElementscip-go gomod github.com/beevik/etree v1.1.0 `github.com/beevik/etree`/Element#CreateElement().("SignedInfo") |
| 119 | signedinfo.CreateElementscip-go gomod github.com/beevik/etree v1.1.0 `github.com/beevik/etree`/Element#CreateElement().("CanonicalizationMethod").CreateAttrscip-go gomod github.com/beevik/etree v1.1.0 `github.com/beevik/etree`/Element#CreateAttr().("Algorithm", opts.c14nNamespacescip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/xmldsig`/SignOptions#c14nNamespace().()) |
| 120 | signedinfo.CreateElementscip-go gomod github.com/beevik/etree v1.1.0 `github.com/beevik/etree`/Element#CreateElement().("SignatureMethod").CreateAttrscip-go gomod github.com/beevik/etree v1.1.0 `github.com/beevik/etree`/Element#CreateAttr().("Algorithm", sigAlg) |
| 121 | reference := signedinfo.CreateElementscip-go gomod github.com/beevik/etree v1.1.0 `github.com/beevik/etree`/Element#CreateElement().("Reference") |
| 122 | if refId == "" { |
| 123 | reference.CreateAttrscip-go gomod github.com/beevik/etree v1.1.0 `github.com/beevik/etree`/Element#CreateAttr().("URI", "") |
| 124 | } else { |
| 125 | reference.CreateAttrscip-go gomod github.com/beevik/etree v1.1.0 `github.com/beevik/etree`/Element#CreateAttr().("URI", "#"+refId) |
| 126 | reference.CreateAttrscip-go gomod github.com/beevik/etree v1.1.0 `github.com/beevik/etree`/Element#CreateAttr().("Type", NsXMLDsigscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/xmldsig`/NsXMLDsig.+"Object") |
| 127 | } |
| 128 | transforms := reference.CreateElementscip-go gomod github.com/beevik/etree v1.1.0 `github.com/beevik/etree`/Element#CreateElement().("Transforms") |
| 129 | if refId == "" { |
| 130 | transforms.CreateElementscip-go gomod github.com/beevik/etree v1.1.0 `github.com/beevik/etree`/Element#CreateElement().("Transform").CreateAttrscip-go gomod github.com/beevik/etree v1.1.0 `github.com/beevik/etree`/Element#CreateAttr().("Algorithm", AlgDsigEnvelopedSignaturescip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/xmldsig`/AlgDsigEnvelopedSignature.) |
| 131 | } |
| 132 | transforms.CreateElementscip-go gomod github.com/beevik/etree v1.1.0 `github.com/beevik/etree`/Element#CreateElement().("Transform").CreateAttrscip-go gomod github.com/beevik/etree v1.1.0 `github.com/beevik/etree`/Element#CreateAttr().("Algorithm", opts.c14nNamespacescip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/xmldsig`/SignOptions#c14nNamespace().()) |
| 133 | reference.CreateElementscip-go gomod github.com/beevik/etree v1.1.0 `github.com/beevik/etree`/Element#CreateElement().("DigestMethod").CreateAttrscip-go gomod github.com/beevik/etree v1.1.0 `github.com/beevik/etree`/Element#CreateAttr().("Algorithm", hashAlg) |
| 134 | reference.CreateElementscip-go gomod github.com/beevik/etree v1.1.0 `github.com/beevik/etree`/Element#CreateElement().("DigestValue").SetTextscip-go gomod github.com/beevik/etree v1.1.0 `github.com/beevik/etree`/Element#SetText().(base64scip-go gomod github.com/golang/go/src go1.18 `encoding/base64`/.StdEncodingscip-go gomod github.com/golang/go/src go1.18 `encoding/base64`/StdEncoding..EncodeToStringscip-go gomod github.com/golang/go/src go1.18 `encoding/base64`/Encoding#EncodeToString().(refDigest)) |
| 135 | return signedinfo |
| 136 | } |
| 137 | |
| 138 | func finishSignaturescip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/xmldsig`/finishSignature().(signature, signedinfo *etreescip-go gomod github.com/beevik/etree v1.1.0 `github.com/beevik/etree`/.Elementscip-go gomod github.com/beevik/etree v1.1.0 `github.com/beevik/etree`/Element#, hash cryptoscip-go gomod github.com/golang/go/src go1.18 crypto/.Hashscip-go gomod github.com/golang/go/src go1.18 crypto/Hash#, privKey cryptoscip-go gomod github.com/golang/go/src go1.18 crypto/.Signerscip-go gomod github.com/golang/go/src go1.18 crypto/Signer#, certs []*x509scip-go gomod github.com/golang/go/src go1.18 `crypto/x509`/.Certificatescip-go gomod github.com/golang/go/src go1.18 `crypto/x509`/Certificate#, opts SignOptionsscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/xmldsig`/SignOptions#) error { |
| 139 | siDigest, err := hashCanonscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/xmldsig`/hashCanon().(signedinfo, hash) |
| 140 | if err != nil { |
| 141 | return err |
| 142 | } |
| 143 | sig, err := privKey.Signscip-go gomod github.com/golang/go/src go1.18 crypto/Signer#Sign().(randscip-go gomod github.com/golang/go/src go1.18 `crypto/rand`/.Readerscip-go gomod github.com/golang/go/src go1.18 `crypto/rand`/Reader., siDigest, hash) |
| 144 | if err != nil { |
| 145 | return err |
| 146 | } |
| 147 | // build the rest of the signature element |
| 148 | if _, ok := privKey.Publicscip-go gomod github.com/golang/go/src go1.18 crypto/Signer#Public().().(*ecdsascip-go gomod github.com/golang/go/src go1.18 `crypto/ecdsa`/.PublicKeyscip-go gomod github.com/golang/go/src go1.18 `crypto/ecdsa`/PublicKey#); ok { |
| 149 | // reformat the signature without ASN.1 structure |
| 150 | esig, err := x509toolsscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/x509tools`/.UnmarshalEcdsaSignaturescip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/x509tools`/UnmarshalEcdsaSignature().(sig) |
| 151 | if err != nil { |
| 152 | return err |
| 153 | } |
| 154 | sig = esig.Packscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/x509tools`/EcdsaSignature#Pack().() |
| 155 | } |
| 156 | signature.CreateElementscip-go gomod github.com/beevik/etree v1.1.0 `github.com/beevik/etree`/Element#CreateElement().("SignatureValue").SetTextscip-go gomod github.com/beevik/etree v1.1.0 `github.com/beevik/etree`/Element#SetText().(base64scip-go gomod github.com/golang/go/src go1.18 `encoding/base64`/.StdEncodingscip-go gomod github.com/golang/go/src go1.18 `encoding/base64`/StdEncoding..EncodeToStringscip-go gomod github.com/golang/go/src go1.18 `encoding/base64`/Encoding#EncodeToString().(sig)) |
| 157 | keyinfo := etreescip-go gomod github.com/beevik/etree v1.1.0 `github.com/beevik/etree`/.NewElementscip-go gomod github.com/beevik/etree v1.1.0 `github.com/beevik/etree`/NewElement().("KeyInfo") |
| 158 | if opts.IncludeKeyValuescip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/xmldsig`/SignOptions#IncludeKeyValue. { |
| 159 | if err := addKeyInfoscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/xmldsig`/addKeyInfo().(keyinfo, privKey.Publicscip-go gomod github.com/golang/go/src go1.18 crypto/Signer#Public().()); err != nil { |
| 160 | return err |
| 161 | } |
| 162 | } |
| 163 | if opts.IncludeX509scip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/xmldsig`/SignOptions#IncludeX509. && len(certs) > 0 { |
| 164 | addCertsscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/xmldsig`/addCerts().(keyinfo, certs) |
| 165 | } |
| 166 | if len(keyinfo.Childscip-go gomod github.com/beevik/etree v1.1.0 `github.com/beevik/etree`/Element#Child.) > 0 { |
| 167 | signature.AddChildscip-go gomod github.com/beevik/etree v1.1.0 `github.com/beevik/etree`/Element#AddChild().(keyinfo) |
| 168 | } |
| 169 | return nil |
| 170 | } |
| 171 | |
| 172 | func hashCanonscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/xmldsig`/hashCanon().(root *etreescip-go gomod github.com/beevik/etree v1.1.0 `github.com/beevik/etree`/.Elementscip-go gomod github.com/beevik/etree v1.1.0 `github.com/beevik/etree`/Element#, hash cryptoscip-go gomod github.com/golang/go/src go1.18 crypto/.Hashscip-go gomod github.com/golang/go/src go1.18 crypto/Hash#) ([]byte, error) { |
| 173 | canon, err := SerializeCanonicalscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/xmldsig`/SerializeCanonical().(root) |
| 174 | if err != nil { |
| 175 | return nil, fmtscip-go gomod github.com/golang/go/src go1.18 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.18 fmt/Errorf().("xmldsig: %w", err) |
| 176 | } |
| 177 | d := hash.Newscip-go gomod github.com/golang/go/src go1.18 crypto/Hash#New().() |
| 178 | d.Writescip-go gomod github.com/golang/go/src go1.18 io/Writer#Write().(canon) |
| 179 | return d.Sumscip-go gomod github.com/golang/go/src go1.18 hash/Hash#Sum().(nil), nil |
| 180 | } |
| 181 | |
| 182 | // Remove all child elements with this tag from the element |
| 183 | func RemoveElementsscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/xmldsig`/RemoveElements().(root *etreescip-go gomod github.com/beevik/etree v1.1.0 `github.com/beevik/etree`/.Elementscip-go gomod github.com/beevik/etree v1.1.0 `github.com/beevik/etree`/Element#, tag string) { |
| 184 | for i := 0; i < len(root.Childscip-go gomod github.com/beevik/etree v1.1.0 `github.com/beevik/etree`/Element#Child.); { |
| 185 | token := root.Childscip-go gomod github.com/beevik/etree v1.1.0 `github.com/beevik/etree`/Element#Child.[i] |
| 186 | if elem, ok := token.(*etreescip-go gomod github.com/beevik/etree v1.1.0 `github.com/beevik/etree`/.Elementscip-go gomod github.com/beevik/etree v1.1.0 `github.com/beevik/etree`/Element#); ok && elem.Tagscip-go gomod github.com/beevik/etree v1.1.0 `github.com/beevik/etree`/Element#Tag. == tag { |
| 187 | root.Childscip-go gomod github.com/beevik/etree v1.1.0 `github.com/beevik/etree`/Element#Child. = append(root.Childscip-go gomod github.com/beevik/etree v1.1.0 `github.com/beevik/etree`/Element#Child.[:i], root.Childscip-go gomod github.com/beevik/etree v1.1.0 `github.com/beevik/etree`/Element#Child.[i+1:]...) |
| 188 | } else { |
| 189 | i++ |
| 190 | } |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | // Determine algorithm URIs for hashing and signing |
| 195 | func hashAlgsscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/xmldsig`/hashAlgs().(hash cryptoscip-go gomod github.com/golang/go/src go1.18 crypto/.Hashscip-go gomod github.com/golang/go/src go1.18 crypto/Hash#, pubKey cryptoscip-go gomod github.com/golang/go/src go1.18 crypto/.PublicKeyscip-go gomod github.com/golang/go/src go1.18 crypto/PublicKey#, opts SignOptionsscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/xmldsig`/SignOptions#) (string, string, error) { |
| 196 | hashName := hashNamesscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/xmldsig`/hashNames.[hash] |
| 197 | if hashName == "" { |
| 198 | return "", "", errorsscip-go gomod github.com/golang/go/src go1.18 errors/.Newscip-go gomod github.com/golang/go/src go1.18 errors/New().("unsupported hash type") |
| 199 | } |
| 200 | var pubName string |
| 201 | switch pubKey.(type) { |
| 202 | case *rsascip-go gomod github.com/golang/go/src go1.18 `crypto/rsa`/.PublicKeyscip-go gomod github.com/golang/go/src go1.18 `crypto/rsa`/PublicKey#: |
| 203 | pubName = "rsa" |
| 204 | case *ecdsascip-go gomod github.com/golang/go/src go1.18 `crypto/ecdsa`/.PublicKeyscip-go gomod github.com/golang/go/src go1.18 `crypto/ecdsa`/PublicKey#: |
| 205 | pubName = "ecdsa" |
| 206 | default: |
| 207 | return "", "", errorsscip-go gomod github.com/golang/go/src go1.18 errors/.Newscip-go gomod github.com/golang/go/src go1.18 errors/New().("unsupported key type") |
| 208 | } |
| 209 | var hashAlg, sigAlg string |
| 210 | if opts.MsCompatHashNamesscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/xmldsig`/SignOptions#MsCompatHashNames. { |
| 211 | hashAlg = NsXMLDsigscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/xmldsig`/NsXMLDsig. + hashName |
| 212 | } else { |
| 213 | hashAlg = HashUrisscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/xmldsig`/HashUris.[hash] |
| 214 | } |
| 215 | if pubName == "rsa" && (hashName == "sha1" || opts.MsCompatHashNamesscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/xmldsig`/SignOptions#MsCompatHashNames.) { |
| 216 | sigAlg = NsXMLDsigscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/xmldsig`/NsXMLDsig. + pubName + "-" + hashName |
| 217 | } else { |
| 218 | sigAlg = NsXMLDsigMorescip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/xmldsig`/NsXMLDsigMore. + pubName + "-" + hashName |
| 219 | } |
| 220 | return hashAlg, sigAlg, nil |
| 221 | } |
| 222 | |
| 223 | // Add public key and optional X509 certificate chain to KeyInfo |
| 224 | func addKeyInfoscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/xmldsig`/addKeyInfo().(keyinfo *etreescip-go gomod github.com/beevik/etree v1.1.0 `github.com/beevik/etree`/.Elementscip-go gomod github.com/beevik/etree v1.1.0 `github.com/beevik/etree`/Element#, pubKey cryptoscip-go gomod github.com/golang/go/src go1.18 crypto/.PublicKeyscip-go gomod github.com/golang/go/src go1.18 crypto/PublicKey#) error { |
| 225 | keyvalue := keyinfo.CreateElementscip-go gomod github.com/beevik/etree v1.1.0 `github.com/beevik/etree`/Element#CreateElement().("KeyValue") |
| 226 | switch k := pubKey.(type) { |
| 227 | case *rsascip-go gomod github.com/golang/go/src go1.18 `crypto/rsa`/.PublicKeyscip-go gomod github.com/golang/go/src go1.18 `crypto/rsa`/PublicKey#: |
| 228 | e := bigscip-go gomod github.com/golang/go/src go1.18 `math/big`/.NewIntscip-go gomod github.com/golang/go/src go1.18 `math/big`/NewInt().(int64(k.Escip-go gomod github.com/golang/go/src go1.18 `crypto/rsa`/PublicKey#E.)) |
| 229 | rkv := keyvalue.CreateElementscip-go gomod github.com/beevik/etree v1.1.0 `github.com/beevik/etree`/Element#CreateElement().("RSAKeyValue") |
| 230 | rkv.CreateElementscip-go gomod github.com/beevik/etree v1.1.0 `github.com/beevik/etree`/Element#CreateElement().("Modulus").SetTextscip-go gomod github.com/beevik/etree v1.1.0 `github.com/beevik/etree`/Element#SetText().(base64scip-go gomod github.com/golang/go/src go1.18 `encoding/base64`/.StdEncodingscip-go gomod github.com/golang/go/src go1.18 `encoding/base64`/StdEncoding..EncodeToStringscip-go gomod github.com/golang/go/src go1.18 `encoding/base64`/Encoding#EncodeToString().(k.Nscip-go gomod github.com/golang/go/src go1.18 `crypto/rsa`/PublicKey#N..Bytesscip-go gomod github.com/golang/go/src go1.18 `math/big`/Int#Bytes().())) |
| 231 | rkv.CreateElementscip-go gomod github.com/beevik/etree v1.1.0 `github.com/beevik/etree`/Element#CreateElement().("Exponent").SetTextscip-go gomod github.com/beevik/etree v1.1.0 `github.com/beevik/etree`/Element#SetText().(base64scip-go gomod github.com/golang/go/src go1.18 `encoding/base64`/.StdEncodingscip-go gomod github.com/golang/go/src go1.18 `encoding/base64`/StdEncoding..EncodeToStringscip-go gomod github.com/golang/go/src go1.18 `encoding/base64`/Encoding#EncodeToString().(e.Bytesscip-go gomod github.com/golang/go/src go1.18 `math/big`/Int#Bytes().())) |
| 232 | case *ecdsascip-go gomod github.com/golang/go/src go1.18 `crypto/ecdsa`/.PublicKeyscip-go gomod github.com/golang/go/src go1.18 `crypto/ecdsa`/PublicKey#: |
| 233 | curve, err := x509toolsscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/x509tools`/.CurveByCurvescip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/x509tools`/CurveByCurve().(k.Curvescip-go gomod github.com/golang/go/src go1.18 `crypto/ecdsa`/PublicKey#Curve.) |
| 234 | if err != nil { |
| 235 | return err |
| 236 | } |
| 237 | curveUrn := fmtscip-go gomod github.com/golang/go/src go1.18 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.18 fmt/Sprintf().("urn:oid:%s", curve.Oidscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/x509tools`/CurveDefinition#Oid.) |
| 238 | ekv := keyvalue.CreateElementscip-go gomod github.com/beevik/etree v1.1.0 `github.com/beevik/etree`/Element#CreateElement().("ECDSAKeyValue") |
| 239 | ekv.CreateElementscip-go gomod github.com/beevik/etree v1.1.0 `github.com/beevik/etree`/Element#CreateElement().("DomainParameters").CreateElementscip-go gomod github.com/beevik/etree v1.1.0 `github.com/beevik/etree`/Element#CreateElement().("NamedCurve").CreateAttrscip-go gomod github.com/beevik/etree v1.1.0 `github.com/beevik/etree`/Element#CreateAttr().("URN", curveUrn) |
| 240 | pk := ekv.CreateElementscip-go gomod github.com/beevik/etree v1.1.0 `github.com/beevik/etree`/Element#CreateElement().("PublicKey") |
| 241 | x := pk.CreateElementscip-go gomod github.com/beevik/etree v1.1.0 `github.com/beevik/etree`/Element#CreateElement().("X") |
| 242 | x.CreateAttrscip-go gomod github.com/beevik/etree v1.1.0 `github.com/beevik/etree`/Element#CreateAttr().("Value", k.Xscip-go gomod github.com/golang/go/src go1.18 `crypto/ecdsa`/PublicKey#X..Stringscip-go gomod github.com/golang/go/src go1.18 `math/big`/Int#String().()) |
| 243 | x.CreateAttrscip-go gomod github.com/beevik/etree v1.1.0 `github.com/beevik/etree`/Element#CreateAttr().("xmlns:xsi", NsXsiscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/xmldsig`/NsXsi.) |
| 244 | x.CreateAttrscip-go gomod github.com/beevik/etree v1.1.0 `github.com/beevik/etree`/Element#CreateAttr().("xsi:type", "PrimeFieldElemType") |
| 245 | y := pk.CreateElementscip-go gomod github.com/beevik/etree v1.1.0 `github.com/beevik/etree`/Element#CreateElement().("Y") |
| 246 | y.CreateAttrscip-go gomod github.com/beevik/etree v1.1.0 `github.com/beevik/etree`/Element#CreateAttr().("Value", k.Yscip-go gomod github.com/golang/go/src go1.18 `crypto/ecdsa`/PublicKey#Y..Stringscip-go gomod github.com/golang/go/src go1.18 `math/big`/Int#String().()) |
| 247 | y.CreateAttrscip-go gomod github.com/beevik/etree v1.1.0 `github.com/beevik/etree`/Element#CreateAttr().("xmlns:xsi", NsXsiscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/xmldsig`/NsXsi.) |
| 248 | y.CreateAttrscip-go gomod github.com/beevik/etree v1.1.0 `github.com/beevik/etree`/Element#CreateAttr().("xsi:type", "PrimeFieldElemType") |
| 249 | default: |
| 250 | return errorsscip-go gomod github.com/golang/go/src go1.18 errors/.Newscip-go gomod github.com/golang/go/src go1.18 errors/New().("unsupported key type") |
| 251 | } |
| 252 | return nil |
| 253 | } |
| 254 | |
| 255 | func addCertsscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/xmldsig`/addCerts().(keyinfo *etreescip-go gomod github.com/beevik/etree v1.1.0 `github.com/beevik/etree`/.Elementscip-go gomod github.com/beevik/etree v1.1.0 `github.com/beevik/etree`/Element#, certs []*x509scip-go gomod github.com/golang/go/src go1.18 `crypto/x509`/.Certificatescip-go gomod github.com/golang/go/src go1.18 `crypto/x509`/Certificate#) { |
| 256 | x509data := keyinfo.CreateElementscip-go gomod github.com/beevik/etree v1.1.0 `github.com/beevik/etree`/Element#CreateElement().("X509Data") |
| 257 | for _, cert := range certs { |
| 258 | x509data.CreateElementscip-go gomod github.com/beevik/etree v1.1.0 `github.com/beevik/etree`/Element#CreateElement().("X509Certificate").SetTextscip-go gomod github.com/beevik/etree v1.1.0 `github.com/beevik/etree`/Element#SetText().(base64scip-go gomod github.com/golang/go/src go1.18 `encoding/base64`/.StdEncodingscip-go gomod github.com/golang/go/src go1.18 `encoding/base64`/StdEncoding..EncodeToStringscip-go gomod github.com/golang/go/src go1.18 `encoding/base64`/Encoding#EncodeToString().(cert.Rawscip-go gomod github.com/golang/go/src go1.18 `crypto/x509`/Certificate#Raw.)) |
| 259 | } |
| 260 | } |