├── VERSION ├── utils ├── version.go ├── config.go ├── token.go ├── sender.go ├── parser.go └── login.go ├── cmd ├── kms │ ├── protobuf │ │ ├── readme │ │ └── decryptor.proto │ ├── generate_private_key.go │ ├── delete_update_token.go │ ├── get_update_token.go │ ├── list.go │ └── create.go ├── cards.go ├── purekit.go ├── scms │ ├── device.go │ ├── dcm.go │ ├── init.go │ ├── dcm │ │ ├── dcm_list.go │ │ └── dcm_create.go │ └── device │ │ └── device_list.go ├── scms.go ├── app │ ├── token.go │ ├── key.go │ ├── create.go │ ├── update.go │ ├── key │ │ ├── delete.go │ │ ├── update.go │ │ └── list.go │ ├── token │ │ ├── delete.go │ │ ├── create.go │ │ └── list.go │ ├── list.go │ └── delete.go ├── app.go ├── pure │ ├── keygen │ │ ├── util.go │ │ ├── hb.go │ │ ├── vs.go │ │ ├── backup.go │ │ ├── auth.go │ │ ├── nms.go │ │ ├── all.go │ │ ├── os.go │ │ └── sk.go │ └── keygen.go ├── kms.go ├── logout.go ├── login.go ├── keygen.go ├── use.go ├── sign.go ├── verify.go ├── register.go ├── decrypt.go ├── cards │ ├── revoke.go │ └── search.go ├── encrypt.go └── extract_pub_key.go ├── client └── protobuf │ ├── http_error.proto │ └── http_error.pb.go ├── .gitignore ├── test ├── helpers │ ├── generators.go │ ├── cmd.go │ └── mailinator.go ├── functional │ ├── init.go │ └── app_token_test.go └── fixtures │ └── fixtures.go ├── go.mod ├── .travis.yml ├── LICENSE ├── Makefile ├── models ├── key.go ├── account.go ├── scms.go └── application.go └── main.go /VERSION: -------------------------------------------------------------------------------- 1 | 5.2.9 2 | -------------------------------------------------------------------------------- /utils/version.go: -------------------------------------------------------------------------------- 1 | package utils 2 | 3 | var ( 4 | Version = "5.2.9" 5 | ) 6 | -------------------------------------------------------------------------------- /cmd/kms/protobuf/readme: -------------------------------------------------------------------------------- 1 | To generate source files run: 2 | 3 | $ protoc --go_out=. *.proto 4 | -------------------------------------------------------------------------------- /client/protobuf/http_error.proto: -------------------------------------------------------------------------------- 1 | syntax = "proto3"; 2 | package protobuf; 3 | 4 | message HttpError { 5 | uint32 code = 1; 6 | string message = 2; 7 | } 8 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Binaries for programs and plugins 2 | *.exe 3 | *.exe~ 4 | *.dll 5 | *.so 6 | *.dylib 7 | 8 | # Test binary, build with `go test -c` 9 | *.test 10 | 11 | # Output of the go coverage tool, specifically when used with LiteIDE 12 | *.out 13 | .idea/ 14 | *.yaml 15 | /vendor 16 | /virgil 17 | /artifacts 18 | -------------------------------------------------------------------------------- /test/helpers/generators.go: -------------------------------------------------------------------------------- 1 | package helpers 2 | 3 | import ( 4 | "crypto/rand" 5 | "encoding/hex" 6 | ) 7 | 8 | const ( 9 | DisposableEmailOperator = "@mailinator.com" 10 | ) 11 | 12 | func GenerateEmail() string { 13 | return GenerateString() + DisposableEmailOperator 14 | } 15 | 16 | func GeneratePassowrd() string { 17 | return GenerateString()[:29] 18 | } 19 | 20 | func GenerateString() string { 21 | randBytes := make([]byte, 32) 22 | _, _ = rand.Read(randBytes) 23 | return hex.EncodeToString(randBytes) 24 | } 25 | -------------------------------------------------------------------------------- /go.mod: -------------------------------------------------------------------------------- 1 | module github.com/VirgilSecurity/virgil-cli 2 | 3 | go 1.15 4 | 5 | require ( 6 | github.com/VirgilSecurity/virgil-sdk-go/v6 v6.0.15 7 | github.com/cpuguy83/go-md2man/v2 v2.0.0 // indirect 8 | github.com/golang/protobuf v1.4.2 9 | github.com/google/uuid v1.1.1 10 | github.com/howeyc/gopass v0.0.0-20190910152052-7cb4b85ec19c 11 | github.com/pkg/errors v0.9.1 12 | github.com/stretchr/testify v1.4.0 13 | github.com/urfave/cli/v2 v2.2.0 14 | golang.org/x/crypto v0.0.0-20200728195943-123391ffb6de // indirect 15 | golang.org/x/sys v0.0.0-20200812155832-6a926be9bd1d // indirect 16 | google.golang.org/protobuf v1.25.0 17 | gopkg.in/yaml.v2 v2.3.0 // indirect 18 | ) 19 | -------------------------------------------------------------------------------- /cmd/kms/protobuf/decryptor.proto: -------------------------------------------------------------------------------- 1 | syntax = "proto3"; 2 | package decryptor; 3 | 4 | option go_package = "cmd/kms/protobuf/decryptor"; 5 | 6 | message Keypair { 7 | uint32 version = 1; 8 | string alias = 2; 9 | uint32 key_version = 3; 10 | bytes public_key = 4; 11 | } 12 | 13 | message Keypairs { 14 | repeated Keypair keypairs = 1; 15 | } 16 | 17 | message KeypairRequest { 18 | string alias = 1; 19 | } 20 | 21 | message DecryptRequest { 22 | uint32 version = 1; 23 | string alias = 2; 24 | bytes request = 3; 25 | } 26 | 27 | message DecryptResponse { 28 | bytes response = 1; 29 | } 30 | 31 | message UpdateTokenResponse { 32 | uint32 version = 1; 33 | bytes update_token = 2; 34 | } 35 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | os: linux 2 | 3 | language: go 4 | 5 | go: 6 | - 1.15.x 7 | 8 | env: 9 | 10 | install: 11 | - if [ "$TRAVIS_OS_NAME" = "windows" ]; then choco install make zip; fi 12 | - go get ./... 13 | 14 | script: 15 | - echo ">>> Run build" 16 | - go env GOOS 17 | - go env GOARCH 18 | - make build 19 | - make pack_artifacts 20 | 21 | builds: &builds 22 | stage: build 23 | 24 | deploys: &deploys 25 | deploy: 26 | - provider: releases 27 | skip_cleanup: true 28 | api_key: $GITHUB_TOKEN 29 | file_glob: true 30 | file: artifacts/** 31 | on: 32 | tags: true 33 | 34 | jobs: 35 | include: 36 | - <<: *builds 37 | os: osx 38 | osx_image: xcode11.3 39 | <<: *deploys 40 | - <<: *builds 41 | os: windows 42 | <<: *deploys 43 | - <<: *builds 44 | os: linux 45 | <<: *deploys -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | BSD 3-Clause License 2 | 3 | Copyright (c) 2020, Virgil Security, Inc. 4 | All rights reserved. 5 | 6 | Redistribution and use in source and binary forms, with or without 7 | modification, are permitted provided that the following conditions are met: 8 | 9 | * Redistributions of source code must retain the above copyright notice, this 10 | list of conditions and the following disclaimer. 11 | 12 | * Redistributions in binary form must reproduce the above copyright notice, 13 | this list of conditions and the following disclaimer in the documentation 14 | and/or other materials provided with the distribution. 15 | 16 | * Neither the name of the copyright holder nor the names of its 17 | contributors may be used to endorse or promote products derived from 18 | this software without specific prior written permission. 19 | 20 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 21 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 23 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 24 | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 26 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 27 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 28 | OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | .DEFAULT_GOAL := build 2 | 3 | .PHONY: build go_build go_test_unit 4 | 5 | # Project-specific variables 6 | # 7 | # Binary name 8 | BINARY:=virgil 9 | 10 | # 11 | # General variables 12 | # 13 | # Packages covered with unit tests. 14 | GO_UNIT_TESTED_PACKAGES=$(shell go list ./...) 15 | 16 | 17 | 18 | ifneq ($(shell go env GOOS),darwin) 19 | GO_BUILD_LDFLAGS+= -linkmode external -extldflags '-static' 20 | endif 21 | 22 | ifeq ($(shell go env GOOS),windows) 23 | BINARY:=$(BINARY).exe 24 | endif 25 | 26 | ifeq ($(shell go env GOARCH), 386) 27 | VIRGIL_PACKAGE_ARCH=i386 28 | endif 29 | 30 | ifeq ($(shell go env GOARCH), amd64) 31 | VIRGIL_PACKAGE_ARCH=x86_64 32 | endif 33 | 34 | VERSION=$(shell cat VERSION) 35 | OS=$(shell go env GOOS) 36 | 37 | 38 | 39 | # Go build flags. 40 | GO_BUILD_FLAGS=-v --ldflags "$(GO_BUILD_LDFLAGS)" -a -installsuffix cgo 41 | 42 | # 43 | # Build targets 44 | # 45 | 46 | build: go_get go_build 47 | tests: go_test_functional 48 | 49 | go_build: 50 | @echo ">>> Building go binary." 51 | go build $(GO_BUILD_FLAGS) -o $(BINARY) 52 | 53 | go_get: 54 | @echo ">>> Getting dependencies." 55 | go get ./... 56 | 57 | go_test_unit: 58 | @echo ">>> Running unit tests." 59 | @go test -cover $(GO_UNIT_TESTED_PACKAGES) 60 | 61 | go_test_functional: 62 | @echo ">>> Running functional tests." 63 | @go test -v -tags="functional" ./test/functional 64 | 65 | pack_artifacts: 66 | @echo ">>> Archiving artifact" 67 | mkdir -p artifacts 68 | if [ "$(OS)" = "windows" ]; then \ 69 | zip artifacts/Virgil_$(VERSION)_$(OS)_$(VIRGIL_PACKAGE_ARCH).zip $(BINARY); \ 70 | else \ 71 | tar cvzf artifacts/Virgil_$(VERSION)_$(OS)_$(VIRGIL_PACKAGE_ARCH).tar.gz $(BINARY); \ 72 | fi 73 | -------------------------------------------------------------------------------- /cmd/cards.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015-2020 Virgil Security Inc. 3 | * 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions are 8 | * met: 9 | * 10 | * (1) Redistributions of source code must retain the above copyright 11 | * notice, this list of conditions and the following disclaimer. 12 | * 13 | * (2) Redistributions in binary form must reproduce the above copyright 14 | * notice, this list of conditions and the following disclaimer in 15 | * the documentation and/or other materials provided with the 16 | * distribution. 17 | * 18 | * (3) Neither the name of the copyright holder nor the names of its 19 | * contributors may be used to endorse or promote products derived from 20 | * this software without specific prior written permission. 21 | * 22 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ''AS IS'' AND ANY EXPRESS OR 23 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 24 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 25 | * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, 26 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 27 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 28 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 30 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 31 | * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 32 | * POSSIBILITY OF SUCH DAMAGE. 33 | * 34 | * Lead Maintainer: Virgil Security Inc. 35 | */ 36 | package cmd 37 | 38 | import ( 39 | "github.com/urfave/cli/v2" 40 | 41 | "github.com/VirgilSecurity/virgil-cli/cmd/cards" 42 | ) 43 | 44 | func Cards() *cli.Command { 45 | return &cli.Command{ 46 | Name: "cards", 47 | Usage: "Manage your cards", 48 | Subcommands: []*cli.Command{ 49 | cards.Search(), 50 | cards.Revoke(), 51 | }, 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /test/functional/init.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015-2020 Virgil Security Inc. 3 | * 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions are 8 | * met: 9 | * 10 | * (1) Redistributions of source code must retain the above copyright 11 | * notice, this list of conditions and the following disclaimer. 12 | * 13 | * (2) Redistributions in binary form must reproduce the above copyright 14 | * notice, this list of conditions and the following disclaimer in 15 | * the documentation and/or other materials provided with the 16 | * distribution. 17 | * 18 | * (3) Neither the name of the copyright holder nor the names of its 19 | * contributors may be used to endorse or promote products derived from 20 | * this software without specific prior written permission. 21 | * 22 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ''AS IS'' AND ANY EXPRESS OR 23 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 24 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 25 | * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, 26 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 27 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 28 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 30 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 31 | * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 32 | * POSSIBILITY OF SUCH DAMAGE. 33 | * 34 | * Lead Maintainer: Virgil Security Inc. 35 | */ 36 | package functional 37 | 38 | import ( 39 | "github.com/VirgilSecurity/virgil-cli/test/helpers" 40 | ) 41 | 42 | var ( 43 | UserEmail string 44 | UserPassword string 45 | ) 46 | 47 | func init() { 48 | UserEmail = helpers.GenerateEmail() 49 | UserPassword = helpers.GeneratePassowrd() 50 | 51 | helpers.RegisterUser(UserEmail, UserPassword) 52 | } 53 | -------------------------------------------------------------------------------- /cmd/purekit.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015-2019 Virgil Security Inc. 3 | * 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions are 8 | * met: 9 | * 10 | * (1) Redistributions of source code must retain the above copyright 11 | * notice, this list of conditions and the following disclaimer. 12 | * 13 | * (2) Redistributions in binary form must reproduce the above copyright 14 | * notice, this list of conditions and the following disclaimer in 15 | * the documentation and/or other materials provided with the 16 | * distribution. 17 | * 18 | * (3) Neither the name of the copyright holder nor the names of its 19 | * contributors may be used to endorse or promote products derived from 20 | * this software without specific prior written permission. 21 | * 22 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ''AS IS'' AND ANY EXPRESS OR 23 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 24 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 25 | * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, 26 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 27 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 28 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 30 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 31 | * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 32 | * POSSIBILITY OF SUCH DAMAGE. 33 | * 34 | * Lead Maintainer: Virgil Security Inc. 35 | */ 36 | 37 | package cmd 38 | 39 | import ( 40 | "github.com/urfave/cli/v2" 41 | 42 | "github.com/VirgilSecurity/virgil-cli/cmd/pure" 43 | ) 44 | 45 | //PureKit manages virgil PHE service implementation 46 | func PureKit() *cli.Command { 47 | return &cli.Command{ 48 | Name: "purekit", 49 | Usage: "Manage your PureKit application keys", 50 | Subcommands: []*cli.Command{ 51 | pure.Keygen(), 52 | pure.UpdateKeys(), 53 | }, 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /cmd/scms/device.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015-2020 Virgil Security Inc. 3 | * 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions are 8 | * met: 9 | * 10 | * (1) Redistributions of source code must retain the above copyright 11 | * notice, this list of conditions and the following disclaimer. 12 | * 13 | * (2) Redistributions in binary form must reproduce the above copyright 14 | * notice, this list of conditions and the following disclaimer in 15 | * the documentation and/or other materials provided with the 16 | * distribution. 17 | * 18 | * (3) Neither the name of the copyright holder nor the names of its 19 | * contributors may be used to endorse or promote products derived from 20 | * this software without specific prior written permission. 21 | * 22 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ''AS IS'' AND ANY EXPRESS OR 23 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 24 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 25 | * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, 26 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 27 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 28 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 30 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 31 | * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 32 | * POSSIBILITY OF SUCH DAMAGE. 33 | * 34 | * Lead Maintainer: Virgil Security Inc. 35 | */ 36 | package scms 37 | 38 | import ( 39 | "github.com/urfave/cli/v2" 40 | 41 | "github.com/VirgilSecurity/virgil-cli/client" 42 | "github.com/VirgilSecurity/virgil-cli/cmd/scms/device" 43 | ) 44 | 45 | func Device(client *client.VirgilHTTPClient) *cli.Command { 46 | return &cli.Command{ 47 | Name: "devices", 48 | Usage: "Manage your scms devices", 49 | Subcommands: []*cli.Command{ 50 | device.List(client), 51 | }, 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /cmd/scms/dcm.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015-2020 Virgil Security Inc. 3 | * 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions are 8 | * met: 9 | * 10 | * (1) Redistributions of source code must retain the above copyright 11 | * notice, this list of conditions and the following disclaimer. 12 | * 13 | * (2) Redistributions in binary form must reproduce the above copyright 14 | * notice, this list of conditions and the following disclaimer in 15 | * the documentation and/or other materials provided with the 16 | * distribution. 17 | * 18 | * (3) Neither the name of the copyright holder nor the names of its 19 | * contributors may be used to endorse or promote products derived from 20 | * this software without specific prior written permission. 21 | * 22 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ''AS IS'' AND ANY EXPRESS OR 23 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 24 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 25 | * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, 26 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 27 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 28 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 30 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 31 | * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 32 | * POSSIBILITY OF SUCH DAMAGE. 33 | * 34 | * Lead Maintainer: Virgil Security Inc. 35 | */ 36 | package scms 37 | 38 | import ( 39 | "github.com/urfave/cli/v2" 40 | 41 | "github.com/VirgilSecurity/virgil-cli/client" 42 | "github.com/VirgilSecurity/virgil-cli/cmd/scms/dcm" 43 | ) 44 | 45 | func Dcm(client *client.VirgilHTTPClient) *cli.Command { 46 | return &cli.Command{ 47 | Name: "dcm", 48 | Usage: "Manage your dcm certificates", 49 | Subcommands: []*cli.Command{ 50 | dcm.List(client), 51 | dcm.Create(client), 52 | }, 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /cmd/scms.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015-2019 Virgil Security Inc. 3 | * 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions are 8 | * met: 9 | * 10 | * (1) Redistributions of source code must retain the above copyright 11 | * notice, this list of conditions and the following disclaimer. 12 | * 13 | * (2) Redistributions in binary form must reproduce the above copyright 14 | * notice, this list of conditions and the following disclaimer in 15 | * the documentation and/or other materials provided with the 16 | * distribution. 17 | * 18 | * (3) Neither the name of the copyright holder nor the names of its 19 | * contributors may be used to endorse or promote products derived from 20 | * this software without specific prior written permission. 21 | * 22 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ''AS IS'' AND ANY EXPRESS OR 23 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 24 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 25 | * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, 26 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 27 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 28 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 30 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 31 | * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 32 | * POSSIBILITY OF SUCH DAMAGE. 33 | * 34 | * Lead Maintainer: Virgil Security Inc. 35 | */ 36 | 37 | package cmd 38 | 39 | import ( 40 | "github.com/urfave/cli/v2" 41 | 42 | "github.com/VirgilSecurity/virgil-cli/client" 43 | "github.com/VirgilSecurity/virgil-cli/cmd/scms" 44 | ) 45 | 46 | func Wave(client *client.VirgilHTTPClient) *cli.Command { 47 | return &cli.Command{ 48 | Name: "scms", 49 | Usage: "Manage your scms application", 50 | Subcommands: []*cli.Command{ 51 | scms.Init(client), 52 | scms.Device(client), 53 | scms.Dcm(client), 54 | }, 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /cmd/app/token.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015-2019 Virgil Security Inc. 3 | * 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions are 8 | * met: 9 | * 10 | * (1) Redistributions of source code must retain the above copyright 11 | * notice, this list of conditions and the following disclaimer. 12 | * 13 | * (2) Redistributions in binary form must reproduce the above copyright 14 | * notice, this list of conditions and the following disclaimer in 15 | * the documentation and/or other materials provided with the 16 | * distribution. 17 | * 18 | * (3) Neither the name of the copyright holder nor the names of its 19 | * contributors may be used to endorse or promote products derived from 20 | * this software without specific prior written permission. 21 | * 22 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ''AS IS'' AND ANY EXPRESS OR 23 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 24 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 25 | * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, 26 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 27 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 28 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 30 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 31 | * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 32 | * POSSIBILITY OF SUCH DAMAGE. 33 | * 34 | * Lead Maintainer: Virgil Security Inc. 35 | */ 36 | 37 | package app 38 | 39 | import ( 40 | "github.com/urfave/cli/v2" 41 | 42 | "github.com/VirgilSecurity/virgil-cli/client" 43 | "github.com/VirgilSecurity/virgil-cli/cmd/app/token" 44 | ) 45 | 46 | func Token(client *client.VirgilHTTPClient) *cli.Command { 47 | return &cli.Command{ 48 | Name: "token", 49 | Usage: "Manage your application tokens", 50 | Subcommands: []*cli.Command{ 51 | token.Create(client), 52 | token.List(client), 53 | token.Delete(client), 54 | }, 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /models/key.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015-2019 Virgil Security Inc. 3 | * 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions are 8 | * met: 9 | * 10 | * (1) Redistributions of source code must retain the above copyright 11 | * notice, this list of conditions and the following disclaimer. 12 | * 13 | * (2) Redistributions in binary form must reproduce the above copyright 14 | * notice, this list of conditions and the following disclaimer in 15 | * the documentation and/or other materials provided with the 16 | * distribution. 17 | * 18 | * (3) Neither the name of the copyright holder nor the names of its 19 | * contributors may be used to endorse or promote products derived from 20 | * this software without specific prior written permission. 21 | * 22 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ''AS IS'' AND ANY EXPRESS OR 23 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 24 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 25 | * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, 26 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 27 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 28 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 30 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 31 | * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 32 | * POSSIBILITY OF SUCH DAMAGE. 33 | * 34 | * Lead Maintainer: Virgil Security Inc. 35 | */ 36 | 37 | package models 38 | 39 | import "time" 40 | 41 | type CreateAccessKeyRequest struct { 42 | Name string `json:"name"` 43 | PublicKey []byte `json:"public_key"` 44 | Signature []byte `json:"signature"` 45 | } 46 | 47 | type AccessKey struct { 48 | ID string `json:"id"` 49 | PublicKey []byte `json:"public_key"` 50 | Name string `json:"name"` 51 | CreatedAt time.Time `json:"created_at"` 52 | } 53 | 54 | type UpdateAccessKeyRequest struct { 55 | Name string `json:"name"` 56 | } 57 | -------------------------------------------------------------------------------- /cmd/app/key.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015-2019 Virgil Security Inc. 3 | * 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions are 8 | * met: 9 | * 10 | * (1) Redistributions of source code must retain the above copyright 11 | * notice, this list of conditions and the following disclaimer. 12 | * 13 | * (2) Redistributions in binary form must reproduce the above copyright 14 | * notice, this list of conditions and the following disclaimer in 15 | * the documentation and/or other materials provided with the 16 | * distribution. 17 | * 18 | * (3) Neither the name of the copyright holder nor the names of its 19 | * contributors may be used to endorse or promote products derived from 20 | * this software without specific prior written permission. 21 | * 22 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ''AS IS'' AND ANY EXPRESS OR 23 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 24 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 25 | * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, 26 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 27 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 28 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 30 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 31 | * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 32 | * POSSIBILITY OF SUCH DAMAGE. 33 | * 34 | * Lead Maintainer: Virgil Security Inc. 35 | */ 36 | 37 | package app 38 | 39 | import ( 40 | "github.com/urfave/cli/v2" 41 | 42 | "github.com/VirgilSecurity/virgil-cli/client" 43 | "github.com/VirgilSecurity/virgil-cli/cmd/app/key" 44 | ) 45 | 46 | func Key(client *client.VirgilHTTPClient) *cli.Command { 47 | return &cli.Command{ 48 | Name: "key", 49 | Aliases: []string{"key"}, 50 | Usage: "Manage your app keys", 51 | Subcommands: []*cli.Command{ 52 | key.Create(client), 53 | key.Delete(client), 54 | key.List(client), 55 | key.Update(client), 56 | }, 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /cmd/app.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015-2019 Virgil Security Inc. 3 | * 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions are 8 | * met: 9 | * 10 | * (1) Redistributions of source code must retain the above copyright 11 | * notice, this list of conditions and the following disclaimer. 12 | * 13 | * (2) Redistributions in binary form must reproduce the above copyright 14 | * notice, this list of conditions and the following disclaimer in 15 | * the documentation and/or other materials provided with the 16 | * distribution. 17 | * 18 | * (3) Neither the name of the copyright holder nor the names of its 19 | * contributors may be used to endorse or promote products derived from 20 | * this software without specific prior written permission. 21 | * 22 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ''AS IS'' AND ANY EXPRESS OR 23 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 24 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 25 | * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, 26 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 27 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 28 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 30 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 31 | * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 32 | * POSSIBILITY OF SUCH DAMAGE. 33 | * 34 | * Lead Maintainer: Virgil Security Inc. 35 | */ 36 | 37 | package cmd 38 | 39 | import ( 40 | "github.com/urfave/cli/v2" 41 | 42 | "github.com/VirgilSecurity/virgil-cli/client" 43 | "github.com/VirgilSecurity/virgil-cli/cmd/app" 44 | ) 45 | 46 | func Application(client *client.VirgilHTTPClient) *cli.Command { 47 | return &cli.Command{ 48 | Name: "app", 49 | Usage: "Manage your applications", 50 | Subcommands: []*cli.Command{ 51 | app.Create(client), 52 | app.List(client), 53 | app.Delete(client), 54 | app.Update(client), 55 | app.Token(client), 56 | app.Key(client), 57 | }, 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /cmd/pure/keygen/util.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015-2020 Virgil Security Inc. 3 | * 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions are 8 | * met: 9 | * 10 | * (1) Redistributions of source code must retain the above copyright 11 | * notice, this list of conditions and the following disclaimer. 12 | * 13 | * (2) Redistributions in binary form must reproduce the above copyright 14 | * notice, this list of conditions and the following disclaimer in 15 | * the documentation and/or other materials provided with the 16 | * distribution. 17 | * 18 | * (3) Neither the name of the copyright holder nor the names of its 19 | * contributors may be used to endorse or promote products derived from 20 | * this software without specific prior written permission. 21 | * 22 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ''AS IS'' AND ANY EXPRESS OR 23 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 24 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 25 | * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, 26 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 27 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 28 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 30 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 31 | * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 32 | * POSSIBILITY OF SUCH DAMAGE. 33 | * 34 | * Lead Maintainer: Virgil Security Inc. 35 | */ 36 | package keygen 37 | 38 | import ( 39 | "github.com/VirgilSecurity/virgil-sdk-go/v6/crypto" 40 | ) 41 | 42 | var crypt = &crypto.Crypto{} 43 | 44 | func generateKeypairEncoded() (sk []byte, pk []byte, err error) { 45 | keyPair, err := crypt.GenerateKeypair() 46 | if err != nil { 47 | return nil, nil, err 48 | } 49 | 50 | if sk, err = crypt.ExportPrivateKey(keyPair); err != nil { 51 | return nil, nil, err 52 | } 53 | 54 | if pk, err = crypt.ExportPublicKey(keyPair.PublicKey()); err != nil { 55 | return nil, nil, err 56 | } 57 | return sk, pk, nil 58 | } 59 | -------------------------------------------------------------------------------- /cmd/kms.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015-2019 Virgil Security Inc. 3 | * 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions are 8 | * met: 9 | * 10 | * (1) Redistributions of source code must retain the above copyright 11 | * notice, this list of conditions and the following disclaimer. 12 | * 13 | * (2) Redistributions in binary form must reproduce the above copyright 14 | * notice, this list of conditions and the following disclaimer in 15 | * the documentation and/or other materials provided with the 16 | * distribution. 17 | * 18 | * (3) Neither the name of the copyright holder nor the names of its 19 | * contributors may be used to endorse or promote products derived from 20 | * this software without specific prior written permission. 21 | * 22 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ''AS IS'' AND ANY EXPRESS OR 23 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 24 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 25 | * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, 26 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 27 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 28 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 30 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 31 | * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 32 | * POSSIBILITY OF SUCH DAMAGE. 33 | * 34 | * Lead Maintainer: Virgil Security Inc. 35 | */ 36 | 37 | package cmd 38 | 39 | import ( 40 | "github.com/urfave/cli/v2" 41 | 42 | "github.com/VirgilSecurity/virgil-cli/client" 43 | "github.com/VirgilSecurity/virgil-cli/cmd/kms" 44 | ) 45 | 46 | func KMS(client *client.VirgilHTTPClient) *cli.Command { 47 | return &cli.Command{ 48 | Name: "kms", 49 | Usage: "Manage your Key Management System", 50 | Subcommands: []*cli.Command{ 51 | kms.Create(client), 52 | kms.KMSPrivateKey(), 53 | kms.List(client), 54 | kms.GetUpdateToken(client), 55 | kms.RotateKeys(), 56 | kms.DeleteUpdateToken(client), 57 | }, 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /cmd/logout.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015-2019 Virgil Security Inc. 3 | * 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions are 8 | * met: 9 | * 10 | * (1) Redistributions of source code must retain the above copyright 11 | * notice, this list of conditions and the following disclaimer. 12 | * 13 | * (2) Redistributions in binary form must reproduce the above copyright 14 | * notice, this list of conditions and the following disclaimer in 15 | * the documentation and/or other materials provided with the 16 | * distribution. 17 | * 18 | * (3) Neither the name of the copyright holder nor the names of its 19 | * contributors may be used to endorse or promote products derived from 20 | * this software without specific prior written permission. 21 | * 22 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ''AS IS'' AND ANY EXPRESS OR 23 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 24 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 25 | * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, 26 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 27 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 28 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 30 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 31 | * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 32 | * POSSIBILITY OF SUCH DAMAGE. 33 | * 34 | * Lead Maintainer: Virgil Security Inc. 35 | */ 36 | 37 | package cmd 38 | 39 | import ( 40 | "fmt" 41 | 42 | "github.com/urfave/cli/v2" 43 | 44 | "github.com/VirgilSecurity/virgil-cli/utils" 45 | ) 46 | 47 | func Logout() *cli.Command { 48 | return &cli.Command{ 49 | Name: "logout", 50 | Usage: "Close user session", 51 | Action: func(context *cli.Context) error { 52 | _, err := utils.LoadAccessToken() 53 | if err != nil { 54 | fmt.Println(utils.LogoutNotNeeded) 55 | return nil 56 | } 57 | _ = utils.DeleteAppFile() 58 | _ = utils.DeleteAccessToken() 59 | fmt.Println(utils.LogoutSuccess) 60 | return nil 61 | }, 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /test/fixtures/fixtures.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015-2020 Virgil Security Inc. 3 | * 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions are 8 | * met: 9 | * 10 | * (1) Redistributions of source code must retain the above copyright 11 | * notice, this list of conditions and the following disclaimer. 12 | * 13 | * (2) Redistributions in binary form must reproduce the above copyright 14 | * notice, this list of conditions and the following disclaimer in 15 | * the documentation and/or other materials provided with the 16 | * distribution. 17 | * 18 | * (3) Neither the name of the copyright holder nor the names of its 19 | * contributors may be used to endorse or promote products derived from 20 | * this software without specific prior written permission. 21 | * 22 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ''AS IS'' AND ANY EXPRESS OR 23 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 24 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 25 | * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, 26 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 27 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 28 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 30 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 31 | * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 32 | * POSSIBILITY OF SUCH DAMAGE. 33 | * 34 | * Lead Maintainer: Virgil Security Inc. 35 | */ 36 | package fixtures 37 | 38 | const ( 39 | // Test Configs 40 | BinaryName = "virgil" 41 | 42 | // Cli prompts 43 | VersionPattern = "CLI version " 44 | 45 | KMSKeyInfoPatternShort = "KMS Key alias: " 46 | KMSKeyInfoPattern = "KMS Key alias: %s version: 1 public key: " // place holder contains upper case key alias 47 | KMSRotateServerPublicKey = "New server public key:" 48 | KMSRotateClientPrivateKey = "New client private key:" 49 | ) 50 | 51 | var ( 52 | AppListHeaders = []string{"Application name", "APP_ID", "created_at"} 53 | AppTokenListHeaders = []string{"Name", "Created On"} 54 | KMSKeysListHeaders = []string{"Keypair alias", "Keypair version", "Public Key"} 55 | ) 56 | -------------------------------------------------------------------------------- /models/account.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015-2019 Virgil Security Inc. 3 | * 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions are 8 | * met: 9 | * 10 | * (1) Redistributions of source code must retain the above copyright 11 | * notice, this list of conditions and the following disclaimer. 12 | * 13 | * (2) Redistributions in binary form must reproduce the above copyright 14 | * notice, this list of conditions and the following disclaimer in 15 | * the documentation and/or other materials provided with the 16 | * distribution. 17 | * 18 | * (3) Neither the name of the copyright holder nor the names of its 19 | * contributors may be used to endorse or promote products derived from 20 | * this software without specific prior written permission. 21 | * 22 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ''AS IS'' AND ANY EXPRESS OR 23 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 24 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 25 | * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, 26 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 27 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 28 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 30 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 31 | * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 32 | * POSSIBILITY OF SUCH DAMAGE. 33 | * 34 | * Lead Maintainer: Virgil Security Inc. 35 | */ 36 | 37 | package models 38 | 39 | type CreateAccountRequest struct { 40 | Name string `json:"name"` 41 | Password string `json:"password"` 42 | Email string `json:"email"` 43 | } 44 | 45 | type LoginRequest struct { 46 | Email string `json:"email"` 47 | Password string `json:"password"` 48 | Verification *Verification `json:"verification"` 49 | } 50 | 51 | type Verification struct { 52 | MFACode string `json:"mfa_code"` 53 | } 54 | 55 | type SessionToken struct { 56 | Token string `json:"session_token"` 57 | } 58 | 59 | type ManagementTokenResponse struct { 60 | Token string `json:"management_token"` 61 | } 62 | 63 | type ManagementTokenRequest struct { 64 | Name string `json:"token_name"` 65 | } 66 | -------------------------------------------------------------------------------- /test/helpers/cmd.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015-2020 Virgil Security Inc. 3 | * 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions are 8 | * met: 9 | * 10 | * (1) Redistributions of source code must retain the above copyright 11 | * notice, this list of conditions and the following disclaimer. 12 | * 13 | * (2) Redistributions in binary form must reproduce the above copyright 14 | * notice, this list of conditions and the following disclaimer in 15 | * the documentation and/or other materials provided with the 16 | * distribution. 17 | * 18 | * (3) Neither the name of the copyright holder nor the names of its 19 | * contributors may be used to endorse or promote products derived from 20 | * this software without specific prior written permission. 21 | * 22 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ''AS IS'' AND ANY EXPRESS OR 23 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 24 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 25 | * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, 26 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 27 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 28 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 30 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 31 | * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 32 | * POSSIBILITY OF SUCH DAMAGE. 33 | * 34 | * Lead Maintainer: Virgil Security Inc. 35 | */ 36 | package helpers 37 | 38 | import ( 39 | "bufio" 40 | "fmt" 41 | "os" 42 | "os/exec" 43 | "path" 44 | "strings" 45 | 46 | "github.com/VirgilSecurity/virgil-cli/test/fixtures" 47 | ) 48 | 49 | func PrepareCmd(args ...string) *exec.Cmd { 50 | projectRoot, _ := os.Getwd() 51 | projectRoot = strings.Replace(projectRoot, "/test/functional", "", 1) 52 | return exec.Command(path.Join(projectRoot, fixtures.BinaryName), args...) 53 | } 54 | 55 | func CmdKiller(cmd *exec.Cmd, scannerErr *bufio.Scanner) bool { 56 | var cmdErrored bool 57 | for scannerErr.Scan() { 58 | cmdErrored = true 59 | fmt.Printf("CmdErr: %s\n", scannerErr.Text()) 60 | } 61 | if cmdErrored { 62 | _ = cmd.Process.Kill() 63 | } 64 | return cmdErrored 65 | } 66 | -------------------------------------------------------------------------------- /cmd/pure/keygen/hb.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015-2020 Virgil Security Inc. 3 | * 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions are 8 | * met: 9 | * 10 | * (1) Redistributions of source code must retain the above copyright 11 | * notice, this list of conditions and the following disclaimer. 12 | * 13 | * (2) Redistributions in binary form must reproduce the above copyright 14 | * notice, this list of conditions and the following disclaimer in 15 | * the documentation and/or other materials provided with the 16 | * distribution. 17 | * 18 | * (3) Neither the name of the copyright holder nor the names of its 19 | * contributors may be used to endorse or promote products derived from 20 | * this software without specific prior written permission. 21 | * 22 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ''AS IS'' AND ANY EXPRESS OR 23 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 24 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 25 | * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, 26 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 27 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 28 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 30 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 31 | * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 32 | * POSSIBILITY OF SUCH DAMAGE. 33 | * 34 | * Lead Maintainer: Virgil Security Inc. 35 | */ 36 | package keygen 37 | 38 | import ( 39 | "encoding/base64" 40 | "fmt" 41 | 42 | "github.com/urfave/cli/v2" 43 | 44 | "github.com/VirgilSecurity/virgil-cli/utils" 45 | ) 46 | 47 | // HashesKey generates a new Hashes key pair 48 | func HashesKey() *cli.Command { 49 | return &cli.Command{ 50 | Name: "hashes", 51 | Aliases: []string{"hb"}, 52 | Usage: "Generate a new Hashes key pair", 53 | Action: func(context *cli.Context) error { 54 | err := printHBKey() 55 | if err != nil { 56 | return utils.CliExit(err) 57 | } 58 | return err 59 | }, 60 | } 61 | } 62 | 63 | func printHBKey() error { 64 | sk, pk, err := generateKeypairEncoded() 65 | if err != nil { 66 | return err 67 | } 68 | 69 | fmt.Println("HB." + base64.StdEncoding.EncodeToString(pk)) 70 | fmt.Println("private key: " + base64.StdEncoding.EncodeToString(sk)) 71 | 72 | return nil 73 | } 74 | -------------------------------------------------------------------------------- /models/scms.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015-2020 Virgil Security Inc. 3 | * 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions are 8 | * met: 9 | * 10 | * (1) Redistributions of source code must retain the above copyright 11 | * notice, this list of conditions and the following disclaimer. 12 | * 13 | * (2) Redistributions in binary form must reproduce the above copyright 14 | * notice, this list of conditions and the following disclaimer in 15 | * the documentation and/or other materials provided with the 16 | * distribution. 17 | * 18 | * (3) Neither the name of the copyright holder nor the names of its 19 | * contributors may be used to endorse or promote products derived from 20 | * this software without specific prior written permission. 21 | * 22 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ''AS IS'' AND ANY EXPRESS OR 23 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 24 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 25 | * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, 26 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 27 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 28 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 30 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 31 | * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 32 | * POSSIBILITY OF SUCH DAMAGE. 33 | * 34 | * Lead Maintainer: Virgil Security Inc. 35 | */ 36 | package models 37 | 38 | import "time" 39 | 40 | type DcmCertificateCreateRequest struct { 41 | Name string `json:"name"` 42 | EncryptPublicKey string `json:"encrypt_public_key"` 43 | VerifyPublicKey string `json:"verify_public_key"` 44 | } 45 | 46 | type DcmCertificateCreateResponse struct { 47 | Name string `json:"name"` 48 | Certificate string `json:"certificate"` 49 | EcaAddress string `json:"eca_address"` 50 | EcaCertificate string `json:"eca_certificate"` 51 | RaAddress string `json:"ra_address"` 52 | Lccf string `json:"lccf"` 53 | } 54 | 55 | type DcmCertificateListItem struct { 56 | Name string `json:"name"` 57 | CreatedAt time.Time `json:"created_at"` 58 | } 59 | 60 | type Device struct { 61 | ID string `json:"id"` 62 | DcmID string `json:"dcm_id"` 63 | ValidFrom time.Time `json:"valid_from"` 64 | ValidTo time.Time `json:"valid_to"` 65 | } 66 | -------------------------------------------------------------------------------- /cmd/pure/keygen/vs.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015-2020 Virgil Security Inc. 3 | * 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions are 8 | * met: 9 | * 10 | * (1) Redistributions of source code must retain the above copyright 11 | * notice, this list of conditions and the following disclaimer. 12 | * 13 | * (2) Redistributions in binary form must reproduce the above copyright 14 | * notice, this list of conditions and the following disclaimer in 15 | * the documentation and/or other materials provided with the 16 | * distribution. 17 | * 18 | * (3) Neither the name of the copyright holder nor the names of its 19 | * contributors may be used to endorse or promote products derived from 20 | * this software without specific prior written permission. 21 | * 22 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ''AS IS'' AND ANY EXPRESS OR 23 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 24 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 25 | * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, 26 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 27 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 28 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 30 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 31 | * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 32 | * POSSIBILITY OF SUCH DAMAGE. 33 | * 34 | * Lead Maintainer: Virgil Security Inc. 35 | */ 36 | package keygen 37 | 38 | import ( 39 | "encoding/base64" 40 | "fmt" 41 | 42 | "github.com/urfave/cli/v2" 43 | 44 | "github.com/VirgilSecurity/virgil-cli/utils" 45 | ) 46 | 47 | // VirgilStorage generates a new Virgil Storage key pair 48 | func VirgilStorage() *cli.Command { 49 | return &cli.Command{ 50 | Name: "signing", 51 | Aliases: []string{"vs"}, 52 | Usage: "Generate a new Virgil Storage key pair", 53 | Action: func(context *cli.Context) error { 54 | err := printSigningKey() 55 | if err != nil { 56 | return utils.CliExit(err) 57 | } 58 | return err 59 | }, 60 | } 61 | } 62 | 63 | func printSigningKey() error { 64 | sk, pk, err := generateKeypairEncoded() 65 | if err != nil { 66 | return err 67 | } 68 | fmt.Println(utils.PureStorageKeyPairCreateSuccessTemplate) 69 | fmt.Println("VSSK." + base64.StdEncoding.EncodeToString(sk)) 70 | fmt.Println("VSPK." + base64.StdEncoding.EncodeToString(pk)) 71 | 72 | return nil 73 | } 74 | -------------------------------------------------------------------------------- /cmd/pure/keygen/backup.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015-2020 Virgil Security Inc. 3 | * 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions are 8 | * met: 9 | * 10 | * (1) Redistributions of source code must retain the above copyright 11 | * notice, this list of conditions and the following disclaimer. 12 | * 13 | * (2) Redistributions in binary form must reproduce the above copyright 14 | * notice, this list of conditions and the following disclaimer in 15 | * the documentation and/or other materials provided with the 16 | * distribution. 17 | * 18 | * (3) Neither the name of the copyright holder nor the names of its 19 | * contributors may be used to endorse or promote products derived from 20 | * this software without specific prior written permission. 21 | * 22 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ''AS IS'' AND ANY EXPRESS OR 23 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 24 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 25 | * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, 26 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 27 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 28 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 30 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 31 | * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 32 | * POSSIBILITY OF SUCH DAMAGE. 33 | * 34 | * Lead Maintainer: Virgil Security Inc. 35 | */ 36 | package keygen 37 | 38 | import ( 39 | "encoding/base64" 40 | "fmt" 41 | 42 | "github.com/urfave/cli/v2" 43 | 44 | "github.com/VirgilSecurity/virgil-cli/utils" 45 | ) 46 | 47 | // Backup generates a new Backup keypair 48 | func Backup() *cli.Command { 49 | return &cli.Command{ 50 | Name: "backup", 51 | Aliases: []string{"bu"}, 52 | Usage: "Generate a new Backup keypair ", 53 | Action: func(context *cli.Context) error { 54 | err := printBackupKeys() 55 | if err != nil { 56 | return utils.CliExit(err) 57 | } 58 | return err 59 | }, 60 | } 61 | } 62 | 63 | func printBackupKeys() error { 64 | sk, pk, err := generateKeypairEncoded() 65 | if err != nil { 66 | return err 67 | } 68 | fmt.Println(utils.PureBackupKeyCreateWarning) 69 | fmt.Println(utils.PureBackupKeyPublicCreateSuccessTemplate + " BU." + base64.StdEncoding.EncodeToString(pk)) 70 | fmt.Println(utils.PureBackupKeyPrivateCreateSuccessTemplate + " " + base64.StdEncoding.EncodeToString(sk)) 71 | 72 | return nil 73 | } 74 | -------------------------------------------------------------------------------- /cmd/pure/keygen/auth.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015-2020 Virgil Security Inc. 3 | * 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions are 8 | * met: 9 | * 10 | * (1) Redistributions of source code must retain the above copyright 11 | * notice, this list of conditions and the following disclaimer. 12 | * 13 | * (2) Redistributions in binary form must reproduce the above copyright 14 | * notice, this list of conditions and the following disclaimer in 15 | * the documentation and/or other materials provided with the 16 | * distribution. 17 | * 18 | * (3) Neither the name of the copyright holder nor the names of its 19 | * contributors may be used to endorse or promote products derived from 20 | * this software without specific prior written permission. 21 | * 22 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ''AS IS'' AND ANY EXPRESS OR 23 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 24 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 25 | * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, 26 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 27 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 28 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 30 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 31 | * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 32 | * POSSIBILITY OF SUCH DAMAGE. 33 | * 34 | * Lead Maintainer: Virgil Security Inc. 35 | */ 36 | package keygen 37 | 38 | import ( 39 | "encoding/base64" 40 | "fmt" 41 | 42 | "github.com/VirgilSecurity/virgil-sdk-go/v6/crypto/wrapper/phe" 43 | "github.com/urfave/cli/v2" 44 | 45 | "github.com/VirgilSecurity/virgil-cli/utils" 46 | ) 47 | 48 | // Auth generates Auth key 49 | func Auth() *cli.Command { 50 | return &cli.Command{ 51 | Name: "auth", 52 | Aliases: []string{"ak"}, 53 | Usage: "Generate a new Auth key", 54 | Action: func(context *cli.Context) error { 55 | err := printAuthKey() 56 | if err != nil { 57 | return utils.CliExit(err) 58 | } 59 | return err 60 | }, 61 | } 62 | } 63 | 64 | func GenerateAuthKey() (key []byte, err error) { 65 | uokmsClient := phe.NewUokmsClient() 66 | err = uokmsClient.SetupDefaults() 67 | if err != nil { 68 | return nil, err 69 | } 70 | key, err = uokmsClient.GenerateClientPrivateKey() 71 | return key, nil 72 | } 73 | 74 | func printAuthKey() error { 75 | key, err := GenerateAuthKey() 76 | if err != nil { 77 | return err 78 | } 79 | fmt.Println(utils.PureAuthKeyCreateSuccess) 80 | fmt.Println(base64.StdEncoding.EncodeToString(key)) 81 | return nil 82 | } 83 | -------------------------------------------------------------------------------- /cmd/kms/generate_private_key.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015-2020 Virgil Security Inc. 3 | * 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions are 8 | * met: 9 | * 10 | * (1) Redistributions of source code must retain the above copyright 11 | * notice, this list of conditions and the following disclaimer. 12 | * 13 | * (2) Redistributions in binary form must reproduce the above copyright 14 | * notice, this list of conditions and the following disclaimer in 15 | * the documentation and/or other materials provided with the 16 | * distribution. 17 | * 18 | * (3) Neither the name of the copyright holder nor the names of its 19 | * contributors may be used to endorse or promote products derived from 20 | * this software without specific prior written permission. 21 | * 22 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ''AS IS'' AND ANY EXPRESS OR 23 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 24 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 25 | * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, 26 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 27 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 28 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 30 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 31 | * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 32 | * POSSIBILITY OF SUCH DAMAGE. 33 | * 34 | * Lead Maintainer: Virgil Security Inc. 35 | */ 36 | package kms 37 | 38 | import ( 39 | "encoding/base64" 40 | "fmt" 41 | 42 | "github.com/VirgilSecurity/virgil-sdk-go/v6/crypto/wrapper/phe" 43 | "github.com/urfave/cli/v2" 44 | 45 | "github.com/VirgilSecurity/virgil-cli/utils" 46 | ) 47 | 48 | // 49 | // KMSPrivateKey generates KMS Private Key 50 | // 51 | func KMSPrivateKey() *cli.Command { 52 | return &cli.Command{ 53 | Name: "client-private", 54 | Aliases: []string{"pk"}, 55 | Usage: "Generate a new KMS Client Private key", 56 | Action: func(context *cli.Context) error { 57 | err := printKMSPrivateKey() 58 | if err != nil { 59 | return utils.CliExit(err) 60 | } 61 | return err 62 | }, 63 | } 64 | } 65 | 66 | func GenerateKMSPrivateKey() ([]byte, error) { 67 | kmsClient := phe.NewUokmsClient() 68 | if err := kmsClient.SetupDefaults(); err != nil { 69 | return []byte{}, err 70 | } 71 | 72 | return kmsClient.GenerateClientPrivateKey() 73 | } 74 | 75 | func printKMSPrivateKey() error { 76 | key, err := GenerateKMSPrivateKey() 77 | if err != nil { 78 | return err 79 | } 80 | fmt.Println(base64.StdEncoding.EncodeToString(key)) 81 | return nil 82 | } 83 | -------------------------------------------------------------------------------- /cmd/pure/keygen/nms.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015-2020 Virgil Security Inc. 3 | * 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions are 8 | * met: 9 | * 10 | * (1) Redistributions of source code must retain the above copyright 11 | * notice, this list of conditions and the following disclaimer. 12 | * 13 | * (2) Redistributions in binary form must reproduce the above copyright 14 | * notice, this list of conditions and the following disclaimer in 15 | * the documentation and/or other materials provided with the 16 | * distribution. 17 | * 18 | * (3) Neither the name of the copyright holder nor the names of its 19 | * contributors may be used to endorse or promote products derived from 20 | * this software without specific prior written permission. 21 | * 22 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ''AS IS'' AND ANY EXPRESS OR 23 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 24 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 25 | * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, 26 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 27 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 28 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 30 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 31 | * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 32 | * POSSIBILITY OF SUCH DAMAGE. 33 | * 34 | * Lead Maintainer: Virgil Security Inc. 35 | */ 36 | package keygen 37 | 38 | import ( 39 | "encoding/base64" 40 | "fmt" 41 | 42 | "github.com/VirgilSecurity/virgil-sdk-go/v6/crypto/wrapper/foundation" 43 | "github.com/urfave/cli/v2" 44 | 45 | "github.com/VirgilSecurity/virgil-cli/utils" 46 | ) 47 | 48 | // Secret generates secret key 49 | func NonRotatableMasterSecret() *cli.Command { 50 | return &cli.Command{ 51 | Name: "nonrotable-master", 52 | Aliases: []string{"nm"}, 53 | Usage: "Generate a new Non Rotatable Master Secret key", 54 | Action: func(context *cli.Context) error { 55 | err := printNonRotatableMasterSecretKey() 56 | if err != nil { 57 | return utils.CliExit(err) 58 | } 59 | return err 60 | }, 61 | } 62 | } 63 | 64 | func printNonRotatableMasterSecretKey() error { 65 | random := foundation.NewCtrDrbg() 66 | if err := random.SetupDefaults(); err != nil { 67 | return err 68 | } 69 | 70 | nmsBytes, err := random.Random(32) 71 | if err != nil { 72 | return err 73 | } 74 | 75 | fmt.Println(utils.PureNMSKeyCreateSuccessTemplate) 76 | fmt.Printf( 77 | "NM.%s\n", 78 | base64.StdEncoding.EncodeToString(nmsBytes), 79 | ) 80 | return nil 81 | } 82 | -------------------------------------------------------------------------------- /cmd/pure/keygen/all.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015-2020 Virgil Security Inc. 3 | * 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions are 8 | * met: 9 | * 10 | * (1) Redistributions of source code must retain the above copyright 11 | * notice, this list of conditions and the following disclaimer. 12 | * 13 | * (2) Redistributions in binary form must reproduce the above copyright 14 | * notice, this list of conditions and the following disclaimer in 15 | * the documentation and/or other materials provided with the 16 | * distribution. 17 | * 18 | * (3) Neither the name of the copyright holder nor the names of its 19 | * contributors may be used to endorse or promote products derived from 20 | * this software without specific prior written permission. 21 | * 22 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ''AS IS'' AND ANY EXPRESS OR 23 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 24 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 25 | * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, 26 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 27 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 28 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 30 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 31 | * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 32 | * POSSIBILITY OF SUCH DAMAGE. 33 | * 34 | * Lead Maintainer: Virgil Security Inc. 35 | */ 36 | package keygen 37 | 38 | import ( 39 | "fmt" 40 | 41 | "github.com/urfave/cli/v2" 42 | 43 | "github.com/VirgilSecurity/virgil-cli/utils" 44 | ) 45 | 46 | // All generates all pure key pairs 47 | func All() *cli.Command { 48 | return &cli.Command{ 49 | Name: "all", 50 | Usage: "Generate all pure key pairs", 51 | Action: func(context *cli.Context) error { 52 | fmt.Println("----------------------------------------------------------------------------------") 53 | 54 | if err := printBackupKeys(); err != nil { 55 | return utils.CliExit(err) 56 | } 57 | 58 | fmt.Println("==================================================================================") 59 | 60 | if err := printNonRotatableMasterSecretKey(); err != nil { 61 | return utils.CliExit(err) 62 | } 63 | 64 | fmt.Println("==================================================================================") 65 | 66 | if err := printSecretKey(); err != nil { 67 | return utils.CliExit(err) 68 | } 69 | 70 | fmt.Println("----------------------------------------------------------------------------------") 71 | 72 | return nil 73 | }, 74 | } 75 | } 76 | -------------------------------------------------------------------------------- /utils/config.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015-2019 Virgil Security Inc. 3 | * 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions are 8 | * met: 9 | * 10 | * (1) Redistributions of source code must retain the above copyright 11 | * notice, this list of conditions and the following disclaimer. 12 | * 13 | * (2) Redistributions in binary form must reproduce the above copyright 14 | * notice, this list of conditions and the following disclaimer in 15 | * the documentation and/or other materials provided with the 16 | * distribution. 17 | * 18 | * (3) Neither the name of the copyright holder nor the names of its 19 | * contributors may be used to endorse or promote products derived from 20 | * this software without specific prior written permission. 21 | * 22 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ''AS IS'' AND ANY EXPRESS OR 23 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 24 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 25 | * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, 26 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 27 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 28 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 30 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 31 | * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 32 | * POSSIBILITY OF SUCH DAMAGE. 33 | * 34 | * Lead Maintainer: Virgil Security Inc. 35 | */ 36 | 37 | package utils 38 | 39 | import ( 40 | "encoding/json" 41 | "errors" 42 | "io/ioutil" 43 | "os" 44 | "os/user" 45 | "path/filepath" 46 | 47 | "github.com/VirgilSecurity/virgil-cli/models" 48 | ) 49 | 50 | func SaveConfig(token string) error { 51 | u, err := user.Current() 52 | if err != nil { 53 | return err 54 | } 55 | 56 | tokenPath := filepath.Join(u.HomeDir, ".virgil-conf") 57 | 58 | if _, err = os.Stat(tokenPath); os.IsNotExist(err) { 59 | if err = os.Mkdir(tokenPath, 0700); err != nil { 60 | return err 61 | } 62 | } 63 | 64 | tokenPath = filepath.Join(tokenPath, "token") 65 | 66 | if err = ioutil.WriteFile(tokenPath, []byte(token), 0600); err != nil { 67 | return err 68 | } 69 | return nil 70 | } 71 | 72 | func ParseAppConfig(data []byte) (config models.AppConfig, err error) { 73 | err = json.Unmarshal(data, &config) 74 | if err != nil { 75 | return config, errors.New("error parsing config: " + err.Error()) 76 | } 77 | if config.AppID == "" || config.APPKeyID == "" || len(config.APPKey) == 0 { 78 | return config, errors.New("error parsing config: all APP_ID, APP_KEY, APP_KEY_ID must be specified") 79 | } 80 | return 81 | } 82 | -------------------------------------------------------------------------------- /test/functional/app_token_test.go: -------------------------------------------------------------------------------- 1 | package functional 2 | 3 | import ( 4 | "bufio" 5 | "fmt" 6 | "strings" 7 | "testing" 8 | 9 | "github.com/stretchr/testify/assert" 10 | 11 | "github.com/VirgilSecurity/virgil-cli/test/fixtures" 12 | "github.com/VirgilSecurity/virgil-cli/test/helpers" 13 | "github.com/VirgilSecurity/virgil-cli/utils" 14 | ) 15 | 16 | func TestAppTokenCreate(t *testing.T) { 17 | appCreateToken(t) 18 | } 19 | 20 | func TestAppTokenDelete(t *testing.T) { 21 | appTokenName := appCreateToken(t) 22 | cmd := helpers.PrepareCmd("app", "token", "delete", appTokenName) 23 | 24 | cmdOut, _ := cmd.StdoutPipe() 25 | cmdErr, _ := cmd.StderrPipe() 26 | 27 | if err := cmd.Start(); err != nil { 28 | fmt.Printf("Cmd failed to start: %+v\n", err) 29 | } 30 | 31 | scannerOut := bufio.NewScanner(cmdOut) 32 | scannerErr := bufio.NewScanner(cmdErr) 33 | 34 | scannerOut.Scan() 35 | tokenDeleteSuccess := scannerOut.Text() 36 | 37 | assert.Equal(t, utils.AppTokenDeleteSuccess, tokenDeleteSuccess) 38 | 39 | helpers.CmdKiller(cmd, scannerErr) 40 | assert.NoError(t, cmd.Wait()) 41 | } 42 | 43 | func TestAppTokenList(t *testing.T) { 44 | appCreateToken(t) 45 | 46 | cmd := helpers.PrepareCmd("app", "token", "list") 47 | 48 | cmdOut, _ := cmd.StdoutPipe() 49 | cmdErr, _ := cmd.StderrPipe() 50 | 51 | if err := cmd.Start(); err != nil { 52 | fmt.Printf("Cmd failed to start: %+v\n", err) 53 | } 54 | 55 | scannerOut := bufio.NewScanner(cmdOut) 56 | scannerErr := bufio.NewScanner(cmdErr) 57 | 58 | var appTokenListOutPut []string 59 | for scannerOut.Scan() { 60 | appTokenListOutPut = append(appTokenListOutPut, scannerOut.Text()) 61 | } 62 | assert.NotEmpty(t, appTokenListOutPut) 63 | for _, appTokenHeader := range fixtures.AppTokenListHeaders { 64 | if len(appTokenListOutPut) > 0 { 65 | assert.True(t, strings.Contains(appTokenListOutPut[0], appTokenHeader)) 66 | } 67 | } 68 | helpers.CmdKiller(cmd, scannerErr) 69 | assert.NoError(t, cmd.Wait()) 70 | } 71 | 72 | func appCreateToken(t *testing.T) string { 73 | helpers.UserLoginByEmail(UserEmail, UserPassword) 74 | 75 | ok := helpers.UseApp(UserEmail, UserPassword) 76 | assert.True(t, ok) 77 | 78 | appTokenName := helpers.GenerateString()[:24] 79 | 80 | cmd := helpers.PrepareCmd("app", "token", "create", "--name", appTokenName) 81 | 82 | cmdOut, _ := cmd.StdoutPipe() 83 | cmdErr, _ := cmd.StderrPipe() 84 | 85 | if err := cmd.Start(); err != nil { 86 | fmt.Printf("Cmd failed to start: %+v\n", err) 87 | } 88 | 89 | scannerOut := bufio.NewScanner(cmdOut) 90 | scannerErr := bufio.NewScanner(cmdErr) 91 | 92 | scannerOut.Scan() 93 | tokenGeneratedSuccess := scannerOut.Text() 94 | 95 | assert.True(t, strings.Contains(tokenGeneratedSuccess, utils.AppTokenCreateSuccess)) 96 | 97 | helpers.CmdKiller(cmd, scannerErr) 98 | assert.NoError(t, cmd.Wait()) 99 | 100 | return appTokenName 101 | } 102 | -------------------------------------------------------------------------------- /cmd/scms/init.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015-2020 Virgil Security Inc. 3 | * 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions are 8 | * met: 9 | * 10 | * (1) Redistributions of source code must retain the above copyright 11 | * notice, this list of conditions and the following disclaimer. 12 | * 13 | * (2) Redistributions in binary form must reproduce the above copyright 14 | * notice, this list of conditions and the following disclaimer in 15 | * the documentation and/or other materials provided with the 16 | * distribution. 17 | * 18 | * (3) Neither the name of the copyright holder nor the names of its 19 | * contributors may be used to endorse or promote products derived from 20 | * this software without specific prior written permission. 21 | * 22 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ''AS IS'' AND ANY EXPRESS OR 23 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 24 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 25 | * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, 26 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 27 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 28 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 30 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 31 | * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 32 | * POSSIBILITY OF SUCH DAMAGE. 33 | * 34 | * Lead Maintainer: Virgil Security Inc. 35 | */ 36 | package scms 37 | 38 | import ( 39 | "errors" 40 | "fmt" 41 | "net/http" 42 | 43 | "github.com/urfave/cli/v2" 44 | 45 | "github.com/VirgilSecurity/virgil-cli/client" 46 | "github.com/VirgilSecurity/virgil-cli/utils" 47 | ) 48 | 49 | func Init(vcli *client.VirgilHTTPClient) *cli.Command { 50 | return &cli.Command{ 51 | Name: "init", 52 | Usage: "Init scms module in application", 53 | Flags: []cli.Flag{&cli.StringFlag{Name: "app_id", Aliases: []string{"app-id"}, Usage: "application id"}}, 54 | 55 | Action: func(context *cli.Context) (err error) { 56 | defaultApp, _ := utils.LoadDefaultApp() 57 | defaultAppID := "" 58 | if defaultApp != nil { 59 | defaultAppID = defaultApp.ID 60 | } 61 | appID := utils.ReadFlagOrDefault(context, "app_id", defaultAppID) 62 | if appID == "" { 63 | return utils.CliExit(errors.New(utils.SpecifyAppIDFlag)) 64 | } 65 | 66 | err = InitFunc(appID, vcli) 67 | 68 | if err != nil { 69 | return utils.CliExit(err) 70 | } 71 | 72 | fmt.Println(utils.SCMSApplicationInitSuccess) 73 | return nil 74 | }, 75 | } 76 | } 77 | 78 | func InitFunc(appID string, vcli *client.VirgilHTTPClient) (err error) { 79 | _, _, err = utils.SendWithCheckRetry(vcli, http.MethodPost, "scms/"+appID+"/init", nil, nil) 80 | return err 81 | } 82 | -------------------------------------------------------------------------------- /models/application.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015-2019 Virgil Security Inc. 3 | * 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions are 8 | * met: 9 | * 10 | * (1) Redistributions of source code must retain the above copyright 11 | * notice, this list of conditions and the following disclaimer. 12 | * 13 | * (2) Redistributions in binary form must reproduce the above copyright 14 | * notice, this list of conditions and the following disclaimer in 15 | * the documentation and/or other materials provided with the 16 | * distribution. 17 | * 18 | * (3) Neither the name of the copyright holder nor the names of its 19 | * contributors may be used to endorse or promote products derived from 20 | * this software without specific prior written permission. 21 | * 22 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ''AS IS'' AND ANY EXPRESS OR 23 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 24 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 25 | * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, 26 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 27 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 28 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 30 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 31 | * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 32 | * POSSIBILITY OF SUCH DAMAGE. 33 | * 34 | * Lead Maintainer: Virgil Security Inc. 35 | */ 36 | 37 | package models 38 | 39 | import "time" 40 | 41 | type CreateAppRequest struct { 42 | Name string `json:"name"` 43 | Description string `json:"description"` 44 | Type string `json:"type"` 45 | } 46 | 47 | type CreateAppResp struct { 48 | ID string `json:"id"` 49 | } 50 | 51 | type CreateAppTokenRequest struct { 52 | Name string `json:"token_name"` 53 | ApplicationID string `json:"application_id"` 54 | } 55 | 56 | type Application struct { 57 | ID string `json:"id"` 58 | Name string `json:"name"` 59 | CreatedAt time.Time `json:"created_at"` 60 | } 61 | 62 | type StoredApplication struct { 63 | ID string `json:"id"` 64 | Name string `json:"name"` 65 | CreatedAt time.Time `json:"created_at"` 66 | Token string `json:"token"` 67 | IsDefault bool `json:"is_default"` 68 | } 69 | type ApplicationToken struct { 70 | ID string `json:"id"` 71 | Name string `json:"token_name"` 72 | CreatedAt time.Time `json:"created_at"` 73 | Token string `json:"app_token"` 74 | } 75 | 76 | type UpdateAppRequest struct { 77 | Name string `json:"name"` 78 | } 79 | 80 | type AppConfig struct { 81 | AppID string `json:"APP_ID"` 82 | APPKeyID string `json:"APP_KEY_ID"` 83 | APPKey []byte `json:"APP_KEY"` 84 | } 85 | -------------------------------------------------------------------------------- /cmd/pure/keygen.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015-2019 Virgil Security Inc. 3 | * 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions are 8 | * met: 9 | * 10 | * (1) Redistributions of source code must retain the above copyright 11 | * notice, this list of conditions and the following disclaimer. 12 | * 13 | * (2) Redistributions in binary form must reproduce the above copyright 14 | * notice, this list of conditions and the following disclaimer in 15 | * the documentation and/or other materials provided with the 16 | * distribution. 17 | * 18 | * (3) Neither the name of the copyright holder nor the names of its 19 | * contributors may be used to endorse or promote products derived from 20 | * this software without specific prior written permission. 21 | * 22 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ''AS IS'' AND ANY EXPRESS OR 23 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 24 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 25 | * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, 26 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 27 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 28 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 30 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 31 | * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 32 | * POSSIBILITY OF SUCH DAMAGE. 33 | * 34 | * Lead Maintainer: Virgil Security Inc. 35 | */ 36 | 37 | package pure 38 | 39 | import ( 40 | "encoding/base64" 41 | "fmt" 42 | 43 | "github.com/VirgilSecurity/virgil-sdk-go/v6/crypto/wrapper/phe" 44 | "github.com/pkg/errors" 45 | "github.com/urfave/cli/v2" 46 | 47 | "github.com/VirgilSecurity/virgil-cli/cmd/pure/keygen" 48 | "github.com/VirgilSecurity/virgil-cli/utils" 49 | ) 50 | 51 | // Keygen generates PureKit private key 52 | func Keygen() *cli.Command { 53 | return &cli.Command{ 54 | Name: "keygen", 55 | Aliases: []string{"kg"}, 56 | Usage: "Generate a new Pure secret key", 57 | Action: func(context *cli.Context) error { 58 | if context.Args().First() != "" { 59 | return utils.CliExit(errors.New("incorrect key type")) 60 | } 61 | pheClient := phe.NewPheClient() 62 | if err := pheClient.SetupDefaults(); err != nil { 63 | return utils.CliExit(err) 64 | } 65 | key, err := pheClient.GenerateClientPrivateKey() 66 | if err != nil { 67 | return utils.CliExit(err) 68 | } 69 | fmt.Println("SK.1." + base64.StdEncoding.EncodeToString(key)) 70 | return nil 71 | }, 72 | Subcommands: []*cli.Command{ 73 | keygen.Secret(), 74 | keygen.Auth(), 75 | keygen.Backup(), 76 | keygen.VirgilStorage(), 77 | keygen.OwnSigningKey(), 78 | keygen.All(), 79 | keygen.NonRotatableMasterSecret(), 80 | }, 81 | } 82 | } 83 | -------------------------------------------------------------------------------- /cmd/pure/keygen/os.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015-2020 Virgil Security Inc. 3 | * 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions are 8 | * met: 9 | * 10 | * (1) Redistributions of source code must retain the above copyright 11 | * notice, this list of conditions and the following disclaimer. 12 | * 13 | * (2) Redistributions in binary form must reproduce the above copyright 14 | * notice, this list of conditions and the following disclaimer in 15 | * the documentation and/or other materials provided with the 16 | * distribution. 17 | * 18 | * (3) Neither the name of the copyright holder nor the names of its 19 | * contributors may be used to endorse or promote products derived from 20 | * this software without specific prior written permission. 21 | * 22 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ''AS IS'' AND ANY EXPRESS OR 23 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 24 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 25 | * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, 26 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 27 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 28 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 30 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 31 | * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 32 | * POSSIBILITY OF SUCH DAMAGE. 33 | * 34 | * Lead Maintainer: Virgil Security Inc. 35 | */ 36 | package keygen 37 | 38 | import ( 39 | "encoding/base64" 40 | "fmt" 41 | 42 | "github.com/VirgilSecurity/virgil-sdk-go/v6/crypto" 43 | "github.com/urfave/cli/v2" 44 | 45 | "github.com/VirgilSecurity/virgil-cli/utils" 46 | ) 47 | 48 | // OwnSigningKey is generates a new own signing key 49 | func OwnSigningKey() *cli.Command { 50 | return &cli.Command{ 51 | Name: "own", 52 | Aliases: []string{"os"}, 53 | Usage: "Generate a new own Signing key", 54 | Description: "Own signing key is used to sign data that is encrypted", 55 | Action: func(context *cli.Context) error { 56 | err := printOwnSigningKeys() 57 | if err != nil { 58 | return utils.CliExit(err) 59 | } 60 | return err 61 | }, 62 | } 63 | } 64 | 65 | func printOwnSigningKeys() error { 66 | crypt := &crypto.Crypto{} 67 | keyPair, err := crypt.GenerateKeypair() 68 | 69 | if err != nil { 70 | return err 71 | } 72 | 73 | prKey, err := crypt.ExportPrivateKey(keyPair) 74 | if err != nil { 75 | return err 76 | } 77 | pkKey, err := crypt.ExportPublicKey(keyPair.PublicKey()) 78 | if err != nil { 79 | return err 80 | } 81 | 82 | fmt.Println(utils.PureOwnSigningKeyCreateSuccessTemplate) 83 | fmt.Println("OSSK." + base64.StdEncoding.EncodeToString(prKey)) 84 | fmt.Println("OSPK." + base64.StdEncoding.EncodeToString(pkKey)) 85 | 86 | return nil 87 | } 88 | -------------------------------------------------------------------------------- /cmd/pure/keygen/sk.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015-2020 Virgil Security Inc. 3 | * 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions are 8 | * met: 9 | * 10 | * (1) Redistributions of source code must retain the above copyright 11 | * notice, this list of conditions and the following disclaimer. 12 | * 13 | * (2) Redistributions in binary form must reproduce the above copyright 14 | * notice, this list of conditions and the following disclaimer in 15 | * the documentation and/or other materials provided with the 16 | * distribution. 17 | * 18 | * (3) Neither the name of the copyright holder nor the names of its 19 | * contributors may be used to endorse or promote products derived from 20 | * this software without specific prior written permission. 21 | * 22 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ''AS IS'' AND ANY EXPRESS OR 23 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 24 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 25 | * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, 26 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 27 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 28 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 30 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 31 | * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 32 | * POSSIBILITY OF SUCH DAMAGE. 33 | * 34 | * Lead Maintainer: Virgil Security Inc. 35 | */ 36 | package keygen 37 | 38 | import ( 39 | "encoding/base64" 40 | "fmt" 41 | 42 | "github.com/VirgilSecurity/virgil-sdk-go/v6/crypto/wrapper/phe" 43 | "github.com/urfave/cli/v2" 44 | 45 | "github.com/VirgilSecurity/virgil-cli/cmd/kms" 46 | "github.com/VirgilSecurity/virgil-cli/utils" 47 | ) 48 | 49 | // Secret generates secret key 50 | func Secret() *cli.Command { 51 | return &cli.Command{ 52 | Name: "secret", 53 | Aliases: []string{"sk"}, 54 | Usage: "Generate a new Secret key", 55 | Action: func(context *cli.Context) error { 56 | err := printSecretKey() 57 | if err != nil { 58 | return utils.CliExit(err) 59 | } 60 | return err 61 | }, 62 | } 63 | } 64 | 65 | func printSecretKey() error { 66 | pheClient := phe.NewPheClient() 67 | if err := pheClient.SetupDefaults(); err != nil { 68 | return err 69 | } 70 | 71 | pheKey, err := pheClient.GenerateClientPrivateKey() 72 | if err != nil { 73 | return err 74 | } 75 | kmsKey, err := kms.GenerateKMSPrivateKey() 76 | if err != nil { 77 | return err 78 | } 79 | authKey, err := GenerateAuthKey() 80 | if err != nil { 81 | return err 82 | } 83 | fmt.Println(utils.PureSecretKeyCreateSuccess) 84 | fmt.Printf( 85 | "SK.1.%s.%s.%s\n", 86 | base64.StdEncoding.EncodeToString(pheKey), 87 | base64.StdEncoding.EncodeToString(kmsKey), 88 | base64.StdEncoding.EncodeToString(authKey), 89 | ) 90 | return nil 91 | } 92 | -------------------------------------------------------------------------------- /client/protobuf/http_error.pb.go: -------------------------------------------------------------------------------- 1 | // Code generated by protoc-gen-go. DO NOT EDIT. 2 | // source: http_error.proto 3 | 4 | package protobuf 5 | 6 | import ( 7 | fmt "fmt" 8 | proto "github.com/golang/protobuf/proto" 9 | math "math" 10 | ) 11 | 12 | // Reference imports to suppress errors if they are not otherwise used. 13 | var _ = proto.Marshal 14 | var _ = fmt.Errorf 15 | var _ = math.Inf 16 | 17 | // This is a compile-time assertion to ensure that this generated file 18 | // is compatible with the proto package it is being compiled against. 19 | // A compilation error at this line likely means your copy of the 20 | // proto package needs to be updated. 21 | const _ = proto.ProtoPackageIsVersion3 // please upgrade the proto package 22 | 23 | type HttpError struct { 24 | Code uint32 `protobuf:"varint,1,opt,name=code,proto3" json:"code,omitempty"` 25 | Message string `protobuf:"bytes,2,opt,name=message,proto3" json:"message,omitempty"` 26 | XXX_NoUnkeyedLiteral struct{} `json:"-"` 27 | XXX_unrecognized []byte `json:"-"` 28 | XXX_sizecache int32 `json:"-"` 29 | } 30 | 31 | func (m *HttpError) Reset() { *m = HttpError{} } 32 | func (m *HttpError) String() string { return proto.CompactTextString(m) } 33 | func (*HttpError) ProtoMessage() {} 34 | func (*HttpError) Descriptor() ([]byte, []int) { 35 | return fileDescriptor_f89808e2956c2911, []int{0} 36 | } 37 | 38 | func (m *HttpError) XXX_Unmarshal(b []byte) error { 39 | return xxx_messageInfo_HttpError.Unmarshal(m, b) 40 | } 41 | func (m *HttpError) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { 42 | return xxx_messageInfo_HttpError.Marshal(b, m, deterministic) 43 | } 44 | func (m *HttpError) XXX_Merge(src proto.Message) { 45 | xxx_messageInfo_HttpError.Merge(m, src) 46 | } 47 | func (m *HttpError) XXX_Size() int { 48 | return xxx_messageInfo_HttpError.Size(m) 49 | } 50 | func (m *HttpError) XXX_DiscardUnknown() { 51 | xxx_messageInfo_HttpError.DiscardUnknown(m) 52 | } 53 | 54 | var xxx_messageInfo_HttpError proto.InternalMessageInfo 55 | 56 | func (m *HttpError) GetCode() uint32 { 57 | if m != nil { 58 | return m.Code 59 | } 60 | return 0 61 | } 62 | 63 | func (m *HttpError) GetMessage() string { 64 | if m != nil { 65 | return m.Message 66 | } 67 | return "" 68 | } 69 | 70 | func init() { 71 | proto.RegisterType((*HttpError)(nil), "protobuf.HttpError") 72 | } 73 | 74 | func init() { proto.RegisterFile("http_error.proto", fileDescriptor_f89808e2956c2911) } 75 | 76 | var fileDescriptor_f89808e2956c2911 = []byte{ 77 | // 101 bytes of a gzipped FileDescriptorProto 78 | 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xe2, 0x12, 0xc8, 0x28, 0x29, 0x29, 79 | 0x88, 0x4f, 0x2d, 0x2a, 0xca, 0x2f, 0xd2, 0x2b, 0x28, 0xca, 0x2f, 0xc9, 0x17, 0xe2, 0x00, 0x53, 80 | 0x49, 0xa5, 0x69, 0x4a, 0x96, 0x5c, 0x9c, 0x1e, 0x25, 0x25, 0x05, 0xae, 0x20, 0x49, 0x21, 0x21, 81 | 0x2e, 0x96, 0xe4, 0xfc, 0x94, 0x54, 0x09, 0x46, 0x05, 0x46, 0x0d, 0xde, 0x20, 0x30, 0x5b, 0x48, 82 | 0x82, 0x8b, 0x3d, 0x37, 0xb5, 0xb8, 0x38, 0x31, 0x3d, 0x55, 0x82, 0x49, 0x81, 0x51, 0x83, 0x33, 83 | 0x08, 0xc6, 0x4d, 0x62, 0x03, 0x1b, 0x62, 0x0c, 0x08, 0x00, 0x00, 0xff, 0xff, 0x19, 0x9f, 0x2d, 84 | 0xf1, 0x5f, 0x00, 0x00, 0x00, 85 | } 86 | -------------------------------------------------------------------------------- /test/helpers/mailinator.go: -------------------------------------------------------------------------------- 1 | package helpers 2 | 3 | import ( 4 | "encoding/json" 5 | "fmt" 6 | "io/ioutil" 7 | "net/http" 8 | "os" 9 | "regexp" 10 | 11 | "github.com/pkg/errors" 12 | ) 13 | 14 | const ( 15 | MailinatorApi = "https://api.mailinator.com/api/" 16 | ConfirmCodePattern = "href=\"https://dashboard(-.{3})?.virgilsecurity.com/api/auth/register/confirm/(.*)\"" 17 | ) 18 | 19 | type MailinatorMessageInfo struct { 20 | Subject string `json:"subject"` 21 | ID string `json:"id"` 22 | SecondsAgo int `json:"seconds_ago"` 23 | } 24 | 25 | type MailinatorMessagePart struct { 26 | Body string `json:"body"` 27 | } 28 | 29 | type MessageData struct { 30 | Data MailinatorMessage `json:"data"` 31 | } 32 | 33 | type MailinatorMessage struct { 34 | Parts []MailinatorMessagePart `json:"parts"` 35 | } 36 | 37 | type MailinatorInboxResponse struct { 38 | Messages []MailinatorMessageInfo `json:"messages"` 39 | } 40 | 41 | func GetConfirmCode(email string) string { 42 | messageText, err := loadMailinatorMessage(email) 43 | re := regexp.MustCompile(ConfirmCodePattern) 44 | res := re.FindAllStringSubmatch(messageText, -1) 45 | if err != nil { 46 | fmt.Printf("mailinator error: %+v", err) 47 | } 48 | confCode := res[0][2] 49 | return confCode 50 | } 51 | 52 | func loadMailinatorMessage(email string) (msg string, err error) { 53 | mailinatorToken := os.Getenv("MAILINATOR_TOKEN") 54 | if mailinatorToken == "" { 55 | fmt.Println("WARNING! Mailinator token not set!") 56 | } 57 | 58 | resp, err := http.Get(fmt.Sprintf(MailinatorApi+"inbox?token=%s&to=%s", mailinatorToken, email)) 59 | if err != nil { 60 | return "", errors.WithMessage(err, "loadMailinatorMessage Get inbox failed: ") 61 | } 62 | 63 | if resp != nil { 64 | defer resp.Body.Close() 65 | } 66 | 67 | body, err := ioutil.ReadAll(resp.Body) 68 | if err != nil { 69 | return "", err 70 | } 71 | 72 | var mailinatorMessageList MailinatorInboxResponse 73 | err = json.Unmarshal(body, &mailinatorMessageList) 74 | if err != nil { 75 | return "", errors.WithMessage(err, "loadMailinatorMessage json mailinator unmarshal inbox failed: ") 76 | } 77 | 78 | if len(mailinatorMessageList.Messages) == 0 { 79 | return "", errors.New("mailinator inbox empty") 80 | } 81 | 82 | if len(mailinatorMessageList.Messages) > 1 { 83 | return "", errors.New("mailinator inbox to few mails") 84 | } 85 | 86 | messageID := mailinatorMessageList.Messages[0].ID 87 | resp, err = http.Get(fmt.Sprintf(MailinatorApi+"message?token=%s&id=%s", mailinatorToken, messageID)) 88 | if err != nil { 89 | return "", errors.WithMessage(err, "loadMailinatorMessage Get message failed: ") 90 | } 91 | 92 | if resp != nil { 93 | defer resp.Body.Close() 94 | } 95 | 96 | body, err = ioutil.ReadAll(resp.Body) 97 | if err != nil { 98 | return "", errors.WithMessage(err, "loadMailinatorMessage read message body failed: ") 99 | } 100 | 101 | var message MessageData 102 | err = json.Unmarshal(body, &message) 103 | if err != nil { 104 | return "", errors.WithMessage(err, "loadMailinatorMessage json mailinator unmarshal message failed: ") 105 | } 106 | 107 | return message.Data.Parts[0].Body, nil 108 | } 109 | -------------------------------------------------------------------------------- /cmd/login.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015-2019 Virgil Security Inc. 3 | * 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions are 8 | * met: 9 | * 10 | * (1) Redistributions of source code must retain the above copyright 11 | * notice, this list of conditions and the following disclaimer. 12 | * 13 | * (2) Redistributions in binary form must reproduce the above copyright 14 | * notice, this list of conditions and the following disclaimer in 15 | * the documentation and/or other materials provided with the 16 | * distribution. 17 | * 18 | * (3) Neither the name of the copyright holder nor the names of its 19 | * contributors may be used to endorse or promote products derived from 20 | * this software without specific prior written permission. 21 | * 22 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ''AS IS'' AND ANY EXPRESS OR 23 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 24 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 25 | * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, 26 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 27 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 28 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 30 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 31 | * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 32 | * POSSIBILITY OF SUCH DAMAGE. 33 | * 34 | * Lead Maintainer: Virgil Security Inc. 35 | */ 36 | 37 | package cmd 38 | 39 | import ( 40 | "fmt" 41 | "os" 42 | "strings" 43 | 44 | "github.com/howeyc/gopass" 45 | "github.com/urfave/cli/v2" 46 | 47 | "github.com/VirgilSecurity/virgil-cli/client" 48 | "github.com/VirgilSecurity/virgil-cli/utils" 49 | ) 50 | 51 | func Login(client *client.VirgilHTTPClient) *cli.Command { 52 | return &cli.Command{ 53 | Name: "login", 54 | Usage: "Open user session", 55 | Flags: []cli.Flag{ 56 | &cli.StringFlag{Name: "username", Aliases: []string{"u"}, Usage: "user email"}, 57 | &cli.StringFlag{Name: "password", Aliases: []string{"p"}, Usage: "user password"}, 58 | }, 59 | Action: func(context *cli.Context) error { 60 | _ = utils.DeleteAccessToken() 61 | _ = utils.DeleteAppFile() 62 | 63 | email := utils.ReadFlagOrDefault(context, "username", "") 64 | pwd := utils.ReadFlagOrDefault(context, "password", "") 65 | 66 | if email == "" { 67 | email = strings.TrimSpace(utils.ReadParamOrDefaultOrFromConsole(context, "email", utils.EmailPrompt, "")) 68 | } 69 | 70 | if pwd == "" { 71 | pwdBytes, err := gopass.GetPasswdPrompt(utils.PasswordPrompt+"\r\n", false, os.Stdin, os.Stdout) 72 | if err != nil { 73 | return utils.CliExit(err) 74 | } 75 | 76 | pwd = string(pwdBytes) 77 | } 78 | 79 | err := utils.Login(email, pwd, client) 80 | 81 | if err == nil { 82 | fmt.Printf("%s %s", utils.LoginSuccess, email) 83 | return err 84 | } 85 | 86 | return utils.CliExit(err) 87 | }, 88 | } 89 | } 90 | -------------------------------------------------------------------------------- /cmd/app/create.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015-2020 Virgil Security Inc. 3 | * 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions are 8 | * met: 9 | * 10 | * (1) Redistributions of source code must retain the above copyright 11 | * notice, this list of conditions and the following disclaimer. 12 | * 13 | * (2) Redistributions in binary form must reproduce the above copyright 14 | * notice, this list of conditions and the following disclaimer in 15 | * the documentation and/or other materials provided with the 16 | * distribution. 17 | * 18 | * (3) Neither the name of the copyright holder nor the names of its 19 | * contributors may be used to endorse or promote products derived from 20 | * this software without specific prior written permission. 21 | * 22 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ''AS IS'' AND ANY EXPRESS OR 23 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 24 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 25 | * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, 26 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 27 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 28 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 30 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 31 | * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 32 | * POSSIBILITY OF SUCH DAMAGE. 33 | * 34 | * Lead Maintainer: Virgil Security Inc. 35 | */ 36 | 37 | package app 38 | 39 | import ( 40 | "fmt" 41 | "net/http" 42 | 43 | "github.com/pkg/errors" 44 | "github.com/urfave/cli/v2" 45 | 46 | "github.com/VirgilSecurity/virgil-cli/client" 47 | "github.com/VirgilSecurity/virgil-cli/models" 48 | "github.com/VirgilSecurity/virgil-cli/utils" 49 | ) 50 | 51 | func Create(vcli *client.VirgilHTTPClient) *cli.Command { 52 | return &cli.Command{ 53 | Name: "create", 54 | Aliases: []string{"c"}, 55 | ArgsUsage: "app_name", 56 | Usage: "Create a new app", 57 | Flags: []cli.Flag{&cli.StringFlag{Name: "type", Usage: "application type (e2ee or pure)"}}, 58 | 59 | Action: func(context *cli.Context) (err error) { 60 | name := utils.ReadParamOrDefaultOrFromConsole(context, "name", utils.ApplicationNamePrompt, "") 61 | var appID string 62 | 63 | appID, err = CreateFunc(name, vcli) 64 | 65 | if err != nil { 66 | return utils.CliExit(err) 67 | } 68 | 69 | fmt.Println(utils.ApplicationIDOutput, appID) 70 | fmt.Println(utils.ApplicationCreateSuccess) 71 | return nil 72 | }, 73 | } 74 | } 75 | 76 | func CreateFunc(name string, vcli *client.VirgilHTTPClient) (appID string, err error) { 77 | req := &models.CreateAppRequest{Name: name, Type: "pki"} 78 | resp := &models.Application{} 79 | 80 | _, _, err = utils.SendWithCheckRetry(vcli, http.MethodPost, "application", req, resp) 81 | 82 | if err != nil { 83 | return 84 | } 85 | 86 | if resp != nil { 87 | return resp.ID, nil 88 | } 89 | 90 | return "", errors.New("empty response") 91 | } 92 | -------------------------------------------------------------------------------- /cmd/app/update.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015-2019 Virgil Security Inc. 3 | * 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions are 8 | * met: 9 | * 10 | * (1) Redistributions of source code must retain the above copyright 11 | * notice, this list of conditions and the following disclaimer. 12 | * 13 | * (2) Redistributions in binary form must reproduce the above copyright 14 | * notice, this list of conditions and the following disclaimer in 15 | * the documentation and/or other materials provided with the 16 | * distribution. 17 | * 18 | * (3) Neither the name of the copyright holder nor the names of its 19 | * contributors may be used to endorse or promote products derived from 20 | * this software without specific prior written permission. 21 | * 22 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ''AS IS'' AND ANY EXPRESS OR 23 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 24 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 25 | * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, 26 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 27 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 28 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 30 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 31 | * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 32 | * POSSIBILITY OF SUCH DAMAGE. 33 | * 34 | * Lead Maintainer: Virgil Security Inc. 35 | */ 36 | 37 | package app 38 | 39 | import ( 40 | "fmt" 41 | "net/http" 42 | 43 | "github.com/pkg/errors" 44 | 45 | "github.com/urfave/cli/v2" 46 | 47 | "github.com/VirgilSecurity/virgil-cli/client" 48 | "github.com/VirgilSecurity/virgil-cli/models" 49 | "github.com/VirgilSecurity/virgil-cli/utils" 50 | ) 51 | 52 | func Update(vcli *client.VirgilHTTPClient) *cli.Command { 53 | return &cli.Command{ 54 | Name: "update", 55 | Aliases: []string{"u"}, 56 | ArgsUsage: "app_id", 57 | Usage: "Update app by id", 58 | Action: func(context *cli.Context) (err error) { 59 | defaultApp, _ := utils.LoadDefaultApp() 60 | defaultAppID := "" 61 | if defaultApp != nil { 62 | defaultAppID = defaultApp.ID 63 | } 64 | 65 | appID := utils.ReadParamOrDefaultOrFromConsole(context, "appID", utils.ApplicationIDPrompt, defaultAppID) 66 | 67 | _, err = getApp(appID, vcli) 68 | if err != nil { 69 | return utils.CliExit(err) 70 | } 71 | err = UpdateFunc(appID, vcli) 72 | 73 | if err == nil { 74 | fmt.Println(utils.ApplicationUpdateSuccess) 75 | } else if err == utils.ErrEntityNotFound { 76 | return utils.CliExit(errors.New(fmt.Sprintf("%s %s\n", utils.ApplicationNotFound, appID))) 77 | } 78 | 79 | if err != nil { 80 | return utils.CliExit(err) 81 | } 82 | return err 83 | }, 84 | } 85 | } 86 | 87 | func UpdateFunc(appID string, vcli *client.VirgilHTTPClient) (err error) { 88 | name := utils.ReadConsoleValue("name", utils.ApplicationNamePrompt) 89 | 90 | req := &models.UpdateAppRequest{Name: name} 91 | 92 | _, _, err = utils.SendWithCheckRetry(vcli, http.MethodPut, "application/"+appID, req, nil) 93 | 94 | return err 95 | } 96 | -------------------------------------------------------------------------------- /cmd/app/key/delete.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015-2019 Virgil Security Inc. 3 | * 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions are 8 | * met: 9 | * 10 | * (1) Redistributions of source code must retain the above copyright 11 | * notice, this list of conditions and the following disclaimer. 12 | * 13 | * (2) Redistributions in binary form must reproduce the above copyright 14 | * notice, this list of conditions and the following disclaimer in 15 | * the documentation and/or other materials provided with the 16 | * distribution. 17 | * 18 | * (3) Neither the name of the copyright holder nor the names of its 19 | * contributors may be used to endorse or promote products derived from 20 | * this software without specific prior written permission. 21 | * 22 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ''AS IS'' AND ANY EXPRESS OR 23 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 24 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 25 | * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, 26 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 27 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 28 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 30 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 31 | * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 32 | * POSSIBILITY OF SUCH DAMAGE. 33 | * 34 | * Lead Maintainer: Virgil Security Inc. 35 | */ 36 | 37 | package key 38 | 39 | import ( 40 | "fmt" 41 | "net/http" 42 | 43 | "github.com/pkg/errors" 44 | "github.com/urfave/cli/v2" 45 | 46 | "github.com/VirgilSecurity/virgil-cli/client" 47 | "github.com/VirgilSecurity/virgil-cli/utils" 48 | ) 49 | 50 | func Delete(vcli *client.VirgilHTTPClient) *cli.Command { 51 | return &cli.Command{ 52 | Name: "delete", 53 | Aliases: []string{"d"}, 54 | ArgsUsage: "app-key_id", 55 | Usage: "Delete App Key by id", 56 | Flags: []cli.Flag{&cli.StringFlag{Name: "app_id", Aliases: []string{"app-id"}, Usage: "application id"}}, 57 | Action: func(context *cli.Context) (err error) { 58 | defaultApp, _ := utils.LoadDefaultApp() 59 | defaultAppID := "" 60 | if defaultApp != nil { 61 | defaultAppID = defaultApp.ID 62 | } 63 | 64 | appID := utils.ReadFlagOrDefault(context, "app_id", defaultAppID) 65 | if appID == "" { 66 | return utils.CliExit(errors.New(utils.SpecifyAppIDFlag)) 67 | } 68 | 69 | apiKeyID := utils.ReadParamOrDefaultOrFromConsole(context, "id", utils.AppKeyIDPrompt, "") 70 | 71 | err = deleteAPIKeyIDFunc(apiKeyID, appID, vcli) 72 | 73 | if err == nil { 74 | fmt.Println(utils.AppKeyDeleteSuccess) 75 | } else if err == utils.ErrEntityNotFound { 76 | return utils.CliExit(errors.New(fmt.Sprintf("%s %s \n", utils.ApiKeyNotFound, apiKeyID))) 77 | } 78 | 79 | if err != nil { 80 | return utils.CliExit(err) 81 | } 82 | return err 83 | }, 84 | } 85 | } 86 | 87 | func deleteAPIKeyIDFunc(apiKeyID, appID string, vcli *client.VirgilHTTPClient) (err error) { 88 | _, _, err = utils.SendWithCheckRetry(vcli, http.MethodDelete, "application/"+appID+"/apikey/"+apiKeyID, nil, nil) 89 | 90 | return err 91 | } 92 | -------------------------------------------------------------------------------- /utils/token.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015-2019 Virgil Security Inc. 3 | * 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions are 8 | * met: 9 | * 10 | * (1) Redistributions of source code must retain the above copyright 11 | * notice, this list of conditions and the following disclaimer. 12 | * 13 | * (2) Redistributions in binary form must reproduce the above copyright 14 | * notice, this list of conditions and the following disclaimer in 15 | * the documentation and/or other materials provided with the 16 | * distribution. 17 | * 18 | * (3) Neither the name of the copyright holder nor the names of its 19 | * contributors may be used to endorse or promote products derived from 20 | * this software without specific prior written permission. 21 | * 22 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ''AS IS'' AND ANY EXPRESS OR 23 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 24 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 25 | * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, 26 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 27 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 28 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 30 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 31 | * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 32 | * POSSIBILITY OF SUCH DAMAGE. 33 | * 34 | * Lead Maintainer: Virgil Security Inc. 35 | */ 36 | 37 | package utils 38 | 39 | import ( 40 | "io/ioutil" 41 | "os" 42 | "os/user" 43 | "path/filepath" 44 | 45 | "github.com/pkg/errors" 46 | ) 47 | 48 | func SaveAccessToken(token string) error { 49 | u, err := user.Current() 50 | if err != nil { 51 | return err 52 | } 53 | 54 | tokenPath := filepath.Join(u.HomeDir, ".virgil") 55 | 56 | if _, err = os.Stat(tokenPath); os.IsNotExist(err) { 57 | if err = os.Mkdir(tokenPath, 0700); err != nil { 58 | return err 59 | } 60 | } 61 | 62 | tokenPath = filepath.Join(tokenPath, "token") 63 | 64 | if err = ioutil.WriteFile(tokenPath, []byte(token), 0600); err != nil { 65 | return err 66 | } 67 | return nil 68 | } 69 | 70 | func LoadAccessToken() (token string, err error) { 71 | u, err := user.Current() 72 | if err != nil { 73 | return "", err 74 | } 75 | 76 | tokenPath := filepath.Join(u.HomeDir, ".virgil") 77 | 78 | if _, err = os.Stat(tokenPath); os.IsNotExist(err) { 79 | return "", errors.New("access token folder does not exist") 80 | } 81 | 82 | tokenPath = filepath.Join(tokenPath, "token") 83 | 84 | tokenRaw, err := ioutil.ReadFile(tokenPath) 85 | if err != nil { 86 | return "", err 87 | } 88 | return string(tokenRaw), nil 89 | } 90 | 91 | func DeleteAccessToken() error { 92 | u, err := user.Current() 93 | if err != nil { 94 | return err 95 | } 96 | 97 | tokenPath := filepath.Join(u.HomeDir, ".virgil") 98 | 99 | if _, err := os.Stat(tokenPath); os.IsNotExist(err) { 100 | return errors.New(".virgil directory does not exist") 101 | } 102 | 103 | tokenPath = filepath.Join(tokenPath, "token") 104 | 105 | return os.Remove(tokenPath) 106 | } 107 | -------------------------------------------------------------------------------- /cmd/app/token/delete.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015-2019 Virgil Security Inc. 3 | * 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions are 8 | * met: 9 | * 10 | * (1) Redistributions of source code must retain the above copyright 11 | * notice, this list of conditions and the following disclaimer. 12 | * 13 | * (2) Redistributions in binary form must reproduce the above copyright 14 | * notice, this list of conditions and the following disclaimer in 15 | * the documentation and/or other materials provided with the 16 | * distribution. 17 | * 18 | * (3) Neither the name of the copyright holder nor the names of its 19 | * contributors may be used to endorse or promote products derived from 20 | * this software without specific prior written permission. 21 | * 22 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ''AS IS'' AND ANY EXPRESS OR 23 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 24 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 25 | * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, 26 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 27 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 28 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 30 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 31 | * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 32 | * POSSIBILITY OF SUCH DAMAGE. 33 | * 34 | * Lead Maintainer: Virgil Security Inc. 35 | */ 36 | 37 | package token 38 | 39 | import ( 40 | "fmt" 41 | "net/http" 42 | 43 | "github.com/pkg/errors" 44 | "github.com/urfave/cli/v2" 45 | 46 | "github.com/VirgilSecurity/virgil-cli/client" 47 | "github.com/VirgilSecurity/virgil-cli/models" 48 | "github.com/VirgilSecurity/virgil-cli/utils" 49 | ) 50 | 51 | func Delete(vcli *client.VirgilHTTPClient) *cli.Command { 52 | return &cli.Command{ 53 | Name: "delete", 54 | Aliases: []string{"d"}, 55 | ArgsUsage: "name", 56 | Usage: "Delete app token by name", 57 | Flags: []cli.Flag{&cli.StringFlag{Name: "app_id", Aliases: []string{"app-id"}, Usage: "app id"}}, 58 | Action: func(context *cli.Context) (err error) { 59 | defaultApp, _ := utils.LoadDefaultApp() 60 | defaultAppID := "" 61 | if defaultApp != nil { 62 | defaultAppID = defaultApp.ID 63 | } 64 | name := utils.ReadParamOrDefaultOrFromConsole(context, "name", utils.AppTokenNamePrompt, "") 65 | 66 | appID := utils.ReadFlagOrDefault(context, "app_id", defaultAppID) 67 | if appID == "" { 68 | return utils.CliExit(errors.New(utils.SpecifyAppIDFlag)) 69 | } 70 | 71 | var tokens []*models.ApplicationToken 72 | tokens, err = listFunc(appID, vcli) 73 | 74 | if err != nil { 75 | return utils.CliExit(err) 76 | } 77 | for _, t := range tokens { 78 | if t.Name == name { 79 | err = deleteAppTokenFunc(appID, t.ID, vcli) 80 | if err == nil { 81 | fmt.Println(utils.AppTokenDeleteSuccess) 82 | return nil 83 | } 84 | return utils.CliExit(err) 85 | } 86 | } 87 | fmt.Println(utils.AppTokenNotFound) 88 | return utils.CliExit(err) 89 | }, 90 | } 91 | } 92 | 93 | func deleteAppTokenFunc(appID, appTokenID string, vcli *client.VirgilHTTPClient) (err error) { 94 | _, _, err = utils.SendWithCheckRetry(vcli, http.MethodDelete, "application/"+appID+"/tokens/"+appTokenID, nil, nil) 95 | return err 96 | } 97 | -------------------------------------------------------------------------------- /cmd/keygen.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015-2020 Virgil Security Inc. 3 | * 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions are 8 | * met: 9 | * 10 | * (1) Redistributions of source code must retain the above copyright 11 | * notice, this list of conditions and the following disclaimer. 12 | * 13 | * (2) Redistributions in binary form must reproduce the above copyright 14 | * notice, this list of conditions and the following disclaimer in 15 | * the documentation and/or other materials provided with the 16 | * distribution. 17 | * 18 | * (3) Neither the name of the copyright holder nor the names of its 19 | * contributors may be used to endorse or promote products derived from 20 | * this software without specific prior written permission. 21 | * 22 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ''AS IS'' AND ANY EXPRESS OR 23 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 24 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 25 | * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, 26 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 27 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 28 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 30 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 31 | * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 32 | * POSSIBILITY OF SUCH DAMAGE. 33 | * 34 | * Lead Maintainer: Virgil Security Inc. 35 | */ 36 | package cmd 37 | 38 | import ( 39 | "encoding/base64" 40 | "fmt" 41 | "io" 42 | "os" 43 | 44 | "github.com/urfave/cli/v2" 45 | 46 | "github.com/VirgilSecurity/virgil-cli/utils" 47 | ) 48 | 49 | func Keygen() *cli.Command { 50 | return &cli.Command{ 51 | Name: "keygen", 52 | Usage: "Generate keypair", 53 | Flags: []cli.Flag{&cli.StringFlag{Name: "o", Usage: "destination file name"}}, 54 | Action: func(context *cli.Context) error { 55 | pass := utils.ReadFlagOrDefault(context, "p", "") 56 | key, err := KeygenFunc() 57 | if err != nil { 58 | return utils.CliExit(err) 59 | } 60 | 61 | var writer io.Writer = os.Stdout 62 | if fileName := utils.ReadFlagOrDefault(context, "o", ""); fileName != "" { 63 | var file *os.File 64 | file, err = os.Create(fileName) 65 | if err != nil { 66 | return utils.CliExit(err) 67 | } 68 | defer func() { 69 | if ferr := file.Close(); ferr != nil { 70 | panic(ferr) 71 | } 72 | }() 73 | 74 | writer = file 75 | } 76 | 77 | encrypted := " " 78 | if pass != "" { 79 | encrypted = " ENCRYPTED " 80 | } 81 | 82 | _, err = fmt.Fprintf(writer, "-----BEGIN%sPRIVATE KEY-----\n", encrypted) 83 | if err != nil { 84 | return utils.CliExit(err) 85 | } 86 | _, err = fmt.Fprintln(writer, base64.StdEncoding.EncodeToString(key)) 87 | if err != nil { 88 | return utils.CliExit(err) 89 | } 90 | _, err = fmt.Fprintf(writer, "-----END%sPRIVATE KEY-----\n", encrypted) 91 | 92 | if err != nil { 93 | return utils.CliExit(err) 94 | } 95 | 96 | return err 97 | }, 98 | } 99 | } 100 | 101 | func KeygenFunc() (privateKey []byte, err error) { 102 | keyPair, err := crypt.GenerateKeypair() 103 | 104 | if err != nil { 105 | return nil, err 106 | } 107 | 108 | return crypt.ExportPrivateKey(keyPair) 109 | } 110 | -------------------------------------------------------------------------------- /cmd/kms/delete_update_token.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015-2020 Virgil Security Inc. 3 | * 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions are 8 | * met: 9 | * 10 | * (1) Redistributions of source code must retain the above copyright 11 | * notice, this list of conditions and the following disclaimer. 12 | * 13 | * (2) Redistributions in binary form must reproduce the above copyright 14 | * notice, this list of conditions and the following disclaimer in 15 | * the documentation and/or other materials provided with the 16 | * distribution. 17 | * 18 | * (3) Neither the name of the copyright holder nor the names of its 19 | * contributors may be used to endorse or promote products derived from 20 | * this software without specific prior written permission. 21 | * 22 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ''AS IS'' AND ANY EXPRESS OR 23 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 24 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 25 | * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, 26 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 27 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 28 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 30 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 31 | * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 32 | * POSSIBILITY OF SUCH DAMAGE. 33 | * 34 | * Lead Maintainer: Virgil Security Inc. 35 | */ 36 | package kms 37 | 38 | import ( 39 | "errors" 40 | "fmt" 41 | "net/http" 42 | 43 | "github.com/golang/protobuf/proto" 44 | "github.com/urfave/cli/v2" 45 | 46 | "github.com/VirgilSecurity/virgil-cli/client" 47 | "github.com/VirgilSecurity/virgil-cli/cmd/kms/protobuf/decryptor" 48 | "github.com/VirgilSecurity/virgil-cli/utils" 49 | ) 50 | 51 | func DeleteUpdateToken(vcli *client.VirgilHTTPClient) *cli.Command { 52 | return &cli.Command{ 53 | Name: "delete-update-token", 54 | Aliases: []string{"dut"}, 55 | ArgsUsage: "kms_key_alias", 56 | Usage: "Delete KMS update token", 57 | Flags: []cli.Flag{&cli.StringFlag{Name: "app-token", Usage: "application token"}}, 58 | Action: func(context *cli.Context) (err error) { 59 | if context.Args().First() == "" { 60 | return utils.CliExit(errors.New(utils.KMSKeyAliasInvalid)) 61 | } 62 | aliasKMSKey := context.Args().First() 63 | 64 | defaultApp, _ := utils.LoadDefaultApp() 65 | defaultAppToken := "" 66 | if defaultApp != nil { 67 | defaultAppToken = defaultApp.Token 68 | } 69 | 70 | appToken := utils.ReadFlagOrDefault(context, "app-token", defaultAppToken) 71 | if appToken == "" { 72 | return utils.CliExit(errors.New(utils.SpecifyAppTokenFlag)) 73 | } 74 | 75 | if err := deleteUpdateToken(appToken, aliasKMSKey, vcli); err != nil { 76 | return utils.CliExit(err) 77 | } 78 | return nil 79 | }, 80 | } 81 | } 82 | 83 | func deleteUpdateToken(appToken string, keyAlias string, vcli *client.VirgilHTTPClient) (err error) { 84 | reqPayload, err := proto.Marshal(&decryptor.KeypairRequest{Alias: keyAlias}) 85 | if err != nil { 86 | return err 87 | } 88 | 89 | _, _, err = utils.SendProtoWithCheckRetry(vcli, http.MethodPost, PrefixKMSApi+"/delete-update-token", reqPayload, nil, appToken) 90 | 91 | if err != nil { 92 | return err 93 | } 94 | 95 | fmt.Println(utils.KMSUpdateTokenDeleteSuccess) 96 | return nil 97 | } 98 | -------------------------------------------------------------------------------- /cmd/use.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015-2019 Virgil Security Inc. 3 | * 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions are 8 | * met: 9 | * 10 | * (1) Redistributions of source code must retain the above copyright 11 | * notice, this list of conditions and the following disclaimer. 12 | * 13 | * (2) Redistributions in binary form must reproduce the above copyright 14 | * notice, this list of conditions and the following disclaimer in 15 | * the documentation and/or other materials provided with the 16 | * distribution. 17 | * 18 | * (3) Neither the name of the copyright holder nor the names of its 19 | * contributors may be used to endorse or promote products derived from 20 | * this software without specific prior written permission. 21 | * 22 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ''AS IS'' AND ANY EXPRESS OR 23 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 24 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 25 | * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, 26 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 27 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 28 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 30 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 31 | * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 32 | * POSSIBILITY OF SUCH DAMAGE. 33 | * 34 | * Lead Maintainer: Virgil Security Inc. 35 | */ 36 | 37 | package cmd 38 | 39 | import ( 40 | "fmt" 41 | "net/http" 42 | "strings" 43 | 44 | "github.com/pkg/errors" 45 | "github.com/urfave/cli/v2" 46 | 47 | "github.com/VirgilSecurity/virgil-cli/client" 48 | "github.com/VirgilSecurity/virgil-cli/models" 49 | "github.com/VirgilSecurity/virgil-cli/utils" 50 | ) 51 | 52 | func UseApp(client *client.VirgilHTTPClient) *cli.Command { 53 | return &cli.Command{ 54 | Name: "use", 55 | Aliases: []string{"use-default-app"}, 56 | ArgsUsage: "name", 57 | Usage: "Changes context to app with specified name. All future commands without specifying app_id will be applied to current app", 58 | Action: func(context *cli.Context) error { 59 | err := useFunc(context, client) 60 | if err != nil { 61 | return utils.CliExit(err) 62 | } 63 | return err 64 | }, 65 | } 66 | } 67 | 68 | func useFunc(context *cli.Context, vcli *client.VirgilHTTPClient) error { 69 | if context.NArg() < 1 { 70 | return errors.New(utils.UseInvalidNumberArguments) 71 | } 72 | 73 | appName := strings.Join(context.Args().Slice(), " ") 74 | 75 | var apps []*models.Application 76 | apps, err := listFunc(vcli) 77 | 78 | if err != nil { 79 | return err 80 | } 81 | 82 | for _, app := range apps { 83 | if app.Name == appName { 84 | err := utils.SaveDefaultApp(vcli, app) 85 | if err != nil { 86 | return err 87 | } 88 | fmt.Println(utils.ApplicationSetContextSuccess) 89 | fmt.Println(utils.UseApplicationWarning) 90 | return nil 91 | } 92 | } 93 | 94 | return errors.New(utils.ApplicationWithNameNotFound + appName) 95 | } 96 | 97 | func listFunc(vcli *client.VirgilHTTPClient) (apps []*models.Application, err error) { 98 | _, _, err = utils.SendWithCheckRetry(vcli, http.MethodGet, "applications", nil, &apps) 99 | 100 | if err != nil { 101 | return apps, err 102 | } 103 | 104 | if apps != nil { 105 | return apps, nil 106 | } 107 | 108 | return nil, errors.New("empty response") 109 | } 110 | -------------------------------------------------------------------------------- /cmd/app/list.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015-2019 Virgil Security Inc. 3 | * 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions are 8 | * met: 9 | * 10 | * (1) Redistributions of source code must retain the above copyright 11 | * notice, this list of conditions and the following disclaimer. 12 | * 13 | * (2) Redistributions in binary form must reproduce the above copyright 14 | * notice, this list of conditions and the following disclaimer in 15 | * the documentation and/or other materials provided with the 16 | * distribution. 17 | * 18 | * (3) Neither the name of the copyright holder nor the names of its 19 | * contributors may be used to endorse or promote products derived from 20 | * this software without specific prior written permission. 21 | * 22 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ''AS IS'' AND ANY EXPRESS OR 23 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 24 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 25 | * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, 26 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 27 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 28 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 30 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 31 | * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 32 | * POSSIBILITY OF SUCH DAMAGE. 33 | * 34 | * Lead Maintainer: Virgil Security Inc. 35 | */ 36 | 37 | package app 38 | 39 | import ( 40 | "fmt" 41 | "net/http" 42 | "sort" 43 | 44 | "github.com/pkg/errors" 45 | "github.com/urfave/cli/v2" 46 | 47 | "github.com/VirgilSecurity/virgil-cli/client" 48 | "github.com/VirgilSecurity/virgil-cli/models" 49 | "github.com/VirgilSecurity/virgil-cli/utils" 50 | ) 51 | 52 | func List(vcli *client.VirgilHTTPClient) *cli.Command { 53 | return &cli.Command{ 54 | Name: "list", 55 | Aliases: []string{"l"}, 56 | Usage: "List your apps", 57 | Action: func(context *cli.Context) (err error) { 58 | var apps []*models.Application 59 | apps, err = listFunc(vcli) 60 | 61 | if err != nil { 62 | return utils.CliExit(err) 63 | } 64 | 65 | defaultApp, _ := utils.LoadDefaultApp() 66 | defaultAppID := "" 67 | if defaultApp != nil { 68 | defaultAppID = defaultApp.ID 69 | } 70 | if len(apps) == 0 { 71 | fmt.Println(utils.ApplicationsNotCreateYet) 72 | return nil 73 | } 74 | sort.Slice(apps, func(i, j int) bool { 75 | return apps[i].CreatedAt.Before(apps[j].CreatedAt) 76 | }) 77 | fmt.Printf("|%25s|%35s|%20s\n", "Application name ", "APP_ID ", " created_at ") 78 | fmt.Printf("|%25s|%35s|%20s\n", 79 | "-------------------------", 80 | "-----------------------------------", 81 | "---------------------------------------", 82 | ) 83 | for _, app := range apps { 84 | appName := app.Name 85 | if app.ID == defaultAppID { 86 | appName += " (default)" 87 | } 88 | fmt.Printf("| %23s | %33s | %19s\n", appName, app.ID, app.CreatedAt) 89 | } 90 | return nil 91 | }, 92 | } 93 | } 94 | 95 | func listFunc(vcli *client.VirgilHTTPClient) (apps []*models.Application, err error) { 96 | _, _, err = utils.SendWithCheckRetry(vcli, http.MethodGet, "applications", nil, &apps) 97 | 98 | if err != nil { 99 | return 100 | } 101 | 102 | if apps != nil { 103 | return apps, nil 104 | } 105 | 106 | return nil, errors.New("empty response") 107 | } 108 | -------------------------------------------------------------------------------- /cmd/app/token/create.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015-2019 Virgil Security Inc. 3 | * 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions are 8 | * met: 9 | * 10 | * (1) Redistributions of source code must retain the above copyright 11 | * notice, this list of conditions and the following disclaimer. 12 | * 13 | * (2) Redistributions in binary form must reproduce the above copyright 14 | * notice, this list of conditions and the following disclaimer in 15 | * the documentation and/or other materials provided with the 16 | * distribution. 17 | * 18 | * (3) Neither the name of the copyright holder nor the names of its 19 | * contributors may be used to endorse or promote products derived from 20 | * this software without specific prior written permission. 21 | * 22 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ''AS IS'' AND ANY EXPRESS OR 23 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 24 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 25 | * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, 26 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 27 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 28 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 30 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 31 | * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 32 | * POSSIBILITY OF SUCH DAMAGE. 33 | * 34 | * Lead Maintainer: Virgil Security Inc. 35 | */ 36 | 37 | package token 38 | 39 | import ( 40 | "fmt" 41 | "net/http" 42 | 43 | "github.com/pkg/errors" 44 | "github.com/urfave/cli/v2" 45 | 46 | "github.com/VirgilSecurity/virgil-cli/client" 47 | "github.com/VirgilSecurity/virgil-cli/models" 48 | "github.com/VirgilSecurity/virgil-cli/utils" 49 | ) 50 | 51 | func Create(vcli *client.VirgilHTTPClient) *cli.Command { 52 | return &cli.Command{ 53 | Name: "create", 54 | ArgsUsage: "token_name", 55 | Usage: "Create a new app token", 56 | Flags: []cli.Flag{&cli.StringFlag{Name: "app_id", Aliases: []string{"app-id"}, Usage: "app id"}, 57 | &cli.StringFlag{Name: "name", Usage: "app token name"}}, 58 | 59 | Action: func(context *cli.Context) (err error) { 60 | defaultApp, _ := utils.LoadDefaultApp() 61 | defaultAppID := "" 62 | if defaultApp != nil { 63 | defaultAppID = defaultApp.ID 64 | } 65 | 66 | appID := utils.ReadFlagOrDefault(context, "app_id", defaultAppID) 67 | if appID == "" { 68 | return utils.CliExit(errors.New(utils.SpecifyAppIDFlag)) 69 | } 70 | name := utils.ReadFlagOrDefault(context, "name", "") 71 | if name == "" { 72 | return utils.CliExit(errors.New(utils.SpecifyTokenNameFlag)) 73 | } 74 | 75 | token, err := CreateFunc(appID, name, vcli) 76 | 77 | if err != nil { 78 | return utils.CliExit(err) 79 | } 80 | fmt.Println(utils.AppTokenCreateSuccess) 81 | fmt.Println(utils.AppTokenSuccessTemplate + " " + token) 82 | return nil 83 | }, 84 | } 85 | } 86 | 87 | func CreateFunc(appID, name string, vcli *client.VirgilHTTPClient) (token string, err error) { 88 | req := &models.CreateAppTokenRequest{Name: name, ApplicationID: appID} 89 | resp := &models.ApplicationToken{} 90 | 91 | _, _, err = utils.SendWithCheckRetry(vcli, http.MethodPost, "/application/"+appID+"/tokens", req, resp) 92 | 93 | if err != nil { 94 | return "", err 95 | } 96 | if resp != nil { 97 | return resp.Token, nil 98 | } 99 | 100 | return "", errors.New("empty response") 101 | } 102 | -------------------------------------------------------------------------------- /cmd/scms/dcm/dcm_list.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015-2020 Virgil Security Inc. 3 | * 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions are 8 | * met: 9 | * 10 | * (1) Redistributions of source code must retain the above copyright 11 | * notice, this list of conditions and the following disclaimer. 12 | * 13 | * (2) Redistributions in binary form must reproduce the above copyright 14 | * notice, this list of conditions and the following disclaimer in 15 | * the documentation and/or other materials provided with the 16 | * distribution. 17 | * 18 | * (3) Neither the name of the copyright holder nor the names of its 19 | * contributors may be used to endorse or promote products derived from 20 | * this software without specific prior written permission. 21 | * 22 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ''AS IS'' AND ANY EXPRESS OR 23 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 24 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 25 | * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, 26 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 27 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 28 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 30 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 31 | * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 32 | * POSSIBILITY OF SUCH DAMAGE. 33 | * 34 | * Lead Maintainer: Virgil Security Inc. 35 | */ 36 | package dcm 37 | 38 | import ( 39 | "fmt" 40 | "net/http" 41 | "sort" 42 | 43 | "github.com/pkg/errors" 44 | "github.com/urfave/cli/v2" 45 | 46 | "github.com/VirgilSecurity/virgil-cli/client" 47 | "github.com/VirgilSecurity/virgil-cli/models" 48 | "github.com/VirgilSecurity/virgil-cli/utils" 49 | ) 50 | 51 | func List(vcli *client.VirgilHTTPClient) *cli.Command { 52 | return &cli.Command{ 53 | Name: "list", 54 | Aliases: []string{"l"}, 55 | Usage: "List your dcm certificates", 56 | Flags: []cli.Flag{ 57 | &cli.StringFlag{Name: "app-token", Usage: "application token"}}, 58 | 59 | Action: func(context *cli.Context) (err error) { 60 | defaultApp, _ := utils.LoadDefaultApp() 61 | defaultAppToken := "" 62 | if defaultApp != nil { 63 | defaultAppToken = defaultApp.Token 64 | } 65 | appToken := utils.ReadFlagOrDefault(context, "app-token", defaultAppToken) 66 | if appToken == "" { 67 | return utils.CliExit(errors.New(utils.SpecifyAppTokenFlag)) 68 | } 69 | 70 | certs, err := dcmListFunc(appToken, vcli) 71 | if err != nil { 72 | return utils.CliExit(err) 73 | } 74 | 75 | if len(certs) == 0 { 76 | fmt.Println(utils.SCMSDCMCertificatesNotCreatedYet) 77 | return nil 78 | } 79 | sort.Slice(certs, func(i, j int) bool { 80 | return certs[i].CreatedAt.Before(certs[j].CreatedAt) 81 | }) 82 | fmt.Printf("|%25s|%20s\n", "Certificate name ", " created_at ") 83 | fmt.Printf("|%25s|%20s\n", "-------------------------", "---------------------------------------") 84 | for _, cert := range certs { 85 | fmt.Printf("|%24s | %19s\n", cert.Name, cert.CreatedAt) 86 | } 87 | return nil 88 | }, 89 | } 90 | } 91 | 92 | func dcmListFunc(appToken string, vcli *client.VirgilHTTPClient) (apps []*models.DcmCertificateListItem, err error) { 93 | _, _, err = utils.SendWithCheckRetry(vcli, http.MethodGet, "/scms/dcm", nil, &apps, appToken) 94 | 95 | if err != nil { 96 | return 97 | } 98 | 99 | if apps != nil { 100 | return apps, nil 101 | } 102 | 103 | return nil, errors.New("empty response") 104 | } 105 | -------------------------------------------------------------------------------- /cmd/app/token/list.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015-2019 Virgil Security Inc. 3 | * 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions are 8 | * met: 9 | * 10 | * (1) Redistributions of source code must retain the above copyright 11 | * notice, this list of conditions and the following disclaimer. 12 | * 13 | * (2) Redistributions in binary form must reproduce the above copyright 14 | * notice, this list of conditions and the following disclaimer in 15 | * the documentation and/or other materials provided with the 16 | * distribution. 17 | * 18 | * (3) Neither the name of the copyright holder nor the names of its 19 | * contributors may be used to endorse or promote products derived from 20 | * this software without specific prior written permission. 21 | * 22 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ''AS IS'' AND ANY EXPRESS OR 23 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 24 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 25 | * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, 26 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 27 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 28 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 30 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 31 | * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 32 | * POSSIBILITY OF SUCH DAMAGE. 33 | * 34 | * Lead Maintainer: Virgil Security Inc. 35 | */ 36 | 37 | package token 38 | 39 | import ( 40 | "fmt" 41 | "net/http" 42 | "sort" 43 | 44 | "github.com/pkg/errors" 45 | "github.com/urfave/cli/v2" 46 | 47 | "github.com/VirgilSecurity/virgil-cli/client" 48 | "github.com/VirgilSecurity/virgil-cli/models" 49 | "github.com/VirgilSecurity/virgil-cli/utils" 50 | ) 51 | 52 | func List(vcli *client.VirgilHTTPClient) *cli.Command { 53 | return &cli.Command{ 54 | Name: "list", 55 | Aliases: []string{"l"}, 56 | Usage: "List your app tokens", 57 | Flags: []cli.Flag{&cli.StringFlag{Name: "app_id", Aliases: []string{"app-id"}, Usage: "app id"}}, 58 | Action: func(context *cli.Context) (err error) { 59 | defaultApp, _ := utils.LoadDefaultApp() 60 | defaultAppID := "" 61 | if defaultApp != nil { 62 | defaultAppID = defaultApp.ID 63 | } 64 | 65 | appID := utils.ReadFlagOrDefault(context, "app_id", defaultAppID) 66 | if appID == "" { 67 | return utils.CliExit(errors.New(utils.SpecifyAppIDFlag)) 68 | } 69 | 70 | var tokens []*models.ApplicationToken 71 | tokens, err = listFunc(appID, vcli) 72 | 73 | if err != nil { 74 | return utils.CliExit(err) 75 | } 76 | if len(tokens) == 0 { 77 | fmt.Println(utils.AppTokensNotCreatedYet) 78 | return nil 79 | } 80 | sort.Slice(tokens, func(i, j int) bool { 81 | return tokens[i].CreatedAt.Before(tokens[j].CreatedAt) 82 | }) 83 | fmt.Printf("|%50s|%20s\n", " Name ", " Created On ") 84 | fmt.Printf("|%50s|%20s\n", "--------------------------------------------------", "----------------------------------------") 85 | for _, t := range tokens { 86 | fmt.Printf("| %48s | %18s\n", t.Name, t.CreatedAt) 87 | } 88 | return nil 89 | }, 90 | } 91 | } 92 | 93 | func listFunc(appID string, vcli *client.VirgilHTTPClient) (apps []*models.ApplicationToken, err error) { 94 | _, _, err = utils.SendWithCheckRetry(vcli, http.MethodGet, "application/"+appID+"/tokens", nil, &apps) 95 | 96 | if err != nil { 97 | return 98 | } 99 | 100 | if apps != nil { 101 | return apps, nil 102 | } 103 | 104 | return nil, errors.New("empty response") 105 | } 106 | -------------------------------------------------------------------------------- /cmd/sign.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015-2020 Virgil Security Inc. 3 | * 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions are 8 | * met: 9 | * 10 | * (1) Redistributions of source code must retain the above copyright 11 | * notice, this list of conditions and the following disclaimer. 12 | * 13 | * (2) Redistributions in binary form must reproduce the above copyright 14 | * notice, this list of conditions and the following disclaimer in 15 | * the documentation and/or other materials provided with the 16 | * distribution. 17 | * 18 | * (3) Neither the name of the copyright holder nor the names of its 19 | * contributors may be used to endorse or promote products derived from 20 | * this software without specific prior written permission. 21 | * 22 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ''AS IS'' AND ANY EXPRESS OR 23 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 24 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 25 | * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, 26 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 27 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 28 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 30 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 31 | * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 32 | * POSSIBILITY OF SUCH DAMAGE. 33 | * 34 | * Lead Maintainer: Virgil Security Inc. 35 | */ 36 | package cmd 37 | 38 | import ( 39 | "encoding/base64" 40 | "errors" 41 | "fmt" 42 | "io" 43 | "os" 44 | 45 | "github.com/urfave/cli/v2" 46 | 47 | "github.com/VirgilSecurity/virgil-cli/utils" 48 | ) 49 | 50 | func Sign() *cli.Command { 51 | return &cli.Command{ 52 | Name: "sign", 53 | ArgsUsage: "[pr_key]", 54 | Usage: "Sign data", 55 | Flags: []cli.Flag{&cli.StringFlag{Name: "o", Usage: "destination file name"}, 56 | &cli.StringFlag{Name: "key", Usage: "private key file"}, 57 | &cli.StringFlag{Name: "i", Usage: "input file"}, 58 | }, 59 | Action: func(context *cli.Context) error { 60 | destinationFileName := utils.ReadFlagOrDefault(context, "o", "") 61 | dataToSign, err := utils.ReadFileFlagOrParamOrFromConsole(context, "i", "data", utils.SignDataPrompt) 62 | if err != nil { 63 | return utils.CliExit(err) 64 | } 65 | keyFileName := utils.ReadFlagOrDefault(context, "key", "") 66 | privateKeyString, err := utils.ReadKeyStringFromFile(context, keyFileName) 67 | if err != nil { 68 | return utils.CliExit(err) 69 | } 70 | 71 | var writer io.Writer = os.Stdout 72 | if destinationFileName != "" { 73 | var file *os.File 74 | file, err = os.Create(destinationFileName) 75 | if err != nil { 76 | return utils.CliExit(err) 77 | } 78 | defer func() { 79 | if ferr := file.Close(); ferr != nil { 80 | panic(ferr) 81 | } 82 | }() 83 | 84 | writer = file 85 | } 86 | 87 | signature, err := SignFunc(privateKeyString, dataToSign) 88 | 89 | if err != nil { 90 | return utils.CliExit(err) 91 | } 92 | 93 | _, err = fmt.Fprint(writer, base64.StdEncoding.EncodeToString(signature)) 94 | if err != nil { 95 | return utils.CliExit(err) 96 | } 97 | fmt.Println() 98 | 99 | return err 100 | }, 101 | } 102 | } 103 | 104 | func SignFunc(privateKeyString string, data []byte) (publicKey []byte, err error) { 105 | pk, err := crypt.ImportPrivateKey([]byte(privateKeyString)) 106 | 107 | if err != nil { 108 | return nil, errors.New(utils.SignCantParsePrivateKey) 109 | } 110 | 111 | return crypt.Sign(data, pk) 112 | } 113 | -------------------------------------------------------------------------------- /cmd/scms/device/device_list.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015-2020 Virgil Security Inc. 3 | * 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions are 8 | * met: 9 | * 10 | * (1) Redistributions of source code must retain the above copyright 11 | * notice, this list of conditions and the following disclaimer. 12 | * 13 | * (2) Redistributions in binary form must reproduce the above copyright 14 | * notice, this list of conditions and the following disclaimer in 15 | * the documentation and/or other materials provided with the 16 | * distribution. 17 | * 18 | * (3) Neither the name of the copyright holder nor the names of its 19 | * contributors may be used to endorse or promote products derived from 20 | * this software without specific prior written permission. 21 | * 22 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ''AS IS'' AND ANY EXPRESS OR 23 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 24 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 25 | * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, 26 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 27 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 28 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 30 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 31 | * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 32 | * POSSIBILITY OF SUCH DAMAGE. 33 | * 34 | * Lead Maintainer: Virgil Security Inc. 35 | */ 36 | package device 37 | 38 | import ( 39 | "fmt" 40 | "net/http" 41 | 42 | "github.com/pkg/errors" 43 | "github.com/urfave/cli/v2" 44 | 45 | "github.com/VirgilSecurity/virgil-cli/client" 46 | "github.com/VirgilSecurity/virgil-cli/models" 47 | "github.com/VirgilSecurity/virgil-cli/utils" 48 | ) 49 | 50 | func List(vcli *client.VirgilHTTPClient) *cli.Command { 51 | return &cli.Command{ 52 | Name: "list", 53 | Aliases: []string{"l"}, 54 | Usage: "List your devices", 55 | Flags: []cli.Flag{ 56 | &cli.StringFlag{Name: "app-token", Usage: "application token"}}, 57 | Action: func(context *cli.Context) (err error) { 58 | defaultApp, _ := utils.LoadDefaultApp() 59 | defaultAppToken := "" 60 | if defaultApp != nil { 61 | defaultAppToken = defaultApp.Token 62 | } 63 | appToken := utils.ReadFlagOrDefault(context, "app-token", defaultAppToken) 64 | if appToken == "" { 65 | return utils.CliExit(errors.New(utils.SpecifyAppTokenFlag)) 66 | } 67 | 68 | devices, err := deviceListFunc(appToken, vcli) 69 | if err != nil { 70 | return utils.CliExit(err) 71 | } 72 | 73 | if len(devices) == 0 { 74 | fmt.Println(utils.SCMSDeviceNotYetRegistered) 75 | return nil 76 | } 77 | 78 | fmt.Printf("|%25s|%35s|%20s|%20s\n", "Device id ", "dcm id ", " valid_from ", " valid_to ") 79 | fmt.Printf("|%25s|%35s|%20s|%20s\n", 80 | "-------------------------", 81 | "-----------------------------------", 82 | "---------------------------------------", 83 | "---------------------------------------", 84 | ) 85 | for _, d := range devices { 86 | fmt.Printf("|%25s|%35s| %19s | %19s\n", d.ID, d.DcmID, d.ValidFrom, d.ValidTo) 87 | } 88 | return nil 89 | }, 90 | } 91 | } 92 | 93 | func deviceListFunc(appToken string, vcli *client.VirgilHTTPClient) (devices []*models.Device, err error) { 94 | _, _, err = utils.SendWithCheckRetry(vcli, http.MethodGet, "scms/devices", nil, &devices, appToken) 95 | 96 | if err != nil { 97 | return 98 | } 99 | 100 | if devices != nil { 101 | return devices, nil 102 | } 103 | 104 | return nil, errors.New("empty response") 105 | } 106 | -------------------------------------------------------------------------------- /utils/sender.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015-2019 Virgil Security Inc. 3 | * 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions are 8 | * met: 9 | * 10 | * (1) Redistributions of source code must retain the above copyright 11 | * notice, this list of conditions and the following disclaimer. 12 | * 13 | * (2) Redistributions in binary form must reproduce the above copyright 14 | * notice, this list of conditions and the following disclaimer in 15 | * the documentation and/or other materials provided with the 16 | * distribution. 17 | * 18 | * (3) Neither the name of the copyright holder nor the names of its 19 | * contributors may be used to endorse or promote products derived from 20 | * this software without specific prior written permission. 21 | * 22 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ''AS IS'' AND ANY EXPRESS OR 23 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 24 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 25 | * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, 26 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 27 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 28 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 30 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 31 | * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 32 | * POSSIBILITY OF SUCH DAMAGE. 33 | * 34 | * Lead Maintainer: Virgil Security Inc. 35 | */ 36 | 37 | package utils 38 | 39 | import ( 40 | "net/http" 41 | 42 | "github.com/VirgilSecurity/virgil-cli/client" 43 | ) 44 | 45 | func SendWithCheckRetry( 46 | vcli *client.VirgilHTTPClient, 47 | method string, 48 | urlPath string, 49 | payload interface{}, 50 | respObj interface{}, 51 | extraOptions ...interface{}, 52 | ) (headers http.Header, cookie string, err error) { 53 | 54 | token := "" 55 | if len(extraOptions) == 0 { 56 | token, err = LoadAccessTokenOrLogin(vcli) 57 | 58 | if err != nil { 59 | return nil, "", err 60 | } 61 | } 62 | header := http.Header{} 63 | 64 | if len(extraOptions) > 0 { 65 | t, ok := extraOptions[0].(string) 66 | if ok && t[:2] == "MT" { 67 | header.Add("SessionToken", t) 68 | } else { 69 | header.Add("AppToken", t) 70 | } 71 | } else if token != "" { 72 | header.Add("ManagementToken", token) 73 | } 74 | 75 | var vErr *client.VirgilAPIError 76 | for vErr == nil { 77 | _, _, vErr = vcli.Send(method, urlPath, payload, respObj, header) 78 | if vErr == nil { 79 | break 80 | } 81 | 82 | _, err = CheckRetry(vErr, vcli) 83 | } 84 | 85 | return nil, "", err 86 | } 87 | 88 | func SendProtoWithCheckRetry( 89 | vcli *client.VirgilHTTPClient, 90 | method string, 91 | urlPath string, 92 | body []byte, 93 | respObj *[]byte, 94 | extraOptions ...interface{}, 95 | ) (headers http.Header, cookie string, err error) { 96 | 97 | token := "" 98 | if len(extraOptions) == 0 { 99 | token, err = LoadAccessTokenOrLogin(vcli) 100 | 101 | if err != nil { 102 | return nil, "", err 103 | } 104 | } 105 | header := http.Header{} 106 | 107 | if len(extraOptions) > 0 { 108 | t, ok := extraOptions[0].(string) 109 | if ok && t[:2] == "MT" { 110 | header.Add("SessionToken", t) 111 | } else { 112 | header.Add("AppToken", t) 113 | } 114 | } else if token != "" { 115 | header.Add("ManagementToken", token) 116 | } 117 | 118 | var vErr *client.VirgilAPIError 119 | for vErr == nil { 120 | _, _, vErr = vcli.SendProto(method, urlPath, body, respObj, header) 121 | if vErr == nil { 122 | break 123 | } 124 | 125 | _, err = CheckRetry(vErr, vcli) 126 | } 127 | 128 | return nil, "", err 129 | } 130 | -------------------------------------------------------------------------------- /cmd/app/key/update.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015-2019 Virgil Security Inc. 3 | * 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions are 8 | * met: 9 | * 10 | * (1) Redistributions of source code must retain the above copyright 11 | * notice, this list of conditions and the following disclaimer. 12 | * 13 | * (2) Redistributions in binary form must reproduce the above copyright 14 | * notice, this list of conditions and the following disclaimer in 15 | * the documentation and/or other materials provided with the 16 | * distribution. 17 | * 18 | * (3) Neither the name of the copyright holder nor the names of its 19 | * contributors may be used to endorse or promote products derived from 20 | * this software without specific prior written permission. 21 | * 22 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ''AS IS'' AND ANY EXPRESS OR 23 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 24 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 25 | * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, 26 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 27 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 28 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 30 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 31 | * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 32 | * POSSIBILITY OF SUCH DAMAGE. 33 | * 34 | * Lead Maintainer: Virgil Security Inc. 35 | */ 36 | 37 | package key 38 | 39 | import ( 40 | "bufio" 41 | "fmt" 42 | "net/http" 43 | "os" 44 | 45 | "github.com/pkg/errors" 46 | "github.com/urfave/cli/v2" 47 | 48 | "github.com/VirgilSecurity/virgil-cli/client" 49 | "github.com/VirgilSecurity/virgil-cli/models" 50 | "github.com/VirgilSecurity/virgil-cli/utils" 51 | ) 52 | 53 | func Update(vcli *client.VirgilHTTPClient) *cli.Command { 54 | return &cli.Command{ 55 | Name: "update", 56 | Aliases: []string{"u"}, 57 | ArgsUsage: "app_key_id", 58 | Usage: "Update existing app-key by id", 59 | Flags: []cli.Flag{&cli.StringFlag{Name: "app_id", Aliases: []string{"app-id"}, Usage: "application id"}}, 60 | Action: func(context *cli.Context) (err error) { 61 | defaultApp, _ := utils.LoadDefaultApp() 62 | defaultAppID := "" 63 | if defaultApp != nil { 64 | defaultAppID = defaultApp.ID 65 | } 66 | 67 | appID := utils.ReadFlagOrDefault(context, "app_id", defaultAppID) 68 | if appID == "" { 69 | return utils.CliExit(errors.New(utils.SpecifyAppIDFlag)) 70 | } 71 | 72 | apiKeyID := utils.ReadParamOrDefaultOrFromConsole(context, "app_key_id", "Enter App Key ID", "") 73 | 74 | _, err = getKey(appID, apiKeyID, vcli) 75 | if err != nil { 76 | return utils.CliExit(err) 77 | } 78 | 79 | err = UpdateFunc(appID, apiKeyID, vcli) 80 | 81 | if err != nil { 82 | return utils.CliExit(err) 83 | } 84 | 85 | fmt.Println("App Key has been successfully updated.") 86 | return nil 87 | }, 88 | } 89 | } 90 | 91 | func UpdateFunc(appID, apiKeyID string, vcli *client.VirgilHTTPClient) (err error) { 92 | scanner := bufio.NewScanner(os.Stdin) 93 | 94 | fmt.Println("Enter new App Key name:") 95 | name := "" 96 | for name == "" { 97 | scanner.Scan() 98 | name = scanner.Text() 99 | if name == "" { 100 | fmt.Printf("name can't be empty") 101 | fmt.Println("Enter new App Key name:") 102 | } 103 | } 104 | 105 | req := &models.UpdateAccessKeyRequest{Name: name} 106 | 107 | _, _, err = utils.SendWithCheckRetry(vcli, http.MethodPut, "application/"+appID+"/apikey/"+apiKeyID, req, nil) 108 | 109 | if err != nil { 110 | return err 111 | } 112 | return err 113 | } 114 | -------------------------------------------------------------------------------- /cmd/kms/get_update_token.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015-2020 Virgil Security Inc. 3 | * 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions are 8 | * met: 9 | * 10 | * (1) Redistributions of source code must retain the above copyright 11 | * notice, this list of conditions and the following disclaimer. 12 | * 13 | * (2) Redistributions in binary form must reproduce the above copyright 14 | * notice, this list of conditions and the following disclaimer in 15 | * the documentation and/or other materials provided with the 16 | * distribution. 17 | * 18 | * (3) Neither the name of the copyright holder nor the names of its 19 | * contributors may be used to endorse or promote products derived from 20 | * this software without specific prior written permission. 21 | * 22 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ''AS IS'' AND ANY EXPRESS OR 23 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 24 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 25 | * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, 26 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 27 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 28 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 30 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 31 | * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 32 | * POSSIBILITY OF SUCH DAMAGE. 33 | * 34 | * Lead Maintainer: Virgil Security Inc. 35 | */ 36 | package kms 37 | 38 | import ( 39 | "encoding/base64" 40 | "fmt" 41 | "net/http" 42 | 43 | "github.com/golang/protobuf/proto" 44 | "github.com/pkg/errors" 45 | "github.com/urfave/cli/v2" 46 | 47 | "github.com/VirgilSecurity/virgil-cli/client" 48 | "github.com/VirgilSecurity/virgil-cli/cmd/kms/protobuf/decryptor" 49 | "github.com/VirgilSecurity/virgil-cli/utils" 50 | ) 51 | 52 | func GetUpdateToken(vcli *client.VirgilHTTPClient) *cli.Command { 53 | return &cli.Command{ 54 | Name: "get-update-token", 55 | Aliases: []string{"ut"}, 56 | ArgsUsage: "kms_key_alias", 57 | Usage: "Get KMS update token", 58 | Flags: []cli.Flag{&cli.StringFlag{Name: "app-token", Usage: "application token"}}, 59 | Action: func(context *cli.Context) (err error) { 60 | if context.Args().First() == "" { 61 | return utils.CliExit(errors.New(utils.KMSKeyAliasInvalid)) 62 | } 63 | aliasKMSKey := context.Args().First() 64 | 65 | defaultApp, _ := utils.LoadDefaultApp() 66 | defaultAppToken := "" 67 | if defaultApp != nil { 68 | defaultAppToken = defaultApp.Token 69 | } 70 | 71 | appToken := utils.ReadFlagOrDefault(context, "app-token", defaultAppToken) 72 | if appToken == "" { 73 | return utils.CliExit(errors.New(utils.SpecifyAppTokenFlag)) 74 | } 75 | 76 | if err := printUpdateToken(appToken, aliasKMSKey, vcli); err != nil { 77 | return utils.CliExit(err) 78 | } 79 | return nil 80 | }, 81 | } 82 | } 83 | 84 | func printUpdateToken(appToken string, keyAlias string, vcli *client.VirgilHTTPClient) (err error) { 85 | reqPayload, err := proto.Marshal(&decryptor.KeypairRequest{Alias: keyAlias}) 86 | if err != nil { 87 | return err 88 | } 89 | 90 | var resp []byte 91 | 92 | _, _, err = utils.SendProtoWithCheckRetry(vcli, http.MethodPost, PrefixKMSApi+"/create-update-token", reqPayload, &resp, appToken) 93 | if err != nil { 94 | return err 95 | } 96 | 97 | protoUpdateToken := &decryptor.UpdateTokenResponse{} 98 | if err := proto.Unmarshal(resp, protoUpdateToken); err != nil { 99 | return err 100 | } 101 | 102 | fmt.Println(base64.StdEncoding.EncodeToString(protoUpdateToken.UpdateToken)) 103 | 104 | return nil 105 | } 106 | -------------------------------------------------------------------------------- /main.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015-2019 Virgil Security Inc. 3 | * 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions are 8 | * met: 9 | * 10 | * (1) Redistributions of source code must retain the above copyright 11 | * notice, this list of conditions and the following disclaimer. 12 | * 13 | * (2) Redistributions in binary form must reproduce the above copyright 14 | * notice, this list of conditions and the following disclaimer in 15 | * the documentation and/or other materials provided with the 16 | * distribution. 17 | * 18 | * (3) Neither the name of the copyright holder nor the names of its 19 | * contributors may be used to endorse or promote products derived from 20 | * this software without specific prior written permission. 21 | * 22 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ''AS IS'' AND ANY EXPRESS OR 23 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 24 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 25 | * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, 26 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 27 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 28 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 30 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 31 | * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 32 | * POSSIBILITY OF SUCH DAMAGE. 33 | * 34 | * Lead Maintainer: Virgil Security Inc. 35 | */ 36 | 37 | package main 38 | 39 | import ( 40 | "fmt" 41 | "log" 42 | "os" 43 | "strings" 44 | 45 | "github.com/urfave/cli/v2" 46 | "github.com/urfave/cli/v2/altsrc" 47 | 48 | "github.com/VirgilSecurity/virgil-cli/client" 49 | "github.com/VirgilSecurity/virgil-cli/cmd" 50 | "github.com/VirgilSecurity/virgil-cli/utils" 51 | ) 52 | 53 | func main() { 54 | flags := []cli.Flag{ 55 | &cli.StringFlag{ 56 | Name: "config", 57 | Aliases: []string{"cfg"}, 58 | Usage: "Yaml config file path", 59 | }, 60 | altsrc.NewStringFlag(&cli.StringFlag{ 61 | Name: "api_gateway_url", 62 | Usage: "Api gateway URL", 63 | EnvVars: []string{"VIRGIL_API_URL"}, 64 | Hidden: true, 65 | }), 66 | } 67 | 68 | apiGatewayClient := &client.VirgilHTTPClient{ 69 | Address: "https://api.virgilsecurity.com/management/v1/", 70 | } 71 | 72 | kmsClient := &client.VirgilHTTPClient{ 73 | Address: "https://api.virgilsecurity.com/", 74 | } 75 | 76 | app := &cli.App{ 77 | Version: fmt.Sprintf("%v", utils.Version), 78 | Name: "CLI", 79 | Usage: "VirgilSecurity command line interface", 80 | Flags: flags, 81 | EnableBashCompletion: true, 82 | Commands: []*cli.Command{ 83 | cmd.Register(apiGatewayClient), 84 | cmd.Login(apiGatewayClient), 85 | cmd.Logout(), 86 | cmd.Application(apiGatewayClient), 87 | cmd.UseApp(apiGatewayClient), 88 | cmd.PureKit(), 89 | cmd.Keygen(), 90 | cmd.Key2Pub(), 91 | cmd.Encrypt(), 92 | cmd.Decrypt(), 93 | cmd.Sign(), 94 | cmd.Verify(), 95 | cmd.Cards(), 96 | cmd.Wave(apiGatewayClient), 97 | // cmd.KMS(kmsClient), 98 | }, 99 | Before: func(c *cli.Context) error { 100 | apiURL := c.String("api_gateway_url") 101 | if strings.TrimSpace(apiURL) != "" { 102 | apiGatewayClient.Address = apiURL 103 | kmsClient.Address = strings.TrimSuffix(apiURL, "management/v1/") 104 | } 105 | 106 | if _, err := os.Stat(c.String("config")); os.IsNotExist(err) { 107 | return nil 108 | } 109 | 110 | return altsrc.InitInputSourceWithContext(flags, altsrc.NewYamlSourceFromFlagFunc("config"))(c) 111 | }, 112 | } 113 | 114 | err := app.Run(os.Args) 115 | if err != nil { 116 | log.Fatal(err) 117 | } 118 | } 119 | -------------------------------------------------------------------------------- /cmd/kms/list.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015-2020 Virgil Security Inc. 3 | * 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions are 8 | * met: 9 | * 10 | * (1) Redistributions of source code must retain the above copyright 11 | * notice, this list of conditions and the following disclaimer. 12 | * 13 | * (2) Redistributions in binary form must reproduce the above copyright 14 | * notice, this list of conditions and the following disclaimer in 15 | * the documentation and/or other materials provided with the 16 | * distribution. 17 | * 18 | * (3) Neither the name of the copyright holder nor the names of its 19 | * contributors may be used to endorse or promote products derived from 20 | * this software without specific prior written permission. 21 | * 22 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ''AS IS'' AND ANY EXPRESS OR 23 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 24 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 25 | * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, 26 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 27 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 28 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 30 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 31 | * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 32 | * POSSIBILITY OF SUCH DAMAGE. 33 | * 34 | * Lead Maintainer: Virgil Security Inc. 35 | */ 36 | 37 | package kms 38 | 39 | import ( 40 | "fmt" 41 | "net/http" 42 | 43 | "github.com/golang/protobuf/proto" 44 | "github.com/pkg/errors" 45 | "github.com/urfave/cli/v2" 46 | 47 | "github.com/VirgilSecurity/virgil-cli/client" 48 | "github.com/VirgilSecurity/virgil-cli/cmd/kms/protobuf/decryptor" 49 | "github.com/VirgilSecurity/virgil-cli/utils" 50 | ) 51 | 52 | func List(vcli *client.VirgilHTTPClient) *cli.Command { 53 | return &cli.Command{ 54 | Name: "list", 55 | Aliases: []string{"l"}, 56 | Usage: "List your KMS keys", 57 | Flags: []cli.Flag{&cli.StringFlag{Name: "app-token", Usage: "application token"}}, 58 | Action: func(context *cli.Context) (err error) { 59 | defaultApp, _ := utils.LoadDefaultApp() 60 | defaultAppToken := "" 61 | if defaultApp != nil { 62 | defaultAppToken = defaultApp.Token 63 | } 64 | 65 | appToken := utils.ReadFlagOrDefault(context, "app-token", defaultAppToken) 66 | if appToken == "" { 67 | return utils.CliExit(errors.New(utils.SpecifyAppTokenFlag)) 68 | } 69 | 70 | keyPairs, err := listFunc(appToken, vcli) 71 | if err != nil { 72 | return utils.CliExit(err) 73 | } 74 | 75 | fmt.Printf("|%25s|%35s|%20s\n", "Keypair alias ", "Keypair version ", " Public Key ") 76 | fmt.Printf("|%25s|%35s|%20s\n", "-------------------------", 77 | "-----------------------------------", "---------------------------------------") 78 | for _, keyPair := range keyPairs { 79 | fmt.Printf( 80 | "| %23s | %33d | %19s\n", 81 | keyPair.Alias, 82 | int(keyPair.KeyVersion), 83 | recoveryKeyChecker(keyPair), 84 | ) 85 | } 86 | return nil 87 | }, 88 | } 89 | } 90 | 91 | func listFunc(appToken string, vcli *client.VirgilHTTPClient) (keyPairs []*decryptor.Keypair, err error) { 92 | var resp []byte 93 | _, _, err = utils.SendProtoWithCheckRetry(vcli, http.MethodPost, PrefixKMSApi+"/search-keypairs", nil, &resp, appToken) 94 | if err != nil { 95 | return nil, err 96 | } 97 | protoKeyPairs := &decryptor.Keypairs{} 98 | if err = proto.Unmarshal(resp, protoKeyPairs); err != nil { 99 | return nil, err 100 | } 101 | 102 | keyPairs = protoKeyPairs.Keypairs 103 | 104 | if keyPairs != nil { 105 | return keyPairs, nil 106 | } 107 | 108 | return nil, errors.New("empty response") 109 | } 110 | -------------------------------------------------------------------------------- /cmd/verify.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015-2020 Virgil Security Inc. 3 | * 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions are 8 | * met: 9 | * 10 | * (1) Redistributions of source code must retain the above copyright 11 | * notice, this list of conditions and the following disclaimer. 12 | * 13 | * (2) Redistributions in binary form must reproduce the above copyright 14 | * notice, this list of conditions and the following disclaimer in 15 | * the documentation and/or other materials provided with the 16 | * distribution. 17 | * 18 | * (3) Neither the name of the copyright holder nor the names of its 19 | * contributors may be used to endorse or promote products derived from 20 | * this software without specific prior written permission. 21 | * 22 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ''AS IS'' AND ANY EXPRESS OR 23 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 24 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 25 | * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, 26 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 27 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 28 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 30 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 31 | * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 32 | * POSSIBILITY OF SUCH DAMAGE. 33 | * 34 | * Lead Maintainer: Virgil Security Inc. 35 | */ 36 | package cmd 37 | 38 | import ( 39 | "encoding/base64" 40 | "errors" 41 | "fmt" 42 | "io/ioutil" 43 | 44 | "github.com/urfave/cli/v2" 45 | 46 | "github.com/VirgilSecurity/virgil-cli/utils" 47 | ) 48 | 49 | func Verify() *cli.Command { 50 | return &cli.Command{ 51 | Name: "verify", 52 | ArgsUsage: "[pub_key]", 53 | Usage: "Verify signature", 54 | Flags: []cli.Flag{ 55 | &cli.StringFlag{Name: "key", Usage: "public key file"}, 56 | &cli.StringFlag{Name: "i", Usage: "input file"}, 57 | &cli.StringFlag{Name: "s", Usage: "signature file"}, 58 | }, 59 | Action: func(context *cli.Context) error { 60 | keyFileName := utils.ReadFlagOrDefault(context, "key", "") 61 | if keyFileName == "" { 62 | return utils.CliExit(errors.New(utils.KeyFileNotSpecified)) 63 | } 64 | inputFileName := utils.ReadFlagOrDefault(context, "i", "") 65 | if inputFileName == "" { 66 | return utils.CliExit(errors.New(utils.InputFileNotSpecified)) 67 | } 68 | signatureFileName := utils.ReadFlagOrDefault(context, "s", "") 69 | if signatureFileName == "" { 70 | return utils.CliExit(errors.New(utils.SignatureFileNotSpecified)) 71 | } 72 | publicKeyString, err := utils.ReadKeyStringFromFile(context, keyFileName) 73 | if err != nil { 74 | return utils.CliExit(err) 75 | } 76 | 77 | data, err := ioutil.ReadFile(inputFileName) 78 | if err != nil { 79 | return utils.CliExit(err) 80 | } 81 | 82 | signature, err := ioutil.ReadFile(signatureFileName) 83 | if err != nil { 84 | return utils.CliExit(err) 85 | } 86 | 87 | err = VerifyFunc(publicKeyString, data, signature) 88 | 89 | if err != nil { 90 | return utils.CliExit(err) 91 | } 92 | 93 | fmt.Println(utils.VerifySuccess) 94 | return nil 95 | }, 96 | } 97 | } 98 | 99 | func VerifyFunc(publicKeyString string, data, signature []byte) (err error) { 100 | pk, err := crypt.ImportPublicKey([]byte(publicKeyString)) 101 | 102 | if err != nil { 103 | return errors.New(utils.CantImportPublicKey) 104 | } 105 | 106 | ss, err := base64.StdEncoding.DecodeString(string(signature)) 107 | 108 | if err != nil { 109 | return err 110 | } 111 | 112 | err = crypt.VerifySignature(data, ss, pk) 113 | if err != nil { 114 | return errors.New(utils.VerifyFailed) 115 | } 116 | 117 | return nil 118 | } 119 | -------------------------------------------------------------------------------- /cmd/register.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015-2019 Virgil Security Inc. 3 | * 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions are 8 | * met: 9 | * 10 | * (1) Redistributions of source code must retain the above copyright 11 | * notice, this list of conditions and the following disclaimer. 12 | * 13 | * (2) Redistributions in binary form must reproduce the above copyright 14 | * notice, this list of conditions and the following disclaimer in 15 | * the documentation and/or other materials provided with the 16 | * distribution. 17 | * 18 | * (3) Neither the name of the copyright holder nor the names of its 19 | * contributors may be used to endorse or promote products derived from 20 | * this software without specific prior written permission. 21 | * 22 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ''AS IS'' AND ANY EXPRESS OR 23 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 24 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 25 | * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, 26 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 27 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 28 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 30 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 31 | * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 32 | * POSSIBILITY OF SUCH DAMAGE. 33 | * 34 | * Lead Maintainer: Virgil Security Inc. 35 | */ 36 | 37 | package cmd 38 | 39 | import ( 40 | "crypto/subtle" 41 | "fmt" 42 | "net/http" 43 | "os" 44 | "strings" 45 | 46 | "github.com/howeyc/gopass" 47 | "github.com/pkg/errors" 48 | "github.com/urfave/cli/v2" 49 | 50 | "github.com/VirgilSecurity/virgil-cli/client" 51 | "github.com/VirgilSecurity/virgil-cli/models" 52 | "github.com/VirgilSecurity/virgil-cli/utils" 53 | ) 54 | 55 | func Register(client *client.VirgilHTTPClient) *cli.Command { 56 | return &cli.Command{ 57 | Name: "register", 58 | ArgsUsage: "email", 59 | Usage: "Register a new account", 60 | Flags: []cli.Flag{ 61 | &cli.StringFlag{Name: "username", Aliases: []string{"u"}, Usage: "user email"}, 62 | &cli.StringFlag{Name: "password", Aliases: []string{"p"}, Usage: "user password"}, 63 | }, 64 | Action: func(context *cli.Context) error { 65 | err := registerFunc(context, client) 66 | if err != nil { 67 | return utils.CliExit(err) 68 | } 69 | return err 70 | }, 71 | } 72 | } 73 | 74 | func registerFunc(context *cli.Context, vcli *client.VirgilHTTPClient) (err error) { 75 | email := utils.ReadFlagOrDefault(context, "username", "") 76 | pwd := utils.ReadFlagOrDefault(context, "password", "") 77 | 78 | _ = utils.DeleteAccessToken() 79 | _ = utils.DeleteAppFile() 80 | 81 | if email == "" { 82 | email = strings.TrimSpace(utils.ReadParamOrDefaultOrFromConsole(context, "email", utils.EmailPrompt, "")) 83 | } 84 | 85 | if pwd == "" { 86 | pwdBytes, err := gopass.GetPasswdPrompt(utils.PasswordPrompt+"\r\n", false, os.Stdin, os.Stdout) 87 | if err != nil { 88 | return err 89 | } 90 | pwdAgainBytes, err := gopass.GetPasswdPrompt(utils.PasswordConfirmPrompt+"\r\n", false, os.Stdin, os.Stdout) 91 | if err != nil { 92 | return err 93 | } 94 | 95 | if subtle.ConstantTimeCompare(pwdBytes, pwdAgainBytes) != 1 { 96 | err = errors.New(utils.PasswordsDoesntMatch) 97 | return err 98 | } 99 | pwd = string(pwdBytes) 100 | } 101 | 102 | req := &models.CreateAccountRequest{Email: email, Password: pwd} 103 | 104 | _, _, vErr := vcli.Send(http.MethodPost, "user/register", req, nil, nil) 105 | 106 | if vErr != nil { 107 | return vErr 108 | } 109 | err = utils.Login(email, pwd, vcli) 110 | 111 | if err != nil { 112 | return err 113 | } 114 | 115 | fmt.Println(utils.AccountSuccessfullyRegistered) 116 | 117 | return nil 118 | } 119 | -------------------------------------------------------------------------------- /cmd/decrypt.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015-2020 Virgil Security Inc. 3 | * 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions are 8 | * met: 9 | * 10 | * (1) Redistributions of source code must retain the above copyright 11 | * notice, this list of conditions and the following disclaimer. 12 | * 13 | * (2) Redistributions in binary form must reproduce the above copyright 14 | * notice, this list of conditions and the following disclaimer in 15 | * the documentation and/or other materials provided with the 16 | * distribution. 17 | * 18 | * (3) Neither the name of the copyright holder nor the names of its 19 | * contributors may be used to endorse or promote products derived from 20 | * this software without specific prior written permission. 21 | * 22 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ''AS IS'' AND ANY EXPRESS OR 23 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 24 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 25 | * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, 26 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 27 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 28 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 30 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 31 | * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 32 | * POSSIBILITY OF SUCH DAMAGE. 33 | * 34 | * Lead Maintainer: Virgil Security Inc. 35 | */ 36 | package cmd 37 | 38 | import ( 39 | "encoding/base64" 40 | "errors" 41 | "fmt" 42 | "io" 43 | "os" 44 | 45 | "github.com/urfave/cli/v2" 46 | 47 | "github.com/VirgilSecurity/virgil-cli/utils" 48 | ) 49 | 50 | func Decrypt() *cli.Command { 51 | return &cli.Command{ 52 | Name: "decrypt", 53 | ArgsUsage: "[inp]", 54 | Usage: "Decrypt data", 55 | Flags: []cli.Flag{ 56 | &cli.StringFlag{Name: "o", Usage: "destination file name"}, 57 | &cli.StringFlag{Name: "key", Usage: "private key file"}, 58 | &cli.StringFlag{Name: "i", Usage: "input file"}, 59 | }, 60 | Action: func(context *cli.Context) error { 61 | destinationFileName := utils.ReadFlagOrDefault(context, "o", "") 62 | keyFileName := utils.ReadFlagOrDefault(context, "key", "") 63 | if keyFileName == "" { 64 | return utils.CliExit(errors.New(utils.KeyFileNotSpecified)) 65 | } 66 | 67 | dataToDecrypt, err := utils.ReadFileFlagOrParamOrFromConsole(context, "i", "inp", utils.DecryptDataPrompt) 68 | if err != nil { 69 | return utils.CliExit(err) 70 | } 71 | 72 | privateKeyString, err := utils.ReadKeyStringFromFile(context, keyFileName) 73 | if err != nil { 74 | return utils.CliExit(err) 75 | } 76 | 77 | var writer io.Writer = os.Stdout 78 | if destinationFileName != "" { 79 | var file *os.File 80 | file, err = os.Create(destinationFileName) 81 | if err != nil { 82 | return utils.CliExit(err) 83 | } 84 | defer func() { 85 | if ferr := file.Close(); ferr != nil { 86 | panic(ferr) 87 | } 88 | }() 89 | 90 | writer = file 91 | } 92 | 93 | key, err := DecryptFunc(privateKeyString, dataToDecrypt) 94 | if err != nil { 95 | return utils.CliExit(err) 96 | } 97 | 98 | _, err = fmt.Fprint(writer, string(key)) 99 | if err != nil { 100 | return utils.CliExit(err) 101 | } 102 | fmt.Println() 103 | 104 | return nil 105 | }, 106 | } 107 | } 108 | 109 | func DecryptFunc(privateKeyString string, data []byte) (publicKey []byte, err error) { 110 | pk, err := crypt.ImportPrivateKey([]byte(privateKeyString)) 111 | 112 | if err != nil { 113 | return nil, errors.New(utils.CantImportPrivateKey) 114 | } 115 | 116 | dd, err := base64.StdEncoding.DecodeString(string(data)) 117 | 118 | if err != nil { 119 | return nil, err 120 | } 121 | 122 | return crypt.Decrypt(dd, pk) 123 | } 124 | -------------------------------------------------------------------------------- /cmd/app/delete.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015-2019 Virgil Security Inc. 3 | * 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions are 8 | * met: 9 | * 10 | * (1) Redistributions of source code must retain the above copyright 11 | * notice, this list of conditions and the following disclaimer. 12 | * 13 | * (2) Redistributions in binary form must reproduce the above copyright 14 | * notice, this list of conditions and the following disclaimer in 15 | * the documentation and/or other materials provided with the 16 | * distribution. 17 | * 18 | * (3) Neither the name of the copyright holder nor the names of its 19 | * contributors may be used to endorse or promote products derived from 20 | * this software without specific prior written permission. 21 | * 22 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ''AS IS'' AND ANY EXPRESS OR 23 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 24 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 25 | * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, 26 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 27 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 28 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 30 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 31 | * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 32 | * POSSIBILITY OF SUCH DAMAGE. 33 | * 34 | * Lead Maintainer: Virgil Security Inc. 35 | */ 36 | 37 | package app 38 | 39 | import ( 40 | "fmt" 41 | "net/http" 42 | 43 | "github.com/pkg/errors" 44 | "github.com/urfave/cli/v2" 45 | 46 | "github.com/VirgilSecurity/virgil-cli/client" 47 | "github.com/VirgilSecurity/virgil-cli/models" 48 | "github.com/VirgilSecurity/virgil-cli/utils" 49 | ) 50 | 51 | func Delete(vcli *client.VirgilHTTPClient) *cli.Command { 52 | return &cli.Command{ 53 | Name: "delete", 54 | Aliases: []string{"d"}, 55 | ArgsUsage: "app_id", 56 | Usage: "Delete app by id", 57 | Action: func(context *cli.Context) (err error) { 58 | defaultApp, _ := utils.LoadDefaultApp() 59 | defaultAppID := "" 60 | if defaultApp != nil { 61 | defaultAppID = defaultApp.ID 62 | } 63 | appID := utils.ReadParamOrDefaultOrFromConsole(context, "appID", utils.ApplicationIDPrompt, defaultAppID) 64 | 65 | app, err := getApp(appID, vcli) 66 | if err != nil { 67 | return utils.CliExit(err) 68 | } 69 | msg := fmt.Sprintf("%s %s (y/n) ?", utils.ApplicationDeletePrompt, app.Name) 70 | yesOrNo := utils.ReadConsoleValue("y or n", msg, "y", "n") 71 | if yesOrNo == "n" { 72 | return 73 | } 74 | err = deleteAppFunc(appID, vcli) 75 | if err == nil { 76 | fmt.Println(utils.ApplicationDeleteSuccess) 77 | } else if err == utils.ErrEntityNotFound { 78 | return utils.CliExit(errors.New(fmt.Sprintf("%s %s \n", utils.ApplicationNotFound, appID))) 79 | } 80 | 81 | if defaultAppID == appID { 82 | _ = utils.DeleteDefaultApp() 83 | } 84 | 85 | if err != nil { 86 | return utils.CliExit(err) 87 | } 88 | return err 89 | }, 90 | } 91 | } 92 | 93 | func deleteAppFunc(appID string, vcli *client.VirgilHTTPClient) (err error) { 94 | _, _, err = utils.SendWithCheckRetry(vcli, http.MethodDelete, "application/"+appID, nil, nil) 95 | return err 96 | } 97 | 98 | func getApp(appID string, vcli *client.VirgilHTTPClient) (app *models.Application, err error) { 99 | var apps []*models.Application 100 | _, _, err = utils.SendWithCheckRetry(vcli, http.MethodGet, "applications", nil, &apps) 101 | 102 | if err != nil { 103 | return 104 | } 105 | 106 | if len(apps) != 0 { 107 | for _, a := range apps { 108 | if a.ID == appID { 109 | return a, nil 110 | } 111 | } 112 | return nil, errors.New(fmt.Sprintf("%s %s \n", utils.ApplicationNotFound, appID)) 113 | } 114 | 115 | return nil, errors.New("empty response") 116 | } 117 | -------------------------------------------------------------------------------- /cmd/scms/dcm/dcm_create.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015-2020 Virgil Security Inc. 3 | * 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions are 8 | * met: 9 | * 10 | * (1) Redistributions of source code must retain the above copyright 11 | * notice, this list of conditions and the following disclaimer. 12 | * 13 | * (2) Redistributions in binary form must reproduce the above copyright 14 | * notice, this list of conditions and the following disclaimer in 15 | * the documentation and/or other materials provided with the 16 | * distribution. 17 | * 18 | * (3) Neither the name of the copyright holder nor the names of its 19 | * contributors may be used to endorse or promote products derived from 20 | * this software without specific prior written permission. 21 | * 22 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ''AS IS'' AND ANY EXPRESS OR 23 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 24 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 25 | * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, 26 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 27 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 28 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 30 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 31 | * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 32 | * POSSIBILITY OF SUCH DAMAGE. 33 | * 34 | * Lead Maintainer: Virgil Security Inc. 35 | */ 36 | package dcm 37 | 38 | import ( 39 | "encoding/json" 40 | "fmt" 41 | "net/http" 42 | 43 | "github.com/pkg/errors" 44 | "github.com/urfave/cli/v2" 45 | 46 | "github.com/VirgilSecurity/virgil-cli/client" 47 | "github.com/VirgilSecurity/virgil-cli/models" 48 | "github.com/VirgilSecurity/virgil-cli/utils" 49 | ) 50 | 51 | func Create(vcli *client.VirgilHTTPClient) *cli.Command { 52 | return &cli.Command{ 53 | Name: "create", 54 | Aliases: []string{"c"}, 55 | Usage: "Create new dcm certificate", 56 | Flags: []cli.Flag{ 57 | &cli.StringFlag{Name: "name", Usage: "dsm certificate name"}, 58 | &cli.StringFlag{Name: "encrypt-pub-key", Usage: "encrypt public key"}, 59 | &cli.StringFlag{Name: "app-token", Usage: "application token"}, 60 | &cli.StringFlag{Name: "verify-pub-key", Usage: "verify public key"}}, 61 | 62 | Action: func(context *cli.Context) (err error) { 63 | name := utils.ReadFlagOrConsoleValue(context, "name", utils.SCMSDCMCertificateNamePrompt) 64 | encryptPubKey := utils.ReadFlagOrConsoleValue(context, "encrypt-pub-key", utils.SCMSDCMPublicKeyPrompt) 65 | verifyPubKey := utils.ReadFlagOrConsoleValue(context, "verify-pub-key", utils.SCMSDCMPublicKeyVerifyPrompt) 66 | 67 | defaultApp, _ := utils.LoadDefaultApp() 68 | defaultAppToken := "" 69 | if defaultApp != nil { 70 | defaultAppToken = defaultApp.Token 71 | } 72 | 73 | appToken := utils.ReadFlagOrDefault(context, "app-token", defaultAppToken) 74 | if appToken == "" { 75 | return utils.CliExit(errors.New(utils.SpecifyAppTokenFlag)) 76 | } 77 | dcm, err := DsmCreateFunc(name, encryptPubKey, verifyPubKey, appToken, vcli) 78 | if err != nil { 79 | return utils.CliExit(err) 80 | } 81 | serialized, err := json.MarshalIndent(dcm, "", "\t") 82 | if err != nil { 83 | return utils.CliExit(err) 84 | } 85 | fmt.Println(string(serialized)) 86 | 87 | return 88 | }, 89 | } 90 | } 91 | 92 | func DsmCreateFunc( 93 | name string, 94 | encryptPubKey string, 95 | verifyPubKey string, 96 | appToken string, 97 | vcli *client.VirgilHTTPClient, 98 | ) (resp models.DcmCertificateCreateResponse, err error) { 99 | 100 | req := &models.DcmCertificateCreateRequest{ 101 | Name: name, 102 | EncryptPublicKey: encryptPubKey, 103 | VerifyPublicKey: verifyPubKey, 104 | } 105 | 106 | _, _, err = utils.SendWithCheckRetry(vcli, http.MethodPost, "/scms/dcm", req, &resp, appToken) 107 | return 108 | } 109 | -------------------------------------------------------------------------------- /cmd/cards/revoke.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015-2020 Virgil Security Inc. 3 | * 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions are 8 | * met: 9 | * 10 | * (1) Redistributions of source code must retain the above copyright 11 | * notice, this list of conditions and the following disclaimer. 12 | * 13 | * (2) Redistributions in binary form must reproduce the above copyright 14 | * notice, this list of conditions and the following disclaimer in 15 | * the documentation and/or other materials provided with the 16 | * distribution. 17 | * 18 | * (3) Neither the name of the copyright holder nor the names of its 19 | * contributors may be used to endorse or promote products derived from 20 | * this software without specific prior written permission. 21 | * 22 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ''AS IS'' AND ANY EXPRESS OR 23 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 24 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 25 | * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, 26 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 27 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 28 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 30 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 31 | * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 32 | * POSSIBILITY OF SUCH DAMAGE. 33 | * 34 | * Lead Maintainer: Virgil Security Inc. 35 | */ 36 | package cards 37 | 38 | import ( 39 | "fmt" 40 | "io/ioutil" 41 | "time" 42 | 43 | "github.com/VirgilSecurity/virgil-sdk-go/v6/crypto" 44 | "github.com/VirgilSecurity/virgil-sdk-go/v6/sdk" 45 | "github.com/VirgilSecurity/virgil-sdk-go/v6/session" 46 | "github.com/pkg/errors" 47 | "github.com/urfave/cli/v2" 48 | 49 | "github.com/VirgilSecurity/virgil-cli/utils" 50 | ) 51 | 52 | var crypt = &crypto.Crypto{} 53 | 54 | func Revoke() *cli.Command { 55 | return &cli.Command{ 56 | Name: "revoke", 57 | ArgsUsage: "[id]", 58 | Flags: []cli.Flag{ 59 | &cli.StringFlag{Name: "c", Usage: "config file name"}, 60 | &cli.StringFlag{Name: "i", Usage: "identity"}, 61 | }, 62 | Usage: "delete cards by id", 63 | Action: func(context *cli.Context) error { 64 | cardID := utils.ReadParamOrDefaultOrFromConsole(context, "id", utils.CardIDPrompt, "") 65 | 66 | configFileName := utils.ReadFlagOrDefault(context, "c", "") 67 | if configFileName == "" { 68 | return utils.CliExit(errors.New(utils.ConfigurationFileNotSpecified)) 69 | } 70 | 71 | data, err := ioutil.ReadFile(configFileName) 72 | if err != nil { 73 | fmt.Print(err) 74 | } 75 | 76 | conf, err := utils.ParseAppConfig(data) 77 | if err != nil { 78 | fmt.Print(err) 79 | } 80 | 81 | privateKey, err := crypt.ImportPrivateKey(conf.APPKey) 82 | if err != nil { 83 | return utils.CliExit(err) 84 | } 85 | 86 | identity := utils.ReadFlagOrConsoleValue(context, "i", utils.CardIdentityPrompt) 87 | 88 | generator := session.JwtGenerator{ 89 | AppKey: privateKey, 90 | AppKeyID: conf.APPKeyID, 91 | AppID: conf.AppID, 92 | AccessTokenSigner: &session.VirgilAccessTokenSigner{Crypto: crypt}, 93 | TTL: time.Minute, 94 | } 95 | 96 | cardManager := sdk.NewCardManager(session.NewGeneratorJwtProvider(generator, session.SetGeneratorJwtProviderDefaultIdentity(identity))) 97 | yesOrNo := utils.ReadConsoleValue("y or n", fmt.Sprintf("%s (y/n) ?", utils.CardDeletePrompt), "y", "n") 98 | if yesOrNo == "n" { 99 | return nil 100 | } 101 | 102 | err = cardManager.RevokeCard(cardID) 103 | 104 | if err == utils.ErrEntityNotFound { 105 | return utils.CliExit(errors.New(fmt.Sprintf("%s %s \n", utils.CardNotFound, cardID))) 106 | } 107 | if err != nil { 108 | return utils.CliExit(err) 109 | } 110 | fmt.Println(utils.CardDeleteSuccess) 111 | 112 | return nil 113 | }, 114 | } 115 | } 116 | -------------------------------------------------------------------------------- /cmd/encrypt.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015-2020 Virgil Security Inc. 3 | * 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions are 8 | * met: 9 | * 10 | * (1) Redistributions of source code must retain the above copyright 11 | * notice, this list of conditions and the following disclaimer. 12 | * 13 | * (2) Redistributions in binary form must reproduce the above copyright 14 | * notice, this list of conditions and the following disclaimer in 15 | * the documentation and/or other materials provided with the 16 | * distribution. 17 | * 18 | * (3) Neither the name of the copyright holder nor the names of its 19 | * contributors may be used to endorse or promote products derived from 20 | * this software without specific prior written permission. 21 | * 22 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ''AS IS'' AND ANY EXPRESS OR 23 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 24 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 25 | * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, 26 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 27 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 28 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 30 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 31 | * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 32 | * POSSIBILITY OF SUCH DAMAGE. 33 | * 34 | * Lead Maintainer: Virgil Security Inc. 35 | */ 36 | package cmd 37 | 38 | import ( 39 | "encoding/base64" 40 | "errors" 41 | "fmt" 42 | "io" 43 | "os" 44 | 45 | "github.com/VirgilSecurity/virgil-sdk-go/v6/crypto" 46 | "github.com/urfave/cli/v2" 47 | 48 | "github.com/VirgilSecurity/virgil-cli/utils" 49 | ) 50 | 51 | var crypt = &crypto.Crypto{} 52 | 53 | func Encrypt() *cli.Command { 54 | return &cli.Command{ 55 | Name: "encrypt", 56 | ArgsUsage: "[pub_key]", 57 | Usage: "Encrypt data", 58 | Flags: []cli.Flag{&cli.StringFlag{Name: "o", Usage: "destination file name"}, 59 | &cli.StringSliceFlag{Name: "key", Usage: "public key file"}, 60 | &cli.StringFlag{Name: "i", Usage: "input file"}, 61 | }, 62 | Action: func(context *cli.Context) error { 63 | dataToEncrypt, err := utils.ReadFileFlagOrParamOrFromConsole(context, "i", "inp", utils.EncryptDataPrompt) 64 | if err != nil { 65 | return utils.CliExit(err) 66 | } 67 | 68 | keyFileNames := context.StringSlice("key") 69 | if len(keyFileNames) == 0 { 70 | return utils.CliExit(errors.New(utils.KeyFileNotSpecified)) 71 | } 72 | 73 | pubKeyStrings := make([]string, len(keyFileNames)) 74 | for i, f := range keyFileNames { 75 | pubKeyStrings[i], err = utils.ReadKeyStringFromFile(context, f) 76 | if err != nil { 77 | return utils.CliExit(err) 78 | } 79 | } 80 | var writer io.Writer = os.Stdout 81 | if destinationFileName := utils.ReadFlagOrDefault(context, "o", ""); destinationFileName != "" { 82 | var file *os.File 83 | file, err = os.Create(destinationFileName) 84 | if err != nil { 85 | return utils.CliExit(err) 86 | } 87 | defer func() { 88 | if ferr := file.Close(); ferr != nil { 89 | panic(ferr) 90 | } 91 | }() 92 | 93 | writer = file 94 | } 95 | 96 | encData, err := EncryptFunc(dataToEncrypt, pubKeyStrings) 97 | 98 | if err != nil { 99 | return utils.CliExit(err) 100 | } 101 | 102 | _, err = fmt.Fprint(writer, base64.StdEncoding.EncodeToString(encData)) 103 | if err != nil { 104 | return utils.CliExit(err) 105 | } 106 | fmt.Println() 107 | 108 | return err 109 | }, 110 | } 111 | } 112 | 113 | func EncryptFunc(data []byte, publicKeysStrings []string) (publicKey []byte, err error) { 114 | pkk := make([]crypto.PublicKey, len(publicKeysStrings)) 115 | 116 | for i, s := range publicKeysStrings { 117 | pkk[i], err = crypt.ImportPublicKey([]byte(s)) 118 | if err != nil { 119 | return nil, errors.New(utils.CantImportPublicKey) 120 | } 121 | } 122 | 123 | return crypt.Encrypt(data, pkk...) 124 | } 125 | -------------------------------------------------------------------------------- /cmd/cards/search.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015-2020 Virgil Security Inc. 3 | * 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions are 8 | * met: 9 | * 10 | * (1) Redistributions of source code must retain the above copyright 11 | * notice, this list of conditions and the following disclaimer. 12 | * 13 | * (2) Redistributions in binary form must reproduce the above copyright 14 | * notice, this list of conditions and the following disclaimer in 15 | * the documentation and/or other materials provided with the 16 | * distribution. 17 | * 18 | * (3) Neither the name of the copyright holder nor the names of its 19 | * contributors may be used to endorse or promote products derived from 20 | * this software without specific prior written permission. 21 | * 22 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ''AS IS'' AND ANY EXPRESS OR 23 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 24 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 25 | * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, 26 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 27 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 28 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 30 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 31 | * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 32 | * POSSIBILITY OF SUCH DAMAGE. 33 | * 34 | * Lead Maintainer: Virgil Security Inc. 35 | */ 36 | package cards 37 | 38 | import ( 39 | "encoding/base64" 40 | "errors" 41 | "fmt" 42 | "io/ioutil" 43 | "time" 44 | 45 | "github.com/VirgilSecurity/virgil-sdk-go/v6/sdk" 46 | "github.com/VirgilSecurity/virgil-sdk-go/v6/session" 47 | "github.com/urfave/cli/v2" 48 | 49 | "github.com/VirgilSecurity/virgil-cli/utils" 50 | ) 51 | 52 | func Search() *cli.Command { 53 | return &cli.Command{ 54 | Name: "search", 55 | ArgsUsage: "[identity]", 56 | Flags: []cli.Flag{ 57 | &cli.StringFlag{Name: "c", Usage: "configuration file"}, 58 | }, 59 | Usage: "search cards by identity", 60 | Action: func(context *cli.Context) error { 61 | identity := utils.ReadParamOrDefaultOrFromConsole(context, "identity", utils.CardIdentityPrompt, "") 62 | 63 | configFileName := utils.ReadFlagOrDefault(context, "c", "") 64 | if configFileName == "" { 65 | return utils.CliExit(errors.New(utils.ConfigurationFileNotSpecified)) 66 | } 67 | 68 | data, err := ioutil.ReadFile(configFileName) 69 | if err != nil { 70 | return utils.CliExit(err) 71 | } 72 | 73 | conf, err := utils.ParseAppConfig(data) 74 | if err != nil { 75 | return utils.CliExit(err) 76 | } 77 | 78 | privateKey, err := crypt.ImportPrivateKey(conf.APPKey) 79 | if err != nil { 80 | return utils.CliExit(err) 81 | } 82 | 83 | generator := session.JwtGenerator{ 84 | AppKey: privateKey, 85 | AppKeyID: conf.APPKeyID, 86 | AppID: conf.AppID, 87 | AccessTokenSigner: &session.VirgilAccessTokenSigner{Crypto: crypt}, 88 | TTL: time.Minute, 89 | } 90 | 91 | cardManager := sdk.NewCardManager(session.NewGeneratorJwtProvider(generator, session.SetGeneratorJwtProviderDefaultIdentity(identity))) 92 | 93 | cards, err := cardManager.SearchCards(identity) 94 | if err != nil { 95 | return utils.CliExit(err) 96 | } 97 | 98 | if len(cards) == 0 { 99 | fmt.Println(utils.CardForIdentityNotFound + identity) 100 | return nil 101 | } 102 | 103 | fmt.Printf("|%64s |%63s |%20s\n", " Card ID ", "Public key ", " created_at ") 104 | fmt.Printf("|%64s|%64s|%20s\n", 105 | "-----------------------------------------------------------------", 106 | "----------------------------------------------------------------", 107 | "---------------------------------------", 108 | ) 109 | for _, c := range cards { 110 | pk, err := crypt.ExportPublicKey(c.PublicKey) 111 | if err != nil { 112 | return utils.CliExit(err) 113 | } 114 | fmt.Printf("|%63s |%63s |%20s\n", c.Id, base64.StdEncoding.EncodeToString(pk), c.CreatedAt) 115 | } 116 | 117 | return nil 118 | }, 119 | } 120 | } 121 | -------------------------------------------------------------------------------- /cmd/extract_pub_key.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015-2020 Virgil Security Inc. 3 | * 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions are 8 | * met: 9 | * 10 | * (1) Redistributions of source code must retain the above copyright 11 | * notice, this list of conditions and the following disclaimer. 12 | * 13 | * (2) Redistributions in binary form must reproduce the above copyright 14 | * notice, this list of conditions and the following disclaimer in 15 | * the documentation and/or other materials provided with the 16 | * distribution. 17 | * 18 | * (3) Neither the name of the copyright holder nor the names of its 19 | * contributors may be used to endorse or promote products derived from 20 | * this software without specific prior written permission. 21 | * 22 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ''AS IS'' AND ANY EXPRESS OR 23 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 24 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 25 | * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, 26 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 27 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 28 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 30 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 31 | * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 32 | * POSSIBILITY OF SUCH DAMAGE. 33 | * 34 | * Lead Maintainer: Virgil Security Inc. 35 | */ 36 | package cmd 37 | 38 | import ( 39 | "bufio" 40 | "encoding/base64" 41 | "fmt" 42 | "io" 43 | "os" 44 | "strings" 45 | 46 | "github.com/urfave/cli/v2" 47 | 48 | "github.com/VirgilSecurity/virgil-cli/utils" 49 | ) 50 | 51 | func Key2Pub() *cli.Command { 52 | return &cli.Command{ 53 | Name: "key2pub", 54 | ArgsUsage: "[prKey]", 55 | Usage: "Extract public key", 56 | Flags: []cli.Flag{&cli.StringFlag{Name: "o", Usage: "destination file name"}, 57 | &cli.StringFlag{Name: "i", Usage: "input file"}, 58 | }, 59 | Action: func(context *cli.Context) error { 60 | 61 | destinationFileName := utils.ReadFlagOrDefault(context, "o", "") 62 | inputFileName := utils.ReadFlagOrDefault(context, "i", "") 63 | 64 | privateKeyString := "" 65 | if inputFileName != "" { 66 | f, err := os.Open(inputFileName) 67 | if err != nil { 68 | return utils.CliExit(err) 69 | } 70 | defer func() { 71 | if err := f.Close(); err != nil { 72 | panic(err) 73 | } 74 | }() 75 | 76 | scanner := bufio.NewScanner(f) 77 | for scanner.Scan() { 78 | t := scanner.Text() 79 | if strings.Contains(t, "BEGIN ") { 80 | continue 81 | } 82 | privateKeyString = t 83 | break 84 | } 85 | } else { 86 | privateKeyString = utils.ReadParamOrDefaultOrFromConsole(context, "prKey", "private key", "") 87 | } 88 | 89 | var writer io.Writer 90 | if destinationFileName != "" { 91 | file, err := os.Create(destinationFileName) 92 | if err != nil { 93 | return utils.CliExit(err) 94 | } 95 | writer = file 96 | defer func() { 97 | if err := file.Close(); err != nil { 98 | panic(err) 99 | } 100 | }() 101 | } else { 102 | writer = os.Stdout 103 | } 104 | key, err := Key2PubFunc(privateKeyString) 105 | 106 | if err != nil { 107 | return utils.CliExit(err) 108 | } 109 | 110 | _, err = fmt.Fprintf(writer, "-----BEGIN PUBLIC KEY-----\n") 111 | if err != nil { 112 | return utils.CliExit(err) 113 | } 114 | _, err = fmt.Fprintln(writer, base64.StdEncoding.EncodeToString(key)) 115 | if err != nil { 116 | return utils.CliExit(err) 117 | } 118 | _, err = fmt.Fprintf(writer, "-----END PUBLIC KEY-----\n") 119 | 120 | if err != nil { 121 | return utils.CliExit(err) 122 | } 123 | 124 | return err 125 | }, 126 | } 127 | } 128 | 129 | func Key2PubFunc(privateKeyString string) (publicKey []byte, err error) { 130 | sk, err := crypt.ImportPrivateKey([]byte(privateKeyString)) 131 | 132 | if err != nil { 133 | return nil, fmt.Errorf(utils.ExtractPubKeyParseFailed) 134 | } 135 | 136 | return crypt.ExportPublicKey(sk.PublicKey()) 137 | } 138 | -------------------------------------------------------------------------------- /utils/parser.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015-2020 Virgil Security Inc. 3 | * 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions are 8 | * met: 9 | * 10 | * (1) Redistributions of source code must retain the above copyright 11 | * notice, this list of conditions and the following disclaimer. 12 | * 13 | * (2) Redistributions in binary form must reproduce the above copyright 14 | * notice, this list of conditions and the following disclaimer in 15 | * the documentation and/or other materials provided with the 16 | * distribution. 17 | * 18 | * (3) Neither the name of the copyright holder nor the names of its 19 | * contributors may be used to endorse or promote products derived from 20 | * this software without specific prior written permission. 21 | * 22 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ''AS IS'' AND ANY EXPRESS OR 23 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 24 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 25 | * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, 26 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 27 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 28 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 30 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 31 | * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 32 | * POSSIBILITY OF SUCH DAMAGE. 33 | * 34 | * Lead Maintainer: Virgil Security Inc. 35 | */ 36 | package utils 37 | 38 | import ( 39 | "encoding/base64" 40 | "strconv" 41 | "strings" 42 | 43 | "github.com/pkg/errors" 44 | ) 45 | 46 | // ParseVersionAndContent splits string into 3 parts: Prefix, version and decoded base64 content 47 | func ParseVersionAndContent(prefix, str string) (version uint32, content []byte, err error) { 48 | parts := strings.Split(str, ".") 49 | if len(parts) != 3 { 50 | return 0, nil, errors.New("invalid string: wrong number of blocks") 51 | } 52 | 53 | if parts[0] != prefix { 54 | return 0, nil, errors.New("invalid string: wrong prefix") 55 | } 56 | 57 | nVersion, err := strconv.Atoi(parts[1]) 58 | if err != nil { 59 | return 0, nil, errors.Wrap(err, "invalid string: malformed version part") 60 | } 61 | 62 | if nVersion < 1 { 63 | return 0, nil, errors.Wrap(err, "invalid version") 64 | } 65 | version = uint32(nVersion) 66 | 67 | content, err = base64.StdEncoding.DecodeString(parts[2]) 68 | if err != nil { 69 | return 0, nil, errors.Wrap(err, "invalid string: malformed data") 70 | } 71 | return 72 | } 73 | 74 | // ParseCombinedEntities splits string into 4 parts: Prefix, version and decoded base64 content Phe and Kms keys 75 | func ParseCombinedEntities(prefix, combinedEntity string) (version uint32, pheKeyContent, kmsKeyContent, authKeyContent []byte, err error) { 76 | parts := strings.Split(combinedEntity, ".") 77 | switch { 78 | case len(parts) != 5 && prefix != "PK": 79 | return 0, nil, nil, nil, errors.New("invalid string: wrong number of blocks") 80 | case len(parts) != 4 && prefix == "PK": 81 | return 0, nil, nil, nil, errors.New("invalid string: wrong number of blocks") 82 | } 83 | 84 | if parts[0] != prefix { 85 | return 0, nil, nil, nil, errors.New("invalid string: wrong prefix") 86 | } 87 | 88 | nVersion, err := strconv.Atoi(parts[1]) 89 | if err != nil { 90 | return 0, nil, nil, nil, errors.Wrap(err, "invalid string: malformed version part") 91 | } 92 | 93 | if nVersion < 1 { 94 | return 0, nil, nil, nil, errors.Wrap(err, "invalid version") 95 | } 96 | version = uint32(nVersion) 97 | 98 | pheKeyContent, err = base64.StdEncoding.DecodeString(parts[2]) 99 | if err != nil { 100 | return 0, nil, nil, nil, errors.Wrap(err, "invalid string: malformed first data part") 101 | } 102 | 103 | kmsKeyContent, err = base64.StdEncoding.DecodeString(parts[3]) 104 | if err != nil { 105 | return 0, nil, nil, nil, errors.Wrap(err, "invalid string: malformed second data part") 106 | } 107 | 108 | if prefix != "PK" { 109 | authKeyContent, err = base64.StdEncoding.DecodeString(parts[4]) 110 | if err != nil { 111 | return 0, nil, nil, nil, errors.Wrap(err, "invalid string: malformed third data part") 112 | } 113 | } 114 | return 115 | } 116 | -------------------------------------------------------------------------------- /cmd/app/key/list.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015-2019 Virgil Security Inc. 3 | * 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions are 8 | * met: 9 | * 10 | * (1) Redistributions of source code must retain the above copyright 11 | * notice, this list of conditions and the following disclaimer. 12 | * 13 | * (2) Redistributions in binary form must reproduce the above copyright 14 | * notice, this list of conditions and the following disclaimer in 15 | * the documentation and/or other materials provided with the 16 | * distribution. 17 | * 18 | * (3) Neither the name of the copyright holder nor the names of its 19 | * contributors may be used to endorse or promote products derived from 20 | * this software without specific prior written permission. 21 | * 22 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ''AS IS'' AND ANY EXPRESS OR 23 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 24 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 25 | * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, 26 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 27 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 28 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 30 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 31 | * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 32 | * POSSIBILITY OF SUCH DAMAGE. 33 | * 34 | * Lead Maintainer: Virgil Security Inc. 35 | */ 36 | 37 | package key 38 | 39 | import ( 40 | "encoding/base64" 41 | "fmt" 42 | "net/http" 43 | "sort" 44 | 45 | "github.com/pkg/errors" 46 | "github.com/urfave/cli/v2" 47 | 48 | "github.com/VirgilSecurity/virgil-cli/client" 49 | "github.com/VirgilSecurity/virgil-cli/models" 50 | "github.com/VirgilSecurity/virgil-cli/utils" 51 | ) 52 | 53 | func List(vcli *client.VirgilHTTPClient) *cli.Command { 54 | return &cli.Command{ 55 | Name: "list", 56 | Aliases: []string{"l"}, 57 | Usage: "List your App Keys", 58 | Flags: []cli.Flag{&cli.StringFlag{Name: "app_id", Aliases: []string{"app-id"}, Usage: "application id"}}, 59 | Action: func(context *cli.Context) (err error) { 60 | defaultApp, _ := utils.LoadDefaultApp() 61 | defaultAppID := "" 62 | if defaultApp != nil { 63 | defaultAppID = defaultApp.ID 64 | } 65 | 66 | appID := utils.ReadFlagOrDefault(context, "app_id", defaultAppID) 67 | if appID == "" { 68 | return utils.CliExit(errors.New(utils.SpecifyAppIDFlag)) 69 | } 70 | 71 | var keys []*models.AccessKey 72 | keys, err = listFunc(appID, vcli) 73 | 74 | if err != nil { 75 | return utils.CliExit(err) 76 | } 77 | 78 | if len(keys) == 0 { 79 | fmt.Println(utils.AppKeysNotCreatedYet) 80 | return nil 81 | } 82 | sort.Slice(keys, func(i, j int) bool { 83 | return keys[i].CreatedAt.Before(keys[j].CreatedAt) 84 | }) 85 | fmt.Printf("|%25s|%35s|%63s |%20s\n", "App key name ", "App Key ID ", " PublicKey ", " Created at ") 86 | fmt.Printf("|%25s|%35s|%64s|%20s\n", 87 | "-------------------------", 88 | "-----------------------------------", 89 | "----------------------------------------------------------------", 90 | "---------------------------------------", 91 | ) 92 | 93 | for _, k := range keys { 94 | fmt.Printf("| %23s | %33s | %62s | %20s\n", k.Name, k.ID, base64.StdEncoding.EncodeToString(k.PublicKey), k.CreatedAt) 95 | } 96 | return nil 97 | }, 98 | } 99 | } 100 | 101 | func listFunc(appID string, vcli *client.VirgilHTTPClient) (keys []*models.AccessKey, err error) { 102 | _, _, err = utils.SendWithCheckRetry(vcli, http.MethodGet, "application/"+appID+"/apikeys", nil, &keys) 103 | 104 | if err != nil { 105 | return 106 | } 107 | 108 | if keys != nil { 109 | return keys, nil 110 | } 111 | 112 | return nil, errors.New("empty response") 113 | } 114 | 115 | func getKey(appID string, keyID string, vcli *client.VirgilHTTPClient) (app *models.AccessKey, err error) { 116 | kk, err := listFunc(appID, vcli) 117 | if err != nil { 118 | return 119 | } 120 | 121 | if len(kk) != 0 { 122 | for _, k := range kk { 123 | if k.ID == keyID { 124 | return k, nil 125 | } 126 | } 127 | } 128 | return nil, errors.New(fmt.Sprintf("key with id %s not found", keyID)) 129 | } 130 | -------------------------------------------------------------------------------- /cmd/kms/create.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015-2020 Virgil Security Inc. 3 | * 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions are 8 | * met: 9 | * 10 | * (1) Redistributions of source code must retain the above copyright 11 | * notice, this list of conditions and the following disclaimer. 12 | * 13 | * (2) Redistributions in binary form must reproduce the above copyright 14 | * notice, this list of conditions and the following disclaimer in 15 | * the documentation and/or other materials provided with the 16 | * distribution. 17 | * 18 | * (3) Neither the name of the copyright holder nor the names of its 19 | * contributors may be used to endorse or promote products derived from 20 | * this software without specific prior written permission. 21 | * 22 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ''AS IS'' AND ANY EXPRESS OR 23 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 24 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 25 | * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, 26 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 27 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 28 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 30 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 31 | * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 32 | * POSSIBILITY OF SUCH DAMAGE. 33 | * 34 | * Lead Maintainer: Virgil Security Inc. 35 | */ 36 | package kms 37 | 38 | import ( 39 | "encoding/base64" 40 | "errors" 41 | "fmt" 42 | "net/http" 43 | 44 | "github.com/golang/protobuf/proto" 45 | "github.com/urfave/cli/v2" 46 | 47 | "github.com/VirgilSecurity/virgil-cli/client" 48 | "github.com/VirgilSecurity/virgil-cli/cmd/kms/protobuf/decryptor" 49 | "github.com/VirgilSecurity/virgil-cli/utils" 50 | ) 51 | 52 | const ( 53 | RecoveryPasswordAlias = "RECOVERY_PASSWORD" 54 | RecoveryPasswordKeyPrefix = "KP." 55 | PrefixKMSApi = "kms/v1" 56 | ) 57 | 58 | // Create KMS Public key 59 | func Create(vcli *client.VirgilHTTPClient) *cli.Command { 60 | return &cli.Command{ 61 | Name: "create", 62 | Aliases: []string{"c"}, 63 | ArgsUsage: "key_name", 64 | Usage: "Create a new key", 65 | Flags: []cli.Flag{&cli.StringFlag{Name: "app-token", Usage: "application token"}}, 66 | 67 | Action: func(context *cli.Context) (err error) { 68 | name := utils.ReadParamOrDefaultOrFromConsole(context, "name", utils.KMSKeyNamePrompt, "") 69 | 70 | defaultApp, _ := utils.LoadDefaultApp() 71 | defaultAppToken := "" 72 | if defaultApp != nil { 73 | defaultAppToken = defaultApp.Token 74 | } 75 | 76 | appToken := utils.ReadFlagOrDefault(context, "app-token", defaultAppToken) 77 | if appToken == "" { 78 | return utils.CliExit(errors.New(utils.SpecifyAppTokenFlag)) 79 | } 80 | 81 | keyPair, err := CreateFunc(name, appToken, vcli) 82 | 83 | if err != nil { 84 | return utils.CliExit(err) 85 | } 86 | 87 | fmt.Printf( 88 | "KMS Key alias: %s version: %d public key: %s\n", 89 | keyPair.Alias, 90 | int(keyPair.KeyVersion), 91 | recoveryKeyChecker(keyPair), 92 | ) 93 | fmt.Println(utils.KMSKeyCreateSuccess) 94 | return nil 95 | }, 96 | } 97 | } 98 | 99 | func CreateFunc(name, appToken string, vcli *client.VirgilHTTPClient) (keyPair *decryptor.Keypair, err error) { 100 | reqPayload, err := proto.Marshal(&decryptor.KeypairRequest{Alias: name}) 101 | if err != nil { 102 | return nil, err 103 | } 104 | var rawResp []byte 105 | _, _, err = utils.SendProtoWithCheckRetry(vcli, http.MethodPost, PrefixKMSApi+"/keypair", reqPayload, &rawResp, appToken) 106 | 107 | if err != nil { 108 | return nil, err 109 | } 110 | 111 | if len(rawResp) == 0 { 112 | return nil, errors.New("raw response lengths = 0") 113 | } 114 | 115 | keyPair = &decryptor.Keypair{} 116 | if err := proto.Unmarshal(rawResp, keyPair); err != nil { 117 | return nil, err 118 | } 119 | 120 | return keyPair, nil 121 | } 122 | 123 | func recoveryKeyChecker(keyPair *decryptor.Keypair) string { 124 | if keyPair.Alias == RecoveryPasswordAlias { 125 | return RecoveryPasswordKeyPrefix + base64.StdEncoding.EncodeToString(keyPair.PublicKey) 126 | } 127 | return base64.StdEncoding.EncodeToString(keyPair.PublicKey) 128 | } 129 | -------------------------------------------------------------------------------- /utils/login.go: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015-2019 Virgil Security Inc. 3 | * 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions are 8 | * met: 9 | * 10 | * (1) Redistributions of source code must retain the above copyright 11 | * notice, this list of conditions and the following disclaimer. 12 | * 13 | * (2) Redistributions in binary form must reproduce the above copyright 14 | * notice, this list of conditions and the following disclaimer in 15 | * the documentation and/or other materials provided with the 16 | * distribution. 17 | * 18 | * (3) Neither the name of the copyright holder nor the names of its 19 | * contributors may be used to endorse or promote products derived from 20 | * this software without specific prior written permission. 21 | * 22 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ''AS IS'' AND ANY EXPRESS OR 23 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 24 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 25 | * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, 26 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 27 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 28 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 30 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 31 | * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 32 | * POSSIBILITY OF SUCH DAMAGE. 33 | * 34 | * Lead Maintainer: Virgil Security Inc. 35 | */ 36 | 37 | package utils 38 | 39 | import ( 40 | "bufio" 41 | "fmt" 42 | "net/http" 43 | "os" 44 | "strings" 45 | 46 | "github.com/google/uuid" 47 | "github.com/howeyc/gopass" 48 | "github.com/pkg/errors" 49 | 50 | "github.com/VirgilSecurity/virgil-cli/client" 51 | "github.com/VirgilSecurity/virgil-cli/models" 52 | ) 53 | 54 | // Login obtains temporary account access token. Email and password may be empty 55 | func Login(email, password string, vcli *client.VirgilHTTPClient) error { 56 | if email == "" { 57 | fmt.Println(EmailPrompt) 58 | 59 | scanner := bufio.NewScanner(os.Stdin) 60 | scanner.Scan() 61 | 62 | email = strings.TrimSpace(scanner.Text()) 63 | } 64 | 65 | if password == "" { 66 | pwd, err := gopass.GetPasswdPrompt(PasswordPrompt+"\r\n", false, os.Stdin, os.Stdout) 67 | if err != nil { 68 | return err 69 | } 70 | password = string(pwd) 71 | } 72 | 73 | req := &models.LoginRequest{ 74 | Email: email, 75 | Password: password, 76 | } 77 | 78 | sessionToken := models.SessionToken{} 79 | 80 | for { 81 | _, _, vErr := vcli.Send(http.MethodPost, "user/login", req, &sessionToken, nil) 82 | if vErr == nil { 83 | break 84 | } 85 | _, err := CheckRetry(vErr, vcli) 86 | if err == ErrEmptyMFACode { 87 | var code []byte 88 | fmt.Printf("%s\n", TwoFactorCodeDescription) 89 | code, err = gopass.GetPasswdPrompt(TwoFactorCodePrompt+"\r\n", true, os.Stdin, os.Stdout) 90 | req.Verification = &models.Verification{MFACode: string(code)} 91 | } 92 | if err == ErrEmailIsNotConfirmed { 93 | fmt.Printf("%s\n", ConfirmationCodeDescription) 94 | code := ReadConsoleValue( 95 | "confirmation_code", 96 | ConfirmationCodePrompt, 97 | ) 98 | 99 | _, _, vErr = vcli.Send(http.MethodGet, "user/register/confirm/"+code, req, nil, nil) 100 | _, err = CheckRetry(vErr, vcli) 101 | } 102 | if err != nil { 103 | return err 104 | } 105 | } 106 | 107 | header := http.Header{} 108 | header.Set("SessionToken", sessionToken.Token) 109 | managementToken := models.ManagementTokenResponse{} 110 | _, _, vErr := vcli.Send(http.MethodPost, "management-token", 111 | models.ManagementTokenRequest{Name: uuid.New().String()}, 112 | &managementToken, header) 113 | if vErr != nil { 114 | return errors.New(fmt.Sprintf("Authorization failed.\n")) 115 | } 116 | 117 | _, _, vErr = vcli.Send(http.MethodPost, "user/logout", nil, nil, header) 118 | if vErr != nil { 119 | return errors.New(fmt.Sprintf("Authorization failed.\n")) 120 | } 121 | 122 | return SaveAccessToken(managementToken.Token) 123 | } 124 | 125 | func LoadAccessTokenOrLogin(vcli *client.VirgilHTTPClient) (token string, err error) { 126 | token, err = LoadAccessToken() 127 | if err != nil { 128 | err = Login("", "", vcli) 129 | if err != nil { 130 | return "", err 131 | } 132 | return LoadAccessToken() 133 | } 134 | return 135 | } 136 | --------------------------------------------------------------------------------