From a4fb479db7152864651d408f04da5aa58c666b86 Mon Sep 17 00:00:00 2001
From: Nickolay Olshevsky <o.nickolay@gmail.com>
Date: Tue, 30 Jan 2024 19:10:13 +0200
Subject: [PATCH] Update pgp_fingerprint_t with vector constructor and
 size_valid() method.

---
 include/repgp/repgp_def.h |  2 +
 src/lib/types.h           | 79 ++++++++++++++++++---------------------
 2 files changed, 38 insertions(+), 43 deletions(-)

diff --git a/include/repgp/repgp_def.h b/include/repgp/repgp_def.h
index 2977ef4a90..88f06d5541 100644
--- a/include/repgp/repgp_def.h
+++ b/include/repgp/repgp_def.h
@@ -95,6 +95,7 @@
 #define PGP_KEY_ID_SIZE 8
 
 /* Size of the fingerprint */
+#define PGP_FINGERPRINT_V3_SIZE 16
 #define PGP_FINGERPRINT_V4_SIZE 20
 #define PGP_FINGERPRINT_V5_SIZE 32
 #define PGP_MAX_FINGERPRINT_SIZE 32
@@ -105,6 +106,7 @@ static_assert(PGP_MAX_FINGERPRINT_SIZE >= PGP_FINGERPRINT_V5_SIZE, "FP size mism
 #if defined(ENABLE_CRYPTO_REFRESH)
 #define PGP_FINGERPRINT_V6_SIZE 32
 static_assert(PGP_MAX_FINGERPRINT_SIZE >= PGP_FINGERPRINT_V6_SIZE, "FP size mismatch.");
+static_assert(PGP_FINGERPRINT_V5_SIZE == PGP_FINGERPRINT_V6_SIZE, "FP size mismatch.");
 #endif
 
 /* SEIPDv2 salt length */
diff --git a/src/lib/types.h b/src/lib/types.h
index 36bdb3a80b..f14459ea91 100644
--- a/src/lib/types.h
+++ b/src/lib/types.h
@@ -1,52 +1,27 @@
 /*
- * Copyright (c) 2017-2021, [Ribose Inc](https://www.ribose.com).
- * Copyright (c) 2009 The NetBSD Foundation, Inc.
+ * Copyright (c) 2017-2023, [Ribose Inc](https://www.ribose.com).
  * All rights reserved.
  *
- * This code is originally derived from software contributed to
- * The NetBSD Foundation by Alistair Crooks (agc@netbsd.org), and
- * carried further by Ribose Inc (https://www.ribose.com).
+ * Redistribution and use in source and binary forms, with or without modification,
+ * are permitted provided that the following conditions are met:
  *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- *    notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- *    notice, this list of conditions and the following disclaimer in the
- *    documentation and/or other materials provided with the distribution.
+ * 1.  Redistributions of source code must retain the above copyright notice,
+ *     this list of conditions and the following disclaimer.
  *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
- * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
- * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
- * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS
- * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
- * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
- * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
- * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
- * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
- * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
- * POSSIBILITY OF SUCH DAMAGE.
- */
-/*
- * Copyright (c) 2005-2008 Nominet UK (www.nic.uk)
- * All rights reserved.
- * Contributors: Ben Laurie, Rachel Willmer. The Contributors have asserted
- * their moral rights under the UK Copyright Design and Patents Act 1988 to
- * be recorded as the authors of this copyright work.
- *
- * Licensed under the Apache License, Version 2.0 (the "License"); you may not
- * use this file except in compliance with the License.
- *
- * You may obtain a copy of the License at
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * 2.  Redistributions in binary form must reproduce the above copyright notice,
+ *     this list of conditions and the following disclaimer in the documentation
+ *     and/or other materials provided with the distribution.
  *
- * See the License for the specific language governing permissions and
- * limitations under the License.
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 #ifndef TYPES_H_
 #define TYPES_H_
@@ -56,6 +31,7 @@
 #include <vector>
 #include <array>
 #include <cstring>
+#include <stdexcept>
 #include <type_traits>
 
 #include <rnp/rnp_def.h>
@@ -106,6 +82,23 @@ typedef struct pgp_fingerprint_t {
     unsigned length;
     bool     operator==(const pgp_fingerprint_t &src) const;
     bool     operator!=(const pgp_fingerprint_t &src) const;
+
+    pgp_fingerprint_t() = default;
+    pgp_fingerprint_t(const std::vector<uint8_t> &src)
+    {
+        if (!size_valid(src.size())) {
+            throw std::invalid_argument("src");
+        }
+        memcpy(fingerprint, src.data(), src.size());
+        length = src.size();
+    }
+
+    static bool
+    size_valid(size_t size)
+    {
+        return (size == PGP_FINGERPRINT_V4_SIZE) || (size == PGP_FINGERPRINT_V3_SIZE) ||
+               (size == PGP_FINGERPRINT_V5_SIZE);
+    }
 } pgp_fingerprint_t;
 
 typedef std::array<uint8_t, PGP_KEY_GRIP_SIZE> pgp_sig_id_t;