| 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 x509tools |
| 18 | |
| 19 | import ( |
| 20 | "crypto" |
| 21 | "crypto/x509/pkix" |
| 22 | "encoding/asn1" |
| 23 | "strings" |
| 24 | "sync" |
| 25 | ) |
| 26 | |
| 27 | var ( |
| 28 | // RFC 3279 |
| 29 | OidDigestMD5 = asn1.ObjectIdentifier{1, 2, 840, 113549, 2, 5} |
| 30 | OidDigestSHA1 = asn1.ObjectIdentifier{1, 3, 14, 3, 2, 26} |
| 31 | // RFC 5758 |
| 32 | OidDigestSHA224 = asn1.ObjectIdentifier{2, 16, 840, 1, 101, 3, 4, 2, 4} |
| 33 | OidDigestSHA256 = asn1.ObjectIdentifier{2, 16, 840, 1, 101, 3, 4, 2, 1} |
| 34 | OidDigestSHA384 = asn1.ObjectIdentifier{2, 16, 840, 1, 101, 3, 4, 2, 2} |
| 35 | OidDigestSHA512 = asn1.ObjectIdentifier{2, 16, 840, 1, 101, 3, 4, 2, 3} |
| 36 | ) |
| 37 | |
| 38 | var HashOids = map[crypto.Hash]asn1.ObjectIdentifier{ |
| 39 | crypto.MD5: OidDigestMD5, |
| 40 | crypto.SHA1: OidDigestSHA1, |
| 41 | crypto.SHA224: OidDigestSHA224, |
| 42 | crypto.SHA256: OidDigestSHA256, |
| 43 | crypto.SHA384: OidDigestSHA384, |
| 44 | crypto.SHA512: OidDigestSHA512, |
| 45 | } |
| 46 | |
| 47 | var HashNames = map[crypto.Hash]string{ |
| 48 | crypto.MD5: "MD5", |
| 49 | crypto.SHA1: "SHA1", |
| 50 | crypto.SHA224: "SHA-224", |
| 51 | crypto.SHA256: "SHA-256", |
| 52 | crypto.SHA384: "SHA-384", |
| 53 | crypto.SHA512: "SHA-512", |
| 54 | } |
| 55 | |
| 56 | var ( |
| 57 | hashesByName map[string]crypto.Hash |
| 58 | once sync.Once |
| 59 | ) |
| 60 | |
| 61 | func HashShortName(hash crypto.Hash) string { |
| 62 | return normalName(HashNames[hash]) |
| 63 | } |
| 64 | |
| 65 | func normalName(name string) string { |
| 66 | return strings.Replace(strings.ToLower(name), "-", "", 1) |
| 67 | } |
| 68 | |
| 69 | func HashByName(name string) crypto.Hash { |
| 70 | name = normalName(name) |
| 71 | once.Do(func() { |
| 72 | hashesByName = make(map[string]crypto.Hash, len(HashNames)) |
| 73 | for h, hn := range HashNames { |
| 74 | hashesByName[normalName(hn)] = h |
| 75 | } |
| 76 | }) |
| 77 | return hashesByName[name] |
| 78 | } |
| 79 | |
| 80 | type digestInfo struct { |
| 81 | DigestAlgorithm pkix.AlgorithmIdentifier |
| 82 | Digest []byte |
| 83 | } |
| 84 | |
| 85 | // Pack a digest along with an algorithm identifier. Mainly useful for |
| 86 | // PKCS#1v1.5 padding (RSA). |
| 87 | func MarshalDigest(hash crypto.Hash, digest []byte) (der []byte, ok bool) { |
| 88 | alg, ok := PkixDigestAlgorithm(hash) |
| 89 | if !ok { |
| 90 | return nil, false |
| 91 | } |
| 92 | der, err := asn1.Marshal(digestInfo{alg, digest}) |
| 93 | if err != nil { |
| 94 | return nil, false |
| 95 | } |
| 96 | return der, true |
| 97 | } |