| 1 | /*- |
| 2 | * Copyright 2014 Square Inc. |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package jose |
| 18 | |
| 19 | import ( |
| 20 | "crypto/ecdsa" |
| 21 | "crypto/rand" |
| 22 | "crypto/rsa" |
| 23 | "fmt" |
| 24 | ) |
| 25 | |
| 26 | // Dummy encrypter for use in examples |
| 27 | var encrypter, _ = NewEncrypter(DIRECT, A128GCM, []byte{}) |
| 28 | |
| 29 | func Example_jWE() { |
| 30 | // Generate a public/private key pair to use for this example. The library |
| 31 | // also provides two utility functions (LoadPublicKey and LoadPrivateKey) |
| 32 | // that can be used to load keys from PEM/DER-encoded data. |
| 33 | privateKey, err := rsa.GenerateKey(rand.Reader, 2048) |
| 34 | if err != nil { |
| 35 | panic(err) |
| 36 | } |
| 37 | |
| 38 | // Instantiate an encrypter using RSA-OAEP with AES128-GCM. An error would |
| 39 | // indicate that the selected algorithm(s) are not currently supported. |
| 40 | publicKey := &privateKey.PublicKey |
| 41 | encrypter, err := NewEncrypter(RSA_OAEP, A128GCM, publicKey) |
| 42 | if err != nil { |
| 43 | panic(err) |
| 44 | } |
| 45 | |
| 46 | // Encrypt a sample plaintext. Calling the encrypter returns an encrypted |
| 47 | // JWE object, which can then be serialized for output afterwards. An error |
| 48 | // would indicate a problem in an underlying cryptographic primitive. |
| 49 | var plaintext = []byte("Lorem ipsum dolor sit amet") |
| 50 | object, err := encrypter.Encrypt(plaintext) |
| 51 | if err != nil { |
| 52 | panic(err) |
| 53 | } |
| 54 | |
| 55 | // Serialize the encrypted object using the full serialization format. |
| 56 | // Alternatively you can also use the compact format here by calling |
| 57 | // object.CompactSerialize() instead. |
| 58 | serialized := object.FullSerialize() |
| 59 | |
| 60 | // Parse the serialized, encrypted JWE object. An error would indicate that |
| 61 | // the given input did not represent a valid message. |
| 62 | object, err = ParseEncrypted(serialized) |
| 63 | if err != nil { |
| 64 | panic(err) |
| 65 | } |
| 66 | |
| 67 | // Now we can decrypt and get back our original plaintext. An error here |
| 68 | // would indicate the the message failed to decrypt, e.g. because the auth |
| 69 | // tag was broken or the message was tampered with. |
| 70 | decrypted, err := object.Decrypt(privateKey) |
| 71 | if err != nil { |
| 72 | panic(err) |
| 73 | } |
| 74 | |
| 75 | fmt.Printf(string(decrypted)) |
| 76 | // output: Lorem ipsum dolor sit amet |
| 77 | } |
| 78 | |
| 79 | func Example_jWS() { |
| 80 | // Generate a public/private key pair to use for this example. The library |
| 81 | // also provides two utility functions (LoadPublicKey and LoadPrivateKey) |
| 82 | // that can be used to load keys from PEM/DER-encoded data. |
| 83 | privateKey, err := rsa.GenerateKey(rand.Reader, 2048) |
| 84 | if err != nil { |
| 85 | panic(err) |
| 86 | } |
| 87 | |
| 88 | // Instantiate a signer using RSASSA-PSS (SHA512) with the given private key. |
| 89 | signer, err := NewSigner(PS512, privateKey) |
| 90 | if err != nil { |
| 91 | panic(err) |
| 92 | } |
| 93 | |
| 94 | // Sign a sample payload. Calling the signer returns a protected JWS object, |
| 95 | // which can then be serialized for output afterwards. An error would |
| 96 | // indicate a problem in an underlying cryptographic primitive. |
| 97 | var payload = []byte("Lorem ipsum dolor sit amet") |
| 98 | object, err := signer.Sign(payload) |
| 99 | if err != nil { |
| 100 | panic(err) |
| 101 | } |
| 102 | |
| 103 | // Serialize the encrypted object using the full serialization format. |
| 104 | // Alternatively you can also use the compact format here by calling |
| 105 | // object.CompactSerialize() instead. |
| 106 | serialized := object.FullSerialize() |
| 107 | |
| 108 | // Parse the serialized, protected JWS object. An error would indicate that |
| 109 | // the given input did not represent a valid message. |
| 110 | object, err = ParseSigned(serialized) |
| 111 | if err != nil { |
| 112 | panic(err) |
| 113 | } |
| 114 | |
| 115 | // Now we can verify the signature on the payload. An error here would |
| 116 | // indicate the the message failed to verify, e.g. because the signature was |
| 117 | // broken or the message was tampered with. |
| 118 | output, err := object.Verify(&privateKey.PublicKey) |
| 119 | if err != nil { |
| 120 | panic(err) |
| 121 | } |
| 122 | |
| 123 | fmt.Printf(string(output)) |
| 124 | // output: Lorem ipsum dolor sit amet |
| 125 | } |
| 126 | |
| 127 | func ExampleNewEncrypter_publicKey() { |
| 128 | var publicKey *rsa.PublicKey |
| 129 | |
| 130 | // Instantiate an encrypter using RSA-OAEP with AES128-GCM. |
| 131 | NewEncrypter(RSA_OAEP, A128GCM, publicKey) |
| 132 | |
| 133 | // Instantiate an encrypter using RSA-PKCS1v1.5 with AES128-CBC+HMAC. |
| 134 | NewEncrypter(RSA1_5, A128CBC_HS256, publicKey) |
| 135 | } |
| 136 | |
| 137 | func ExampleNewEncrypter_symmetric() { |
| 138 | var sharedKey []byte |
| 139 | |
| 140 | // Instantiate an encrypter using AES128-GCM with AES-GCM key wrap. |
| 141 | NewEncrypter(A128GCMKW, A128GCM, sharedKey) |
| 142 | |
| 143 | // Instantiate an encrypter using AES256-GCM directly, w/o key wrapping. |
| 144 | NewEncrypter(DIRECT, A256GCM, sharedKey) |
| 145 | } |
| 146 | |
| 147 | func ExampleNewSigner_publicKey() { |
| 148 | var rsaPrivateKey *rsa.PrivateKey |
| 149 | var ecdsaPrivateKey *ecdsa.PrivateKey |
| 150 | |
| 151 | // Instantiate a signer using RSA-PKCS#1v1.5 with SHA-256. |
| 152 | NewSigner(RS256, rsaPrivateKey) |
| 153 | |
| 154 | // Instantiate a signer using ECDSA with SHA-384. |
| 155 | NewSigner(ES384, ecdsaPrivateKey) |
| 156 | } |
| 157 | |
| 158 | func ExampleNewSigner_symmetric() { |
| 159 | var sharedKey []byte |
| 160 | |
| 161 | // Instantiate an signer using HMAC-SHA256. |
| 162 | NewSigner(HS256, sharedKey) |
| 163 | |
| 164 | // Instantiate an signer using HMAC-SHA512. |
| 165 | NewSigner(HS512, sharedKey) |
| 166 | } |
| 167 | |
| 168 | func ExampleNewMultiEncrypter() { |
| 169 | var publicKey *rsa.PublicKey |
| 170 | var sharedKey []byte |
| 171 | |
| 172 | // Instantiate an encrypter using AES-GCM. |
| 173 | encrypter, err := NewMultiEncrypter(A128GCM) |
| 174 | if err != nil { |
| 175 | panic(err) |
| 176 | } |
| 177 | |
| 178 | // Add a recipient using a shared key with AES-GCM key wap |
| 179 | err = encrypter.AddRecipient(A128GCMKW, sharedKey) |
| 180 | if err != nil { |
| 181 | panic(err) |
| 182 | } |
| 183 | |
| 184 | // Add a recipient using an RSA public key with RSA-OAEP |
| 185 | err = encrypter.AddRecipient(RSA_OAEP, publicKey) |
| 186 | if err != nil { |
| 187 | panic(err) |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | func ExampleNewMultiSigner() { |
| 192 | var privateKey *rsa.PrivateKey |
| 193 | var sharedKey []byte |
| 194 | |
| 195 | // Instantiate a signer for multiple recipients. |
| 196 | signer := NewMultiSigner() |
| 197 | |
| 198 | // Add a recipient using a shared key with HMAC-SHA256 |
| 199 | err := signer.AddRecipient(HS256, sharedKey) |
| 200 | if err != nil { |
| 201 | panic(err) |
| 202 | } |
| 203 | |
| 204 | // Add a recipient using an RSA private key with RSASSA-PSS with SHA384 |
| 205 | err = signer.AddRecipient(PS384, privateKey) |
| 206 | if err != nil { |
| 207 | panic(err) |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | func ExampleEncrypter_encrypt() { |
| 212 | // Encrypt a plaintext in order to get an encrypted JWE object. |
| 213 | var plaintext = []byte("This is a secret message") |
| 214 | |
| 215 | encrypter.Encrypt(plaintext) |
| 216 | } |
| 217 | |
| 218 | func ExampleEncrypter_encryptWithAuthData() { |
| 219 | // Encrypt a plaintext in order to get an encrypted JWE object. Also attach |
| 220 | // some additional authenticated data (AAD) to the object. Note that objects |
| 221 | // with attached AAD can only be represented using full serialization. |
| 222 | var plaintext = []byte("This is a secret message") |
| 223 | var aad = []byte("This is authenticated, but public data") |
| 224 | |
| 225 | encrypter.EncryptWithAuthData(plaintext, aad) |
| 226 | } |