| /* |
| Copyright 2017 Google Inc. |
| |
| Licensed under the Apache License, Version 2.0 (the "License"); |
| you may not use this file except in compliance with the License. |
| You may obtain a copy of the License at |
| |
| http://www.apache.org/licenses/LICENSE-2.0 |
| |
| Unless required by applicable law or agreed to in writing, software |
| distributed under the License is distributed on an "AS IS" BASIS, |
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| See the License for the specific language governing permissions and |
| limitations under the License. |
| */ |
| |
| package mock |
| |
| import ( |
| "crypto/rand" |
| "crypto/rsa" |
| "crypto/x509" |
| "crypto/x509/pkix" |
| "encoding/pem" |
| "math/big" |
| "time" |
| ) |
| |
| var mockKey *rsa.PrivateKey |
| var mockKeyPEM, mockCertPEM []byte |
| |
| func makeKeys() { |
| key, err := rsa.GenerateKey(rand.Reader, 2048) |
| if err != nil { |
| panic(err.Error()) |
| } |
| mockKey = key |
| |
| mockKeyPEM = pem.EncodeToMemory(&pem.Block{ |
| Type: "RSA PRIVATE KEY", |
| Bytes: x509.MarshalPKCS1PrivateKey(key), |
| }) |
| |
| templ := makeCertTemplate() |
| |
| certBytes, err := x509.CreateCertificate(rand.Reader, templ, templ, key.Public(), key) |
| if err != nil { |
| panic(err.Error()) |
| } |
| |
| mockCertPEM = pem.EncodeToMemory(&pem.Block{ |
| Type: "CERTIFICATE", |
| Bytes: certBytes, |
| }) |
| } |
| |
| func makeCertTemplate() *x509.Certificate { |
| now := time.Now() |
| |
| return &x509.Certificate{ |
| SerialNumber: big.NewInt(1), |
| Subject: pkix.Name{ |
| Country: []string{"US"}, |
| Organization: []string{"Google"}, |
| OrganizationalUnit: []string{"Cloud"}, |
| Locality: []string{"Mountain View"}, |
| Province: []string{"CA"}, |
| CommonName: "mockserver", |
| }, |
| NotBefore: now, |
| NotAfter: now.Add(time.Hour), |
| } |
| } |