~juju-qa/ubuntu/xenial/juju/2.0-rc2

« back to all changes in this revision

Viewing changes to src/golang.org/x/crypto/openpgp/packet/private_key.go

  • Committer: Nicholas Skaggs
  • Date: 2016-09-30 14:39:30 UTC
  • mfrom: (1.8.1)
  • Revision ID: nicholas.skaggs@canonical.com-20160930143930-vwwhrefh6ftckccy
import upstream

Show diffs side-by-side

added added

removed removed

Lines of Context:
8
8
        "bytes"
9
9
        "crypto/cipher"
10
10
        "crypto/dsa"
 
11
        "crypto/ecdsa"
11
12
        "crypto/rsa"
12
13
        "crypto/sha1"
13
 
        "golang.org/x/crypto/openpgp/elgamal"
14
 
        "golang.org/x/crypto/openpgp/errors"
15
 
        "golang.org/x/crypto/openpgp/s2k"
16
14
        "io"
17
15
        "io/ioutil"
18
16
        "math/big"
19
17
        "strconv"
20
18
        "time"
 
19
 
 
20
        "golang.org/x/crypto/openpgp/elgamal"
 
21
        "golang.org/x/crypto/openpgp/errors"
 
22
        "golang.org/x/crypto/openpgp/s2k"
21
23
)
22
24
 
23
25
// PrivateKey represents a possibly encrypted private key. See RFC 4880,
47
49
        return pk
48
50
}
49
51
 
 
52
func NewElGamalPrivateKey(currentTime time.Time, priv *elgamal.PrivateKey) *PrivateKey {
 
53
        pk := new(PrivateKey)
 
54
        pk.PublicKey = *NewElGamalPublicKey(currentTime, &priv.PublicKey)
 
55
        pk.PrivateKey = priv
 
56
        return pk
 
57
}
 
58
 
 
59
func NewECDSAPrivateKey(currentTime time.Time, priv *ecdsa.PrivateKey) *PrivateKey {
 
60
        pk := new(PrivateKey)
 
61
        pk.PublicKey = *NewECDSAPublicKey(currentTime, &priv.PublicKey)
 
62
        pk.PrivateKey = priv
 
63
        return pk
 
64
}
 
65
 
50
66
func (pk *PrivateKey) parse(r io.Reader) (err error) {
51
67
        err = (&pk.PublicKey).parse(r)
52
68
        if err != nil {
130
146
                err = serializeRSAPrivateKey(privateKeyBuf, priv)
131
147
        case *dsa.PrivateKey:
132
148
                err = serializeDSAPrivateKey(privateKeyBuf, priv)
 
149
        case *elgamal.PrivateKey:
 
150
                err = serializeElGamalPrivateKey(privateKeyBuf, priv)
 
151
        case *ecdsa.PrivateKey:
 
152
                err = serializeECDSAPrivateKey(privateKeyBuf, priv)
133
153
        default:
134
154
                err = errors.InvalidArgumentError("unknown private key type")
135
155
        }
185
205
        return writeBig(w, priv.X)
186
206
}
187
207
 
 
208
func serializeElGamalPrivateKey(w io.Writer, priv *elgamal.PrivateKey) error {
 
209
        return writeBig(w, priv.X)
 
210
}
 
211
 
 
212
func serializeECDSAPrivateKey(w io.Writer, priv *ecdsa.PrivateKey) error {
 
213
        return writeBig(w, priv.D)
 
214
}
 
215
 
188
216
// Decrypt decrypts an encrypted private key using a passphrase.
189
217
func (pk *PrivateKey) Decrypt(passphrase []byte) error {
190
218
        if !pk.Encrypted {
236
264
                return pk.parseDSAPrivateKey(data)
237
265
        case PubKeyAlgoElGamal:
238
266
                return pk.parseElGamalPrivateKey(data)
 
267
        case PubKeyAlgoECDSA:
 
268
                return pk.parseECDSAPrivateKey(data)
239
269
        }
240
270
        panic("impossible")
241
271
}
311
341
 
312
342
        return nil
313
343
}
 
344
 
 
345
func (pk *PrivateKey) parseECDSAPrivateKey(data []byte) (err error) {
 
346
        ecdsaPub := pk.PublicKey.PublicKey.(*ecdsa.PublicKey)
 
347
 
 
348
        buf := bytes.NewBuffer(data)
 
349
        d, _, err := readMPI(buf)
 
350
        if err != nil {
 
351
                return
 
352
        }
 
353
 
 
354
        pk.PrivateKey = &ecdsa.PrivateKey{
 
355
                PublicKey: *ecdsaPub,
 
356
                D:         new(big.Int).SetBytes(d),
 
357
        }
 
358
        pk.Encrypted = false
 
359
        pk.encryptedData = nil
 
360
 
 
361
        return nil
 
362
}