| 1 |
// Copyright 2011 The Go Authors. All rights reserved. |
| 2 |
// Use of this source code is governed by a BSD-style |
| 3 |
// license that can be found in the LICENSE file. |
| 4 |
|
| 5 |
// Package packet implements parsing and serialization of OpenPGP packets, as |
| 6 |
// specified in RFC 4880. |
| 7 |
package packet // import "github.com/ProtonMail/go-crypto/openpgp/packet" |
| 8 |
|
| 9 |
import ( |
| 10 |
"bytes" |
| 11 |
"crypto/cipher" |
| 12 |
"crypto/rsa" |
| 13 |
"io" |
| 14 |
|
| 15 |
"github.com/ProtonMail/go-crypto/openpgp/errors" |
| 16 |
"github.com/ProtonMail/go-crypto/openpgp/internal/algorithm" |
| 17 |
) |
| 18 |
|
| 19 |
// readFull is the same as io.ReadFull except that reading zero bytes returns |
| 20 |
// ErrUnexpectedEOF rather than EOF. |
| 21 |
func readFull(r io.Reader, buf []byte) (n int, err error) { |
| 22 |
n, err = io.ReadFull(r, buf) |
| 23 |
if err == io.EOF { |
| 24 |
err = io.ErrUnexpectedEOF |
| 25 |
} |
| 26 |
return |
| 27 |
} |
| 28 |
|
| 29 |
// readN reads exactly n bytes from r. |
| 30 |
func readN(r io.Reader, n uint32) ([]byte, error) { |
| 31 |
var buf bytes.Buffer |
| 32 |
if _, err := io.CopyN(&buf, r, int64(n)); err != nil { |
| 33 |
if err == io.EOF { |
| 34 |
err = io.ErrUnexpectedEOF |
| 35 |
} |
| 36 |
return nil, err |
| 37 |
} |
| 38 |
return buf.Bytes(), nil |
| 39 |
} |
| 40 |
|
| 41 |
// readLength reads an OpenPGP length from r. See RFC 4880, section 4.2.2. |
| 42 |
func readLength(r io.Reader) (length int64, isPartial bool, err error) { |
| 43 |
var buf [4]byte |
| 44 |
_, err = readFull(r, buf[:1]) |
| 45 |
if err != nil { |
| 46 |
return |
| 47 |
} |
| 48 |
switch { |
| 49 |
case buf[0] < 192: |
| 50 |
length = int64(buf[0]) |
| 51 |
case buf[0] < 224: |
| 52 |
length = int64(buf[0]-192) << 8 |
| 53 |
_, err = readFull(r, buf[0:1]) |
| 54 |
if err != nil { |
| 55 |
return |
| 56 |
} |
| 57 |
length += int64(buf[0]) + 192 |
| 58 |
case buf[0] < 255: |
| 59 |
length = int64(1) << (buf[0] & 0x1f) |
| 60 |
isPartial = true |
| 61 |
default: |
| 62 |
_, err = readFull(r, buf[0:4]) |
| 63 |
if err != nil { |
| 64 |
return |
| 65 |
} |
| 66 |
length = int64(buf[0])<<24 | |
| 67 |
int64(buf[1])<<16 | |
| 68 |
int64(buf[2])<<8 | |
| 69 |
int64(buf[3]) |
| 70 |
} |
| 71 |
return |
| 72 |
} |
| 73 |
|
| 74 |
// partialLengthReader wraps an io.Reader and handles OpenPGP partial lengths. |
| 75 |
// The continuation lengths are parsed and removed from the stream and EOF is |
| 76 |
// returned at the end of the packet. See RFC 4880, section 4.2.2.4. |
| 77 |
type partialLengthReader struct { |
| 78 |
r io.Reader |
| 79 |
remaining int64 |
| 80 |
isPartial bool |
| 81 |
} |
| 82 |
|
| 83 |
func (r *partialLengthReader) Read(p []byte) (n int, err error) { |
| 84 |
for r.remaining == 0 { |
| 85 |
if !r.isPartial { |
| 86 |
return 0, io.EOF |
| 87 |
} |
| 88 |
r.remaining, r.isPartial, err = readLength(r.r) |
| 89 |
if err != nil { |
| 90 |
return 0, err |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
toRead := int64(len(p)) |
| 95 |
if toRead > r.remaining { |
| 96 |
toRead = r.remaining |
| 97 |
} |
| 98 |
|
| 99 |
n, err = r.r.Read(p[:int(toRead)]) |
| 100 |
r.remaining -= int64(n) |
| 101 |
if n < int(toRead) && err == io.EOF { |
| 102 |
err = io.ErrUnexpectedEOF |
| 103 |
} |
| 104 |
return |
| 105 |
} |
| 106 |
|
| 107 |
// partialLengthWriter writes a stream of data using OpenPGP partial lengths. |
| 108 |
// See RFC 4880, section 4.2.2.4. |
| 109 |
type partialLengthWriter struct { |
| 110 |
w io.WriteCloser |
| 111 |
buf bytes.Buffer |
| 112 |
lengthByte [1]byte |
| 113 |
} |
| 114 |
|
| 115 |
func (w *partialLengthWriter) Write(p []byte) (n int, err error) { |
| 116 |
bufLen := w.buf.Len() |
| 117 |
if bufLen > 512 { |
| 118 |
for power := uint(30); ; power-- { |
| 119 |
l := 1 << power |
| 120 |
if bufLen >= l { |
| 121 |
w.lengthByte[0] = 224 + uint8(power) |
| 122 |
_, err = w.w.Write(w.lengthByte[:]) |
| 123 |
if err != nil { |
| 124 |
return |
| 125 |
} |
| 126 |
var m int |
| 127 |
m, err = w.w.Write(w.buf.Next(l)) |
| 128 |
if err != nil { |
| 129 |
return |
| 130 |
} |
| 131 |
if m != l { |
| 132 |
return 0, io.ErrShortWrite |
| 133 |
} |
| 134 |
break |
| 135 |
} |
| 136 |
} |
| 137 |
} |
| 138 |
return w.buf.Write(p) |
| 139 |
} |
| 140 |
|
| 141 |
func (w *partialLengthWriter) Close() (err error) { |
| 142 |
len := w.buf.Len() |
| 143 |
err = serializeLength(w.w, len) |
| 144 |
if err != nil { |
| 145 |
return err |
| 146 |
} |
| 147 |
_, err = w.buf.WriteTo(w.w) |
| 148 |
if err != nil { |
| 149 |
return err |
| 150 |
} |
| 151 |
return w.w.Close() |
| 152 |
} |
| 153 |
|
| 154 |
// A spanReader is an io.LimitReader, but it returns ErrUnexpectedEOF if the |
| 155 |
// underlying Reader returns EOF before the limit has been reached. |
| 156 |
type spanReader struct { |
| 157 |
r io.Reader |
| 158 |
n int64 |
| 159 |
} |
| 160 |
|
| 161 |
func (l *spanReader) Read(p []byte) (n int, err error) { |
| 162 |
if l.n <= 0 { |
| 163 |
return 0, io.EOF |
| 164 |
} |
| 165 |
if int64(len(p)) > l.n { |
| 166 |
p = p[0:l.n] |
| 167 |
} |
| 168 |
n, err = l.r.Read(p) |
| 169 |
l.n -= int64(n) |
| 170 |
if l.n > 0 && err == io.EOF { |
| 171 |
err = io.ErrUnexpectedEOF |
| 172 |
} |
| 173 |
return |
| 174 |
} |
| 175 |
|
| 176 |
// readHeader parses a packet header and returns an io.Reader which will return |
| 177 |
// the contents of the packet. See RFC 4880, section 4.2. |
| 178 |
func readHeader(r io.Reader) (tag packetType, length int64, contents io.Reader, err error) { |
| 179 |
var buf [4]byte |
| 180 |
_, err = io.ReadFull(r, buf[:1]) |
| 181 |
if err != nil { |
| 182 |
return |
| 183 |
} |
| 184 |
if buf[0]&0x80 == 0 { |
| 185 |
err = errors.StructuralError("tag byte does not have MSB set") |
| 186 |
return |
| 187 |
} |
| 188 |
if buf[0]&0x40 == 0 { |
| 189 |
// Old format packet |
| 190 |
tag = packetType((buf[0] & 0x3f) >> 2) |
| 191 |
lengthType := buf[0] & 3 |
| 192 |
if lengthType == 3 { |
| 193 |
length = -1 |
| 194 |
contents = r |
| 195 |
return |
| 196 |
} |
| 197 |
lengthBytes := 1 << lengthType |
| 198 |
_, err = readFull(r, buf[0:lengthBytes]) |
| 199 |
if err != nil { |
| 200 |
return |
| 201 |
} |
| 202 |
for i := 0; i < lengthBytes; i++ { |
| 203 |
length <<= 8 |
| 204 |
length |= int64(buf[i]) |
| 205 |
} |
| 206 |
contents = &spanReader{r, length} |
| 207 |
return |
| 208 |
} |
| 209 |
|
| 210 |
// New format packet |
| 211 |
tag = packetType(buf[0] & 0x3f) |
| 212 |
length, isPartial, err := readLength(r) |
| 213 |
if err != nil { |
| 214 |
return |
| 215 |
} |
| 216 |
if isPartial { |
| 217 |
contents = &partialLengthReader{ |
| 218 |
remaining: length, |
| 219 |
isPartial: true, |
| 220 |
r: r, |
| 221 |
} |
| 222 |
length = -1 |
| 223 |
} else { |
| 224 |
contents = &spanReader{r, length} |
| 225 |
} |
| 226 |
return |
| 227 |
} |
| 228 |
|
| 229 |
// serializeHeader writes an OpenPGP packet header to w. See RFC 4880, section |
| 230 |
// 4.2. |
| 231 |
func serializeHeader(w io.Writer, ptype packetType, length int) (err error) { |
| 232 |
err = serializeType(w, ptype) |
| 233 |
if err != nil { |
| 234 |
return |
| 235 |
} |
| 236 |
return serializeLength(w, length) |
| 237 |
} |
| 238 |
|
| 239 |
// serializeType writes an OpenPGP packet type to w. See RFC 4880, section |
| 240 |
// 4.2. |
| 241 |
func serializeType(w io.Writer, ptype packetType) (err error) { |
| 242 |
var buf [1]byte |
| 243 |
buf[0] = 0x80 | 0x40 | byte(ptype) |
| 244 |
_, err = w.Write(buf[:]) |
| 245 |
return |
| 246 |
} |
| 247 |
|
| 248 |
// serializeLength writes an OpenPGP packet length to w. See RFC 4880, section |
| 249 |
// 4.2.2. |
| 250 |
func serializeLength(w io.Writer, length int) (err error) { |
| 251 |
var buf [5]byte |
| 252 |
var n int |
| 253 |
|
| 254 |
if length < 192 { |
| 255 |
buf[0] = byte(length) |
| 256 |
n = 1 |
| 257 |
} else if length < 8384 { |
| 258 |
length -= 192 |
| 259 |
buf[0] = 192 + byte(length>>8) |
| 260 |
buf[1] = byte(length) |
| 261 |
n = 2 |
| 262 |
} else { |
| 263 |
buf[0] = 255 |
| 264 |
buf[1] = byte(length >> 24) |
| 265 |
buf[2] = byte(length >> 16) |
| 266 |
buf[3] = byte(length >> 8) |
| 267 |
buf[4] = byte(length) |
| 268 |
n = 5 |
| 269 |
} |
| 270 |
|
| 271 |
_, err = w.Write(buf[:n]) |
| 272 |
return |
| 273 |
} |
| 274 |
|
| 275 |
// serializeStreamHeader writes an OpenPGP packet header to w where the |
| 276 |
// length of the packet is unknown. It returns a io.WriteCloser which can be |
| 277 |
// used to write the contents of the packet. See RFC 4880, section 4.2. |
| 278 |
func serializeStreamHeader(w io.WriteCloser, ptype packetType) (out io.WriteCloser, err error) { |
| 279 |
err = serializeType(w, ptype) |
| 280 |
if err != nil { |
| 281 |
return |
| 282 |
} |
| 283 |
out = &partialLengthWriter{w: w} |
| 284 |
return |
| 285 |
} |
| 286 |
|
| 287 |
// Packet represents an OpenPGP packet. Users are expected to try casting |
| 288 |
// instances of this interface to specific packet types. |
| 289 |
type Packet interface { |
| 290 |
parse(io.Reader) error |
| 291 |
} |
| 292 |
|
| 293 |
// consumeAll reads from the given Reader until error, returning the number of |
| 294 |
// bytes read. |
| 295 |
func consumeAll(r io.Reader) (n int64, err error) { |
| 296 |
var m int |
| 297 |
var buf [1024]byte |
| 298 |
|
| 299 |
for { |
| 300 |
m, err = r.Read(buf[:]) |
| 301 |
n += int64(m) |
| 302 |
if err == io.EOF { |
| 303 |
err = nil |
| 304 |
return |
| 305 |
} |
| 306 |
if err != nil { |
| 307 |
return |
| 308 |
} |
| 309 |
} |
| 310 |
} |
| 311 |
|
| 312 |
// packetType represents the numeric ids of the different OpenPGP packet types. See |
| 313 |
// http://www.iana.org/assignments/pgp-parameters/pgp-parameters.xhtml#pgp-parameters-2 |
| 314 |
type packetType uint8 |
| 315 |
|
| 316 |
const ( |
| 317 |
packetTypeEncryptedKey packetType = 1 |
| 318 |
packetTypeSignature packetType = 2 |
| 319 |
packetTypeSymmetricKeyEncrypted packetType = 3 |
| 320 |
packetTypeOnePassSignature packetType = 4 |
| 321 |
packetTypePrivateKey packetType = 5 |
| 322 |
packetTypePublicKey packetType = 6 |
| 323 |
packetTypePrivateSubkey packetType = 7 |
| 324 |
packetTypeCompressed packetType = 8 |
| 325 |
packetTypeSymmetricallyEncrypted packetType = 9 |
| 326 |
packetTypeMarker packetType = 10 |
| 327 |
packetTypeLiteralData packetType = 11 |
| 328 |
packetTypeTrust packetType = 12 |
| 329 |
packetTypeUserId packetType = 13 |
| 330 |
packetTypePublicSubkey packetType = 14 |
| 331 |
packetTypeUserAttribute packetType = 17 |
| 332 |
packetTypeSymmetricallyEncryptedIntegrityProtected packetType = 18 |
| 333 |
packetTypeAEADEncrypted packetType = 20 |
| 334 |
packetPadding packetType = 21 |
| 335 |
) |
| 336 |
|
| 337 |
// EncryptedDataPacket holds encrypted data. It is currently implemented by |
| 338 |
// SymmetricallyEncrypted and AEADEncrypted. |
| 339 |
type EncryptedDataPacket interface { |
| 340 |
Decrypt(CipherFunction, []byte) (io.ReadCloser, error) |
| 341 |
} |
| 342 |
|
| 343 |
// Read reads a single OpenPGP packet from the given io.Reader. If there is an |
| 344 |
// error parsing a packet, the whole packet is consumed from the input. |
| 345 |
func Read(r io.Reader) (p Packet, err error) { |
| 346 |
tag, len, contents, err := readHeader(r) |
| 347 |
if err != nil { |
| 348 |
return |
| 349 |
} |
| 350 |
|
| 351 |
switch tag { |
| 352 |
case packetTypeEncryptedKey: |
| 353 |
p = new(EncryptedKey) |
| 354 |
case packetTypeSignature: |
| 355 |
p = new(Signature) |
| 356 |
case packetTypeSymmetricKeyEncrypted: |
| 357 |
p = new(SymmetricKeyEncrypted) |
| 358 |
case packetTypeOnePassSignature: |
| 359 |
p = new(OnePassSignature) |
| 360 |
case packetTypePrivateKey, packetTypePrivateSubkey: |
| 361 |
pk := new(PrivateKey) |
| 362 |
if tag == packetTypePrivateSubkey { |
| 363 |
pk.IsSubkey = true |
| 364 |
} |
| 365 |
p = pk |
| 366 |
case packetTypePublicKey, packetTypePublicSubkey: |
| 367 |
isSubkey := tag == packetTypePublicSubkey |
| 368 |
p = &PublicKey{IsSubkey: isSubkey} |
| 369 |
case packetTypeCompressed: |
| 370 |
p = new(Compressed) |
| 371 |
case packetTypeSymmetricallyEncrypted: |
| 372 |
p = new(SymmetricallyEncrypted) |
| 373 |
case packetTypeLiteralData: |
| 374 |
p = new(LiteralData) |
| 375 |
case packetTypeUserId: |
| 376 |
p = new(UserId) |
| 377 |
case packetTypeUserAttribute: |
| 378 |
p = new(UserAttribute) |
| 379 |
case packetTypeSymmetricallyEncryptedIntegrityProtected: |
| 380 |
se := new(SymmetricallyEncrypted) |
| 381 |
se.IntegrityProtected = true |
| 382 |
p = se |
| 383 |
case packetTypeAEADEncrypted: |
| 384 |
p = new(AEADEncrypted) |
| 385 |
case packetPadding: |
| 386 |
p = Padding(len) |
| 387 |
case packetTypeMarker: |
| 388 |
p = new(Marker) |
| 389 |
case packetTypeTrust: |
| 390 |
// Not implemented, just consume |
| 391 |
err = errors.UnknownPacketTypeError(tag) |
| 392 |
default: |
| 393 |
// Packet Tags from 0 to 39 are critical. |
| 394 |
// Packet Tags from 40 to 63 are non-critical. |
| 395 |
if tag < 40 { |
| 396 |
err = errors.CriticalUnknownPacketTypeError(tag) |
| 397 |
} else { |
| 398 |
err = errors.UnknownPacketTypeError(tag) |
| 399 |
} |
| 400 |
} |
| 401 |
if p != nil { |
| 402 |
err = p.parse(contents) |
| 403 |
} |
| 404 |
if err != nil { |
| 405 |
consumeAll(contents) |
| 406 |
} |
| 407 |
return |
| 408 |
} |
| 409 |
|
| 410 |
// ReadWithCheck reads a single OpenPGP message packet from the given io.Reader. If there is an |
| 411 |
// error parsing a packet, the whole packet is consumed from the input. |
| 412 |
// ReadWithCheck additionally checks if the OpenPGP message packet sequence adheres |
| 413 |
// to the packet composition rules in rfc4880, if not throws an error. |
| 414 |
func ReadWithCheck(r io.Reader, sequence *SequenceVerifier) (p Packet, msgErr error, err error) { |
| 415 |
tag, len, contents, err := readHeader(r) |
| 416 |
if err != nil { |
| 417 |
return |
| 418 |
} |
| 419 |
switch tag { |
| 420 |
case packetTypeEncryptedKey: |
| 421 |
msgErr = sequence.Next(ESKSymbol) |
| 422 |
p = new(EncryptedKey) |
| 423 |
case packetTypeSignature: |
| 424 |
msgErr = sequence.Next(SigSymbol) |
| 425 |
p = new(Signature) |
| 426 |
case packetTypeSymmetricKeyEncrypted: |
| 427 |
msgErr = sequence.Next(ESKSymbol) |
| 428 |
p = new(SymmetricKeyEncrypted) |
| 429 |
case packetTypeOnePassSignature: |
| 430 |
msgErr = sequence.Next(OPSSymbol) |
| 431 |
p = new(OnePassSignature) |
| 432 |
case packetTypeCompressed: |
| 433 |
msgErr = sequence.Next(CompSymbol) |
| 434 |
p = new(Compressed) |
| 435 |
case packetTypeSymmetricallyEncrypted: |
| 436 |
msgErr = sequence.Next(EncSymbol) |
| 437 |
p = new(SymmetricallyEncrypted) |
| 438 |
case packetTypeLiteralData: |
| 439 |
msgErr = sequence.Next(LDSymbol) |
| 440 |
p = new(LiteralData) |
| 441 |
case packetTypeSymmetricallyEncryptedIntegrityProtected: |
| 442 |
msgErr = sequence.Next(EncSymbol) |
| 443 |
se := new(SymmetricallyEncrypted) |
| 444 |
se.IntegrityProtected = true |
| 445 |
p = se |
| 446 |
case packetTypeAEADEncrypted: |
| 447 |
msgErr = sequence.Next(EncSymbol) |
| 448 |
p = new(AEADEncrypted) |
| 449 |
case packetPadding: |
| 450 |
p = Padding(len) |
| 451 |
case packetTypeMarker: |
| 452 |
p = new(Marker) |
| 453 |
case packetTypeTrust: |
| 454 |
// Not implemented, just consume |
| 455 |
err = errors.UnknownPacketTypeError(tag) |
| 456 |
case packetTypePrivateKey, |
| 457 |
packetTypePrivateSubkey, |
| 458 |
packetTypePublicKey, |
| 459 |
packetTypePublicSubkey, |
| 460 |
packetTypeUserId, |
| 461 |
packetTypeUserAttribute: |
| 462 |
msgErr = sequence.Next(UnknownSymbol) |
| 463 |
consumeAll(contents) |
| 464 |
default: |
| 465 |
// Packet Tags from 0 to 39 are critical. |
| 466 |
// Packet Tags from 40 to 63 are non-critical. |
| 467 |
if tag < 40 { |
| 468 |
err = errors.CriticalUnknownPacketTypeError(tag) |
| 469 |
} else { |
| 470 |
err = errors.UnknownPacketTypeError(tag) |
| 471 |
} |
| 472 |
} |
| 473 |
if p != nil { |
| 474 |
err = p.parse(contents) |
| 475 |
} |
| 476 |
if err != nil { |
| 477 |
consumeAll(contents) |
| 478 |
} |
| 479 |
return |
| 480 |
} |
| 481 |
|
| 482 |
// SignatureType represents the different semantic meanings of an OpenPGP |
| 483 |
// signature. See RFC 4880, section 5.2.1. |
| 484 |
type SignatureType uint8 |
| 485 |
|
| 486 |
const ( |
| 487 |
SigTypeBinary SignatureType = 0x00 |
| 488 |
SigTypeText SignatureType = 0x01 |
| 489 |
SigTypeGenericCert SignatureType = 0x10 |
| 490 |
SigTypePersonaCert SignatureType = 0x11 |
| 491 |
SigTypeCasualCert SignatureType = 0x12 |
| 492 |
SigTypePositiveCert SignatureType = 0x13 |
| 493 |
SigTypeSubkeyBinding SignatureType = 0x18 |
| 494 |
SigTypePrimaryKeyBinding SignatureType = 0x19 |
| 495 |
SigTypeDirectSignature SignatureType = 0x1F |
| 496 |
SigTypeKeyRevocation SignatureType = 0x20 |
| 497 |
SigTypeSubkeyRevocation SignatureType = 0x28 |
| 498 |
SigTypeCertificationRevocation SignatureType = 0x30 |
| 499 |
) |
| 500 |
|
| 501 |
// PublicKeyAlgorithm represents the different public key system specified for |
| 502 |
// OpenPGP. See |
| 503 |
// http://www.iana.org/assignments/pgp-parameters/pgp-parameters.xhtml#pgp-parameters-12 |
| 504 |
type PublicKeyAlgorithm uint8 |
| 505 |
|
| 506 |
const ( |
| 507 |
PubKeyAlgoRSA PublicKeyAlgorithm = 1 |
| 508 |
PubKeyAlgoElGamal PublicKeyAlgorithm = 16 |
| 509 |
PubKeyAlgoDSA PublicKeyAlgorithm = 17 |
| 510 |
// RFC 6637, Section 5. |
| 511 |
PubKeyAlgoECDH PublicKeyAlgorithm = 18 |
| 512 |
PubKeyAlgoECDSA PublicKeyAlgorithm = 19 |
| 513 |
// https://www.ietf.org/archive/id/draft-koch-eddsa-for-openpgp-04.txt |
| 514 |
PubKeyAlgoEdDSA PublicKeyAlgorithm = 22 |
| 515 |
// https://datatracker.ietf.org/doc/html/draft-ietf-openpgp-crypto-refresh |
| 516 |
PubKeyAlgoX25519 PublicKeyAlgorithm = 25 |
| 517 |
PubKeyAlgoX448 PublicKeyAlgorithm = 26 |
| 518 |
PubKeyAlgoEd25519 PublicKeyAlgorithm = 27 |
| 519 |
PubKeyAlgoEd448 PublicKeyAlgorithm = 28 |
| 520 |
|
| 521 |
// Deprecated in RFC 4880, Section 13.5. Use key flags instead. |
| 522 |
PubKeyAlgoRSAEncryptOnly PublicKeyAlgorithm = 2 |
| 523 |
PubKeyAlgoRSASignOnly PublicKeyAlgorithm = 3 |
| 524 |
|
| 525 |
// PQC DSA algorithms |
| 526 |
PubKeyAlgoMldsa65Ed25519 = 30 |
| 527 |
PubKeyAlgoMldsa87Ed448 = 31 |
| 528 |
PubKeyAlgoSlhdsaShake128s = 32 |
| 529 |
PubKeyAlgoSlhdsaShake128f = 33 |
| 530 |
PubKeyAlgoSlhdsaShake256s = 34 |
| 531 |
|
| 532 |
// PQC KEM algorithms |
| 533 |
PubKeyAlgoMlkem768X25519 = 35 |
| 534 |
PubKeyAlgoMlkem1024X448 = 36 |
| 535 |
) |
| 536 |
|
| 537 |
// CanEncrypt returns true if it's possible to encrypt a message to a public |
| 538 |
// key of the given type. |
| 539 |
func (pka PublicKeyAlgorithm) CanEncrypt() bool { |
| 540 |
switch pka { |
| 541 |
case PubKeyAlgoRSA, PubKeyAlgoRSAEncryptOnly, PubKeyAlgoElGamal, PubKeyAlgoECDH, PubKeyAlgoX25519, PubKeyAlgoX448, |
| 542 |
PubKeyAlgoMlkem768X25519, PubKeyAlgoMlkem1024X448: |
| 543 |
return true |
| 544 |
} |
| 545 |
return false |
| 546 |
} |
| 547 |
|
| 548 |
// CanSign returns true if it's possible for a public key of the given type to |
| 549 |
// sign a message. |
| 550 |
func (pka PublicKeyAlgorithm) CanSign() bool { |
| 551 |
switch pka { |
| 552 |
case PubKeyAlgoRSA, PubKeyAlgoRSASignOnly, PubKeyAlgoDSA, PubKeyAlgoECDSA, PubKeyAlgoEdDSA, |
| 553 |
PubKeyAlgoEd25519, PubKeyAlgoEd448, |
| 554 |
PubKeyAlgoMldsa65Ed25519, PubKeyAlgoMldsa87Ed448, |
| 555 |
PubKeyAlgoSlhdsaShake128s, PubKeyAlgoSlhdsaShake128f, PubKeyAlgoSlhdsaShake256s: |
| 556 |
return true |
| 557 |
} |
| 558 |
return false |
| 559 |
} |
| 560 |
|
| 561 |
// CipherFunction represents the different block ciphers specified for OpenPGP. See |
| 562 |
// http://www.iana.org/assignments/pgp-parameters/pgp-parameters.xhtml#pgp-parameters-13 |
| 563 |
type CipherFunction algorithm.CipherFunction |
| 564 |
|
| 565 |
const ( |
| 566 |
Cipher3DES CipherFunction = 2 |
| 567 |
CipherCAST5 CipherFunction = 3 |
| 568 |
CipherAES128 CipherFunction = 7 |
| 569 |
CipherAES192 CipherFunction = 8 |
| 570 |
CipherAES256 CipherFunction = 9 |
| 571 |
) |
| 572 |
|
| 573 |
// KeySize returns the key size, in bytes, of cipher. |
| 574 |
func (cipher CipherFunction) KeySize() int { |
| 575 |
return algorithm.CipherFunction(cipher).KeySize() |
| 576 |
} |
| 577 |
|
| 578 |
// IsSupported returns true if the cipher is supported from the library |
| 579 |
func (cipher CipherFunction) IsSupported() bool { |
| 580 |
return algorithm.CipherFunction(cipher).KeySize() > 0 |
| 581 |
} |
| 582 |
|
| 583 |
// blockSize returns the block size, in bytes, of cipher. |
| 584 |
func (cipher CipherFunction) blockSize() int { |
| 585 |
return algorithm.CipherFunction(cipher).BlockSize() |
| 586 |
} |
| 587 |
|
| 588 |
// new returns a fresh instance of the given cipher. |
| 589 |
func (cipher CipherFunction) new(key []byte) (block cipher.Block) { |
| 590 |
return algorithm.CipherFunction(cipher).New(key) |
| 591 |
} |
| 592 |
|
| 593 |
// padToKeySize left-pads a MPI with zeroes to match the length of the |
| 594 |
// specified RSA public. |
| 595 |
func padToKeySize(pub *rsa.PublicKey, b []byte) []byte { |
| 596 |
k := (pub.N.BitLen() + 7) / 8 |
| 597 |
if len(b) >= k { |
| 598 |
return b |
| 599 |
} |
| 600 |
bb := make([]byte, k) |
| 601 |
copy(bb[len(bb)-len(b):], b) |
| 602 |
return bb |
| 603 |
} |
| 604 |
|
| 605 |
// CompressionAlgo Represents the different compression algorithms |
| 606 |
// supported by OpenPGP (except for BZIP2, which is not currently |
| 607 |
// supported). See Section 9.3 of RFC 4880. |
| 608 |
type CompressionAlgo uint8 |
| 609 |
|
| 610 |
const ( |
| 611 |
CompressionNone CompressionAlgo = 0 |
| 612 |
CompressionZIP CompressionAlgo = 1 |
| 613 |
CompressionZLIB CompressionAlgo = 2 |
| 614 |
) |
| 615 |
|
| 616 |
// AEADMode represents the different Authenticated Encryption with Associated |
| 617 |
// Data specified for OpenPGP. |
| 618 |
// See https://www.ietf.org/archive/id/draft-ietf-openpgp-crypto-refresh-07.html#section-9.6 |
| 619 |
type AEADMode algorithm.AEADMode |
| 620 |
|
| 621 |
const ( |
| 622 |
AEADModeEAX AEADMode = 1 |
| 623 |
AEADModeOCB AEADMode = 2 |
| 624 |
AEADModeGCM AEADMode = 3 |
| 625 |
) |
| 626 |
|
| 627 |
func (mode AEADMode) IvLength() int { |
| 628 |
return algorithm.AEADMode(mode).NonceLength() |
| 629 |
} |
| 630 |
|
| 631 |
func (mode AEADMode) TagLength() int { |
| 632 |
return algorithm.AEADMode(mode).TagLength() |
| 633 |
} |
| 634 |
|
| 635 |
// IsSupported returns true if the aead mode is supported from the library |
| 636 |
func (mode AEADMode) IsSupported() bool { |
| 637 |
return algorithm.AEADMode(mode).TagLength() > 0 |
| 638 |
} |
| 639 |
|
| 640 |
// new returns a fresh instance of the given mode. |
| 641 |
func (mode AEADMode) new(block cipher.Block) (cipher.AEAD, error) { |
| 642 |
return algorithm.AEADMode(mode).New(block) |
| 643 |
} |
| 644 |
|
| 645 |
// ReasonForRevocation represents a revocation reason code as per RFC4880 |
| 646 |
// section 5.2.3.23. |
| 647 |
type ReasonForRevocation uint8 |
| 648 |
|
| 649 |
const ( |
| 650 |
NoReason ReasonForRevocation = 0 |
| 651 |
KeySuperseded ReasonForRevocation = 1 |
| 652 |
KeyCompromised ReasonForRevocation = 2 |
| 653 |
KeyRetired ReasonForRevocation = 3 |
| 654 |
UserIDNotValid ReasonForRevocation = 32 |
| 655 |
Unknown ReasonForRevocation = 200 |
| 656 |
) |
| 657 |
|
| 658 |
func NewReasonForRevocation(value byte) ReasonForRevocation { |
| 659 |
if value < 4 || value == 32 { |
| 660 |
return ReasonForRevocation(value) |
| 661 |
} |
| 662 |
return Unknown |
| 663 |
} |
| 664 |
|
| 665 |
// Curve is a mapping to supported ECC curves for key generation. |
| 666 |
// See https://www.ietf.org/archive/id/draft-ietf-openpgp-crypto-refresh-06.html#name-curve-specific-wire-formats |
| 667 |
type Curve string |
| 668 |
|
| 669 |
const ( |
| 670 |
Curve25519 Curve = "Curve25519" |
| 671 |
Curve448 Curve = "Curve448" |
| 672 |
CurveNistP256 Curve = "P256" |
| 673 |
CurveNistP384 Curve = "P384" |
| 674 |
CurveNistP521 Curve = "P521" |
| 675 |
CurveSecP256k1 Curve = "SecP256k1" |
| 676 |
CurveBrainpoolP256 Curve = "BrainpoolP256" |
| 677 |
CurveBrainpoolP384 Curve = "BrainpoolP384" |
| 678 |
CurveBrainpoolP512 Curve = "BrainpoolP512" |
| 679 |
) |
| 680 |
|
| 681 |
// TrustLevel represents a trust level per RFC4880 5.2.3.13 |
| 682 |
type TrustLevel uint8 |
| 683 |
|
| 684 |
// TrustAmount represents a trust amount per RFC4880 5.2.3.13 |
| 685 |
type TrustAmount uint8 |
| 686 |
|
| 687 |
const ( |
| 688 |
// versionSize is the length in bytes of the version value. |
| 689 |
versionSize = 1 |
| 690 |
// algorithmSize is the length in bytes of the key algorithm value. |
| 691 |
algorithmSize = 1 |
| 692 |
// keyVersionSize is the length in bytes of the key version value |
| 693 |
keyVersionSize = 1 |
| 694 |
// keyIdSize is the length in bytes of the key identifier value. |
| 695 |
keyIdSize = 8 |
| 696 |
// timestampSize is the length in bytes of encoded timestamps. |
| 697 |
timestampSize = 4 |
| 698 |
// fingerprintSizeV6 is the length in bytes of the key fingerprint in v6. |
| 699 |
fingerprintSizeV6 = 32 |
| 700 |
// fingerprintSize is the length in bytes of the key fingerprint. |
| 701 |
fingerprintSize = 20 |
| 702 |
) |