|
| 1 | +package aesrsa |
| 2 | + |
| 3 | +import ( |
| 4 | + "crypto/aes" |
| 5 | + "crypto/cipher" |
| 6 | + "crypto/rand" |
| 7 | + "crypto/rsa" |
| 8 | + "crypto/sha256" |
| 9 | + "crypto/x509" |
| 10 | + "encoding/pem" |
| 11 | + "fmt" |
| 12 | + |
| 13 | + "github.com/ARM-software/golang-utils/utils/commonerrors" |
| 14 | + "github.com/ARM-software/golang-utils/utils/filesystem" |
| 15 | +) |
| 16 | + |
| 17 | +// ParsePEMBlock will parse the first PEM block found within path |
| 18 | +func ParsePEMBlock(path string) (block *pem.Block, err error) { |
| 19 | + if path == "" { |
| 20 | + err = fmt.Errorf("%w: no certificate provided", commonerrors.ErrUndefined) |
| 21 | + return |
| 22 | + } |
| 23 | + |
| 24 | + if !filesystem.Exists(path) { |
| 25 | + err = fmt.Errorf("%w: could not find certificate at '%v'", commonerrors.ErrNotFound, path) |
| 26 | + return |
| 27 | + } |
| 28 | + |
| 29 | + certBytes, err := filesystem.ReadFile(path) |
| 30 | + if err != nil { |
| 31 | + err = fmt.Errorf("%w: failed to read certificate: %v", commonerrors.ErrUnexpected, err.Error()) |
| 32 | + return |
| 33 | + } |
| 34 | + |
| 35 | + block, _ = pem.Decode(certBytes) |
| 36 | + if block == nil { |
| 37 | + err = fmt.Errorf("%w: failed to decode PEM block from certificate", commonerrors.ErrUnexpected) |
| 38 | + return |
| 39 | + } |
| 40 | + |
| 41 | + return |
| 42 | +} |
| 43 | + |
| 44 | +// DecryptHybridAESRSAEncryptedPayloadFromPrivateKey takes a path to an RSA private key and uses it to decode the AES |
| 45 | +// key in a hybrid encoded payload. This AES key is then used to decode the actual payload contents. Information of |
| 46 | +// the use of hybrid AES RSA encryption can be found here https://www.ijrar.org/papers/IJRAR23B1852.pdf |
| 47 | +func DecryptHybridAESRSAEncryptedPayloadFromPrivateKey(privateKeyPath string, payload *HybridAESRSAEncryptedPayload) (decrypted []byte, err error) { |
| 48 | + block, err := ParsePEMBlock(privateKeyPath) |
| 49 | + if err != nil { |
| 50 | + err = fmt.Errorf("%w: could not parse PEM block from '%v': %v", commonerrors.ErrUnexpected, privateKeyPath, err.Error()) |
| 51 | + return |
| 52 | + } |
| 53 | + |
| 54 | + if block == nil { |
| 55 | + err = fmt.Errorf("%w: block was empty", commonerrors.ErrEmpty) |
| 56 | + return |
| 57 | + } |
| 58 | + |
| 59 | + return DecryptHybridAESRSAEncryptedPayloadFromBytes(block.Bytes, payload) |
| 60 | +} |
| 61 | + |
| 62 | +// DecryptHybridAESRSAEncryptedPayloadFromPrivateKeyPath takes a path to an RSA private key PEM file and uses it to |
| 63 | +// decode the AES key in a hybrid encoded payload. This AES key is then used to decode the actual payload contents. |
| 64 | +// Information of the use of hybrid AES RSA encryption can be found here https://www.ijrar.org/papers/IJRAR23B1852.pdf |
| 65 | +func DecryptHybridAESRSAEncryptedPayloadFromBytes(block []byte, payload *HybridAESRSAEncryptedPayload) (decrypted []byte, err error) { |
| 66 | + if payload == nil { |
| 67 | + err = fmt.Errorf("%w: payload must not be nil", commonerrors.ErrUndefined) |
| 68 | + return |
| 69 | + } |
| 70 | + |
| 71 | + priv, err := x509.ParsePKCS1PrivateKey(block) |
| 72 | + if err != nil { |
| 73 | + err = fmt.Errorf("%w: could not parse private key %v", commonerrors.ErrUnexpected, err.Error()) |
| 74 | + return |
| 75 | + } |
| 76 | + |
| 77 | + ciphertext, encryptedKey, nonce, err := DecodeHybridAESRSAEncryptedPayload(payload) |
| 78 | + if err != nil { |
| 79 | + err = fmt.Errorf("%w: could not decode payload: %v", commonerrors.ErrUnexpected, err.Error()) |
| 80 | + return |
| 81 | + } |
| 82 | + |
| 83 | + aesKey, err := rsa.DecryptOAEP(sha256.New(), rand.Reader, priv, encryptedKey, []byte{}) |
| 84 | + if err != nil { |
| 85 | + err = fmt.Errorf("%w: could not decrypt private key %v", commonerrors.ErrUnexpected, err.Error()) |
| 86 | + return |
| 87 | + } |
| 88 | + |
| 89 | + blockCipher, err := aes.NewCipher(aesKey) |
| 90 | + if err != nil { |
| 91 | + err = fmt.Errorf("%w: could not create new cipher %v", commonerrors.ErrUnexpected, err.Error()) |
| 92 | + return |
| 93 | + } |
| 94 | + |
| 95 | + aesGCM, err := cipher.NewGCM(blockCipher) |
| 96 | + if err != nil { |
| 97 | + err = fmt.Errorf("%w: could not create new GCM %v", commonerrors.ErrUnexpected, err.Error()) |
| 98 | + return |
| 99 | + } |
| 100 | + |
| 101 | + decrypted, err = aesGCM.Open(nil, nonce, ciphertext, nil) |
| 102 | + if err != nil { |
| 103 | + err = fmt.Errorf("%w: could not open ciphertext %v", commonerrors.ErrUnexpected, err.Error()) |
| 104 | + return |
| 105 | + } |
| 106 | + |
| 107 | + return |
| 108 | +} |
| 109 | + |
| 110 | +// EncryptHybridAESRSAEncryptedPayloadFromBytes takes an x509 certificate for key encypherment and uses it to |
| 111 | +// encode a payload using hybrid RSA AES encryption where an AES key is used to encrypt the content in payload and the |
| 112 | +// AES key is encrypted using RSA encryption. AES encryption is used to encode the payload itself as it is faster than |
| 113 | +// RSA for larger payloads. RSA is used to encrypt the relatively small AES key and allows asymmetric encryption |
| 114 | +// whilst also being fast. More information can be found at https://www.ijrar.org/papers/IJRAR23B1852.pdf |
| 115 | +func EncryptHybridAESRSAEncryptedPayloadFromBytes(block []byte, payload []byte) (encrypted *HybridAESRSAEncryptedPayload, err error) { |
| 116 | + cert, err := x509.ParseCertificate(block) |
| 117 | + if err != nil { |
| 118 | + err = fmt.Errorf("%w: failed parsing certificate: %v", commonerrors.ErrUnexpected, err.Error()) |
| 119 | + return |
| 120 | + } |
| 121 | + |
| 122 | + rsaPub, ok := cert.PublicKey.(*rsa.PublicKey) |
| 123 | + if !ok { |
| 124 | + err = fmt.Errorf("%w: public key in certificate is not RSA", commonerrors.ErrInvalid) |
| 125 | + return |
| 126 | + } |
| 127 | + |
| 128 | + aesKey := make([]byte, 32) |
| 129 | + _, err = rand.Read(aesKey) |
| 130 | + if err != nil { |
| 131 | + err = fmt.Errorf("%w: failed generating AES key: %v", commonerrors.ErrUnexpected, err.Error()) |
| 132 | + return |
| 133 | + } |
| 134 | + |
| 135 | + blockCipher, err := aes.NewCipher(aesKey) |
| 136 | + if err != nil { |
| 137 | + err = fmt.Errorf("%w: failed to create cipher: %v", commonerrors.ErrUnexpected, err.Error()) |
| 138 | + return |
| 139 | + } |
| 140 | + |
| 141 | + aesGCM, err := cipher.NewGCM(blockCipher) |
| 142 | + if err != nil { |
| 143 | + err = fmt.Errorf("%w: failed creating GCM: %v", commonerrors.ErrUnexpected, err.Error()) |
| 144 | + return |
| 145 | + } |
| 146 | + |
| 147 | + nonce := make([]byte, aesGCM.NonceSize()) |
| 148 | + _, err = rand.Read(nonce) |
| 149 | + if err != nil { |
| 150 | + err = fmt.Errorf("%w: failed to generate nonce: %v", commonerrors.ErrUnexpected, err.Error()) |
| 151 | + return |
| 152 | + } |
| 153 | + |
| 154 | + ciphertext := aesGCM.Seal(nil, nonce, payload, nil) |
| 155 | + encryptedAESKey, err := rsa.EncryptOAEP(sha256.New(), rand.Reader, rsaPub, aesKey, []byte{}) |
| 156 | + if err != nil { |
| 157 | + err = fmt.Errorf("%w: could not complete RSA encryption: %v", commonerrors.ErrUnexpected, err.Error()) |
| 158 | + return |
| 159 | + } |
| 160 | + |
| 161 | + encrypted = EncodeHybridAESRSAEncryptedPayload(ciphertext, encryptedAESKey, nonce) |
| 162 | + return |
| 163 | +} |
| 164 | + |
| 165 | +// EncryptHybridAESRSAEncryptedPayloadFromCertificate takes a path to a valid x509 certificate for key encypherment |
| 166 | +// and uses it to encode a payload using hybrid RSA AES encryption where an AES key is used to encrypt the content in |
| 167 | +// payload and the AES key is encrypted using RSA encryption. AES encryption is used to encode the payload itself as |
| 168 | +// it is faster than RSA for larger payloads. RSA is used to encrypt the relatively small AES key and allows asymmetric |
| 169 | +// encryption whilst also being fast. More information can be found at https://www.ijrar.org/papers/IJRAR23B1852.pdf |
| 170 | +func EncryptHybridAESRSAEncryptedPayloadFromCertificate(certPath string, payload []byte) (encrypted *HybridAESRSAEncryptedPayload, err error) { |
| 171 | + block, err := ParsePEMBlock(certPath) |
| 172 | + if err != nil { |
| 173 | + err = fmt.Errorf("%w: could not parse PEM block from '%v': %v", commonerrors.ErrUnexpected, certPath, err.Error()) |
| 174 | + return |
| 175 | + } |
| 176 | + |
| 177 | + if block == nil { |
| 178 | + err = fmt.Errorf("%w: block was empty", commonerrors.ErrEmpty) |
| 179 | + return |
| 180 | + } |
| 181 | + |
| 182 | + return EncryptHybridAESRSAEncryptedPayloadFromBytes(block.Bytes, payload) |
| 183 | +} |
0 commit comments