app: restructure and start building p15 output

This commit is contained in:
Greg T. Wallace 2024-01-25 20:16:37 -05:00
parent 6610c92058
commit e2e4f2037c
24 changed files with 622 additions and 168 deletions

View file

@ -0,0 +1,27 @@
package asn1obj
import (
"encoding/asn1"
"math/bits"
)
// BitString returns a BIT STRING of the content
func BitString(content []byte) []byte {
bs := asn1.BitString{
Bytes: content,
}
// drop trailing 0s by removing them from overall length
if len(content) > 0 {
trailing0s := bits.TrailingZeros8(content[len(content)-1])
bs.BitLength = 8*len(content) - trailing0s
}
// should never error
asn1result, err := asn1.Marshal(bs)
if err != nil {
panic(err)
}
return asn1result
}

View file

@ -0,0 +1,17 @@
package asn1obj
import (
"encoding/asn1"
"math/big"
)
// Integer returns an ASN.1 OBJECT IDENTIFIER with the oidValue bytes
func Integer(bigInt *big.Int) []byte {
// should never error
asn1result, err := asn1.Marshal(bigInt)
if err != nil {
panic(err)
}
return asn1result
}

27
pkg/tools/asn1obj/misc.go Normal file
View file

@ -0,0 +1,27 @@
package asn1obj
import "encoding/asn1"
// Explicit wraps another ASN.1 Object with the EXPLICIT wrapper using
// the tag number specified
func Explicit(explicitTagNumber int, wrappedElement []byte) []byte {
raw := asn1.RawValue{
Class: asn1.ClassContextSpecific,
Tag: explicitTagNumber,
IsCompound: true,
Bytes: wrappedElement,
}
// should never error
asn1result, err := asn1.Marshal(raw)
if err != nil {
panic(err)
}
return asn1result
}
// Null returns the NULL value
func Null() []byte {
return asn1.NullBytes
}

View file

@ -0,0 +1,23 @@
package asn1obj
import (
"encoding/asn1"
)
// OctetString returns an OCTET STRING of the content
func OctetString(content []byte) []byte {
raw := asn1.RawValue{
Class: asn1.ClassUniversal,
Tag: asn1.TagOctetString,
IsCompound: false,
Bytes: content,
}
// should never error
asn1result, err := asn1.Marshal(raw)
if err != nil {
panic(err)
}
return asn1result
}

19
pkg/tools/asn1obj/oid.go Normal file
View file

@ -0,0 +1,19 @@
package asn1obj
import "encoding/asn1"
var (
OIDPkscs15Content = asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 15, 3, 1} // pkcs15content (PKCS #15 content type)
OIDrsaEncryptionPKCS1 = asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 1, 1} // rsaEncryption (PKCS #1)
)
// ObjectIdentifier returns an ASN.1 OBJECT IDENTIFIER with the oidValue bytes
func ObjectIdentifier(oid asn1.ObjectIdentifier) []byte {
// should never error
asn1result, err := asn1.Marshal(oid)
if err != nil {
panic(err)
}
return asn1result
}

View file

@ -0,0 +1,26 @@
package asn1obj
import "encoding/asn1"
// Sequence returns an ASN.1 SEQUENCE with the specified content
func Sequence(content [][]byte) []byte {
val := []byte{}
for i := range content {
val = append(val, content[i]...)
}
raw := asn1.RawValue{
Class: asn1.ClassUniversal,
Tag: asn1.TagSequence,
IsCompound: true,
Bytes: val,
}
// should never error
asn1result, err := asn1.Marshal(raw)
if err != nil {
panic(err)
}
return asn1result
}

View file

@ -0,0 +1,16 @@
package asn1obj
import (
"encoding/asn1"
)
// UTF8String returns the specified string as a UTF8String
func UTF8String(s string) []byte {
// should never error
asn1result, err := asn1.MarshalWithParams(s, "utf8")
if err != nil {
panic(err)
}
return asn1result
}

32
pkg/tools/bitwise.go Normal file
View file

@ -0,0 +1,32 @@
package tools
// BitwiseComplimentOf returns the bitwise compliment of data
func BitwiseComplimentOf(data []byte) []byte {
compliment := []byte{}
for i := range data {
compliment = append(compliment, ^data[i])
}
return compliment
}
// IsBitwiseCompliment returns true if data1 and data2 are bitwise compliments,
// otherwise it returns false
func IsBitwiseCompliment(data1, data2 []byte) bool {
// if not same length, definitely not compliments
if len(data1) != len(data2) {
return false
}
// check each byte
for i := range data1 {
// if any byte is NOT the bitwise compliment of the matching byte in other data
// set, then the full set is not bitwise compliment and false
if data1[i] != ^data2[i] {
return false
}
}
return true
}