github.com/adnan-c/fabric_e2e_couchdb@v0.6.1-preview.0.20170228180935-21ce6b23cf91/orderer/mocks/util/util.go (about) 1 /* 2 Copyright IBM Corp. 2017 All Rights Reserved. 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 util 18 19 import ( 20 "crypto/rand" 21 "crypto/rsa" 22 "crypto/x509" 23 "crypto/x509/pkix" 24 "encoding/pem" 25 "math/big" 26 ) 27 28 // GenerateMockPublicPrivateKeyPairPEM returns public/private key pair encoded as PEM strings 29 func GenerateMockPublicPrivateKeyPairPEM(isCA bool) (string, string, error) { 30 privateKey, err := rsa.GenerateKey(rand.Reader, 1024) 31 if err != nil { 32 return "", "", err 33 } 34 privateKeyPEM := string(pem.EncodeToMemory( 35 &pem.Block{ 36 Type: "RSA PRIVATE KEY", 37 Bytes: x509.MarshalPKCS1PrivateKey(privateKey), 38 }, 39 )) 40 41 template := x509.Certificate{ 42 SerialNumber: big.NewInt(100), 43 Subject: pkix.Name{ 44 Organization: []string{"Hyperledger Fabric"}, 45 }, 46 } 47 if isCA { 48 template.IsCA = true 49 template.KeyUsage |= x509.KeyUsageCertSign 50 } 51 52 publicKeyCert, err := x509.CreateCertificate(rand.Reader, &template, &template, privateKey.Public(), privateKey) 53 if err != nil { 54 return "", "", err 55 } 56 57 publicKeyCertPEM := string(pem.EncodeToMemory( 58 &pem.Block{ 59 Type: "CERTIFICATE", 60 Bytes: publicKeyCert, 61 }, 62 )) 63 64 return publicKeyCertPEM, privateKeyPEM, nil 65 }