src/server/enterprise/server/api_server.go
func validateActivationCode(code string) (expiration time.Time, err error) {
	// Parse the public key.  If these steps fail, something is seriously
	// wrong and we should crash the service by panicking.
	block, _ := pem.Decode([]byte(publicKey))
	if block == nil {
		return time.Time{}, fmt.Errorf("failed to pem decode public key")
	}
	pub, err := x509.ParsePKIXPublicKey(block.Bytes)
	if err != nil {
		return time.Time{}, fmt.Errorf("failed to parse DER encoded public key: %s", err.Error())
	}
	rsaPub, ok := pub.(*rsa.PublicKey)
	if !ok {
		return time.Time{}, fmt.Errorf("public key isn't an RSA key")
	}

	// Decode the base64-encoded activation code
	decodedActivationCode, err := base64.StdEncoding.DecodeString(code)
	if err != nil {
		return time.Time{}, fmt.Errorf("activation code is not base64 encoded")
	}
	activationCode := &activationCode{}
	if err := json.Unmarshal(decodedActivationCode, &activationCode); err != nil {
		return time.Time{}, fmt.Errorf("activation code is not valid JSON")
	}

	// Decode the signature
	decodedSignature, err := base64.StdEncoding.DecodeString(activationCode.Signature)
	if err != nil {
		return time.Time{}, fmt.Errorf("signature is not base64 encoded")
	}

	// Compute the sha256 checksum of the token
	hashedToken := sha256.Sum256([]byte(activationCode.Token))

	// Verify that the signature is valid
	if err := rsa.VerifyPKCS1v15(rsaPub, crypto.SHA256, hashedToken[:], decodedSignature); err != nil {
		return time.Time{}, fmt.Errorf("invalid signature in activation code")
	}

	// Unmarshal the token
	token := token{}
	if err := json.Unmarshal([]byte(activationCode.Token), &token); err != nil {
		return time.Time{}, fmt.Errorf("token is not valid JSON")
	}

	// Parse the expiration. Note that this string is generated by Date.toJSON()
	// running in node, so Go's definition of RFC 3339 timestamps (which is
	// incomplete) must be compatible with the strings that node generates. So far
	// it seems to work.
	expiration, err = time.Parse(time.RFC3339, token.Expiry)
	if err != nil {
		return time.Time{}, fmt.Errorf("expiration is not valid ISO 8601 string")
	}
	// Check that the activation code has not expired
	if time.Now().After(expiration) {
		return time.Time{}, fmt.Errorf("the activation code has expired")
	}
	return expiration, nil
}
func validateActivationCode(code string) (expiration time.Time, err error) {
	// Parse the public key.  If these steps fail, something is seriously
	// wrong and we should crash the service by panicking.
	block, _ := pem.Decode([]byte(publicKey))
	if block == nil {
		return time.Time{}, fmt.Errorf("failed to pem decode public key")
	}
	pub, err := x509.ParsePKIXPublicKey(block.Bytes)
	if err != nil {
		return time.Time{}, fmt.Errorf("failed to parse DER encoded public key: %s", err.Error())
	}
	rsaPub, ok := pub.(*rsa.PublicKey)
	if !ok {
		return time.Time{}, fmt.Errorf("public key isn't an RSA key")
	}

	// Decode the base64-encoded activation code
	decodedActivationCode, err := base64.StdEncoding.DecodeString(code)
	if err != nil {
		return time.Time{}, fmt.Errorf("activation code is not base64 encoded")
	}
	activationCode := &activationCode{}
	if err := json.Unmarshal(decodedActivationCode, &activationCode); err != nil {
		return time.Time{}, fmt.Errorf("activation code is not valid JSON")
	}

	// Decode the signature
	decodedSignature, err := base64.StdEncoding.DecodeString(activationCode.Signature)
	if err != nil {
		return time.Time{}, fmt.Errorf("signature is not base64 encoded")
	}

	// Compute the sha256 checksum of the token
	hashedToken := sha256.Sum256([]byte(activationCode.Token))

	// Verify that the signature is valid
	if err := rsa.VerifyPKCS1v15(rsaPub, crypto.SHA256, hashedToken[:], decodedSignature); err != nil {
		return time.Time{}, fmt.Errorf("invalid signature in activation code")
	}

	// Unmarshal the token
	token := token{}
	if err := json.Unmarshal([]byte(activationCode.Token), &token); err != nil {
		return time.Time{}, fmt.Errorf("token is not valid JSON")
	}

	// Parse the expiration. Note that this string is generated by Date.toJSON()
	// running in node, so Go's definition of RFC 3339 timestamps (which is
	// incomplete) must be compatible with the strings that node generates. So far
	// it seems to work.
	expiration, err = time.Parse(time.RFC3339, token.Expiry)
	if err != nil {
		return time.Time{}, fmt.Errorf("expiration is not valid ISO 8601 string")
	}
	// Check that the activation code has not expired
	if time.Now().After(expiration) {
		return time.Time{}, fmt.Errorf("the activation code has expired")
	}
	return expiration, nil
}
func validateActivationCode ( code string ) ( expiration time . Time , err error ) { // Parse the public key.  If these steps fail, something is seriously // wrong and we should crash the service by panicking. block , _ := pem . Decode ( [ ] byte ( publicKey ) ) if block == nil { return time . Time { } , fmt . Errorf ( " " ) } pub , err := x509 . ParsePKIXPublicKey ( block . Bytes ) if err != nil { return time . Time { } , fmt . Errorf ( " " , err . Error ( ) ) } rsaPub , ok := pub . ( * rsa . PublicKey ) if ! ok { return time . Time { } , fmt . Errorf ( " " ) } // Decode the base64-encoded activation code decodedActivationCode , err := base64 . StdEncoding . DecodeString ( code ) if err != nil { return time . Time { } , fmt . Errorf ( " " ) } activationCode := & activationCode { } if err := json . Unmarshal ( decodedActivationCode , & activationCode ) ; err != nil { return time . Time { } , fmt . Errorf ( " " ) } // Decode the signature decodedSignature , err := base64 . StdEncoding . DecodeString ( activationCode . Signature ) if err != nil { return time . Time { } , fmt . Errorf ( " " ) } // Compute the sha256 checksum of the token hashedToken := sha256 . Sum256 ( [ ] byte ( activationCode . Token ) ) // Verify that the signature is valid if err := rsa . VerifyPKCS1v15 ( rsaPub , crypto . SHA256 , hashedToken [ : ] , decodedSignature ) ; err != nil { return time . Time { } , fmt . Errorf ( " " ) } // Unmarshal the token token := token { } if err := json . Unmarshal ( [ ] byte ( activationCode . Token ) , & token ) ; err != nil { return time . Time { } , fmt . Errorf ( " " ) } // Parse the expiration. Note that this string is generated by Date.toJSON() // running in node, so Go's definition of RFC 3339 timestamps (which is // incomplete) must be compatible with the strings that node generates. So far // it seems to work. expiration , err = time . Parse ( time . RFC3339 , token . Expiry ) if err != nil { return time . Time { } , fmt . Errorf ( " " ) } // Check that the activation code has not expired if time . Now ( ) . After ( expiration ) { return time . Time { } , fmt . Errorf ( " " ) } return expiration , nil }
// validateActivationCode checks the validity of an activation code
validateActivationCode checks the validity of an activation code