Bump project version to 212.1.
[macgdbp.git] / dev / asn1-wrap-ed25519.go
1 /*
2 * MacGDBp
3 * Copyright (c) 2020, Blue Static <https://www.bluestatic.org>
4 *
5 * This program is free software; you can redistribute it and/or modify it under the terms of the GNU
6 * General Public License as published by the Free Software Foundation; either version 2 of the
7 * License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
10 * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along with this program; if not,
14 * write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
15 */
16
17 /*
18 asn1-wrap-ed25519 takes a raw private key from signer-ed25519 and reencodes it as PEM PKCS8 (ASN.1).
19
20 The raw keys generated by signer-ed25519 are not compatible with the OpenSSL
21 command. By storing it in PKCS8, the keys can be used with openssl commands.
22
23 Usage:
24
25 ./signer-ed25519 -new-key > out.key
26 ./asn1-wrap-ed25519 out.key > out-wrapped.key
27 */
28 package main
29
30 import (
31 "crypto/ed25519"
32 "crypto/x509"
33 "encoding/pem"
34 "fmt"
35 "io/ioutil"
36 "os"
37 )
38
39 func main() {
40 keyfile := os.Args[1]
41
42 keyPemData, err := ioutil.ReadFile(keyfile)
43 if err != nil {
44 fmt.Fprintf(os.Stderr, "Failed to read key: %v\n", err)
45 os.Exit(1)
46 }
47
48 keyPem, _ := pem.Decode(keyPemData)
49 if keyPem == nil {
50 fmt.Fprintf(os.Stderr, "Failed to decode PEM: %v\n", err)
51 os.Exit(1)
52 }
53
54 key := ed25519.PrivateKey(keyPem.Bytes)
55
56 asn1Bytes, err := x509.MarshalPKCS8PrivateKey(key)
57 if err != nil {
58 fmt.Fprintf(os.Stderr, "Failed to ASN.1 encode key: %v\n", err)
59 os.Exit(1)
60 }
61
62 asn1Pem := &pem.Block{Type: "ED25519 PRIVATE KEY", Bytes: asn1Bytes}
63
64 err = pem.Encode(os.Stdout, asn1Pem)
65 if err != nil {
66 fmt.Println("Failed to PEM-encode ASN.1 key: %v\n", err)
67 os.Exit(1)
68 }
69 }