| 1 |
// Copyright 2023 Google LLC |
| 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 |
// test_signer.go is a net/rpc server that listens on stdin/stdout, exposing |
| 16 |
// mock methods for testing enterprise certificate proxy flow. |
| 17 |
package main |
| 18 |
|
| 19 |
import ( |
| 20 |
"crypto" |
| 21 |
"crypto/tls" |
| 22 |
"crypto/x509" |
| 23 |
"io" |
| 24 |
"log" |
| 25 |
"net/rpc" |
| 26 |
"os" |
| 27 |
"time" |
| 28 |
) |
| 29 |
|
| 30 |
// SignArgs encapsulate the parameters for the Sign method. |
| 31 |
type SignArgs struct { |
| 32 |
Digest []byte |
| 33 |
Opts crypto.SignerOpts |
| 34 |
} |
| 35 |
|
| 36 |
// EnterpriseCertSigner exports RPC methods for signing. |
| 37 |
type EnterpriseCertSigner struct { |
| 38 |
cert *tls.Certificate |
| 39 |
} |
| 40 |
|
| 41 |
// Connection wraps a pair of unidirectional streams as an io.ReadWriteCloser. |
| 42 |
type Connection struct { |
| 43 |
io.ReadCloser |
| 44 |
io.WriteCloser |
| 45 |
} |
| 46 |
|
| 47 |
// Close closes c's underlying ReadCloser and WriteCloser. |
| 48 |
func (c *Connection) Close() error { |
| 49 |
rerr := c.ReadCloser.Close() |
| 50 |
werr := c.WriteCloser.Close() |
| 51 |
if rerr != nil { |
| 52 |
return rerr |
| 53 |
} |
| 54 |
return werr |
| 55 |
} |
| 56 |
|
| 57 |
// CertificateChain returns the credential as a raw X509 cert chain. This |
| 58 |
// contains the public key. |
| 59 |
func (k *EnterpriseCertSigner) CertificateChain(ignored struct{}, certificateChain *[][]byte) error { |
| 60 |
*certificateChain = k.cert.Certificate |
| 61 |
return nil |
| 62 |
} |
| 63 |
|
| 64 |
// Public returns the first public key for this Key, in ASN.1 DER form. |
| 65 |
func (k *EnterpriseCertSigner) Public(ignored struct{}, publicKey *[]byte) (err error) { |
| 66 |
if len(k.cert.Certificate) == 0 { |
| 67 |
return nil |
| 68 |
} |
| 69 |
cert, err := x509.ParseCertificate(k.cert.Certificate[0]) |
| 70 |
if err != nil { |
| 71 |
return err |
| 72 |
} |
| 73 |
*publicKey, err = x509.MarshalPKIXPublicKey(cert.PublicKey) |
| 74 |
return err |
| 75 |
} |
| 76 |
|
| 77 |
// Sign signs a message by encrypting a message digest. |
| 78 |
func (k *EnterpriseCertSigner) Sign(args SignArgs, resp *[]byte) (err error) { |
| 79 |
return nil |
| 80 |
} |
| 81 |
|
| 82 |
func main() { |
| 83 |
enterpriseCertSigner := new(EnterpriseCertSigner) |
| 84 |
|
| 85 |
data, err := os.ReadFile(os.Args[1]) |
| 86 |
if err != nil { |
| 87 |
log.Fatalf("Error reading certificate: %v", err) |
| 88 |
} |
| 89 |
cert, _ := tls.X509KeyPair(data, data) |
| 90 |
|
| 91 |
enterpriseCertSigner.cert = &cert |
| 92 |
|
| 93 |
if err := rpc.Register(enterpriseCertSigner); err != nil { |
| 94 |
log.Fatalf("Error registering net/rpc: %v", err) |
| 95 |
} |
| 96 |
|
| 97 |
// If the parent process dies, we should exit. |
| 98 |
// We can detect this by periodically checking if the PID of the parent |
| 99 |
// process is 1 (https://stackoverflow.com/a/2035683). |
| 100 |
go func() { |
| 101 |
for { |
| 102 |
if os.Getppid() == 1 { |
| 103 |
log.Fatalln("Parent process died, exiting...") |
| 104 |
} |
| 105 |
time.Sleep(time.Second) |
| 106 |
} |
| 107 |
}() |
| 108 |
|
| 109 |
rpc.ServeConn(&Connection{os.Stdin, os.Stdout}) |
| 110 |
} |