|
| 1 | +// Copyright 2026 The Tessera authors. All Rights Reserved. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +// pemtovkey is a utility to convert a PEM containing an ML-DSA-44 key and MTC CA subject to a vkey representation. |
| 16 | +package main |
| 17 | + |
| 18 | +import ( |
| 19 | + "crypto/sha256" |
| 20 | + "crypto/x509" |
| 21 | + "encoding/asn1" |
| 22 | + "encoding/base64" |
| 23 | + "encoding/binary" |
| 24 | + "encoding/pem" |
| 25 | + "flag" |
| 26 | + "fmt" |
| 27 | + "os" |
| 28 | + |
| 29 | + fnote "github.com/transparency-dev/formats/note" |
| 30 | +) |
| 31 | + |
| 32 | +const ( |
| 33 | + algMLDSA44 = 0x06 |
| 34 | +) |
| 35 | + |
| 36 | +var ( |
| 37 | + certFile = flag.String("cert", "", "Path to x509 PEM file to read") |
| 38 | + |
| 39 | + mtcTrustAnchorExperimental = asn1.ObjectIdentifier{1, 3, 6, 1, 4, 1, 44363, 47, 1} |
| 40 | +) |
| 41 | + |
| 42 | +func main() { |
| 43 | + flag.Parse() |
| 44 | + |
| 45 | + pubKBytes, name := keyAndIDFromCert(*certFile) |
| 46 | + |
| 47 | + h := keyHashMLDSA(name, pubKBytes) |
| 48 | + vkey := fmt.Sprintf("%s+%08x+%s", name, h, base64.StdEncoding.EncodeToString(pubKBytes)) |
| 49 | + |
| 50 | + if _, err := fnote.NewVerifierForCosignatureV1(vkey); err != nil { |
| 51 | + exit("Generated vkey is invalid: %v", err) |
| 52 | + } |
| 53 | + |
| 54 | + fmt.Println(vkey) |
| 55 | +} |
| 56 | + |
| 57 | +func keyAndIDFromCert(path string) ([]byte, string) { |
| 58 | + c, err := os.ReadFile(path) |
| 59 | + if err != nil { |
| 60 | + exit("Failed to read file %q: %v", path, err) |
| 61 | + } |
| 62 | + |
| 63 | + der, _ := pem.Decode(c) |
| 64 | + crt, err := x509.ParseCertificate(der.Bytes) |
| 65 | + if err != nil { |
| 66 | + exit("Failed to parse certificate: %v", err) |
| 67 | + } |
| 68 | + |
| 69 | + var relOID string |
| 70 | + for _, n := range crt.Subject.Names { |
| 71 | + if mtcTrustAnchorExperimental.Equal(n.Type) { |
| 72 | + v, ok := n.Value.(string) |
| 73 | + if !ok { |
| 74 | + exit("Found MTC trust anchor ID %q, but couldn't convert value to a string", n) |
| 75 | + } |
| 76 | + relOID = v |
| 77 | + break |
| 78 | + } |
| 79 | + } |
| 80 | + if relOID == "" { |
| 81 | + exit("No MTC trust anchor ID present in cert.") |
| 82 | + } |
| 83 | + name := fmt.Sprintf("oid/1.3.6.1.4.1.44363.47.1.%s", relOID) |
| 84 | + |
| 85 | + |
| 86 | + pub := crt.RawSubjectPublicKeyInfo[22:] |
| 87 | + if l := len(pub); l != 1312 { |
| 88 | + exit("Invalid public key length %d, expected 1312", l) |
| 89 | + } |
| 90 | + |
| 91 | + return append([]byte{algMLDSA44}, pub...), name |
| 92 | +} |
| 93 | + |
| 94 | +func keyHashMLDSA(name string, key []byte) uint32 { |
| 95 | + h := sha256.New() |
| 96 | + h.Write([]byte(name)) |
| 97 | + h.Write([]byte("\n")) |
| 98 | + h.Write(key) |
| 99 | + sum := h.Sum(nil) |
| 100 | + return binary.BigEndian.Uint32(sum) |
| 101 | +} |
| 102 | + |
| 103 | +func exit(m string, a ...any) { |
| 104 | + fmt.Printf(m, a...) |
| 105 | + os.Exit(1) |
| 106 | +} |
0 commit comments