util

package
v0.0.0-...-9c6c125 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 18, 2017 License: Apache-2.0 Imports: 31 Imported by: 3

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func B64Decode

func B64Decode(str string) (buf []byte, err error)

B64Decode base64 decodes a string

func B64Encode

func B64Encode(buf []byte) string

B64Encode base64 encodes bytes

func ConvertJSONFileToJSONString

func ConvertJSONFileToJSONString(jsonFileLocation string) string

ConvertJSONFileToJSONString converts a file of json format to a json string

func CreateToken

func CreateToken(cert []byte, key []byte, body []byte) (string, error)

CreateToken creates a JWT-like token. In a normal JWT token, the format of the token created is:

<algorithm,claims,signature>

where each part is base64-encoded string separated by a period. In this JWT-like token, there are two differences:

  1. the claims section is a certificate, so the format is: <certificate,signature>
  2. the signature uses the private key associated with the certificate, and the signature is across both the certificate and the "body" argument, which is the body of an HTTP request, though could be any arbitrary bytes.

@param cert The pem-encoded certificate @param key The pem-encoded key @param body The body of an HTTP request

func DERCertToPEM

func DERCertToPEM(der []byte) []byte

DERCertToPEM converts DER to PEM format

func DecodeToken

func DecodeToken(token string) (*x509.Certificate, string, string, error)

DecodeToken extracts an X509 certificate and base64 encoded signature from a token

func ECDSASignDirect

func ECDSASignDirect(signKey interface{}, msg []byte) (*big.Int, *big.Int, error)

ECDSASignDirect signs the message msg and returns R,S using ECDSA

func FileExists

func FileExists(name string) bool

FileExists checks to see if a file exists

func GenECDSAToken

func GenECDSAToken(cert []byte, key []byte, body []byte) (string, error)

GenECDSAToken signs the http body and cert with ECDSA using EC private key

func GenNumber

func GenNumber(numlen *big.Int) *big.Int

GenNumber generates random numbers of type *big.Int with fixed length

func GenRSAToken

func GenRSAToken(cert []byte, key []byte, body []byte) (string, error)

GenRSAToken signs the http body and cert with RSA using RSA private key

func GenRootKey

func GenRootKey(csp bccsp.BCCSP) (bccsp.Key, error)

GenRootKey generates a new root key

func GetAttributes

func GetAttributes(jsonString string) map[string]string

GetAttributes get attributes from jsonString @jsonString : jsonString containing Attributes @ returns : map containing attribute name as Key and Attribute Value as value

func GetBCCSP

func GetBCCSP(cfg interface{}) (bccsp.BCCSP, error)

GetBCCSP returns the BCCSP instance

func GetDB

func GetDB(driver string, dbPath string) (*sqlx.DB, error)

GetDB returns a handle to an established driver-specific database connection

func GetDefaultHomeDir

func GetDefaultHomeDir() string

GetDefaultHomeDir returns the default cop home

func GetECPrivateKey

func GetECPrivateKey(raw []byte) (*ecdsa.PrivateKey, error)

GetECPrivateKey get *ecdsa.PrivateKey from key pem

func GetEnrollmentIDFromPEM

func GetEnrollmentIDFromPEM(cert []byte) (string, error)

GetEnrollmentIDFromPEM returns the EnrollmentID from a PEM buffer

func GetEnrollmentIDFromX509Certificate

func GetEnrollmentIDFromX509Certificate(cert *x509.Certificate) string

GetEnrollmentIDFromX509Certificate returns the EnrollmentID from the X509 certificate

func GetPrivateKey

func GetPrivateKey(privateKey string) (*rsa.PrivateKey, error)

GetPrivateKey converts a private key []byte to *rsa.PrivateKey object The Private Key has to be PEM encoded

func GetPublicKey

func GetPublicKey(publicKey string) (*rsa.PublicKey, error)

GetPublicKey converts publicKey.pem to *rsa.PublicKey

func GetRSAPrivateKey

func GetRSAPrivateKey(raw []byte) (*rsa.PrivateKey, error)

GetRSAPrivateKey get *rsa.PrivateKey from key pem

func GetX509CertificateFromPEM

func GetX509CertificateFromPEM(cert []byte) (*x509.Certificate, error)

GetX509CertificateFromPEM converts a PEM buffer to an X509 Certificate

func HTTPRequestToString

func HTTPRequestToString(req *http.Request) string

HTTPRequestToString returns a string for an HTTP request for debuggging

func HTTPResponseToString

func HTTPResponseToString(resp *http.Response) string

HTTPResponseToString returns a string for an HTTP response for debuggging

func MakeFileAbs

func MakeFileAbs(file, dir string) (string, error)

MakeFileAbs makes 'file' absolute relative to 'dir' if not already absolute

func Marshal

func Marshal(from interface{}, what string) ([]byte, error)

Marshal to bytes

func NewSelfSignedCert

func NewSelfSignedCert() ([]byte, interface{}, error)

NewSelfSignedCert returns new key ans cert Takes JSON string as input Returns JSON Strinng with Cert and json Signaure Signature String */

func RSASign

func RSASign(message []byte, shaAlgo string, rsaPrivateKey *rsa.PrivateKey) string

RSASign Signs Message as per RSA Algo returns RSA bigint String Signature ShaAlgo is hard coded right now to SHA384. Will implement dynamic algo**/

func RSASignJSON

func RSASignJSON(jsonString string, signatureJSON string, rsaPrivateKey *rsa.PrivateKey, cert string) string

RSASignJSON Signs JSon string jsonString : JSonString to be signed signatureJson : json string containing signature and ECert certificate : in based64 encoding returns JSON String with updated signature */

func RSAVerifySig

func RSAVerifySig(publicKey *rsa.PublicKey, hashAlgo string, signature string, message []byte) bool

RSAVerifySig Verifies RSA Signature return boolean

func RandomString

func RandomString(n int) string

RandomString returns a random string

func ReadFile

func ReadFile(file string) ([]byte, error)

ReadFile reads a file

func ReadJSONAsMapFile

func ReadJSONAsMapFile(configFileLocation string, stringLocator string) string

ReadJSONAsMapFile reads the keyvalue from file system

func ReadJSONAsMapString

func ReadJSONAsMapString(jsonString string, stringLocator string) string

ReadJSONAsMapString reads keyvalue from strings

func RemoveQuotes

func RemoveQuotes(str string) string

RemoveQuotes removes outer quotes from a string if necessary

func SignECMessage

func SignECMessage(JSONString string, signatureJSON string) string

SignECMessage generates a certificate and privKey and returns a signedJSON string containing the R and S value.

func StrContained

func StrContained(str string, strs []string) bool

StrContained returns true if 'str' is in 'strs'; otherwise return false

func Unmarshal

func Unmarshal(from []byte, to interface{}, what string) error

Unmarshal from bytes

func ValidateCert

func ValidateCert(cert *x509.Certificate) bool

ValidateCert checks for expiry in the certificate cert Does not check for revocation

func VerifyECMessage

func VerifyECMessage(JSONString string, signatureString string) bool

VerifyECMessage Verifies EC Message

func VerifyECMessageImpl

func VerifyECMessageImpl(JSONString string, signatureString string, pub *ecdsa.PublicKey) bool

VerifyECMessageImpl implements the Elliptic Curve signature verification

func VerifyMessage

func VerifyMessage(jsonString string, signatureString string) bool

VerifyMessage Gets Public Key from Certificate Certificate can be in PEM or DER Format It verifies both RSA and EC signatures**/

func VerifyRSAMessageImpl

func VerifyRSAMessageImpl(jsonString string, signatureString string, publicKey *rsa.PublicKey) bool

VerifyRSAMessageImpl implements the RSA signature verification

func VerifyToken

func VerifyToken(token string, body []byte) (*x509.Certificate, error)

VerifyToken verifies token signed by either ECDSA or RSA and returns the associated user ID

func WriteFile

func WriteFile(file string, buf []byte, perm os.FileMode) error

WriteFile writes a file

func WriteJSONAsMapToFile

func WriteJSONAsMapToFile(stringLocator string, value string, filePath string) error

WriteJSONAsMapToFile reads JSON String from File and Updates it with value

func WriteJSONAsMapToString

func WriteJSONAsMapToString(jsonString string, stringLocator string, value string) string

WriteJSONAsMapToString writes a json map to a json string

func WriteJSONToString

func WriteJSONToString(jsonString string, valueMap map[string]string) string

WriteJSONToString takes a map as input and returns json map

func WriteToJSON

func WriteToJSON(filePath string, cotentToAppend string)

WriteToJSON reads a file name from configfile and writes json file one at a time

Types

type CertificateSpec

type CertificateSpec struct {
	NotBefore time.Time
	NotAfter  time.Time

	State string

	Organization     string
	OrganizationUnit string
	// contains filtered or unexported fields
}

CertificateSpec defines structure for Certificate template certificateType 1: Self Signed , 2 : COP Server Signed , 3 : CSR

func (*CertificateSpec) GetCertificateType

func (spec *CertificateSpec) GetCertificateType() float64

GetCertificateType returns certificateType 1:Self Signed , 2:COP Signed , 3: CSR

func (*CertificateSpec) GetCommonName

func (spec *CertificateSpec) GetCommonName() string

GetCommonName returns the spec's Common Name field/value

func (*CertificateSpec) GetCountry

func (spec *CertificateSpec) GetCountry() string

GetCountry returns the spec's Country field/value

func (*CertificateSpec) GetExtensions

func (spec *CertificateSpec) GetExtensions() *[]pkix.Extension

GetExtensions returns the sepc's extensions

func (*CertificateSpec) GetLocality

func (spec *CertificateSpec) GetLocality() string

GetLocality returs subject's locality

func (*CertificateSpec) GetNotAfter

func (spec *CertificateSpec) GetNotAfter() time.Time

GetNotAfter returns the spec NotAfter (time.Time) field/value

func (*CertificateSpec) GetNotBefore

func (spec *CertificateSpec) GetNotBefore() time.Time

GetNotBefore returns the spec NotBefore (time.Time) field/value

func (*CertificateSpec) GetOrganization

func (spec *CertificateSpec) GetOrganization() string

GetOrganization returns the spec's Organization field/value

func (*CertificateSpec) GetOrganizationalUnit

func (spec *CertificateSpec) GetOrganizationalUnit() string

GetOrganizationalUnit returns subject's OrganizationalUNIT

func (*CertificateSpec) GetPublicKey

func (spec *CertificateSpec) GetPublicKey() interface{}

GetPublicKey returns the spec's Public Key field/value

func (*CertificateSpec) GetSerialNumber

func (spec *CertificateSpec) GetSerialNumber() *big.Int

GetSerialNumber returns the spec's Serial Number field/value

func (*CertificateSpec) GetSignatureAlgorithm

func (spec *CertificateSpec) GetSignatureAlgorithm() x509.SignatureAlgorithm

GetSignatureAlgorithm returns the X509.SignatureAlgorithm field/value

func (*CertificateSpec) GetState

func (spec *CertificateSpec) GetState() string

GetState returns subejct's state

func (*CertificateSpec) GetSubjectKeyID

func (spec *CertificateSpec) GetSubjectKeyID() *[]byte

GetSubjectKeyID returns the spec's subject KeyID

func (*CertificateSpec) GetUsage

func (spec *CertificateSpec) GetUsage() x509.KeyUsage

GetUsage returns the spec's usage (which is the x509.KeyUsage) field/value

type ECDSASignature

type ECDSASignature struct {
	R, S *big.Int
}

ECDSASignature forms the structure for R and S value for ECDSA

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL