diff --git a/util/cert/cert.go b/util/cert/cert.go deleted file mode 100644 index 74e10023..00000000 --- a/util/cert/cert.go +++ /dev/null @@ -1,96 +0,0 @@ -// Copyright © 2016 Abcum Ltd -// -// 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 cert - -import ( - "fmt" - "os" - - "encoding/pem" -) - -func Extract(enc, key, crt string) (err error) { - - var file *os.File - - data := []byte(enc) - - pemk, pemc := extract(data) - if pemk == nil || pemc == nil { - return fmt.Errorf("Can not decode PEM encoded file") - } - - file, err = os.Create(key) - if err != nil { - return fmt.Errorf("Can not decode PEM encoded private key into %s", key) - } - file.Write(pemk) - file.Close() - - file, err = os.Create(crt) - if err != nil { - return fmt.Errorf("Can not decode PEM encoded certificate into %s", crt) - } - file.Write(pemc) - file.Close() - - return - -} - -func extract(data []byte) (key []byte, crt []byte) { - - key = extractKey(data) - crt = extractCrt(data) - - return - -} - -func extractKey(data []byte) (output []byte) { - - var block *pem.Block - - for len(data) > 0 { - block, data = pem.Decode(data) - if block == nil { - break - } - if block.Type == "RSA PRIVATE KEY" { - output = append(output, pem.EncodeToMemory(block)...) - } - } - - return - -} - -func extractCrt(data []byte) (output []byte) { - - var block *pem.Block - - for len(data) > 0 { - block, data = pem.Decode(data) - if block == nil { - break - } - if block.Type == "CERTIFICATE" { - output = append(output, pem.EncodeToMemory(block)...) - } - } - - return - -} diff --git a/util/cert/cert_test.go b/util/cert/cert_test.go deleted file mode 100644 index 00a73bca..00000000 --- a/util/cert/cert_test.go +++ /dev/null @@ -1,141 +0,0 @@ -// Copyright © 2016 Abcum Ltd -// -// 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 cert - -import ( - "os" - "testing" - - . "github.com/smartystreets/goconvey/convey" -) - -var enc = `-----BEGIN RSA PRIVATE KEY----- -MIIJKAIBAAKCAgEAuHh3CRgdIEtTLwSbGV3ekfE4udLTBAY6m3sr/3Dw1aarc2o6 -BGjxq9tZJToOMrrIVnSuk8Sk1x6U0z/ycW2qJikvZYbjg6q4JX8X9ylYJWqqBO2T -1LIoY7IfG6iOEsulcezl9pDlWVpv0fOrHtR75mjx376JGao3Evk9MLzt5qMzIAam -VN2fbYMqsi26ih9BaTLHAXdoD+A+vZ8w8J0IG/ARdQBu9PdOXO2hhzk4Carfxhni -Koqo2etgKQg42skof6GH04QlcS5/0EV2mdnenWuoTijpkmvNqbzJhuTJ28B3RSBw -sA3ckBQEDri1FtX1Uqxw3g3qHD4NEcCT0WErBRB5W0AI1vsRQOhPInouiM0XPpem -ZiNETC+DFNQ7gG10g3OUTLS64SvJq8BHH6xH1WP8ND5vix5ilDXp0CtDzzYEOoJa -nz+MJGLFgkPYGqgWXEDsmcIcTsVPblFRXjyDQ9fnb4U4oz1WvurRDMamCT875RwZ -CDpxjIPAJMyir1u4vGuxY60PAF/NyOeHjf7k8eMVzMjvKvG0TjR6mOF4DNLb6T+z -Vijcq8wynPs8D1jGhYrsvGvhkkk/9B6Ty7TWvg7lqrRpxw0QalSrgFclNQ5tAkem -RxcXGsNHKvFYZJpEfo5fHGVnt/1m1tvmmniladOlkeK9JGYaq4MhGSBxCfECAwEA -AQKCAgAtv4pd0gIfKS8XAQBp+RKihP05cGRuohDU0GYn8l13zt7EP7rlrxUINtzT -06MDx7i5YDSPZvwucWIdRcWdhcHltt8PQbWYyLTkb7GE1VcL6HkDwdugqxJDL0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KNyr -zDKc+zwPWMaFiuy8a+GSST/0HpPLtNa+DuWqtGnHDRBqVKuAVyU1Dm0CR6ZHFxca -w0cq8VhkmkR+jl8cZWe3/WbW2+aaeKVp06WR4r0kZhqrgyEZIHEJ8QIDAQABozUw -MzAOBgNVHQ8BAf8EBAMCAvwwEwYDVR0lBAwwCgYIKwYBBQUHAwEwDAYDVR0TAQH/ -BAIwADANBgkqhkiG9w0BAQ0FAAOCAgEATIGvRoQdFrOiyLPMsNC3I49fUu5sfxse -OesCgDfzIXgUxZuHTiSbon6QP3+YfrRvWunuhl68xlQLZk7dBhZ0CM7Qe/TS06vL -Shjp2BsuJs2Ej8Vw0xuqAjCkuSOXqsu3Lt3G6edSrsZNBoi+76/uIOnaoZ19N4n5 -R5p6tggwRHf7xzggjvmRx+iyjyn3uY8LkN57xNZEjFxI9MihnKrUx9NofsdfYpJ3 -VTQoXN+dIPFO+PFCmZ4/EVd/hxc/kKHxiUXQ9Zs6KFEYkh4mVmt6BT3mO6U4bE+3 -aZSKx7JJs0A9USyWUP7p6P7QMCBtY+UxXqY5CKIyi0i/3+REbdmXfIQPeolliiqI -M17g5KCJO8wbBRzyzIsL14KkXj4zaSud9vBcJR6kJ6skOKM629zPyKtTlE56bBEy -Bi3woBxcw9E71Cd9Qbibv34NXU7lagv4a+Mz8pKnRXSbVTEHgPbfqFdnK4+OYDee -7HUqZKacL1+CT2nxo/ll1Bb93SUrSz72dJKZdSDUJ1YiwfhAX0LYdgFYihGFyftf -TKMZpHXH4xE0TVPv6y/jpMEH/6EKbxgYyJUwaJndSEBuCEMEUzxwr0/EXY2shk0R -4aigCFBVyrwOTlzoVJcNx5jEMgBQ8Npvxpm898GxZssDJpk74AICs3iy7OjDcKLn -EPJLOqCsvLs= ------END CERTIFICATE----- -` - -func TestValid(t *testing.T) { - - Convey("PEM encoded file should extract", t, func() { - err := Extract(enc, "test.crt", "test.key") - So(err, ShouldBeNil) - os.Remove("test.crt") - os.Remove("test.key") - }) - -} - -func TestInvalid(t *testing.T) { - - Convey("Unknown encoded file should not extract", t, func() { - err := Extract("err", "test.crt", "test.key") - So(err, ShouldNotBeNil) - os.Remove("test.crt") - os.Remove("test.key") - }) - - Convey("Certificate file can not be created", t, func() { - err := Extract(enc, "nofolder/test.crt", "test.key") - So(err, ShouldNotBeNil) - os.Remove("test.crt") - os.Remove("test.key") - }) - - Convey("Private key file can not be created", t, func() { - err := Extract(enc, "test.crt", "nofolder/test.key") - So(err, ShouldNotBeNil) - os.Remove("test.crt") - os.Remove("test.key") - }) - -}